Skip to content
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

Fix for android 11 #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions src/android/nl/xservices/plugins/Toast.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class Toast extends CordovaPlugin {
private static final String ACTION_SHOW_EVENT = "show";
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 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;

Expand All @@ -38,7 +38,8 @@ public class Toast extends CordovaPlugin {
private static final boolean IS_AT_LEAST_LOLLIPOP = Build.VERSION.SDK_INT >= 21;
private static final boolean IS_AT_LEAST_PIE = Build.VERSION.SDK_INT >= 28;

// 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 String currentMessage;
Expand Down Expand Up @@ -146,9 +147,17 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) {
return false;
}
if (mostRecentToast == null || !mostRecentToast.getView().isShown()) {
getViewGroup().setOnTouchListener(null);
return false;
// on Android 11 getView is deprecated and return null the previous condition crashes on android
// if the user tap on the notification
if (!(mostRecentToast != null && Build.VERSION.SDK_INT < 30)) {
// if getview is not null so mostRecentToast.getView().isShown() i can execute old code

getViewGroup().setOnTouchListener(null);
return true;
}else {
// getView null notify the touch event
return returnTapEvent("touch", msg, data, callbackContext);
}
}

float w = mostRecentToast.getView().getWidth();
Expand Down Expand Up @@ -193,35 +202,26 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
}
// trigger show every 2500 ms for as long as the requested duration
_timer = new CountDownTimer(hideAfterMs, 2500) {
public void onTick(long millisUntilFinished) {
// see https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/116
// and https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/120
// if (!IS_AT_LEAST_PIE) {
// toast.show();
// }
}
public void onFinish() {
returnTapEvent("hide", msg, data, callbackContext);
toast.cancel();
}
}.start();

mostRecentToast = toast;
toast.show();
public void onTick(long millisUntilFinished) {
// see https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/116
// and https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/120
// if (!IS_AT_LEAST_PIE) {
// toast.show();
// }
}

public void onFinish() {
returnTapEvent("hide", msg, data, callbackContext);
toast.cancel();
}}.start();

PluginResult pr = new PluginResult(PluginResult.Status.OK);
pr.setKeepCallback(true);
callbackContext.sendPluginResult(pr);
}
});
mostRecentToast=toast;toast.show();

return true;
} else {
callbackContext.error("toast." + action + " is not a supported function. Did you mean '" + ACTION_SHOW_EVENT + "'?");
return false;
}
}
PluginResult pr = new PluginResult(PluginResult.Status.OK);pr.setKeepCallback(true);callbackContext.sendPluginResult(pr);
}});

return true;}else{callbackContext.error("toast."+action+" is not a supported function. Did you mean '"+ACTION_SHOW_EVENT+"'?");return false;}}

private void hide() {
if (mostRecentToast != null) {
Expand Down