0
0
Fork 0
mirror of https://github.com/YTVanced/VancedMicroG synced 2024-11-24 04:05:13 +00:00

Make heartbeat interval adjustable #47

This commit is contained in:
mar-v-in 2015-12-04 11:26:59 -08:00
parent 4c74f8a4f2
commit 93645ca68f
3 changed files with 22 additions and 4 deletions

View file

@ -27,6 +27,7 @@ import android.os.IBinder;
import android.os.Looper;
import android.os.PowerManager;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
@ -75,7 +76,8 @@ public class McsService extends Service implements Handler.Callback {
public static final String SERVICE_HOST = "mtalk.google.com";
public static final int SERVICE_PORT = 5228;
public static final int HEARTBEAT_MS = 60000;
private static final String PREF_GCM_HEARTBEAT = "gcm_heartbeat_interval";
public int heartbeatMs = 60000;
private static Socket sslSocket;
private static McsInputStream inputStream;
@ -114,6 +116,7 @@ public class McsService extends Service implements Handler.Callback {
@Override
public void onCreate() {
super.onCreate();
updateHeartbeatMs();
heartbeatIntent = PendingIntent.getService(this, 0, new Intent(ACTION_HEARTBEAT, null, this, McsService.class), 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
@ -125,6 +128,10 @@ public class McsService extends Service implements Handler.Callback {
}
}
private void updateHeartbeatMs() {
heartbeatMs = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(this).getString(PREF_GCM_HEARTBEAT, "60")) * 1000;
}
@Override
public IBinder onBind(Intent intent) {
return null;
@ -156,6 +163,7 @@ public class McsService extends Service implements Handler.Callback {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
updateHeartbeatMs();
synchronized (McsService.class) {
if (rootHandler != null) {
wakeLock.acquire(5000);
@ -188,7 +196,7 @@ public class McsService extends Service implements Handler.Callback {
inputStream.start();
outputStream.start();
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), HEARTBEAT_MS, heartbeatIntent);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), heartbeatMs, heartbeatIntent);
} catch (Exception e) {
Log.w(TAG, "Exception while connecting!", e);
rootHandler.sendMessage(rootHandler.obtainMessage(MSG_TEARDOWN, e));
@ -377,9 +385,9 @@ public class McsService extends Service implements Handler.Callback {
sslSocket.close();
} catch (Exception ignored) {
}
scheduleReconnect(this);
alarmManager.cancel(heartbeatIntent);
if (wakeLock != null) {
wakeLock.release();

View file

@ -59,6 +59,10 @@ This can take a couple of minutes."</string>
<string name="pref_gcm_enable_mcs_title">Enable Google Cloud Messaging</string>
<string name="pref_gcm_enable_mcs_summary">Google Cloud Messaging is a push notification provider used by many applications. To use it you must enable device checkin.</string>
<string name="pref_gcm_heartbeat" translatable="false">gcm_heartbeat_interval</string>
<string name="pref_gcm_heartbeat_title">Cloud Messaging heartbeat interval</string>
<string name="pref_gcm_heartbeat_summary">The interval in seconds for the system to heartbeat the Google servers. Increasing this number will reduce battery consumption, but might cause delays on push messages.</string>
<string name="prefcat_components">Components</string>
<string name="prefcat_location_service">Location service</string>

View file

@ -27,6 +27,12 @@
android:key="@string/pref_gcm_enable_mcs"
android:summary="@string/pref_gcm_enable_mcs_summary"
android:title="@string/pref_gcm_enable_mcs_title" />
<EditTextPreference
android:defaultValue="60"
android:dependency="@string/pref_gcm_enable_mcs"
android:key="@string/pref_gcm_heartbeat"
android:summary="@string/pref_gcm_heartbeat_summary"
android:title="@string/pref_gcm_heartbeat_title" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/prefcat_location_service">
<Preference android:title="@string/nlp_settings_label">