1 /*
2  * Copyright (C) 2024 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.content.Intent.ACTION_PROFILE_INACCESSIBLE;
20 import static android.content.Intent.ACTION_PROFILE_UNAVAILABLE;
21 
22 import static com.android.settings.privatespace.PrivateSpaceMaintainer.PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL;
23 
24 import android.app.settings.SettingsEnums;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.os.Looper;
32 import android.util.Log;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 
37 import androidx.activity.OnBackPressedCallback;
38 import androidx.annotation.NonNull;
39 import androidx.annotation.Nullable;
40 import androidx.navigation.fragment.NavHostFragment;
41 
42 import com.android.settings.R;
43 import com.android.settings.core.InstrumentedFragment;
44 
45 import com.google.android.setupdesign.GlifLayout;
46 
47 public class SetupPreFinishDelayFragment extends InstrumentedFragment {
48     private static final String TAG = "SetupPreFinishDelayFrag";
49     private static final Handler sHandler = new Handler(Looper.getMainLooper());
50     private static final int MAX_DELAY_BEFORE_SETUP_FINISH = 5000;
51     private boolean mActionProfileUnavailable;
52     private boolean mActionProfileInaccessible;
53 
54     protected final BroadcastReceiver mBroadcastReceiver =
55             new BroadcastReceiver() {
56                 @Override
57                 public void onReceive(Context context, Intent intent) {
58                     if (intent == null) {
59                         return;
60                     }
61                     String action = intent.getAction();
62                     Log.i(TAG, "Received broadcast: " + action);
63                     if (ACTION_PROFILE_UNAVAILABLE.equals(action)) {
64                         mActionProfileUnavailable = true;
65                     } else if (ACTION_PROFILE_INACCESSIBLE.equals(action)) {
66                         mActionProfileInaccessible = true;
67                     }
68                     if (mActionProfileUnavailable && mActionProfileInaccessible) {
69                         showSetupSuccessScreen();
70                     }
71                 }
72             };
73 
74     private Runnable mRunnable =
75             () -> {
76                 showSetupSuccessScreen();
77             };
78 
79     @Override
onCreate(@ullable Bundle savedInstanceState)80     public void onCreate(@Nullable Bundle savedInstanceState) {
81         if (android.os.Flags.allowPrivateProfile()
82                 && android.multiuser.Flags.enablePrivateSpaceFeatures()) {
83             super.onCreate(savedInstanceState);
84         }
85     }
86 
87     @NonNull
88     @Override
onCreateView( @onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)89     public View onCreateView(
90             @NonNull LayoutInflater inflater,
91             @Nullable ViewGroup container,
92             @Nullable Bundle savedInstanceState) {
93         GlifLayout rootView =
94                 (GlifLayout)
95                         inflater.inflate(R.layout.private_space_wait_screen, container, false);
96         OnBackPressedCallback callback =
97                 new OnBackPressedCallback(true /* enabled by default */) {
98                     @Override
99                     public void handleOnBackPressed() {
100                         // Handle the back button event. We intentionally don't want to allow back
101                         // button to work in this screen during the setup flow.
102                     }
103                 };
104         requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
105         if (savedInstanceState == null) {
106             // TODO(b/307729746): Add test to verify PS is locked and auto-lock value is set to
107             // auto-lock on device lock after setup completion.
108             PrivateSpaceMaintainer privateSpaceMaintainer =
109                     PrivateSpaceMaintainer.getInstance(getActivity());
110             privateSpaceMaintainer.setPrivateSpaceAutoLockSetting(
111                     PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL);
112             privateSpaceMaintainer.lockPrivateSpace();
113         }
114         return rootView;
115     }
116 
117     @Override
onPause()118     public void onPause() {
119         super.onPause();
120         getActivity().unregisterReceiver(mBroadcastReceiver);
121     }
122 
123     @Override
onResume()124     public void onResume() {
125         super.onResume();
126         final IntentFilter intentFilter = new IntentFilter();
127         intentFilter.addAction(ACTION_PROFILE_UNAVAILABLE);
128         intentFilter.addAction(ACTION_PROFILE_INACCESSIBLE);
129         getActivity().registerReceiver(mBroadcastReceiver, intentFilter);
130         sHandler.postDelayed(mRunnable, MAX_DELAY_BEFORE_SETUP_FINISH);
131     }
132 
133     @Override
onDestroy()134     public void onDestroy() {
135         sHandler.removeCallbacks(mRunnable);
136         super.onDestroy();
137     }
138 
139     @Override
getMetricsCategory()140     public int getMetricsCategory() {
141         return SettingsEnums.PRIVATE_SPACE_SETUP_PRE_FINISH;
142     }
143 
showSetupSuccessScreen()144     private void showSetupSuccessScreen() {
145         sHandler.removeCallbacks(mRunnable);
146         NavHostFragment.findNavController(SetupPreFinishDelayFragment.this)
147                 .navigate(R.id.action_success_fragment);
148     }
149 }
150