1 /* 2 * Copyright 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.bluetooth; 18 19 import static android.os.UserManager.DISALLOW_BLUETOOTH; 20 import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; 21 22 import android.bluetooth.BluetoothAdapter; 23 import android.car.drivingstate.CarUxRestrictions; 24 import android.car.userlib.CarUserManagerHelper; 25 import android.content.BroadcastReceiver; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.IntentFilter; 29 import android.content.pm.PackageManager; 30 31 import androidx.lifecycle.LifecycleObserver; 32 import androidx.preference.Preference; 33 34 import com.android.car.settings.R; 35 import com.android.car.settings.common.FragmentController; 36 import com.android.car.settings.common.PreferenceController; 37 38 /** 39 * Controls a preference that, when clicked, launches the page for pairing new Bluetooth devices. 40 * The associated preference for this controller should define the fragment attribute or an intent 41 * to launch for the Bluetooth device pairing page. If the adapter is not enabled, a click will 42 * enable Bluetooth. The summary message is updated to indicate this effect to the user. 43 */ 44 public class PairNewDevicePreferenceController extends PreferenceController<Preference> implements 45 LifecycleObserver { 46 47 private final CarUserManagerHelper mCarUserManagerHelper; 48 private final IntentFilter mIntentFilter = new IntentFilter( 49 BluetoothAdapter.ACTION_STATE_CHANGED); 50 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 51 @Override 52 public void onReceive(Context context, Intent intent) { 53 refreshUi(); 54 } 55 }; 56 PairNewDevicePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)57 public PairNewDevicePreferenceController(Context context, String preferenceKey, 58 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 59 super(context, preferenceKey, fragmentController, uxRestrictions); 60 mCarUserManagerHelper = new CarUserManagerHelper(context); 61 } 62 63 @Override getPreferenceType()64 protected Class<Preference> getPreferenceType() { 65 return Preference.class; 66 } 67 68 @Override checkInitialized()69 protected void checkInitialized() { 70 if (getPreference().getIntent() == null && getPreference().getFragment() == null) { 71 throw new IllegalStateException( 72 "Preference should declare fragment or intent for page to pair new devices"); 73 } 74 } 75 76 @Override getAvailabilityStatus()77 protected int getAvailabilityStatus() { 78 if (!getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) { 79 return UNSUPPORTED_ON_DEVICE; 80 } 81 return isUserRestricted() ? DISABLED_FOR_USER : AVAILABLE; 82 } 83 isUserRestricted()84 private boolean isUserRestricted() { 85 return mCarUserManagerHelper.isCurrentProcessUserHasRestriction(DISALLOW_BLUETOOTH) 86 || mCarUserManagerHelper.isCurrentProcessUserHasRestriction( 87 DISALLOW_CONFIG_BLUETOOTH); 88 } 89 90 @Override onStartInternal()91 protected void onStartInternal() { 92 getContext().registerReceiver(mReceiver, mIntentFilter); 93 } 94 95 @Override onStopInternal()96 protected void onStopInternal() { 97 getContext().unregisterReceiver(mReceiver); 98 } 99 100 @Override updateState(Preference preference)101 protected void updateState(Preference preference) { 102 preference.setSummary( 103 BluetoothAdapter.getDefaultAdapter().isEnabled() ? "" : getContext().getString( 104 R.string.bluetooth_pair_new_device_summary)); 105 } 106 107 @Override handlePreferenceClicked(Preference preference)108 protected boolean handlePreferenceClicked(Preference preference) { 109 // Enable the adapter if it is not on (user is notified via summary message). 110 BluetoothAdapter.getDefaultAdapter().enable(); 111 return false; // Don't handle so that preference framework will launch pairing fragment. 112 } 113 } 114