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.security; 18 19 import static android.app.Activity.RESULT_OK; 20 21 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_NONE; 22 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PASSWORD; 23 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PATTERN; 24 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PIN; 25 26 import android.car.drivingstate.CarUxRestrictions; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.os.Bundle; 30 import android.os.UserHandle; 31 import android.os.UserManager; 32 33 import androidx.annotation.Nullable; 34 import androidx.annotation.VisibleForTesting; 35 import androidx.fragment.app.Fragment; 36 import androidx.preference.Preference; 37 38 import com.android.car.settings.R; 39 import com.android.car.settings.common.ActivityResultCallback; 40 import com.android.car.settings.common.FragmentController; 41 import com.android.car.settings.common.Logger; 42 import com.android.car.settings.common.PreferenceController; 43 import com.android.internal.widget.LockPatternUtils; 44 45 /** 46 * Business logic for the choose lock type preference. This preference will launch a 47 * ScreenLockActivity to verify the current lock and then launch a new ChooseLockTypeFragment 48 * instance with the provided result data. 49 */ 50 public class ChooseLockTypePreferenceController extends PreferenceController<Preference> 51 implements ActivityResultCallback { 52 53 private static final Logger LOG = new Logger(ChooseLockTypePreferenceController.class); 54 @VisibleForTesting 55 static final int LOCK_CHECK = 1; 56 57 private final UserManager mUserManager; 58 private LockPatternUtils mLockPatternUtils; 59 ChooseLockTypePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)60 public ChooseLockTypePreferenceController(Context context, String preferenceKey, 61 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 62 this(context, preferenceKey, fragmentController, uxRestrictions, UserManager.get(context), 63 new LockPatternUtils(context)); 64 } 65 66 @VisibleForTesting ChooseLockTypePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, UserManager userManager, LockPatternUtils lockPatternUtils)67 ChooseLockTypePreferenceController(Context context, String preferenceKey, 68 FragmentController fragmentController, CarUxRestrictions uxRestrictions, 69 UserManager userManager, LockPatternUtils lockPatternUtils) { 70 super(context, preferenceKey, fragmentController, uxRestrictions); 71 mUserManager = userManager; 72 mLockPatternUtils = lockPatternUtils; 73 } 74 75 @Override getPreferenceType()76 protected Class<Preference> getPreferenceType() { 77 return Preference.class; 78 } 79 80 @Override updateState(Preference preference)81 protected void updateState(Preference preference) { 82 int passwordQuality = mLockPatternUtils.getCredentialTypeForUser(UserHandle.myUserId()); 83 84 switch (passwordQuality) { 85 case CREDENTIAL_TYPE_NONE: 86 preference.setSummary(R.string.security_lock_none); 87 break; 88 case CREDENTIAL_TYPE_PATTERN: 89 preference.setSummary(R.string.security_lock_pattern); 90 break; 91 case CREDENTIAL_TYPE_PIN: 92 preference.setSummary(R.string.security_lock_pin); 93 break; 94 case CREDENTIAL_TYPE_PASSWORD: 95 preference.setSummary(R.string.security_lock_password); 96 break; 97 default: 98 preference.setSummary(""); 99 break; 100 } 101 } 102 103 @Override handlePreferenceClicked(Preference preference)104 protected boolean handlePreferenceClicked(Preference preference) { 105 Intent intent = new Intent(getContext(), VerifyLockChangeActivity.class); 106 getFragmentController().startActivityForResult(intent, LOCK_CHECK, this); 107 return true; 108 } 109 110 @Override processActivityResult(int requestCode, int resultCode, @Nullable Intent data)111 public void processActivityResult(int requestCode, int resultCode, @Nullable Intent data) { 112 if (requestCode != LOCK_CHECK) { 113 LOG.e("Got unknown activity result"); 114 return; 115 } 116 if (resultCode != RESULT_OK) { 117 return; 118 } 119 Fragment fragment = new ChooseLockTypeFragment(); 120 Bundle bundle = fragment.getArguments(); 121 if (bundle == null) { 122 bundle = new Bundle(); 123 } 124 if (data != null) { 125 bundle.putParcelable(PasswordHelper.EXTRA_CURRENT_SCREEN_LOCK, 126 data.getParcelableExtra(PasswordHelper.EXTRA_CURRENT_SCREEN_LOCK)); 127 bundle.putInt(PasswordHelper.EXTRA_CURRENT_PASSWORD_QUALITY, 128 data.getIntExtra(PasswordHelper.EXTRA_CURRENT_PASSWORD_QUALITY, -1)); 129 } 130 fragment.setArguments(bundle); 131 getFragmentController().launchFragment(fragment); 132 } 133 134 @Override getAvailabilityStatus()135 public int getAvailabilityStatus() { 136 return mUserManager.isGuestUser() ? DISABLED_FOR_PROFILE : AVAILABLE; 137 } 138 } 139