1 /* 2 * Copyright (C) 2019 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.development.bluetooth; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.util.Log; 22 import android.view.View; 23 import android.widget.RadioButton; 24 import android.widget.RadioGroup; 25 import android.widget.TextView; 26 27 import com.android.settings.R; 28 import com.android.settingslib.CustomDialogPreferenceCompat; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 /** 34 * Abstract class for Bluetooth A2DP config preference in developer option. 35 */ 36 public abstract class BaseBluetoothDialogPreference extends CustomDialogPreferenceCompat implements 37 RadioGroup.OnCheckedChangeListener{ 38 private static final String TAG = "BaseBluetoothDlgPref"; 39 40 protected List<Integer> mRadioButtonIds = new ArrayList<>(); 41 protected List<String> mRadioButtonStrings = new ArrayList<>(); 42 protected List<String> mSummaryStrings = new ArrayList<>(); 43 44 private Callback mCallback; 45 BaseBluetoothDialogPreference(Context context)46 public BaseBluetoothDialogPreference(Context context) { 47 super(context); 48 } 49 BaseBluetoothDialogPreference(Context context, AttributeSet attrs)50 public BaseBluetoothDialogPreference(Context context, AttributeSet attrs) { 51 super(context, attrs); 52 } 53 BaseBluetoothDialogPreference(Context context, AttributeSet attrs, int defStyleAttr)54 public BaseBluetoothDialogPreference(Context context, AttributeSet attrs, int defStyleAttr) { 55 super(context, attrs, defStyleAttr); 56 } 57 BaseBluetoothDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)58 public BaseBluetoothDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, 59 int defStyleRes) { 60 super(context, attrs, defStyleAttr, defStyleRes); 61 } 62 63 @Override onBindDialogView(View view)64 protected void onBindDialogView(View view) { 65 super.onBindDialogView(view); 66 if (mCallback == null) { 67 Log.e(TAG, "Unable to show dialog by the callback is null"); 68 return; 69 } 70 if (mRadioButtonStrings.size() != mRadioButtonIds.size()) { 71 Log.e(TAG, "Unable to show dialog by the view and string size are not matched"); 72 return; 73 } 74 final int currentIndex = mCallback.getCurrentConfigIndex(); 75 if (currentIndex < 0 || currentIndex >= mRadioButtonIds.size()) { 76 Log.e(TAG, "Unable to show dialog by the incorrect index: " + currentIndex); 77 return; 78 } 79 // Initial radio button group 80 final RadioGroup radioGroup = view.findViewById(getRadioButtonGroupId()); 81 if (radioGroup == null) { 82 Log.e(TAG, "Unable to show dialog by no radio button group: " 83 + getRadioButtonGroupId()); 84 return; 85 } 86 radioGroup.check(mRadioButtonIds.get(currentIndex)); 87 radioGroup.setOnCheckedChangeListener(this); 88 // Initial radio button 89 final List<Integer> selectableIndex = mCallback.getSelectableIndex(); 90 RadioButton radioButton; 91 for (int i = 0; i < mRadioButtonStrings.size(); i++) { 92 radioButton = view.findViewById(mRadioButtonIds.get(i)); 93 if (radioButton == null) { 94 Log.e(TAG, "Unable to show dialog by no radio button:" + mRadioButtonIds.get(i)); 95 return; 96 } 97 radioButton.setText(mRadioButtonStrings.get(i)); 98 radioButton.setEnabled(selectableIndex.contains(i)); 99 } 100 // Initial help information text view 101 final TextView helpTextView = view.findViewById(R.id.bluetooth_audio_codec_help_info); 102 if (selectableIndex.size() == mRadioButtonIds.size()) { 103 // View will be invisible when all options are enabled. 104 helpTextView.setVisibility(View.GONE); 105 } else { 106 helpTextView.setText( 107 com.android.settingslib.R.string.bluetooth_select_a2dp_codec_type_help_info); 108 helpTextView.setVisibility(View.VISIBLE); 109 } 110 } 111 112 @Override onCheckedChanged(RadioGroup group, int checkedId)113 public void onCheckedChanged(RadioGroup group, int checkedId) { 114 if (mCallback == null) { 115 Log.e(TAG, "Callback is null"); 116 return; 117 } 118 mCallback.onIndexUpdated(mRadioButtonIds.indexOf(checkedId)); 119 getDialog().dismiss(); 120 } 121 122 /** 123 * Method to set callback. 124 */ setCallback(Callback callback)125 public void setCallback(Callback callback) { 126 mCallback = callback; 127 } 128 129 /** 130 * Method to get summary strings by index. 131 */ generateSummary(int index)132 protected String generateSummary(int index) { 133 if (index > mSummaryStrings.size()) { 134 Log.e(TAG, "Unable to get summary of " + index + ". Size is " + mSummaryStrings.size()); 135 return null; 136 } 137 return index == getDefaultIndex() ? mSummaryStrings.get(getDefaultIndex()) : 138 String.format(getContext().getResources().getString( 139 com.android.settingslib.R 140 .string.bluetooth_select_a2dp_codec_streaming_label), 141 mSummaryStrings.get(index)); 142 } 143 144 /** 145 * Method to get default index. 146 */ getDefaultIndex()147 protected int getDefaultIndex() { 148 return 0; 149 } 150 151 /** 152 * Method to get radio button group id. 153 */ getRadioButtonGroupId()154 protected abstract int getRadioButtonGroupId(); 155 156 /** 157 * Callback interface for this class to manipulate data from controller. 158 */ 159 public interface Callback { 160 /** 161 * Method to get current Bluetooth A2DP config index. 162 */ getCurrentConfigIndex()163 int getCurrentConfigIndex(); 164 /** 165 * Method to get selectable config index which means supported by phone and device. 166 * 167 * @return the available {@link List} of the Bluetooth A2DP config. 168 */ getSelectableIndex()169 List<Integer> getSelectableIndex(); 170 /** 171 * Method to notify controller when user changes config. 172 * 173 * @param index for the selected index. 174 */ onIndexUpdated(int index)175 void onIndexUpdated(int index); 176 } 177 } 178