1 /*
2  * Copyright (C) 2023 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.privatespace;
18 
19 import static android.app.admin.DevicePolicyManager.ACTION_SET_NEW_PASSWORD;
20 import static android.app.admin.DevicePolicyManager.EXTRA_PASSWORD_COMPLEXITY;
21 import static android.app.admin.DevicePolicyManager.PASSWORD_COMPLEXITY_LOW;
22 
23 import static com.android.settings.password.ChooseLockSettingsHelper.EXTRA_KEY_CHOOSE_LOCK_SCREEN_DESCRIPTION;
24 import static com.android.settings.password.ChooseLockSettingsHelper.EXTRA_KEY_CHOOSE_LOCK_SCREEN_TITLE;
25 import static com.android.settings.password.ChooseLockSettingsHelper.EXTRA_KEY_FINGERPRINT_ENROLLMENT_ONLY;
26 import static com.android.settings.privatespace.PrivateSpaceSetupActivity.ACCOUNT_LOGIN_ACTION;
27 import static com.android.settings.privatespace.PrivateSpaceSetupActivity.EXTRA_ACTION_TYPE;
28 import static com.android.settings.privatespace.PrivateSpaceSetupActivity.SET_LOCK_ACTION;
29 
30 import android.content.Intent;
31 import android.os.Bundle;
32 import android.os.UserHandle;
33 import android.util.Log;
34 
35 import androidx.activity.result.ActivityResult;
36 import androidx.activity.result.ActivityResultLauncher;
37 import androidx.activity.result.contract.ActivityResultContracts;
38 import androidx.annotation.Nullable;
39 import androidx.fragment.app.FragmentActivity;
40 
41 import com.android.internal.widget.LockPatternUtils;
42 import com.android.settings.R;
43 import com.android.settings.SetupWizardUtils;
44 import com.android.settings.overlay.FeatureFactory;
45 
46 import com.google.android.setupdesign.util.ThemeHelper;
47 
48 /**
49  * Activity that is started as private profile user that helps to set private profile lock or add an
50  * account on the private profile.
51  */
52 public class PrivateProfileContextHelperActivity extends FragmentActivity {
53     private static final String TAG = "PrivateSpaceHelperAct";
54     private final ActivityResultLauncher<Intent> mAddAccountToPrivateProfile =
55             registerForActivityResult(
56                     new ActivityResultContracts.StartActivityForResult(), this::onAccountAdded);
57     private final ActivityResultLauncher<Intent> mSetNewPrivateProfileLock =
58             registerForActivityResult(
59                     new ActivityResultContracts.StartActivityForResult(),
60                     this::onSetNewProfileLockActionCompleted);
61 
62     @Override
onCreate(Bundle savedInstanceState)63     protected void onCreate(Bundle savedInstanceState) {
64         if (!android.os.Flags.allowPrivateProfile()
65                 || !android.multiuser.Flags.enablePrivateSpaceFeatures()) {
66             return;
67         }
68         setTheme(SetupWizardUtils.getTheme(this, getIntent()));
69         ThemeHelper.trySetDynamicColor(this);
70         super.onCreate(savedInstanceState);
71         if (savedInstanceState == null) {
72             int action = getIntent().getIntExtra(EXTRA_ACTION_TYPE, -1);
73             if (action == ACCOUNT_LOGIN_ACTION) {
74                 setContentView(R.layout.private_space_wait_screen);
75                 PrivateSpaceLoginFeatureProvider privateSpaceLoginFeatureProvider =
76                         FeatureFactory.getFeatureFactory().getPrivateSpaceLoginFeatureProvider();
77                 UserHandle userHandle =
78                         PrivateSpaceMaintainer.getInstance(this).getPrivateProfileHandle();
79                 if (userHandle != null) {
80                     if (!privateSpaceLoginFeatureProvider.initiateAccountLogin(
81                             createContextAsUser(userHandle, 0), mAddAccountToPrivateProfile)) {
82                         setResult(RESULT_OK);
83                         finish();
84                     }
85                 } else {
86                     Log.w(TAG, "Private profile user handle is null");
87                     setResult(RESULT_CANCELED);
88                     finish();
89                 }
90             } else if (action == SET_LOCK_ACTION) {
91                 createPrivateSpaceLock();
92             }
93         }
94     }
95 
createPrivateSpaceLock()96     private void createPrivateSpaceLock() {
97         final Intent intent = new Intent(ACTION_SET_NEW_PASSWORD);
98         intent.putExtra(EXTRA_PASSWORD_COMPLEXITY, PASSWORD_COMPLEXITY_LOW);
99         intent.putExtra(EXTRA_KEY_FINGERPRINT_ENROLLMENT_ONLY, true);
100         intent.putExtra(
101                 EXTRA_KEY_CHOOSE_LOCK_SCREEN_TITLE, R.string.private_space_lock_setup_title);
102         intent.putExtra(
103                 EXTRA_KEY_CHOOSE_LOCK_SCREEN_DESCRIPTION,
104                 R.string.private_space_lock_setup_description);
105         mSetNewPrivateProfileLock.launch(intent);
106     }
107 
onAccountAdded(@ullable ActivityResult result)108     private void onAccountAdded(@Nullable ActivityResult result) {
109         if (result == null) {
110             Log.i(TAG, "private space account login result null");
111             setResult(RESULT_CANCELED);
112             finish();
113             return;
114         }
115         final int resultCode = result.getResultCode();
116         if (resultCode == RESULT_OK) {
117             Log.i(TAG, "private space account login success");
118         } else if (resultCode == RESULT_FIRST_USER) {
119             Log.i(TAG, "private space account login skipped");
120         } else {
121             Log.i(TAG, "private space account login failed");
122         }
123         setResult(
124                 resultCode == RESULT_OK || resultCode == RESULT_FIRST_USER
125                         ? RESULT_OK
126                         : RESULT_CANCELED);
127         finish();
128     }
129 
onSetNewProfileLockActionCompleted(@ullable ActivityResult result)130     private void onSetNewProfileLockActionCompleted(@Nullable ActivityResult result) {
131         LockPatternUtils lockPatternUtils =
132                 FeatureFactory.getFeatureFactory()
133                         .getSecurityFeatureProvider()
134                         .getLockPatternUtils(this);
135         if (result != null && lockPatternUtils.isSeparateProfileChallengeEnabled(getUserId())) {
136             Log.i(TAG, "separate private space lock setup success");
137             setResult(RESULT_OK);
138         } else {
139             Log.i(TAG, "separate private space lock not setup");
140             setResult(RESULT_CANCELED);
141         }
142         finish();
143     }
144 }
145