From cefc13a2cdf813843fcde7d9802da55ad90aacc9 Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 15 Nov 2012 21:44:56 -0800 Subject: [PATCH] Docs say should allow GCM to reregister on app upgrade. And that the registrar takes care of storing devid. So app version when registration arrives, and get from registrar except after an upgrade. --- .../XWords4/res/values/common_rsrc.xml | 2 +- .../eehouse/android/xw4/GCMIntentService.java | 7 +--- .../src/org/eehouse/android/xw4/XWPrefs.java | 33 ++++++++++++++++--- .../eehouse/android/xw4/jni/UtilCtxtImpl.java | 4 ++- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/xwords4/android/XWords4/res/values/common_rsrc.xml b/xwords4/android/XWords4/res/values/common_rsrc.xml index e09033bba..e354646b6 100644 --- a/xwords4/android/XWords4/res/values/common_rsrc.xml +++ b/xwords4/android/XWords4/res/values/common_rsrc.xml @@ -68,7 +68,7 @@ key_sms_phones key_connstat_data key_dev_id - key_gcm_regid + key_gcmvers_regid key_relay_regid key_checked_sms diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GCMIntentService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GCMIntentService.java index 9bfeca058..2ab40f4bc 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GCMIntentService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GCMIntentService.java @@ -78,15 +78,10 @@ public class GCMIntentService extends GCMBaseIntentService { try { GCMRegistrar.checkDevice( app ); // GCMRegistrar.checkManifest( app ); - final String regId = GCMRegistrar.getRegistrationId( app ); + String regId = XWPrefs.getGCMDevID( app ); if (regId.equals("")) { GCMRegistrar.register( app, GCMConsts.SENDER_ID ); } - - String curID = XWPrefs.getGCMDevID( app ); - if ( null == curID || ! curID.equals( regId ) ) { - XWPrefs.setGCMDevID( app, regId ); - } } catch ( UnsupportedOperationException uoe ) { DbgUtils.logf( "Device can't do GCM." ); } catch ( Exception whatever ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java index 24ebbe8eb..4ac4561c0 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java @@ -24,6 +24,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.text.TextUtils; +import com.google.android.gcm.GCMRegistrar; import java.util.ArrayList; import java.util.ArrayList; @@ -112,6 +113,24 @@ public class XWPrefs { return result; } + public static int getPrefsInt( Context context, int keyID, int defaultValue ) + { + String key = context.getString( keyID ); + SharedPreferences sp = PreferenceManager + .getDefaultSharedPreferences( context ); + return sp.getInt( key, defaultValue ); + } + + public static void setPrefsInt( Context context, int keyID, int newValue ) + { + SharedPreferences sp = PreferenceManager + .getDefaultSharedPreferences( context ); + SharedPreferences.Editor editor = sp.edit(); + String key = context.getString( keyID ); + editor.putInt( key, newValue ); + editor.commit(); + } + public static boolean getPrefsBoolean( Context context, int keyID, boolean defaultValue ) { @@ -186,22 +205,26 @@ public class XWPrefs { public static void setGCMDevID( Context context, String devID ) { - setPrefsString( context, R.string.key_gcm_regid, devID ); + int curVers = Utils.getAppVersion( context ); + setPrefsInt( context, R.string.key_gcmvers_regid, curVers ); clearPrefsKey( context, R.string.key_relay_regid ); } public static String getGCMDevID( Context context ) { - String result = getPrefsString( context, R.string.key_gcm_regid ); - if ( result.equals("") ) { - result = null; + int curVers = Utils.getAppVersion( context ); + int storedVers = getPrefsInt( context, R.string.key_gcmvers_regid, 0 ); + String result; + if ( 0 != storedVers && storedVers < curVers ) { + result = ""; // Don't trust what registrar has + } else { + result = GCMRegistrar.getRegistrationId( context ); } return result; } public static void clearGCMDevID( Context context ) { - clearPrefsKey( context, R.string.key_gcm_regid ); clearRelayDevID( context ); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/UtilCtxtImpl.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/UtilCtxtImpl.java index f39ad5e1a..b4ad0eb6a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/UtilCtxtImpl.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/UtilCtxtImpl.java @@ -102,7 +102,9 @@ public class UtilCtxtImpl implements UtilCtxt { typ = UtilCtxt.ID_TYPE_RELAY; } else { result = XWPrefs.getGCMDevID( m_context ); - if ( null != result ) { + if ( result.equals("") ) { + result = null; + } else { typ = UtilCtxt.ID_TYPE_ANDROID_GCM; } }