Java tutorial
/* * Copyright (c) 2013 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ package com.google.cloud.backend.android; import java.util.List; import android.accounts.AccountManager; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; /** * An {@link Activity} class that wraps CloudBackend features including CRUD of * {@link CloudEntity}, Google account authentication and Google Cloud * Messaging. Developer may create a subclass to use these features. If you want * to inherit other {@link Activity} subclasses, you may rewrite the superclass * name of the class definition below. * */ public class CloudBackendSherlockFragmentActivity extends SherlockFragmentActivity { private static final int REQUEST_ACCOUNT_PICKER = 2; protected static final int REQUEST_PLAY_SERVICES = 3; private static final String PREF_KEY_ACCOUNT_NAME = "PREF_KEY_ACCOUNT_NAME"; private GoogleAccountCredential credential; private CloudBackendMessaging cloudBackend; // is this app subscribed to the Cloud Message? private static boolean isSubscribedToBroadcastMessage = false; /** * Returns {@link CloudBackendMessaging} instance for this activity. * * @return {@link CloudBackendMessaging} */ protected CloudBackendMessaging getCloudBackend() { return cloudBackend; } /** * Subclasses may override this to return false if the app need to disable * authentication. */ public boolean isAuthEnabled() { return Consts.IS_AUTH_ENABLED; } /** * Subclasses may override this to return false if an activity doesn't need play services (or it would annoy users) */ public boolean checkPlayServicesEnabled() { return true; } /** * Subclasses may override this to execute initialization of the activity. If * it uses any CloudBackend features, it should be executed inside * {@link #onPostCreate()} that will be called after CloudBackend * initializations such as user authentication. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // init backend cloudBackend = new CloudBackendMessaging(this); // create credential credential = GoogleAccountCredential.usingAudience(this, Consts.AUTH_AUDIENCE); cloudBackend.setCredential(credential); // if auth enabled, get account name from the shared pref if (isAuthEnabled()) { String accountName = getPreferencesAccountName(); if (accountName == null) { // let user pick an account super.startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); return; // continue init in onActivityResult } else { credential.setSelectedAccountName(accountName); } } // post create initialization _onPostCreate(); } private void _onPostCreate() { // init subscription to broadcast message (note: changed to auth enabled too to stop welcome screen error) if (isAuthEnabled() && getCredentialAccountName() != null && !isSubscribedToBroadcastMessage) { isSubscribedToBroadcastMessage = true; CloudCallbackHandler<List<CloudEntity>> handler = new CloudCallbackHandler<List<CloudEntity>>() { @Override public void onComplete(List<CloudEntity> results) { onBroadcastMessageReceived(results); } }; cloudBackend.subscribeToCloudMessage(CloudBackendMessaging.TOPIC_ID_BROADCAST, handler); } // call onPostCreate this.onPostCreate(); } /** * Will be called after initialization process including the account picking. * Subclasses may override to execute any initialization of the activity. */ protected void onPostCreate() { } /** * Will be called after account has been set on first configuration. * Used primarily to restart the camera to fix a preview bug */ protected void onPostAuthenticate() { } @Override protected void onResume() { super.onResume(); if (checkPlayServicesEnabled()) { int playAvailability = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (playAvailability != ConnectionResult.SUCCESS) { // prompt to install Google Play if necessary GooglePlayServicesUtil.getErrorDialog(playAvailability, this, REQUEST_PLAY_SERVICES).show(); } } } /** * Handles broadcast messages from the backend. Subclasses may override this * to handle the message accordingly. * * @param message * {@link CloudEntity} that contains the message values. */ public void onBroadcastMessageReceived(List<CloudEntity> message) { } /** * Handles callback from Intents like authorization request or account * picking. You *must* call the superclass here, or the authentication * dialog will not be shown. */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_ACCOUNT_PICKER: if (resultCode == RESULT_OK && data != null && data.getExtras() != null) { // set the picked account name to the credential String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); credential.setSelectedAccountName(accountName); // save account name to shared pref SharedPreferences.Editor e = cloudBackend.getSharedPreferences().edit(); e.putString(PREF_KEY_ACCOUNT_NAME, accountName); e.commit(); // post authenticate initialization onPostAuthenticate(); } // post create initialization _onPostCreate(); break; } } /** * Returns the account name stored in preferences, or null if none is stored */ protected String getPreferencesAccountName() { return cloudBackend.getSharedPreferences().getString(PREF_KEY_ACCOUNT_NAME, null); } /** * Returns account name selected by user, or null if any account is selected. */ protected String getCredentialAccountName() { return credential == null ? null : credential.getSelectedAccountName(); } }