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 android.app.Activity;
20 import android.app.ActivityManager;
21 import android.app.role.RoleManager;
22 import android.app.settings.SettingsEnums;
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.graphics.drawable.Drawable;
27 import android.os.Bundle;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.Toast;
33 
34 import androidx.activity.OnBackPressedCallback;
35 import androidx.annotation.Nullable;
36 
37 import com.android.settings.R;
38 import com.android.settings.core.InstrumentedFragment;
39 import com.android.settingslib.widget.LottieColorUtils;
40 
41 import com.airbnb.lottie.LottieAnimationView;
42 import com.google.android.setupcompat.template.FooterBarMixin;
43 import com.google.android.setupcompat.template.FooterButton;
44 import com.google.android.setupdesign.GlifLayout;
45 
46 import java.util.List;
47 
48 /** Fragment for the final screen shown on successful completion of private space setup. */
49 public class SetupSuccessFragment extends InstrumentedFragment {
50     private static final String TAG = "SetupSuccessFragment";
51 
52     @Override
onCreateView( LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)53     public View onCreateView(
54             LayoutInflater inflater,
55             @Nullable ViewGroup container,
56             @Nullable Bundle savedInstanceState) {
57         if (!android.os.Flags.allowPrivateProfile()
58                 || !android.multiuser.Flags.enablePrivateSpaceFeatures()) {
59             return null;
60         }
61         GlifLayout rootView =
62                 (GlifLayout)
63                         inflater.inflate(R.layout.private_space_setup_success, container, false);
64         final FooterBarMixin mixin = rootView.getMixin(FooterBarMixin.class);
65         mixin.setPrimaryButton(
66                 new FooterButton.Builder(getContext())
67                         .setText(R.string.private_space_done_label)
68                         .setListener(onClickNext())
69                         .setButtonType(FooterButton.ButtonType.NEXT)
70                         .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Primary)
71                         .build());
72         OnBackPressedCallback callback =
73                 new OnBackPressedCallback(true /* enabled by default */) {
74                     @Override
75                     public void handleOnBackPressed() {
76                         // Handle the back button event. We intentionally don't want to allow back
77                         // button to work in this screen during the setup flow.
78                     }
79                 };
80         requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
81         LottieAnimationView lottieAnimationView = rootView.findViewById(R.id.lottie_animation);
82         LottieColorUtils.applyDynamicColors(getContext(), lottieAnimationView);
83 
84         return rootView;
85     }
86 
87     @Override
getMetricsCategory()88     public int getMetricsCategory() {
89         return SettingsEnums.PRIVATE_SPACE_SETUP_FINISH;
90     }
91 
onClickNext()92     private View.OnClickListener onClickNext() {
93         return v -> {
94             Activity activity = getActivity();
95             if (activity != null) {
96                 mMetricsFeatureProvider.action(
97                         getContext(), SettingsEnums.ACTION_PRIVATE_SPACE_SETUP_DONE);
98                 Intent allAppsIntent = new Intent(Intent.ACTION_ALL_APPS);
99                 ResolveInfo resolveInfo =
100                         activity.getPackageManager()
101                                 .resolveActivityAsUser(
102                                         new Intent(Intent.ACTION_MAIN)
103                                                 .addCategory(Intent.CATEGORY_HOME),
104                                         PackageManager.MATCH_SYSTEM_ONLY,
105                                         activity.getUserId());
106                 if (resolveInfo != null) {
107                     RoleManager mRoleManager = getContext().getSystemService(RoleManager.class);
108                     final List<String> packageNames = mRoleManager
109                             .getRoleHolders(RoleManager.ROLE_HOME);
110                     if (packageNames.contains(resolveInfo.activityInfo.packageName)) {
111                         allAppsIntent.setPackage(resolveInfo.activityInfo.packageName);
112                         allAppsIntent.setComponent(resolveInfo.activityInfo.getComponentName());
113                     }
114                 }
115                 activity.setTheme(R.style.Theme_SubSettings);
116                 if (allAppsIntent.getPackage() != null) {
117                     accessPrivateSpaceToast();
118                     startActivity(allAppsIntent);
119                 }
120                 Log.i(TAG, "Private space setup complete");
121                 deleteAllTaskAndFinish(activity);
122             }
123         };
124     }
125 
accessPrivateSpaceToast()126     private void accessPrivateSpaceToast() {
127         Drawable drawable = getContext().getDrawable(R.drawable.ic_private_space_icon);
128         Toast.makeCustomToastWithIcon(
129                         getContext(),
130                         null /* looper */ ,
131                         getContext().getString(R.string.private_space_scrolldown_to_access),
132                         Toast.LENGTH_SHORT,
133                         drawable)
134                 .show();
135     }
136 
deleteAllTaskAndFinish(Activity activity)137     private void deleteAllTaskAndFinish(Activity activity) {
138         ActivityManager activityManager = activity.getSystemService(ActivityManager.class);
139         List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();
140         for (var task : tasks) {
141             task.finishAndRemoveTask();
142         }
143     }
144 }
145