diff --git a/play-services-core/src/main/java/org/microg/gms/checkin/CheckinService.java b/play-services-core/src/main/java/org/microg/gms/checkin/CheckinService.java index 67680fcd..ccdaf4fe 100644 --- a/play-services-core/src/main/java/org/microg/gms/checkin/CheckinService.java +++ b/play-services-core/src/main/java/org/microg/gms/checkin/CheckinService.java @@ -22,6 +22,7 @@ import android.app.IntentService; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; +import android.preference.PreferenceManager; import android.support.v4.content.WakefulBroadcastReceiver; import android.util.Log; @@ -53,16 +54,18 @@ public class CheckinService extends IntentService { @Override protected void onHandleIntent(Intent intent) { try { - LastCheckinInfo info = CheckinManager.checkin(this, intent.getBooleanExtra(EXTRA_FORCE_CHECKIN, false)); - if (info != null) { - Log.d(TAG, "Checked in as " + Long.toHexString(info.androidId)); - String accountType = AuthConstants.DEFAULT_ACCOUNT_TYPE; - for (Account account : AccountManager.get(this).getAccountsByType(accountType)) { - PeopleManager.loadUserInfo(this, account); - } - McsService.scheduleReconnect(this); - if (intent.hasExtra(EXTRA_CALLBACK_INTENT)) { - startService((Intent) intent.getParcelableExtra(EXTRA_CALLBACK_INTENT)); + if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(TriggerReceiver.PREF_ENABLE_CHECKIN, false)) { + LastCheckinInfo info = CheckinManager.checkin(this, intent.getBooleanExtra(EXTRA_FORCE_CHECKIN, false)); + if (info != null) { + Log.d(TAG, "Checked in as " + Long.toHexString(info.androidId)); + String accountType = AuthConstants.DEFAULT_ACCOUNT_TYPE; + for (Account account : AccountManager.get(this).getAccountsByType(accountType)) { + PeopleManager.loadUserInfo(this, account); + } + McsService.scheduleReconnect(this); + if (intent.hasExtra(EXTRA_CALLBACK_INTENT)) { + startService((Intent) intent.getParcelableExtra(EXTRA_CALLBACK_INTENT)); + } } } } catch (Exception e) { diff --git a/play-services-core/src/main/java/org/microg/gms/checkin/TriggerReceiver.java b/play-services-core/src/main/java/org/microg/gms/checkin/TriggerReceiver.java index 12814e88..7837484f 100644 --- a/play-services-core/src/main/java/org/microg/gms/checkin/TriggerReceiver.java +++ b/play-services-core/src/main/java/org/microg/gms/checkin/TriggerReceiver.java @@ -26,7 +26,7 @@ import android.util.Log; public class TriggerReceiver extends WakefulBroadcastReceiver { private static final String TAG = "GmsCheckinTrigger"; - private static final String PREF_ENABLE_CHECKIN = "checkin_enable_service"; + public static final String PREF_ENABLE_CHECKIN = "checkin_enable_service"; private static final long REGULAR_CHECKIN_INTERVAL = 12 * 60 * 60 * 1000; // 12 hours @Override diff --git a/play-services-core/src/main/java/org/microg/gms/common/PackageUtils.java b/play-services-core/src/main/java/org/microg/gms/common/PackageUtils.java index b1456c34..ccb0e8ee 100644 --- a/play-services-core/src/main/java/org/microg/gms/common/PackageUtils.java +++ b/play-services-core/src/main/java/org/microg/gms/common/PackageUtils.java @@ -99,6 +99,7 @@ public class PackageUtils { @SuppressWarnings("deprecation") public static String packageFromPendingIntent(PendingIntent pi) { + if (pi == null) return null; if (SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { return pi.getTargetPackage(); } else { diff --git a/play-services-core/src/main/java/org/microg/gms/gcm/PushRegisterService.java b/play-services-core/src/main/java/org/microg/gms/gcm/PushRegisterService.java index b844ee13..e6946b21 100644 --- a/play-services-core/src/main/java/org/microg/gms/gcm/PushRegisterService.java +++ b/play-services-core/src/main/java/org/microg/gms/gcm/PushRegisterService.java @@ -101,24 +101,29 @@ public class PushRegisterService extends IntentService { protected void onHandleIntent(Intent intent) { Log.d(TAG, "onHandleIntent: " + intent); Log.d(TAG, "onHandleIntent: " + intent.getExtras()); - if (LastCheckinInfo.read(this).lastCheckin > 0) { - try { - if (ACTION_C2DM_UNREGISTER.equals(intent.getAction()) || - (ACTION_C2DM_REGISTER.equals(intent.getAction()) && "1".equals(intent.getStringExtra(EXTRA_DELETE)))) { - unregister(intent); - } else if (ACTION_C2DM_REGISTER.equals(intent.getAction())) { - register(intent); + if (GcmPrefs.get(this).isGcmEnabled()) { + if (LastCheckinInfo.read(this).lastCheckin > 0) { + try { + if (ACTION_C2DM_UNREGISTER.equals(intent.getAction()) || + (ACTION_C2DM_REGISTER.equals(intent.getAction()) && "1".equals(intent.getStringExtra(EXTRA_DELETE)))) { + unregister(intent); + } else if (ACTION_C2DM_REGISTER.equals(intent.getAction())) { + register(intent); + } + } catch (Exception e) { + Log.w(TAG, e); } - } catch (Exception e) { - Log.w(TAG, e); + } else if (!intent.getBooleanExtra(EXTRA_SKIP_TRY_CHECKIN, false)) { + Log.d(TAG, "No checkin yet, trying to checkin"); + intent.putExtra(EXTRA_SKIP_TRY_CHECKIN, true); + Intent subIntent = new Intent(this, CheckinService.class); + subIntent.putExtra(CheckinService.EXTRA_FORCE_CHECKIN, true); + subIntent.putExtra(CheckinService.EXTRA_CALLBACK_INTENT, intent); + startService(subIntent); } - } else if (!intent.getBooleanExtra(EXTRA_SKIP_TRY_CHECKIN, false)) { - Log.d(TAG, "No checkin yet, trying to checkin"); - intent.putExtra(EXTRA_SKIP_TRY_CHECKIN, true); - Intent subIntent = new Intent(this, CheckinService.class); - subIntent.putExtra(CheckinService.EXTRA_FORCE_CHECKIN, true); - subIntent.putExtra(CheckinService.EXTRA_CALLBACK_INTENT, intent); - startService(subIntent); + } else { + // GCM is disabled, deny registration + replyNotAvailable(this, intent); } } @@ -146,7 +151,19 @@ public class PushRegisterService extends IntentService { } } + public static void replyNotAvailable(Context context, Intent intent) { + replyNotAvailable(context, intent, null); + } + public static void replyNotAvailable(Context context, Intent intent, String packageName) { + if (packageName == null) { + PendingIntent pendingIntent = intent.getParcelableExtra(EXTRA_APP); + packageName = PackageUtils.packageFromPendingIntent(pendingIntent); + } + if (packageName == null) { + // skip reply + return; + } Intent outIntent = new Intent(ACTION_C2DM_REGISTRATION); outIntent.putExtra(EXTRA_ERROR, ERROR_SERVICE_NOT_AVAILABLE); Log.d(TAG, "registration not allowed"); diff --git a/play-services-core/src/main/java/org/microg/gms/ui/GcmAppFragment.java b/play-services-core/src/main/java/org/microg/gms/ui/GcmAppFragment.java index 388aace7..26fa5c5c 100644 --- a/play-services-core/src/main/java/org/microg/gms/ui/GcmAppFragment.java +++ b/play-services-core/src/main/java/org/microg/gms/ui/GcmAppFragment.java @@ -7,6 +7,7 @@ import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.provider.Settings; +import android.support.annotation.StringRes; import android.support.v14.preference.SwitchPreference; import android.support.v4.app.Fragment; import android.support.v7.app.AlertDialog; @@ -123,34 +124,7 @@ public class GcmAppFragment extends ResourceSettingsFragment { if (!(boolean) newValue) { final List registrations = database.getRegistrationsByApp(packageName); if (!registrations.isEmpty()) { - new AlertDialog.Builder(getContext()) - .setTitle(String.format(getString(R.string.gcm_unregister_confirm_title), appName)) - .setMessage(R.string.gcm_unregister_after_deny_message) - .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - new Thread(new Runnable() { - @Override - public void run() { - for (GcmDatabase.Registration registration : registrations) { - PushRegisterService.unregister(getContext(), registration.packageName, registration.signature, null, null); - } - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - updateAppDetails(); - } - }); - } - }).start(); - } - }) - .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Do nothing - } - }).show(); + showUnregisterConfirm(registrations, getString(R.string.gcm_unregister_after_deny_message)); } } database.setAppAllowRegister(packageName, (Boolean) newValue); @@ -182,34 +156,7 @@ public class GcmAppFragment extends ResourceSettingsFragment { registerDetails.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { - new AlertDialog.Builder(getContext()) - .setTitle(String.format(getString(R.string.gcm_unregister_confirm_title), appName)) - .setMessage(R.string.gcm_unregister_confirm_message) - .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - new Thread(new Runnable() { - @Override - public void run() { - for (GcmDatabase.Registration registration : registrations) { - PushRegisterService.unregister(getContext(), registration.packageName, registration.signature, null, null); - } - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - updateAppDetails(); - } - }); - } - }).start(); - } - }) - .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Do nothing - } - }).show(); + showUnregisterConfirm(registrations, getString(R.string.gcm_unregister_confirm_message)); return true; } }); @@ -227,6 +174,37 @@ public class GcmAppFragment extends ResourceSettingsFragment { } } + private void showUnregisterConfirm(final List registrations, String unregisterConfirmDesc) { + new AlertDialog.Builder(getContext()) + .setTitle(getString(R.string.gcm_unregister_confirm_title, appName)) + .setMessage(unregisterConfirmDesc) + .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + new Thread(new Runnable() { + @Override + public void run() { + for (GcmDatabase.Registration registration : registrations) { + PushRegisterService.unregister(getContext(), registration.packageName, registration.signature, null, null); + } + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + updateAppDetails(); + } + }); + } + }).start(); + } + }) + .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // Do nothing + } + }).show(); + } + public static class AsActivity extends AbstractSettingsActivity { public AsActivity() { showHomeAsUp = true; 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 6381e1dd..92f8061b 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 @@ -213,6 +213,7 @@ public class GcmFragment extends ResourceSettingsFragment implements SwitchBar.O setIcon(packageManager.getApplicationIcon(applicationInfo)); } catch (PackageManager.NameNotFoundException e) { setTitle(app.packageName); + setIcon(android.R.drawable.sym_def_app_icon); } setOnPreferenceClickListener(this); updateViewDetails();