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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/*
* 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 android.window;
import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.ColorInt;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.TestApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import com.android.internal.R;
import com.android.internal.policy.DecorView;
/**
* <p>The view which allows an activity to customize its splash screen exit animation.</p>
*
* <p>Activities will receive this view as a parameter of
* {@link SplashScreen.OnExitAnimationListener#onSplashScreenExit} if
* they set {@link SplashScreen#setOnExitAnimationListener}.
* When this callback is called, this view will be on top of the activity.</p>
*
* <p>This view is composed of a view containing the splashscreen icon (see
* windowSplashscreenAnimatedIcon) and a background.
* Developers can use {@link #getIconView} to get this view and replace the drawable or
* add animation to it. The background of this view is filled with a single color, which can be
* edited during the animation by {@link View#setBackground} or {@link View#setBackgroundColor}.</p>
*
* @see SplashScreen
*/
public final class SplashScreenView extends FrameLayout {
private static final String TAG = SplashScreenView.class.getSimpleName();
private static final boolean DEBUG = false;
private boolean mNotCopyable;
private int mInitBackgroundColor;
private View mIconView;
private Bitmap mParceledIconBitmap;
private View mBrandingImageView;
private Bitmap mParceledBrandingBitmap;
private long mIconAnimationDuration;
private long mIconAnimationStart;
private Animatable mAnimatableIcon;
private ValueAnimator mAnimator;
// cache original window and status
private Window mWindow;
private boolean mDrawBarBackground;
private int mStatusBarColor;
private int mNavigationBarColor;
/**
* Internal builder to create a SplashScreenWindowView object.
* @hide
*/
public static class Builder {
private final Context mContext;
private int mIconSize;
private @ColorInt int mBackgroundColor;
private Bitmap mParceledIconBitmap;
private Drawable mIconDrawable;
private int mBrandingImageWidth;
private int mBrandingImageHeight;
private Drawable mBrandingDrawable;
private Bitmap mParceledBrandingBitmap;
private long mIconAnimationStart;
private long mIconAnimationDuration;
public Builder(@NonNull Context context) {
mContext = context;
}
/**
* When create from {@link SplashScreenViewParcelable}, all the materials were be settled so
* you do not need to call other set methods.
*/
public Builder createFromParcel(SplashScreenViewParcelable parcelable) {
mIconSize = parcelable.getIconSize();
mBackgroundColor = parcelable.getBackgroundColor();
if (parcelable.mIconBitmap != null) {
mIconDrawable = new BitmapDrawable(mContext.getResources(), parcelable.mIconBitmap);
mParceledIconBitmap = parcelable.mIconBitmap;
}
if (parcelable.mBrandingBitmap != null) {
setBrandingDrawable(new BitmapDrawable(mContext.getResources(),
parcelable.mBrandingBitmap), parcelable.mBrandingWidth,
parcelable.mBrandingHeight);
mParceledBrandingBitmap = parcelable.mBrandingBitmap;
}
mIconAnimationStart = parcelable.mIconAnimationStart;
mIconAnimationDuration = parcelable.mIconAnimationDuration;
return this;
}
/**
* Set the rectangle size for the center view.
*/
public Builder setIconSize(int iconSize) {
mIconSize = iconSize;
return this;
}
/**
* Set the background color for the view.
*/
public Builder setBackgroundColor(@ColorInt int backgroundColor) {
mBackgroundColor = backgroundColor;
return this;
}
/**
* Set the Drawable object to fill the center view.
*/
public Builder setCenterViewDrawable(Drawable drawable) {
mIconDrawable = drawable;
return this;
}
/**
* Set the animation duration if icon is animatable.
*/
public Builder setAnimationDuration(int duration) {
mIconAnimationDuration = duration;
return this;
}
/**
* Set the Drawable object and size for the branding view.
*/
public Builder setBrandingDrawable(Drawable branding, int width, int height) {
mBrandingDrawable = branding;
mBrandingImageWidth = width;
mBrandingImageHeight = height;
return this;
}
/**
* Create SplashScreenWindowView object from materials.
*/
public SplashScreenView build() {
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
final SplashScreenView view = (SplashScreenView)
layoutInflater.inflate(R.layout.splash_screen_view, null, false);
view.mInitBackgroundColor = mBackgroundColor;
view.setBackgroundColor(mBackgroundColor);
view.mIconView = view.findViewById(R.id.splashscreen_icon_view);
view.mBrandingImageView = view.findViewById(R.id.splashscreen_branding_view);
// center icon
if (mIconSize != 0) {
final ViewGroup.LayoutParams params = view.mIconView.getLayoutParams();
params.width = mIconSize;
params.height = mIconSize;
view.mIconView.setLayoutParams(params);
}
if (mIconDrawable != null) {
view.mIconView.setBackground(mIconDrawable);
view.initIconAnimation(mIconDrawable, mIconAnimationDuration);
}
view.mIconAnimationStart = mIconAnimationStart;
view.mIconAnimationDuration = mIconAnimationDuration;
if (mParceledIconBitmap != null) {
view.mParceledIconBitmap = mParceledIconBitmap;
}
// branding image
if (mBrandingImageHeight > 0 && mBrandingImageWidth > 0) {
final ViewGroup.LayoutParams params = view.mBrandingImageView.getLayoutParams();
params.width = mBrandingImageWidth;
params.height = mBrandingImageHeight;
view.mBrandingImageView.setLayoutParams(params);
}
if (mBrandingDrawable != null) {
view.mBrandingImageView.setBackground(mBrandingDrawable);
}
if (mParceledBrandingBitmap != null) {
view.mParceledBrandingBitmap = mParceledBrandingBitmap;
}
if (DEBUG) {
Log.d(TAG, " build " + view + " Icon: view: " + view.mIconView + " drawable: "
+ mIconDrawable + " size: " + mIconSize + "\n Branding: view: "
+ view.mBrandingImageView + " drawable: " + mBrandingDrawable
+ " size w: " + mBrandingImageWidth + " h: " + mBrandingImageHeight);
}
return view;
}
}
/** @hide */
public SplashScreenView(Context context) {
super(context);
}
/** @hide */
public SplashScreenView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
/**
* Declared this view is not copyable.
* @hide
*/
public void setNotCopyable() {
mNotCopyable = true;
}
/**
* Whether this view is copyable.
* @hide
*/
public boolean isCopyable() {
return !mNotCopyable;
}
/**
* Returns the duration of the icon animation if icon is animatable.
*
* @see android.R.attr#windowSplashScreenAnimatedIcon
* @see android.R.attr#windowSplashScreenAnimationDuration
*/
public long getIconAnimationDurationMillis() {
return mIconAnimationDuration;
}
/**
* If the replaced icon is animatable, return the animation start time in millisecond based on
* system. The start time is set using {@link SystemClock#uptimeMillis()}.
*/
public long getIconAnimationStartMillis() {
return mIconAnimationStart;
}
void initIconAnimation(Drawable iconDrawable, long duration) {
if (iconDrawable instanceof Animatable) {
mAnimatableIcon = (Animatable) iconDrawable;
mAnimator = ValueAnimator.ofInt(0, 1);
mAnimator.setDuration(duration);
mAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
mIconAnimationStart = SystemClock.uptimeMillis();
mAnimatableIcon.start();
}
@Override
public void onAnimationEnd(Animator animation) {
mAnimatableIcon.stop();
}
@Override
public void onAnimationCancel(Animator animation) {
mAnimatableIcon.stop();
}
@Override
public void onAnimationRepeat(Animator animation) {
// do not repeat
mAnimatableIcon.stop();
}
});
}
}
/**
* @hide
*/
public void startIntroAnimation() {
if (mAnimatableIcon != null) {
mAnimator.start();
}
}
/**
* <p>Remove this view and release its resource. </p>
* <p><strong>Do not</strong> invoke this method from a drawing method
* ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
*/
public void remove() {
setVisibility(GONE);
if (mParceledIconBitmap != null) {
mIconView.setBackground(null);
mParceledIconBitmap.recycle();
mParceledIconBitmap = null;
}
if (mParceledBrandingBitmap != null) {
mBrandingImageView.setBackground(null);
mParceledBrandingBitmap.recycle();
mParceledBrandingBitmap = null;
}
if (mWindow != null) {
final DecorView decorView = (DecorView) mWindow.peekDecorView();
if (DEBUG) {
Log.d(TAG, "remove starting view");
}
if (decorView != null) {
decorView.removeView(this);
}
restoreSystemUIColors();
mWindow = null;
}
}
/**
* Cache the root window.
* @hide
*/
public void cacheRootWindow(Window window) {
mWindow = window;
}
/**
* Called after SplashScreenView has added on the root window.
* @hide
*/
public void makeSystemUIColorsTransparent() {
if (mWindow != null) {
final WindowManager.LayoutParams attr = mWindow.getAttributes();
mDrawBarBackground = (attr.flags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0;
mWindow.addFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
mStatusBarColor = mWindow.getStatusBarColor();
mNavigationBarColor = mWindow.getNavigationBarDividerColor();
mWindow.setStatusBarColor(Color.TRANSPARENT);
mWindow.setNavigationBarColor(Color.TRANSPARENT);
}
setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
private void restoreSystemUIColors() {
if (mWindow != null) {
if (!mDrawBarBackground) {
mWindow.clearFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
mWindow.setStatusBarColor(mStatusBarColor);
mWindow.setNavigationBarColor(mNavigationBarColor);
}
}
/**
* Get the view containing the Splash Screen icon and its background.
* @see android.R.attr#windowSplashScreenAnimatedIcon
*/
public @Nullable View getIconView() {
return mIconView;
}
/**
* Get the branding image view.
* @hide
*/
@TestApi
public @Nullable View getBrandingView() {
return mBrandingImageView;
}
/**
* Get the initial background color of this view.
* @hide
*/
@ColorInt int getInitBackgroundColor() {
return mInitBackgroundColor;
}
/**
* Use to create {@link SplashScreenView} object across process.
* @hide
*/
public static class SplashScreenViewParcelable implements Parcelable {
private int mIconSize;
private int mBackgroundColor;
private Bitmap mIconBitmap;
private int mBrandingWidth;
private int mBrandingHeight;
private Bitmap mBrandingBitmap;
private long mIconAnimationStart;
private long mIconAnimationDuration;
public SplashScreenViewParcelable(SplashScreenView view) {
ViewGroup.LayoutParams params = view.getIconView().getLayoutParams();
mIconSize = params.height;
mBackgroundColor = view.getInitBackgroundColor();
mIconBitmap = copyDrawable(view.getIconView().getBackground());
mBrandingBitmap = copyDrawable(view.getBrandingView().getBackground());
params = view.getBrandingView().getLayoutParams();
mBrandingWidth = params.width;
mBrandingHeight = params.height;
mIconAnimationStart = view.getIconAnimationStartMillis();
mIconAnimationDuration = view.getIconAnimationDurationMillis();
}
private Bitmap copyDrawable(Drawable drawable) {
if (drawable != null) {
final Rect initialBounds = drawable.copyBounds();
final int width = initialBounds.width();
final int height = initialBounds.height();
final Bitmap snapshot = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas bmpCanvas = new Canvas(snapshot);
drawable.setBounds(0, 0, width, height);
drawable.draw(bmpCanvas);
final Bitmap copyBitmap = snapshot.createAshmemBitmap();
snapshot.recycle();
return copyBitmap;
}
return null;
}
private SplashScreenViewParcelable(@NonNull Parcel source) {
readParcel(source);
}
private void readParcel(@NonNull Parcel source) {
mIconSize = source.readInt();
mBackgroundColor = source.readInt();
mIconBitmap = source.readTypedObject(Bitmap.CREATOR);
mBrandingWidth = source.readInt();
mBrandingHeight = source.readInt();
mBrandingBitmap = source.readTypedObject(Bitmap.CREATOR);
mIconAnimationStart = source.readLong();
mIconAnimationDuration = source.readLong();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mIconSize);
dest.writeInt(mBackgroundColor);
dest.writeTypedObject(mIconBitmap, flags);
dest.writeInt(mBrandingWidth);
dest.writeInt(mBrandingHeight);
dest.writeTypedObject(mBrandingBitmap, flags);
dest.writeLong(mIconAnimationStart);
dest.writeLong(mIconAnimationDuration);
}
public static final @NonNull Parcelable.Creator<SplashScreenViewParcelable> CREATOR =
new Parcelable.Creator<SplashScreenViewParcelable>() {
public SplashScreenViewParcelable createFromParcel(@NonNull Parcel source) {
return new SplashScreenViewParcelable(source);
}
public SplashScreenViewParcelable[] newArray(int size) {
return new SplashScreenViewParcelable[size];
}
};
/**
* Release the bitmap if another process cannot handle it.
*/
public void clearIfNeeded() {
if (mIconBitmap != null) {
mIconBitmap.recycle();
mIconBitmap = null;
}
if (mBrandingBitmap != null) {
mBrandingBitmap.recycle();
mBrandingBitmap = null;
}
}
int getIconSize() {
return mIconSize;
}
int getBackgroundColor() {
return mBackgroundColor;
}
}
}
|