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 android.bluetooth.BluetoothAdapter; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 25 import androidx.annotation.StringRes; 26 27 import com.android.car.settings.R; 28 29 /** Dialog for changing the advertised name of the local bluetooth adapter. */ 30 public class LocalRenameDialogFragment extends BluetoothRenameDialogFragment { 31 32 /** Tag identifying the dialog for changing the name of the local Bluetooth adapter. */ 33 public static final String TAG = "LocalBluetoothRename"; 34 35 private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 36 37 private final IntentFilter mFilter = new IntentFilter( 38 BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); 39 40 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 41 @Override 42 public void onReceive(Context context, Intent intent) { 43 updateDeviceName(); 44 } 45 }; 46 47 @Override onStart()48 public void onStart() { 49 super.onStart(); 50 requireContext().registerReceiver(mReceiver, mFilter); 51 } 52 53 @Override onStop()54 public void onStop() { 55 super.onStop(); 56 requireContext().unregisterReceiver(mReceiver); 57 } 58 59 @Override 60 @StringRes getDialogTitle()61 protected int getDialogTitle() { 62 return R.string.bluetooth_rename_vehicle; 63 } 64 65 @Override getDeviceName()66 protected String getDeviceName() { 67 if (mBluetoothAdapter.isEnabled()) { 68 return mBluetoothAdapter.getName(); 69 } 70 return null; 71 } 72 73 @Override setDeviceName(String deviceName)74 protected void setDeviceName(String deviceName) { 75 mBluetoothAdapter.setName(deviceName); 76 } 77 } 78