Created
January 7, 2013 20:06
-
-
Save alexjlockwood/4477939 to your computer and use it in GitHub Desktop.
The main Activity outlined in the blog post.
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.accounts.Account; | |
import android.accounts.AccountManager; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
import com.google.android.gms.auth.GoogleAuthUtil; | |
import com.google.android.gms.common.AccountPicker; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GooglePlayServicesUtil; | |
public class AuthActivity extends Activity { | |
static final int REQUEST_CODE_RECOVER_PLAY_SERVICES = 1001; | |
static final int REQUEST_CODE_PICK_ACCOUNT = 1002; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
if (checkPlayServices() && checkUserAccount()) { | |
// Then we're good to go! | |
} | |
} | |
private boolean checkPlayServices() { | |
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); | |
if (status != ConnectionResult.SUCCESS) { | |
if (GooglePlayServicesUtil.isUserRecoverableError(status)) { | |
showErrorDialog(status); | |
} else { | |
Toast.makeText(this, "This device is not supported.", Toast.LENGTH_LONG).show(); | |
finish(); | |
} | |
return false; | |
} | |
return true; | |
} | |
void showErrorDialog(int code) { | |
GooglePlayServicesUtil.getErrorDialog(code, this, REQUEST_CODE_RECOVER_PLAY_SERVICES).show(); | |
} | |
private boolean checkUserAccount() { | |
String accountName = AccountUtils.getAccountName(this); | |
if (accountName == null) { | |
// Then the user was not found in the SharedPreferences. Either the | |
// application deliberately removed the account, or the application's | |
// data has been forcefully erased. | |
showAccountPicker(); | |
return false; | |
} | |
Account account = AccountUtils.getGoogleAccountByName(this, accountName); | |
if (account == null) { | |
// Then the account has since been removed. | |
AccountUtils.removeAccount(this); | |
showAccountPicker(); | |
return false; | |
} | |
return true; | |
} | |
private void showAccountPicker() { | |
Intent pickAccountIntent = AccountPicker.newChooseAccountIntent(null, null, | |
new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, true, null, null, null, null); | |
startActivityForResult(pickAccountIntent, REQUEST_CODE_PICK_ACCOUNT); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
switch (requestCode) { | |
case REQUEST_CODE_RECOVER_PLAY_SERVICES: | |
if (resultCode == RESULT_CANCELED) { | |
Toast.makeText(this, "Google Play Services must be installed and up-to-date.", | |
Toast.LENGTH_SHORT).show(); | |
finish(); | |
} | |
return; | |
case REQUEST_CODE_PICK_ACCOUNT: | |
if (resultCode == RESULT_OK) { | |
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); | |
AccountUtils.setAccountName(this, accountName); | |
} else if (resultCode == RESULT_CANCELED) { | |
Toast.makeText(this, "This application requires a Google account.", | |
Toast.LENGTH_SHORT).show(); | |
finish(); | |
} | |
return; | |
} | |
super.onActivityResult(requestCode, resultCode, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Alex,
Thanks for this, very useful.
I initially found it through a gist search, and then spent a while hunting for your blog. Might be worth adding a pointer to help people find the relevant blog post on the description in gist - something like "The main activity outlined in the blog post: http://www.androiddesignpatterns.com/2013/01/google-play-services-setup.html".
Cheers,
Richard