Skip to content

Commit

Permalink
Fix for API 30 / Android 11 by removing deprecated API calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpa99c committed Jun 28, 2021
1 parent 09d536b commit 7a61201
Showing 1 changed file with 38 additions and 105 deletions.
143 changes: 38 additions & 105 deletions src/android/nl/xservices/plugins/Toast.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package nl.xservices.plugins;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.CountDownTimer;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AlignmentSpan;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
Expand All @@ -36,7 +29,6 @@ public class Toast extends CordovaPlugin {
private ViewGroup viewGroup;

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
private boolean isPaused;
Expand All @@ -59,23 +51,31 @@ public boolean execute(String action, JSONArray args, final CallbackContext call
}

final JSONObject options = args.getJSONObject(0);
final String msg = options.getString("message");
final Spannable message = new SpannableString(msg);
message.setSpan(
new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
0,
msg.length() - 1,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);

final String duration = options.getString("duration");
final String position = options.getString("position");
final int addPixelsY = options.has("addPixelsY") ? options.getInt("addPixelsY") : 0;
final JSONObject data = options.has("data") ? options.getJSONObject("data") : null;
final JSONObject styling = options.optJSONObject("styling");

JSONObject styling = options.optJSONObject("styling");
final String msg = options.getString("message");
currentMessage = msg;
currentData = data;

String _msg = msg;
if(styling != null){
final String textColor = styling.optString("textColor", "#000000");
_msg = "<font color='"+textColor+"' ><b>" + _msg + "</b></font>";

final String backgroundColor = styling.optString("backgroundColor", "#333333");
final Double textSize = styling.optDouble("textSize", -1);
final double opacity = styling.optDouble("opacity", 0.8);
final int cornerRadius = styling.optInt("cornerRadius", 100);
final int horizontalPadding = styling.optInt("horizontalPadding", 50);
final int verticalPadding = styling.optInt("verticalPadding", 30);
}
final String html = _msg;


cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
int hideAfterMs;
Expand All @@ -87,12 +87,31 @@ public void run() {
// assuming a number of ms
hideAfterMs = Integer.parseInt(duration);
}

Spanned message;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// FROM_HTML_MODE_LEGACY is the behaviour that was used for versions below android N
// we are using this flag to give a consistent behaviour
message = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
} else {
message = Html.fromHtml(html);
}
final android.widget.Toast toast = android.widget.Toast.makeText(
IS_AT_LEAST_LOLLIPOP ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
message,
"short".equalsIgnoreCase(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG
);

toast.addCallback(new android.widget.Toast.Callback(){
public void onToastShown() {

}

public void onToastHidden() {
returnTapEvent("hide", msg, data, callbackContext);
}
});

if ("top".equals(position)) {
toast.setGravity(GRAVITY_TOP, 0, BASE_TOP_BOTTOM_OFFSET + addPixelsY);
} else if ("bottom".equals(position)) {
Expand All @@ -104,93 +123,7 @@ public void run() {
return;
}

// if one of the custom layout options have been passed in, draw our own shape
if (styling != null && Build.VERSION.SDK_INT >= 16) {

// the defaults mimic the default toast as close as possible
final String backgroundColor = styling.optString("backgroundColor", "#333333");
final String textColor = styling.optString("textColor", "#ffffff");
final Double textSize = styling.optDouble("textSize", -1);
final double opacity = styling.optDouble("opacity", 0.8);
final int cornerRadius = styling.optInt("cornerRadius", 100);
final int horizontalPadding = styling.optInt("horizontalPadding", 50);
final int verticalPadding = styling.optInt("verticalPadding", 30);

GradientDrawable shape = new GradientDrawable();
shape.setCornerRadius(cornerRadius);
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));
if (textSize > -1) {
toastTextView.setTextSize(textSize.floatValue());
}

toast.getView().setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);

// this gives the toast a very subtle shadow on newer devices
if (Build.VERSION.SDK_INT >= 21) {
toast.getView().setElevation(6);
}
}

// On Android >= 5 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;

return tapped && returnTapEvent("touch", msg, data, callbackContext);
}
});
} else {
toast.getView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent("touch", msg, data, callbackContext);
}
});
}
// trigger show every 2500 ms for as long as the requested duration
_timer = new CountDownTimer(hideAfterMs, 2500) {
public void onTick(long millisUntilFinished) {
Expand Down

2 comments on commit 7a61201

@shahdeep1989
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dpa99c ,

Getting error on cordova build android

Task :app:compileDebugJavaWithJavac FAILED
/platforms/android/app/src/main/java/nl/xservices/plugins/Toast.java:105: error: cannot find symbol
toast.addCallback(new android.widget.Toast.Callback(){
^
symbol: class Callback
location: class Toast
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 5s
31 actionable tasks: 6 executed, 25 up-to-date
Command failed with exit code 1: /platforms/android/gradlew cdvBuildDebug -b /platforms/android/build.gradle

$ cordova platforms ls
Installed platforms:
android 9.1.0
ios 6.2.0
Available platforms:
browser ^6.0.0
electron ^1.0.0
osx ^6.0.0

$ cordova -v
10.0.0

$ cordova plugins ls
cordova-custom-config 5.1.0 "cordova-custom-config"
cordova-play-services-version-adapter 1.0.2 "Cordova/Ionic Google Play Services Versions Incompatibility Adapter"
cordova-plugin-app-version 0.1.12 "AppVersion"
cordova-plugin-appavailability 0.4.2 "AppAvailability"
cordova-plugin-camera 5.0.2 "Camera"
cordova-plugin-crop 0.3.1 "CropPlugin"
cordova-plugin-device 2.0.3 "Device"
cordova-plugin-dialogs 2.0.2 "Notification"
cordova-plugin-doubledummysolver 2.0.0 "Double Dummy Solver"
cordova-plugin-file 6.0.2 "File"
cordova-plugin-globalization 1.11.0 "Globalization"
cordova-plugin-inappbrowser 4.1.0 "InAppBrowser"
cordova-plugin-insomnia 4.3.0 "Insomnia (prevent screen sleep)"
cordova-plugin-network-information 2.0.2 "Network Information"
cordova-plugin-purchase 9.0.0 "Purchase"
cordova-plugin-splashscreen 6.0.0 "Splashscreen"
cordova-plugin-statusbar 2.4.3 "StatusBar"
cordova-plugin-vibration 3.1.1 "Vibration"
cordova-plugin-webviewcolor 2.2.0 "WebView background color plugin"
cordova-plugin-whitelist 1.3.4 "Whitelist"
cordova-plugin-x-socialsharing 5.6.8 "SocialSharing"
cordova-plugin-x-toast 2.7.2 "Toast"
cordova-support-android-plugin 1.0.2 "cordova-support-android-plugin"
cordova-support-google-services 1.1.0 "cordova-support-google-services"
es6-promise-plugin 4.2.2 "Promise"
phonegap-plugin-multidex 1.0.0 "Multidex"
phonegap-plugin-push 2.2.3 "PushPlugin"

@123dma
Copy link

@123dma 123dma commented on 7a61201 Sep 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed add this(if) to work API less 30

if (Build.VERSION.SDK_INT >= 30) {
toast.addCallback(new android.widget.Toast.Callback() {
public void onToastShown() {

          }

          public void onToastHidden() {
            returnTapEvent("hide", msg, data, callbackContext);
          }
        });

}

Great Job

Please sign in to comment.