1 package com.android.permissioncontroller.permission.service
2 
3 import android.content.BroadcastReceiver
4 import android.content.Context
5 import android.content.Intent
6 import android.content.pm.PackageManager
7 import android.os.Process
8 import android.os.UserHandle
9 
10 /**
11  * For manually exempting a restricted permission. STOPSHIP This functionality should not be in the
12  * final release.
13  */
14 class ExemptRestrictedPermission : BroadcastReceiver() {
onReceivenull15     override fun onReceive(context: Context, intent: Intent) {
16         val packageName = intent.getStringExtra("package") ?: return
17         val permission = intent.getStringExtra("permission") ?: return
18         val userId = intent.getIntExtra("user", Process.myUserHandle().identifier)
19 
20         val userContext = context.createContextAsUser(UserHandle.of(userId), 0)
21 
22         // Use upgrade flag. If the permission needs to be manually exempted then it probably
23         // should have been done on upgrade.
24         userContext.packageManager.addWhitelistedRestrictedPermission(
25             packageName,
26             permission,
27             PackageManager.FLAG_PERMISSION_WHITELIST_UPGRADE
28         )
29     }
30 }
31