• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.settings.enterprise;
18 
19 import static android.security.advancedprotection.AdvancedProtectionManager.ADVANCED_PROTECTION_SYSTEM_ENTITY;
20 
21 import android.app.Activity;
22 import android.app.admin.DevicePolicyManager;
23 import android.app.admin.EnforcingAdmin;
24 import android.app.admin.UnknownAuthority;
25 import android.content.ComponentName;
26 import android.content.DialogInterface;
27 import android.content.Intent;
28 import android.os.Bundle;
29 import android.os.UserHandle;
30 import android.security.advancedprotection.AdvancedProtectionManager;
31 
32 import com.android.settingslib.RestrictedLockUtils;
33 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
34 
35 public class ActionDisabledByAdminDialog extends Activity
36         implements DialogInterface.OnDismissListener {
37 
38     private ActionDisabledByAdminDialogHelper mDialogHelper;
39 
40     @Override
onCreate(Bundle savedInstanceState)41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         final RestrictedLockUtils.EnforcedAdmin enforcedAdmin =
44                 getAdminDetailsFromIntent(getIntent());
45         final String restriction = getRestrictionFromIntent(getIntent());
46         mDialogHelper = new ActionDisabledByAdminDialogHelper(this, restriction);
47         mDialogHelper.prepareDialogBuilder(restriction, enforcedAdmin)
48                 .setOnDismissListener(this)
49                 .show();
50     }
51 
52     @Override
onNewIntent(Intent intent)53     public void onNewIntent(Intent intent) {
54         super.onNewIntent(intent);
55         final EnforcedAdmin admin = getAdminDetailsFromIntent(intent);
56         final String restriction = getRestrictionFromIntent(intent);
57         mDialogHelper.updateDialog(restriction, admin);
58     }
59 
60     @androidx.annotation.VisibleForTesting
getAdminDetailsFromIntent(Intent intent)61     EnforcedAdmin getAdminDetailsFromIntent(Intent intent) {
62         final EnforcedAdmin enforcedAdmin = new EnforcedAdmin(null, UserHandle.of(
63                 UserHandle.myUserId()));
64         if (intent == null) {
65             return enforcedAdmin;
66         }
67         enforcedAdmin.component = intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
68                 ComponentName.class);
69         int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
70 
71         Bundle adminDetails = null;
72         if (enforcedAdmin.component == null) {
73             DevicePolicyManager dpm = getSystemService(DevicePolicyManager.class);
74             final String restriction = getRestrictionFromIntent(intent);
75             if (android.security.Flags.aapmApi() && dpm != null && restriction != null) {
76                 // TODO(b/381025131): Move advanced protection logic to DevicePolicyManager or
77                 //  elsewhere.
78                 launchAdvancedProtectionDialogOrTryToSetAdminComponent(dpm, userId, restriction,
79                         enforcedAdmin);
80             } else {
81                 adminDetails = dpm.getEnforcingAdminAndUserDetails(userId, restriction);
82                 if (adminDetails != null) {
83                     enforcedAdmin.component = adminDetails.getParcelable(
84                             DevicePolicyManager.EXTRA_DEVICE_ADMIN, ComponentName.class);
85                 }
86             }
87         }
88 
89         if (intent.hasExtra(Intent.EXTRA_USER)) {
90             enforcedAdmin.user = intent.getParcelableExtra(Intent.EXTRA_USER, UserHandle.class);
91         } else {
92             if (adminDetails != null) {
93                 userId = adminDetails.getInt(Intent.EXTRA_USER_ID, UserHandle.myUserId());
94             }
95             if (userId == UserHandle.USER_NULL) {
96                 enforcedAdmin.user = null;
97             } else {
98                 enforcedAdmin.user = UserHandle.of(userId);
99             }
100         }
101         return enforcedAdmin;
102     }
103 
launchAdvancedProtectionDialogOrTryToSetAdminComponent(DevicePolicyManager dpm, int userId, String restriction, EnforcedAdmin enforcedAdmin)104     private void launchAdvancedProtectionDialogOrTryToSetAdminComponent(DevicePolicyManager dpm,
105             int userId, String restriction, EnforcedAdmin enforcedAdmin) {
106         EnforcingAdmin enforcingAdmin = dpm.getEnforcingAdmin(userId, restriction);
107         if (enforcingAdmin == null) {
108             return;
109         }
110         if (enforcingAdmin.getAuthority() instanceof UnknownAuthority authority
111                 && ADVANCED_PROTECTION_SYSTEM_ENTITY.equals(authority.getName())) {
112             Intent apmSupportIntent = AdvancedProtectionManager
113                     .createSupportIntentForPolicyIdentifierOrRestriction(restriction,
114                             AdvancedProtectionManager.SUPPORT_DIALOG_TYPE_UNKNOWN);
115             startActivityAsUser(apmSupportIntent, UserHandle.of(userId));
116             finish();
117         } else {
118             enforcedAdmin.component = enforcingAdmin.getComponentName();
119         }
120     }
121 
122     @androidx.annotation.VisibleForTesting
getRestrictionFromIntent(Intent intent)123     String getRestrictionFromIntent(Intent intent) {
124         if (intent == null) return null;
125         return intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION);
126     }
127 
128     @Override
onDismiss(DialogInterface dialog)129     public void onDismiss(DialogInterface dialog) {
130         finish();
131     }
132 }
133