1 /* 2 * Copyright (C) 2017 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.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 import android.os.Bundle; 25 26 import com.android.internal.logging.nano.MetricsProto; 27 import com.android.settings.R; 28 import com.android.settingslib.bluetooth.LocalBluetoothAdapter; 29 import com.android.settingslib.bluetooth.LocalBluetoothManager; 30 31 /** Provides a dialog for changing the advertised name of the local bluetooth adapter. */ 32 public class LocalDeviceNameDialogFragment extends BluetoothNameDialogFragment { 33 public static final String TAG = "LocalAdapterName"; 34 private LocalBluetoothAdapter mLocalAdapter; 35 newInstance()36 public static LocalDeviceNameDialogFragment newInstance() { 37 return new LocalDeviceNameDialogFragment(); 38 } 39 40 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 41 @Override 42 public void onReceive(Context context, Intent intent) { 43 String action = intent.getAction(); 44 if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action) || 45 (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action) && 46 intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR) 47 == BluetoothAdapter.STATE_ON)) { 48 updateDeviceName(); 49 } 50 } 51 }; 52 53 @Override onCreate(Bundle savedInstanceState)54 public void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 LocalBluetoothManager localManager = Utils.getLocalBtManager(getActivity()); 57 mLocalAdapter = localManager.getBluetoothAdapter(); 58 } 59 60 @Override onResume()61 public void onResume() { 62 super.onResume(); 63 IntentFilter filter = new IntentFilter(); 64 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 65 filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); 66 getActivity().registerReceiver(mReceiver, filter); 67 } 68 69 @Override onPause()70 public void onPause() { 71 super.onPause(); 72 getActivity().unregisterReceiver(mReceiver); 73 } 74 75 @Override getMetricsCategory()76 public int getMetricsCategory() { 77 return MetricsProto.MetricsEvent.DIALOG_BLUETOOTH_RENAME; 78 } 79 80 @Override getDialogTitle()81 protected int getDialogTitle() { 82 return R.string.bluetooth_rename_device; 83 } 84 85 @Override getDeviceName()86 protected String getDeviceName() { 87 if (mLocalAdapter != null && mLocalAdapter.isEnabled()) { 88 return mLocalAdapter.getName(); 89 } 90 return null; 91 } 92 93 @Override setDeviceName(String deviceName)94 protected void setDeviceName(String deviceName) { 95 mLocalAdapter.setName(deviceName); 96 } 97 } 98