Browse Source

Handle all possible version names in update checker

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
39b89423d9
  1. 38
      app/src/main/java/awais/instagrabber/utils/FlavorTown.java
  2. 3
      app/src/main/java/awais/instagrabber/utils/UpdateCheckCommon.java
  3. 1
      app/src/main/res/values/strings.xml

38
app/src/main/java/awais/instagrabber/utils/FlavorTown.java

@ -1,12 +1,16 @@
package awais.instagrabber.utils; package awais.instagrabber.utils;
import android.content.Context; import android.content.Context;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import awais.instagrabber.BuildConfig; import awais.instagrabber.BuildConfig;
import awais.instagrabber.R; import awais.instagrabber.R;
@ -16,6 +20,7 @@ import static awais.instagrabber.utils.Utils.settingsHelper;
public final class FlavorTown { public final class FlavorTown {
private static final String TAG = "FlavorTown"; private static final String TAG = "FlavorTown";
private static final UpdateChecker UPDATE_CHECKER = UpdateChecker.getInstance(); private static final UpdateChecker UPDATE_CHECKER = UpdateChecker.getInstance();
private static final Pattern VERSION_NAME_PATTERN = Pattern.compile("v?(\\d+\\.\\d+\\.\\d+)(?:_?)(\\w*)(?:-?)(\\w*)");
private static boolean checking = false; private static boolean checking = false;
@ -28,21 +33,42 @@ public final class FlavorTown {
if (checking) return; if (checking) return;
checking = true; checking = true;
AppExecutors.getInstance().networkIO().execute(() -> { AppExecutors.getInstance().networkIO().execute(() -> {
final String version = UPDATE_CHECKER.getLatestVersion();
if (version == null) return;
if (force && version.equals(BuildConfig.VERSION_NAME)) {
Toast.makeText(context, "You're already on the latest version", Toast.LENGTH_SHORT).show();
final String onlineVersionName = UPDATE_CHECKER.getLatestVersion();
if (onlineVersionName == null) return;
final String onlineVersion = getVersion(onlineVersionName);
final String localVersion = getVersion(BuildConfig.VERSION_NAME);
if (Objects.equals(onlineVersion, localVersion)) {
if (force) {
AppExecutors.getInstance().mainThread().execute(() -> {
final Context applicationContext = context.getApplicationContext();
// Check if app was closed or crashed before reaching here
if (applicationContext == null) return;
// Show toast if version number preference was tapped
Toast.makeText(applicationContext, R.string.on_latest_version, Toast.LENGTH_SHORT).show();
});
}
return; return;
} }
final boolean shouldShowDialog = UpdateCheckCommon.shouldShowUpdateDialog(force, version);
final boolean shouldShowDialog = UpdateCheckCommon.shouldShowUpdateDialog(force, onlineVersionName);
if (!shouldShowDialog) return; if (!shouldShowDialog) return;
UpdateCheckCommon.showUpdateDialog(context, version, (dialog, which) -> {
UpdateCheckCommon.showUpdateDialog(context, onlineVersionName, (dialog, which) -> {
UPDATE_CHECKER.onDownload(context); UPDATE_CHECKER.onDownload(context);
dialog.dismiss(); dialog.dismiss();
}); });
}); });
} }
private static String getVersion(@NonNull final String versionName) {
final Matcher matcher = VERSION_NAME_PATTERN.matcher(versionName);
if (!matcher.matches()) return versionName;
try {
return matcher.group(1);
} catch (Exception e) {
Log.e(TAG, "getVersion: ", e);
}
return versionName;
}
public static void changelogCheck(@NonNull final Context context) { public static void changelogCheck(@NonNull final Context context) {
if (settingsHelper.getInteger(Constants.PREV_INSTALL_VERSION) < BuildConfig.VERSION_CODE) { if (settingsHelper.getInteger(Constants.PREV_INSTALL_VERSION) < BuildConfig.VERSION_CODE) {
int appUaCode = settingsHelper.getInteger(Constants.APP_UA_CODE); int appUaCode = settingsHelper.getInteger(Constants.APP_UA_CODE);

3
app/src/main/java/awais/instagrabber/utils/UpdateCheckCommon.java

@ -17,8 +17,7 @@ public final class UpdateCheckCommon {
public static boolean shouldShowUpdateDialog(final boolean force, public static boolean shouldShowUpdateDialog(final boolean force,
@NonNull final String version) { @NonNull final String version) {
final String skippedVersion = settingsHelper.getString(Constants.SKIPPED_VERSION); final String skippedVersion = settingsHelper.getString(Constants.SKIPPED_VERSION);
return force || (!version.equals(BuildConfig.VERSION_NAME) && !BuildConfig.DEBUG && !skippedVersion
.equals(version));
return force || (!BuildConfig.DEBUG && !skippedVersion.equals(version));
} }
public static void showUpdateDialog(@NonNull final Context context, public static void showUpdateDialog(@NonNull final Context context,

1
app/src/main/res/values/strings.xml

@ -476,4 +476,5 @@
<string name="crash_report_subject">Barinsta Crash Report</string> <string name="crash_report_subject">Barinsta Crash Report</string>
<string name="crash_report_title">Select an email app to send crash logs</string> <string name="crash_report_title">Select an email app to send crash logs</string>
<string name="skip_update">Skip this update</string> <string name="skip_update">Skip this update</string>
<string name="on_latest_version">You\'re already on the latest version</string>
</resources> </resources>
Loading…
Cancel
Save