Skip to content
This repository has been archived by the owner on Aug 4, 2018. It is now read-only.

Commit

Permalink
Update CordovaLib
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Mar 30, 2016
1 parent 3b50f69 commit f9cd259
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CallbackContext {

private String callbackId;
private CordovaWebView webView;
private boolean finished;
protected boolean finished;
private int changingThreads;

public CallbackContext(String callbackId, CordovaWebView webView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one

import android.app.Activity;
import android.app.AlertDialog;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
Expand Down Expand Up @@ -108,26 +109,20 @@ public void onCreate(Bundle savedInstanceState) {

// need to activate preferences before super.onCreate to avoid "requestFeature() must be called before adding content" exception
loadConfig();
if(!preferences.getBoolean("ShowTitle", false))
{
if (!preferences.getBoolean("ShowTitle", false)) {
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}

if(preferences.getBoolean("SetFullscreen", false))
{
if (preferences.getBoolean("SetFullscreen", false)) {
Log.d(TAG, "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version.");
preferences.set("Fullscreen", true);
}
if(preferences.getBoolean("Fullscreen", false))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
if (preferences.getBoolean("Fullscreen", false)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
immersiveMode = true;
}
else
{
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
Expand All @@ -137,12 +132,11 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

cordovaInterface = makeCordovaInterface();
if(savedInstanceState != null)
{
if (savedInstanceState != null) {
cordovaInterface.restoreInstanceState(savedInstanceState);
}
}

protected void init() {
appView = makeWebView();
createViews();
Expand Down Expand Up @@ -191,7 +185,7 @@ protected void createViews() {

/**
* Construct the default web view object.
*
* <p/>
* Override this to customize the webview that is used.
*/
protected CordovaWebView makeWebView() {
Expand Down Expand Up @@ -244,13 +238,13 @@ protected void onPause() {

/**
* Called when the activity receives a new intent
**/
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//Forward to plugins
if (this.appView != null)
this.appView.onNewIntent(intent);
this.appView.onNewIntent(intent);
}

/**
Expand All @@ -260,7 +254,7 @@ protected void onNewIntent(Intent intent) {
protected void onResume() {
super.onResume();
LOG.d(TAG, "Resumed the activity.");

if (this.appView == null) {
return;
}
Expand Down Expand Up @@ -320,16 +314,17 @@ public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && immersiveMode) {
final int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

getWindow().getDecorView().setSystemUiVisibility(uiOptions);
}
}

@SuppressLint("NewApi")
@Override
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
// Capture requestCode here so that it is captured in the setActivityResultCallback() case.
Expand All @@ -341,10 +336,10 @@ public void startActivityForResult(Intent intent, int requestCode, Bundle option
* Called when an activity you launched exits, giving you the requestCode you started it with,
* the resultCode it returned, and any additional data from it.
*
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Expand All @@ -357,9 +352,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
* Report an error to the host application. These errors are unrecoverable (i.e. the main resource is unavailable).
* The errorCode parameter corresponds to one of the ERROR_* constants.
*
* @param errorCode The error code corresponding to an ERROR_* value.
* @param description A String describing the error.
* @param failingUrl The url that failed to load.
* @param errorCode The error code corresponding to an ERROR_* value.
* @param description A String describing the error.
* @param failingUrl The url that failed to load.
*/
public void onReceivedError(final int errorCode, final String description, final String failingUrl) {
final CordovaActivity me = this;
Expand Down Expand Up @@ -448,9 +443,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
/**
* Called when a message is sent to plugin.
*
* @param id The message id
* @param data The message data
* @return Object or null
* @param id The message id
* @param data The message data
* @return Object or null
*/
public Object onMessage(String id, Object data) {
if ("onReceivedError".equals(id)) {
Expand All @@ -466,16 +461,15 @@ public Object onMessage(String id, Object data) {
return null;
}

protected void onSaveInstanceState(Bundle outState)
{
protected void onSaveInstanceState(Bundle outState) {
cordovaInterface.onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}

/**
* Called by the system when the device configuration changes while your activity is running.
*
* @param newConfig The new device configuration
* @param newConfig The new device configuration
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
Expand All @@ -488,4 +482,27 @@ public void onConfigurationChanged(Configuration newConfig) {
pm.onConfigurationChanged(newConfig);
}
}

/**
* Called by the system when the user grants permissions
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[],
int[] grantResults) {
try
{
cordovaInterface.onRequestPermissionResult(requestCode, permissions, grantResults);
}
catch (JSONException e)
{
LOG.d(TAG, "JSONException: Parameters fed into the method are not valid");
e.printStackTrace();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,20 @@ public interface CordovaInterface {
* Returns a shared thread pool that can be used for background tasks.
*/
public ExecutorService getThreadPool();

/**
* Sends a permission request to the activity for one permission.
*/
public void requestPermission(CordovaPlugin plugin, int requestCode, String permission);

/**
* Sends a permission request to the activity for a group of permissions
*/
public void requestPermissions(CordovaPlugin plugin, int requestCode, String [] permissions);

/**
* Check for a permission. Returns true if the permission is granted, false otherwise.
*/
public boolean hasPermission(String permission);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ Licensed to the Apache Software Foundation (ASF) under one

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -38,8 +43,11 @@ public class CordovaInterfaceImpl implements CordovaInterface {

protected ActivityResultHolder savedResult;
protected CordovaPlugin activityResultCallback;
protected CordovaPlugin permissionResultCallback;
protected String initCallbackService;
protected int activityResultRequestCode;
protected boolean activityWasDestroyed = false;
protected Bundle savedPluginState;

public CordovaInterfaceImpl(Activity activity) {
this(activity, Executors.newCachedThreadPool());
Expand Down Expand Up @@ -89,12 +97,31 @@ public ExecutorService getThreadPool() {
}

/**
* Dispatches any pending onActivityResult callbacks.
* Dispatches any pending onActivityResult callbacks and sends the resume event if the
* Activity was destroyed by the OS.
*/
public void onCordovaInit(PluginManager pluginManager) {
this.pluginManager = pluginManager;
if (savedResult != null) {
onActivityResult(savedResult.requestCode, savedResult.resultCode, savedResult.intent);
} else if(activityWasDestroyed) {
// If there was no Activity result, we still need to send out the resume event if the
// Activity was destroyed by the OS
activityWasDestroyed = false;
if(pluginManager != null)
{
CoreAndroid appPlugin = (CoreAndroid) pluginManager.getPlugin(CoreAndroid.PLUGIN_NAME);
if(appPlugin != null) {
JSONObject obj = new JSONObject();
try {
obj.put("action", "resume");
} catch (JSONException e) {
LOG.e(TAG, "Failed to create event message", e);
}
appPlugin.sendResumeEvent(new PluginResult(PluginResult.Status.OK, obj));
}
}

}
}

Expand All @@ -109,6 +136,10 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent intent)
savedResult = new ActivityResultHolder(requestCode, resultCode, intent);
if (pluginManager != null) {
callback = pluginManager.getPlugin(initCallbackService);
if(callback != null) {
callback.onRestoreStateForActivityResult(savedPluginState.getBundle(callback.getServiceName()),
new ResumeCallback(callback.getServiceName(), pluginManager));
}
}
}
activityResultCallback = null;
Expand All @@ -120,7 +151,7 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent intent)
callback.onActivityResult(requestCode, resultCode, intent);
return true;
}
Log.w(TAG, "Got an activity result, but no plugin was registered to receive it" + (savedResult != null ? " yet!": "."));
Log.w(TAG, "Got an activity result, but no plugin was registered to receive it" + (savedResult != null ? " yet!" : "."));
return false;
}

Expand All @@ -141,13 +172,19 @@ public void onSaveInstanceState(Bundle outState) {
String serviceName = activityResultCallback.getServiceName();
outState.putString("callbackService", serviceName);
}
if(pluginManager != null){
outState.putBundle("plugin", pluginManager.onSaveInstanceState());
}

}

/**
* Call this from onCreate() so that any saved startActivityForResult parameters will be restored.
*/
public void restoreInstanceState(Bundle savedInstanceState) {
initCallbackService = savedInstanceState.getString("callbackService");
savedPluginState = savedInstanceState.getBundle("plugin");
activityWasDestroyed = true;
}

private static class ActivityResultHolder {
Expand All @@ -161,4 +198,46 @@ public ActivityResultHolder(int requestCode, int resultCode, Intent intent) {
this.intent = intent;
}
}

/**
* Called by the system when the user grants permissions
*
* @param requestCode
* @param permissions
* @param grantResults
*/
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException {
if(permissionResultCallback != null)
{
permissionResultCallback.onRequestPermissionResult(requestCode, permissions, grantResults);
permissionResultCallback = null;
}
}

public void requestPermission(CordovaPlugin plugin, int requestCode, String permission) {
permissionResultCallback = plugin;
String[] permissions = new String [1];
permissions[0] = permission;
getActivity().requestPermissions(permissions, requestCode);
}

public void requestPermissions(CordovaPlugin plugin, int requestCode, String [] permissions)
{
permissionResultCallback = plugin;
getActivity().requestPermissions(permissions, requestCode);
}

public boolean hasPermission(String permission)
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
int result = activity.checkSelfPermission(permission);
return PackageManager.PERMISSION_GRANTED == result;
}
else
{
return true;
}
}
}
Loading

0 comments on commit f9cd259

Please sign in to comment.