• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.drivingstate.CarUxRestrictions;
20 import android.car.userlib.CarUserManagerHelper;
21 import android.content.Context;
22 import android.os.Bundle;
23 
24 import androidx.fragment.app.Fragment;
25 import androidx.preference.Preference;
26 
27 import com.android.car.settings.R;
28 import com.android.car.settings.common.FragmentController;
29 import com.android.car.settings.common.PreferenceController;
30 
31 /**
32  * Business Logic for security lock preferences. It can be extended to change which fragment should
33  * be opened when clicked.
34  */
35 public abstract class LockTypeBasePreferenceController extends PreferenceController<Preference> {
36 
37     private final CarUserManagerHelper mCarUserManagerHelper;
38     private byte[] mCurrentPassword;
39     private int mCurrentPasswordQuality;
40 
LockTypeBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)41     public LockTypeBasePreferenceController(Context context, String preferenceKey,
42             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
43         super(context, preferenceKey, fragmentController, uxRestrictions);
44         mCarUserManagerHelper = new CarUserManagerHelper(context);
45     }
46 
47 
48     @Override
getPreferenceType()49     protected Class<Preference> getPreferenceType() {
50         return Preference.class;
51     }
52 
53     /**
54      * Fragment specified here will be opened when the Preference is clicked. Return null to prevent
55      * navigation.
56      */
fragmentToOpen()57     protected abstract Fragment fragmentToOpen();
58 
59     /**
60      * If the current password quality is one of the values returned by this function, the
61      * controller will identify as having the current lock.
62      */
allowedPasswordQualities()63     protected abstract int[] allowedPasswordQualities();
64 
65 
66     /** Sets the quality of the current password. */
setCurrentPasswordQuality(int currentPasswordQuality)67     public void setCurrentPasswordQuality(int currentPasswordQuality) {
68         mCurrentPasswordQuality = currentPasswordQuality;
69     }
70 
71     /** Gets whether the preference related to this controller is the current lock type. */
isCurrentLock()72     protected boolean isCurrentLock() {
73         for (int allowedQuality : allowedPasswordQualities()) {
74             if (mCurrentPasswordQuality == allowedQuality) {
75                 return true;
76             }
77         }
78         return false;
79     }
80 
81     /** Sets the current password so it can be provided in the bundle in the fragment. */
setCurrentPassword(byte[] currentPassword)82     public void setCurrentPassword(byte[] currentPassword) {
83         mCurrentPassword = currentPassword;
84     }
85 
86     /** Gets the current password. */
getCurrentPassword()87     protected byte[] getCurrentPassword() {
88         return mCurrentPassword;
89     }
90 
91     @Override
updateState(Preference preference)92     protected void updateState(Preference preference) {
93         preference.setSummary(getSummary());
94     }
95 
96     @Override
handlePreferenceClicked(Preference preference)97     protected boolean handlePreferenceClicked(Preference preference) {
98         Fragment fragment = fragmentToOpen();
99         if (fragment != null) {
100             if (mCurrentPassword != null) {
101                 Bundle args = fragment.getArguments();
102                 if (args == null) {
103                     args = new Bundle();
104                 }
105                 args.putByteArray(PasswordHelper.EXTRA_CURRENT_SCREEN_LOCK, mCurrentPassword);
106                 fragment.setArguments(args);
107             }
108             getFragmentController().launchFragment(fragment);
109             return true;
110         }
111         return false;
112     }
113 
114     @Override
getAvailabilityStatus()115     public int getAvailabilityStatus() {
116         return mCarUserManagerHelper.isCurrentProcessGuestUser() ? DISABLED_FOR_USER : AVAILABLE;
117     }
118 
getSummary()119     private CharSequence getSummary() {
120         return isCurrentLock() ? getContext().getString(R.string.current_screen_lock) : "";
121     }
122 }
123