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.tv.settings;
18 
19 import android.app.Activity;
20 import android.app.admin.DevicePolicyManager;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.os.UserHandle;
25 
26 import com.android.settingslib.RestrictedLockUtils;
27 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
28 
29 public class ActionDisabledByAdminDialog extends Activity
30         implements DialogInterface.OnDismissListener {
31 
32     private ActionDisabledByAdminDialogHelper mDialogHelper;
33 
34     @Override
onCreate(Bundle savedInstanceState)35     protected void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         final RestrictedLockUtils.EnforcedAdmin enforcedAdmin =
38                 getAdminDetailsFromIntent(getIntent());
39         final String restriction = getRestrictionFromIntent(getIntent());
40         mDialogHelper = new ActionDisabledByAdminDialogHelper(this);
41         mDialogHelper.prepareDialogBuilder(restriction, enforcedAdmin)
42                 .setOnDismissListener(this)
43                 .show();
44     }
45 
46     @Override
onNewIntent(Intent intent)47     public void onNewIntent(Intent intent) {
48         super.onNewIntent(intent);
49         final EnforcedAdmin admin = getAdminDetailsFromIntent(intent);
50         final String restriction = getRestrictionFromIntent(intent);
51         mDialogHelper.updateDialog(restriction, admin);
52     }
53 
54     @androidx.annotation.VisibleForTesting
getAdminDetailsFromIntent(Intent intent)55     EnforcedAdmin getAdminDetailsFromIntent(Intent intent) {
56         final EnforcedAdmin admin = new EnforcedAdmin(null, UserHandle.of(UserHandle.myUserId()));
57         if (intent == null) {
58             return admin;
59         }
60         admin.component = intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN);
61 
62         if (intent.hasExtra(Intent.EXTRA_USER)) {
63             admin.user = intent.getParcelableExtra(Intent.EXTRA_USER);
64         } else {
65             int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
66             if (userId == UserHandle.USER_NULL) {
67                 admin.user = null;
68             } else {
69                 admin.user = UserHandle.of(userId);
70             }
71         }
72         return admin;
73     }
74 
75     @androidx.annotation.VisibleForTesting
getRestrictionFromIntent(Intent intent)76     String getRestrictionFromIntent(Intent intent) {
77         if (intent == null) {
78             return null;
79         }
80         return intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION);
81     }
82 
83     @Override
onDismiss(DialogInterface dialog)84     public void onDismiss(DialogInterface dialog) {
85         finish();
86     }
87 }
88