| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.window; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.StyleRes; |
| 22 |
import android.annotation.SuppressLint; |
| 23 |
import android.annotation.UiThread; |
| 24 |
import android.app.Activity; |
| 25 |
import android.app.ActivityOptions; |
| 26 |
import android.app.ActivityThread; |
| 27 |
import android.app.AppGlobals; |
| 28 |
import android.content.Context; |
| 29 |
import android.content.res.Resources; |
| 30 |
import android.os.IBinder; |
| 31 |
import android.os.RemoteException; |
| 32 |
import android.util.Log; |
| 33 |
import android.util.Singleton; |
| 34 |
import android.util.Slog; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public interface SplashScreen { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
int SPLASH_SCREEN_STYLE_UNDEFINED = -1; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
int SPLASH_SCREEN_STYLE_SOLID_COLOR = 0; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
int SPLASH_SCREEN_STYLE_ICON = 1; |
| 61 |
|
| 62 |
|
| 63 |
@IntDef(prefix = { "SPLASH_SCREEN_STYLE_" }, value = { |
| 64 |
SPLASH_SCREEN_STYLE_UNDEFINED, |
| 65 |
SPLASH_SCREEN_STYLE_SOLID_COLOR, |
| 66 |
SPLASH_SCREEN_STYLE_ICON |
| 67 |
}) |
| 68 |
@interface SplashScreenStyle {} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@SuppressLint("ExecutorRegistration") |
| 86 |
void setOnExitAnimationListener(@NonNull SplashScreen.OnExitAnimationListener listener); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
void clearOnExitAnimationListener(); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
void setSplashScreenTheme(@StyleRes int themeId); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
interface OnExitAnimationListener { |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
@UiThread |
| 129 |
void onSplashScreenExit(@NonNull SplashScreenView view); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
class SplashScreenImpl implements SplashScreen { |
| 136 |
private static final String TAG = "SplashScreenImpl"; |
| 137 |
|
| 138 |
private OnExitAnimationListener mExitAnimationListener; |
| 139 |
private final IBinder mActivityToken; |
| 140 |
private final SplashScreenManagerGlobal mGlobal; |
| 141 |
|
| 142 |
public SplashScreenImpl(Context context) { |
| 143 |
mActivityToken = context.getActivityToken(); |
| 144 |
mGlobal = SplashScreenManagerGlobal.getInstance(); |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public void setOnExitAnimationListener( |
| 149 |
@NonNull SplashScreen.OnExitAnimationListener listener) { |
| 150 |
if (mActivityToken == null) { |
| 151 |
|
| 152 |
return; |
| 153 |
} |
| 154 |
synchronized (mGlobal.mGlobalLock) { |
| 155 |
if (listener != null) { |
| 156 |
mExitAnimationListener = listener; |
| 157 |
mGlobal.addImpl(this); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public void clearOnExitAnimationListener() { |
| 164 |
if (mActivityToken == null) { |
| 165 |
|
| 166 |
return; |
| 167 |
} |
| 168 |
synchronized (mGlobal.mGlobalLock) { |
| 169 |
mExitAnimationListener = null; |
| 170 |
mGlobal.removeImpl(this); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
public void setSplashScreenTheme(@StyleRes int themeId) { |
| 175 |
if (mActivityToken == null) { |
| 176 |
Log.w(TAG, "Couldn't persist the starting theme. This instance is not an Activity"); |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
Activity activity = ActivityThread.currentActivityThread().getActivity( |
| 181 |
mActivityToken); |
| 182 |
if (activity == null) { |
| 183 |
return; |
| 184 |
} |
| 185 |
String themeName = themeId != Resources.ID_NULL |
| 186 |
? activity.getResources().getResourceName(themeId) : null; |
| 187 |
|
| 188 |
try { |
| 189 |
AppGlobals.getPackageManager().setSplashScreenTheme( |
| 190 |
activity.getComponentName().getPackageName(), |
| 191 |
themeName, activity.getUserId()); |
| 192 |
} catch (RemoteException e) { |
| 193 |
Log.w(TAG, "Couldn't persist the starting theme", e); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
class SplashScreenManagerGlobal { |
| 204 |
private static final String TAG = SplashScreen.class.getSimpleName(); |
| 205 |
private final Object mGlobalLock = new Object(); |
| 206 |
private final ArrayList<SplashScreenImpl> mImpls = new ArrayList<>(); |
| 207 |
|
| 208 |
private SplashScreenManagerGlobal() { |
| 209 |
ActivityThread.currentActivityThread().registerSplashScreenManager(this); |
| 210 |
} |
| 211 |
|
| 212 |
public static SplashScreenManagerGlobal getInstance() { |
| 213 |
return sInstance.get(); |
| 214 |
} |
| 215 |
|
| 216 |
private static final Singleton<SplashScreenManagerGlobal> sInstance = |
| 217 |
new Singleton<SplashScreenManagerGlobal>() { |
| 218 |
@Override |
| 219 |
protected SplashScreenManagerGlobal create() { |
| 220 |
return new SplashScreenManagerGlobal(); |
| 221 |
} |
| 222 |
}; |
| 223 |
|
| 224 |
private void addImpl(SplashScreenImpl impl) { |
| 225 |
synchronized (mGlobalLock) { |
| 226 |
mImpls.add(impl); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
private void removeImpl(SplashScreenImpl impl) { |
| 231 |
synchronized (mGlobalLock) { |
| 232 |
mImpls.remove(impl); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
private SplashScreenImpl findImpl(IBinder token) { |
| 237 |
synchronized (mGlobalLock) { |
| 238 |
for (SplashScreenImpl impl : mImpls) { |
| 239 |
if (impl.mActivityToken == token) { |
| 240 |
return impl; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
return null; |
| 245 |
} |
| 246 |
|
| 247 |
public void tokenDestroyed(IBinder token) { |
| 248 |
synchronized (mGlobalLock) { |
| 249 |
final SplashScreenImpl impl = findImpl(token); |
| 250 |
if (impl != null) { |
| 251 |
removeImpl(impl); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
public void handOverSplashScreenView(@NonNull IBinder token, |
| 257 |
@NonNull SplashScreenView splashScreenView) { |
| 258 |
dispatchOnExitAnimation(token, splashScreenView); |
| 259 |
} |
| 260 |
|
| 261 |
private void dispatchOnExitAnimation(IBinder token, SplashScreenView view) { |
| 262 |
synchronized (mGlobalLock) { |
| 263 |
final SplashScreenImpl impl = findImpl(token); |
| 264 |
if (impl == null) { |
| 265 |
return; |
| 266 |
} |
| 267 |
if (impl.mExitAnimationListener == null) { |
| 268 |
Slog.e(TAG, "cannot dispatch onExitAnimation to listener " + token); |
| 269 |
return; |
| 270 |
} |
| 271 |
impl.mExitAnimationListener.onSplashScreenExit(view); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
public boolean containsExitListener(IBinder token) { |
| 276 |
synchronized (mGlobalLock) { |
| 277 |
final SplashScreenImpl impl = findImpl(token); |
| 278 |
return impl != null && impl.mExitAnimationListener != null; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |