mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2025-12-22 09:41:51 +00:00
Created BroadcastReceiver that kicks the C2DM/GCM update process when the app is upgraded
This commit is contained in:
@@ -79,6 +79,17 @@
|
||||
</intent-filter>
|
||||
</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">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.USER_PRESENT" />
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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("")) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user