package Ġcom . ade vint a . android . bar ista . interaction Ċ Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . pm . PackageManager Ċ import Ġandroid . os . Build Ċ import Ġandroid . util . Log Ċ import Ġandroidx . test . core . app . Application Provider . getApplicationContext Ċ import Ġandroidx . test . platform . app . Instrumentation Registry . get Instrumentation Ċ import Ġandroidx . test . uia utom ator . Ui Device Ċ import Ġandroidx . test . uia utom ator . Ui Object NotFoundException Ċ import Ġandroidx . test . uia utom ator . Ui Selector Ċ import Ġcom . ade vint a . android . bar ista . interaction . Bar ista Sleep Interactions . sleep Thread Ċ Ċ object ĠPermission Gran ter Ġ{ ĊĊĠ Ġprivate Ġval ĠPER MISSION S _ DIALOG _ DELAY Ġ= Ġ 3 0 0 0 ĊĊĠ Ġprivate Ġval ĠPER MISSION _ DIALOG _ ALLOW _ FO REG ROUND _ IDS Ġ= ĠlistOf ( ĊĠĠĠ Ġ" com . android . permission controller : id / permission _ allow _ foreground _ only _ button ", ĊĠĠĠ Ġ" com . android . permission controller : id / permission _ allow _ button ", ĊĠĠĠ Ġ" com . android . package installer : id / permission _ allow _ button " ĊĠ Ġ) ĊĊĠ Ġprivate Ġval ĠPER MISSION _ DIALOG _ ALLOW _ ONE _ TIME _ IDS Ġ= ĠlistOf ( ĊĠĠĠ Ġ" com . android . permission controller : id / permission _ allow _ one _ time _ button ", ĊĠĠĠ Ġ" com . android . permission controller : id / permission _ allow _ button ", ĊĠĠĠ Ġ" com . android . package installer : id / permission _ allow _ button " ĊĠ Ġ) ĊĊĠ Ġ// ĠIn ĠAPI Ġ 3 0 Ġthe Ġ" Den y " Ġbutton Ġhas Ġthe Ġfirst ĠID Ġwhen Ġit Ġis Ġshown Ġthe Ġfirst Ġtime . ĠSecond Ġtime ĊĠ Ġ// Ġit Ġhas Ġthe Ġsecond ĠID Ġ( do Ġnot Ġask Ġagain ), Ġalthough Ġtext Ġis Ġthe Ġsame . ĠIn ĠAPI Ġ 2 9 Ġthe Ġbuttons ĊĠ Ġ// Ġare Ġshown Ġseparately . ĊĠ Ġprivate Ġval ĠPER MISSION _ DIALOG _ DEN Y _ IDS Ġ= ĠlistOf ( ĊĠĠĠ Ġ" com . android . permission controller : id / permission _ deny _ button ", ĊĠĠĠ Ġ" com . android . permission controller : id / permission _ deny _ and _ dont _ ask _ again _ button ", ĊĠĠĠ Ġ" com . android . package installer : id / permission _ deny _ button " ĊĠ Ġ) ĊĊĠ Ġprivate Ġfun ĠList < String >. to Permission Button Regex () Ġ= Ġjoin ToString ( ĊĠĠĠ Ġprefix Ġ= Ġ"^ (", ĊĠĠĠ Ġseparator Ġ= Ġ"| ", ĊĠĠĠ Ġpostfix Ġ= Ġ") $" ĊĠ Ġ) Ġ{ Ġit Ġ} ĊĊĠ Ġ@ Jvm Static ĊĠ Ġfun Ġallow Permissions IfNeeded ( permission Needed : ĠString ) Ġ{ ĊĠĠĠ Ġclick Permission Dialog Button ( permission Needed , ĠPER MISSION _ DIALOG _ ALLOW _ FO REG ROUND _ IDS . to Permission Button Regex ()) ĊĠ Ġ} ĊĊĠ Ġ@ Jvm Static ĊĠ Ġfun Ġallow Permission One Time ( permission Needed : ĠString ) Ġ{ ĊĠĠĠ Ġclick Permission Dialog Button ( permission Needed , ĠPER MISSION _ DIALOG _ ALLOW _ ONE _ TIME _ IDS . to Permission Button Regex ()) ĊĠ Ġ} ĊĊĠ Ġ@ Jvm Static ĊĠ Ġfun Ġdeny Permissions ( permission Requested : ĠString ) Ġ{ ĊĠĠĠ Ġclick Permission Dialog Button ( permission Requested , ĠPER MISSION _ DIALOG _ DEN Y _ IDS . to Permission Button Regex ()) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġclick Permission Dialog Button ( permission Needed : ĠString , Ġpermissions Ids : ĠString ) Ġ{ ĊĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠ Ġif Ġ( Build . VERSION . SDK _ INT Ġ>= ĠBuild . VERSION _ CODES . M Ġ&& Ġ! has Needed Permission ( ĊĠĠĠĠĠĠĠĠĠ Ġget ApplicationContext (), ĊĠĠĠĠĠĠĠĠĠ Ġpermission Needed ĊĠĠĠĠĠĠĠ Ġ)) Ġ{ ĊĠĠĠĠĠĠĠ Ġsleep Thread ( PERMISSION S _ DIALOG _ DELAY . to Long ()) ĊĠĠĠĠĠĠĠ Ġval Ġdevice Ġ= ĠUi Device . getInstance ( get Instrumentation ()) ĊĊĠĠĠĠĠĠĠ Ġval Ġallow Permissions Ġ= Ġdevice . find Object ( ĊĠĠĠĠĠĠĠĠĠ ĠUi Selector () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. click able ( true ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. check able ( false ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. resourceId Matches ( permissions Ids ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġif Ġ( allow Permissions . exists ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġallow Permissions . click () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ġcatch Ġ( e : ĠUi Object NotFoundException ) Ġ{ ĊĠĠĠĠĠ ĠLog . e (" Bar ista ", Ġ" There Ġis Ġno Ġpermissions Ġdialog Ġto Ġinteract Ġwith ", Ġe ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġhas Needed Permission ( context : ĠContext , Ġpermission Needed : ĠString ): ĠBoolean Ġ{ ĊĠĠĠ Ġval Ġpermission Status Ġ= Ġcheck Self Permission ( context , Ġpermission Needed ) ĊĠĠĠ Ġreturn Ġpermission Status Ġ== ĠPackage Manager . PERMISSION _ GRAN TED ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġcheck Self Permission ( context : ĠContext , Ġpermission : ĠString ): ĠInt Ġ{ ĊĠĠĠ Ġreturn Ġcontext . check Permission ( permission , Ġandroid . os . Process . my Pid (), Ġandroid . os . Process . my Uid ()) ĊĠ Ġ} Ċ } Ċ