mirror of
https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git
synced 2026-07-12 00:02:05 +08:00
#59 Add specific duration of the toast
This commit is contained in:
@@ -3,6 +3,7 @@ package nl.xservices.plugins;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.GradientDrawable;
|
||||
import android.os.Build;
|
||||
import android.os.CountDownTimer;
|
||||
import android.view.Gravity;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
@@ -16,16 +17,6 @@ import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/*
|
||||
// TODO nice way for the Toast plugin to offer a longer delay than the default short and long options
|
||||
// TODO also look at https://github.com/JohnPersano/Supertoasts
|
||||
new CountDownTimer(6000, 1000) {
|
||||
public void onTick(long millisUntilFinished) {toast.show();}
|
||||
public void onFinish() {toast.show();}
|
||||
}.start();
|
||||
|
||||
Also, check https://github.com/JohnPersano/SuperToasts
|
||||
*/
|
||||
public class Toast extends CordovaPlugin {
|
||||
|
||||
private static final String ACTION_SHOW_EVENT = "show";
|
||||
@@ -40,29 +31,24 @@ public class Toast extends CordovaPlugin {
|
||||
private android.widget.Toast mostRecentToast;
|
||||
private ViewGroup viewGroup;
|
||||
|
||||
private static final boolean IS_AT_LEAST_LOLLIPOP = Build.VERSION.SDK_INT >= 21;
|
||||
|
||||
// note that webView.isPaused() is not Xwalk compatible, so tracking it poor-man style
|
||||
private boolean isPaused;
|
||||
|
||||
private static CountDownTimer _timer;
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
if (ACTION_HIDE_EVENT.equals(action)) {
|
||||
if (mostRecentToast != null) {
|
||||
mostRecentToast.cancel();
|
||||
getViewGroup().setOnTouchListener(null);
|
||||
}
|
||||
hide();
|
||||
callbackContext.success();
|
||||
return true;
|
||||
|
||||
} else if (ACTION_SHOW_EVENT.equals(action)) {
|
||||
|
||||
if (this.isPaused) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final JSONObject options = args.getJSONObject(0);
|
||||
|
||||
final String message = options.getString("message");
|
||||
final String duration = options.getString("duration");
|
||||
final String position = options.getString("position");
|
||||
@@ -72,10 +58,20 @@ public class Toast extends CordovaPlugin {
|
||||
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
int hideAfterMs;
|
||||
if ("short".equalsIgnoreCase(duration)) {
|
||||
hideAfterMs = 2000;
|
||||
} else if ("long".equalsIgnoreCase(duration)) {
|
||||
hideAfterMs = 4000;
|
||||
} else {
|
||||
// assuming a number of ms
|
||||
hideAfterMs = Integer.parseInt(duration);
|
||||
}
|
||||
final android.widget.Toast toast = android.widget.Toast.makeText(
|
||||
IS_AT_LEAST_LOLLIPOP ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
|
||||
Build.VERSION.SDK_INT >= 21 ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
|
||||
message,
|
||||
"short".equals(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG);
|
||||
android.widget.Toast.LENGTH_LONG // actually controlled by a timer further down
|
||||
);
|
||||
|
||||
if ("top".equals(position)) {
|
||||
toast.setGravity(GRAVITY_TOP, 0, BASE_TOP_BOTTOM_OFFSET + addPixelsY);
|
||||
@@ -104,11 +100,11 @@ public class Toast extends CordovaPlugin {
|
||||
shape.setAlpha((int)(opacity * 255)); // 0-255, where 0 is an invisible background
|
||||
shape.setColor(Color.parseColor(backgroundColor));
|
||||
toast.getView().setBackground(shape);
|
||||
|
||||
|
||||
final TextView toastTextView;
|
||||
toastTextView = (TextView) toast.getView().findViewById(android.R.id.message);
|
||||
toastTextView.setTextColor(Color.parseColor(textColor));
|
||||
|
||||
|
||||
toast.getView().setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);
|
||||
|
||||
// this gives the toast a very subtle shadow on newer devices
|
||||
@@ -117,67 +113,60 @@ public class Toast extends CordovaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
// On Android >= 5 you can no longer rely on the 'toast.getView().setOnTouchListener',
|
||||
// On newer Android devices you can no longer rely on the 'toast.getView().setOnTouchListener',
|
||||
// so created something funky that compares the Toast position to the tap coordinates.
|
||||
if (IS_AT_LEAST_LOLLIPOP) {
|
||||
getViewGroup().setOnTouchListener(new View.OnTouchListener() {
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
getViewGroup().setOnTouchListener(new View.OnTouchListener() {
|
||||
@Override
|
||||
public boolean onTouch(View view, MotionEvent motionEvent) {
|
||||
if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) {
|
||||
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);
|
||||
if (mostRecentToast == null || !mostRecentToast.getView().isShown()) {
|
||||
getViewGroup().setOnTouchListener(null);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
toast.show();
|
||||
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;
|
||||
|
||||
return tapped && returnTapEvent(message, data, callbackContext);
|
||||
}
|
||||
});
|
||||
|
||||
// trigger show every 2500 ms for as long as the requested duration
|
||||
_timer = new CountDownTimer(hideAfterMs, 2500) {
|
||||
public void onTick(long millisUntilFinished) {toast.show();}
|
||||
public void onFinish() {toast.cancel();}
|
||||
}.start();
|
||||
|
||||
mostRecentToast = toast;
|
||||
toast.show();
|
||||
|
||||
PluginResult pr = new PluginResult(PluginResult.Status.OK);
|
||||
pr.setKeepCallback(true);
|
||||
@@ -192,6 +181,16 @@ public class Toast extends CordovaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private void hide() {
|
||||
if (mostRecentToast != null) {
|
||||
mostRecentToast.cancel();
|
||||
getViewGroup().setOnTouchListener(null);
|
||||
}
|
||||
if (_timer != null) {
|
||||
_timer.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean returnTapEvent(String message, JSONObject data, CallbackContext callbackContext) {
|
||||
final JSONObject json = new JSONObject();
|
||||
try {
|
||||
@@ -202,6 +201,7 @@ public class Toast extends CordovaPlugin {
|
||||
e.printStackTrace();
|
||||
}
|
||||
callbackContext.success(json);
|
||||
hide();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -215,10 +215,7 @@ public class Toast extends CordovaPlugin {
|
||||
|
||||
@Override
|
||||
public void onPause(boolean multitasking) {
|
||||
if (mostRecentToast != null) {
|
||||
mostRecentToast.cancel();
|
||||
getViewGroup().setOnTouchListener(null);
|
||||
}
|
||||
hide();
|
||||
this.isPaused = true;
|
||||
}
|
||||
|
||||
@@ -226,4 +223,4 @@ public class Toast extends CordovaPlugin {
|
||||
public void onResume(boolean multitasking) {
|
||||
this.isPaused = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-16
@@ -5,42 +5,38 @@
|
||||
@implementation Toast
|
||||
|
||||
- (void)show:(CDVInvokedUrlCommand*)command {
|
||||
|
||||
NSDictionary* options = [command argumentAtIndex:0];
|
||||
|
||||
NSString *message = options[@"message"];
|
||||
NSString *duration = options[@"duration"];
|
||||
NSString *position = options[@"position"];
|
||||
NSDictionary *data = options[@"data"];
|
||||
NSNumber *addPixelsY = options[@"addPixelsY"];
|
||||
NSDictionary *styling = options[@"styling"];
|
||||
|
||||
|
||||
if (![position isEqual: @"top"] && ![position isEqual: @"center"] && ![position isEqual: @"bottom"]) {
|
||||
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"invalid position. valid options are 'top', 'center' and 'bottom'"];
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
return;
|
||||
}
|
||||
|
||||
NSInteger durationInt;
|
||||
if ([duration isEqual: @"short"]) {
|
||||
durationInt = 2;
|
||||
} else if ([duration isEqual: @"long"]) {
|
||||
durationInt = 5;
|
||||
|
||||
NSTimeInterval durationMS;
|
||||
if ([duration.lowercaseString isEqualToString: @"short"]) {
|
||||
durationMS = 2000;
|
||||
} else if ([duration.lowercaseString isEqualToString: @"long"]) {
|
||||
durationMS = 4000;
|
||||
} else {
|
||||
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"invalid duration. valid options are 'short' and 'long'"];
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
return;
|
||||
durationMS = [duration intValue];
|
||||
}
|
||||
|
||||
|
||||
[self.webView makeToast:message
|
||||
duration:durationInt
|
||||
duration:durationMS / 1000
|
||||
position:position
|
||||
addPixelsY:addPixelsY == nil ? 0 : [addPixelsY intValue]
|
||||
data:data
|
||||
styling:styling
|
||||
commandDelegate:self.commandDelegate
|
||||
callbackId:command.callbackId];
|
||||
|
||||
|
||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||
pluginResult.keepCallback = [NSNumber numberWithBool:YES];
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
@@ -48,7 +44,7 @@
|
||||
|
||||
- (void)hide:(CDVInvokedUrlCommand*)command {
|
||||
[self.webView hideToast];
|
||||
|
||||
|
||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
}
|
||||
|
||||
+2
-2
@@ -126,10 +126,10 @@ namespace WPCordovaClassLib.Cordova.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
int hideDelay = 2800;
|
||||
int hideDelay = 2500;
|
||||
if ("long".Equals(duration))
|
||||
{
|
||||
hideDelay = 5500;
|
||||
hideDelay = 5000;
|
||||
}
|
||||
else if (!"short".Equals(duration))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user