Skip to content

Instantly share code, notes, and snippets.

@embarq
Last active July 19, 2018 07:33
Show Gist options
  • Select an option

  • Save embarq/60d090f499fbc65b6fd4cacc6649629e to your computer and use it in GitHub Desktop.

Select an option

Save embarq/60d090f499fbc65b6fd4cacc6649629e to your computer and use it in GitHub Desktop.
/**
* PrivacyScreenPlugin.java Cordova Plugin Implementation
* Created by Tommy-Carlos Williams on 18/07/14.
* Copyright (c) 2014 Tommy-Carlos Williams. All rights reserved.
* MIT Licensed
*/
package org.devgeeks.privacyscreen;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.file.FileUtils;
import org.apache.cordova.LOG;
import android.app.Activity;
import android.media.Image;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.os.Bundle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import android.content.Intent;
import android.app.Dialog;
import android.net.Uri;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.view.Display;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.ViewGroup.LayoutParams;
import android.graphics.Color;
import com.google.android.gms.common.util.IOUtils;
import com.thetaris.saveupapp.develop.BuildConfig;
import com.thetaris.saveupapp.develop.MainActivity;
public class PrivacyScreenPlugin extends CordovaPlugin {
private Intent splashView;
private static final String LOG_TAG = "PrivacyScreenPlugin";
private ImageView splashImageView;
private Dialog splashDialog;
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, 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;
}
private void showSplash() {
final int drawableId = getSplashId();
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));
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();
}
/**
* Called when the system is about to start resuming a previous activity.
*
* @param multitasking Flag indicating if multitasking is turned on for app.
*/
@Override
public void onPause(boolean multitasking) {
this.showSplash();
this.webView.getView().setVisibility(View.INVISIBLE);
super.onPause(multitasking);
}
/**
* Called when the activity will start interacting with the user.
*
* @param multitasking Flag indicating if multitasking is turned on for app.
*/
@Override
public void onResume(boolean multitasking) {
if (this.splashDialog != null) {
this.splashDialog.dismiss();
this.splashDialog = null;
this.splashView = null;
}
this.webView.getView().setVisibility(View.VISIBLE);
super.onResume(multitasking);
}
}
/**
* PrivacyScreenPlugin.java Cordova Plugin Implementation
* Created by Tommy-Carlos Williams on 18/07/14.
* Copyright (c) 2014 Tommy-Carlos Williams. All rights reserved.
* MIT Licensed
*/
package org.devgeeks.privacyscreen;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.file.FileUtils;
import org.apache.cordova.LOG;
import android.app.Activity;
import android.media.Image;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.os.Bundle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import android.content.Intent;
import android.app.Dialog;
import android.net.Uri;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.view.Display;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.ViewGroup.LayoutParams;
import android.graphics.Color;
import com.google.android.gms.common.util.IOUtils;
import com.thetaris.saveupapp.develop.BuildConfig;
import com.thetaris.saveupapp.develop.MainActivity;
public class PrivacyScreenPlugin extends CordovaPlugin {
private Intent splashView;
private static final String LOG_TAG = "PrivacyScreenPlugin";
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
// try {
// this.splashView = this.getSplashView();
// } catch (Exception e) {
// Log.w(LOG_TAG, e.getMessage());
// }
}
//Not used in Android
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if ("setTimer".equals(action)) {
callbackContext.success();
return true;
}
else if ("hidePrivacyScreen".equals(action)) {
callbackContext.success();
return true;
}
else if ("showPrivacyScreen".equals(action)) {
callbackContext.success();
return true;
}
return false; // Returning false results in a "MethodNotFound" error.
}
public Intent getSplashView () throws Exception {
String url = preferences.getString("PrivacyAndroidImageName", "");
if (!url.isEmpty()) {
url = url + ".png";
} else {
throw new Exception("Missing PrivacyImageName!");
}
FileUtils fileUtils = new FileUtils();
Intent intent = new Intent(Intent.ACTION_VIEW);
try {
Uri path = Uri.parse(url);
File source = new File(path.getPath());
String filenameArray[] = url.split("\\.");
String extension = filenameArray[filenameArray.length-1];
if (!source.isFile()) {
// throw new IOException("Invalid path");
Log.d(LOG_TAG, "Not a file. Trying www/");
InputStream inputStream = null;
OutputStream outputStream = null;
File pPath = this.cordova.getActivity().getExternalCacheDir();
File f= new File(pPath, "output."+extension);
f.createNewFile();
inputStream = this.cordova.getActivity().getAssets().open("www/"+url);
outputStream =new FileOutputStream(f);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0)
outputStream.write(buf,0,len);
outputStream.close();
inputStream.close();
Log.d(LOG_TAG, f.getAbsolutePath());
path = FileProvider.getUriForFile(
this.cordova.getActivity(),
BuildConfig.APPLICATION_ID + ".provider",
f
);
}
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
// Convert the URI string to lower case to ensure compatibility with MimeTypeMap (see CB-2185).
intent.setDataAndType(path, MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase(Locale.getDefault())));
return intent;
} catch (IOException e) {
Log.d(LOG_TAG, "Could not create file: " + e.toString());
throw new IOException(e);
}
}
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;
}
private void showSplash() {
final int drawableId = getSplashId();
/*final int splashscreenTime = preferences.getInteger("SplashScreenDelay", DEFAULT_SPLASHSCREEN_DURATION);
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.
ImageView 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
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);
}*/
}
});
}
/**
* Called when the system is about to start resuming a previous activity.
*
* @param multitasking Flag indicating if multitasking is turned on for app.
*/
@Override
public void onPause(boolean multitasking) {
this.webView.getView().setVisibility(View.INVISIBLE);
this.showSplash();
super.onPause(multitasking);
// Log.d(LOG_TAG, "Pausing activity");
// try {
// this.splashView = this.getSplashView();
// this.cordova.getActivity().startActivity(this.splashView);
// } catch (Exception e) {
// Log.d(LOG_TAG, String.valueOf(this.splashView == null));
// Log.e(LOG_TAG, e.toString());
// }
}
/**
* Called when the activity will start interacting with the user.
*
* @param multitasking Flag indicating if multitasking is turned on for app.
*/
@Override
public void onResume(boolean multitasking) {
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
webView.postMessage("splashscreen", "hide");
}
});
super.onResume(multitasking);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment