From d99fa7e86fbb799f52bc955e77850fd3ed77793c Mon Sep 17 00:00:00 2001 From: Marvin W Date: Wed, 8 Feb 2017 14:14:08 +0100 Subject: [PATCH] Directly start/stop GCM service if setting is changed in UI --- .../src/main/java/org/microg/gms/gcm/GcmPrefs.java | 2 +- .../src/main/java/org/microg/gms/gcm/McsService.java | 11 +++++++++-- .../src/main/java/org/microg/gms/ui/GcmFragment.java | 7 +++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/play-services-core/src/main/java/org/microg/gms/gcm/GcmPrefs.java b/play-services-core/src/main/java/org/microg/gms/gcm/GcmPrefs.java index 5ede7317..41c54c0f 100644 --- a/play-services-core/src/main/java/org/microg/gms/gcm/GcmPrefs.java +++ b/play-services-core/src/main/java/org/microg/gms/gcm/GcmPrefs.java @@ -49,7 +49,7 @@ public class GcmPrefs implements SharedPreferences.OnSharedPreferenceChangeListe private SharedPreferences defaultPreferences; - public GcmPrefs(Context context) { + private GcmPrefs(Context context) { if (context != null) { defaultPreferences = PreferenceManager.getDefaultSharedPreferences(context); defaultPreferences.registerOnSharedPreferenceChangeListener(this); diff --git a/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java b/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java index ff79f699..91cb9824 100644 --- a/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java +++ b/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java @@ -170,8 +170,15 @@ public class McsService extends Service implements Handler.Callback { } } + public static void stop(Context context) { + context.stopService(new Intent(context, McsService.class)); + closeAll(); + } + @Override public void onDestroy() { + alarmManager.cancel(heartbeatIntent); + closeAll(); database.close(); super.onDestroy(); } @@ -571,7 +578,7 @@ public class McsService extends Service implements Handler.Callback { } } - private void tryClose(Closeable closeable) { + private static void tryClose(Closeable closeable) { if (closeable != null) { try { closeable.close(); @@ -580,7 +587,7 @@ public class McsService extends Service implements Handler.Callback { } } - private void closeAll() { + private static void closeAll() { tryClose(inputStream); tryClose(outputStream); if (sslSocket != null) { diff --git a/play-services-core/src/main/java/org/microg/gms/ui/GcmFragment.java b/play-services-core/src/main/java/org/microg/gms/ui/GcmFragment.java index 92f8061b..eab3f8db 100644 --- a/play-services-core/src/main/java/org/microg/gms/ui/GcmFragment.java +++ b/play-services-core/src/main/java/org/microg/gms/ui/GcmFragment.java @@ -38,6 +38,7 @@ import com.google.android.gms.R; import org.microg.gms.gcm.GcmDatabase; import org.microg.gms.gcm.GcmPrefs; +import org.microg.gms.gcm.McsConstants; import org.microg.gms.gcm.McsService; import org.microg.tools.ui.AbstractSettingsActivity; import org.microg.tools.ui.DimmableIconPreference; @@ -140,6 +141,12 @@ public class GcmFragment extends ResourceSettingsFragment implements SwitchBar.O public void onSwitchChanged(SwitchCompat switchView, boolean isChecked) { if (switchView == switchCompat) { getPreferenceManager().getSharedPreferences().edit().putBoolean(GcmPrefs.PREF_ENABLE_GCM, isChecked).apply(); + if (!isChecked) { + McsService.stop(getContext()); + } else { + getContext().startService(new Intent(McsConstants.ACTION_CONNECT, null, getContext(), McsService.class)); + } + updateContent(); } }