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.BluetoothDevice;
21 import android.content.Context;
22 import android.os.Bundle;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.settings.R;
27 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
28 import com.android.settingslib.bluetooth.LocalBluetoothManager;
29 
30 /** Provides a dialog for changing the display name of a remote bluetooth device. */
31 public class RemoteDeviceNameDialogFragment extends BluetoothNameDialogFragment {
32     public static final String TAG = "RemoteDeviceName";
33     private static final String KEY_CACHED_DEVICE_ADDRESS = "cached_device";
34 
35     private CachedBluetoothDevice mDevice;
36 
newInstance(CachedBluetoothDevice device)37     public static RemoteDeviceNameDialogFragment newInstance(CachedBluetoothDevice device) {
38         Bundle args = new Bundle(1);
39         args.putString(KEY_CACHED_DEVICE_ADDRESS, device.getDevice().getAddress());
40         RemoteDeviceNameDialogFragment fragment = new RemoteDeviceNameDialogFragment();
41         fragment.setArguments(args);
42         return fragment;
43     }
44 
45     @VisibleForTesting
getDevice(Context context)46     CachedBluetoothDevice getDevice(Context context) {
47         String deviceAddress = getArguments().getString(KEY_CACHED_DEVICE_ADDRESS);
48         LocalBluetoothManager manager = Utils.getLocalBtManager(context);
49         BluetoothDevice device = manager.getBluetoothAdapter().getRemoteDevice(deviceAddress);
50         return manager.getCachedDeviceManager().findDevice(device);
51     }
52 
53     @Override
onAttach(Context context)54     public void onAttach(Context context) {
55         super.onAttach(context);
56         mDevice = getDevice(context);
57     }
58 
59     @Override
getMetricsCategory()60     public int getMetricsCategory() {
61         return SettingsEnums.DIALOG_BLUETOOTH_PAIRED_DEVICE_RENAME;
62     }
63 
64     @Override
getDialogTitle()65     protected int getDialogTitle() {
66         return R.string.bluetooth_device_name;
67     }
68 
69     @Override
getDeviceName()70     protected String getDeviceName() {
71         if (mDevice != null) {
72             return mDevice.getName();
73         }
74         return null;
75     }
76 
77     @Override
setDeviceName(String deviceName)78     protected void setDeviceName(String deviceName) {
79         if (mDevice != null) {
80             mDevice.setName(deviceName);
81         }
82     }
83 }
84