Browse Source
Separate out latest version check logic per flavor
renovate/org.robolectric-robolectric-4.x
Separate out latest version check logic per flavor
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
8 changed files with 184 additions and 144 deletions
-
64app/src/fdroid/java/awais/instagrabber/utils/UpdateChecker.java
-
61app/src/github/java/awais/instagrabber/utils/UpdateChecker.java
-
4app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java
-
1app/src/main/java/awais/instagrabber/utils/Constants.java
-
100app/src/main/java/awais/instagrabber/utils/FlavorTown.java
-
39app/src/main/java/awais/instagrabber/utils/UpdateCheckCommon.java
-
58app/src/main/java/awais/instagrabber/utils/UpdateChecker.java
-
1app/src/main/res/values/strings.xml
@ -0,0 +1,64 @@ |
|||||
|
package awais.instagrabber.utils; |
||||
|
|
||||
|
import android.util.Log; |
||||
|
|
||||
|
import androidx.annotation.NonNull; |
||||
|
import androidx.annotation.Nullable; |
||||
|
import androidx.appcompat.app.AppCompatActivity; |
||||
|
|
||||
|
import org.json.JSONObject; |
||||
|
|
||||
|
import java.net.HttpURLConnection; |
||||
|
import java.net.URL; |
||||
|
|
||||
|
public class UpdateChecker { |
||||
|
private static final Object LOCK = new Object(); |
||||
|
private static final String TAG = UpdateChecker.class.getSimpleName(); |
||||
|
|
||||
|
private static UpdateChecker instance; |
||||
|
|
||||
|
public static UpdateChecker getInstance() { |
||||
|
if (instance == null) { |
||||
|
synchronized (LOCK) { |
||||
|
if (instance == null) { |
||||
|
instance = new UpdateChecker(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return instance; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Needs to be called asynchronously |
||||
|
* |
||||
|
* @return the latest version from f-droid |
||||
|
*/ |
||||
|
@Nullable |
||||
|
public String getLatestVersion() { |
||||
|
HttpURLConnection conn = null; |
||||
|
try { |
||||
|
conn = (HttpURLConnection) new URL("https://f-droid.org/api/v1/packages/me.austinhuang.instagrabber").openConnection(); |
||||
|
conn.setUseCaches(false); |
||||
|
conn.setRequestProperty("User-Agent", "https://Barinsta.AustinHuang.me / mailto:[email protected]"); |
||||
|
conn.connect(); |
||||
|
final int responseCode = conn.getResponseCode(); |
||||
|
if (responseCode == HttpURLConnection.HTTP_OK) { |
||||
|
final JSONObject data = new JSONObject(NetworkUtils.readFromConnection(conn)); |
||||
|
return "v" + data.getJSONArray("packages").getJSONObject(0).getString("versionName"); |
||||
|
// if (BuildConfig.VERSION_CODE < data.getInt("suggestedVersionCode")) { |
||||
|
// } |
||||
|
} |
||||
|
} catch (final Exception e) { |
||||
|
Log.e(TAG, "", e); |
||||
|
} finally { |
||||
|
if (conn != null) { |
||||
|
conn.disconnect(); |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
public void onDownload(@NonNull final AppCompatActivity context) { |
||||
|
Utils.openURL(context, "https://f-droid.org/packages/me.austinhuang.instagrabber/"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,61 @@ |
|||||
|
package awais.instagrabber.utils; |
||||
|
|
||||
|
import android.content.Context; |
||||
|
import android.util.Log; |
||||
|
|
||||
|
import androidx.annotation.NonNull; |
||||
|
import androidx.annotation.Nullable; |
||||
|
|
||||
|
import java.net.HttpURLConnection; |
||||
|
import java.net.URL; |
||||
|
|
||||
|
public class UpdateChecker { |
||||
|
private static final Object LOCK = new Object(); |
||||
|
private static final String TAG = UpdateChecker.class.getSimpleName(); |
||||
|
|
||||
|
private static UpdateChecker instance; |
||||
|
|
||||
|
public static UpdateChecker getInstance() { |
||||
|
if (instance == null) { |
||||
|
synchronized (LOCK) { |
||||
|
if (instance == null) { |
||||
|
instance = new UpdateChecker(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return instance; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Needs to be called asynchronously |
||||
|
* |
||||
|
* @return the latest version from Github |
||||
|
*/ |
||||
|
@Nullable |
||||
|
public String getLatestVersion() { |
||||
|
HttpURLConnection conn = null; |
||||
|
try { |
||||
|
conn = (HttpURLConnection) new URL("https://github.com/austinhuang0131/barinsta/releases/latest").openConnection(); |
||||
|
conn.setInstanceFollowRedirects(false); |
||||
|
conn.setUseCaches(false); |
||||
|
conn.setRequestProperty("User-Agent", "https://Barinsta.AustinHuang.me / mailto:[email protected]"); |
||||
|
conn.connect(); |
||||
|
final int responseCode = conn.getResponseCode(); |
||||
|
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { |
||||
|
return "v" + conn.getHeaderField("Location").split("/v")[1]; |
||||
|
// return !version.equals(BuildConfig.VERSION_NAME); |
||||
|
} |
||||
|
} catch (final Exception e) { |
||||
|
Log.e(TAG, "", e); |
||||
|
} finally { |
||||
|
if (conn != null) { |
||||
|
conn.disconnect(); |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
public void onDownload(@NonNull final Context context) { |
||||
|
Utils.openURL(context, "https://github.com/austinhuang0131/instagrabber/releases/latest"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,39 @@ |
|||||
|
package awais.instagrabber.utils; |
||||
|
|
||||
|
import android.content.Context; |
||||
|
import android.content.DialogInterface; |
||||
|
|
||||
|
import androidx.annotation.NonNull; |
||||
|
|
||||
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
||||
|
|
||||
|
import awais.instagrabber.BuildConfig; |
||||
|
import awais.instagrabber.R; |
||||
|
|
||||
|
import static awais.instagrabber.utils.Utils.settingsHelper; |
||||
|
|
||||
|
public final class UpdateCheckCommon { |
||||
|
|
||||
|
public static boolean shouldShowUpdateDialog(final boolean force, |
||||
|
@NonNull final String version) { |
||||
|
final String skippedVersion = settingsHelper.getString(Constants.SKIPPED_VERSION); |
||||
|
return force || (!version.equals(BuildConfig.VERSION_NAME) && !BuildConfig.DEBUG && !skippedVersion |
||||
|
.equals(version)); |
||||
|
} |
||||
|
|
||||
|
public static void showUpdateDialog(@NonNull final Context context, |
||||
|
@NonNull final String version, |
||||
|
@NonNull final DialogInterface.OnClickListener onDownloadClickListener) { |
||||
|
AppExecutors.getInstance().mainThread().execute(() -> { |
||||
|
new MaterialAlertDialogBuilder(context) |
||||
|
.setTitle(context.getString(R.string.update_available, version)) |
||||
|
.setNeutralButton(R.string.skip_update, (dialog, which) -> { |
||||
|
settingsHelper.putString(Constants.SKIPPED_VERSION, version); |
||||
|
dialog.dismiss(); |
||||
|
}) |
||||
|
.setPositiveButton(R.string.action_download, onDownloadClickListener) |
||||
|
.setNegativeButton(R.string.cancel, null) |
||||
|
.show(); |
||||
|
}); |
||||
|
} |
||||
|
} |
@ -1,58 +0,0 @@ |
|||||
package awais.instagrabber.utils; |
|
||||
|
|
||||
import android.os.AsyncTask; |
|
||||
import android.util.Log; |
|
||||
|
|
||||
import androidx.annotation.NonNull; |
|
||||
|
|
||||
import org.json.JSONObject; |
|
||||
|
|
||||
import java.net.HttpURLConnection; |
|
||||
import java.net.URL; |
|
||||
|
|
||||
import awais.instagrabber.BuildConfig; |
|
||||
import awais.instagrabber.interfaces.FetchListener; |
|
||||
|
|
||||
public final class UpdateChecker extends AsyncTask<Void, Void, Boolean> { |
|
||||
private final FetchListener<String> fetchListener; |
|
||||
private String version; |
|
||||
|
|
||||
public UpdateChecker(final FetchListener<String> fetchListener) { |
|
||||
this.fetchListener = fetchListener; |
|
||||
} |
|
||||
|
|
||||
@NonNull |
|
||||
@Override |
|
||||
protected Boolean doInBackground(final Void... voids) { |
|
||||
try { |
|
||||
version = ""; |
|
||||
|
|
||||
HttpURLConnection conn = |
|
||||
(HttpURLConnection) new URL("https://f-droid.org/api/v1/packages/me.austinhuang.instagrabber").openConnection(); |
|
||||
conn.setUseCaches(false); |
|
||||
conn.setRequestProperty("User-Agent", "https://Barinsta.AustinHuang.me / mailto:[email protected]"); |
|
||||
conn.connect(); |
|
||||
|
|
||||
final int responseCode = conn.getResponseCode(); |
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) { |
|
||||
final JSONObject data = new JSONObject(NetworkUtils.readFromConnection(conn)); |
|
||||
if (BuildConfig.VERSION_CODE < data.getInt("suggestedVersionCode")) { |
|
||||
version = data.getJSONArray("packages").getJSONObject(0).getString("versionName"); |
|
||||
return true; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
conn.disconnect(); |
|
||||
} catch (final Exception e) { |
|
||||
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); |
|
||||
} |
|
||||
|
|
||||
return false; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void onPostExecute(final Boolean result) { |
|
||||
if (result != null && result && fetchListener != null) |
|
||||
fetchListener.onResult("v"+version); |
|
||||
} |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue