mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-11-26 06:11:19 +00:00
Allow stopping the service manually when running as a service in extraordinary cases to avoid the user has to reboot the phone (fixes #63)
This commit is contained in:
parent
0ed501c362
commit
0203aebc40
2 changed files with 35 additions and 13 deletions
|
@ -1,5 +1,6 @@
|
|||
package com.nutomic.syncthingandroid.fragments;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
|
@ -42,7 +43,7 @@ public class DrawerFragment extends Fragment implements SyncthingService.OnServi
|
|||
private TextView mDrawerActionImportExport;
|
||||
private TextView mDrawerActionRestart;
|
||||
private TextView mDrawerActionSettings;
|
||||
private TextView mExitButton;
|
||||
private TextView mDrawerActionExit;
|
||||
|
||||
private MainActivity mActivity;
|
||||
private SharedPreferences sharedPreferences = null;
|
||||
|
@ -84,7 +85,7 @@ public class DrawerFragment extends Fragment implements SyncthingService.OnServi
|
|||
mDrawerActionImportExport = view.findViewById(R.id.drawerActionImportExport);
|
||||
mDrawerActionRestart = view.findViewById(R.id.drawerActionRestart);
|
||||
mDrawerActionSettings = view.findViewById(R.id.drawerActionSettings);
|
||||
mExitButton = view.findViewById(R.id.drawerActionExit);
|
||||
mDrawerActionExit = view.findViewById(R.id.drawerActionExit);
|
||||
|
||||
// Add listeners to buttons.
|
||||
mDrawerActionShowQrCode.setOnClickListener(this);
|
||||
|
@ -92,7 +93,7 @@ public class DrawerFragment extends Fragment implements SyncthingService.OnServi
|
|||
mDrawerActionImportExport.setOnClickListener(this);
|
||||
mDrawerActionRestart.setOnClickListener(this);
|
||||
mDrawerActionSettings.setOnClickListener(this);
|
||||
mExitButton.setOnClickListener(this);
|
||||
mDrawerActionExit.setOnClickListener(this);
|
||||
|
||||
updateLabels();
|
||||
updateButtons();
|
||||
|
@ -123,13 +124,7 @@ public class DrawerFragment extends Fragment implements SyncthingService.OnServi
|
|||
mDrawerActionShowQrCode.setVisibility(synthingRunning ? View.VISIBLE : View.GONE);
|
||||
mDrawerActionWebGui.setVisibility(synthingRunning ? View.VISIBLE : View.GONE);
|
||||
mDrawerActionRestart.setVisibility(synthingRunning ? View.VISIBLE : View.GONE);
|
||||
|
||||
// Do not show the exit button if our app runs as a background service.
|
||||
mExitButton.setVisibility(
|
||||
sharedPreferences.getBoolean(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND, false) ?
|
||||
View.GONE :
|
||||
View.VISIBLE
|
||||
);
|
||||
mDrawerActionExit.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -179,9 +174,23 @@ public class DrawerFragment extends Fragment implements SyncthingService.OnServi
|
|||
mActivity.closeDrawer();
|
||||
break;
|
||||
case R.id.drawerActionExit:
|
||||
Log.i(TAG, "Exiting app on user request");
|
||||
mActivity.stopService(new Intent(mActivity, SyncthingService.class));
|
||||
mActivity.finish();
|
||||
if (sharedPreferences != null && sharedPreferences.getBoolean(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND, false)) {
|
||||
/**
|
||||
* App is running as a service. Show an explanation why exiting syncthing is an
|
||||
* extraordinary request, then ask the user to confirm.
|
||||
*/
|
||||
AlertDialog mExitConfirmationDialog = new AlertDialog.Builder(mActivity)
|
||||
.setTitle(R.string.dialog_exit_while_running_as_service_title)
|
||||
.setMessage(R.string.dialog_exit_while_running_as_service_message)
|
||||
.setPositiveButton(R.string.yes, (d, i) -> {
|
||||
doExit();
|
||||
})
|
||||
.setNegativeButton(R.string.no, (d, i) -> {})
|
||||
.show();
|
||||
} else {
|
||||
// App is not running as a service.
|
||||
doExit();
|
||||
}
|
||||
mActivity.closeDrawer();
|
||||
break;
|
||||
case R.id.drawerActionShowQrCode:
|
||||
|
@ -189,4 +198,13 @@ public class DrawerFragment extends Fragment implements SyncthingService.OnServi
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void doExit() {
|
||||
if (mActivity == null || mActivity.isFinishing()) {
|
||||
return;
|
||||
}
|
||||
Log.i(TAG, "Exiting app on user request");
|
||||
mActivity.stopService(new Intent(mActivity, SyncthingService.class));
|
||||
mActivity.finish();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,6 +48,10 @@ Please report any problems you encounter via Github.</string>
|
|||
|
||||
|
||||
|
||||
<!-- Title of the exit app when running as a service confirmation dialog -->
|
||||
<string name="dialog_exit_while_running_as_service_title">Confirm to stop service</string>
|
||||
<string name="dialog_exit_while_running_as_service_message">For your consideration: You configured Syncthing to run as a service. Therefore it monitors run conditions and syncs at any time in the background when conditions match. You should only stop the service manually if you ran into severe problems. Otherwise, uncheck running as a service in the settings. Would you like to stop the service now until the next device reboot?</string>
|
||||
|
||||
<!-- Title of the "add folder" menu action -->
|
||||
<string name="add_folder">Add Folder</string>
|
||||
|
||||
|
|
Loading…
Reference in a new issue