Created
March 14, 2014 08:41
-
-
Save runemart/9544170 to your computer and use it in GitHub Desktop.
Android GCM activity (requires AndroidAnnotations)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.SharedPreferences; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.preference.PreferenceManager; | |
import android.util.Log; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GooglePlayServicesUtil; | |
import com.google.android.gms.gcm.GoogleCloudMessaging; | |
import org.androidannotations.annotations.AfterInject; | |
import org.androidannotations.annotations.Background; | |
import org.androidannotations.annotations.EActivity; | |
import org.androidannotations.annotations.UiThread; | |
import java.io.IOException; | |
@EActivity | |
public abstract class GCMActivity extends Activity { | |
private static final String TAG = GCMActivity.class.getSimpleName(); | |
private static final String PREF_KEY_GCM_REGISTRATION_ID = "pref_key_gcm_registration_id"; | |
private static final String PREF_KEY_APP_VERSION = "pref_key_app_version"; | |
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; | |
@AfterInject | |
protected void afterInject() { | |
if (isPlayServicesAvailable() && getStoredRegistrationId().isEmpty()) | |
setupGCM(getGCMSenderId()); | |
} | |
protected boolean isPlayServicesAvailable() { | |
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); | |
if (resultCode != ConnectionResult.SUCCESS) { | |
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { | |
GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show(); | |
} else { | |
Log.d(TAG, "This device is not supported."); | |
finish(); | |
} | |
return false; | |
} | |
return true; | |
} | |
protected String getStoredRegistrationId() { | |
String registrationId = PreferenceManager.getDefaultSharedPreferences(this).getString(PREF_KEY_GCM_REGISTRATION_ID, ""); | |
if (registrationId.isEmpty()) { | |
Log.d(TAG, "GCM registration ID not found."); | |
return ""; | |
} | |
if (getOldAppVersion() != getCurrentAppVersion()) { | |
Log.d(TAG, "App version changed."); | |
return ""; | |
} | |
return registrationId; | |
} | |
private int getOldAppVersion() { | |
return PreferenceManager.getDefaultSharedPreferences(this).getInt(PREF_KEY_APP_VERSION, Integer.MIN_VALUE); | |
} | |
private int getCurrentAppVersion() { | |
try { | |
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0); | |
return packageInfo.versionCode; | |
} catch (PackageManager.NameNotFoundException e) { | |
throw new RuntimeException("Could not get package name: " + e); | |
} | |
} | |
@Background | |
protected void setupGCM(String senderId) { | |
try { | |
setupGCMSuccess(GoogleCloudMessaging.getInstance(this).register(senderId)); | |
} catch (IOException e) { | |
Log.e(TAG, e.getMessage()); | |
setupGCMError(); | |
} | |
} | |
@UiThread | |
protected void setupGCMError() { | |
} | |
@UiThread | |
protected void setupGCMSuccess(String registrationId) { | |
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit(); | |
edit.putString(PREF_KEY_GCM_REGISTRATION_ID, registrationId); | |
edit.putInt(PREF_KEY_APP_VERSION, getCurrentAppVersion()); | |
edit.commit(); | |
sendGCMRegistrationIdToBackend(registrationId); | |
} | |
@Background | |
protected abstract void sendGCMRegistrationIdToBackend(String registrationId); | |
protected abstract String getGCMSenderId(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment