1 /* 2 * Copyright (C) 2024 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.util.Pair; 23 import android.view.View; 24 import android.widget.ImageButton; 25 import android.widget.ImageView; 26 import android.widget.TextView; 27 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 import androidx.preference.PreferenceFragmentCompat; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.R; 34 import com.android.settings.flags.Flags; 35 import com.android.settingslib.bluetooth.BluetoothUtils; 36 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 37 import com.android.settingslib.core.lifecycle.Lifecycle; 38 import com.android.settingslib.widget.LayoutPreference; 39 40 /** This class adds a header with device name and status (connected/disconnected, etc.). */ 41 public class GeneralBluetoothDetailsHeaderController extends BluetoothDetailsController { 42 private static final String KEY_GENERAL_DEVICE_HEADER = "general_bluetooth_device_header"; 43 44 @Nullable 45 private LayoutPreference mLayoutPreference; 46 GeneralBluetoothDetailsHeaderController( Context context, PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle)47 public GeneralBluetoothDetailsHeaderController( 48 Context context, 49 PreferenceFragmentCompat fragment, 50 CachedBluetoothDevice device, 51 Lifecycle lifecycle) { 52 super(context, fragment, device, lifecycle); 53 } 54 55 @Override isAvailable()56 public boolean isAvailable() { 57 if (!Flags.enableBluetoothDeviceDetailsPolish()) { 58 return false; 59 } 60 boolean hasLeAudio = 61 mCachedDevice.getUiAccessibleProfiles().stream() 62 .anyMatch(profile -> profile.getProfileId() == BluetoothProfile.LE_AUDIO); 63 return !BluetoothUtils.isAdvancedDetailsHeader(mCachedDevice.getDevice()) && !hasLeAudio; 64 } 65 66 @Override init(PreferenceScreen screen)67 protected void init(PreferenceScreen screen) { 68 mLayoutPreference = screen.findPreference(KEY_GENERAL_DEVICE_HEADER); 69 } 70 71 @Override refresh()72 protected void refresh() { 73 if (!isAvailable() || mLayoutPreference == null) { 74 return; 75 } 76 ImageView imageView = mLayoutPreference.findViewById(R.id.bt_header_icon); 77 if (imageView != null) { 78 final Pair<Drawable, String> pair = 79 BluetoothUtils.getBtRainbowDrawableWithDescription(mContext, mCachedDevice); 80 imageView.setImageDrawable(pair.first); 81 imageView.setContentDescription(pair.second); 82 } 83 84 TextView title = mLayoutPreference.findViewById(R.id.bt_header_device_name); 85 if (title != null) { 86 title.setText(mCachedDevice.getName()); 87 } 88 TextView summary = mLayoutPreference.findViewById(R.id.bt_header_connection_summary); 89 if (summary != null) { 90 summary.setText(mCachedDevice.getConnectionSummary()); 91 } 92 boolean isTempBond = com.android.settingslib.flags.Flags.enableTemporaryBondDevicesUi() 93 && BluetoothUtils.isTemporaryBondDevice(mCachedDevice.getDevice()); 94 if (!isTempBond) { 95 ImageButton renameButton = mLayoutPreference.findViewById(R.id.rename_button); 96 renameButton.setVisibility(View.VISIBLE); 97 renameButton.setOnClickListener( 98 view -> { 99 RemoteDeviceNameDialogFragment.newInstance(mCachedDevice) 100 .show( 101 mFragment.getFragmentManager(), 102 RemoteDeviceNameDialogFragment.TAG); 103 }); 104 } 105 } 106 107 @Override 108 @NonNull getPreferenceKey()109 public String getPreferenceKey() { 110 return KEY_GENERAL_DEVICE_HEADER; 111 } 112 } 113