-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CB-8917: Added pending plugin callbacks to resume event payload #239
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ Licensed to the Apache Software Foundation (ASF) under one | |
|
||
package org.apache.cordova; | ||
|
||
import android.Manifest; | ||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
|
@@ -28,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one | |
import android.util.Log; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
@@ -46,6 +46,8 @@ public class CordovaInterfaceImpl implements CordovaInterface { | |
protected CordovaPlugin permissionResultCallback; | ||
protected String initCallbackService; | ||
protected int activityResultRequestCode; | ||
protected boolean activityWasDestroyed = false; | ||
protected Bundle savedPluginState; | ||
|
||
public CordovaInterfaceImpl(Activity activity) { | ||
this(activity, Executors.newCachedThreadPool()); | ||
|
@@ -95,12 +97,28 @@ 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; | ||
|
||
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)); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -115,6 +133,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; | ||
|
@@ -126,7 +148,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; | ||
} | ||
|
||
|
@@ -147,13 +169,17 @@ public void onSaveInstanceState(Bundle outState) { | |
String serviceName = activityResultCallback.getServiceName(); | ||
outState.putString("callbackService", serviceName); | ||
} | ||
|
||
outState.putBundle("plugin", pluginManager.onSaveInstanceState()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to check if pluginManager is not null? I ask because there is a check for that on line 116. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pluginManager should be created when the first URL is loaded. In the default Android project, this happens in |
||
} | ||
|
||
/** | ||
* 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 { | ||
|
@@ -209,6 +235,4 @@ public boolean hasPermission(String permission) | |
return true; | ||
} | ||
} | ||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure savedPluginState will always be non-null at this point? And that it will have the expected bundle?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe so. This will only be called after the Activity was destroyed in which case
onSaveInstanceState()
will have been called and thesavedPluginState
bundle saved. I believe Activity results are only delivered afteronCreate()
is called, so the Bundle will have already been retrieved (but I can double check that). It is the responsibility of the plugin to check if the bundle they receive is null.