Skip to content

Commit

Permalink
Rounding out #8
Browse files Browse the repository at this point in the history
  • Loading branch information
mlynch committed Jan 15, 2018
1 parent c0cfb5a commit 89c3f47
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 34 deletions.
2 changes: 1 addition & 1 deletion android/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions android/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/android/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/android/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ protected void restoreInstanceState(Bundle savedInstanceState) {
String lastOptionsJson = savedInstanceState.getString(BUNDLE_PLUGIN_CALL_OPTIONS_SAVED_KEY);

if (lastPluginId != null) {

// If we have JSON blob saved, create a new plugin call with the original options
if (lastOptionsJson != null) {
try {
JSObject options = new JSObject(lastOptionsJson);
Expand All @@ -403,10 +405,13 @@ protected void restoreInstanceState(Bundle savedInstanceState) {
} catch (JSONException ex) {
Log.e(TAG, "Unable to restore plugin call, unable to parse persisted JSON object", ex);
}
} else {
Bundle bundleData = savedInstanceState.getBundle(BUNDLE_PLUGIN_CALL_BUNDLE_KEY);
}

// TODO: Process a bundle
// Let the plugin restore any state it needs
Bundle bundleData = savedInstanceState.getBundle(BUNDLE_PLUGIN_CALL_BUNDLE_KEY);
PluginHandle lastPlugin = getPlugin(lastPluginId);
if (lastPlugin != null) {
lastPlugin.getInstance().restoreState(bundleData);
}
}
}
Expand All @@ -424,7 +429,7 @@ public void saveInstanceState(Bundle outState) {
outState.putString(BUNDLE_LAST_PLUGIN_ID_KEY, call.getPluginId());
outState.putString(BUNDLE_LAST_PLUGIN_CALL_METHOD_NAME_KEY, call.getMethodName());
outState.putString(BUNDLE_PLUGIN_CALL_OPTIONS_SAVED_KEY, call.getData().toString());
outState.putBundle(BUNDLE_PLUGIN_CALL_BUNDLE_KEY, handle.getInstance().persistLastCallOptions());
outState.putBundle(BUNDLE_PLUGIN_CALL_BUNDLE_KEY, handle.getInstance().saveInstanceState());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,37 @@


/**
* MessageHandler handles messages from the webview, dispatching them
* MessageHandler handles messages from the WebView, dispatching them
* to plugins.
*/
public class MessageHandler {
private Bridge bridge;
private WebView webView;


public MessageHandler(Bridge capacitor, WebView webView) {
this.bridge = capacitor;
public MessageHandler(Bridge bridge, WebView webView) {
this.bridge = bridge;
this.webView = webView;

webView.addJavascriptInterface(this, "androidBridge");
}

/**
* The main message handler that will be called from JavaScript
* to send a message to the native bridge.
* @param jsonStr
*/
@JavascriptInterface
public void postMessageStr(String str) {
Log.d(Bridge.TAG, "Post message: " + str);
}

@JavascriptInterface
@SuppressWarnings("unused")
public void postMessage(String jsonStr) {
try {
Log.d(Bridge.TAG, jsonStr);

JSObject postData = new JSObject(jsonStr);
String callbackId = postData.getString("callbackId");
String pluginId = postData.getString("pluginId");
String methodName = postData.getString("methodName");
JSObject methodData = postData.getJSObject("options");

Log.d(Bridge.TAG, "callback: " + callbackId + ", pluginId: " + pluginId + ", methodName: " + methodName + ", methodData: " + methodData.toString());
Log.d(Bridge.TAG, "To native: " + callbackId + ", pluginId: " + pluginId +
", methodName: " + methodName);

this.callPluginMethod(callbackId, pluginId, methodName, methodData);

Expand All @@ -46,16 +45,18 @@ public void postMessage(String jsonStr) {
}
}

void callPluginMethod(String callbackId, String pluginId, String methodName, JSObject methodData) {
private void callPluginMethod(String callbackId, String pluginId, String methodName, JSObject methodData) {
PluginCall call = new PluginCall(this, pluginId, callbackId, methodName, methodData);

bridge.callPluginMethod(pluginId, methodName, call);
}

public void responseMessage(String callbackId, PluginResult successResult, PluginResult errorResult) {
public void sendResponseMessage(String callbackId, String pluginId, String methodName, PluginResult successResult, PluginResult errorResult) {
try {
PluginResult data = new PluginResult();
data.put("callbackId", callbackId);
data.put("pluginId", pluginId);
data.put("methodName", methodName);

if (errorResult != null) {
data.put("success", false);
Expand All @@ -72,17 +73,17 @@ public void responseMessage(String callbackId, PluginResult successResult, Plugi

final WebView webView = this.webView;
webView.post(new Runnable() {
@Override
public void run() {
webView.evaluateJavascript(runScript, null);
@Override
public void run() {
webView.evaluateJavascript(runScript, null);
}
});
} else {
bridge.storeDanglingPluginResult(data);
}

} catch (Exception ex) {
Log.e(Bridge.TAG, "responseMessage: error: " + ex);
Log.e(Bridge.TAG, "sendResponseMessage: error: " + ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,13 @@ private void addEventListener(String eventName, PluginCall call) {
listeners = new ArrayList<PluginCall>();
eventListeners.put(eventName, listeners);

// Must add the call before sending retained arguments
listeners.add(call);

sendRetainedArgumentsForEvent(eventName);
} else {
listeners.add(call);
}

listeners.add(call);
}

/**
Expand Down Expand Up @@ -334,7 +337,7 @@ protected void handleRequestPermissionsResult(int requestCode, String[] permissi
* store option values in a {@link Bundle} to avoid exceeding limits.
* @return a new {@link Bundle} with fields set from the options of the last saved {@link PluginCall}
*/
protected Bundle persistLastCallOptions() {
protected Bundle saveInstanceState() {
PluginCall savedCall = getSavedCall();

if (savedCall == null) {
Expand All @@ -351,6 +354,15 @@ protected Bundle persistLastCallOptions() {
return ret;
}

/**
* Called when the app is opened with a previously un-handled
* activity response. If the plugin that started the activity
* stored data in {@link Plugin#saveInstanceState()} then this
* method will be called to allow the plugin to restore from that.
* @param state
*/
protected void restoreState(Bundle state) {
}

/**
* Handle activity result, should be overridden by each plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public void successCallback(PluginResult successResult) {
return;
}

this.msgHandler.responseMessage(this.callbackId, successResult, null);
this.msgHandler.sendResponseMessage(this.callbackId, this.pluginId, this.methodName, successResult, null);
}


public void success(JSObject data) {
PluginResult result = new PluginResult(data);
this.msgHandler.responseMessage(this.callbackId, result, null);
this.msgHandler.sendResponseMessage(this.callbackId, this.pluginId, this.methodName, result, null);
}

public void success() {
Expand All @@ -69,7 +69,7 @@ public void errorCallback(String msg) {
Log.e(Bridge.TAG, jsonEx.toString());
}

this.msgHandler.responseMessage(this.callbackId, null, errorResult);
this.msgHandler.sendResponseMessage(this.callbackId, this.pluginId, this.methodName, null, errorResult);
}

public void error(String msg, Exception ex) {
Expand All @@ -85,7 +85,7 @@ public void error(String msg, Exception ex) {
Log.e(Bridge.TAG, jsonEx.toString());
}

this.msgHandler.responseMessage(this.callbackId, null, errorResult);
this.msgHandler.sendResponseMessage(this.callbackId, this.pluginId, this.methodName, null, errorResult);
}

public void error(String msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.util.Log;

import org.json.JSONException;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down Expand Up @@ -70,6 +72,10 @@ public String toString() {
}

public JSObject getData() {
return this.json;
try {
return this.json.getJSObject("data");
} catch (JSONException ex) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
Expand Down Expand Up @@ -185,11 +186,27 @@ public void processImage(PluginCall call, Intent data) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Uri contentUri = Uri.fromFile(f);
Bitmap bitmap = BitmapFactory.decodeFile(imageFileSavePath, bmOptions);

returnBase64(call, bitmap);
}
}

@Override
protected Bundle saveInstanceState() {
Bundle bundle = super.saveInstanceState();
bundle.putString("cameraImageFileSavePath", imageFileSavePath);
return bundle;
}

@Override
protected void restoreState(Bundle state) {
String storedImageFileSavePath = state.getString("cameraImageFileSavePath");
if (storedImageFileSavePath != null) {
imageFileSavePath = storedImageFileSavePath;
}
}


private void returnBase64(PluginCall call, Bitmap bitmap) {
int quality = call.getInt("quality", 100);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

@NativePlugin()
public class SplashScreen extends Plugin {
private static final int DEFAULT_FADE_IN_DURATION = 200;
private static final int DEFAULT_FADE_OUT_DURATION = 200;
private static final int DEFAULT_SHOW_DURATION = 3000;
private static final boolean DEFAULT_AUTO_HIDE = true;

@Override
public void load() {
buildViews();
}

@PluginMethod()
public void show(PluginCall call) {
Expand All @@ -18,4 +27,17 @@ public void hide(PluginCall call) {

}

private void buildViews() {

}

@Override
public void handleOnPause() {
super.handleOnPause();
}

@Override
protected void handleOnResume() {
super.handleOnResume();
}
}

0 comments on commit 89c3f47

Please sign in to comment.