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