• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.os.UserManager.DISALLOW_BLUETOOTH;
20 import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
21 
22 import android.app.admin.DevicePolicyManager;
23 import android.bluetooth.BluetoothAdapter;
24 import android.car.drivingstate.CarUxRestrictions;
25 import android.car.userlib.CarUserManagerHelper;
26 import android.content.Context;
27 import android.content.pm.PackageManager;
28 
29 import androidx.preference.Preference;
30 
31 import com.android.car.settings.R;
32 import com.android.car.settings.common.FragmentController;
33 import com.android.car.settings.common.PreferenceController;
34 import com.android.internal.widget.LockPatternUtils;
35 
36 /**
37  * Business logic which controls whether the preference is clickeble according to the password
38  * quality of current user.
39  */
40 public class AddTrustedDevicePreferenceController extends PreferenceController<Preference> {
41     private CarUserManagerHelper mCarUserManagerHelper;
42     private LockPatternUtils mLockPatternUtils;
43 
AddTrustedDevicePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44     public AddTrustedDevicePreferenceController(Context context, String preferenceKey,
45             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
46         super(context, preferenceKey, fragmentController, uxRestrictions);
47         mCarUserManagerHelper = new CarUserManagerHelper(context);
48         mLockPatternUtils = new LockPatternUtils(context);
49     }
50 
51     @Override
getAvailabilityStatus()52     protected int getAvailabilityStatus() {
53         if (!getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
54             return UNSUPPORTED_ON_DEVICE;
55         }
56         return isUserRestricted() ? DISABLED_FOR_USER : AVAILABLE;
57     }
58 
isUserRestricted()59     private boolean isUserRestricted() {
60         return mCarUserManagerHelper.isCurrentProcessUserHasRestriction(DISALLOW_BLUETOOTH)
61                 || mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
62                 DISALLOW_CONFIG_BLUETOOTH);
63     }
64 
65     @Override
updateState(Preference preference)66     protected void updateState(Preference preference) {
67         preference.setSummary(
68                 BluetoothAdapter.getDefaultAdapter().isEnabled() ? "" : getContext().getString(
69                         R.string.add_device_summary));
70         preference.setEnabled(hasPassword());
71     }
72 
hasPassword()73     private boolean hasPassword() {
74         return mLockPatternUtils.getKeyguardStoredPasswordQuality(
75                 mCarUserManagerHelper.getCurrentProcessUserId())
76                 != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
77     }
78 
79     @Override
getPreferenceType()80     protected Class<Preference> getPreferenceType() {
81         return Preference.class;
82     }
83 
84     @Override
handlePreferenceClicked(Preference preference)85     public boolean handlePreferenceClicked(Preference preference) {
86         // Enable the adapter if it is not on (user is notified via summary message).
87         BluetoothAdapter.getDefaultAdapter().enable();
88         /** Need to start {@link AddTrustedDeviceActivity} after the click. */
89         return false;
90     }
91 }
92