• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settingslib.supervision
18 
19 import android.app.admin.DeviceAdminReceiver
20 import android.app.supervision.SupervisionManager
21 import android.content.ComponentName
22 import android.content.Context
23 import android.content.Intent
24 import android.content.pm.PackageManager
25 import android.os.UserHandle
26 import android.util.Log
27 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin
28 
29 /** Helper class for supervision-enforced restrictions. */
30 object SupervisionRestrictionsHelper {
31 
32     /**
33      * Creates an instance of [EnforcedAdmin] that uses the correct supervision component or returns
34      * null if supervision is not enabled.
35      */
36     @JvmStatic
createEnforcedAdminnull37     fun createEnforcedAdmin(
38         context: Context,
39         restriction: String,
40         user: UserHandle,
41     ): EnforcedAdmin? {
42         val supervisionManager = context.getSystemService(SupervisionManager::class.java)
43         val supervisionAppPackage = supervisionManager?.activeSupervisionAppPackage ?: return null
44         var supervisionComponent: ComponentName? = null
45 
46         // Try to find the service whose package matches the active supervision app.
47         val resolveSupervisionApps =
48             context.packageManager.queryIntentServicesAsUser(
49                 Intent("android.app.action.BIND_SUPERVISION_APP_SERVICE"),
50                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS,
51                 user.identifier,
52             )
53         resolveSupervisionApps
54             .mapNotNull { it.serviceInfo?.componentName }
55             .find { it.packageName == supervisionAppPackage }
56             ?.let { supervisionComponent = it }
57 
58         if (supervisionComponent == null) {
59             // Try to find the PO receiver whose package matches the active supervision app, for
60             // backwards compatibility.
61             val resolveDeviceAdmins =
62                 context.packageManager.queryBroadcastReceiversAsUser(
63                     Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED),
64                     PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS,
65                     user.identifier,
66                 )
67             resolveDeviceAdmins
68                 .mapNotNull { it.activityInfo?.componentName }
69                 .find { it.packageName == supervisionAppPackage }
70                 ?.let { supervisionComponent = it }
71         }
72 
73         if (supervisionComponent == null) {
74             Log.d(SupervisionLog.TAG, "Could not find the supervision component.")
75         }
76         return EnforcedAdmin(supervisionComponent, restriction, user)
77     }
78 }
79