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
|
/*
* 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.IntDef;
import android.annotation.NonNull;
import android.annotation.StyleRes;
import android.annotation.SuppressLint;
import android.annotation.UiThread;
import android.app.Activity;
import android.app.ActivityOptions;
import android.app.ActivityThread;
import android.app.AppGlobals;
import android.content.Context;
import android.content.res.Resources;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
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 {
/**
* The splash screen style is not defined.
* @hide
*/
int SPLASH_SCREEN_STYLE_UNDEFINED = -1;
/**
* Flag to be used with {@link ActivityOptions#setSplashScreenStyle}, to avoid showing the
* splash screen icon of the launched activity
*/
int SPLASH_SCREEN_STYLE_SOLID_COLOR = 0;
/**
* Flag to be used with {@link ActivityOptions#setSplashScreenStyle}, to show the splash screen
* icon of the launched activity.
*/
int SPLASH_SCREEN_STYLE_ICON = 1;
/** @hide */
@IntDef(prefix = { "SPLASH_SCREEN_STYLE_" }, value = {
SPLASH_SCREEN_STYLE_UNDEFINED,
SPLASH_SCREEN_STYLE_SOLID_COLOR,
SPLASH_SCREEN_STYLE_ICON
})
@interface SplashScreenStyle {}
/**
* <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(@NonNull SplashScreen.OnExitAnimationListener listener);
/**
* Clear exist listener
* @see #setOnExitAnimationListener
*/
void clearOnExitAnimationListener();
/**
* Overrides the theme used for the {@link SplashScreen}s of this application.
* <p>
* By default, the {@link SplashScreen} uses the theme set in the manifest. This method
* overrides and persists the theme used for the {@link SplashScreen} of this application.
* <p>
* To reset to the default theme, set this the themeId to {@link Resources#ID_NULL}.
* <p>
* <b>Note:</b> Internally, the theme name is resolved and persisted. This means that the theme
* name must be stable across versions, otherwise it won't be found after your application is
* updated.
*
* @param themeId The ID of the splashscreen theme to be used in place of the one defined in
* the manifest.
*/
void setSplashScreenTheme(@StyleRes int themeId);
/**
* 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>This method is never invoked if your activity clear the listener by
* {@link #clearOnExitAnimationListener}.
*
* @param view The view object which on top of this Activity.
* @see #setOnExitAnimationListener
* @see #clearOnExitAnimationListener
*/
@UiThread
void onSplashScreenExit(@NonNull SplashScreenView view);
}
/**
* @hide
*/
class SplashScreenImpl implements SplashScreen {
private static final String TAG = "SplashScreenImpl";
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(
@NonNull SplashScreen.OnExitAnimationListener listener) {
if (mActivityToken == null) {
// This is not an activity.
return;
}
synchronized (mGlobal.mGlobalLock) {
if (listener != null) {
mExitAnimationListener = listener;
mGlobal.addImpl(this);
}
}
}
@Override
public void clearOnExitAnimationListener() {
if (mActivityToken == null) {
// This is not an activity.
return;
}
synchronized (mGlobal.mGlobalLock) {
mExitAnimationListener = null;
mGlobal.removeImpl(this);
}
}
public void setSplashScreenTheme(@StyleRes int themeId) {
if (mActivityToken == null) {
Log.w(TAG, "Couldn't persist the starting theme. This instance is not an Activity");
return;
}
Activity activity = ActivityThread.currentActivityThread().getActivity(
mActivityToken);
if (activity == null) {
return;
}
String themeName = themeId != Resources.ID_NULL
? activity.getResources().getResourceName(themeId) : null;
try {
AppGlobals.getPackageManager().setSplashScreenTheme(
activity.getComponentName().getPackageName(),
themeName, activity.getUserId());
} catch (RemoteException e) {
Log.w(TAG, "Couldn't persist the starting theme", e);
}
}
}
/**
* 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 handOverSplashScreenView(@NonNull IBinder token,
@NonNull SplashScreenView splashScreenView) {
dispatchOnExitAnimation(token, splashScreenView);
}
private 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;
}
}
}
}
|