Skip to content

Commit

Permalink
WIP: Implementing proper restarts and replaces of services
Browse files Browse the repository at this point in the history
  • Loading branch information
0xf104a committed Dec 3, 2023
1 parent 4ddcc14 commit 07882c0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ public void startService(Context context){

public void stopService(Context context){
Log.i(TAG, "Stopping service...");
if(getServiceClass() == null){
Class<?> serviceClass = getServiceClass();
Log.d(TAG, "Class: " + serviceClass);
if(serviceClass == null){
Log.w(TAG, "Can not stop service: we do not know its class");
return;
}
context.stopService(new Intent(context, getServiceClass()));
context.stopService(new Intent(context, serviceClass));
}

public void bindService(Context context, ServiceConnection connection){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public class NotificationWebsocketService extends Service
@Override
public void onCreate(){
mBinder = new NotificationServiceBinder(this);
mNotificationController = new NotificationController(this, mServiceSettings);
startForeground(1, mNotificationController.getServiceNotification());
mServiceSettings = new ServiceSettings(this);
mAPI = mServiceSettings.getAPIFromSettings();
mNotificationController = new NotificationController(this, mServiceSettings);
mStatusController = new StatusController(this);
mConnectionController = new ConnectionController(mServiceSettings);
mStatusController.addComponent(NotificationServiceComponents.SERVICE_COMPONENT_CONNECTION,
Expand All @@ -43,7 +44,6 @@ public void onCreate(){
mConnectionController.setConnectionStatusListener(this, this);
Thread wsThread = new Thread(this::startListening);
wsThread.start();
startForeground(1, mNotificationController.getServiceNotification());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class NotificationServiceConnection implements ServiceConnection {
private final String TAG = "SettingsActivity.NotificationServiceConnection";
private final SettingsActivity.SettingsFragment settings;
private NotificationServiceBinder mService;
private ServiceSettings mServiceSettings;
public boolean isConnected = false;

public NotificationServiceConnection(SettingsActivity.SettingsFragment _settings) {
Expand Down Expand Up @@ -158,9 +157,14 @@ public void startNotificationService() {
}

private boolean isNotificationServiceRunning() {
Class<?> serviceClass = mServiceController.getServiceClass();
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<?> services = manager.getRunningServices(Integer.MAX_VALUE);
return (services.size() > 0);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

private void updateNotificationServiceStatus(SettingsFragment settings) {
Expand Down Expand Up @@ -358,11 +362,15 @@ private void setSSOPreferencesState(){
Log.wtf(TAG, "login_sso preference is null!");
throw new NullPointerException();
}
if(getBoolPreference(ServiceSettingConfig.USE_WEBSOCKET, false)){
login_sso.setEnabled(false);
}
if(getBoolPreference("sso_enabled",false)){
findPreference("server").setEnabled(false);
findPreference("password").setEnabled(false);
findPreference("login").setEnabled(false);
findPreference("insecure_connection").setEnabled(false);
findPreference("use_websocket").setEnabled(false);
login_sso.setSummary("Stop using Nextcloud app for authentication");
login_sso.setTitle("Log out from Nexcloud");
login_sso.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
Expand All @@ -377,6 +385,7 @@ public boolean onPreferenceClick(Preference preference) {
findPreference("password").setEnabled(true);
findPreference("login").setEnabled(true);
findPreference("insecure_connection").setEnabled(true);
findPreference("use_websocket").setEnabled(true);
login_sso.setSummary("Use on-device Nextcloud account");
login_sso.setTitle("Log in via Nextcloud app");
login_sso.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
Expand Down Expand Up @@ -536,13 +545,25 @@ public void onResume() {
Log.d(TAG, "onResume called");
}

private void onWebsocketStatusChanged(){
if(getBoolPreference(ServiceSettingConfig.USE_WEBSOCKET, false)) {
disableSSO();
findPreference("login_sso").setEnabled(false);
} else {
findPreference("login_sso").setEnabled(true);
}
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SettingsActivity activity = (SettingsActivity) getActivity();
if(activity == null){
Log.wtf(TAG, "Activity can not be null!");
throw new NullPointerException();
}
if(key.equals(ServiceSettingConfig.USE_WEBSOCKET)){
onWebsocketStatusChanged();
}
activity.onPreferencesChanges(key);
}
}
Expand Down

0 comments on commit 07882c0

Please sign in to comment.