Created BroadcastReceiver that kicks the C2DM/GCM update process when the app is upgraded

This commit is contained in:
felipeal
2012-08-07 21:49:56 +00:00
parent 5df7017daf
commit abc57f9609
4 changed files with 53 additions and 8 deletions

View File

@@ -79,6 +79,17 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<!--
Receiver called when the application is updated (Android 3.1+), it will check
if the previous version used C2DM and update to GCM if necessary.
-->
<receiver
android:name=".Updater">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
<receiver android:name=".UserPresentReceiver" android:enabled="false"> <receiver android:name=".UserPresentReceiver" android:enabled="false">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.USER_PRESENT" /> <action android:name="android.intent.action.USER_PRESENT" />

View File

@@ -150,4 +150,22 @@ public class DeviceRegistrar {
Configuration config = context.getResources().getConfiguration(); Configuration config = context.getResources().getConfiguration();
return (config.screenLayout & xlargeBit) == xlargeBit; 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;
}
}
} }

View File

@@ -44,14 +44,8 @@ public class HistoryActivity extends Activity implements OnChildClickListener {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
SharedPreferences prefs = Prefs.get(this); boolean isGcmUpdate = DeviceRegistrar.updateC2DM(this);
String c2dmRegId = prefs.getString("deviceRegistrationID", null); if (!isGcmUpdate) {
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 {
// Run the setup first if necessary // Run the setup first if necessary
String gcmRegId = GCMRegistrar.getRegistrationId(this); String gcmRegId = GCMRegistrar.getRegistrationId(this);
if (gcmRegId.equals("")) { if (gcmRegId.equals("")) {

View File

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