Created
April 7, 2015 22:13
-
-
Save brainail/d1bc6a9d58db085cbd67 to your computer and use it in GitHub Desktop.
Drive REST API, authorization flow
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
// Base auth task | |
public abstract class AbstractAuthTask extends AsyncTask<Void, Void, Void> { | |
protected final AuthorizationFlow mAuthFlow; | |
AbstractAuthTask(final AuthorizationFlow authFlow) { | |
mAuthFlow = authFlow; | |
} | |
} |
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
// Base fetch task | |
public abstract class AbstractFetchTokenTask extends AbstractAuthTask { | |
protected final String mScope; | |
protected final String mEmail; | |
AbstractFetchTokenTask(final AuthorizationFlow authFlow, final String email, final String scope) { | |
super(authFlow); | |
mScope = scope; | |
mEmail = email; | |
} | |
// Get a authentication token if one is not available. If the error is not recoverable then | |
// it displays the error message on parent activity right away. | |
protected abstract String fetchToken() throws IOException; | |
@Override | |
protected Void doInBackground(final Void ... params) { | |
try { | |
fetchUserInfo(); | |
} catch (IOException exception) { | |
onError(exception); | |
ToolUI.showToast(mAuthFlow.getActivity(), R.string.auth_flow_fetch_token_io_error); | |
} | |
return null; | |
} | |
private void fetchUserInfo() throws IOException { | |
final String authToken = fetchToken(); | |
if (!TextUtils.isEmpty(authToken)) { | |
mAuthFlow.onAuthTokenFetched(authToken); | |
} | |
} | |
protected void onError(final Exception error) { | |
Log(error); | |
} | |
} |
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
// Codes | |
public final class AuthRequestCode { | |
private static final int BASE = 1000; | |
public static final int PICK_ACCOUNT = BASE; | |
public static final int RECOVER_FROM_AUTH_ERROR = BASE + 1; | |
public static final int RECOVER_FROM_PLAY_SERVICES_ERROR = BASE + 2; | |
public static boolean isRecoverableError(final int code) { | |
boolean result = RECOVER_FROM_AUTH_ERROR == code; | |
result |= RECOVER_FROM_PLAY_SERVICES_ERROR == code; | |
return result; | |
} | |
} |
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
// Info about user | |
public class AuthUserInfo { | |
public String email; | |
public AuthUserInfo(final String email) { | |
this.email = email; | |
} | |
} |
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
// Task to clear token/user | |
public class ClearTokenTask extends AbstractAuthTask { | |
protected final String mEmail; | |
ClearTokenTask(final AuthorizationFlow authFlow, final String email) { | |
super(authFlow); | |
mEmail = email; | |
} | |
@Override | |
protected Void doInBackground(final Void ... params) { | |
try { | |
clearToken(); | |
} catch (Exception exception) { | |
Log("Something was wrong while clearing token for account"); | |
} | |
return null; | |
} | |
private void clearToken() throws IOException, GoogleAuthException { | |
if (!TextUtils.isEmpty(mEmail)) { | |
final String token = GoogleAuthUtil.getToken(JApplication.appContext(), mEmail, ToolDriveScope.formFullScope()); | |
if (!TextUtils.isEmpty(token)) { | |
GoogleAuthUtil.clearToken(<Application Context>, token); | |
} | |
} | |
} | |
} |
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
// Task to fetch token/user | |
public class FetchTokenTask extends AbstractFetchTokenTask { | |
public FetchTokenTask(final AuthorizationFlow authFlow, final String email, final String scope) { | |
super(authFlow, email, scope); | |
} | |
@Override | |
protected String fetchToken() throws IOException { | |
try { | |
return GoogleAuthUtil.getToken(<Application Context>, mEmail, mScope); | |
} catch (UserRecoverableAuthException exception) { | |
// GooglePlayServices.apk is either old, disabled, or not present, which is | |
// recoverable, so we need to show the user some UI through the activity. | |
mAuthFlow.handleError(exception); | |
} catch (GoogleAuthException exception) { | |
onError(exception); | |
ToolUI.showToast(mAuthFlow.getActivity(), R.string.auth_flow_device_suitability); | |
} | |
return null; | |
} | |
} |
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
// Dialog to show Gps errorsse | |
public class GooglePsErrorDialog extends DialogFragment { | |
public static final String MANAGER_TAG = "<Application Id>.tag#google.play.services.error.dialog"; | |
public static class Args { | |
public static final String STATUS_CODE = "<Application Id>.arg#status.code"; | |
public static final String REQUEST_CODE = "<Application Id>.arg#request.code"; | |
} | |
private int mStatusCode; | |
private int mRequestCode; | |
public static void show(final Activity activity, final int statusCode, final int requestCode) { | |
if (null != activity) { | |
newInstance(statusCode, requestCode).show(activity.getFragmentManager(), MANAGER_TAG); | |
} | |
} | |
public static void show(final Activity activity, final int statusCode) { | |
show(activity, statusCode, 0); | |
} | |
public static GooglePsErrorDialog newInstance(final int statusCode, final int requestCode) { | |
final GooglePsErrorDialog dialog = new GooglePsErrorDialog(); | |
final Bundle args = new Bundle(); | |
args.putInt(Args.STATUS_CODE, statusCode); | |
args.putInt(Args.REQUEST_CODE, requestCode); | |
dialog.setArguments(args); | |
return dialog; | |
} | |
public static GooglePsErrorDialog newInstance(final int statusCode) { | |
return newInstance(statusCode, 0); | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
final Bundle args = getArguments(); | |
mStatusCode = args.getInt(Args.STATUS_CODE, ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED); | |
mRequestCode = args.getInt(Args.REQUEST_CODE, 0); | |
} | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
// Workaround for | |
// android.view.WindowLeaked: | |
// Activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41522b80 | |
// that was originally added here (Dialog#show()) | |
final Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(mStatusCode, getActivity(), mRequestCode); | |
return null != errorDialog ? errorDialog : super.onCreateDialog(savedInstanceState); | |
} | |
} |
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
// Accounts chooser | |
public final class ToolAuth { | |
public final static String [] ACCOUNT_TYPES = {"com.google"}; | |
public static Intent formChooseGoogleAccountsIntent() { | |
return AccountPicker.newChooseAccountIntent(null, null, ACCOUNT_TYPES, true, null, null, null, null); | |
} | |
} |
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
// Scopes to request from Google Drive | |
public final class ToolDriveScope { | |
public static final String USER_INFO = "https://www.googleapis.com/auth/userinfo.profile"; | |
public static final String APP_DATA = DriveScopes.DRIVE_APPDATA; | |
public static String formFullScope() { | |
final StringBuilder fullScope = new StringBuilder("oauth2:"); | |
fullScope.append(USER_INFO).append(ToolStrings.SPACE); | |
fullScope.append(APP_DATA); | |
return fullScope.toString(); | |
} | |
} |
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
public final class ToolTasks { | |
// Safely executes async task. | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
public static void safeExecuteAuthTask(final AbstractAuthTask authTask) { | |
try { | |
if (!Sdk.isSdkSupported(Sdk.HONEYCOMB)) { | |
authTask.execute(); | |
} else { | |
authTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); | |
} | |
} catch (Exception exception) { | |
Log("Smth was wrong while executing ad task.\nException: %s", exception.getLocalizedMessage()); | |
} | |
} | |
} |
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
public final class ToolUI { | |
public static void showToast(final Activity activity, final String message) { | |
// Show default Android toast or snackbar from the Material design ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment