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.content.BroadcastReceiver; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.IntentFilter; 28 import android.content.pm.PackageManager; 29 import android.os.UserManager; 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 UserManager mUserManager; 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 mUserManager = UserManager.get(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_PROFILE : AVAILABLE; 82 } 83 isUserRestricted()84 private boolean isUserRestricted() { 85 return mUserManager.hasUserRestriction(DISALLOW_BLUETOOTH) 86 || mUserManager.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH); 87 } 88 89 @Override onStartInternal()90 protected void onStartInternal() { 91 getContext().registerReceiver(mReceiver, mIntentFilter); 92 } 93 94 @Override onStopInternal()95 protected void onStopInternal() { 96 getContext().unregisterReceiver(mReceiver); 97 } 98 99 @Override updateState(Preference preference)100 protected void updateState(Preference preference) { 101 preference.setSummary( 102 BluetoothAdapter.getDefaultAdapter().isEnabled() ? "" : getContext().getString( 103 R.string.bluetooth_pair_new_device_summary)); 104 } 105 106 @Override handlePreferenceClicked(Preference preference)107 protected boolean handlePreferenceClicked(Preference preference) { 108 // Enable the adapter if it is not on (user is notified via summary message). 109 BluetoothAdapter.getDefaultAdapter().enable(); 110 return false; // Don't handle so that preference framework will launch pairing fragment. 111 } 112 } 113