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