From abc57f96093764b0e8017e90fdea67665075b36b Mon Sep 17 00:00:00 2001 From: felipeal Date: Tue, 7 Aug 2012 21:49:56 +0000 Subject: [PATCH] Created BroadcastReceiver that kicks the C2DM/GCM update process when the app is upgraded --- android/AndroidManifest.xml | 11 ++++++++++ .../apps/chrometophone/DeviceRegistrar.java | 18 +++++++++++++++ .../apps/chrometophone/HistoryActivity.java | 10 ++------- .../android/apps/chrometophone/Updater.java | 22 +++++++++++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 android/src/com/google/android/apps/chrometophone/Updater.java diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml index f5b97f6..9508227 100644 --- a/android/AndroidManifest.xml +++ b/android/AndroidManifest.xml @@ -79,6 +79,17 @@ + + + + + + + diff --git a/android/src/com/google/android/apps/chrometophone/DeviceRegistrar.java b/android/src/com/google/android/apps/chrometophone/DeviceRegistrar.java index 6dbc227..b351075 100644 --- a/android/src/com/google/android/apps/chrometophone/DeviceRegistrar.java +++ b/android/src/com/google/android/apps/chrometophone/DeviceRegistrar.java @@ -150,4 +150,22 @@ public class DeviceRegistrar { Configuration config = context.getResources().getConfiguration(); return (config.screenLayout & xlargeBit) == xlargeBit; } + + /** + * Update the device to use GCM instead of C2DM. + */ + static boolean updateC2DM(Context context) { + SharedPreferences prefs = Prefs.get(context); + String c2dmRegId = prefs.getString("deviceRegistrationID", null); + // The old versions of the app that used C2DM stored the registration id in the default + // preferences; the new version stores it in the GCM library. + if (c2dmRegId != null) { + Log.i(TAG, "Updating from C2DM to GCM"); + DeviceRegistrar.unregisterWithServer(context, c2dmRegId, "ac2dm"); + GCMRegistrar.register(context, DeviceRegistrar.SENDER_ID); + return true; + } else { + return false; + } + } } diff --git a/android/src/com/google/android/apps/chrometophone/HistoryActivity.java b/android/src/com/google/android/apps/chrometophone/HistoryActivity.java index 3bbe824..ebe058e 100644 --- a/android/src/com/google/android/apps/chrometophone/HistoryActivity.java +++ b/android/src/com/google/android/apps/chrometophone/HistoryActivity.java @@ -44,14 +44,8 @@ public class HistoryActivity extends Activity implements OnChildClickListener { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - SharedPreferences prefs = Prefs.get(this); - String c2dmRegId = prefs.getString("deviceRegistrationID", null); - if (c2dmRegId != null) { - // this is an update from the version that uses C2DM: must - // unregister and register again using GCM - DeviceRegistrar.unregisterWithServer(this, c2dmRegId, "ac2dm"); - GCMRegistrar.register(this, DeviceRegistrar.SENDER_ID); - } else { + boolean isGcmUpdate = DeviceRegistrar.updateC2DM(this); + if (!isGcmUpdate) { // Run the setup first if necessary String gcmRegId = GCMRegistrar.getRegistrationId(this); if (gcmRegId.equals("")) { diff --git a/android/src/com/google/android/apps/chrometophone/Updater.java b/android/src/com/google/android/apps/chrometophone/Updater.java new file mode 100644 index 0000000..2e0490b --- /dev/null +++ b/android/src/com/google/android/apps/chrometophone/Updater.java @@ -0,0 +1,22 @@ +package com.google.android.apps.chrometophone; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +/** + * Receiver called when the application is updated (Android 3.1+), it will check + * if the previous version used C2DM and, if so, update to GCM. + */ +public class Updater extends BroadcastReceiver { + + private static final String TAG = "Updater"; + + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "onReceive(" + intent + ")"); + DeviceRegistrar.updateC2DM(context); + } + +}