|
| 1 | +/* |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with this work for additional information |
| 5 | + regarding copyright ownership. The ASF licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, |
| 13 | + software distributed under the License is distributed on an |
| 14 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + KIND, either express or implied. See the License for the |
| 16 | + specific language governing permissions and limitations |
| 17 | + under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package org.apache.cordova; |
| 21 | + |
| 22 | +import android.animation.Animator; |
| 23 | +import android.animation.AnimatorListenerAdapter; |
| 24 | +import android.annotation.SuppressLint; |
| 25 | +import android.os.Handler; |
| 26 | +import android.view.View; |
| 27 | +import android.view.animation.AccelerateInterpolator; |
| 28 | + |
| 29 | +import androidx.annotation.NonNull; |
| 30 | +import androidx.core.splashscreen.SplashScreen; |
| 31 | +import androidx.core.splashscreen.SplashScreenViewProvider; |
| 32 | + |
| 33 | +import org.json.JSONArray; |
| 34 | +import org.json.JSONException; |
| 35 | + |
| 36 | +@SuppressLint("LongLogTag") |
| 37 | +public class SplashScreenPlugin extends CordovaPlugin { |
| 38 | + static final String PLUGIN_NAME = "CordovaSplashScreenPlugin"; |
| 39 | + |
| 40 | + // Default config preference values |
| 41 | + private static final boolean DEFAULT_AUTO_HIDE = true; |
| 42 | + private static final int DEFAULT_DELAY_TIME = -1; |
| 43 | + private static final boolean DEFAULT_FADE = true; |
| 44 | + private static final int DEFAULT_FADE_TIME = 500; |
| 45 | + |
| 46 | + // Config preference values |
| 47 | + /** |
| 48 | + * @param boolean autoHide to auto splash screen (default=true) |
| 49 | + */ |
| 50 | + private boolean autoHide; |
| 51 | + /** |
| 52 | + * @param int delayTime in milliseconds (default=-1) |
| 53 | + */ |
| 54 | + private int delayTime; |
| 55 | + /** |
| 56 | + * @param int fade to fade out splash screen (default=true) |
| 57 | + */ |
| 58 | + private boolean isFadeEnabled; |
| 59 | + /** |
| 60 | + * @param int fadeDuration fade out duration in milliseconds (default=500) |
| 61 | + */ |
| 62 | + private int fadeDuration; |
| 63 | + |
| 64 | + // Internal variables |
| 65 | + /** |
| 66 | + * @param boolean keepOnScreen flag to determine if the splash screen remains visible. |
| 67 | + */ |
| 68 | + private boolean keepOnScreen = true; |
| 69 | + |
| 70 | + @Override |
| 71 | + protected void pluginInitialize() { |
| 72 | + // Auto Hide & Delay Settings |
| 73 | + autoHide = preferences.getBoolean("AutoHideSplashScreen", DEFAULT_AUTO_HIDE); |
| 74 | + delayTime = preferences.getInteger("SplashScreenDelay", DEFAULT_DELAY_TIME); |
| 75 | + LOG.d(PLUGIN_NAME, "Auto Hide: " + autoHide); |
| 76 | + if (delayTime != DEFAULT_DELAY_TIME) { |
| 77 | + LOG.d(PLUGIN_NAME, "Delay: " + delayTime + "ms"); |
| 78 | + } |
| 79 | + |
| 80 | + // Fade & Fade Duration |
| 81 | + isFadeEnabled = preferences.getBoolean("FadeSplashScreen", DEFAULT_FADE); |
| 82 | + fadeDuration = preferences.getInteger("FadeSplashScreenDuration", DEFAULT_FADE_TIME); |
| 83 | + LOG.d(PLUGIN_NAME, "Fade: " + isFadeEnabled); |
| 84 | + if (isFadeEnabled) { |
| 85 | + LOG.d(PLUGIN_NAME, "Fade Duration: " + fadeDuration + "ms"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public boolean execute( |
| 91 | + String action, |
| 92 | + JSONArray args, |
| 93 | + CallbackContext callbackContext |
| 94 | + ) throws JSONException { |
| 95 | + if (action.equals("hide") && autoHide == false) { |
| 96 | + /* |
| 97 | + * The `.hide()` method can only be triggered if the `splashScreenAutoHide` |
| 98 | + * is set to `false`. |
| 99 | + */ |
| 100 | + keepOnScreen = false; |
| 101 | + } else { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + callbackContext.success(); |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Object onMessage(String id, Object data) { |
| 111 | + switch (id) { |
| 112 | + case "setupSplashScreen": |
| 113 | + setupSplashScreen((SplashScreen) data); |
| 114 | + break; |
| 115 | + |
| 116 | + case "onPageFinished": |
| 117 | + attemptCloseOnPageFinished(); |
| 118 | + break; |
| 119 | + } |
| 120 | + |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + private void setupSplashScreen(SplashScreen splashScreen) { |
| 125 | + // Setup Splash Screen Delay |
| 126 | + splashScreen.setKeepOnScreenCondition(() -> keepOnScreen); |
| 127 | + |
| 128 | + // auto hide splash screen when custom delay is defined. |
| 129 | + if (autoHide && delayTime != DEFAULT_DELAY_TIME) { |
| 130 | + Handler splashScreenDelayHandler = new Handler(); |
| 131 | + splashScreenDelayHandler.postDelayed(() -> keepOnScreen = false, delayTime); |
| 132 | + } |
| 133 | + |
| 134 | + // auto hide splash screen with default delay (-1) delay is controlled by the |
| 135 | + // `onPageFinished` message. |
| 136 | + |
| 137 | + // If auto hide is disabled (false), the hiding of the splash screen must be determined & |
| 138 | + // triggered by the front-end code with the `navigator.splashscreen.hide()` method. |
| 139 | + |
| 140 | + // Setup the fade |
| 141 | + splashScreen.setOnExitAnimationListener(new SplashScreen.OnExitAnimationListener() { |
| 142 | + @Override |
| 143 | + public void onSplashScreenExit(@NonNull SplashScreenViewProvider splashScreenViewProvider) { |
| 144 | + View splashScreenView = splashScreenViewProvider.getView(); |
| 145 | + |
| 146 | + splashScreenView |
| 147 | + .animate() |
| 148 | + .alpha(0.0f) |
| 149 | + .setDuration(isFadeEnabled ? fadeDuration : 0) |
| 150 | + .setStartDelay(isFadeEnabled ? 0 : fadeDuration) |
| 151 | + .setInterpolator(new AccelerateInterpolator()) |
| 152 | + .setListener(new AnimatorListenerAdapter() { |
| 153 | + @Override |
| 154 | + public void onAnimationEnd(Animator animation) { |
| 155 | + super.onAnimationEnd(animation); |
| 156 | + splashScreenViewProvider.remove(); |
| 157 | + } |
| 158 | + }).start(); |
| 159 | + } |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + private void attemptCloseOnPageFinished() { |
| 164 | + if (autoHide && delayTime == DEFAULT_DELAY_TIME) { |
| 165 | + keepOnScreen = false; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments