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.applications.credentials;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 import android.util.Slog;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 import com.android.settings.SettingsActivity;
28 
29 /**
30  * Standalone activity used to launch a {@link DefaultCombinedPicker} fragment if the user is a
31  * normal user, a {@link DefaultCombinedPickerWork} fragment if the user is a work profile or {@link
32  * DefaultCombinedPickerPrivate} fragment if the user is a private profile.
33  */
34 public class CredentialsPickerActivity extends SettingsActivity {
35     private static final String TAG = "CredentialsPickerActivity";
36 
37     /** Injects the fragment name into the intent so the correct fragment is opened. */
38     @VisibleForTesting
injectFragmentIntoIntent(Context context, Intent intent)39     public static void injectFragmentIntoIntent(Context context, Intent intent) {
40         final int userId = UserHandle.myUserId();
41         final UserManager userManager = UserManager.get(context);
42 
43         if (DefaultCombinedPickerWork.isUserHandledByFragment(userManager, userId)) {
44             Slog.d(TAG, "Creating picker fragment using work profile");
45             intent.putExtra(EXTRA_SHOW_FRAGMENT, DefaultCombinedPickerWork.class.getName());
46         } else if (DefaultCombinedPickerPrivate.isUserHandledByFragment(userManager)) {
47             Slog.d(TAG, "Creating picker fragment using private profile");
48             intent.putExtra(EXTRA_SHOW_FRAGMENT, DefaultCombinedPickerPrivate.class.getName());
49         } else {
50             Slog.d(TAG, "Creating picker fragment using normal profile");
51             intent.putExtra(EXTRA_SHOW_FRAGMENT, DefaultCombinedPicker.class.getName());
52         }
53     }
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         final String packageName = getCallingPackage();
58         final Intent intent = getIntent();
59 
60         intent.putExtra(DefaultCombinedPicker.EXTRA_PACKAGE_NAME, packageName);
61         injectFragmentIntoIntent(this, intent);
62 
63         super.onCreate(savedInstanceState);
64     }
65 
66     @Override
isValidFragment(String fragmentName)67     protected boolean isValidFragment(String fragmentName) {
68         return super.isValidFragment(fragmentName)
69                 || DefaultCombinedPicker.class.getName().equals(fragmentName)
70                 || DefaultCombinedPickerWork.class.getName().equals(fragmentName)
71                 || DefaultCombinedPickerPrivate.class.getName().equals(fragmentName);
72     }
73 }
74