@@ -0,0 +1,413 @@
|
||||
/*
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you 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 org.apache.cordova.splashscreen;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.Handler;
|
||||
import android.view.Display;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup.LayoutParams;
|
||||
import android.view.WindowManager;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.AlphaAnimation;
|
||||
import android.view.animation.DecelerateInterpolator;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CordovaWebView;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
public class SplashScreen extends CordovaPlugin {
|
||||
private static final String LOG_TAG = "SplashScreen";
|
||||
// Cordova 3.x.x has a copy of this plugin bundled with it (SplashScreenInternal.java).
|
||||
// Enable functionality only if running on 4.x.x.
|
||||
private static final boolean HAS_BUILT_IN_SPLASH_SCREEN = Integer.valueOf(CordovaWebView.CORDOVA_VERSION.split("\\.")[0]) < 4;
|
||||
private static final int DEFAULT_SPLASHSCREEN_DURATION = 3000;
|
||||
private static final int DEFAULT_FADE_DURATION = 500;
|
||||
private static Dialog splashDialog;
|
||||
private static ProgressDialog spinnerDialog;
|
||||
private static boolean firstShow = true;
|
||||
private static boolean lastHideAfterDelay; // https://issues.apache.org/jira/browse/CB-9094
|
||||
|
||||
/**
|
||||
* Displays the splash drawable.
|
||||
*/
|
||||
private ImageView splashImageView;
|
||||
|
||||
/**
|
||||
* Remember last device orientation to detect orientation changes.
|
||||
*/
|
||||
private int orientation;
|
||||
|
||||
// Helper to be compile-time compatible with both Cordova 3.x and 4.x.
|
||||
private View getView() {
|
||||
try {
|
||||
return (View)webView.getClass().getMethod("getView").invoke(webView);
|
||||
} catch (Exception e) {
|
||||
return (View)webView;
|
||||
}
|
||||
}
|
||||
|
||||
private int getSplashId() {
|
||||
int drawableId = 0;
|
||||
String splashResource = preferences.getString("SplashScreen", "screen");
|
||||
if (splashResource != null) {
|
||||
drawableId = cordova.getActivity().getResources().getIdentifier(splashResource, "drawable", cordova.getActivity().getClass().getPackage().getName());
|
||||
if (drawableId == 0) {
|
||||
drawableId = cordova.getActivity().getResources().getIdentifier(splashResource, "drawable", cordova.getActivity().getPackageName());
|
||||
}
|
||||
}
|
||||
return drawableId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void pluginInitialize() {
|
||||
if (HAS_BUILT_IN_SPLASH_SCREEN) {
|
||||
return;
|
||||
}
|
||||
// Make WebView invisible while loading URL
|
||||
// CB-11326 Ensure we're calling this on UI thread
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getView().setVisibility(View.INVISIBLE);
|
||||
}
|
||||
});
|
||||
int drawableId = getSplashId();
|
||||
|
||||
// Save initial orientation.
|
||||
orientation = cordova.getActivity().getResources().getConfiguration().orientation;
|
||||
|
||||
if (firstShow) {
|
||||
boolean autoHide = preferences.getBoolean("AutoHideSplashScreen", true);
|
||||
showSplashScreen(autoHide);
|
||||
}
|
||||
|
||||
if (preferences.getBoolean("SplashShowOnlyFirstTime", true)) {
|
||||
firstShow = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorter way to check value of "SplashMaintainAspectRatio" preference.
|
||||
*/
|
||||
private boolean isMaintainAspectRatio () {
|
||||
return preferences.getBoolean("SplashMaintainAspectRatio", false);
|
||||
}
|
||||
|
||||
private int getFadeDuration () {
|
||||
int fadeSplashScreenDuration = preferences.getBoolean("FadeSplashScreen", true) ?
|
||||
preferences.getInteger("FadeSplashScreenDuration", DEFAULT_FADE_DURATION) : 0;
|
||||
|
||||
if (fadeSplashScreenDuration < 30) {
|
||||
// [CB-9750] This value used to be in decimal seconds, so we will assume that if someone specifies 10
|
||||
// they mean 10 seconds, and not the meaningless 10ms
|
||||
fadeSplashScreenDuration *= 1000;
|
||||
}
|
||||
|
||||
return fadeSplashScreenDuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause(boolean multitasking) {
|
||||
if (HAS_BUILT_IN_SPLASH_SCREEN) {
|
||||
return;
|
||||
}
|
||||
// hide the splash screen to avoid leaking a window
|
||||
this.removeSplashScreen(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if (HAS_BUILT_IN_SPLASH_SCREEN) {
|
||||
return;
|
||||
}
|
||||
// hide the splash screen to avoid leaking a window
|
||||
this.removeSplashScreen(true);
|
||||
// If we set this to true onDestroy, we lose track when we go from page to page!
|
||||
//firstShow = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
if (action.equals("hide")) {
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
webView.postMessage("splashscreen", "hide");
|
||||
}
|
||||
});
|
||||
} else if (action.equals("show")) {
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
webView.postMessage("splashscreen", "show");
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
callbackContext.success();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object onMessage(String id, Object data) {
|
||||
if (HAS_BUILT_IN_SPLASH_SCREEN) {
|
||||
return null;
|
||||
}
|
||||
if ("splashscreen".equals(id)) {
|
||||
if ("hide".equals(data.toString())) {
|
||||
this.removeSplashScreen(false);
|
||||
} else {
|
||||
this.showSplashScreen(false);
|
||||
}
|
||||
} else if ("spinner".equals(id)) {
|
||||
if ("stop".equals(data.toString())) {
|
||||
getView().setVisibility(View.VISIBLE);
|
||||
}
|
||||
} else if ("onReceivedError".equals(id)) {
|
||||
this.spinnerStop();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Don't add @Override so that plugin still compiles on 3.x.x for a while
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
if (newConfig.orientation != orientation) {
|
||||
orientation = newConfig.orientation;
|
||||
|
||||
// Splash drawable may change with orientation, so reload it.
|
||||
if (splashImageView != null) {
|
||||
int drawableId = getSplashId();
|
||||
if (drawableId != 0) {
|
||||
splashImageView.setImageDrawable(cordova.getActivity().getResources().getDrawable(drawableId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeSplashScreen(final boolean forceHideImmediately) {
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if (splashDialog != null && splashDialog.isShowing()) {
|
||||
final int fadeSplashScreenDuration = getFadeDuration();
|
||||
// CB-10692 If the plugin is being paused/destroyed, skip the fading and hide it immediately
|
||||
if (fadeSplashScreenDuration > 0 && forceHideImmediately == false) {
|
||||
AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
|
||||
fadeOut.setInterpolator(new DecelerateInterpolator());
|
||||
fadeOut.setDuration(fadeSplashScreenDuration);
|
||||
|
||||
splashImageView.setAnimation(fadeOut);
|
||||
splashImageView.startAnimation(fadeOut);
|
||||
|
||||
fadeOut.setAnimationListener(new Animation.AnimationListener() {
|
||||
@Override
|
||||
public void onAnimationStart(Animation animation) {
|
||||
spinnerStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationEnd(Animation animation) {
|
||||
if (splashDialog != null && splashDialog.isShowing()) {
|
||||
splashDialog.dismiss();
|
||||
splashDialog = null;
|
||||
splashImageView = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationRepeat(Animation animation) {
|
||||
}
|
||||
});
|
||||
} else {
|
||||
spinnerStop();
|
||||
splashDialog.dismiss();
|
||||
splashDialog = null;
|
||||
splashImageView = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the splash screen over the full Activity
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
private void showSplashScreen(final boolean hideAfterDelay) {
|
||||
final int splashscreenTime = preferences.getInteger("SplashScreenDelay", DEFAULT_SPLASHSCREEN_DURATION);
|
||||
final int drawableId = getSplashId();
|
||||
|
||||
final int fadeSplashScreenDuration = getFadeDuration();
|
||||
final int effectiveSplashDuration = Math.max(0, splashscreenTime - fadeSplashScreenDuration);
|
||||
|
||||
lastHideAfterDelay = hideAfterDelay;
|
||||
|
||||
// Prevent to show the splash dialog if the activity is in the process of finishing
|
||||
if (cordova.getActivity().isFinishing()) {
|
||||
return;
|
||||
}
|
||||
// If the splash dialog is showing don't try to show it again
|
||||
if (splashDialog != null && splashDialog.isShowing()) {
|
||||
return;
|
||||
}
|
||||
if (drawableId == 0 || (splashscreenTime <= 0 && hideAfterDelay)) {
|
||||
return;
|
||||
}
|
||||
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
// Get reference to display
|
||||
Display display = cordova.getActivity().getWindowManager().getDefaultDisplay();
|
||||
Context context = webView.getContext();
|
||||
|
||||
// Use an ImageView to render the image because of its flexible scaling options.
|
||||
splashImageView = new ImageView(context);
|
||||
splashImageView.setImageResource(drawableId);
|
||||
LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
|
||||
splashImageView.setLayoutParams(layoutParams);
|
||||
|
||||
splashImageView.setMinimumHeight(display.getHeight());
|
||||
splashImageView.setMinimumWidth(display.getWidth());
|
||||
|
||||
// TODO: Use the background color of the webView's parent instead of using the preference.
|
||||
splashImageView.setBackgroundColor(preferences.getInteger("backgroundColor", Color.BLACK));
|
||||
|
||||
if (isMaintainAspectRatio()) {
|
||||
// CENTER_CROP scale mode is equivalent to CSS "background-size:cover"
|
||||
splashImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
||||
}
|
||||
else {
|
||||
// FIT_XY scales image non-uniformly to fit into image view.
|
||||
splashImageView.setScaleType(ImageView.ScaleType.FIT_XY);
|
||||
}
|
||||
|
||||
// Create and show the dialog
|
||||
splashDialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar);
|
||||
// check to see if the splash screen should be full screen
|
||||
if ((cordova.getActivity().getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
|
||||
== WindowManager.LayoutParams.FLAG_FULLSCREEN) {
|
||||
splashDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
}
|
||||
splashDialog.setContentView(splashImageView);
|
||||
splashDialog.setCancelable(false);
|
||||
splashDialog.show();
|
||||
|
||||
if (preferences.getBoolean("ShowSplashScreenSpinner", true)) {
|
||||
spinnerStart();
|
||||
}
|
||||
|
||||
// Set Runnable to remove splash screen just in case
|
||||
if (hideAfterDelay) {
|
||||
final Handler handler = new Handler();
|
||||
handler.postDelayed(new Runnable() {
|
||||
public void run() {
|
||||
if (lastHideAfterDelay) {
|
||||
removeSplashScreen(false);
|
||||
}
|
||||
}
|
||||
}, effectiveSplashDuration);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Show only spinner in the center of the screen
|
||||
private void spinnerStart() {
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
spinnerStop();
|
||||
|
||||
spinnerDialog = new ProgressDialog(webView.getContext());
|
||||
spinnerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
spinnerDialog = null;
|
||||
}
|
||||
});
|
||||
|
||||
spinnerDialog.setCancelable(false);
|
||||
spinnerDialog.setIndeterminate(true);
|
||||
|
||||
RelativeLayout centeredLayout = new RelativeLayout(cordova.getActivity());
|
||||
centeredLayout.setGravity(Gravity.CENTER);
|
||||
centeredLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
|
||||
|
||||
ProgressBar progressBar = new ProgressBar(webView.getContext());
|
||||
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
|
||||
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
|
||||
progressBar.setLayoutParams(layoutParams);
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
|
||||
String colorName = preferences.getString("SplashScreenSpinnerColor", null);
|
||||
if(colorName != null){
|
||||
int[][] states = new int[][] {
|
||||
new int[] { android.R.attr.state_enabled}, // enabled
|
||||
new int[] {-android.R.attr.state_enabled}, // disabled
|
||||
new int[] {-android.R.attr.state_checked}, // unchecked
|
||||
new int[] { android.R.attr.state_pressed} // pressed
|
||||
};
|
||||
int progressBarColor = Color.parseColor(colorName);
|
||||
int[] colors = new int[] {
|
||||
progressBarColor,
|
||||
progressBarColor,
|
||||
progressBarColor,
|
||||
progressBarColor
|
||||
};
|
||||
ColorStateList colorStateList = new ColorStateList(states, colors);
|
||||
progressBar.setIndeterminateTintList(colorStateList);
|
||||
}
|
||||
}
|
||||
|
||||
centeredLayout.addView(progressBar);
|
||||
|
||||
spinnerDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
|
||||
spinnerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
||||
|
||||
spinnerDialog.show();
|
||||
spinnerDialog.setContentView(centeredLayout);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void spinnerStop() {
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if (spinnerDialog != null && spinnerDialog.isShowing()) {
|
||||
spinnerDialog.dismiss();
|
||||
spinnerDialog = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
170
plugins/cordova-plugin-splashscreen/src/browser/SplashScreenProxy.js
vendored
Normal file
170
plugins/cordova-plugin-splashscreen/src/browser/SplashScreenProxy.js
vendored
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you 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.
|
||||
*
|
||||
*/
|
||||
|
||||
// Default parameter values including image size can be changed in `config.xml`
|
||||
var splashImageWidth = 170;
|
||||
var splashImageHeight = 200;
|
||||
var position = { x: 0, y: 0, width: splashImageWidth, height: splashImageHeight };
|
||||
var localSplash; // the image to display
|
||||
var localSplashImage;
|
||||
var bgColor = "#464646";
|
||||
var imageSrc = '/img/logo.png';
|
||||
var splashScreenDelay = 3000; // in milliseconds
|
||||
var showSplashScreen = true; // show splashcreen by default
|
||||
var cordova = require('cordova');
|
||||
var configHelper = cordova.require('cordova/confighelper');
|
||||
var autoHideSplashScreen = true;
|
||||
|
||||
function updateImageLocation() {
|
||||
position.width = Math.min(splashImageWidth, window.innerWidth);
|
||||
position.height = position.width * (splashImageHeight / splashImageWidth);
|
||||
|
||||
localSplash.style.width = window.innerWidth + "px";
|
||||
localSplash.style.height = window.innerHeight + "px";
|
||||
localSplash.style.top = "0px";
|
||||
localSplash.style.left = "0px";
|
||||
|
||||
localSplashImage.style.top = "50%";
|
||||
localSplashImage.style.left = "50%";
|
||||
localSplashImage.style.height = position.height + "px";
|
||||
localSplashImage.style.width = position.width + "px";
|
||||
localSplashImage.style.marginTop = (-position.height / 2) + "px";
|
||||
localSplashImage.style.marginLeft = (-position.width / 2) + "px";
|
||||
}
|
||||
|
||||
function onResize() {
|
||||
updateImageLocation();
|
||||
}
|
||||
|
||||
var SplashScreen = {
|
||||
setBGColor: function (cssBGColor) {
|
||||
bgColor = cssBGColor;
|
||||
if (localSplash) {
|
||||
localSplash.style.backgroundColor = bgColor;
|
||||
}
|
||||
},
|
||||
show: function () {
|
||||
if(!localSplash) {
|
||||
window.addEventListener("resize", onResize, false);
|
||||
localSplash = document.createElement("div");
|
||||
localSplash.style.backgroundColor = bgColor;
|
||||
localSplash.style.position = "absolute";
|
||||
localSplash.style["z-index"] = "99999";
|
||||
|
||||
localSplashImage = document.createElement("img");
|
||||
localSplashImage.src = imageSrc;
|
||||
localSplashImage.style.position = "absolute";
|
||||
|
||||
updateImageLocation();
|
||||
|
||||
localSplash.appendChild(localSplashImage);
|
||||
document.body.appendChild(localSplash);
|
||||
|
||||
// deviceready fires earlier than the plugin init on cold-start
|
||||
if (SplashScreen.shouldHideImmediately) {
|
||||
SplashScreen.shouldHideImmediately = false;
|
||||
window.setTimeout(function () {
|
||||
SplashScreen.hide();
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
},
|
||||
hide: function () {
|
||||
if(localSplash) {
|
||||
var innerLocalSplash = localSplash;
|
||||
localSplash = null;
|
||||
window.removeEventListener("resize", onResize, false);
|
||||
|
||||
innerLocalSplash.style.opacity = '0';
|
||||
innerLocalSplash.style["-webkit-transition"] = "opacity 1s ease-in-out";
|
||||
innerLocalSplash.style["-moz-transition"] = "opacity 1s ease-in-out";
|
||||
innerLocalSplash.style["-ms-transition"] = "opacity 1s ease-in-out";
|
||||
innerLocalSplash.style["-o-transition"] = "opacity 1s ease-in-out";
|
||||
|
||||
window.setTimeout(function () {
|
||||
document.body.removeChild(innerLocalSplash);
|
||||
innerLocalSplash = null;
|
||||
}, 1000);
|
||||
} else {
|
||||
SplashScreen.shouldHideImmediately = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads preferences via ConfigHelper and substitutes default parameters.
|
||||
*/
|
||||
function readPreferencesFromCfg(cfg) {
|
||||
try {
|
||||
var value = cfg.getPreferenceValue('ShowSplashScreen');
|
||||
if(typeof value != 'undefined') {
|
||||
showSplashScreen = value === 'true';
|
||||
}
|
||||
|
||||
splashScreenDelay = cfg.getPreferenceValue('SplashScreenDelay') || splashScreenDelay;
|
||||
splashScreenDelay = parseInt(splashScreenDelay, 10);
|
||||
|
||||
imageSrc = cfg.getPreferenceValue('SplashScreen') || imageSrc;
|
||||
bgColor = cfg.getPreferenceValue('SplashScreenBackgroundColor') || bgColor;
|
||||
splashImageWidth = cfg.getPreferenceValue('SplashScreenWidth') || splashImageWidth;
|
||||
splashImageHeight = cfg.getPreferenceValue('SplashScreenHeight') || splashImageHeight;
|
||||
autoHideSplashScreen = cfg.getPreferenceValue('AutoHideSplashScreen') || autoHideSplashScreen;
|
||||
autoHideSplashScreen = (autoHideSplashScreen === true || autoHideSplashScreen.toLowerCase() === 'true');
|
||||
} catch(e) {
|
||||
var msg = '[Browser][SplashScreen] Error occurred on loading preferences from config.xml: ' + JSON.stringify(e);
|
||||
console.error(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows and hides splashscreen if it is enabled, with a delay according the current preferences.
|
||||
*/
|
||||
function showAndHide() {
|
||||
if(showSplashScreen) {
|
||||
SplashScreen.show();
|
||||
|
||||
window.setTimeout(function() {
|
||||
SplashScreen.hide();
|
||||
}, splashScreenDelay);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to read config.xml and override default properties and then shows and hides splashscreen if it is enabled.
|
||||
*/
|
||||
(function initAndShow() {
|
||||
configHelper.readConfig(function(config) {
|
||||
readPreferencesFromCfg(config);
|
||||
if (autoHideSplashScreen) {
|
||||
showAndHide();
|
||||
} else {
|
||||
SplashScreen.show();
|
||||
}
|
||||
|
||||
}, function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
})();
|
||||
|
||||
module.exports = SplashScreen;
|
||||
|
||||
require("cordova/exec/proxy").add("SplashScreen", SplashScreen);
|
||||
|
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you 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.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <Cordova/CDVPlugin.h>
|
||||
|
||||
typedef struct {
|
||||
BOOL iPhone;
|
||||
BOOL iPad;
|
||||
BOOL iPhone4;
|
||||
BOOL iPhone5;
|
||||
BOOL iPhone6;
|
||||
BOOL iPhone6Plus;
|
||||
BOOL retina;
|
||||
BOOL iPhoneX;
|
||||
|
||||
} CDV_iOSDevice;
|
||||
|
||||
@interface CDVSplashScreen : CDVPlugin {
|
||||
UIActivityIndicatorView* _activityView;
|
||||
UIImageView* _imageView;
|
||||
NSString* _curImageName;
|
||||
BOOL _visible;
|
||||
BOOL _destroyed;
|
||||
}
|
||||
|
||||
- (void)show:(CDVInvokedUrlCommand*)command;
|
||||
- (void)hide:(CDVInvokedUrlCommand*)command;
|
||||
|
||||
@end
|
514
plugins/cordova-plugin-splashscreen/src/ios/CDVSplashScreen.m
Normal file
514
plugins/cordova-plugin-splashscreen/src/ios/CDVSplashScreen.m
Normal file
@@ -0,0 +1,514 @@
|
||||
/*
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you 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.
|
||||
*/
|
||||
|
||||
#import "CDVSplashScreen.h"
|
||||
#import <Cordova/CDVViewController.h>
|
||||
#import <Cordova/CDVScreenOrientationDelegate.h>
|
||||
#import "CDVViewController+SplashScreen.h"
|
||||
|
||||
#define kSplashScreenDurationDefault 3000.0f
|
||||
#define kFadeDurationDefault 500.0f
|
||||
|
||||
|
||||
@implementation CDVSplashScreen
|
||||
|
||||
- (void)pluginInitialize
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageDidLoad) name:CDVPageDidLoadNotification object:nil];
|
||||
|
||||
[self setVisible:YES];
|
||||
}
|
||||
|
||||
- (void)show:(CDVInvokedUrlCommand*)command
|
||||
{
|
||||
[self setVisible:YES];
|
||||
}
|
||||
|
||||
- (void)hide:(CDVInvokedUrlCommand*)command
|
||||
{
|
||||
[self setVisible:NO andForce:YES];
|
||||
}
|
||||
|
||||
- (void)pageDidLoad
|
||||
{
|
||||
id autoHideSplashScreenValue = [self.commandDelegate.settings objectForKey:[@"AutoHideSplashScreen" lowercaseString]];
|
||||
|
||||
// if value is missing, default to yes
|
||||
if ((autoHideSplashScreenValue == nil) || [autoHideSplashScreenValue boolValue]) {
|
||||
[self setVisible:NO];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
|
||||
{
|
||||
[self updateImage];
|
||||
}
|
||||
|
||||
- (void)createViews
|
||||
{
|
||||
/*
|
||||
* The Activity View is the top spinning throbber in the status/battery bar. We init it with the default Grey Style.
|
||||
*
|
||||
* whiteLarge = UIActivityIndicatorViewStyleWhiteLarge
|
||||
* white = UIActivityIndicatorViewStyleWhite
|
||||
* gray = UIActivityIndicatorViewStyleGray
|
||||
*
|
||||
*/
|
||||
|
||||
// Determine whether rotation should be enabled for this device
|
||||
// Per iOS HIG, landscape is only supported on iPad and iPhone 6+
|
||||
CDV_iOSDevice device = [self getCurrentDevice];
|
||||
BOOL autorotateValue = (device.iPad || device.iPhone6Plus || device.iPhoneX) ?
|
||||
[(CDVViewController *)self.viewController shouldAutorotateDefaultValue] :
|
||||
NO;
|
||||
|
||||
[(CDVViewController *)self.viewController setEnabledAutorotation:autorotateValue];
|
||||
|
||||
NSString* topActivityIndicator = [self.commandDelegate.settings objectForKey:[@"TopActivityIndicator" lowercaseString]];
|
||||
UIActivityIndicatorViewStyle topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
|
||||
|
||||
if ([topActivityIndicator isEqualToString:@"whiteLarge"])
|
||||
{
|
||||
topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhiteLarge;
|
||||
}
|
||||
else if ([topActivityIndicator isEqualToString:@"white"])
|
||||
{
|
||||
topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhite;
|
||||
}
|
||||
else if ([topActivityIndicator isEqualToString:@"gray"])
|
||||
{
|
||||
topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
|
||||
}
|
||||
|
||||
UIView* parentView = self.viewController.view;
|
||||
parentView.userInteractionEnabled = NO; // disable user interaction while splashscreen is shown
|
||||
_activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:topActivityIndicatorStyle];
|
||||
_activityView.center = CGPointMake(parentView.bounds.size.width / 2, parentView.bounds.size.height / 2);
|
||||
_activityView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin
|
||||
| UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
|
||||
[_activityView startAnimating];
|
||||
|
||||
// Set the frame & image later.
|
||||
_imageView = [[UIImageView alloc] init];
|
||||
[parentView addSubview:_imageView];
|
||||
|
||||
id showSplashScreenSpinnerValue = [self.commandDelegate.settings objectForKey:[@"ShowSplashScreenSpinner" lowercaseString]];
|
||||
// backwards compatibility - if key is missing, default to true
|
||||
if ((showSplashScreenSpinnerValue == nil) || [showSplashScreenSpinnerValue boolValue])
|
||||
{
|
||||
[parentView addSubview:_activityView];
|
||||
}
|
||||
|
||||
// Frame is required when launching in portrait mode.
|
||||
// Bounds for landscape since it captures the rotation.
|
||||
[parentView addObserver:self forKeyPath:@"frame" options:0 context:nil];
|
||||
[parentView addObserver:self forKeyPath:@"bounds" options:0 context:nil];
|
||||
|
||||
[self updateImage];
|
||||
_destroyed = NO;
|
||||
}
|
||||
|
||||
- (void)hideViews
|
||||
{
|
||||
[_imageView setAlpha:0];
|
||||
[_activityView setAlpha:0];
|
||||
}
|
||||
|
||||
- (void)destroyViews
|
||||
{
|
||||
_destroyed = YES;
|
||||
[(CDVViewController *)self.viewController setEnabledAutorotation:[(CDVViewController *)self.viewController shouldAutorotateDefaultValue]];
|
||||
|
||||
[_imageView removeFromSuperview];
|
||||
[_activityView removeFromSuperview];
|
||||
_imageView = nil;
|
||||
_activityView = nil;
|
||||
_curImageName = nil;
|
||||
|
||||
self.viewController.view.userInteractionEnabled = YES; // re-enable user interaction upon completion
|
||||
@try {
|
||||
[self.viewController.view removeObserver:self forKeyPath:@"frame"];
|
||||
[self.viewController.view removeObserver:self forKeyPath:@"bounds"];
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
// When reloading the page from a remotely connected Safari, there
|
||||
// are no observers, so the removeObserver method throws an exception,
|
||||
// that we can safely ignore.
|
||||
// Alternatively we can check whether there are observers before calling removeObserver
|
||||
}
|
||||
}
|
||||
|
||||
- (CDV_iOSDevice) getCurrentDevice
|
||||
{
|
||||
CDV_iOSDevice device;
|
||||
|
||||
UIScreen* mainScreen = [UIScreen mainScreen];
|
||||
CGFloat mainScreenHeight = mainScreen.bounds.size.height;
|
||||
CGFloat mainScreenWidth = mainScreen.bounds.size.width;
|
||||
|
||||
int limit = MAX(mainScreenHeight,mainScreenWidth);
|
||||
|
||||
device.iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
|
||||
device.iPhone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
|
||||
device.retina = ([mainScreen scale] == 2.0);
|
||||
device.iPhone4 = (device.iPhone && limit == 480.0);
|
||||
device.iPhone5 = (device.iPhone && limit == 568.0);
|
||||
// note these below is not a true device detect, for example if you are on an
|
||||
// iPhone 6/6+ but the app is scaled it will prob set iPhone5 as true, but
|
||||
// this is appropriate for detecting the runtime screen environment
|
||||
device.iPhone6 = (device.iPhone && limit == 667.0);
|
||||
device.iPhone6Plus = (device.iPhone && limit == 736.0);
|
||||
device.iPhoneX = (device.iPhone && limit == 812.0);
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
- (BOOL) isUsingCDVLaunchScreen {
|
||||
NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
|
||||
if (launchStoryboardName) {
|
||||
return ([launchStoryboardName isEqualToString:@"CDVLaunchScreen"]);
|
||||
} else {
|
||||
return NO;
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString*)getImageName:(UIInterfaceOrientation)currentOrientation delegate:(id<CDVScreenOrientationDelegate>)orientationDelegate device:(CDV_iOSDevice)device
|
||||
{
|
||||
// Use UILaunchImageFile if specified in plist. Otherwise, use Default.
|
||||
NSString* imageName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchImageFile"];
|
||||
|
||||
// detect if we are using CB-9762 Launch Storyboard; if so, return the associated image instead
|
||||
if ([self isUsingCDVLaunchScreen]) {
|
||||
imageName = @"LaunchStoryboard";
|
||||
return imageName;
|
||||
}
|
||||
|
||||
NSUInteger supportedOrientations = [orientationDelegate supportedInterfaceOrientations];
|
||||
|
||||
// Checks to see if the developer has locked the orientation to use only one of Portrait or Landscape
|
||||
BOOL supportsLandscape = (supportedOrientations & UIInterfaceOrientationMaskLandscape);
|
||||
BOOL supportsPortrait = (supportedOrientations & UIInterfaceOrientationMaskPortrait || supportedOrientations & UIInterfaceOrientationMaskPortraitUpsideDown);
|
||||
// this means there are no mixed orientations in there
|
||||
BOOL isOrientationLocked = !(supportsPortrait && supportsLandscape);
|
||||
|
||||
if (imageName)
|
||||
{
|
||||
imageName = [imageName stringByDeletingPathExtension];
|
||||
}
|
||||
else
|
||||
{
|
||||
imageName = @"Default";
|
||||
}
|
||||
|
||||
// Add Asset Catalog specific prefixes
|
||||
if ([imageName isEqualToString:@"LaunchImage"])
|
||||
{
|
||||
if (device.iPhone4 || device.iPhone5 || device.iPad) {
|
||||
imageName = [imageName stringByAppendingString:@"-700"];
|
||||
} else if(device.iPhone6) {
|
||||
imageName = [imageName stringByAppendingString:@"-800"];
|
||||
} else if(device.iPhone6Plus || device.iPhoneX ) {
|
||||
if(device.iPhone6Plus) {
|
||||
imageName = [imageName stringByAppendingString:@"-800"];
|
||||
} else {
|
||||
imageName = [imageName stringByAppendingString:@"-1100"];
|
||||
}
|
||||
if (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown)
|
||||
{
|
||||
imageName = [imageName stringByAppendingString:@"-Portrait"];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (device.iPhone5)
|
||||
{ // does not support landscape
|
||||
imageName = [imageName stringByAppendingString:@"-568h"];
|
||||
}
|
||||
else if (device.iPhone6)
|
||||
{ // does not support landscape
|
||||
imageName = [imageName stringByAppendingString:@"-667h"];
|
||||
}
|
||||
else if (device.iPhone6Plus || device.iPhoneX)
|
||||
{ // supports landscape
|
||||
if (isOrientationLocked)
|
||||
{
|
||||
imageName = [imageName stringByAppendingString:(supportsLandscape ? @"-Landscape" : @"")];
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (currentOrientation)
|
||||
{
|
||||
case UIInterfaceOrientationLandscapeLeft:
|
||||
case UIInterfaceOrientationLandscapeRight:
|
||||
imageName = [imageName stringByAppendingString:@"-Landscape"];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (device.iPhoneX) {
|
||||
imageName = [imageName stringByAppendingString:@"-2436h"];
|
||||
} else {
|
||||
imageName = [imageName stringByAppendingString:@"-736h"];
|
||||
}
|
||||
}
|
||||
else if (device.iPad)
|
||||
{ // supports landscape
|
||||
if (isOrientationLocked)
|
||||
{
|
||||
imageName = [imageName stringByAppendingString:(supportsLandscape ? @"-Landscape" : @"-Portrait")];
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (currentOrientation)
|
||||
{
|
||||
case UIInterfaceOrientationLandscapeLeft:
|
||||
case UIInterfaceOrientationLandscapeRight:
|
||||
imageName = [imageName stringByAppendingString:@"-Landscape"];
|
||||
break;
|
||||
|
||||
case UIInterfaceOrientationPortrait:
|
||||
case UIInterfaceOrientationPortraitUpsideDown:
|
||||
default:
|
||||
imageName = [imageName stringByAppendingString:@"-Portrait"];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return imageName;
|
||||
}
|
||||
|
||||
- (UIInterfaceOrientation)getCurrentOrientation
|
||||
{
|
||||
UIInterfaceOrientation iOrientation = [UIApplication sharedApplication].statusBarOrientation;
|
||||
UIDeviceOrientation dOrientation = [UIDevice currentDevice].orientation;
|
||||
|
||||
bool landscape;
|
||||
|
||||
if (dOrientation == UIDeviceOrientationUnknown || dOrientation == UIDeviceOrientationFaceUp || dOrientation == UIDeviceOrientationFaceDown) {
|
||||
// If the device is laying down, use the UIInterfaceOrientation based on the status bar.
|
||||
landscape = UIInterfaceOrientationIsLandscape(iOrientation);
|
||||
} else {
|
||||
// If the device is not laying down, use UIDeviceOrientation.
|
||||
landscape = UIDeviceOrientationIsLandscape(dOrientation);
|
||||
|
||||
// There's a bug in iOS!!!! http://openradar.appspot.com/7216046
|
||||
// So values needs to be reversed for landscape!
|
||||
if (dOrientation == UIDeviceOrientationLandscapeLeft)
|
||||
{
|
||||
iOrientation = UIInterfaceOrientationLandscapeRight;
|
||||
}
|
||||
else if (dOrientation == UIDeviceOrientationLandscapeRight)
|
||||
{
|
||||
iOrientation = UIInterfaceOrientationLandscapeLeft;
|
||||
}
|
||||
else if (dOrientation == UIDeviceOrientationPortrait)
|
||||
{
|
||||
iOrientation = UIInterfaceOrientationPortrait;
|
||||
}
|
||||
else if (dOrientation == UIDeviceOrientationPortraitUpsideDown)
|
||||
{
|
||||
iOrientation = UIInterfaceOrientationPortraitUpsideDown;
|
||||
}
|
||||
}
|
||||
|
||||
return iOrientation;
|
||||
}
|
||||
|
||||
// Sets the view's frame and image.
|
||||
- (void)updateImage
|
||||
{
|
||||
NSString* imageName = [self getImageName:[self getCurrentOrientation] delegate:(id<CDVScreenOrientationDelegate>)self.viewController device:[self getCurrentDevice]];
|
||||
|
||||
if (![imageName isEqualToString:_curImageName])
|
||||
{
|
||||
UIImage* img = [UIImage imageNamed:imageName];
|
||||
_imageView.image = img;
|
||||
_curImageName = imageName;
|
||||
}
|
||||
|
||||
// Check that splash screen's image exists before updating bounds
|
||||
if (_imageView.image)
|
||||
{
|
||||
[self updateBounds];
|
||||
}
|
||||
else
|
||||
{
|
||||
NSLog(@"WARNING: The splashscreen image named %@ was not found", imageName);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)updateBounds
|
||||
{
|
||||
if ([self isUsingCDVLaunchScreen]) {
|
||||
// CB-9762's launch screen expects the image to fill the screen and be scaled using AspectFill.
|
||||
CGSize viewportSize = [UIApplication sharedApplication].delegate.window.bounds.size;
|
||||
_imageView.frame = CGRectMake(0, 0, viewportSize.width, viewportSize.height);
|
||||
_imageView.contentMode = UIViewContentModeScaleAspectFill;
|
||||
return;
|
||||
}
|
||||
|
||||
UIImage* img = _imageView.image;
|
||||
CGRect imgBounds = (img) ? CGRectMake(0, 0, img.size.width, img.size.height) : CGRectZero;
|
||||
|
||||
CGSize screenSize = [self.viewController.view convertRect:[UIScreen mainScreen].bounds fromView:nil].size;
|
||||
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
|
||||
CGAffineTransform imgTransform = CGAffineTransformIdentity;
|
||||
|
||||
/* If and only if an iPhone application is landscape-only as per
|
||||
* UISupportedInterfaceOrientations, the view controller's orientation is
|
||||
* landscape. In this case the image must be rotated in order to appear
|
||||
* correctly.
|
||||
*/
|
||||
CDV_iOSDevice device = [self getCurrentDevice];
|
||||
if (UIInterfaceOrientationIsLandscape(orientation) && !device.iPhone6Plus && !device.iPad && !device.iPhoneX)
|
||||
{
|
||||
imgTransform = CGAffineTransformMakeRotation(M_PI / 2);
|
||||
imgBounds.size = CGSizeMake(imgBounds.size.height, imgBounds.size.width);
|
||||
}
|
||||
|
||||
// There's a special case when the image is the size of the screen.
|
||||
if (CGSizeEqualToSize(screenSize, imgBounds.size))
|
||||
{
|
||||
CGRect statusFrame = [self.viewController.view convertRect:[UIApplication sharedApplication].statusBarFrame fromView:nil];
|
||||
if (!(IsAtLeastiOSVersion(@"7.0")))
|
||||
{
|
||||
imgBounds.origin.y -= statusFrame.size.height;
|
||||
}
|
||||
}
|
||||
else if (imgBounds.size.width > 0)
|
||||
{
|
||||
CGRect viewBounds = self.viewController.view.bounds;
|
||||
CGFloat imgAspect = imgBounds.size.width / imgBounds.size.height;
|
||||
CGFloat viewAspect = viewBounds.size.width / viewBounds.size.height;
|
||||
// This matches the behaviour of the native splash screen.
|
||||
CGFloat ratio;
|
||||
if (viewAspect > imgAspect)
|
||||
{
|
||||
ratio = viewBounds.size.width / imgBounds.size.width;
|
||||
}
|
||||
else
|
||||
{
|
||||
ratio = viewBounds.size.height / imgBounds.size.height;
|
||||
}
|
||||
imgBounds.size.height *= ratio;
|
||||
imgBounds.size.width *= ratio;
|
||||
}
|
||||
|
||||
_imageView.transform = imgTransform;
|
||||
_imageView.frame = imgBounds;
|
||||
}
|
||||
|
||||
- (void)setVisible:(BOOL)visible
|
||||
{
|
||||
[self setVisible:visible andForce:NO];
|
||||
}
|
||||
|
||||
- (void)setVisible:(BOOL)visible andForce:(BOOL)force
|
||||
{
|
||||
if (visible != _visible || force)
|
||||
{
|
||||
_visible = visible;
|
||||
|
||||
id fadeSplashScreenValue = [self.commandDelegate.settings objectForKey:[@"FadeSplashScreen" lowercaseString]];
|
||||
id fadeSplashScreenDuration = [self.commandDelegate.settings objectForKey:[@"FadeSplashScreenDuration" lowercaseString]];
|
||||
|
||||
float fadeDuration = fadeSplashScreenDuration == nil ? kFadeDurationDefault : [fadeSplashScreenDuration floatValue];
|
||||
|
||||
id splashDurationString = [self.commandDelegate.settings objectForKey: [@"SplashScreenDelay" lowercaseString]];
|
||||
float splashDuration = splashDurationString == nil ? kSplashScreenDurationDefault : [splashDurationString floatValue];
|
||||
|
||||
id autoHideSplashScreenValue = [self.commandDelegate.settings objectForKey:[@"AutoHideSplashScreen" lowercaseString]];
|
||||
BOOL autoHideSplashScreen = true;
|
||||
|
||||
if (autoHideSplashScreenValue != nil) {
|
||||
autoHideSplashScreen = [autoHideSplashScreenValue boolValue];
|
||||
}
|
||||
|
||||
if (!autoHideSplashScreen) {
|
||||
// CB-10412 SplashScreenDelay does not make sense if the splashscreen is hidden manually
|
||||
splashDuration = 0;
|
||||
}
|
||||
|
||||
|
||||
if (fadeSplashScreenValue == nil)
|
||||
{
|
||||
fadeSplashScreenValue = @"true";
|
||||
}
|
||||
|
||||
if (![fadeSplashScreenValue boolValue])
|
||||
{
|
||||
fadeDuration = 0;
|
||||
}
|
||||
else if (fadeDuration < 30)
|
||||
{
|
||||
// [CB-9750] This value used to be in decimal seconds, so we will assume that if someone specifies 10
|
||||
// they mean 10 seconds, and not the meaningless 10ms
|
||||
fadeDuration *= 1000;
|
||||
}
|
||||
|
||||
if (_visible)
|
||||
{
|
||||
if (_imageView == nil)
|
||||
{
|
||||
[self createViews];
|
||||
}
|
||||
}
|
||||
else if (fadeDuration == 0 && splashDuration == 0)
|
||||
{
|
||||
[self destroyViews];
|
||||
}
|
||||
else
|
||||
{
|
||||
__weak __typeof(self) weakSelf = self;
|
||||
float effectiveSplashDuration;
|
||||
|
||||
// [CB-10562] AutoHideSplashScreen may be "true" but we should still be able to hide the splashscreen manually.
|
||||
if (!autoHideSplashScreen || force) {
|
||||
effectiveSplashDuration = (fadeDuration) / 1000;
|
||||
} else {
|
||||
effectiveSplashDuration = (splashDuration - fadeDuration) / 1000;
|
||||
}
|
||||
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (uint64_t) effectiveSplashDuration * NSEC_PER_SEC), dispatch_get_main_queue(), CFBridgingRelease(CFBridgingRetain(^(void) {
|
||||
if (!_destroyed) {
|
||||
[UIView transitionWithView:self.viewController.view
|
||||
duration:(fadeDuration / 1000)
|
||||
options:UIViewAnimationOptionTransitionNone
|
||||
animations:^(void) {
|
||||
[weakSelf hideViews];
|
||||
}
|
||||
completion:^(BOOL finished) {
|
||||
// Always destroy views, otherwise you could have an
|
||||
// invisible splashscreen that is overlayed over your active views
|
||||
// which causes that no touch events are passed
|
||||
if (!_destroyed) {
|
||||
[weakSelf destroyViews];
|
||||
// TODO: It might also be nice to have a js event happen here -jm
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
})));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you 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.
|
||||
*/
|
||||
|
||||
#import <Cordova/CDVViewController.h>
|
||||
|
||||
@interface CDVViewController (SplashScreen)
|
||||
|
||||
@property (nonatomic, assign) BOOL enabledAutorotation;
|
||||
@property (nonatomic, readonly) BOOL shouldAutorotateDefaultValue;
|
||||
|
||||
|
||||
@end
|
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you 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.
|
||||
*/
|
||||
|
||||
#import "CDVViewController+SplashScreen.h"
|
||||
#import <objc/runtime.h>
|
||||
|
||||
@implementation CDVViewController (SplashScreen)
|
||||
|
||||
@dynamic enabledAutorotation;
|
||||
|
||||
- (void)setEnabledAutorotation:(BOOL)value
|
||||
{
|
||||
objc_setAssociatedObject(self,
|
||||
@selector(enabledAutorotation),
|
||||
[NSNumber numberWithBool:value],
|
||||
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
- (BOOL)enabledAutorotation
|
||||
{
|
||||
NSNumber *number = (NSNumber *)objc_getAssociatedObject(self, @selector(enabledAutorotation));
|
||||
|
||||
// Defaulting to YES to correspond parent CDVViewController behavior
|
||||
if (number == nil)
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
return [number boolValue];
|
||||
}
|
||||
|
||||
+ (void)load
|
||||
{
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
Class class = [self class];
|
||||
|
||||
SEL originalSelector = @selector(shouldAutorotate);
|
||||
SEL swizzledSelector = @selector(splash_shouldAutorotate);
|
||||
|
||||
Method originalMethod = class_getInstanceMethod(class, originalSelector);
|
||||
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
|
||||
|
||||
BOOL didAddMethod = class_addMethod(class,
|
||||
originalSelector,
|
||||
method_getImplementation(swizzledMethod),
|
||||
method_getTypeEncoding(swizzledMethod));
|
||||
|
||||
if (didAddMethod) {
|
||||
class_replaceMethod(class,
|
||||
swizzledSelector,
|
||||
method_getImplementation(originalMethod),
|
||||
method_getTypeEncoding(originalMethod));
|
||||
} else {
|
||||
method_exchangeImplementations(originalMethod, swizzledMethod);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark - Method Swizzling
|
||||
|
||||
- (BOOL)splash_shouldAutorotate
|
||||
{
|
||||
return self.enabledAutorotation;
|
||||
}
|
||||
|
||||
|
||||
- (BOOL)shouldAutorotateDefaultValue
|
||||
{
|
||||
return [self splash_shouldAutorotate];
|
||||
}
|
||||
|
||||
@end
|
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
using Microsoft.Phone.Info;
|
||||
using System;
|
||||
using System.Windows;
|
||||
|
||||
namespace WPCordovaClassLib.Cordova.Commands
|
||||
{
|
||||
public enum Resolutions { WVGA, WXGA, HD };
|
||||
|
||||
public static class ResolutionHelper
|
||||
{
|
||||
public static Resolutions CurrentResolution
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (Application.Current.Host.Content.ScaleFactor)
|
||||
{
|
||||
case 100: return Resolutions.WVGA;
|
||||
case 160: return Resolutions.WXGA;
|
||||
case 150: return Resolutions.HD;
|
||||
}
|
||||
throw new InvalidOperationException("Unknown resolution");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
255
plugins/cordova-plugin-splashscreen/src/wp/SplashScreen.cs
Normal file
255
plugins/cordova-plugin-splashscreen/src/wp/SplashScreen.cs
Normal file
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Ink;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Animation;
|
||||
using System.Windows.Shapes;
|
||||
using Microsoft.Phone.Info;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Diagnostics;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Resources;
|
||||
using System.IO;
|
||||
using System.Xml.Linq;
|
||||
using System.Linq;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace WPCordovaClassLib.Cordova.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Listens for changes to the state of the battery on the device.
|
||||
/// Currently only the "isPlugged" parameter available via native APIs.
|
||||
/// </summary>
|
||||
public class SplashScreen : BaseCommand
|
||||
{
|
||||
private Popup popup;
|
||||
|
||||
// Time until we dismiss the splashscreen
|
||||
private int prefDelay = 3000;
|
||||
|
||||
// Whether we hide it by default
|
||||
private bool prefAutoHide = true;
|
||||
|
||||
// Path to image to use
|
||||
private string prefImagePath = "SplashScreenImage.jpg";
|
||||
|
||||
// static because autodismiss is only ever applied once, at app launch
|
||||
// subsequent page loads should not cause the SplashScreen to be shown.
|
||||
private static bool WasShown = false;
|
||||
|
||||
public SplashScreen()
|
||||
{
|
||||
LoadConfigPrefs();
|
||||
|
||||
Image SplashScreen = new Image()
|
||||
{
|
||||
Height = Application.Current.Host.Content.ActualHeight,
|
||||
Width = Application.Current.Host.Content.ActualWidth,
|
||||
Stretch = Stretch.Fill
|
||||
};
|
||||
|
||||
var imageResource = GetSplashScreenImageResource();
|
||||
if (imageResource != null)
|
||||
{
|
||||
BitmapImage splash_image = new BitmapImage();
|
||||
splash_image.SetSource(imageResource.Stream);
|
||||
SplashScreen.Source = splash_image;
|
||||
}
|
||||
|
||||
// Instansiate the popup and set the Child property of Popup to SplashScreen
|
||||
popup = new Popup() { IsOpen = false,
|
||||
Child = SplashScreen,
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch,
|
||||
VerticalAlignment = VerticalAlignment.Center
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public override void OnInit()
|
||||
{
|
||||
// we only want to autoload on the first page load.
|
||||
// but OnInit is called for every page load.
|
||||
if (!SplashScreen.WasShown)
|
||||
{
|
||||
SplashScreen.WasShown = true;
|
||||
show();
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadConfigPrefs()
|
||||
{
|
||||
StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));
|
||||
if (streamInfo != null)
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(streamInfo.Stream))
|
||||
{
|
||||
//This will Read Keys Collection for the xml file
|
||||
XDocument configFile = XDocument.Parse(sr.ReadToEnd());
|
||||
|
||||
string configAutoHide = configFile.Descendants()
|
||||
.Where(x => x.Name.LocalName == "preference")
|
||||
.Where(x => (string)x.Attribute("name") == "AutoHideSplashScreen")
|
||||
.Select(x => (string)x.Attribute("value"))
|
||||
.FirstOrDefault();
|
||||
|
||||
bool bVal;
|
||||
prefAutoHide = bool.TryParse(configAutoHide, out bVal) ? bVal : prefAutoHide;
|
||||
|
||||
string configDelay = configFile.Descendants()
|
||||
.Where(x => x.Name.LocalName == "preference")
|
||||
.Where(x => (string)x.Attribute("name") == "SplashScreenDelay")
|
||||
.Select(x => (string)x.Attribute("value"))
|
||||
.FirstOrDefault();
|
||||
int nVal;
|
||||
prefDelay = int.TryParse(configDelay, out nVal) ? nVal : prefDelay;
|
||||
|
||||
string configImage = configFile.Descendants()
|
||||
.Where(x => x.Name.LocalName == "preference")
|
||||
.Where(x => (string)x.Attribute("name") == "SplashScreen")
|
||||
.Select(x => (string)x.Attribute("value"))
|
||||
.FirstOrDefault();
|
||||
|
||||
if (!String.IsNullOrEmpty(configImage))
|
||||
{
|
||||
prefImagePath = configImage;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private StreamResourceInfo GetSplashScreenImageResource()
|
||||
{
|
||||
// Get the base filename for the splash screen images
|
||||
string imageName = System.IO.Path.GetFileNameWithoutExtension(prefImagePath);
|
||||
Uri imageUri = null;
|
||||
StreamResourceInfo imageResource = null;
|
||||
|
||||
// First, try to get a resolution-specific splashscreen
|
||||
try
|
||||
{
|
||||
// Determine the device's resolution
|
||||
switch (ResolutionHelper.CurrentResolution)
|
||||
{
|
||||
case Resolutions.HD:
|
||||
imageUri = new Uri(imageName + ".screen-720p.jpg", UriKind.Relative);
|
||||
break;
|
||||
|
||||
case Resolutions.WVGA:
|
||||
imageUri = new Uri(imageName + ".screen-WVGA.jpg", UriKind.Relative);
|
||||
break;
|
||||
|
||||
case Resolutions.WXGA:
|
||||
default:
|
||||
imageUri = new Uri(imageName + ".screen-WXGA.jpg", UriKind.Relative);
|
||||
break;
|
||||
}
|
||||
|
||||
imageResource = Application.GetResourceStream(imageUri);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// It's OK if we didn't get a resolution-specific image
|
||||
}
|
||||
|
||||
// Fallback to the default image name without decoration
|
||||
if (imageResource == null)
|
||||
{
|
||||
imageUri = new Uri(prefImagePath, UriKind.Relative);
|
||||
imageResource = Application.GetResourceStream(imageUri);
|
||||
}
|
||||
|
||||
if (imageUri != null) Debug.WriteLine("INFO :: SplashScreen: using image {0}", imageUri.OriginalString);
|
||||
|
||||
return imageResource;
|
||||
}
|
||||
|
||||
public void show(string options = null)
|
||||
{
|
||||
Deployment.Current.Dispatcher.BeginInvoke(() =>
|
||||
{
|
||||
if (!popup.IsOpen)
|
||||
{
|
||||
popup.Child.Opacity = 0;
|
||||
|
||||
Storyboard story = new Storyboard();
|
||||
DoubleAnimation animation = new DoubleAnimation()
|
||||
{
|
||||
From = 0.0,
|
||||
To = 1.0,
|
||||
Duration = new Duration(TimeSpan.FromSeconds(0.2))
|
||||
};
|
||||
|
||||
Storyboard.SetTarget(animation, popup.Child);
|
||||
Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
|
||||
story.Children.Add(animation);
|
||||
|
||||
story.Begin();
|
||||
|
||||
popup.IsOpen = true;
|
||||
|
||||
if (prefAutoHide)
|
||||
{
|
||||
StartAutoHideTimer();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void hide(string options = null)
|
||||
{
|
||||
Deployment.Current.Dispatcher.BeginInvoke(() =>
|
||||
{
|
||||
if (popup.IsOpen)
|
||||
{
|
||||
popup.Child.Opacity = 1.0;
|
||||
|
||||
Storyboard story = new Storyboard();
|
||||
DoubleAnimation animation = new DoubleAnimation()
|
||||
{
|
||||
From = 1.0,
|
||||
To = 0.0,
|
||||
Duration = new Duration(TimeSpan.FromSeconds(0.4))
|
||||
};
|
||||
|
||||
Storyboard.SetTarget(animation, popup.Child);
|
||||
Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
|
||||
story.Children.Add(animation);
|
||||
story.Completed += (object sender, EventArgs e) =>
|
||||
{
|
||||
popup.IsOpen = false;
|
||||
};
|
||||
story.Begin();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void StartAutoHideTimer()
|
||||
{
|
||||
var timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(prefDelay) };
|
||||
timer.Tick += (object sender, EventArgs e) =>
|
||||
{
|
||||
hide();
|
||||
timer.Stop();
|
||||
};
|
||||
timer.Start();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user