diff options
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/app/Activity.java | 23 | ||||
| -rw-r--r-- | core/java/android/app/ActivityClient.java | 15 | ||||
| -rw-r--r-- | core/java/android/app/ActivityTaskManager.java | 18 | ||||
| -rw-r--r-- | core/java/android/app/ActivityThread.java | 120 | ||||
| -rw-r--r-- | core/java/android/app/ClientTransactionHandler.java | 11 | ||||
| -rw-r--r-- | core/java/android/app/IActivityClientController.aidl | 5 | ||||
| -rw-r--r-- | core/java/android/app/IActivityTaskManager.aidl | 7 | ||||
| -rw-r--r-- | core/java/android/app/servertransaction/ResumeActivityItem.java | 2 | ||||
| -rw-r--r-- | core/java/android/app/servertransaction/TransferSplashScreenViewStateItem.java | 105 | ||||
| -rw-r--r-- | core/java/android/view/ViewRootImpl.java | 14 | ||||
| -rw-r--r-- | core/java/android/window/ITaskOrganizer.aidl | 5 | ||||
| -rw-r--r-- | core/java/android/window/SplashScreen.java | 188 | ||||
| -rw-r--r-- | core/java/android/window/SplashScreenView.aidl | 20 | ||||
| -rw-r--r-- | core/java/android/window/SplashScreenView.java | 344 | ||||
| -rw-r--r-- | core/java/android/window/TaskOrganizer.java | 11 |
15 files changed, 880 insertions, 8 deletions
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index 4728f11a402d..992d054737b9 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -139,6 +139,8 @@ import android.view.translation.UiTranslationController; import android.widget.AdapterView; import android.widget.Toast; import android.widget.Toolbar; +import android.window.SplashScreen; +import android.window.SplashScreenView; import com.android.internal.R; import com.android.internal.annotations.GuardedBy; @@ -961,6 +963,10 @@ public class Activity extends ContextThemeWrapper private UiTranslationController mUiTranslationController; + private SplashScreen mSplashScreen; + /** @hide */ + SplashScreenView mSplashScreenView; + private final WindowControllerCallback mWindowControllerCallback = new WindowControllerCallback() { /** @@ -1603,6 +1609,23 @@ public class Activity extends ContextThemeWrapper } /** + * Get the interface that activity use to talk to the splash screen. + * @see SplashScreen + */ + public final @NonNull SplashScreen getSplashScreen() { + return getOrCreateSplashScreen(); + } + + private SplashScreen getOrCreateSplashScreen() { + synchronized (this) { + if (mSplashScreen == null) { + mSplashScreen = new SplashScreen.SplashScreenImpl(this); + } + return mSplashScreen; + } + } + + /** * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with * the attribute {@link android.R.attr#persistableMode} set to * <code>persistAcrossReboots</code>. diff --git a/core/java/android/app/ActivityClient.java b/core/java/android/app/ActivityClient.java index 401f8cc13bad..e3b5e9a32324 100644 --- a/core/java/android/app/ActivityClient.java +++ b/core/java/android/app/ActivityClient.java @@ -46,9 +46,9 @@ public class ActivityClient { } /** Reports {@link Activity#onResume()} is done. */ - public void activityResumed(IBinder token) { + public void activityResumed(IBinder token, boolean handleSplashScreenExit) { try { - getActivityClientController().activityResumed(token); + getActivityClientController().activityResumed(token, handleSplashScreenExit); } catch (RemoteException e) { e.rethrowFromSystemServer(); } @@ -488,6 +488,17 @@ public class ActivityClient { } } + /** + * Reports the splash screen view has attached to client. + */ + void reportSplashScreenAttached(IBinder token) { + try { + getActivityClientController().splashScreenAttached(token); + } catch (RemoteException e) { + e.rethrowFromSystemServer(); + } + } + public static ActivityClient getInstance() { return sInstance.get(); } diff --git a/core/java/android/app/ActivityTaskManager.java b/core/java/android/app/ActivityTaskManager.java index 70fa4445479b..233f737b8e0f 100644 --- a/core/java/android/app/ActivityTaskManager.java +++ b/core/java/android/app/ActivityTaskManager.java @@ -19,6 +19,7 @@ package android.app; import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY; import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SystemService; import android.annotation.TestApi; @@ -37,6 +38,7 @@ import android.os.ServiceManager; import android.util.DisplayMetrics; import android.util.Singleton; import android.view.RemoteAnimationDefinition; +import android.window.SplashScreenView.SplashScreenViewParcelable; import java.util.List; @@ -243,6 +245,22 @@ public class ActivityTaskManager { } /** + * Notify the server that splash screen of the given task has been copied" + * + * @param taskId Id of task to handle the material to reconstruct the splash screen view. + * @param parcelable Used to reconstruct the view, null means the surface is un-copyable. + * @hide + */ + public void onSplashScreenViewCopyFinished(int taskId, + @Nullable SplashScreenViewParcelable parcelable) { + try { + getService().onSplashScreenViewCopyFinished(taskId, parcelable); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** * Return the default limit on the number of recents that an app can make. * @hide */ diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index bb6a774cbee2..3d9f6123963f 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -171,12 +171,15 @@ import android.view.View; import android.view.ViewDebug; import android.view.ViewManager; import android.view.ViewRootImpl; +import android.view.ViewTreeObserver; import android.view.Window; import android.view.WindowManager; import android.view.WindowManagerGlobal; import android.view.autofill.AutofillId; import android.view.translation.TranslationSpec; import android.webkit.WebView; +import android.window.SplashScreen; +import android.window.SplashScreenView; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; @@ -185,6 +188,7 @@ import com.android.internal.content.ReferrerIntent; import com.android.internal.os.BinderInternal; import com.android.internal.os.RuntimeInit; import com.android.internal.os.SomeArgs; +import com.android.internal.policy.DecorView; import com.android.internal.util.ArrayUtils; import com.android.internal.util.FastPrintWriter; import com.android.internal.util.Preconditions; @@ -227,6 +231,7 @@ import java.util.Map; import java.util.Objects; import java.util.TimeZone; import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; @@ -285,6 +290,7 @@ public final class ActivityThread extends ClientTransactionHandler { /** Use background GC policy and default JIT threshold. */ private static final int VM_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1; + private static final int REMOVE_SPLASH_SCREEN_VIEW_TIMEOUT = 5000; /** * Denotes an invalid sequence number corresponding to a process state change. */ @@ -486,6 +492,8 @@ public final class ActivityThread extends ClientTransactionHandler { final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>(); + private SplashScreen.SplashScreenManagerGlobal mSplashScreenGlobal; + final GcIdler mGcIdler = new GcIdler(); final PurgeIdler mPurgeIdler = new PurgeIdler(); @@ -1930,6 +1938,8 @@ public final class ActivityThread extends ClientTransactionHandler { public static final int INSTRUMENT_WITHOUT_RESTART = 170; public static final int FINISH_INSTRUMENTATION_WITHOUT_RESTART = 171; + public static final int REMOVE_SPLASH_SCREEN_VIEW = 172; + String codeToString(int code) { if (DEBUG_MESSAGES) { switch (code) { @@ -1976,6 +1986,8 @@ public final class ActivityThread extends ClientTransactionHandler { case INSTRUMENT_WITHOUT_RESTART: return "INSTRUMENT_WITHOUT_RESTART"; case FINISH_INSTRUMENTATION_WITHOUT_RESTART: return "FINISH_INSTRUMENTATION_WITHOUT_RESTART"; + case REMOVE_SPLASH_SCREEN_VIEW: + return "REMOVE_SPLASH_SCREEN_VIEW"; } } return Integer.toString(code); @@ -2169,6 +2181,9 @@ public final class ActivityThread extends ClientTransactionHandler { case FINISH_INSTRUMENTATION_WITHOUT_RESTART: handleFinishInstrumentationWithoutRestart(); break; + case REMOVE_SPLASH_SCREEN_VIEW: + handleRemoveSplashScreenView((ActivityClientRecord) msg.obj); + break; } Object obj = msg.obj; if (obj instanceof SomeArgs) { @@ -3955,6 +3970,106 @@ public final class ActivityThread extends ClientTransactionHandler { } /** + * Register a splash screen manager to this process. + */ + public void registerSplashScreenManager( + @NonNull SplashScreen.SplashScreenManagerGlobal manager) { + synchronized (this) { + mSplashScreenGlobal = manager; + } + } + + @Override + public boolean isHandleSplashScreenExit(@NonNull IBinder token) { + synchronized (this) { + return mSplashScreenGlobal != null && mSplashScreenGlobal.containsExitListener(token); + } + } + + @Override + public void handleAttachSplashScreenView(@NonNull ActivityClientRecord r, + @Nullable SplashScreenView.SplashScreenViewParcelable parcelable) { + final DecorView decorView = (DecorView) r.window.peekDecorView(); + if (parcelable != null && decorView != null) { + createSplashScreen(r, decorView, parcelable); + } else { + // shouldn't happen! + Slog.e(TAG, "handleAttachSplashScreenView failed, unable to attach"); + } + } + + private void createSplashScreen(ActivityClientRecord r, DecorView decorView, + SplashScreenView.SplashScreenViewParcelable parcelable) { + final SplashScreenView.Builder builder = new SplashScreenView.Builder(r.activity); + final SplashScreenView view = builder.createFromParcel(parcelable).build(); + decorView.addView(view); + view.cacheRootWindow(r.window); + view.makeSystemUIColorsTransparent(); + r.activity.mSplashScreenView = view; + view.requestLayout(); + // Ensure splash screen view is shown before remove the splash screen window. + final ViewRootImpl impl = decorView.getViewRootImpl(); + final boolean hardwareEnabled = impl != null && impl.isHardwareEnabled(); + final AtomicBoolean notified = new AtomicBoolean(); + if (hardwareEnabled) { + final Runnable frameCommit = new Runnable() { + @Override + public void run() { + view.post(() -> { + if (!notified.get()) { + view.getViewTreeObserver().unregisterFrameCommitCallback(this); + ActivityClient.getInstance().reportSplashScreenAttached( + r.token); + notified.set(true); + } + }); + } + }; + view.getViewTreeObserver().registerFrameCommitCallback(frameCommit); + } else { + final ViewTreeObserver.OnDrawListener onDrawListener = + new ViewTreeObserver.OnDrawListener() { + @Override + public void onDraw() { + view.post(() -> { + if (!notified.get()) { + view.getViewTreeObserver().removeOnDrawListener(this); + ActivityClient.getInstance().reportSplashScreenAttached( + r.token); + notified.set(true); + } + }); + } + }; + view.getViewTreeObserver().addOnDrawListener(onDrawListener); + } + } + + @Override + public void handOverSplashScreenView(@NonNull ActivityClientRecord r) { + if (r.activity.mSplashScreenView != null) { + Message msg = mH.obtainMessage(H.REMOVE_SPLASH_SCREEN_VIEW, r); + mH.sendMessageDelayed(msg, REMOVE_SPLASH_SCREEN_VIEW_TIMEOUT); + synchronized (this) { + if (mSplashScreenGlobal != null) { + mSplashScreenGlobal.dispatchOnExitAnimation(r.token, + r.activity.mSplashScreenView); + } + } + } + } + + /** + * Force remove splash screen view. + */ + private void handleRemoveSplashScreenView(@NonNull ActivityClientRecord r) { + if (r.activity.mSplashScreenView != null) { + r.activity.mSplashScreenView.remove(); + r.activity.mSplashScreenView = null; + } + } + + /** * Cycle activity through onPause and onUserLeaveHint so that PIP is entered if supported, then * return to its previous state. This allows activities that rely on onUserLeaveHint instead of * onPictureInPictureRequested to enter picture-in-picture. @@ -5174,6 +5289,11 @@ public final class ActivityThread extends ClientTransactionHandler { r.setState(ON_DESTROY); mLastReportedWindowingMode.remove(r.activity.getActivityToken()); schedulePurgeIdler(); + synchronized (this) { + if (mSplashScreenGlobal != null) { + mSplashScreenGlobal.tokenDestroyed(r.token); + } + } // updatePendingActivityConfiguration() reads from mActivities to update // ActivityClientRecord which runs in a different thread. Protect modifications to // mActivities to avoid race. diff --git a/core/java/android/app/ClientTransactionHandler.java b/core/java/android/app/ClientTransactionHandler.java index 0e1c827145d2..cf5fd148c030 100644 --- a/core/java/android/app/ClientTransactionHandler.java +++ b/core/java/android/app/ClientTransactionHandler.java @@ -28,6 +28,7 @@ import android.content.res.Configuration; import android.os.IBinder; import android.util.MergedConfiguration; import android.view.DisplayAdjustments.FixedRotationAdjustments; +import android.window.SplashScreenView.SplashScreenViewParcelable; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.content.ReferrerIntent; @@ -158,6 +159,16 @@ public abstract class ClientTransactionHandler { /** Request that an activity enter picture-in-picture. */ public abstract void handlePictureInPictureRequested(@NonNull ActivityClientRecord r); + /** Whether the activity want to handle splash screen exit animation */ + public abstract boolean isHandleSplashScreenExit(@NonNull IBinder token); + + /** Attach a splash screen window view to the top of the activity */ + public abstract void handleAttachSplashScreenView(@NonNull ActivityClientRecord r, + @NonNull SplashScreenViewParcelable parcelable); + + /** Hand over the splash screen window view to the activity */ + public abstract void handOverSplashScreenView(@NonNull ActivityClientRecord r); + /** Perform activity launch. */ public abstract Activity handleLaunchActivity(@NonNull ActivityClientRecord r, PendingTransactionActions pendingActions, Intent customIntent); diff --git a/core/java/android/app/IActivityClientController.aidl b/core/java/android/app/IActivityClientController.aidl index 9d3286fa271c..bb743b89e00f 100644 --- a/core/java/android/app/IActivityClientController.aidl +++ b/core/java/android/app/IActivityClientController.aidl @@ -34,7 +34,7 @@ import com.android.internal.policy.IKeyguardDismissCallback; */ interface IActivityClientController { oneway void activityIdle(in IBinder token, in Configuration config, in boolean stopProfiling); - oneway void activityResumed(in IBinder token); + oneway void activityResumed(in IBinder token, in boolean handleSplashScreenExit); oneway void activityTopResumedStateLost(); /** * Notifies that the activity has completed paused. This call is not one-way because it can make @@ -142,4 +142,7 @@ interface IActivityClientController { * on the back stack. */ oneway void onBackPressedOnTaskRoot(in IBinder token); + + /** Reports that the splash screen view has attached to activity. */ + oneway void splashScreenAttached(in IBinder token); } diff --git a/core/java/android/app/IActivityTaskManager.aidl b/core/java/android/app/IActivityTaskManager.aidl index 38a3e70b3742..542f754ce364 100644 --- a/core/java/android/app/IActivityTaskManager.aidl +++ b/core/java/android/app/IActivityTaskManager.aidl @@ -70,6 +70,7 @@ import android.view.IRecentsAnimationRunner; import android.view.RemoteAnimationDefinition; import android.view.RemoteAnimationAdapter; import android.window.IWindowOrganizerController; +import android.window.SplashScreenView; import com.android.internal.app.IVoiceInteractor; import com.android.internal.os.IResultReceiver; @@ -348,4 +349,10 @@ interface IActivityTaskManager { * Clears launch params for given packages. */ void clearLaunchParamsForPackages(in List<String> packageNames); + + /** + * A splash screen view has copied. + */ + void onSplashScreenViewCopyFinished(int taskId, + in SplashScreenView.SplashScreenViewParcelable material); } diff --git a/core/java/android/app/servertransaction/ResumeActivityItem.java b/core/java/android/app/servertransaction/ResumeActivityItem.java index d451599cc7b0..e6fdc006615a 100644 --- a/core/java/android/app/servertransaction/ResumeActivityItem.java +++ b/core/java/android/app/servertransaction/ResumeActivityItem.java @@ -60,7 +60,7 @@ public class ResumeActivityItem extends ActivityLifecycleItem { public void postExecute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { // TODO(lifecycler): Use interface callback instead of actual implementation. - ActivityClient.getInstance().activityResumed(token); + ActivityClient.getInstance().activityResumed(token, client.isHandleSplashScreenExit(token)); } @Override diff --git a/core/java/android/app/servertransaction/TransferSplashScreenViewStateItem.java b/core/java/android/app/servertransaction/TransferSplashScreenViewStateItem.java new file mode 100644 index 000000000000..5374984d31d0 --- /dev/null +++ b/core/java/android/app/servertransaction/TransferSplashScreenViewStateItem.java @@ -0,0 +1,105 @@ +/* + * 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.app.servertransaction; + +import android.annotation.IntDef; +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.app.ActivityThread; +import android.app.ClientTransactionHandler; +import android.os.Parcel; +import android.window.SplashScreenView.SplashScreenViewParcelable; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Transfer a splash screen view to an Activity. + * @hide + */ +public class TransferSplashScreenViewStateItem extends ActivityTransactionItem { + + private SplashScreenViewParcelable mSplashScreenViewParcelable; + private @TransferRequest int mRequest; + + @IntDef(value = { + ATTACH_TO, + HANDOVER_TO + }) + @Retention(RetentionPolicy.SOURCE) + public @interface TransferRequest {} + // request client to attach the view on it. + public static final int ATTACH_TO = 0; + // tell client that you can handle the splash screen view. + public static final int HANDOVER_TO = 1; + + @Override + public void execute(@NonNull ClientTransactionHandler client, + @NonNull ActivityThread.ActivityClientRecord r, + PendingTransactionActions pendingActions) { + switch (mRequest) { + case ATTACH_TO: + client.handleAttachSplashScreenView(r, mSplashScreenViewParcelable); + break; + case HANDOVER_TO: + client.handOverSplashScreenView(r); + break; + } + } + + @Override + public void recycle() { + ObjectPool.recycle(this); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(mRequest); + dest.writeTypedObject(mSplashScreenViewParcelable, flags); + } + + private TransferSplashScreenViewStateItem() {} + private TransferSplashScreenViewStateItem(Parcel in) { + mRequest = in.readInt(); + mSplashScreenViewParcelable = in.readTypedObject(SplashScreenViewParcelable.CREATOR); + } + + /** Obtain an instance initialized with provided params. */ + public static TransferSplashScreenViewStateItem obtain(@TransferRequest int state, + @Nullable SplashScreenViewParcelable parcelable) { + TransferSplashScreenViewStateItem instance = + ObjectPool.obtain(TransferSplashScreenViewStateItem.class); + if (instance == null) { + instance = new TransferSplashScreenViewStateItem(); + } + instance.mRequest = state; + instance.mSplashScreenViewParcelable = parcelable; + + return instance; + } + + public static final @NonNull Creator<TransferSplashScreenViewStateItem> CREATOR = + new Creator<TransferSplashScreenViewStateItem>() { + public TransferSplashScreenViewStateItem createFromParcel(Parcel in) { + return new TransferSplashScreenViewStateItem(in); + } + + public TransferSplashScreenViewStateItem[] newArray(int size) { + return new TransferSplashScreenViewStateItem[size]; + } + }; +} diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index 6b13a290b20e..228ee3c76b14 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -2836,8 +2836,7 @@ public final class ViewRootImpl implements ViewParent, mScroller.abortAnimation(); } // Our surface is gone - if (mAttachInfo.mThreadedRenderer != null && - mAttachInfo.mThreadedRenderer.isEnabled()) { + if (isHardwareEnabled()) { mAttachInfo.mThreadedRenderer.destroy(); } } else if ((surfaceReplaced @@ -3923,8 +3922,15 @@ public final class ViewRootImpl implements ViewParent, }; } + /** + * @hide + */ + public boolean isHardwareEnabled() { + return mAttachInfo.mThreadedRenderer != null && mAttachInfo.mThreadedRenderer.isEnabled(); + } + private boolean addFrameCompleteCallbackIfNeeded() { - if (mAttachInfo.mThreadedRenderer == null || !mAttachInfo.mThreadedRenderer.isEnabled()) { + if (!isHardwareEnabled()) { return false; } @@ -4268,7 +4274,7 @@ public final class ViewRootImpl implements ViewParent, boolean useAsyncReport = false; if (!dirty.isEmpty() || mIsAnimating || accessibilityFocusDirty) { - if (mAttachInfo.mThreadedRenderer != null && mAttachInfo.mThreadedRenderer.isEnabled()) { + if (isHardwareEnabled()) { // If accessibility focus moved, always invalidate the root. boolean invalidateRoot = accessibilityFocusDirty || mInvalidateRootRequested; mInvalidateRootRequested = false; diff --git a/core/java/android/window/ITaskOrganizer.aidl b/core/java/android/window/ITaskOrganizer.aidl index 88b2257a55b1..8f541d0bd194 100644 --- a/core/java/android/window/ITaskOrganizer.aidl +++ b/core/java/android/window/ITaskOrganizer.aidl @@ -42,6 +42,11 @@ oneway interface ITaskOrganizer { void removeStartingWindow(int taskId); /** + * Called when the Task want to copy the splash screen. + */ + void copySplashScreenView(int taskId); + + /** * A callback when the Task is available for the registered organizer. The client is responsible * for releasing the SurfaceControl in the callback. For non-root tasks, the leash may initially * be hidden so it is up to the organizer to show this task. diff --git a/core/java/android/window/SplashScreen.java b/core/java/android/window/SplashScreen.java new file mode 100644 index 000000000000..4b88a9bc39de --- /dev/null +++ b/core/java/android/window/SplashScreen.java @@ -0,0 +1,188 @@ +/* + * Copyright (C) 2021 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 android.annotation.NonNull; +import android.annotation.Nullable; +import android.annotation.SuppressLint; +import android.app.Activity; +import android.app.ActivityThread; +import android.content.Context; +import android.os.IBinder; +import android.util.Singleton; +import android.util.Slog; + +import java.util.ArrayList; + +/** + * The interface that apps use to talk to the splash screen. + * <p> + * Each splash screen instance is bound to a particular {@link Activity}. + * To obtain a {@link SplashScreen} for an Activity, use + * <code>Activity.getSplashScreen()</code> to get the SplashScreen.</p> + */ +public interface SplashScreen { + /** + * <p>Specifies whether an {@link Activity} wants to handle the splash screen animation on its + * own. Normally the splash screen will show on screen before the content of the activity has + * been drawn, and disappear when the activity is showing on the screen. With this listener set, + * the activity will receive {@link OnExitAnimationListener#onSplashScreenExit} callback if + * splash screen is showed, then the activity can create its own exit animation based on the + * SplashScreenView.</p> + * + * <p> Note that this method must be called before splash screen leave, so it only takes effect + * during or before {@link Activity#onResume}.</p> + * + * @param listener the listener for receive the splash screen with + * + * @see OnExitAnimationListener#onSplashScreenExit(SplashScreenView) + */ + @SuppressLint("ExecutorRegistration") + void setOnExitAnimationListener(@Nullable SplashScreen.OnExitAnimationListener listener); + + /** + * Listens for the splash screen exit event. + */ + interface OnExitAnimationListener { + /** + * When receiving this callback, the {@link SplashScreenView} object will be drawing on top + * of the activity. The {@link SplashScreenView} represents the splash screen view + * object, developer can make an exit animation based on this view.</p> + * + * <p>If {@link SplashScreenView#remove} is not called after 5000ms, the method will be + * automatically called and the splash screen removed.</p> + * + * <p>This method is never invoked if your activity sets + * {@link #setOnExitAnimationListener} to <code>null</code>.. + * + * @param view The view object which on top of this Activity. + * @see #setOnExitAnimationListener + */ + void onSplashScreenExit(@NonNull SplashScreenView view); + } + + /** + * @hide + */ + class SplashScreenImpl implements SplashScreen { + private OnExitAnimationListener mExitAnimationListener; + private final IBinder mActivityToken; + private final SplashScreenManagerGlobal mGlobal; + + public SplashScreenImpl(Context context) { + mActivityToken = context.getActivityToken(); + mGlobal = SplashScreenManagerGlobal.getInstance(); + } + + @Override + public void setOnExitAnimationListener( + @Nullable SplashScreen.OnExitAnimationListener listener) { + if (mActivityToken == null) { + // This is not an activity. + return; + } + synchronized (mGlobal.mGlobalLock) { + mExitAnimationListener = listener; + if (listener != null) { + mGlobal.addImpl(this); + } else { + mGlobal.removeImpl(this); + } + } + } + } + + /** + * This class is only used internally to manage the activities for this process. + * + * @hide + */ + class SplashScreenManagerGlobal { + private static final String TAG = SplashScreen.class.getSimpleName(); + private final Object mGlobalLock = new Object(); + private final ArrayList<SplashScreenImpl> mImpls = new ArrayList<>(); + + private SplashScreenManagerGlobal() { + ActivityThread.currentActivityThread().registerSplashScreenManager(this); + } + + public static SplashScreenManagerGlobal getInstance() { + return sInstance.get(); + } + + private static final Singleton<SplashScreenManagerGlobal> sInstance = + new Singleton<SplashScreenManagerGlobal>() { + @Override + protected SplashScreenManagerGlobal create() { + return new SplashScreenManagerGlobal(); + } + }; + + private void addImpl(SplashScreenImpl impl) { + synchronized (mGlobalLock) { + mImpls.add(impl); + } + } + + private void removeImpl(SplashScreenImpl impl) { + synchronized (mGlobalLock) { + mImpls.remove(impl); + } + } + + private SplashScreenImpl findImpl(IBinder token) { + synchronized (mGlobalLock) { + for (SplashScreenImpl impl : mImpls) { + if (impl.mActivityToken == token) { + return impl; + } + } + } + return null; + } + + public void tokenDestroyed(IBinder token) { + synchronized (mGlobalLock) { + final SplashScreenImpl impl = findImpl(token); + if (impl != null) { + removeImpl(impl); + } + } + } + + public void dispatchOnExitAnimation(IBinder token, SplashScreenView view) { + synchronized (mGlobalLock) { + final SplashScreenImpl impl = findImpl(token); + if (impl == null) { + return; + } + if (impl.mExitAnimationListener == null) { + Slog.e(TAG, "cannot dispatch onExitAnimation to listener " + token); + return; + } + impl.mExitAnimationListener.onSplashScreenExit(view); + } + } + + public boolean containsExitListener(IBinder token) { + synchronized (mGlobalLock) { + final SplashScreenImpl impl = findImpl(token); + return impl != null && impl.mExitAnimationListener != null; + } + } + } +} diff --git a/core/java/android/window/SplashScreenView.aidl b/core/java/android/window/SplashScreenView.aidl new file mode 100644 index 000000000000..cc7ac1edce80 --- /dev/null +++ b/core/java/android/window/SplashScreenView.aidl @@ -0,0 +1,20 @@ +/* + * 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; + +/** @hide */ +parcelable SplashScreenView.SplashScreenViewParcelable; diff --git a/core/java/android/window/SplashScreenView.java b/core/java/android/window/SplashScreenView.java new file mode 100644 index 000000000000..b8d3fc2eda05 --- /dev/null +++ b/core/java/android/window/SplashScreenView.java @@ -0,0 +1,344 @@ +/* + * 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.annotation.ColorInt; +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Rect; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.os.Parcel; +import android.os.Parcelable; +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 mParceledBitmap; + // 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 mParceledBitmap; + private Drawable mIconDrawable; + + 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.getBitmap() != null) { + mIconDrawable = new BitmapDrawable(mContext.getResources(), parcelable.getBitmap()); + mParceledBitmap = parcelable.getBitmap(); + } + 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; + } + + /** + * 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); + 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); + } + if (mParceledBitmap != null) { + view.mParceledBitmap = mParceledBitmap; + } + if (DEBUG) { + Log.d(TAG, " build " + view + " center view? " + view.mIconView + + " iconSize " + mIconSize); + } + 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; + } + + /** + * <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 (mParceledBitmap != null) { + mIconView.setBackground(null); + mParceledBitmap.recycle(); + mParceledBitmap = 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. + */ + public @Nullable View getIconView() { + return mIconView; + } + + /** + * 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 mBitmap; + + public SplashScreenViewParcelable(SplashScreenView view) { + final ViewGroup.LayoutParams params = view.getIconView().getLayoutParams(); + mIconSize = params.height; + mBackgroundColor = view.getInitBackgroundColor(); + + final Drawable background = view.getIconView().getBackground(); + if (background != null) { + final Rect initialBounds = background.copyBounds(); + final int width = initialBounds.width(); + final int height = initialBounds.height(); + + final Bitmap iconSnapshot = Bitmap.createBitmap(width, height, + Bitmap.Config.ARGB_8888); + final Canvas bmpCanvas = new Canvas(iconSnapshot); + background.setBounds(0, 0, width, height); + background.draw(bmpCanvas); + mBitmap = iconSnapshot.createAshmemBitmap(); + iconSnapshot.recycle(); + } + } + + private SplashScreenViewParcelable(@NonNull Parcel source) { + readParcel(source); + } + + private void readParcel(@NonNull Parcel source) { + mIconSize = source.readInt(); + mBackgroundColor = source.readInt(); + mBitmap = source.readTypedObject(Bitmap.CREATOR); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(mIconSize); + dest.writeInt(mBackgroundColor); + dest.writeTypedObject(mBitmap, flags); + } + + 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 (mBitmap != null) { + mBitmap.recycle(); + mBitmap = null; + } + } + + int getIconSize() { + return mIconSize; + } + + int getBackgroundColor() { + return mBackgroundColor; + } + + Bitmap getBitmap() { + return mBitmap; + } + } +} diff --git a/core/java/android/window/TaskOrganizer.java b/core/java/android/window/TaskOrganizer.java index cdb4762a4f0a..217ade82b336 100644 --- a/core/java/android/window/TaskOrganizer.java +++ b/core/java/android/window/TaskOrganizer.java @@ -105,6 +105,12 @@ public class TaskOrganizer extends WindowOrganizer { public void removeStartingWindow(int taskId) {} /** + * Called when the Task want to copy the splash screen. + */ + @BinderThread + public void copySplashScreenView(int taskId) {} + + /** * Called when a task with the registered windowing mode can be controlled by this task * organizer. For non-root tasks, the leash may initially be hidden so it is up to the organizer * to show this task. @@ -223,6 +229,11 @@ public class TaskOrganizer extends WindowOrganizer { } @Override + public void copySplashScreenView(int taskId) { + mExecutor.execute(() -> TaskOrganizer.this.copySplashScreenView(taskId)); + } + + @Override public void onTaskAppeared(ActivityManager.RunningTaskInfo taskInfo, SurfaceControl leash) { mExecutor.execute(() -> TaskOrganizer.this.onTaskAppeared(taskInfo, leash)); } |
