package Ġcom . example . android . analyzer . core . k tools Ċ Ċ import Ġandroid . content . pm . PackageManager Ċ import Ġandroid . support . v 4 . app . Activity Compat Ċ import Ġandroid . support . v 4 . app . Fragment Ċ import Ġandroid . support . v 4 . app . Fragment Activity Ċ import Ġandroid . support . v 4 . content . Context Compat Ċ Ċ abstract Ġclass ĠPermission Manager Ġ{ Ċ ĉ private Ġvar Ġlast RequestId Ġ= Ġ 0 Ċ ĉ private Ġval Ġpermission Requests Ġ= ĠHashMap < Int , ĠPermission Request >() Ċ ĉ fun Ġrequest Permission ( permission Request : ĠPermission Request ) Ġ{ Ċĉ ĉ val ĠrequestId Ġ= Ġ++ last RequestId Ċĉ ĉ permission Requests [ requestId ] Ġ= Ġpermission Request Ċĉ ĉ ask For Permissions ( permission Request . permissions , ĠrequestId ) Ċ ĉ } ĊĊ ĉ fun Ġon Permission Result ( request Code : ĠInt , Ġpermissions : ĠArray < out ĠString >, Ġgrant Results : ĠInt Array ) Ġ{ Ċĉ ĉ val Ġpermission Request Ġ= Ġpermission Requests [ request Code ] Ċĉ ĉ permission Request ?. let Ġ{ Ċĉĉ ĉ val Ġgranted Ġ= ĠArrayList < String >() Ċĉĉ ĉ val Ġdenied Ġ= ĠArrayList < String >() ĊĊĉĉ ĉ permissions . indices . forEach Ġ{ Ċĉĉĉ ĉ if Ġ( grant Results [ it ] Ġ== ĠPackage Manager . PERMISSION _ GRAN TED ) Ċĉĉĉĉ ĉ gr anted . add ( permissions [ it ]) Ċĉĉĉ ĉ else Ċĉĉĉĉ ĉ den ied . add ( permissions [ it ]) Ċĉĉ ĉ } ĊĊĊĉĉ ĉ if Ġ( gr anted . isNotEmpty ()) Ċĉĉĉ ĉ permission Request . gr anted Callback . invoke ( gr anted ) Ċĉĉ ĉ if Ġ( den ied . isNotEmpty ()) Ċĉĉĉ ĉ permission Request . den ied Callback . invoke ( den ied ) ĊĊĉĉ ĉ permission Requests . remove ( request Code ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġcheck Permission ( permission : ĠString ) Ġ= Ġperform Permission Check ( permission ) Ġ== ĠPackage Manager . PERMISSION _ GRAN TED ĊĊ ĉ abstract Ġfun Ġperform Permission Check ( permission : ĠString ): ĠInt ĊĊ ĉ abstract Ġfun Ġask For Permissions ( permissions : ĠList < String >, ĠrequestId : ĠInt ) Ċ } Ċ Ċ class ĠActivity Permission Manager ( val Ġactivity : ĠFragment Activity ) Ġ: ĠPermission Manager () Ġ{ Ċ ĉ override Ġfun Ġperform Permission Check ( permission : ĠString ) Ġ= ĠContext Compat . check Self Permission ( activity , Ġpermission ) ĊĊ ĉ override Ġfun Ġask For Permissions ( permissions : ĠList < String >, ĠrequestId : ĠInt ) Ġ{ Ċĉ ĉ Activity Compat . request Permissions ( activity , Ġpermissions . to Typed Array (), ĠrequestId ) Ċ ĉ } Ċ } Ċ Ċ class ĠFragment Permission Manager ( val Ġfragment : ĠFragment ) Ġ: ĠPermission Manager () Ġ{ Ċ ĉ override Ġfun Ġperform Permission Check ( permission : ĠString ) Ġ= ĠContext Compat . check Self Permission ( fragment . activity !! , Ġpermission ) ĊĊ ĉ override Ġfun Ġask For Permissions ( permissions : ĠList < String >, ĠrequestId : ĠInt ) Ġ{ Ċĉ ĉ fragment . request Permissions ( permissions . to Typed Array (), ĠrequestId ) Ċ ĉ } Ċ } Ċ Ċ open Ġclass ĠPermission Request ( Ċ ĉ val Ġpermissions : ĠList < String >, Ċ ĉ val Ġgranted Callback : Ġ( gr anted Permissions : ĠList < String >) Ġ-> ĠUnit Ġ= Ġ{}, Ċ ĉ val Ġdenied Callback : Ġ( den ied Permissions : ĠList < String >) Ġ-> ĠUnit Ġ= Ġ{}) Ċ Ċ class ĠSingle Permission Request ( Ċ ĉ permission : ĠString , Ċ ĉ gr anted Callback : Ġ( gr anted Permission : ĠString ) Ġ-> ĠUnit Ġ= Ġ{}, Ċ ĉ den ied Callback : Ġ( den ied Permission : ĠString ) Ġ-> ĠUnit Ġ= Ġ{}) Ġ: ĠPermission Request ( listOf ( permission ), Ġ{ Ġgranted Callback ( it [ 0 ]) Ġ}, Ġ{ Ġdenied Callback ( it [ 0 ]) Ġ})