Changes to support hide notification with data on android devices

-Since android toasts does not support any kind of listener or callback
when the toast hides, the solutions is kind of a hack but it works.
This commit is contained in:
ElNinjaGaiden 2016-03-04 11:26:00 -06:00
parent 9b9365d9da
commit 566f6f7392

View File

@ -45,25 +45,19 @@ public class Toast extends CordovaPlugin {
// note that webView.isPaused() is not Xwalk compatible, so tracking it poor-man style // note that webView.isPaused() is not Xwalk compatible, so tracking it poor-man style
private boolean isPaused; private boolean isPaused;
private String currentMessage;
private JSONObject currentData;
@Override @Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (ACTION_HIDE_EVENT.equals(action)) { if (ACTION_HIDE_EVENT.equals(action)) {
if (mostRecentToast != null) { if (mostRecentToast != null) {
mostRecentToast.cancel(); mostRecentToast.cancel();
getViewGroup().setOnTouchListener(null); getViewGroup().setOnTouchListener(null);
returnTapEvent("hide", currentMessage, currentData, callbackContext);
mostRecentToast = null;
} }
final JSONObject hideOptions = args.getJSONObject(0); callbackContext.success();
final String hidemessage = hideOptions.getString("message");
final JSONObject hideData = hideOptions.has("data") ? hideOptions.getJSONObject("data") : null;
final JSONObject json = new JSONObject();
try {
json.put("event", "hide");
json.put("message", hidemessage);
json.put("data", hideData);
} catch (JSONException e) {
e.printStackTrace();
}
callbackContext.success(json);
return true; return true;
} else if (ACTION_SHOW_EVENT.equals(action)) { } else if (ACTION_SHOW_EVENT.equals(action)) {
@ -81,6 +75,9 @@ public class Toast extends CordovaPlugin {
final JSONObject data = options.has("data") ? options.getJSONObject("data") : null; final JSONObject data = options.has("data") ? options.getJSONObject("data") : null;
final JSONObject styling = options.optJSONObject("styling"); final JSONObject styling = options.optJSONObject("styling");
currentMessage = message;
currentData = data;
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(new Runnable() {
public void run() { public void run() {
final android.widget.Toast toast = android.widget.Toast.makeText( final android.widget.Toast toast = android.widget.Toast.makeText(
@ -173,7 +170,7 @@ public class Toast extends CordovaPlugin {
if (tapped) { if (tapped) {
getViewGroup().setOnTouchListener(null); getViewGroup().setOnTouchListener(null);
return returnTapEvent(message, data, callbackContext); return returnTapEvent("touch", message, data, callbackContext);
} }
return false; return false;
} }
@ -182,12 +179,27 @@ public class Toast extends CordovaPlugin {
toast.getView().setOnTouchListener(new View.OnTouchListener() { toast.getView().setOnTouchListener(new View.OnTouchListener() {
@Override @Override
public boolean onTouch(View view, MotionEvent motionEvent) { public boolean onTouch(View view, MotionEvent motionEvent) {
return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent(message, data, callbackContext); return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent("touch", message, data, callbackContext);
} }
}); });
} }
Thread thread = new Thread(){
@Override
public void run() {
try {
Thread.sleep("short".equals(duration) ? 2000 : 3500);
if (mostRecentToast != null) {
returnTapEvent("hide", message, data, callbackContext);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
toast.show(); toast.show();
thread.start();
mostRecentToast = toast; mostRecentToast = toast;
PluginResult pr = new PluginResult(PluginResult.Status.OK); PluginResult pr = new PluginResult(PluginResult.Status.OK);
@ -203,10 +215,10 @@ public class Toast extends CordovaPlugin {
} }
} }
private boolean returnTapEvent(String message, JSONObject data, CallbackContext callbackContext) { private boolean returnTapEvent(String eventName, String message, JSONObject data, CallbackContext callbackContext) {
final JSONObject json = new JSONObject(); final JSONObject json = new JSONObject();
try { try {
json.put("event", "touch"); json.put("event", eventName);
json.put("message", message); json.put("message", message);
json.put("data", data); json.put("data", data);
} catch (JSONException e) { } catch (JSONException e) {