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 package com.android.car.settings.bluetooth; 17 18 import android.bluetooth.BluetoothDevice; 19 import android.os.Bundle; 20 import android.view.View; 21 import android.widget.Button; 22 23 import com.android.car.list.EditTextLineItem; 24 import com.android.car.list.SingleTextLineItem; 25 import com.android.car.list.TypedPagedListAdapter; 26 import com.android.car.settings.R; 27 import com.android.car.settings.common.ListSettingsFragment; 28 import com.android.car.settings.common.Logger; 29 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 30 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; 31 import com.android.settingslib.bluetooth.LocalBluetoothManager; 32 import com.android.settingslib.bluetooth.LocalBluetoothProfile; 33 34 import java.util.ArrayList; 35 36 /** 37 * Shows details about a bluetooth device, including actions related to the device, 38 * e.g. forget etc. The intent should include information about the device, use that to 39 * render UI, e.g. show name etc. 40 */ 41 public class BluetoothDetailFragment extends ListSettingsFragment implements 42 BluetoothProfileLineItem.DataChangedListener { 43 private static final Logger LOG = new Logger(BluetoothDetailFragment.class); 44 45 public static final String EXTRA_BT_DEVICE = "extra_bt_device"; 46 47 private BluetoothDevice mDevice; 48 private CachedBluetoothDevice mCachedDevice; 49 50 private CachedBluetoothDeviceManager mDeviceManager; 51 private LocalBluetoothManager mLocalManager; 52 private EditTextLineItem mInputLineItem; 53 private Button mOkButton; 54 getInstance(BluetoothDevice btDevice)55 public static BluetoothDetailFragment getInstance(BluetoothDevice btDevice) { 56 BluetoothDetailFragment bluetoothDetailFragment = new BluetoothDetailFragment(); 57 Bundle bundle = ListSettingsFragment.getBundle(); 58 bundle.putParcelable(EXTRA_BT_DEVICE, btDevice); 59 bundle.putInt(EXTRA_TITLE_ID, R.string.bluetooth_settings); 60 bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar_with_button); 61 bluetoothDetailFragment.setArguments(bundle); 62 return bluetoothDetailFragment; 63 } 64 65 @Override onCreate(Bundle savedInstanceState)66 public void onCreate(Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 mDevice = getArguments().getParcelable(EXTRA_BT_DEVICE); 69 mLocalManager = 70 LocalBluetoothManager.getInstance(getContext(), /* onInitCallback= */ null); 71 if (mLocalManager == null) { 72 LOG.e("Bluetooth is not supported on this device"); 73 return; 74 } 75 mDeviceManager = mLocalManager.getCachedDeviceManager(); 76 mCachedDevice = mDeviceManager.findDevice(mDevice); 77 if (mCachedDevice == null) { 78 mCachedDevice = mDeviceManager.addDevice( 79 mLocalManager.getBluetoothAdapter(), 80 mLocalManager.getProfileManager(), 81 mDevice); 82 } 83 } 84 85 @Override onActivityCreated(Bundle savedInstanceState)86 public void onActivityCreated(Bundle savedInstanceState) { 87 if (mDevice == null) { 88 LOG.w("No bluetooth device set."); 89 return; 90 } 91 super.onActivityCreated(savedInstanceState); 92 93 setupForgetButton(); 94 setupOkButton(); 95 } 96 97 @Override onDataChanged()98 public void onDataChanged() { 99 mPagedListAdapter.notifyDataSetChanged(); 100 } 101 102 @Override getLineItems()103 public ArrayList<TypedPagedListAdapter.LineItem> getLineItems() { 104 ArrayList<TypedPagedListAdapter.LineItem> lineItems = new ArrayList<>(); 105 mInputLineItem = new EditTextLineItem( 106 getContext().getText(R.string.bluetooth_preference_paired_dialog_name_label), 107 mCachedDevice.getName()); 108 mInputLineItem.setTextType(EditTextLineItem.TextType.TEXT); 109 lineItems.add(mInputLineItem); 110 lineItems.add(new SingleTextLineItem(getContext().getText( 111 R.string.bluetooth_device_advanced_profile_header_title))); 112 addProfileLineItems(lineItems); 113 return lineItems; 114 } 115 addProfileLineItems(ArrayList<TypedPagedListAdapter.LineItem> lineItems)116 private void addProfileLineItems(ArrayList<TypedPagedListAdapter.LineItem> lineItems) { 117 for (LocalBluetoothProfile profile : mCachedDevice.getConnectableProfiles()) { 118 lineItems.add(new BluetoothProfileLineItem( 119 getContext(), profile, mCachedDevice, this)); 120 } 121 } 122 setupForgetButton()123 private void setupForgetButton() { 124 Button fortgetButton = getActivity().findViewById(R.id.action_button2); 125 fortgetButton.setVisibility(View.VISIBLE); 126 fortgetButton.setText(R.string.forget); 127 fortgetButton.setOnClickListener(v -> { 128 mCachedDevice.unpair(); 129 getFragmentController().goBack(); 130 }); 131 } 132 setupOkButton()133 private void setupOkButton() { 134 mOkButton = getActivity().findViewById(R.id.action_button1); 135 mOkButton.setText(android.R.string.ok); 136 mOkButton.setOnClickListener(v -> { 137 if (!mInputLineItem.getInput().equals(mCachedDevice.getName())) { 138 mCachedDevice.setName(mInputLineItem.getInput()); 139 } 140 getFragmentController().goBack(); 141 }); 142 } 143 } 144