• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.settings.SettingsEnums;
20 import android.bluetooth.BluetoothAdapter;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.os.Bundle;
26 
27 import com.android.settings.R;
28 
29 /** Provides a dialog for changing the advertised name of the local bluetooth adapter. */
30 public class LocalDeviceNameDialogFragment extends BluetoothNameDialogFragment {
31     public static final String TAG = "LocalAdapterName";
32     private BluetoothAdapter mBluetoothAdapter;
33 
34     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
35         @Override
36         public void onReceive(Context context, Intent intent) {
37             String action = intent.getAction();
38             if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action) ||
39                     (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action) &&
40                             intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
41                                     == BluetoothAdapter.STATE_ON)) {
42                 updateDeviceName();
43             }
44         }
45     };
46 
47     @Override
onCreate(Bundle savedInstanceState)48     public void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
51     }
52 
53     @Override
onResume()54     public void onResume() {
55         super.onResume();
56         IntentFilter filter = new IntentFilter();
57         filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
58         filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
59         getActivity().registerReceiver(mReceiver, filter);
60     }
61 
62     @Override
onPause()63     public void onPause() {
64         super.onPause();
65         getActivity().unregisterReceiver(mReceiver);
66     }
67 
68     @Override
getMetricsCategory()69     public int getMetricsCategory() {
70         return SettingsEnums.DIALOG_BLUETOOTH_RENAME;
71     }
72 
73     @Override
getDialogTitle()74     protected int getDialogTitle() {
75         return R.string.bluetooth_rename_device;
76     }
77 
78     @Override
getDeviceName()79     protected String getDeviceName() {
80         if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
81             return mBluetoothAdapter.getName();
82         }
83         return null;
84     }
85 
86     @Override
setDeviceName(String deviceName)87     protected void setDeviceName(String deviceName) {
88         mBluetoothAdapter.setName(deviceName);
89     }
90 }
91