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
|
/*
* Copyright (C) 2016 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.settings.widget;
import android.annotation.IdRes;
import android.annotation.UserIdInt;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.support.annotation.IntDef;
import android.support.annotation.VisibleForTesting;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.applications.AppInfoBase;
import com.android.settings.applications.InstalledAppDetails;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.core.lifecycle.Lifecycle;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import static com.android.internal.logging.nano.MetricsProto.MetricsEvent
.ACTION_OPEN_APP_NOTIFICATION_SETTING;
import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_OPEN_APP_SETTING;
public class EntityHeaderController {
@IntDef({ActionType.ACTION_NONE,
ActionType.ACTION_APP_PREFERENCE,
ActionType.ACTION_NOTIF_PREFERENCE})
@Retention(RetentionPolicy.SOURCE)
public @interface ActionType {
int ACTION_NONE = 0;
int ACTION_APP_PREFERENCE = 1;
int ACTION_NOTIF_PREFERENCE = 2;
}
public static final String PREF_KEY_APP_HEADER = "pref_app_header";
private static final String TAG = "AppDetailFeature";
private final Context mAppContext;
private final Activity mActivity;
private final Fragment mFragment;
private final int mMetricsCategory;
private final View mHeader;
private Lifecycle mLifecycle;
private RecyclerView mRecyclerView;
private Drawable mIcon;
private String mIconContentDescription;
private CharSequence mLabel;
private CharSequence mSummary;
private String mPackageName;
private Intent mAppNotifPrefIntent;
@UserIdInt
private int mUid = UserHandle.USER_NULL;
@ActionType
private int mAction1;
@ActionType
private int mAction2;
private boolean mHasAppInfoLink;
private boolean mIsInstantApp;
/**
* Creates a new instance of the controller.
*
* @param fragment The fragment that header will be placed in.
* @param header Optional: header view if it's already created.
*/
public static EntityHeaderController newInstance(Activity activity, Fragment fragment,
View header) {
return new EntityHeaderController(activity, fragment, header);
}
private EntityHeaderController(Activity activity, Fragment fragment, View header) {
mActivity = activity;
mAppContext = activity.getApplicationContext();
mFragment = fragment;
mMetricsCategory = FeatureFactory.getFactory(mAppContext).getMetricsFeatureProvider()
.getMetricsCategory(fragment);
if (header != null) {
mHeader = header;
} else {
mHeader = LayoutInflater.from(fragment.getContext())
.inflate(R.layout.settings_entity_header, null /* root */);
}
}
public EntityHeaderController setRecyclerView(RecyclerView recyclerView, Lifecycle lifecycle) {
mRecyclerView = recyclerView;
mLifecycle = lifecycle;
return this;
}
/**
* Set the icon in the header. Callers should also consider calling setIconContentDescription
* to provide a description of this icon for accessibility purposes.
*/
public EntityHeaderController setIcon(Drawable icon) {
if (icon != null) {
mIcon = icon.getConstantState().newDrawable(mAppContext.getResources());
}
return this;
}
/**
* Convenience method to set the header icon from an ApplicationsState.AppEntry. Callers should
* also consider calling setIconContentDescription to provide a description of this icon for
* accessibility purposes.
*/
public EntityHeaderController setIcon(ApplicationsState.AppEntry appEntry) {
if (appEntry.icon != null) {
mIcon = appEntry.icon.getConstantState().newDrawable(mAppContext.getResources());
}
return this;
}
public EntityHeaderController setIconContentDescription(String contentDescription) {
mIconContentDescription = contentDescription;
return this;
}
public EntityHeaderController setLabel(CharSequence label) {
mLabel = label;
return this;
}
public EntityHeaderController setLabel(ApplicationsState.AppEntry appEntry) {
mLabel = appEntry.label;
return this;
}
public EntityHeaderController setSummary(CharSequence summary) {
mSummary = summary;
return this;
}
public EntityHeaderController setSummary(PackageInfo packageInfo) {
if (packageInfo != null) {
mSummary = packageInfo.versionName;
}
return this;
}
public EntityHeaderController setHasAppInfoLink(boolean hasAppInfoLink) {
mHasAppInfoLink = hasAppInfoLink;
return this;
}
public EntityHeaderController setButtonActions(@ActionType int action1,
@ActionType int action2) {
mAction1 = action1;
mAction2 = action2;
return this;
}
public EntityHeaderController setPackageName(String packageName) {
mPackageName = packageName;
return this;
}
public EntityHeaderController setUid(int uid) {
mUid = uid;
return this;
}
public EntityHeaderController setAppNotifPrefIntent(Intent appNotifPrefIntent) {
mAppNotifPrefIntent = appNotifPrefIntent;
return this;
}
public EntityHeaderController setIsInstantApp(boolean isInstantApp) {
this.mIsInstantApp = isInstantApp;
return this;
}
/**
* Done mutating entity header, rebinds everything and return a new {@link LayoutPreference}.
*/
public LayoutPreference done(Activity activity, Context uiContext) {
final LayoutPreference pref = new LayoutPreference(uiContext, done(activity));
// Makes sure it's the first preference onscreen.
pref.setOrder(-1000);
pref.setSelectable(false);
pref.setKey(PREF_KEY_APP_HEADER);
return pref;
}
/**
* Done mutating entity header, rebinds everything (optionally skip rebinding buttons).
*/
public View done(Activity activity, boolean rebindActions) {
styleActionBar(activity);
ImageView iconView = mHeader.findViewById(R.id.entity_header_icon);
if (iconView != null) {
iconView.setImageDrawable(mIcon);
iconView.setContentDescription(mIconContentDescription);
}
setText(R.id.entity_header_title, mLabel);
setText(R.id.entity_header_summary, mSummary);
if (mIsInstantApp) {
setText(R.id.install_type,
mHeader.getResources().getString(R.string.install_type_instant));
}
if (rebindActions) {
bindHeaderButtons();
}
return mHeader;
}
/**
* Only binds entity header with button actions.
*/
public EntityHeaderController bindHeaderButtons() {
final View entityHeaderContent = mHeader.findViewById(R.id.entity_header_content);
final ImageButton button1 = mHeader.findViewById(android.R.id.button1);
final ImageButton button2 = mHeader.findViewById(android.R.id.button2);
bindAppInfoLink(entityHeaderContent);
bindButton(button1, mAction1);
bindButton(button2, mAction2);
return this;
}
private void bindAppInfoLink(View entityHeaderContent) {
if (!mHasAppInfoLink) {
// Caller didn't ask for app link, skip.
return;
}
if (entityHeaderContent == null
|| mPackageName == null
|| mPackageName.equals(Utils.OS_PKG)
|| mUid == UserHandle.USER_NULL) {
Log.w(TAG, "Missing ingredients to build app info link, skip");
return;
}
entityHeaderContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppInfoBase.startAppInfoFragment(
InstalledAppDetails.class, R.string.application_info_label,
mPackageName, mUid, mFragment, 0 /* request */,
mMetricsCategory);
}
});
return;
}
public EntityHeaderController styleActionBar(Activity activity) {
if (activity == null) {
Log.w(TAG, "No activity, cannot style actionbar.");
return this;
}
final ActionBar actionBar = activity.getActionBar();
if (actionBar == null) {
Log.w(TAG, "No actionbar, cannot style actionbar.");
return this;
}
actionBar.setBackgroundDrawable(
new ColorDrawable(Utils.getColorAttr(activity, android.R.attr.colorSecondary)));
actionBar.setElevation(0);
if (mRecyclerView != null && mLifecycle != null) {
ActionBarShadowController.attachToRecyclerView(mActivity, mLifecycle, mRecyclerView);
}
return this;
}
/**
* Done mutating entity header, rebinds everything.
*/
@VisibleForTesting
View done(Activity activity) {
return done(activity, true /* rebindActions */);
}
private void bindButton(ImageButton button, @ActionType int action) {
if (button == null) {
return;
}
switch (action) {
case ActionType.ACTION_NOTIF_PREFERENCE: {
if (mAppNotifPrefIntent == null) {
button.setVisibility(View.GONE);
} else {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FeatureFactory.getFactory(mAppContext).getMetricsFeatureProvider()
.actionWithSource(mAppContext, mMetricsCategory,
ACTION_OPEN_APP_NOTIFICATION_SETTING);
mFragment.startActivity(mAppNotifPrefIntent);
}
});
button.setVisibility(View.VISIBLE);
}
return;
}
case ActionType.ACTION_APP_PREFERENCE: {
final Intent intent = resolveIntent(
new Intent(Intent.ACTION_APPLICATION_PREFERENCES).setPackage(mPackageName));
if (intent == null) {
button.setVisibility(View.GONE);
return;
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FeatureFactory.getFactory(mAppContext).getMetricsFeatureProvider()
.actionWithSource(mAppContext, mMetricsCategory,
ACTION_OPEN_APP_SETTING);
mFragment.startActivity(intent);
}
});
button.setVisibility(View.VISIBLE);
return;
}
case ActionType.ACTION_NONE: {
button.setVisibility(View.GONE);
return;
}
}
}
private Intent resolveIntent(Intent i) {
ResolveInfo result = mAppContext.getPackageManager().resolveActivity(i, 0);
if (result != null) {
return new Intent(i.getAction())
.setClassName(result.activityInfo.packageName, result.activityInfo.name);
}
return null;
}
private void setText(@IdRes int id, CharSequence text) {
TextView textView = mHeader.findViewById(id);
if (textView != null) {
textView.setText(text);
textView.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
}
}
}
|