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