summaryrefslogtreecommitdiff
path: root/ui/common/src/main/java
diff options
context:
space:
mode:
authorByteHamster <ByteHamster@users.noreply.github.com>2024-04-05 19:20:27 +0200
committerGitHub <noreply@github.com>2024-04-05 19:20:27 +0200
commit92ab575b150ab49ca85e0ac994558142e49c9e68 (patch)
tree422dcd76895a4ba06ed02723ff61351435c0fe49 /ui/common/src/main/java
parent2143ab135182434911d4554a8ef08115eaa0d2d0 (diff)
downloadAntennaPod-92ab575b150ab49ca85e0ac994558142e49c9e68.zip
Delete core module (#7060)
Diffstat (limited to 'ui/common/src/main/java')
-rw-r--r--ui/common/src/main/java/de/danoeh/antennapod/ui/common/ConfirmationDialog.java54
-rw-r--r--ui/common/src/main/java/de/danoeh/antennapod/ui/common/IntentUtils.java67
2 files changed, 121 insertions, 0 deletions
diff --git a/ui/common/src/main/java/de/danoeh/antennapod/ui/common/ConfirmationDialog.java b/ui/common/src/main/java/de/danoeh/antennapod/ui/common/ConfirmationDialog.java
new file mode 100644
index 000000000..8acedc7d3
--- /dev/null
+++ b/ui/common/src/main/java/de/danoeh/antennapod/ui/common/ConfirmationDialog.java
@@ -0,0 +1,54 @@
+package de.danoeh.antennapod.ui.common;
+
+import android.content.Context;
+import android.content.DialogInterface;
+import androidx.appcompat.app.AlertDialog;
+import com.google.android.material.dialog.MaterialAlertDialogBuilder;
+import android.util.Log;
+
+/**
+ * Creates an AlertDialog which asks the user to confirm something. Other
+ * classes can handle events like confirmation or cancellation.
+ */
+public abstract class ConfirmationDialog {
+
+ private static final String TAG = ConfirmationDialog.class.getSimpleName();
+
+ private final Context context;
+ private final int titleId;
+ private final String message;
+
+ private int positiveText;
+
+ public ConfirmationDialog(Context context, int titleId, int messageId) {
+ this(context, titleId, context.getString(messageId));
+ }
+
+ public ConfirmationDialog(Context context, int titleId, String message) {
+ this.context = context;
+ this.titleId = titleId;
+ this.message = message;
+ }
+
+ private void onCancelButtonPressed(DialogInterface dialog) {
+ Log.d(TAG, "Dialog was cancelled");
+ dialog.dismiss();
+ }
+
+ public void setPositiveText(int id) {
+ this.positiveText = id;
+ }
+
+ public abstract void onConfirmButtonPressed(DialogInterface dialog);
+
+ public final AlertDialog createNewDialog() {
+ MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
+ builder.setTitle(titleId);
+ builder.setMessage(message);
+ builder.setPositiveButton(positiveText != 0 ? positiveText : R.string.confirm_label,
+ (dialog, which) -> onConfirmButtonPressed(dialog));
+ builder.setNegativeButton(R.string.cancel_label, (dialog, which) -> onCancelButtonPressed(dialog));
+ builder.setOnCancelListener(ConfirmationDialog.this::onCancelButtonPressed);
+ return builder.create();
+ }
+}
diff --git a/ui/common/src/main/java/de/danoeh/antennapod/ui/common/IntentUtils.java b/ui/common/src/main/java/de/danoeh/antennapod/ui/common/IntentUtils.java
new file mode 100644
index 000000000..26c703e7b
--- /dev/null
+++ b/ui/common/src/main/java/de/danoeh/antennapod/ui/common/IntentUtils.java
@@ -0,0 +1,67 @@
+package de.danoeh.antennapod.ui.common;
+
+import android.content.ActivityNotFoundException;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.net.Uri;
+import android.util.Log;
+import android.widget.Toast;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.ArrayUtils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Locale;
+
+public class IntentUtils {
+ private static final String TAG = "IntentUtils";
+
+ private IntentUtils(){}
+
+ /*
+ * Checks if there is at least one exported activity that can be performed for the intent
+ */
+ public static boolean isCallable(final Context context, final Intent intent) {
+ List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
+ PackageManager.MATCH_DEFAULT_ONLY);
+ for(ResolveInfo info : list) {
+ if(info.activityInfo.exported) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static void sendLocalBroadcast(Context context, String action) {
+ context.sendBroadcast(new Intent(action).setPackage(context.getPackageName()));
+ }
+
+ public static void openInBrowser(Context context, String url) {
+ try {
+ Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
+ myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ context.startActivity(myIntent);
+ } catch (ActivityNotFoundException e) {
+ Toast.makeText(context, R.string.pref_no_browser_found, Toast.LENGTH_LONG).show();
+ Log.e(TAG, Log.getStackTraceString(e));
+ }
+ }
+
+ public static String getLocalizedWebsiteLink(Context context) {
+ try (InputStream is = context.getAssets().open("website-languages.txt")) {
+ String[] languages = IOUtils.toString(is, StandardCharsets.UTF_8.name()).split("\n");
+ String deviceLanguage = Locale.getDefault().getLanguage();
+ if (ArrayUtils.contains(languages, deviceLanguage) && !"en".equals(deviceLanguage)) {
+ return "https://antennapod.org/" + deviceLanguage;
+ } else {
+ return "https://antennapod.org";
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}