6060public class MainActivity extends ActivityWithDialog {
6161 private static final int STORAGE_ACCESS_CODE = 1 ;
6262 private static final String ACTION_START_STOP_RECEIVED = "com.gianlu.aria2android.START_STOP_RECEIVED" ;
63- private boolean isRunning ;
63+ private volatile boolean isRunning ;
6464 private ServiceBroadcastReceiver receiver ;
6565 private Messenger serviceMessenger ;
66- private ToggleButton toggleServer ;
67- private MaterialEditTextPreference outputPath ;
68- private MaterialPreferenceCategory generalCategory ;
69- private MaterialPreferenceCategory rpcCategory ;
70- private MaterialPreferenceCategory notificationsCategory ;
7166 private final ServiceConnection serviceConnection = new ServiceConnection () {
7267 @ Override
7368 public void onServiceConnected (ComponentName componentName , IBinder iBinder ) {
7469 serviceMessenger = new Messenger (iBinder );
75- checkIntentAction (getIntent ());
7670 askForStatus ();
7771 }
7872
@@ -81,6 +75,11 @@ public void onServiceDisconnected(ComponentName componentName) {
8175 serviceMessenger = null ;
8276 }
8377 };
78+ private ToggleButton toggleServer ;
79+ private MaterialEditTextPreference outputPath ;
80+ private MaterialPreferenceCategory generalCategory ;
81+ private MaterialPreferenceCategory rpcCategory ;
82+ private MaterialPreferenceCategory notificationsCategory ;
8483 private LinearLayout logsContainer ;
8584 private MessageView logsMessage ;
8685
@@ -95,17 +94,16 @@ private static boolean isARM() {
9594 @ Override
9695 protected void onResume () {
9796 super .onResume ();
98-
9997 askForStatus ();
10098 }
10199
102100 private void askForStatus () {
103- if (serviceMessenger == null ) return ;
104-
105- try {
106- serviceMessenger . send ( Message . obtain ( null , BinService . STATUS , null ));
107- } catch ( RemoteException ex ) {
108- Logging . log ( ex );
101+ if (serviceMessenger != null ) {
102+ try {
103+ serviceMessenger . send ( Message . obtain ( null , BinService . STATUS , null ));
104+ } catch ( RemoteException ex ) {
105+ Logging . log ( ex );
106+ }
109107 }
110108 }
111109
@@ -333,6 +331,7 @@ protected void onCreate(Bundle savedInstanceState) {
333331 version .setText (BinUtils .binVersion (this ));
334332
335333 backwardCompatibility ();
334+ checkIntentAction (getIntent ());
336335 }
337336
338337 @ Override
@@ -374,17 +373,6 @@ private void backwardCompatibility() {
374373 }
375374 }
376375
377- @ Override
378- protected void onDestroy () {
379- try {
380- unbindService (serviceConnection );
381- } catch (IllegalArgumentException ex ) {
382- Logging .log (ex );
383- }
384-
385- super .onDestroy ();
386- }
387-
388376 private boolean startService () {
389377 Prefs .putLong (PK .CURRENT_SESSION_START , System .currentTimeMillis ());
390378 AnalyticsApplication .sendAnalytics (Utils .ACTION_TURN_ON );
@@ -430,22 +418,20 @@ public void askRationale(@NonNull AlertDialog.Builder builder) {
430418 receiver = new ServiceBroadcastReceiver ();
431419 LocalBroadcastManager .getInstance (this ).registerReceiver (receiver , filter );
432420 try {
433- if (serviceMessenger != null ) {
434- serviceMessenger .send (Message .obtain (null , BinService .START , StartConfig .fromPrefs ()));
435- return true ;
436- } else {
437- bindService (new Intent (this , BinService .class ), serviceConnection , BIND_AUTO_CREATE );
438- return false ;
439- }
421+ bindService (new Intent (this , BinService .class ), serviceConnection , BIND_AUTO_CREATE );
422+ startService (new Intent (this , BinService .class )
423+ .setAction (BinService .ACTION_START_SERVICE )
424+ .putExtra ("config" , StartConfig .fromPrefs ()));
425+ return true ;
440426 } catch (JSONException ex ) {
441427 Toaster .with (this ).message (R .string .failedLoadingOptions ).ex (ex ).show ();
442428 LocalBroadcastManager .getInstance (this ).unregisterReceiver (receiver );
443429 return false ;
444- } catch (RemoteException ex ) {
430+ } /* catch (RemoteException ex) {
445431 Toaster.with(this).message(R.string.failedStarting).ex(ex).show();
446432 LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
447433 return false;
448- }
434+ } */
449435 }
450436
451437 @ Override
@@ -454,16 +440,29 @@ protected void onStart() {
454440 bindService (new Intent (this , BinService .class ), serviceConnection , BIND_AUTO_CREATE );
455441 }
456442
443+ @ Override
444+ protected void onDestroy () {
445+ super .onDestroy ();
446+
447+ try {
448+ unbindService (serviceConnection );
449+ } catch (IllegalArgumentException ex ) {
450+ Logging .log (ex );
451+ }
452+ }
453+
457454 private boolean stopService () {
458455 if (serviceMessenger == null ) return true ;
459456
460457 try {
461- serviceMessenger .send (Message .obtain (null , BinService .STOP , null ));
462- } catch (RemoteException ex ) {
463- Toaster .with (this ).message (R .string .failedStopping ).ex (ex ).show ();
464- return false ;
458+ unbindService (serviceConnection );
459+ } catch (IllegalArgumentException ex ) {
460+ Logging .log (ex );
465461 }
466462
463+ startService (new Intent (this , BinService .class )
464+ .setAction (BinService .ACTION_STOP_SERVICE ));
465+
467466 Bundle bundle = null ;
468467 if (Prefs .getLong (PK .CURRENT_SESSION_START , -1 ) != -1 ) {
469468 bundle = new Bundle ();
@@ -572,11 +571,12 @@ private class ServiceBroadcastReceiver extends BroadcastReceiver {
572571
573572 @ Override
574573 public void onReceive (Context context , final Intent intent ) {
575- final BinService .Action action = BinService .Action .find (intent );
574+ BinService .Action action = BinService .Action .find (intent );
576575 if (action != null && intent != null ) {
577576 runOnUiThread (() -> {
578577 switch (action ) {
579578 case SERVER_STATUS :
579+ System .out .println ("UPDATE!!" );
580580 updateUiStatus (intent .getBooleanExtra ("on" , false ));
581581 break ;
582582 case SERVER_START :
0 commit comments