2121import static android .content .pm .PackageManager .PERMISSION_GRANTED ;
2222import static com .android .volley .Request .Method .GET ;
2323import static java .lang .Integer .parseInt ;
24- import static io .github .ratul .topactivity .App .showToast ;
2524import static io .github .ratul .topactivity .utils .AutostartUtil .isAutoStartPermissionAvailable ;
2625import static io .github .ratul .topactivity .utils .AutostartUtil .requestAutoStartPermission ;
2726
3938import android .os .Process ;
4039import android .provider .Settings ;
4140import android .util .DisplayMetrics ;
42- import android .view .Menu ;
43- import android .view .MenuItem ;
4441import android .view .View ;
4542import android .view .WindowInsets ;
4643import android .view .WindowMetrics ;
4744import android .widget .Button ;
48- import android .widget .EditText ;
4945import android .widget .LinearLayout ;
5046import android .widget .TextView ;
5147
5450import androidx .activity .result .contract .ActivityResultContracts ;
5551import androidx .appcompat .app .AlertDialog ;
5652import androidx .appcompat .app .AppCompatActivity ;
57- import androidx .appcompat .widget .SwitchCompat ;
53+ import androidx .coordinatorlayout .widget .CoordinatorLayout ;
5854import androidx .core .content .ContextCompat ;
5955import androidx .core .graphics .Insets ;
6056import androidx .core .view .ViewCompat ;
6157import androidx .core .view .WindowInsetsCompat ;
6258
6359import com .android .volley .toolbox .JsonObjectRequest ;
6460import com .android .volley .toolbox .Volley ;
61+ import com .google .android .material .appbar .MaterialToolbar ;
62+ import com .google .android .material .dialog .MaterialAlertDialogBuilder ;
63+ import com .google .android .material .divider .MaterialDivider ;
64+ import com .google .android .material .materialswitch .MaterialSwitch ;
65+ import com .google .android .material .snackbar .Snackbar ;
66+ import com .google .android .material .textfield .TextInputLayout ;
6567
6668import org .json .JSONObject ;
6769
7072import io .github .ratul .topactivity .receivers .NotificationReceiver ;
7173import io .github .ratul .topactivity .services .AccessibilityMonitoringService ;
7274import io .github .ratul .topactivity .services .PackageMonitoringService ;
73- import io .github .ratul .topactivity .utils .AutostartUtil ;
7475import io .github .ratul .topactivity .utils .DatabaseUtil ;
7576import io .github .ratul .topactivity .utils .WindowUtil ;
7677
@@ -82,7 +83,8 @@ public class MainActivity extends AppCompatActivity {
8283 public static final String ACTION_STATE_CHANGED = "io.github.ratul.topactivity.ACTION_STATE_CHANGED" ;
8384 public static final String EXTRA_FROM_QS_TILE = "from_qs_tile" ;
8485 private ActivityResultLauncher <String > notificationPermissionLauncher ;
85- private SwitchCompat showWindow , showNotification , useAccessibility ;
86+ private CoordinatorLayout baseView ;
87+ private MaterialSwitch showWindow , showNotification , useAccessibility ;
8688 private BroadcastReceiver updateReceiver ;
8789 private PackageMonitoringService monitoringService ;
8890 private boolean isServiceBound = false ;
@@ -117,7 +119,8 @@ protected void onCreate(Bundle savedInstanceState) {
117119 super .onCreate (savedInstanceState );
118120 EdgeToEdge .enable (this );
119121 setContentView (R .layout .activity_main );
120- ViewCompat .setOnApplyWindowInsetsListener (findViewById (R .id .main ), (v , insets ) -> {
122+ baseView = findViewById (R .id .main );
123+ ViewCompat .setOnApplyWindowInsetsListener (baseView , (v , insets ) -> {
121124 Insets systemBars = insets .getInsets (WindowInsetsCompat .Type .systemBars ());
122125 v .setPadding (systemBars .left , systemBars .top , systemBars .right , systemBars .bottom );
123126 return insets ;
@@ -132,14 +135,15 @@ protected void onCreate(Bundle savedInstanceState) {
132135 DatabaseUtil .setShowingWindow (isWindowActuallyShowing );
133136 }
134137
138+ MaterialToolbar toolbar = findViewById (R .id .topAppBar );
135139 showWindow = findViewById (R .id .show_window );
136140 showNotification = findViewById (R .id .show_notification );
137141 useAccessibility = findViewById (R .id .use_accessibility );
138142 Button downloadAccessibility = findViewById (R .id .download_accessibility );
139143 Button configureWidth = findViewById (R .id .configure_width );
140144
141145 LinearLayout autostartLayout = findViewById (R .id .autostart_layout );
142- TextView autostartDivider = findViewById (R .id .autostart_divider );
146+ MaterialDivider autostartDivider = findViewById (R .id .autostart_divider );
143147 Button allowAutostart = findViewById (R .id .allow_autostart );
144148
145149 updateReceiver = new UpdateSwitchReceiver ();
@@ -194,6 +198,18 @@ protected void onCreate(Bundle savedInstanceState) {
194198
195199 configureWidth .setOnClickListener (v -> configureWidth ());
196200
201+ toolbar .setOnMenuItemClickListener (item -> {
202+ if (item .getItemId () == R .id .github ) {
203+ openLink ("https://github.com/codehasan/Current-Activity" );
204+ return true ;
205+ } else if (item .getItemId () == R .id .check_update ) {
206+ showToast ("Checking for update" );
207+ checkForUpdate (false );
208+ return true ;
209+ }
210+ return false ;
211+ });
212+
197213 if (isAutoStartPermissionAvailable (this )) {
198214 autostartLayout .setVisibility (View .VISIBLE );
199215 autostartDivider .setVisibility (View .VISIBLE );
@@ -225,23 +241,6 @@ protected void onResume() {
225241 refreshAccessibilitySwitch ();
226242 }
227243
228- @ Override
229- public boolean onCreateOptionsMenu (Menu menu ) {
230- getMenuInflater ().inflate (R .menu .menu_main , menu );
231- return super .onCreateOptionsMenu (menu );
232- }
233-
234- @ Override
235- public boolean onOptionsItemSelected (MenuItem item ) {
236- if (item .getItemId () == R .id .github ) {
237- openLink ("https://github.com/codehasan/Current-Activity" );
238- } else if (item .getItemId () == R .id .check_update ) {
239- showToast (this , "Checking for update" );
240- checkForUpdate (false );
241- }
242- return true ;
243- }
244-
245244 @ Override
246245 protected void onDestroy () {
247246 unregisterReceiver (updateReceiver );
@@ -317,7 +316,7 @@ private JsonObjectRequest getVersionCheckRequest(boolean silent) {
317316 null ,
318317 response -> {
319318 try {
320- processUpdateResponse (response );
319+ processUpdateResponse (response , silent );
321320 } catch (Throwable ignored ) {
322321 handleErrorResponse (silent );
323322 }
@@ -330,18 +329,18 @@ private JsonObjectRequest getVersionCheckRequest(boolean silent) {
330329
331330 private void handleErrorResponse (boolean silent ) {
332331 if (!silent ) {
333- showToast (this , "Failed to check for update" );
332+ showToast ("Failed to check for update" );
334333 openLink ("https://github.com/codehasan/Current-Activity/releases" );
335334 }
336335 }
337336
338- private void processUpdateResponse (JSONObject response ) throws Throwable {
337+ private void processUpdateResponse (JSONObject response , boolean silent ) throws Throwable {
339338 String tag = response .getString ("tag_name" );
340339 String serverVersion = tag .replaceAll ("[^0-9]" , "" );
341340 String currentVersion = BuildConfig .VERSION_NAME .replaceAll ("[^0-9]" , "" );
342341
343342 if (parseInt (serverVersion ) > parseInt (currentVersion )) {
344- new AlertDialog . Builder (this )
343+ new MaterialAlertDialogBuilder (this )
345344 .setTitle ("Update Available" )
346345 .setMessage ("A new version (" + tag + ") is available. Do you want to download it?" )
347346 .setPositiveButton ("Download" , (dialog , which ) -> {
@@ -350,23 +349,25 @@ private void processUpdateResponse(JSONObject response) throws Throwable {
350349 })
351350 .setNeutralButton ("Cancel" , (dialog , which ) -> dialog .dismiss ())
352351 .show ();
352+ } else if (!silent ) {
353+ showToast ("Already on the latest version" );
353354 }
354355 }
355356
356357 private void configureWidth () {
357358 View dialogView = getLayoutInflater ().inflate (R .layout .content_configure_width , null );
358- EditText widthInput = dialogView .findViewById (R .id .width );
359+ TextInputLayout widthInput = dialogView .findViewById (R .id .width );
359360 TextView helperText = dialogView .findViewById (R .id .helper );
360361
361362 int screenWidth = getScreenWidth ();
362363 int userWidth = DatabaseUtil .getUserWidth ();
363364
364365 if (userWidth != -1 ) {
365- widthInput .setText (String .valueOf (userWidth ));
366+ widthInput .getEditText (). setText (String .valueOf (userWidth ));
366367 }
367368 helperText .append ("enter a width between 500 and " + screenWidth + "." );
368369
369- AlertDialog alertDialog = new AlertDialog . Builder (this )
370+ AlertDialog alertDialog = new MaterialAlertDialogBuilder (this )
370371 .setTitle ("Configure Width" )
371372 .setView (dialogView )
372373 .setNeutralButton ("Cancel" , (dialog , which ) -> dialog .dismiss ())
@@ -376,12 +377,12 @@ private void configureWidth() {
376377 alertDialog .setOnShowListener (dialog -> {
377378 Button saveButton = alertDialog .getButton (AlertDialog .BUTTON_POSITIVE );
378379 saveButton .setOnClickListener (v -> {
379- String input = widthInput .getText ().toString ();
380+ String input = widthInput .getEditText (). getText ().toString ();
380381
381382 if (input .trim ().isEmpty ()) {
382383 DatabaseUtil .setUserWidth (-1 );
383384 dialog .dismiss ();
384- showToast (this , "Saved" );
385+ showToast ("Saved" );
385386 return ;
386387 }
387388
@@ -396,7 +397,7 @@ private void configureWidth() {
396397
397398 DatabaseUtil .setUserWidth (width );
398399 dialog .dismiss ();
399- showToast (this , "Saved" );
400+ showToast ("Saved" );
400401 });
401402 });
402403
@@ -447,7 +448,7 @@ private void requestNotificationPermission() {
447448
448449 private void requestSystemOverlayPermission () {
449450 if (!Settings .canDrawOverlays (this )) {
450- new AlertDialog . Builder (this )
451+ new MaterialAlertDialogBuilder (this )
451452 .setTitle ("System Overlay" )
452453 .setMessage ("Please allow draw over other apps permission for 'Current Activity'" )
453454 .setPositiveButton ("Settings" , (dialog , which ) -> {
@@ -456,35 +457,42 @@ private void requestSystemOverlayPermission() {
456457 startActivity (intent );
457458 dialog .dismiss ();
458459 })
460+ .setNeutralButton ("Cancel" , (dialog , which ) -> dialog .dismiss ())
459461 .show ();
460462 }
461463 }
462464
463465 private void requestCommonPermissions () {
464466 if (isAccessibilityNotStarted ()) {
465- new AlertDialog . Builder (this )
467+ new MaterialAlertDialogBuilder (this )
466468 .setTitle ("Accessibility Permission" )
467469 .setMessage ("Please enable Accessibility Service for 'Current Activity'" )
468470 .setPositiveButton ("Settings" , (dialog , button ) -> {
469471 startActivity (new Intent (Settings .ACTION_ACCESSIBILITY_SETTINGS ));
470472 dialog .dismiss ();
471473 })
474+ .setNeutralButton ("Cancel" , (dialog , which ) -> dialog .dismiss ())
472475 .show ();
473476 }
474477
475478 if (!isUsageStatsGranted ()) {
476- new AlertDialog . Builder (this )
479+ new MaterialAlertDialogBuilder (this )
477480 .setTitle ("Usage Access" )
478481 .setMessage ("Please allow Usage Access permission for 'Current Activity'" )
479482 .setPositiveButton ("Settings" , (di , btn ) -> {
480483 startActivity (new Intent (Settings .ACTION_USAGE_ACCESS_SETTINGS ));
481484 di .dismiss ();
482485 })
486+ .setNeutralButton ("Cancel" , (dialog , which ) -> dialog .dismiss ())
483487 .show ();
484488 }
485489 }
486490
487491 private void openLink (String link ) {
488492 startActivity (new Intent (Intent .ACTION_VIEW , Uri .parse (link )));
489493 }
494+
495+ private void showToast (String message ) {
496+ Snackbar .make (baseView , message , Snackbar .LENGTH_SHORT ).show ();
497+ }
490498}
0 commit comments