CB-8753 android: Adds SplashMaintainAspectRatio preference (close #43)

This commit is contained in:
Alex Talis
2015-03-27 11:33:01 -07:00
committed by Andrew Grieve
parent 5dc26d7e65
commit dd9c88204c
2 changed files with 66 additions and 16 deletions
+61 -16
View File
@@ -22,13 +22,14 @@ 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.graphics.Color;
import android.os.Handler;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import org.apache.cordova.CallbackContext;
@@ -46,7 +47,17 @@ public class SplashScreen extends CordovaPlugin {
private static ProgressDialog spinnerDialog;
private static boolean firstShow = true;
// Helper to be compile-time compatable with both Cordova 3.x and 4.x.
/**
* 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);
@@ -74,11 +85,21 @@ public class SplashScreen extends CordovaPlugin {
}
}
// Save initial orientation.
orientation = cordova.getActivity().getResources().getConfiguration().orientation;
firstShow = false;
loadSpinner();
showSplashScreen(true);
}
/**
* Shorter way to check value of "SplashMaintainAspectRatio" preference.
*/
private boolean isMaintainAspectRatio () {
return preferences.getBoolean("SplashMaintainAspectRatio", false);
}
@Override
public void onPause(boolean multitasking) {
if (HAS_BUILT_IN_SPLASH_SCREEN) {
@@ -152,12 +173,28 @@ public class SplashScreen extends CordovaPlugin {
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 = preferences.getInteger("SplashDrawableId", 0);
if (drawableId != 0) {
splashImageView.setImageDrawable(cordova.getActivity().getResources().getDrawable(drawableId));
}
}
}
}
private void removeSplashScreen() {
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
if (splashDialog != null && splashDialog.isShowing()) {
splashDialog.dismiss();
splashDialog = null;
splashImageView = null;
}
}
});
@@ -172,7 +209,7 @@ public class SplashScreen extends CordovaPlugin {
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()) {
if (splashDialog != null && splashDialog.isShowing()) {
return;
}
if (drawableId == 0 || (splashscreenTime <= 0 && hideAfterDelay)) {
@@ -185,18 +222,26 @@ public class SplashScreen extends CordovaPlugin {
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);
// 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);
// 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);
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);
@@ -206,7 +251,7 @@ public class SplashScreen extends CordovaPlugin {
splashDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
splashDialog.setContentView(root);
splashDialog.setContentView(splashImageView);
splashDialog.setCancelable(false);
splashDialog.show();