Directly start/stop GCM service if setting is changed in UI

This commit is contained in:
Marvin W 2017-02-08 14:14:08 +01:00
parent 170d5e4524
commit d99fa7e86f
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
3 changed files with 17 additions and 3 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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();
}
}