Automatically checkin after 12 hours

This commit is contained in:
mar-v-in 2015-10-04 00:15:24 +02:00
parent 40ca65bbf4
commit 1f67d1aad0
8 changed files with 54 additions and 27 deletions

View File

@ -1,4 +1,5 @@
language: android language: android
sudo: false
git: git:
submodules: false submodules: false
before_install: before_install:

View File

@ -135,6 +135,9 @@
<intent-filter> <intent-filter>
<action android:name="android.server.checkin.CHECKIN"/> <action android:name="android.server.checkin.CHECKIN"/>
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
<intent-filter> <intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/> <action android:name="com.google.android.c2dm.intent.RECEIVE"/>

View File

@ -22,11 +22,13 @@ import android.app.IntentService;
import android.content.Intent; import android.content.Intent;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log; import android.util.Log;
import com.google.android.gms.R; import com.google.android.gms.R;
import com.google.android.gms.checkin.internal.ICheckinService; import com.google.android.gms.checkin.internal.ICheckinService;
import org.microg.gms.gcm.McsService;
import org.microg.gms.people.PeopleManager; import org.microg.gms.people.PeopleManager;
public class CheckinService extends IntentService { public class CheckinService extends IntentService {
@ -50,15 +52,18 @@ public class CheckinService extends IntentService {
LastCheckinInfo info = CheckinManager.checkin(this, intent.getBooleanExtra("force", false)); LastCheckinInfo info = CheckinManager.checkin(this, intent.getBooleanExtra("force", false));
if (info != null) { if (info != null) {
Log.d(TAG, "Checked in as " + Long.toHexString(info.androidId)); Log.d(TAG, "Checked in as " + Long.toHexString(info.androidId));
} String accountType = getString(R.string.google_account_type);
String accountType = getString(R.string.google_account_type); for (Account account : AccountManager.get(this).getAccountsByType(accountType)) {
for (Account account : AccountManager.get(this).getAccountsByType(accountType)) { PeopleManager.loadUserInfo(this, account);
PeopleManager.loadUserInfo(this, account); }
McsService.scheduleReconnect(this);
} }
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, e); Log.w(TAG, e);
} finally {
WakefulBroadcastReceiver.completeWakefulIntent(intent);
stopSelf();
} }
stopSelf();
} }
@Override @Override

View File

@ -55,6 +55,6 @@ public class LastCheckinInfo {
.putLong(PREF_SECURITY_TOKEN, securityToken) .putLong(PREF_SECURITY_TOKEN, securityToken)
.putString(PREF_VERSION_INFO, versionInfo) .putString(PREF_VERSION_INFO, versionInfo)
.putString(PREF_DEVICE_DATA_VERSION_INFO, deviceDataVersionInfo) .putString(PREF_DEVICE_DATA_VERSION_INFO, deviceDataVersionInfo)
.apply(); .commit();
} }
} }

View File

@ -16,22 +16,38 @@
package org.microg.gms.checkin; package org.microg.gms.checkin;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log; import android.util.Log;
public class TriggerReceiver extends BroadcastReceiver { public class TriggerReceiver extends WakefulBroadcastReceiver {
private static final String TAG = "GmsCheckinTrigger"; private static final String TAG = "GmsCheckinTrigger";
private static final String PREF_ENABLE_CHECKIN = "checkin_enable_service";
private static final long REGULAR_CHECKIN_INTERVAL = 12 * 60 * 60 * 1000; // 12 hours
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Trigger checkin: " + intent); boolean force = "android.provider.Telephony.SECRET_CODE".equals(intent.getAction());
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Intent subIntent = new Intent(context, CheckinService.class); if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREF_ENABLE_CHECKIN, false) || force) {
if ("android.provider.Telephony.SECRET_CODE".equals(intent.getAction())) { if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()) &&
subIntent.putExtra("force", true); LastCheckinInfo.read(context).lastCheckin > System.currentTimeMillis() - REGULAR_CHECKIN_INTERVAL) {
return;
}
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected() || force) {
Intent subIntent = new Intent(context, CheckinService.class);
subIntent.putExtra("force", force);
startWakefulService(context, subIntent);
}
} else {
Log.d(TAG, "Ignoring " + intent + ": checkin is disabled");
} }
context.startService(subIntent);
} }
} }

View File

@ -131,6 +131,12 @@ public class McsService extends IntentService implements Handler.Callback {
return inputStream != null && inputStream.isAlive() && outputStream != null && outputStream.isAlive(); return inputStream != null && inputStream.isAlive() && outputStream != null && outputStream.isAlive();
} }
public static void scheduleReconnect(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + getCurrentDelay(),
PendingIntent.getBroadcast(context, 1, new Intent("org.microg.gms.gcm.RECONNECT", null, context, TriggerReceiver.class), 0));
}
public synchronized static long getCurrentDelay() { public synchronized static long getCurrentDelay() {
long delay = currentDelay == 0 ? 5000 : currentDelay; long delay = currentDelay == 0 ? 5000 : currentDelay;
if (currentDelay < 60000) currentDelay += 5000; if (currentDelay < 60000) currentDelay += 5000;
@ -365,8 +371,7 @@ public class McsService extends IntentService implements Handler.Callback {
if (currentDelay == 0) { if (currentDelay == 0) {
sendBroadcast(new Intent("org.microg.gms.gcm.RECONNECT"), "org.microg.gms.STATUS_BROADCAST"); sendBroadcast(new Intent("org.microg.gms.gcm.RECONNECT"), "org.microg.gms.STATUS_BROADCAST");
} else { } else {
alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + getCurrentDelay(), scheduleReconnect(this);
PendingIntent.getBroadcast(this, 1, new Intent("org.microg.gms.gcm.RECONNECT", null, this, TriggerReceiver.class), 0));
} }
alarmManager.cancel(heartbeatIntent); alarmManager.cancel(heartbeatIntent);
if (wakeLock != null) { if (wakeLock != null) {

View File

@ -16,17 +16,16 @@
package org.microg.gms.gcm; package org.microg.gms.gcm;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver; import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log; import android.util.Log;
import org.microg.gms.checkin.LastCheckinInfo;
public class TriggerReceiver extends WakefulBroadcastReceiver { public class TriggerReceiver extends WakefulBroadcastReceiver {
private static final String TAG = "GmsGcmTrigger"; private static final String TAG = "GmsGcmTrigger";
private static final String PREF_ENABLE_GCM = "gcm_enable_mcs_service"; private static final String PREF_ENABLE_GCM = "gcm_enable_mcs_service";
@ -34,13 +33,15 @@ public class TriggerReceiver extends WakefulBroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
boolean force = "android.provider.Telephony.SECRET_CODE".equals(intent.getAction()); boolean force = "android.provider.Telephony.SECRET_CODE".equals(intent.getAction());
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREF_ENABLE_GCM, false) || force) { if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREF_ENABLE_GCM, false) || force) {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
McsService.resetCurrentDelay(); McsService.resetCurrentDelay();
} }
if (LastCheckinInfo.read(context).androidId == 0) {
Log.d(TAG, "Ignoring " + intent + ": need to checkin first.");
}
NetworkInfo networkInfo = cm.getActiveNetworkInfo(); NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected() || force) { if (networkInfo != null && networkInfo.isConnected() || force) {
@ -49,7 +50,7 @@ public class TriggerReceiver extends WakefulBroadcastReceiver {
} else { } else {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
Log.d(TAG, "Ignoring " + intent + ": service is running. schedule reconnect instead."); Log.d(TAG, "Ignoring " + intent + ": service is running. schedule reconnect instead.");
scheduleReconnect(context, alarmManager); McsService.scheduleReconnect(context);
} else { } else {
Log.d(TAG, "Ignoring " + intent + ": service is running. heartbeat instead."); Log.d(TAG, "Ignoring " + intent + ": service is running. heartbeat instead.");
startWakefulService(context, new Intent(McsService.ACTION_HEARTBEAT, null, context, McsService.class)); startWakefulService(context, new Intent(McsService.ACTION_HEARTBEAT, null, context, McsService.class));
@ -57,15 +58,11 @@ public class TriggerReceiver extends WakefulBroadcastReceiver {
} }
} else { } else {
Log.d(TAG, "Ignoring " + intent + ": network is offline, scheduling new attempt."); Log.d(TAG, "Ignoring " + intent + ": network is offline, scheduling new attempt.");
scheduleReconnect(context, alarmManager); McsService.scheduleReconnect(context);
} }
} else { } else {
Log.d(TAG, "Ignoring " + intent + ": gcm is disabled"); Log.d(TAG, "Ignoring " + intent + ": gcm is disabled");
} }
} }
private void scheduleReconnect(Context context, AlarmManager alarmManager) {
alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + McsService.getCurrentDelay(),
PendingIntent.getBroadcast(context, 1, new Intent("org.microg.gms.gcm.RECONNECT", null, context, TriggerReceiver.class), 0));
}
} }

View File

@ -33,10 +33,10 @@
<string name="pref_auth_trust_google_title">Trust Google for app permissions</string> <string name="pref_auth_trust_google_title">Trust Google for app permissions</string>
<string name="pref_auth_trust_google_summary">When disabled, the user is asked before an apps authorization request is sent to Google. Some applications will fail to use the Google account if this is disabled.</string> <string name="pref_auth_trust_google_summary">When disabled, the user is asked before an apps authorization request is sent to Google. Some applications will fail to use the Google account if this is disabled.</string>
<string name="pref_checkin_enable">checkin_enable_service</string> <string name="pref_checkin_enable" translatable="false">checkin_enable_service</string>
<string name="pref_checkin_enable_title">Enable device checkin</string> <string name="pref_checkin_enable_title">Enable device checkin</string>
<string name="pref_checkin_enable_summary">Device checkin is a hidden process that is used to create an unique identifier for Google services. microG Services strips identifying bits other than your Google account name from this data.</string> <string name="pref_checkin_enable_summary">Device checkin is a hidden process that is used to create an unique identifier for Google services. microG Services strips identifying bits other than your Google account name from this data.</string>
<string name="pref_gcm_enable_mcs">gcm_enable_mcs_service</string> <string name="pref_gcm_enable_mcs" translatable="false">gcm_enable_mcs_service</string>
<string name="pref_gcm_enable_mcs_title">Enable Google Cloud Messaging</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_enable_mcs_summary">Google Cloud Messaging is a push notification provider used by many applications. To use it you must enable device checkin.</string>