Skip to content

Commit f0267ee

Browse files
committed
add auto update check
1 parent be676d4 commit f0267ee

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

app/src/main/java/io/github/ratul/topactivity/ui/MainActivity.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import androidx.appcompat.widget.SwitchCompat;
5555
import androidx.core.content.ContextCompat;
5656

57-
import com.android.volley.RequestQueue;
5857
import com.android.volley.toolbox.JsonObjectRequest;
5958
import com.android.volley.toolbox.Volley;
6059

@@ -262,7 +261,6 @@ private boolean isUsageStatsGranted() {
262261
AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
263262
int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
264263
Process.myUid(), getPackageName());
265-
266264
if (mode == AppOpsManager.MODE_DEFAULT) {
267265
return checkCallingOrSelfPermission(PACKAGE_USAGE_STATS) == PERMISSION_GRANTED;
268266
}
@@ -277,10 +275,18 @@ private boolean isAccessibilityNotStarted() {
277275
}
278276

279277
private void checkForUpdate(boolean silent) {
280-
RequestQueue requestQueue = Volley.newRequestQueue(this);
281-
JSONObject jsonObject = new JSONObject();
282-
String url = "https://api.github.com/repos/codehasan/Current-Activity/releases/latest";
283-
JsonObjectRequest releasesRequest = new JsonObjectRequest(GET, url, jsonObject,
278+
try {
279+
Volley.newRequestQueue(this)
280+
.add(getVersionCheckRequest(silent));
281+
} catch (Throwable ignored) {
282+
handleErrorResponse(silent);
283+
}
284+
}
285+
286+
private JsonObjectRequest getVersionCheckRequest(boolean silent) {
287+
JsonObjectRequest request = new JsonObjectRequest(GET,
288+
"https://api.github.com/repos/codehasan/Current-Activity/releases/latest",
289+
null,
284290
response -> {
285291
try {
286292
processUpdateResponse(response);
@@ -289,10 +295,9 @@ private void checkForUpdate(boolean silent) {
289295
}
290296
},
291297
error -> handleErrorResponse(silent));
292-
releasesRequest.setShouldRetryConnectionErrors(true);
293-
releasesRequest.setShouldCache(false);
294-
295-
requestQueue.add(releasesRequest);
298+
request.setShouldRetryConnectionErrors(true);
299+
request.setShouldCache(false);
300+
return request;
296301
}
297302

298303
private void handleErrorResponse(boolean silent) {
@@ -307,7 +312,7 @@ private void processUpdateResponse(JSONObject response) throws Throwable {
307312
String serverVersion = tag.replaceAll("[^0-9]", "");
308313
String currentVersion = BuildConfig.VERSION_NAME.replaceAll("[^0-9]", "");
309314

310-
if (parseInt(serverVersion) != parseInt(currentVersion)) {
315+
if (parseInt(serverVersion) > parseInt(currentVersion)) {
311316
new AlertDialog.Builder(this)
312317
.setTitle("Update Available")
313318
.setMessage("A new version (" + tag + ") is available. Do you want to download it?")

0 commit comments

Comments
 (0)