• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.flags.Flags;
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.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 
BluetoothDetailsHeaderController(Context context, PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle)44     public BluetoothDetailsHeaderController(Context context, PreferenceFragmentCompat fragment,
45             CachedBluetoothDevice device, Lifecycle lifecycle) {
46         super(context, fragment, device, lifecycle);
47     }
48 
49     @Override
isAvailable()50     public boolean isAvailable() {
51         if (Flags.enableBluetoothDeviceDetailsPolish()) {
52             return false;
53         }
54         boolean hasLeAudio = mCachedDevice.getUiAccessibleProfiles()
55                 .stream()
56                 .anyMatch(profile -> profile.getProfileId() == BluetoothProfile.LE_AUDIO);
57         return !BluetoothUtils.isAdvancedDetailsHeader(mCachedDevice.getDevice()) && !hasLeAudio;
58     }
59 
60     @Override
init(PreferenceScreen screen)61     protected void init(PreferenceScreen screen) {
62         final LayoutPreference headerPreference = screen.findPreference(KEY_DEVICE_HEADER);
63         mHeaderController = EntityHeaderController.newInstance(mFragment.getActivity(), mFragment,
64                 headerPreference.findViewById(R.id.entity_header));
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         }
75 
76         mHeaderController.setLabel(mCachedDevice.getName());
77         mHeaderController.setIcon(pair.first);
78         mHeaderController.setIconContentDescription(pair.second);
79         mHeaderController.setSummary(summaryText);
80     }
81 
82     @Override
refresh()83     protected void refresh() {
84         if (isAvailable()) {
85             setHeaderProperties();
86             mHeaderController.done(true /* rebindActions */);
87         }
88     }
89 
90     @Override
getPreferenceKey()91     public String getPreferenceKey() {
92         return KEY_DEVICE_HEADER;
93     }
94 }