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
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.widget;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;
/**
* This class is used to initialize view which was inflated
* from {@link R.xml.app_entities_header.xml}.
*
* <p>How to use AppEntitiesHeaderController?
*
* <p>1. Add a {@link LayoutPreference} in layout XML file.
* <pre>
* <com.android.settingslib.widget.LayoutPreference
* android:key="app_entities_header"
* android:layout="@layout/app_entities_header"/>
* </pre>
*
* <p>2. Use AppEntitiesHeaderController to call below methods, then you can initialize
* view of <code>app_entities_header</code>.
*
* <pre>
*
* View headerView = ((LayoutPreference) screen.findPreference("app_entities_header"))
* .findViewById(R.id.app_entities_header);
*
* final AppEntityInfo appEntityInfo = new AppEntityInfo.Builder()
* .setIcon(icon)
* .setTitle(title)
* .setSummary(summary)
* .setOnClickListener(view -> doSomething())
* .build();
*
* AppEntitiesHeaderController.newInstance(context, headerView)
* .setHeaderTitleRes(R.string.xxxxx)
* .setHeaderDetailsRes(R.string.xxxxx)
* .setHeaderDetailsClickListener(onClickListener)
* .setAppEntity(0, appEntityInfo)
* .setAppEntity(1, appEntityInfo)
* .setAppEntity(2, appEntityInfo)
* .apply();
* </pre>
*/
public class AppEntitiesHeaderController {
private static final String TAG = "AppEntitiesHeaderCtl";
@VisibleForTesting
public static final int MAXIMUM_APPS = 3;
private final Context mContext;
private final TextView mHeaderTitleView;
private final TextView mHeaderEmptyView;
private final Button mHeaderDetailsView;
private final AppEntityInfo[] mAppEntityInfos;
private final View mAppViewsContainer;
private final View[] mAppEntityViews;
private final ImageView[] mAppIconViews;
private final TextView[] mAppTitleViews;
private final TextView[] mAppSummaryViews;
private int mHeaderTitleRes;
private int mHeaderDetailsRes;
private int mHeaderEmptyRes;
private CharSequence mHeaderDetails;
private View.OnClickListener mDetailsOnClickListener;
/**
* Creates a new instance of the controller.
*
* @param context the Context the view is running in
* @param appEntitiesHeaderView view was inflated from <code>app_entities_header</code>
*/
public static AppEntitiesHeaderController newInstance(@NonNull Context context,
@NonNull View appEntitiesHeaderView) {
return new AppEntitiesHeaderController(context, appEntitiesHeaderView);
}
private AppEntitiesHeaderController(Context context, View appEntitiesHeaderView) {
mContext = context;
mHeaderTitleView = appEntitiesHeaderView.findViewById(R.id.header_title);
mHeaderDetailsView = appEntitiesHeaderView.findViewById(R.id.header_details);
mHeaderEmptyView = appEntitiesHeaderView.findViewById(R.id.empty_view);
mAppViewsContainer = appEntitiesHeaderView.findViewById(R.id.app_views_container);
mAppEntityInfos = new AppEntityInfo[MAXIMUM_APPS];
mAppIconViews = new ImageView[MAXIMUM_APPS];
mAppTitleViews = new TextView[MAXIMUM_APPS];
mAppSummaryViews = new TextView[MAXIMUM_APPS];
mAppEntityViews = new View[]{
appEntitiesHeaderView.findViewById(R.id.app1_view),
appEntitiesHeaderView.findViewById(R.id.app2_view),
appEntitiesHeaderView.findViewById(R.id.app3_view)
};
// Initialize view in advance, so we won't take too much time to do it when controller is
// binding view.
for (int index = 0; index < MAXIMUM_APPS; index++) {
final View appView = mAppEntityViews[index];
mAppIconViews[index] = (ImageView) appView.findViewById(R.id.app_icon);
mAppTitleViews[index] = (TextView) appView.findViewById(R.id.app_title);
mAppSummaryViews[index] = (TextView) appView.findViewById(R.id.app_summary);
}
}
/**
* Set the text resource for app entities header title.
*/
public AppEntitiesHeaderController setHeaderTitleRes(@StringRes int titleRes) {
mHeaderTitleRes = titleRes;
return this;
}
/**
* Set the text resource for app entities header details.
*/
public AppEntitiesHeaderController setHeaderDetailsRes(@StringRes int detailsRes) {
mHeaderDetailsRes = detailsRes;
return this;
}
/**
* Set the text for app entities header details.
*/
public AppEntitiesHeaderController setHeaderDetails(CharSequence detailsText) {
mHeaderDetails = detailsText;
return this;
}
/**
* Register a callback to be invoked when header details view is clicked.
*/
public AppEntitiesHeaderController setHeaderDetailsClickListener(
@Nullable View.OnClickListener clickListener) {
mDetailsOnClickListener = clickListener;
return this;
}
/**
* Sets the string resource id for the empty text.
*/
public AppEntitiesHeaderController setHeaderEmptyRes(@StringRes int emptyRes) {
mHeaderEmptyRes = emptyRes;
return this;
}
/**
* Set an app entity at a specified position view.
*
* @param index the index at which the specified view is to be inserted
* @param appEntityInfo the information of an app entity
* @return this {@code AppEntitiesHeaderController} object
*/
public AppEntitiesHeaderController setAppEntity(int index,
@NonNull AppEntityInfo appEntityInfo) {
mAppEntityInfos[index] = appEntityInfo;
return this;
}
/**
* Remove an app entity at a specified position view.
*
* @param index the index at which the specified view is to be removed
* @return this {@code AppEntitiesHeaderController} object
*/
public AppEntitiesHeaderController removeAppEntity(int index) {
mAppEntityInfos[index] = null;
return this;
}
/**
* Clear all app entities in app entities header.
*
* @return this {@code AppEntitiesHeaderController} object
*/
public AppEntitiesHeaderController clearAllAppEntities() {
for (int index = 0; index < MAXIMUM_APPS; index++) {
removeAppEntity(index);
}
return this;
}
/**
* Done mutating app entities header, rebinds everything.
*/
public void apply() {
bindHeaderTitleView();
if (isAppEntityInfosEmpty()) {
setEmptyViewVisible(true);
return;
}
setEmptyViewVisible(false);
bindHeaderDetailsView();
// Rebind all apps view
for (int index = 0; index < MAXIMUM_APPS; index++) {
bindAppEntityView(index);
}
}
private void bindHeaderTitleView() {
CharSequence titleText = "";
try {
titleText = mContext.getText(mHeaderTitleRes);
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Resource of header title can't not be found!", e);
}
mHeaderTitleView.setText(titleText);
mHeaderTitleView.setVisibility(
TextUtils.isEmpty(titleText) ? View.GONE : View.VISIBLE);
}
private void bindHeaderDetailsView() {
CharSequence detailsText = mHeaderDetails;
if (TextUtils.isEmpty(detailsText)) {
try {
detailsText = mContext.getText(mHeaderDetailsRes);
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Resource of header details can't not be found!", e);
}
}
mHeaderDetailsView.setText(detailsText);
mHeaderDetailsView.setVisibility(
TextUtils.isEmpty(detailsText) ? View.GONE : View.VISIBLE);
mHeaderDetailsView.setOnClickListener(mDetailsOnClickListener);
}
private void bindAppEntityView(int index) {
final AppEntityInfo appEntityInfo = mAppEntityInfos[index];
mAppEntityViews[index].setVisibility(appEntityInfo != null ? View.VISIBLE : View.GONE);
if (appEntityInfo != null) {
mAppEntityViews[index].setOnClickListener(appEntityInfo.getClickListener());
mAppIconViews[index].setImageDrawable(appEntityInfo.getIcon());
final CharSequence title = appEntityInfo.getTitle();
mAppTitleViews[index].setVisibility(
TextUtils.isEmpty(title) ? View.INVISIBLE : View.VISIBLE);
mAppTitleViews[index].setText(title);
final CharSequence summary = appEntityInfo.getSummary();
mAppSummaryViews[index].setVisibility(
TextUtils.isEmpty(summary) ? View.INVISIBLE : View.VISIBLE);
mAppSummaryViews[index].setText(summary);
}
}
private void setEmptyViewVisible(boolean visible) {
if (mHeaderEmptyRes != 0) {
mHeaderEmptyView.setText(mHeaderEmptyRes);
}
mHeaderEmptyView.setVisibility(visible ? View.VISIBLE : View.GONE);
mHeaderDetailsView.setVisibility(visible ? View.GONE : View.VISIBLE);
mAppViewsContainer.setVisibility(visible ? View.GONE : View.VISIBLE);
}
private boolean isAppEntityInfosEmpty() {
for (AppEntityInfo info : mAppEntityInfos) {
if (info != null) {
return false;
}
}
return true;
}
}
|