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 private boolean mIsAnimationPlaying = true; 53 54 @Override onCreateView( LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)55 public View onCreateView( 56 LayoutInflater inflater, 57 @Nullable ViewGroup container, 58 @Nullable Bundle savedInstanceState) { 59 if (!android.os.Flags.allowPrivateProfile() 60 || !android.multiuser.Flags.enablePrivateSpaceFeatures()) { 61 return null; 62 } 63 GlifLayout rootView = 64 (GlifLayout) 65 inflater.inflate(R.layout.private_space_setup_success, container, false); 66 final FooterBarMixin mixin = rootView.getMixin(FooterBarMixin.class); 67 mixin.setPrimaryButton( 68 new FooterButton.Builder(getContext()) 69 .setText(R.string.private_space_done_label) 70 .setListener(onClickNext()) 71 .setButtonType(FooterButton.ButtonType.NEXT) 72 .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Primary) 73 .build()); 74 OnBackPressedCallback callback = 75 new OnBackPressedCallback(true /* enabled by default */) { 76 @Override 77 public void handleOnBackPressed() { 78 // Handle the back button event. We intentionally don't want to allow back 79 // button to work in this screen during the setup flow. 80 } 81 }; 82 requireActivity().getOnBackPressedDispatcher().addCallback(this, callback); 83 LottieAnimationView lottieAnimationView = rootView.findViewById(R.id.lottie_animation); 84 LottieColorUtils.applyDynamicColors(getContext(), lottieAnimationView); 85 lottieAnimationView.setOnClickListener(v -> handleAnimationClick(lottieAnimationView)); 86 PrivateSpaceAccessibilityUtils.updateAccessibilityActionForAnimation(getContext(), 87 lottieAnimationView, mIsAnimationPlaying); 88 89 return rootView; 90 } 91 92 @Override getMetricsCategory()93 public int getMetricsCategory() { 94 return SettingsEnums.PRIVATE_SPACE_SETUP_FINISH; 95 } 96 onClickNext()97 private View.OnClickListener onClickNext() { 98 return v -> { 99 Activity activity = getActivity(); 100 if (activity != null) { 101 mMetricsFeatureProvider.action( 102 getContext(), SettingsEnums.ACTION_PRIVATE_SPACE_SETUP_DONE); 103 Intent allAppsIntent = new Intent(Intent.ACTION_ALL_APPS); 104 ResolveInfo resolveInfo = 105 activity.getPackageManager() 106 .resolveActivityAsUser( 107 new Intent(Intent.ACTION_MAIN) 108 .addCategory(Intent.CATEGORY_HOME), 109 PackageManager.MATCH_SYSTEM_ONLY, 110 activity.getUserId()); 111 if (resolveInfo != null) { 112 RoleManager mRoleManager = getContext().getSystemService(RoleManager.class); 113 final List<String> packageNames = mRoleManager 114 .getRoleHolders(RoleManager.ROLE_HOME); 115 if (packageNames.contains(resolveInfo.activityInfo.packageName)) { 116 allAppsIntent.setPackage(resolveInfo.activityInfo.packageName); 117 allAppsIntent.setComponent(resolveInfo.activityInfo.getComponentName()); 118 } 119 } 120 activity.setTheme(R.style.Theme_SubSettings); 121 if (allAppsIntent.getPackage() != null) { 122 accessPrivateSpaceToast(); 123 startActivity(allAppsIntent); 124 } 125 Log.i(TAG, "Private space setup complete"); 126 deleteAllTaskAndFinish(activity); 127 } 128 }; 129 } 130 accessPrivateSpaceToast()131 private void accessPrivateSpaceToast() { 132 Drawable drawable = getContext().getDrawable(R.drawable.ic_private_space_icon); 133 Toast.makeCustomToastWithIcon( 134 getContext(), 135 null /* looper */ , 136 getContext().getString(R.string.private_space_scrolldown_to_access), 137 Toast.LENGTH_SHORT, 138 drawable) 139 .show(); 140 } 141 deleteAllTaskAndFinish(Activity activity)142 private void deleteAllTaskAndFinish(Activity activity) { 143 ActivityManager activityManager = activity.getSystemService(ActivityManager.class); 144 List<ActivityManager.AppTask> tasks = activityManager.getAppTasks(); 145 for (var task : tasks) { 146 task.finishAndRemoveTask(); 147 } 148 } 149 handleAnimationClick(LottieAnimationView lottieAnimationView)150 private void handleAnimationClick(LottieAnimationView lottieAnimationView) { 151 if (mIsAnimationPlaying) { 152 lottieAnimationView.pauseAnimation(); 153 } else { 154 lottieAnimationView.playAnimation(); 155 } 156 mIsAnimationPlaying = !mIsAnimationPlaying; 157 PrivateSpaceAccessibilityUtils.updateAccessibilityActionForAnimation(getContext(), 158 lottieAnimationView, mIsAnimationPlaying); 159 } 160 } 161