Skip to content
Merged
Show file tree
Hide file tree
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
35 changes: 15 additions & 20 deletions src/main/java/org/mtransit/android/commons/LinkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
Expand All @@ -22,27 +23,23 @@ public String getLogTag() {
@Nullable
public static final String NO_LABEL = null;

public static boolean open(@NonNull Context context, @Nullable Uri uri, @Nullable String label, @Nullable int... intentFlags) {
return open(context, uri, label, null, intentFlags); // pkg == null (default)
}

public static boolean open(@NonNull Context context, @Nullable Uri uri, @Nullable String label, @Nullable String pkg, @Nullable int... intentFlags) {
if (uri == null) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
return open(context, uri, label, pkg, null, intentFlags);
}

public static boolean open(@NonNull Context context, @Nullable Uri uri, @Nullable String label, @Nullable String pkg, @Nullable Bundle extra, @Nullable int... intentFlags) {
if (uri == null) return false;
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (pkg != null) {
intent.setPackage(pkg);
}
if (intentFlags != null) {
for (int intentFlag : intentFlags) {
intent.addFlags(intentFlag);
}
if (extra != null) {
intent.putExtras(extra);
}
return open(context, intent, label);
}

public static boolean open(@NonNull Context context, @Nullable Uri uri, @Nullable String label, @Nullable int... intentFlags) {
if (uri == null) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (intentFlags != null) {
for (int intentFlag : intentFlags) {
intent.addFlags(intentFlag);
Expand All @@ -52,11 +49,9 @@ public static boolean open(@NonNull Context context, @Nullable Uri uri, @Nullabl
}

public static boolean open(@NonNull Context context, @Nullable Intent intent, @Nullable String label) {
if (intent == null) {
return false;
}
if (intent == null) return false;
try {
context.startActivity(intent); // required starting API Level 30+ (else would required android.permission.QUERY_ALL_PACKAGES permission)
context.startActivity(intent); // required starting API Level 30+ (else would require 'android.permission.QUERY_ALL_PACKAGES' permission)
} catch (ActivityNotFoundException e) {
ToastUtils.makeTextAndShowCentered(context, context.getString(R.string.opening_failed_and_uri, intent.getData()));
return false;
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/org/mtransit/android/commons/StoreUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public final class StoreUtils implements MTLog.Loggable {

private static final String TAG = StoreUtils.class.getSimpleName();
private static final String LOG_TAG = StoreUtils.class.getSimpleName();

@NonNull
@Override
public String getLogTag() {
return TAG;
return LOG_TAG;
}

private static final String HTTP_SCHEME = "http";
Expand All @@ -30,12 +31,25 @@ public String getLogTag() {
private static final String GOOGLE_PLAY_PKG = "com.android.vending";

public static boolean viewAppPage(@NonNull Context context, @NonNull String pkg, @Nullable String label) {
int[] flags = new int[]{ //
final int[] flags = new int[]{ //
Intent.FLAG_ACTIVITY_NEW_TASK, // make sure it does NOT open in the stack of your activity
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED, // task re-parenting if needed
Intent.FLAG_ACTIVITY_CLEAR_TOP, // make sure it opens on app page even if already open in search result
};
boolean success;
final Bundle extras = new Bundle();
extras.putBoolean("overlay", true);
extras.putBoolean("inline", true);
extras.putString("callerId", context.getPackageName());
// tries to force Google Play Store package 1st and extras (no flags)
success = LinkUtils.open(context, Uri.parse(String.format(GOOGLE_PLAY_STORE_BASE_URI_AND_PKG, pkg)), label, GOOGLE_PLAY_PKG, extras);
if (success) {
return true;
}
success = LinkUtils.open(context, Uri.parse(String.format(GOOGLE_PLAY_STORE_BASE_WWW_URI_AND_PKG, pkg)), label, GOOGLE_PLAY_PKG, extras);
if (success) {
return true;
}
// tries to force Google Play Store package 1st
success = LinkUtils.open(context, Uri.parse(String.format(GOOGLE_PLAY_STORE_BASE_URI_AND_PKG, pkg)), label, GOOGLE_PLAY_PKG, flags);
if (success) {
Expand Down