1 /*
2  * Copyright (C) 2022 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.biometrics;
18 
19 import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME;
20 import static com.android.settings.biometrics.BiometricEnrollBase.EXTRA_FROM_SETTINGS_SUMMARY;
21 
22 import android.app.admin.DevicePolicyManager;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.os.UserHandle;
27 import android.os.UserManager;
28 
29 import androidx.activity.result.ActivityResultLauncher;
30 import androidx.annotation.Nullable;
31 
32 import com.android.internal.app.UnlaunchableAppActivity;
33 import com.android.settings.core.SettingsBaseActivity;
34 import com.android.settingslib.RestrictedLockUtils;
35 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
36 import com.android.settingslib.transition.SettingsTransitionHelper;
37 
38 /**
39  * Utilities for navigation shared between Security Settings and Safety Center.
40  */
41 public class BiometricNavigationUtils {
42 
43     private final int mUserId;
44 
BiometricNavigationUtils(int userId)45     public BiometricNavigationUtils(int userId) {
46         mUserId = userId;
47     }
48 
49     /**
50      * Tries to launch the Settings screen if Quiet Mode is not enabled
51      * for managed profile, otherwise shows a dialog to disable the Quiet Mode.
52      *
53      * @param className The class name of Settings screen to launch.
54      * @param extras    Extras to put into the launching {@link Intent}.
55      * @param launcher  Launcher to launch activity if non-quiet mode
56      * @return true if the Settings screen is launching.
57      */
launchBiometricSettings(Context context, String className, Bundle extras, @Nullable ActivityResultLauncher<Intent> launcher)58     public boolean launchBiometricSettings(Context context, String className, Bundle extras,
59             @Nullable ActivityResultLauncher<Intent> launcher) {
60         final Intent quietModeDialogIntent = getQuietModeDialogIntent(context);
61         if (quietModeDialogIntent != null) {
62             context.startActivity(quietModeDialogIntent);
63             return false;
64         }
65 
66         final Intent settingsPageIntent = getSettingsPageIntent(className, extras);
67         if (launcher != null) {
68             launcher.launch(settingsPageIntent);
69         } else {
70             context.startActivity(settingsPageIntent);
71         }
72         return true;
73     }
74 
75     /**
76      * Returns {@link Intent} to launch an appropriate Settings screen.
77      *
78      * <p>If the Setting is disabled by admin, returns {@link Intent} to launch an explanation.
79      * If Quiet Mode is enabled for managed profile, returns {@link Intent} to launch a dialog
80      * to disable the Quiet Mode. Otherwise, returns {@link Intent} to launch the Settings screen.
81      *
82      * @param className     The class name of Settings screen to launch.
83      * @param enforcedAdmin Details of admin account that disables changing the setting.
84      * @param extras        Extras to put into the result {@link Intent}.
85      */
getBiometricSettingsIntent(Context context, String className, EnforcedAdmin enforcedAdmin, Bundle extras)86     public Intent getBiometricSettingsIntent(Context context, String className,
87             EnforcedAdmin enforcedAdmin, Bundle extras) {
88         if (enforcedAdmin != null) {
89             return getRestrictedDialogIntent(context, enforcedAdmin);
90         }
91         final Intent quietModeDialogIntent = getQuietModeDialogIntent(context);
92         return quietModeDialogIntent != null ? quietModeDialogIntent
93                 : getSettingsPageIntent(className, extras);
94     }
95 
getQuietModeDialogIntent(Context context)96     private Intent getQuietModeDialogIntent(Context context) {
97         final UserManager userManager = UserManager.get(context);
98         if (userManager.isQuietModeEnabled(UserHandle.of(mUserId))) {
99             return UnlaunchableAppActivity.createInQuietModeDialogIntent(mUserId);
100         }
101         return null;
102     }
103 
getRestrictedDialogIntent(Context context, EnforcedAdmin enforcedAdmin)104     private Intent getRestrictedDialogIntent(Context context, EnforcedAdmin enforcedAdmin) {
105         final Intent intent = RestrictedLockUtils
106                 .getShowAdminSupportDetailsIntent(context, enforcedAdmin);
107         int targetUserId = mUserId;
108         if (enforcedAdmin.user != null && RestrictedLockUtils
109                 .isCurrentUserOrProfile(context, enforcedAdmin.user.getIdentifier())) {
110             targetUserId = enforcedAdmin.user.getIdentifier();
111         }
112         intent.putExtra(DevicePolicyManager.EXTRA_RESTRICTION, enforcedAdmin.enforcedRestriction);
113         intent.putExtra(Intent.EXTRA_USER_ID, targetUserId);
114         return intent;
115     }
116 
getSettingsPageIntent(String className, Bundle extras)117     private Intent getSettingsPageIntent(String className, Bundle extras) {
118         final Intent intent = new Intent();
119         intent.setClassName(SETTINGS_PACKAGE_NAME, className);
120         if (!extras.isEmpty()) {
121             intent.putExtras(extras);
122         }
123         intent.putExtra(EXTRA_FROM_SETTINGS_SUMMARY, true);
124         intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
125         intent.putExtra(SettingsBaseActivity.EXTRA_PAGE_TRANSITION_TYPE,
126                 SettingsTransitionHelper.TransitionType.TRANSITION_SLIDE);
127 
128         return intent;
129     }
130 }
131