1 /*
2  * Copyright (C) 2018 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 android.app.admin.DevicePolicyManager;
20 import android.car.drivingstate.CarUxRestrictions;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.UserHandle;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.ConfirmationDialogFragment;
30 import com.android.car.settings.common.FragmentController;
31 import com.android.internal.widget.LockPatternUtils;
32 import com.android.internal.widget.LockscreenCredential;
33 
34 /** Business logic for the no lock preference. */
35 public class NoLockPreferenceController extends LockTypeBasePreferenceController {
36 
37     private static final int[] ALLOWED_PASSWORD_QUALITIES =
38             new int[]{DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED};
39 
40     @VisibleForTesting
41     final ConfirmationDialogFragment.ConfirmListener mConfirmListener = arguments -> {
42         int userId = UserHandle.myUserId();
43         new LockPatternUtils(getContext()).setLockCredential(
44                 LockscreenCredential.createNone(), getCurrentPassword(), userId);
45         getFragmentController().goBack();
46     };
47 
NoLockPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)48     public NoLockPreferenceController(Context context, String preferenceKey,
49             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
50         super(context, preferenceKey, fragmentController, uxRestrictions);
51     }
52 
53 
54     /**
55      * If the dialog to confirm removal of lock was open previously, make sure the listener is
56      * restored.
57      */
58     @Override
onCreateInternal()59     protected void onCreateInternal() {
60         super.onCreateInternal();
61         ConfirmationDialogFragment dialog =
62                 (ConfirmationDialogFragment) getFragmentController().findDialogByTag(
63                         ConfirmationDialogFragment.TAG);
64         ConfirmationDialogFragment.resetListeners(
65                 dialog,
66                 mConfirmListener,
67                 /* rejectListener= */ null,
68                 /* neutralListener= */ null);
69     }
70 
71     @Override
handlePreferenceClicked(Preference preference)72     protected boolean handlePreferenceClicked(Preference preference) {
73         if (isCurrentLock()) {
74             // No need to show dialog to confirm remove screen lock if screen lock is already
75             // removed, which is true if this controller is the current lock.
76             getFragmentController().goBack();
77         } else {
78             getFragmentController().showDialog(
79                 getConfirmRemoveScreenLockDialogFragment(),
80                 ConfirmationDialogFragment.TAG);
81         }
82         return true;
83     }
84 
85     @Override
activityToOpen()86     protected Intent activityToOpen() {
87         // Selecting this preference does not open a new activity. Instead it opens a dialog to
88         // confirm the removal of the existing lock screen.
89         return null;
90     }
91 
92     @Override
allowedPasswordQualities()93     protected int[] allowedPasswordQualities() {
94         return ALLOWED_PASSWORD_QUALITIES;
95     }
96 
getConfirmRemoveScreenLockDialogFragment()97     private ConfirmationDialogFragment getConfirmRemoveScreenLockDialogFragment() {
98         return new ConfirmationDialogFragment.Builder(getContext())
99                 .setTitle(R.string.remove_screen_lock_title)
100                 .setMessage(R.string.remove_screen_lock_message)
101                 .setPositiveButton(R.string.remove_button, mConfirmListener)
102                 .setNegativeButton(android.R.string.cancel, /* rejectListener= */ null)
103                 .build();
104     }
105 }
106