1 /*
2  * Copyright (C) 2021 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.car.settings.privacy;
18 
19 import android.app.ActivityManager;
20 import android.car.drivingstate.CarUxRestrictions;
21 import android.content.Context;
22 import android.content.pm.UserInfo;
23 import android.os.UserManager;
24 import android.widget.Toast;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.FragmentController;
30 import com.android.car.settings.common.PreferenceController;
31 import com.android.car.settings.profiles.ProfileHelper;
32 import com.android.car.settings.profiles.ProfileUtils;
33 import com.android.car.settings.profiles.RemoveProfileHandler;
34 import com.android.internal.annotations.VisibleForTesting;
35 
36 /** Business logic for preference that deletes the current user profile. */
37 public class DeleteUserPreferenceController
38         extends PreferenceController<Preference> {
39 
40     private final UserInfo mUserInfo;
41     private final RemoveProfileHandler mRemoveProfileHandler;
42 
DeleteUserPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)43     public DeleteUserPreferenceController(Context context,
44             String preferenceKey, FragmentController fragmentController,
45             CarUxRestrictions uxRestrictions) {
46         this(context, preferenceKey, fragmentController, uxRestrictions,
47                 new RemoveProfileHandler(context, ProfileHelper.getInstance(context),
48                         UserManager.get(context), fragmentController));
49     }
50 
51     @VisibleForTesting
DeleteUserPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, RemoveProfileHandler removeProfileHandler)52     DeleteUserPreferenceController(Context context,
53             String preferenceKey, FragmentController fragmentController,
54             CarUxRestrictions uxRestrictions, RemoveProfileHandler removeProfileHandler) {
55         super(context, preferenceKey, fragmentController, uxRestrictions);
56         mRemoveProfileHandler = removeProfileHandler;
57         mUserInfo = ProfileUtils.getUserInfo(getContext(), ActivityManager.getCurrentUser());
58         mRemoveProfileHandler.setUserInfo(mUserInfo);
59     }
60 
61     @Override
onCreateInternal()62     protected void onCreateInternal() {
63         super.onCreateInternal();
64         mRemoveProfileHandler.resetListeners();
65         setClickableWhileDisabled(getPreference(), /* clickable= */true, p ->
66                 Toast.makeText(getContext(), getContext().getString(R.string.action_unavailable),
67                         Toast.LENGTH_LONG).show());
68     }
69 
70     @Override
getPreferenceType()71     protected Class<Preference> getPreferenceType() {
72         return Preference.class;
73     }
74 
75     @Override
handlePreferenceClicked(Preference preference)76     protected boolean handlePreferenceClicked(Preference preference) {
77         mRemoveProfileHandler.showConfirmRemoveProfileDialog();
78         return true;
79     }
80 
81     @AvailabilityStatus
getDefaultAvailabilityStatus()82     protected int getDefaultAvailabilityStatus() {
83         return mRemoveProfileHandler.canRemoveProfile(mUserInfo) ? AVAILABLE
84                 : AVAILABLE_FOR_VIEWING;
85     }
86 }
87