#65 Success callback not triggered when toast tapped in Android

This commit is contained in:
EddyVerbruggen 2016-01-15 13:12:03 +01:00
parent a5cdda3ea2
commit ac0746b9b9
4 changed files with 94 additions and 22 deletions

View File

@ -217,6 +217,8 @@ The Android code was entirely created by me.
For iOS most credits go to this excellent [Toast for iOS project by Charles Scalesse] (https://github.com/scalessec/Toast). For iOS most credits go to this excellent [Toast for iOS project by Charles Scalesse] (https://github.com/scalessec/Toast).
## 6. CHANGELOG ## 6. CHANGELOG
2.3.2: The click event introduced with 2.3.0 did not work with Android 5+.
2.3.0: The plugin will now report back to JS if Toasts were tapped by the user. 2.3.0: The plugin will now report back to JS if Toasts were tapped by the user.
2.0.1: iOS messages are hidden when another one is shown. [Thanks Richie Min!](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/pull/13) 2.0.1: iOS messages are hidden when another one is shown. [Thanks Richie Min!](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/pull/13)

View File

@ -1,6 +1,6 @@
{ {
"name": "cordova-plugin-x-toast", "name": "cordova-plugin-x-toast",
"version": "2.3.1", "version": "2.3.2",
"description": "This plugin allows you to show a Toast. A Toast is a little non intrusive buttonless popup which automatically disappears.", "description": "This plugin allows you to show a Toast. A Toast is a little non intrusive buttonless popup which automatically disappears.",
"cordova": { "cordova": {
"id": "cordova-plugin-x-toast", "id": "cordova-plugin-x-toast",

View File

@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-x-toast" id="cordova-plugin-x-toast"
version="2.3.1"> version="2.3.2">
<name>Toast</name> <name>Toast</name>

View File

@ -4,6 +4,7 @@ import android.os.Build;
import android.view.Gravity; import android.view.Gravity;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
@ -27,7 +28,14 @@ public class Toast extends CordovaPlugin {
private static final String ACTION_SHOW_EVENT = "show"; private static final String ACTION_SHOW_EVENT = "show";
private static final String ACTION_HIDE_EVENT = "hide"; private static final String ACTION_HIDE_EVENT = "hide";
private static final int GRAVITY_TOP = Gravity.TOP|Gravity.CENTER_HORIZONTAL;
private static final int GRAVITY_CENTER = Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL;
private static final int GRAVITY_BOTTOM = Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL;
private static final int BASE_TOP_BOTTOM_OFFSET = 20;
private android.widget.Toast mostRecentToast; private android.widget.Toast mostRecentToast;
private ViewGroup viewGroup;
private static final boolean IS_AT_LEAST_ANDROID5 = Build.VERSION.SDK_INT >= 21; private static final boolean IS_AT_LEAST_ANDROID5 = Build.VERSION.SDK_INT >= 21;
@ -39,6 +47,7 @@ public class Toast extends CordovaPlugin {
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);
} }
callbackContext.success(); callbackContext.success();
return true; return true;
@ -59,7 +68,7 @@ public class Toast extends CordovaPlugin {
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(new Runnable() {
public void run() { public void run() {
android.widget.Toast toast = android.widget.Toast.makeText( final android.widget.Toast toast = android.widget.Toast.makeText(
IS_AT_LEAST_ANDROID5 ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(), IS_AT_LEAST_ANDROID5 ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
message, message,
"short".equals(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG); "short".equals(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG);
@ -71,35 +80,74 @@ public class Toast extends CordovaPlugin {
// } catch (Exception ignore) { // } catch (Exception ignore) {
// } // }
if ("top".equals(position)) { if ("top".equals(position)) {
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 20 + addPixelsY); toast.setGravity(GRAVITY_TOP, 0, BASE_TOP_BOTTOM_OFFSET + addPixelsY);
} else if ("bottom".equals(position)) { } else if ("bottom".equals(position)) {
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 20 - addPixelsY); toast.setGravity(GRAVITY_BOTTOM, 0, BASE_TOP_BOTTOM_OFFSET - addPixelsY);
} else if ("center".equals(position)) { } else if ("center".equals(position)) {
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, addPixelsY); toast.setGravity(GRAVITY_CENTER, 0, addPixelsY);
} else { } else {
callbackContext.error("invalid position. valid options are 'top', 'center' and 'bottom'"); callbackContext.error("invalid position. valid options are 'top', 'center' and 'bottom'");
return; return;
} }
toast.getView().setOnTouchListener(new View.OnTouchListener() { // On Android >= 5 you can no longer rely on the 'toast.getView().setOnTouchListener',
@Override // so created something funky that compares the Toast position to the tap coordinates.
public boolean onTouch(View view, MotionEvent motionEvent) { if (IS_AT_LEAST_ANDROID5) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { getViewGroup().setOnTouchListener(new View.OnTouchListener() {
JSONObject json = new JSONObject(); @Override
try { public boolean onTouch(View view, MotionEvent motionEvent) {
json.put("event", "touch"); if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) {
json.put("message", message); return false;
json.put("data", data); }
} catch (JSONException e) { if (mostRecentToast == null || !mostRecentToast.getView().isShown()) {
e.printStackTrace(); getViewGroup().setOnTouchListener(null);
return false;
}
float w = mostRecentToast.getView().getWidth();
float startX = (view.getWidth() / 2) - (w / 2);
float endX = (view.getWidth() / 2) + (w / 2);
float startY;
float endY;
float g = mostRecentToast.getGravity();
float y = mostRecentToast.getYOffset();
float h = mostRecentToast.getView().getHeight();
if (g == GRAVITY_BOTTOM) {
startY = view.getHeight() - y - h;
endY = view.getHeight() - y;
} else if (g == GRAVITY_CENTER) {
startY = (view.getHeight() / 2) + y - (h / 2);
endY = (view.getHeight() / 2) + y + (h / 2);
} else {
// top
startY = y;
endY = y + h;
}
float tapX = motionEvent.getX();
float tapY = motionEvent.getY();
final boolean tapped = tapX >= startX && tapX <= endX &&
tapY >= startY && tapY <= endY;
if (tapped) {
getViewGroup().setOnTouchListener(null);
return returnTapEvent(message, data, callbackContext);
} }
callbackContext.success(json);
return true;
} else {
return false; return false;
} }
} });
}); } else {
toast.getView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent(message, data, callbackContext);
}
});
}
toast.show(); toast.show();
mostRecentToast = toast; mostRecentToast = toast;
@ -117,10 +165,32 @@ public class Toast extends CordovaPlugin {
} }
} }
private boolean returnTapEvent(String message, JSONObject data, CallbackContext callbackContext) {
final JSONObject json = new JSONObject();
try {
json.put("event", "touch");
json.put("message", message);
json.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
callbackContext.success(json);
return true;
}
// lazy init and caching
private ViewGroup getViewGroup() {
if (viewGroup == null) {
viewGroup = (ViewGroup) ((ViewGroup) cordova.getActivity().findViewById(android.R.id.content)).getChildAt(0);
}
return viewGroup;
}
@Override @Override
public void onPause(boolean multitasking) { public void onPause(boolean multitasking) {
if (mostRecentToast != null) { if (mostRecentToast != null) {
mostRecentToast.cancel(); mostRecentToast.cancel();
getViewGroup().setOnTouchListener(null);
} }
this.isPaused = true; this.isPaused = true;
} }