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.bluetooth.BluetoothDevice; 20 import android.content.Context; 21 import android.graphics.drawable.Drawable; 22 import android.provider.DeviceConfig; 23 import android.util.Pair; 24 25 import androidx.preference.PreferenceFragmentCompat; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settings.R; 29 import com.android.settings.core.SettingsUIDeviceConfig; 30 import com.android.settings.widget.EntityHeaderController; 31 import com.android.settingslib.bluetooth.BluetoothUtils; 32 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 33 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; 34 import com.android.settingslib.bluetooth.LocalBluetoothManager; 35 import com.android.settingslib.core.lifecycle.Lifecycle; 36 import com.android.settingslib.widget.LayoutPreference; 37 38 /** 39 * This class adds a header with device name and status (connected/disconnected, etc.). 40 */ 41 public class BluetoothDetailsHeaderController extends BluetoothDetailsController { 42 private static final String KEY_DEVICE_HEADER = "bluetooth_device_header"; 43 44 private EntityHeaderController mHeaderController; 45 private LocalBluetoothManager mLocalManager; 46 private CachedBluetoothDeviceManager mDeviceManager; 47 BluetoothDetailsHeaderController(Context context, PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle, LocalBluetoothManager bluetoothManager)48 public BluetoothDetailsHeaderController(Context context, PreferenceFragmentCompat fragment, 49 CachedBluetoothDevice device, Lifecycle lifecycle, 50 LocalBluetoothManager bluetoothManager) { 51 super(context, fragment, device, lifecycle); 52 mLocalManager = bluetoothManager; 53 mDeviceManager = mLocalManager.getCachedDeviceManager(); 54 } 55 56 @Override isAvailable()57 public boolean isAvailable() { 58 final boolean advancedEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI, 59 SettingsUIDeviceConfig.BT_ADVANCED_HEADER_ENABLED, true); 60 return !advancedEnabled 61 || !BluetoothUtils.getBooleanMetaData(mCachedDevice.getDevice(), 62 BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET); 63 } 64 65 @Override init(PreferenceScreen screen)66 protected void init(PreferenceScreen screen) { 67 final LayoutPreference headerPreference = screen.findPreference(KEY_DEVICE_HEADER); 68 mHeaderController = EntityHeaderController.newInstance(mFragment.getActivity(), mFragment, 69 headerPreference.findViewById(R.id.entity_header)); 70 screen.addPreference(headerPreference); 71 } 72 setHeaderProperties()73 protected void setHeaderProperties() { 74 final Pair<Drawable, String> pair = 75 BluetoothUtils.getBtRainbowDrawableWithDescription(mContext, mCachedDevice); 76 String summaryText = mCachedDevice.getConnectionSummary(); 77 // If both the hearing aids are connected, two device status should be shown. 78 // If Second Summary is unavailable, to set it to null. 79 mHeaderController.setSecondSummary( 80 mDeviceManager.getSubDeviceSummary(mCachedDevice)); 81 mHeaderController.setLabel(mCachedDevice.getName()); 82 mHeaderController.setIcon(pair.first); 83 mHeaderController.setIconContentDescription(pair.second); 84 mHeaderController.setSummary(summaryText); 85 } 86 87 @Override refresh()88 protected void refresh() { 89 if (isAvailable()) { 90 setHeaderProperties(); 91 mHeaderController.done(mFragment.getActivity(), true /* rebindActions */); 92 } 93 } 94 95 @Override getPreferenceKey()96 public String getPreferenceKey() { 97 return KEY_DEVICE_HEADER; 98 } 99 }