1 /*
2  * Copyright (C) 2021 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 java.util.Objects.requireNonNull;
20 
21 import android.app.Activity;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.UserHandle;
27 
28 import androidx.appcompat.app.AlertDialog;
29 
30 import com.android.settings.R;
31 import com.android.settings.Settings;
32 import com.android.settings.applications.specialaccess.deviceadmin.DeviceAdminAdd;
33 import com.android.settingslib.enterprise.ActionDisabledLearnMoreButtonLauncher;
34 
35 /**
36  * Helper class to set up the "Learn more" button in the action disabled dialog.
37  */
38 public final class ActionDisabledLearnMoreButtonLauncherImpl
39         extends ActionDisabledLearnMoreButtonLauncher {
40 
41     private final Activity mActivity;
42     private final AlertDialog.Builder mBuilder;
43 
ActionDisabledLearnMoreButtonLauncherImpl(Activity activity, AlertDialog.Builder builder)44     ActionDisabledLearnMoreButtonLauncherImpl(Activity activity, AlertDialog.Builder builder) {
45         mActivity = requireNonNull(activity, "activity cannot be null");
46         mBuilder = requireNonNull(builder, "builder cannot be null");
47     }
48 
49     @Override
setLearnMoreButton(Runnable action)50     public void setLearnMoreButton(Runnable action) {
51         requireNonNull(action, "action cannot be null");
52 
53         mBuilder.setNeutralButton(R.string.learn_more, (dialog, which) -> action.run());
54     }
55 
56     @Override
launchShowAdminPolicies(Context context, UserHandle user, ComponentName admin)57     protected void launchShowAdminPolicies(Context context, UserHandle user, ComponentName admin) {
58         requireNonNull(context, "context cannot be null");
59         requireNonNull(user, "user cannot be null");
60         requireNonNull(admin, "admin cannot be null");
61 
62         Intent intent = new Intent()
63                 .setClass(mActivity, DeviceAdminAdd.class)
64                 .putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin)
65                 .putExtra(DeviceAdminAdd.EXTRA_CALLED_FROM_SUPPORT_DIALOG, true);
66         // DeviceAdminAdd class may need to run as managed profile.
67         mActivity.startActivityAsUser(intent, user);
68     }
69 
70     @Override
launchShowAdminSettings(Context context)71     protected void launchShowAdminSettings(Context context) {
72         requireNonNull(context, "context cannot be null");
73 
74         Intent intent = new Intent()
75                 .setClass(mActivity, Settings.DeviceAdminSettingsActivity.class)
76                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
77         // Activity merges both managed profile and parent users
78         // admins so show as same user as this activity.
79         mActivity.startActivity(intent);
80     }
81 
82     @Override
finishSelf()83     protected void finishSelf() {
84         mActivity.finish();
85     }
86 }
87