Browse Source
Merge pull request #900 from ammargitham/per-flavor-update-check
Merge pull request #900 from ammargitham/per-flavor-update-check
Update checker per flavorrenovate/org.robolectric-robolectric-4.x
Austin Huang
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 220 additions and 147 deletions
-
3app/build.gradle
-
64app/src/fdroid/java/awais/instagrabber/utils/UpdateChecker.java
-
61app/src/github/java/awais/instagrabber/utils/UpdateChecker.java
-
3app/src/main/java/awais/instagrabber/activities/MainActivity.java
-
5app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java
-
1app/src/main/java/awais/instagrabber/utils/Constants.java
-
118app/src/main/java/awais/instagrabber/utils/FlavorTown.java
-
38app/src/main/java/awais/instagrabber/utils/UpdateCheckCommon.java
-
58app/src/main/java/awais/instagrabber/utils/UpdateChecker.java
-
2app/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,38 @@ |
|||||
|
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 || (!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