Skip to content

Commit aa1f4a9

Browse files
committed
add update check login
1 parent f8692b1 commit aa1f4a9

File tree

1 file changed

+70
-28
lines changed

1 file changed

+70
-28
lines changed

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

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import static android.Manifest.permission.PACKAGE_USAGE_STATS;
2020
import static android.Manifest.permission.POST_NOTIFICATIONS;
2121
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
22-
import static io.github.ratul.topactivity.utils.NullSafety.isNullOrEmpty;
22+
import static com.android.volley.Request.Method.GET;
23+
import static java.lang.Integer.parseInt;
24+
import static io.github.ratul.topactivity.App.showToast;
2325

2426
import android.app.AppOpsManager;
2527
import android.content.BroadcastReceiver;
@@ -52,7 +54,12 @@
5254
import androidx.appcompat.widget.SwitchCompat;
5355
import androidx.core.content.ContextCompat;
5456

55-
import io.github.ratul.topactivity.App;
57+
import com.android.volley.RequestQueue;
58+
import com.android.volley.toolbox.JsonObjectRequest;
59+
import com.android.volley.toolbox.Volley;
60+
61+
import org.json.JSONObject;
62+
5663
import io.github.ratul.topactivity.BuildConfig;
5764
import io.github.ratul.topactivity.R;
5865
import io.github.ratul.topactivity.receivers.NotificationReceiver;
@@ -69,8 +76,8 @@ public class MainActivity extends AppCompatActivity {
6976
public static final String ACTION_STATE_CHANGED = "io.github.ratul.topactivity.ACTION_STATE_CHANGED";
7077
public static final String EXTRA_FROM_QS_TILE = "from_qs_tile";
7178
private ActivityResultLauncher<String> notificationPermissionLauncher;
72-
private BroadcastReceiver updateReceiver;
7379
private SwitchCompat showWindow, showNotification, useAccessibility;
80+
private BroadcastReceiver updateReceiver;
7481
private PackageMonitoringService monitoringService;
7582
private boolean isServiceBound = false;
7683

@@ -103,6 +110,7 @@ public void onReceive(Context context, Intent intent) {
103110
protected void onCreate(Bundle savedInstanceState) {
104111
super.onCreate(savedInstanceState);
105112
setContentView(R.layout.activity_main);
113+
checkForUpdate(true);
106114
startAccessibilityService();
107115
DatabaseUtil.setDisplayWidth(getScreenWidth());
108116

@@ -163,11 +171,8 @@ protected void onCreate(Bundle savedInstanceState) {
163171
});
164172

165173
downloadAccessibility.setOnClickListener(v -> {
166-
Intent intent = new Intent(Intent.ACTION_VIEW)
167-
.setData(Uri.parse(
168-
"https://github.com/codehasan/Current-Activity/releases/tag/v"
169-
+ BuildConfig.VERSION_NAME));
170-
startActivity(intent);
174+
openLink("https://github.com/codehasan/Current-Activity/releases/tag/v"
175+
+ BuildConfig.VERSION_NAME);
171176
});
172177

173178
configureWidth.setOnClickListener(v -> configureWidth());
@@ -196,28 +201,17 @@ protected void onResume() {
196201

197202
@Override
198203
public boolean onCreateOptionsMenu(Menu menu) {
199-
menu.add("GitHub Repo")
200-
.setIcon(R.drawable.ic_github)
201-
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
202-
menu.add("Check for Update");
204+
getMenuInflater().inflate(R.menu.menu_main, menu);
203205
return super.onCreateOptionsMenu(menu);
204206
}
205207

206208
@Override
207209
public boolean onOptionsItemSelected(MenuItem item) {
208-
CharSequence title = item.getTitle();
209-
if (isNullOrEmpty(title)) return true;
210-
211-
Intent intent = new Intent(Intent.ACTION_VIEW);
212-
switch (title.toString()) {
213-
case "GitHub Repo":
214-
intent.setData(Uri.parse("https://github.com/codehasan/Current-Activity"));
215-
startActivity(intent);
216-
break;
217-
case "Check for Update":
218-
intent.setData(Uri.parse("https://github.com/codehasan/Current-Activity/releases"));
219-
startActivity(intent);
220-
break;
210+
if (item.getItemId() == R.id.github) {
211+
openLink("https://github.com/codehasan/Current-Activity");
212+
} else if (item.getItemId() == R.id.check_update) {
213+
showToast(this, "Checking for update");
214+
checkForUpdate(false);
221215
}
222216
return true;
223217
}
@@ -282,6 +276,50 @@ private boolean isAccessibilityNotStarted() {
282276
AccessibilityMonitoringService.getInstance() == null;
283277
}
284278

279+
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,
284+
response -> {
285+
try {
286+
processUpdateResponse(response);
287+
} catch (Throwable ignored) {
288+
handleErrorResponse(silent);
289+
}
290+
},
291+
error -> handleErrorResponse(silent));
292+
releasesRequest.setShouldRetryConnectionErrors(true);
293+
releasesRequest.setShouldCache(false);
294+
295+
requestQueue.add(releasesRequest);
296+
}
297+
298+
private void handleErrorResponse(boolean silent) {
299+
if (!silent) {
300+
showToast(this, "Failed to check for update");
301+
openLink("https://github.com/codehasan/Current-Activity/releases");
302+
}
303+
}
304+
305+
private void processUpdateResponse(JSONObject response) throws Throwable {
306+
String tag = response.getString("tag_name");
307+
String serverVersion = tag.replaceAll("[^0-9]", "");
308+
String currentVersion = BuildConfig.VERSION_NAME.replaceAll("[^0-9]", "");
309+
310+
if (parseInt(serverVersion) != parseInt(currentVersion)) {
311+
new AlertDialog.Builder(this)
312+
.setTitle("Update Available")
313+
.setMessage("A new version (" + tag + ") is available. Do you want to download it?")
314+
.setPositiveButton("Download", (dialog, which) -> {
315+
openLink("https://github.com/codehasan/Current-Activity/releases/tag/" + tag);
316+
dialog.dismiss();
317+
})
318+
.setNeutralButton("Cancel", (dialog, which) -> dialog.dismiss())
319+
.show();
320+
}
321+
}
322+
285323
private void configureWidth() {
286324
View dialogView = getLayoutInflater().inflate(R.layout.content_configure_width, null);
287325
EditText widthInput = dialogView.findViewById(R.id.width);
@@ -310,11 +348,11 @@ private void configureWidth() {
310348
if (input.trim().isEmpty()) {
311349
DatabaseUtil.setUserWidth(-1);
312350
dialog.dismiss();
313-
App.showToast(this, "Saved");
351+
showToast(this, "Saved");
314352
return;
315353
}
316354

317-
int width = Integer.parseInt(input);
355+
int width = parseInt(input);
318356
if (width < 500) {
319357
widthInput.setError("Width should be greater than 500");
320358
return;
@@ -325,7 +363,7 @@ private void configureWidth() {
325363

326364
DatabaseUtil.setUserWidth(width);
327365
dialog.dismiss();
328-
App.showToast(this, "Saved");
366+
showToast(this, "Saved");
329367
});
330368
});
331369

@@ -412,4 +450,8 @@ private void requestCommonPermissions() {
412450
.show();
413451
}
414452
}
453+
454+
private void openLink(String link) {
455+
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
456+
}
415457
}

0 commit comments

Comments
 (0)