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.delete;
18 
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.app.Activity;
22 import android.app.settings.SettingsEnums;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.graphics.drawable.Drawable;
26 import android.os.Bundle;
27 import android.os.UserHandle;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.ImageView;
33 import android.widget.LinearLayout;
34 import android.widget.TextView;
35 
36 import androidx.annotation.NonNull;
37 import androidx.annotation.Nullable;
38 import androidx.navigation.fragment.NavHostFragment;
39 
40 import com.android.settings.R;
41 import com.android.settings.core.InstrumentedFragment;
42 import com.android.settings.password.ChooseLockSettingsHelper;
43 import com.android.settings.privatespace.PrivateSpaceMaintainer;
44 import com.android.settingslib.accounts.AuthenticatorHelper;
45 
46 import com.google.android.setupcompat.template.FooterBarMixin;
47 import com.google.android.setupcompat.template.FooterButton;
48 import com.google.android.setupdesign.GlifLayout;
49 
50 /** Fragment to delete private space that lists the accounts logged in to the private profile. */
51 public class PrivateSpaceDeleteFragment extends InstrumentedFragment {
52     private static final String TAG = "PrivateSpaceDeleteFrag";
53     private View mContentView;
54     private static final int CREDENTIAL_CONFIRM_REQUEST = 1;
55     @Nullable private UserHandle mPrivateUserHandle;
56 
57     @Override
onCreate(@ullable Bundle icicle)58     public void onCreate(@Nullable Bundle icicle) {
59         if (android.os.Flags.allowPrivateProfile()
60                 && android.multiuser.Flags.enablePrivateSpaceFeatures()) {
61             super.onCreate(icicle);
62         }
63     }
64 
65     @Override
onStart()66     public void onStart() {
67         super.onStart();
68         if (PrivateSpaceMaintainer.getInstance(getContext()).isPrivateSpaceLocked()) {
69             getActivity().finish();
70         }
71     }
72 
73     @Override
getMetricsCategory()74     public int getMetricsCategory() {
75         return SettingsEnums.PRIVATE_SPACE_SETTINGS;
76     }
77 
startAuthenticationForDelete()78     private View.OnClickListener startAuthenticationForDelete() {
79         return v -> {
80             final ChooseLockSettingsHelper.Builder builder =
81                     new ChooseLockSettingsHelper.Builder(getActivity(), this);
82             if (mPrivateUserHandle != null) {
83                 builder.setRequestCode(CREDENTIAL_CONFIRM_REQUEST)
84                         .setUserId(mPrivateUserHandle.getIdentifier())
85                         .show();
86             } else {
87                 Log.e(TAG, "Private space user handle cannot be null");
88                 getActivity().finish();
89             }
90         };
91     }
92 
93     @NonNull
94     @Override
onCreateView( @onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)95     public View onCreateView(
96             @NonNull LayoutInflater inflater,
97             @Nullable ViewGroup container,
98             @Nullable Bundle savedInstanceState) {
99         mPrivateUserHandle =
100                 PrivateSpaceMaintainer.getInstance(getContext()).getPrivateProfileHandle();
101         if (mPrivateUserHandle == null) {
102             Log.e(TAG, "Private space user handle cannot be null");
103             getActivity().finish();
104         }
105         mContentView = inflater.inflate(R.layout.private_space_delete, container, false);
106         final GlifLayout layout = mContentView.findViewById(R.id.private_space_delete_layout);
107         final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
108         final Activity activity = getActivity();
109         mixin.setPrimaryButton(
110                 new FooterButton.Builder(activity)
111                         .setText(R.string.private_space_delete_button_label)
112                         .setListener(startAuthenticationForDelete())
113                         .setButtonType(FooterButton.ButtonType.OTHER)
114                         .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Primary)
115                         .build());
116         mixin.setSecondaryButton(
117                 new FooterButton.Builder(activity)
118                         .setText(android.R.string.cancel)
119                         .setListener(view -> activity.onBackPressed())
120                         .setButtonType(FooterButton.ButtonType.CANCEL)
121                         .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Secondary)
122                         .build());
123 
124         loadPrivateProfileAccountList();
125         return mContentView;
126     }
127 
loadPrivateProfileAccountList()128     private void loadPrivateProfileAccountList() {
129         View accountsLabel = mContentView.findViewById(R.id.accounts_label);
130         LinearLayout contents = (LinearLayout) mContentView.findViewById(R.id.accounts);
131         contents.removeAllViews();
132 
133         Context context = getActivity();
134 
135         AccountManager accountManager = AccountManager.get(context);
136 
137         LayoutInflater inflater = context.getSystemService(LayoutInflater.class);
138 
139         final AuthenticatorHelper helper =
140                 new AuthenticatorHelper(context, mPrivateUserHandle, null);
141         final String[] accountTypes = helper.getEnabledAccountTypes();
142 
143         for (String type : accountTypes) {
144             final String accountType = type;
145             final Account[] accounts =
146                     accountManager.getAccountsByTypeAsUser(accountType, mPrivateUserHandle);
147             Drawable icon = helper.getDrawableForType(getContext(), accountType);
148             if (icon == null) {
149                 icon = context.getPackageManager().getDefaultActivityIcon();
150             }
151             for (Account account : accounts) {
152                 View child = inflater.inflate(R.layout.main_clear_account, contents, false);
153                 child.<ImageView>findViewById(android.R.id.icon).setImageDrawable(icon);
154                 child.<TextView>findViewById(android.R.id.title).setText(account.name);
155                 contents.addView(child);
156             }
157         }
158 
159         if (contents.getChildCount() > 0) {
160             accountsLabel.setVisibility(View.VISIBLE);
161             contents.setVisibility(View.VISIBLE);
162         }
163     }
164 
165     @Override
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)166     public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
167         super.onActivityResult(requestCode, resultCode, data);
168         if (requestCode == CREDENTIAL_CONFIRM_REQUEST && resultCode == Activity.RESULT_OK) {
169             NavHostFragment.findNavController(PrivateSpaceDeleteFragment.this)
170                     .navigate(R.id.action_authenticate_delete);
171         }
172     }
173 }
174