2013-04-03 10:47:26 -07:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2013-07-31 19:57:24 -04:00
|
|
|
package org.apache.cordova.splashscreen;
|
2013-04-03 10:47:26 -07:00
|
|
|
|
2014-11-17 23:48:45 -08:00
|
|
|
import android.app.Dialog;
|
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.view.Display;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.WindowManager;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
2013-07-10 09:31:32 -04:00
|
|
|
import org.apache.cordova.CallbackContext;
|
|
|
|
import org.apache.cordova.CordovaPlugin;
|
2014-11-17 23:48:45 -08:00
|
|
|
import org.apache.cordova.CordovaWebView;
|
2013-04-03 10:47:26 -07:00
|
|
|
import org.json.JSONArray;
|
2014-11-17 23:48:45 -08:00
|
|
|
import org.json.JSONException;
|
2013-04-03 10:47:26 -07:00
|
|
|
|
|
|
|
public class SplashScreen extends CordovaPlugin {
|
2014-11-17 23:48:45 -08:00
|
|
|
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 Dialog splashDialog;
|
|
|
|
private static ProgressDialog spinnerDialog;
|
|
|
|
private static boolean firstShow = true;
|
2013-04-03 10:47:26 -07:00
|
|
|
|
2014-12-10 16:09:54 -05:00
|
|
|
// Helper to be compile-time compatable 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 10:47:26 -07:00
|
|
|
@Override
|
2014-11-17 23:48:45 -08:00
|
|
|
protected void pluginInitialize() {
|
|
|
|
if (HAS_BUILT_IN_SPLASH_SCREEN || !firstShow) {
|
|
|
|
return;
|
2013-04-03 10:47:26 -07:00
|
|
|
}
|
2014-11-17 23:48:45 -08:00
|
|
|
// Make WebView invisible while loading URL
|
2014-12-10 16:09:54 -05:00
|
|
|
getView().setVisibility(View.INVISIBLE);
|
2014-11-17 23:48:45 -08:00
|
|
|
int drawableId = preferences.getInteger("SplashDrawableId", 0);
|
|
|
|
if (drawableId == 0) {
|
|
|
|
String splashResource = preferences.getString("SplashScreen", null);
|
|
|
|
if (splashResource != null) {
|
|
|
|
drawableId = cordova.getActivity().getResources().getIdentifier(splashResource, "drawable", cordova.getActivity().getClass().getPackage().getName());
|
|
|
|
preferences.set("SplashDrawableId", drawableId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
firstShow = false;
|
|
|
|
loadSpinner();
|
|
|
|
showSplashScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause(boolean multitasking) {
|
|
|
|
if (HAS_BUILT_IN_SPLASH_SCREEN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// hide the splash screen to avoid leaking a window
|
|
|
|
this.removeSplashScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
if (HAS_BUILT_IN_SPLASH_SCREEN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// hide the splash screen to avoid leaking a window
|
|
|
|
this.removeSplashScreen();
|
|
|
|
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 if (action.equals("spinnerStart")) {
|
|
|
|
if (!HAS_BUILT_IN_SPLASH_SCREEN) {
|
|
|
|
final String title = args.getString(0);
|
|
|
|
final String message = args.getString(1);
|
|
|
|
cordova.getActivity().runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
spinnerStart(title, message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-03 10:47:26 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
callbackContext.success();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:48:45 -08:00
|
|
|
@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();
|
|
|
|
} else {
|
|
|
|
this.showSplashScreen();
|
|
|
|
}
|
|
|
|
} else if ("spinner".equals(id)) {
|
|
|
|
if ("stop".equals(data.toString())) {
|
|
|
|
this.spinnerStop();
|
2014-12-10 16:09:54 -05:00
|
|
|
getView().setVisibility(View.VISIBLE);
|
2014-11-17 23:48:45 -08:00
|
|
|
}
|
|
|
|
} else if ("onReceivedError".equals(id)) {
|
|
|
|
spinnerStop();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void removeSplashScreen() {
|
|
|
|
cordova.getActivity().runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
if (splashDialog != null && splashDialog.isShowing()) {
|
|
|
|
splashDialog.dismiss();
|
|
|
|
splashDialog = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the splash screen over the full Activity
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
private void showSplashScreen() {
|
|
|
|
final int splashscreenTime = preferences.getInteger("SplashScreenDelay", 3000);
|
|
|
|
final int drawableId = preferences.getInteger("SplashDrawableId", 0);
|
|
|
|
|
|
|
|
// If the splash dialog is showing don't try to show it again
|
|
|
|
if (this.splashDialog != null && splashDialog.isShowing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (drawableId == 0 || splashscreenTime <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cordova.getActivity().runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
// Get reference to display
|
|
|
|
Display display = cordova.getActivity().getWindowManager().getDefaultDisplay();
|
|
|
|
Context context = webView.getContext();
|
|
|
|
|
|
|
|
// Create the layout for the dialog
|
|
|
|
LinearLayout root = new LinearLayout(context);
|
|
|
|
root.setMinimumHeight(display.getHeight());
|
|
|
|
root.setMinimumWidth(display.getWidth());
|
|
|
|
root.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
|
|
|
|
// TODO: Use the background color of the webview's parent instead of using the
|
|
|
|
// preference.
|
|
|
|
root.setBackgroundColor(preferences.getInteger("backgroundColor", Color.BLACK));
|
|
|
|
root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT, 0.0F));
|
|
|
|
root.setBackgroundResource(drawableId);
|
|
|
|
|
|
|
|
// 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(root);
|
|
|
|
splashDialog.setCancelable(false);
|
|
|
|
splashDialog.show();
|
|
|
|
|
|
|
|
// Set Runnable to remove splash screen just in case
|
|
|
|
final Handler handler = new Handler();
|
|
|
|
handler.postDelayed(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
removeSplashScreen();
|
|
|
|
}
|
|
|
|
}, splashscreenTime);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the spinner
|
|
|
|
*/
|
|
|
|
private void loadSpinner() {
|
|
|
|
// If loadingDialog property, then show the App loading dialog for first page of app
|
|
|
|
String loading = null;
|
|
|
|
if (webView.canGoBack()) {
|
|
|
|
loading = preferences.getString("LoadingDialog", null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
loading = preferences.getString("LoadingPageDialog", null);
|
|
|
|
}
|
|
|
|
if (loading != null) {
|
|
|
|
String title = "";
|
|
|
|
String message = "Loading Application...";
|
|
|
|
|
|
|
|
if (loading.length() > 0) {
|
|
|
|
int comma = loading.indexOf(',');
|
|
|
|
if (comma > 0) {
|
|
|
|
title = loading.substring(0, comma);
|
|
|
|
message = loading.substring(comma + 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
title = "";
|
|
|
|
message = loading;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spinnerStart(title, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void spinnerStart(final String title, final String message) {
|
|
|
|
cordova.getActivity().runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
spinnerStop();
|
|
|
|
spinnerDialog = ProgressDialog.show(webView.getContext(), title, message, true, true,
|
|
|
|
new DialogInterface.OnCancelListener() {
|
|
|
|
public void onCancel(DialogInterface dialog) {
|
|
|
|
spinnerDialog = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void spinnerStop() {
|
|
|
|
cordova.getActivity().runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
if (spinnerDialog != null && spinnerDialog.isShowing()) {
|
|
|
|
spinnerDialog.dismiss();
|
|
|
|
spinnerDialog = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-04-03 10:47:26 -07:00
|
|
|
}
|