1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/******************************************************************************
* Copyright (c) 2020, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
package com.android.settings.widget;
import android.content.Context;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.StringRes;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
/** A preference which provide Group action buttons (connect, disconnect,
* refresh, cacnelRefresh, forget
**/
public class GroupOptionsPreference extends Preference {
private static final String TAG = "GroupOptionsPreference";
private final ButtonInfo mBtnAddSrcGroup = new ButtonInfo();
private final ButtonInfo mBtnConnect = new ButtonInfo();
private final ButtonInfo mBtnForget = new ButtonInfo();
private final ButtonInfo mBtnDisconnect = new ButtonInfo();
private final ButtonInfo mBtnRefresh = new ButtonInfo();
private final ButtonInfo mBtnCancelRefresh = new ButtonInfo();
private final TextViewInfo mTvGroupId = new TextViewInfo();
private final TextViewInfo mTvStatus = new TextViewInfo();
private final ProgressInfo mProgressScan = new ProgressInfo();
public GroupOptionsPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public GroupOptionsPreference(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public GroupOptionsPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setLayoutResource(R.layout.bluetooth_group_options);
setSelectable(false);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
holder.setDividerAllowedAbove(true);
holder.setDividerAllowedBelow(true);
mBtnAddSrcGroup.mButton = (Button) holder.findViewById(R.id.id_btn_group_add_source);
mBtnConnect.mButton = (Button) holder.findViewById(R.id.id_btn_connect);
mBtnForget.mButton = (Button) holder.findViewById(R.id.id_btn_forget);
mBtnDisconnect.mButton = (Button) holder.findViewById(R.id.id_btn_disconnect);
mBtnRefresh.mButton = (Button) holder.findViewById(R.id.id_btn_refresh);
mBtnCancelRefresh.mButton = (Button) holder.findViewById(R.id.id_btn_refresh_cancel);
mTvGroupId.mTextView = (TextView)holder.findViewById(R.id.id_tv_groupid);
mTvStatus.mTextView =(TextView)holder.findViewById(R.id.id_tv_status);
mProgressScan.mProgress = (ProgressBar)holder.findViewById(R.id.id_progress_group_scan);
mBtnAddSrcGroup.setUpButton();
mBtnConnect.setUpButton();
mBtnForget.setUpButton();
mBtnDisconnect.setUpButton();
mBtnRefresh.setUpButton();
mBtnCancelRefresh.setUpButton();
mTvGroupId.setUpTextView();
mTvStatus.setUpTextView();
mProgressScan.setUpProgress();
}
public GroupOptionsPreference setAddSourceGroupButtonVisible(boolean isVisible) {
if (isVisible != mBtnAddSrcGroup.mIsVisible) {
mBtnAddSrcGroup.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setAddSourceGroupButtonText(@StringRes int textResId) {
final String newText = getContext().getString(textResId);
if (!TextUtils.equals(newText, mBtnAddSrcGroup.mText)) {
mBtnAddSrcGroup.mText = newText;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setAddSourceGroupButtonEnabled(boolean isEnabled) {
if (isEnabled != mBtnAddSrcGroup.mIsEnabled) {
mBtnAddSrcGroup.mIsEnabled = isEnabled;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setAddSourceGroupButtonOnClickListener(
View.OnClickListener listener) {
if (listener != mBtnAddSrcGroup.mListener) {
mBtnAddSrcGroup.mListener = listener;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setConnectButtonVisible(boolean isVisible) {
if (isVisible != mBtnConnect.mIsVisible) {
mBtnConnect.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setConnectButtonText(@StringRes int textResId) {
final String newText = getContext().getString(textResId);
if (!TextUtils.equals(newText, mBtnConnect.mText)) {
mBtnConnect.mText = newText;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setConnectButtonEnabled(boolean isEnabled) {
if (isEnabled != mBtnConnect.mIsEnabled) {
mBtnConnect.mIsEnabled = isEnabled;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setConnectButtonOnClickListener(
View.OnClickListener listener) {
if (listener != mBtnConnect.mListener) {
mBtnConnect.mListener = listener;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setForgettButtonVisible(boolean isVisible) {
if (isVisible != mBtnForget.mIsVisible) {
mBtnForget.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setForgetButtonText(@StringRes int textResId) {
final String newText = getContext().getString(textResId);
if (!TextUtils.equals(newText, mBtnForget.mText)) {
mBtnForget.mText = newText;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setForgetButtonEnabled(boolean isEnabled) {
if (isEnabled != mBtnForget.mIsEnabled) {
mBtnForget.mIsEnabled = isEnabled;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setForgetButtonOnClickListener(
View.OnClickListener listener) {
if (listener != mBtnForget.mListener) {
mBtnForget.mListener = listener;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setDisconnectButtonVisible(boolean isVisible) {
if (isVisible != mBtnDisconnect.mIsVisible) {
mBtnDisconnect.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setDisconnectButtonText(@StringRes int textResId) {
final String newText = getContext().getString(textResId);
if (!TextUtils.equals(newText, mBtnDisconnect.mText)) {
mBtnDisconnect.mText = newText;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setDisconnectButtonEnabled(boolean isEnabled) {
if (isEnabled != mBtnDisconnect.mIsEnabled) {
mBtnDisconnect.mIsEnabled = isEnabled;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setDisconnectButtonOnClickListener(
View.OnClickListener listener) {
if (listener != mBtnDisconnect.mListener) {
mBtnDisconnect.mListener = listener;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setRefreshButtonVisible(boolean isVisible) {
if (isVisible != mBtnRefresh.mIsVisible) {
mBtnRefresh.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setRefreshButtonText(@StringRes int textResId) {
final String newText = getContext().getString(textResId);
if (!TextUtils.equals(newText, mBtnRefresh.mText)) {
mBtnRefresh.mText = newText;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setRefreshButtonEnabled(boolean isEnabled) {
if (isEnabled != mBtnRefresh.mIsEnabled) {
mBtnRefresh.mIsEnabled = isEnabled;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setRefreshButtonOnClickListener(
View.OnClickListener listener) {
if (listener != mBtnRefresh.mListener) {
mBtnRefresh.mListener = listener;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setCancelRefreshButtonText(@StringRes int textResId) {
final String newText = getContext().getString(textResId);
if (!TextUtils.equals(newText, mBtnCancelRefresh.mText)) {
mBtnCancelRefresh.mText = newText;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setCancelRefreshButtonVisible(boolean isVisible) {
if (isVisible != mBtnCancelRefresh.mIsVisible) {
mBtnCancelRefresh.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setCancelRefreshButtonEnabled(boolean isEnabled) {
if (isEnabled != mBtnCancelRefresh.mIsEnabled) {
mBtnCancelRefresh.mIsEnabled = isEnabled;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setCancelRefreshButtonOnClickListener(
View.OnClickListener listener) {
if (listener != mBtnCancelRefresh.mListener) {
mBtnCancelRefresh.mListener = listener;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setTvSetIdVisible(boolean isVisible) {
if (isVisible != mTvGroupId.mIsVisible) {
mTvGroupId.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setTextViewText(String newText) {
if (!TextUtils.equals(newText, mTvGroupId.mText)) {
mTvGroupId.mText = newText;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setTvStatusVisible(boolean isVisible) {
if (isVisible != mTvStatus.mIsVisible) {
mTvStatus.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setTexStatusText(@StringRes int textResId) {
final String newText = getContext().getString(textResId);
if (!TextUtils.equals(newText, mTvStatus.mText)) {
mTvStatus.mText = newText;
notifyChanged();
}
return this;
}
public GroupOptionsPreference setProgressScanVisible(boolean isVisible) {
if (isVisible != mProgressScan.mIsVisible) {
mProgressScan.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
static class ButtonInfo {
private Button mButton;
private CharSequence mText;
private View.OnClickListener mListener;
private boolean mIsEnabled = true;
private boolean mIsVisible = true;
void setUpButton() {
mButton.setText(mText);
mButton.setOnClickListener(mListener);
mButton.setEnabled(mIsEnabled);
mButton.setTypeface(null, Typeface.BOLD);
if (shouldBeVisible()) {
mButton.setVisibility(View.VISIBLE);
} else {
mButton.setVisibility(View.GONE);
}
}
private boolean shouldBeVisible() {
return mIsVisible && (!TextUtils.isEmpty(mText));
}
}
static class TextViewInfo {
private TextView mTextView;
private CharSequence mText;
private boolean mIsVisible = true;
void setUpTextView() {
mTextView.setText(mText);
mTextView.setTypeface(null, Typeface.BOLD);
if (shouldBeVisible()) {
mTextView.setVisibility(View.VISIBLE);
} else {
mTextView.setVisibility(View.INVISIBLE);
}
}
private boolean shouldBeVisible() {
return mIsVisible && (!TextUtils.isEmpty(mText));
}
}
static class ProgressInfo {
private ProgressBar mProgress;
private boolean mIsVisible = true;
void setUpProgress() {
if (shouldBeVisible()) {
mProgress.setVisibility(View.VISIBLE);
} else {
mProgress.setVisibility(View.INVISIBLE);
}
}
private boolean shouldBeVisible() {
return mIsVisible;
}
}
}
|