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 static android.bluetooth.BluetoothDevice.BOND_NONE; 20 import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; 21 22 import android.app.settings.SettingsEnums; 23 import android.bluetooth.BluetoothDevice; 24 import android.content.Context; 25 import android.os.Bundle; 26 import android.provider.DeviceConfig; 27 import android.util.Log; 28 import android.view.Menu; 29 import android.view.MenuInflater; 30 import android.view.MenuItem; 31 32 import androidx.annotation.VisibleForTesting; 33 34 import com.android.settings.R; 35 import com.android.settings.core.SettingsUIDeviceConfig; 36 import com.android.settings.dashboard.RestrictedDashboardFragment; 37 import com.android.settings.overlay.FeatureFactory; 38 import com.android.settings.slices.BlockingSlicePrefController; 39 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 40 import com.android.settingslib.bluetooth.LocalBluetoothManager; 41 import com.android.settingslib.core.AbstractPreferenceController; 42 import com.android.settingslib.core.lifecycle.Lifecycle; 43 44 import java.util.ArrayList; 45 import java.util.List; 46 47 public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment { 48 public static final String KEY_DEVICE_ADDRESS = "device_address"; 49 private static final String TAG = "BTDeviceDetailsFrg"; 50 51 @VisibleForTesting 52 static int EDIT_DEVICE_NAME_ITEM_ID = Menu.FIRST; 53 54 /** 55 * An interface to let tests override the normal mechanism for looking up the 56 * CachedBluetoothDevice and LocalBluetoothManager, and substitute their own mocks instead. 57 * This is only needed in situations where you instantiate the fragment indirectly (eg via an 58 * intent) and can't use something like spying on an instance you construct directly via 59 * newInstance. 60 */ 61 @VisibleForTesting 62 interface TestDataFactory { getDevice(String deviceAddress)63 CachedBluetoothDevice getDevice(String deviceAddress); getManager(Context context)64 LocalBluetoothManager getManager(Context context); 65 } 66 67 @VisibleForTesting 68 static TestDataFactory sTestDataFactory; 69 70 @VisibleForTesting 71 String mDeviceAddress; 72 @VisibleForTesting 73 LocalBluetoothManager mManager; 74 @VisibleForTesting 75 CachedBluetoothDevice mCachedDevice; 76 BluetoothDeviceDetailsFragment()77 public BluetoothDeviceDetailsFragment() { 78 super(DISALLOW_CONFIG_BLUETOOTH); 79 } 80 81 @VisibleForTesting getLocalBluetoothManager(Context context)82 LocalBluetoothManager getLocalBluetoothManager(Context context) { 83 if (sTestDataFactory != null) { 84 return sTestDataFactory.getManager(context); 85 } 86 return Utils.getLocalBtManager(context); 87 } 88 89 @VisibleForTesting getCachedDevice(String deviceAddress)90 CachedBluetoothDevice getCachedDevice(String deviceAddress) { 91 if (sTestDataFactory != null) { 92 return sTestDataFactory.getDevice(deviceAddress); 93 } 94 BluetoothDevice remoteDevice = 95 mManager.getBluetoothAdapter().getRemoteDevice(deviceAddress); 96 return mManager.getCachedDeviceManager().findDevice(remoteDevice); 97 } 98 newInstance(String deviceAddress)99 public static BluetoothDeviceDetailsFragment newInstance(String deviceAddress) { 100 Bundle args = new Bundle(1); 101 args.putString(KEY_DEVICE_ADDRESS, deviceAddress); 102 BluetoothDeviceDetailsFragment fragment = new BluetoothDeviceDetailsFragment(); 103 fragment.setArguments(args); 104 return fragment; 105 } 106 107 @Override onAttach(Context context)108 public void onAttach(Context context) { 109 mDeviceAddress = getArguments().getString(KEY_DEVICE_ADDRESS); 110 mManager = getLocalBluetoothManager(context); 111 mCachedDevice = getCachedDevice(mDeviceAddress); 112 super.onAttach(context); 113 if (mCachedDevice == null) { 114 // Close this page if device is null with invalid device mac address 115 Log.w(TAG, "onAttach() CachedDevice is null!"); 116 finish(); 117 return; 118 } 119 use(AdvancedBluetoothDetailsHeaderController.class).init(mCachedDevice); 120 121 final BluetoothFeatureProvider featureProvider = FeatureFactory.getFactory( 122 context).getBluetoothFeatureProvider(context); 123 final boolean sliceEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI, 124 SettingsUIDeviceConfig.BT_SLICE_SETTINGS_ENABLED, true); 125 126 use(BlockingSlicePrefController.class).setSliceUri(sliceEnabled 127 ? featureProvider.getBluetoothDeviceSettingsUri(mCachedDevice.getDevice()) 128 : null); 129 } 130 131 @Override onResume()132 public void onResume() { 133 super.onResume(); 134 finishFragmentIfNecessary(); 135 } 136 137 @VisibleForTesting finishFragmentIfNecessary()138 void finishFragmentIfNecessary() { 139 if (mCachedDevice.getBondState() == BOND_NONE) { 140 finish(); 141 return; 142 } 143 } 144 145 @Override getMetricsCategory()146 public int getMetricsCategory() { 147 return SettingsEnums.BLUETOOTH_DEVICE_DETAILS; 148 } 149 150 @Override getLogTag()151 protected String getLogTag() { 152 return TAG; 153 } 154 155 @Override getPreferenceScreenResId()156 protected int getPreferenceScreenResId() { 157 return R.xml.bluetooth_device_details_fragment; 158 } 159 160 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)161 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 162 MenuItem item = menu.add(0, EDIT_DEVICE_NAME_ITEM_ID, 0, R.string.bluetooth_rename_button); 163 item.setIcon(com.android.internal.R.drawable.ic_mode_edit); 164 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 165 super.onCreateOptionsMenu(menu, inflater); 166 } 167 168 @Override onOptionsItemSelected(MenuItem menuItem)169 public boolean onOptionsItemSelected(MenuItem menuItem) { 170 if (menuItem.getItemId() == EDIT_DEVICE_NAME_ITEM_ID) { 171 RemoteDeviceNameDialogFragment.newInstance(mCachedDevice).show( 172 getFragmentManager(), RemoteDeviceNameDialogFragment.TAG); 173 return true; 174 } 175 return super.onOptionsItemSelected(menuItem); 176 } 177 178 @Override createPreferenceControllers(Context context)179 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 180 ArrayList<AbstractPreferenceController> controllers = new ArrayList<>(); 181 182 if (mCachedDevice != null) { 183 Lifecycle lifecycle = getSettingsLifecycle(); 184 controllers.add(new BluetoothDetailsHeaderController(context, this, mCachedDevice, 185 lifecycle, mManager)); 186 controllers.add(new BluetoothDetailsButtonsController(context, this, mCachedDevice, 187 lifecycle)); 188 controllers.add(new BluetoothDetailsCompanionAppsController(context, this, 189 mCachedDevice, lifecycle)); 190 controllers.add(new BluetoothDetailsProfilesController(context, this, mManager, 191 mCachedDevice, lifecycle)); 192 controllers.add(new BluetoothDetailsMacAddressController(context, this, mCachedDevice, 193 lifecycle)); 194 } 195 return controllers; 196 } 197 } 198