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
|
/*
* Copyright (C) 2020 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.systemui.toast;
import static android.content.pm.ApplicationInfo.FLAG_SYSTEM;
import static android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
import android.animation.Animator;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.UserHandle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.internal.R;
import com.android.launcher3.icons.IconFactory;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import com.android.systemui.plugins.ToastPlugin;
/**
* SystemUI TextToast that can be customized by ToastPlugins. Should never instantiate this class
* directly. Instead, use {@link ToastFactory#createToast}.
*/
public class SystemUIToast implements ToastPlugin.Toast {
static final String TAG = "SystemUIToast";
final Context mContext;
final CharSequence mText;
final ToastPlugin.Toast mPluginToast;
private final String mPackageName;
@UserIdInt private final int mUserId;
private final LayoutInflater mLayoutInflater;
final int mDefaultX = 0;
final int mDefaultHorizontalMargin = 0;
final int mDefaultVerticalMargin = 0;
private int mDefaultY;
private int mDefaultGravity;
@NonNull private final View mToastView;
@Nullable private final Animator mInAnimator;
@Nullable private final Animator mOutAnimator;
SystemUIToast(LayoutInflater layoutInflater, Context context, CharSequence text,
String packageName, int userId, int orientation) {
this(layoutInflater, context, text, null, packageName, userId,
orientation);
}
SystemUIToast(LayoutInflater layoutInflater, Context context, CharSequence text,
ToastPlugin.Toast pluginToast, String packageName, @UserIdInt int userId,
int orientation) {
mLayoutInflater = layoutInflater;
mContext = context;
mText = text;
mPluginToast = pluginToast;
mPackageName = packageName;
mUserId = userId;
mToastView = inflateToastView();
mInAnimator = createInAnimator();
mOutAnimator = createOutAnimator();
onOrientationChange(orientation);
}
@Override
@NonNull
public Integer getGravity() {
if (isPluginToast() && mPluginToast.getGravity() != null) {
return mPluginToast.getGravity();
}
return mDefaultGravity;
}
@Override
@NonNull
public Integer getXOffset() {
if (isPluginToast() && mPluginToast.getXOffset() != null) {
return mPluginToast.getXOffset();
}
return mDefaultX;
}
@Override
@NonNull
public Integer getYOffset() {
if (isPluginToast() && mPluginToast.getYOffset() != null) {
return mPluginToast.getYOffset();
}
return mDefaultY;
}
@Override
@NonNull
public Integer getHorizontalMargin() {
if (isPluginToast() && mPluginToast.getHorizontalMargin() != null) {
return mPluginToast.getHorizontalMargin();
}
return mDefaultHorizontalMargin;
}
@Override
@NonNull
public Integer getVerticalMargin() {
if (isPluginToast() && mPluginToast.getVerticalMargin() != null) {
return mPluginToast.getVerticalMargin();
}
return mDefaultVerticalMargin;
}
@Override
@NonNull
public View getView() {
return mToastView;
}
@Override
@Nullable
public Animator getInAnimation() {
return mInAnimator;
}
@Override
@Nullable
public Animator getOutAnimation() {
return mOutAnimator;
}
/**
* Whether this toast has a custom animation.
*/
public boolean hasCustomAnimation() {
return getInAnimation() != null || getOutAnimation() != null;
}
private boolean isPluginToast() {
return mPluginToast != null;
}
private View inflateToastView() {
if (isPluginToast() && mPluginToast.getView() != null) {
return mPluginToast.getView();
}
final View toastView = mLayoutInflater.inflate(
com.android.systemui.R.layout.text_toast, null);
final TextView textView = toastView.findViewById(com.android.systemui.R.id.text);
final ImageView iconView = toastView.findViewById(com.android.systemui.R.id.icon);
textView.setText(mText);
ApplicationInfo appInfo = null;
try {
appInfo = mContext.getPackageManager()
.getApplicationInfoAsUser(mPackageName, 0, mUserId);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Package name not found package=" + mPackageName
+ " user=" + mUserId);
}
if (appInfo != null && appInfo.targetSdkVersion < Build.VERSION_CODES.S) {
// no two-line limit
textView.setMaxLines(Integer.MAX_VALUE);
// no app icon
toastView.findViewById(com.android.systemui.R.id.icon).setVisibility(View.GONE);
} else {
Drawable icon = getBadgedIcon(mContext, mPackageName, mUserId);
if (icon == null) {
iconView.setVisibility(View.GONE);
} else {
iconView.setImageDrawable(icon);
if (appInfo == null) {
Log.d(TAG, "No appInfo for pkg=" + mPackageName + " usr=" + mUserId);
} else if (appInfo.labelRes != 0) {
try {
Resources res = mContext.getPackageManager().getResourcesForApplication(
appInfo,
new Configuration(mContext.getResources().getConfiguration()));
iconView.setContentDescription(res.getString(appInfo.labelRes));
} catch (PackageManager.NameNotFoundException e) {
Log.d(TAG, "Cannot find application resources for icon label.");
}
}
}
}
return toastView;
}
/**
* Called on orientation changes to update parameters associated with the toast placement.
*/
public void onOrientationChange(int orientation) {
if (mPluginToast != null) {
mPluginToast.onOrientationChange(orientation);
}
mDefaultY = mContext.getResources().getDimensionPixelSize(R.dimen.toast_y_offset);
mDefaultGravity =
mContext.getResources().getInteger(R.integer.config_toastDefaultGravity);
}
private Animator createInAnimator() {
if (isPluginToast() && mPluginToast.getInAnimation() != null) {
return mPluginToast.getInAnimation();
}
return ToastDefaultAnimation.Companion.toastIn(getView());
}
private Animator createOutAnimator() {
if (isPluginToast() && mPluginToast.getOutAnimation() != null) {
return mPluginToast.getOutAnimation();
}
return ToastDefaultAnimation.Companion.toastOut(getView());
}
/**
* Get badged app icon if necessary, similar as used in the Settings UI.
* @return The icon to use
*/
public static Drawable getBadgedIcon(@NonNull Context context, String packageName,
int userId) {
if (!(context.getApplicationContext() instanceof Application)) {
return null;
}
final Context userContext;
try {
userContext = context.createPackageContextAsUser("android",
0, new UserHandle(userId));
} catch (NameNotFoundException e) {
Log.e(TAG, "Could not create user package context");
return null;
}
final ApplicationsState appState =
ApplicationsState.getInstance((Application) context.getApplicationContext());
if (!appState.isUserAdded(userId)) {
Log.d(TAG, "user hasn't been fully initialized, not showing an app icon for "
+ "packageName=" + packageName);
return null;
}
final PackageManager packageManager = userContext.getPackageManager();
final AppEntry appEntry = appState.getEntry(packageName, userId);
if (appEntry == null || appEntry.info == null
|| !showApplicationIcon(appEntry.info, packageManager)) {
return null;
}
final ApplicationInfo appInfo = appEntry.info;
UserHandle user = UserHandle.getUserHandleForUid(appInfo.uid);
IconFactory iconFactory = IconFactory.obtain(context);
Bitmap iconBmp = iconFactory.createBadgedIconBitmap(
appInfo.loadUnbadgedIcon(packageManager), user, true).icon;
return new BitmapDrawable(context.getResources(), iconBmp);
}
private static boolean showApplicationIcon(ApplicationInfo appInfo,
PackageManager packageManager) {
if (hasFlag(appInfo.flags, FLAG_UPDATED_SYSTEM_APP)) {
return packageManager.getLaunchIntentForPackage(appInfo.packageName)
!= null;
}
return !hasFlag(appInfo.flags, FLAG_SYSTEM);
}
private static boolean hasFlag(int flags, int flag) {
return (flags & flag) != 0;
}
}
|