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_CONFIG_BLUETOOTH; 20 21 import android.bluetooth.BluetoothAdapter; 22 import android.car.drivingstate.CarUxRestrictions; 23 import android.content.BroadcastReceiver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 28 import androidx.preference.Preference; 29 30 import com.android.car.settings.common.FragmentController; 31 32 /** 33 * Displays the name of the local Bluetooth adapter. When the associated preference is clicked, a 34 * dialog is shown to allow the user to update the adapter name. If the user has the {@link 35 * DISALLOW_CONFIG_BLUETOOTH} restriction, interaction with the preference is disabled. 36 */ 37 public class BluetoothNamePreferenceController extends BluetoothPreferenceController<Preference> { 38 39 private final IntentFilter mIntentFilter = new IntentFilter( 40 BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); 41 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 42 @Override 43 public void onReceive(Context context, Intent intent) { 44 refreshUi(); 45 } 46 }; 47 BluetoothNamePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)48 public BluetoothNamePreferenceController(Context context, String preferenceKey, 49 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 50 super(context, preferenceKey, fragmentController, uxRestrictions); 51 } 52 53 @Override getPreferenceType()54 protected Class<Preference> getPreferenceType() { 55 return Preference.class; 56 } 57 58 @Override onStartInternal()59 protected void onStartInternal() { 60 super.onStartInternal(); 61 getContext().registerReceiver(mReceiver, mIntentFilter); 62 } 63 64 @Override onStopInternal()65 protected void onStopInternal() { 66 super.onStopInternal(); 67 getContext().unregisterReceiver(mReceiver); 68 } 69 70 @Override updateState(Preference preference)71 protected void updateState(Preference preference) { 72 preference.setSelectable(!getUserManager().hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)); 73 preference.setSummary(BluetoothAdapter.getDefaultAdapter().getName()); 74 } 75 76 @Override handlePreferenceClicked(Preference preference)77 protected boolean handlePreferenceClicked(Preference preference) { 78 getFragmentController().showDialog(new LocalRenameDialogFragment(), 79 LocalRenameDialogFragment.TAG); 80 return true; 81 } 82 } 83