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