Android Open Source - built.io-todo-android Get Google Access Token






From Project

Back to project page built.io-todo-android.

License

The source code is released under:

Terms of Use & Licensing<br />Hand Drawn Web Icon Set is free and it will always be free. You can use it for both commercial and non-commercial projects. You can modify the icons anyway you like. Howe...

If you think the Android project built.io-todo-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.raweng.built.userInterface;
/*from   w ww  . j av a2 s.c  om*/
import java.io.IOException;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.raweng.built.BuiltError;

/**
 * 
 * @author raw engineering, Inc
 *
 */
class GetGoogleAccessToken extends AsyncTask<Void, Void, Void>{

  Activity loginActivity;
  Context loginContext;
  String loginScope;
  String loginEmail;
  String accessToken;
  int requestCode;
  int errorCode;
  boolean sendForSuccess = true;
  BuiltError error;
  
  final int ERROR_OCCURRED            = 0;
  final int SUCCESS_FETCH_ACCESSTOKEN = 1;
  final int NEED_PERMISSION           = 2;

  IGoogleLoginDelegate googleLoginInstance;
  
  Handler handleSendToken = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      if(msg.what == SUCCESS_FETCH_ACCESSTOKEN){
        googleLoginInstance.onSuccess(accessToken);
      }else if(msg.what == NEED_PERMISSION){
        sendForSuccess = false;
      }else if(msg.what == ERROR_OCCURRED){
        sendForSuccess = false;
        googleLoginInstance.onError(error,errorCode);
      }
    }
  };
  

  public GetGoogleAccessToken(Context context, Activity loginActivityInstance, IGoogleLoginDelegate googleDelegate, String email, String scope, int requestCode) {
    this.loginContext   = context;
    this.loginActivity  = loginActivityInstance;
    this.loginScope     = scope;
    this.loginEmail     = email;
    this.requestCode    = requestCode;
    googleLoginInstance = googleDelegate;
  }

  @Override
  protected Void doInBackground(Void... params) {
    try {
      fetchGoogleAccessToken();
    } catch (IOException networkError) {
      Message message = new Message();
      message.what = ERROR_OCCURRED;
      handleSendToken.sendMessage(message);
      onError(networkError.getMessage(),408,networkError);
    }
    return null;
  }

  protected void onError(String msg,int responseCode, Exception e) {
    final String errorMsg = e.getMessage();
    errorCode = responseCode;
    Object exceptionObject = e;
    
    Message message = new Message();
    message.what = ERROR_OCCURRED;
    error = new BuiltError();
    error.setErrorMessage(errorMsg);
    HashMap<String, Object> setError = new HashMap<String, Object>();
    setError.put(msg, exceptionObject);
    error.setErrors(setError);
    if(errorCode != 0){
      error.setErrorCode(errorCode);
    }
    handleSendToken.sendMessage(message);
  }
  protected void fetchGoogleAccessToken() throws IOException {
    try {
      accessToken = GoogleAuthUtil.getToken(loginContext, loginEmail, loginScope);
      
      Message message = new Message();
      message.what = SUCCESS_FETCH_ACCESSTOKEN;
      handleSendToken.sendMessage(message);
      
    } catch (UserRecoverableAuthException userRecoverableException) {
      
      Message message = new Message();
      message.what = NEED_PERMISSION;
      handleSendToken.sendMessage(message);
      
      loginActivity.startActivityForResult(userRecoverableException.getIntent(), requestCode);
      onError(userRecoverableException.getMessage(),requestCode, userRecoverableException);

    } catch (GoogleAuthException fatalException) {

      onError("Unrecoverable error " + fatalException.getMessage(),0, fatalException);
    } catch(Exception e){
      
      onError(e.getMessage(),0, e);
    }
  }
}




Java Source Code List

android.UnusedStub.java
com.example.todoapp.LoginActivity.java
com.example.todoapp.TaskModel.java
com.example.todoapp.TaskViewHolder.java
com.example.todoapp.TodoActivity.java
com.example.todoapp.TodoAppApplication.java
com.raweng.built.userInterface.BuiltAuthResultCallBack.java
com.raweng.built.userInterface.BuiltDetails.java
com.raweng.built.userInterface.BuiltListViewResultCallBack.java
com.raweng.built.userInterface.BuiltTwitterLoginActivity.java
com.raweng.built.userInterface.BuiltUIListViewController.java
com.raweng.built.userInterface.BuiltUILoginController.java
com.raweng.built.userInterface.BuiltUIPickerController.java
com.raweng.built.userInterface.BuiltUISignUpController.java
com.raweng.built.userInterface.BuiltioUserInterfaceActivity.java
com.raweng.built.userInterface.CustomEditTextErrorField.java
com.raweng.built.userInterface.GetGoogleAccessToken.java
com.raweng.built.userInterface.IGoogleLoginDelegate.java
com.raweng.built.userInterface.ResultDataSource.java
com.raweng.built.userInterface.UIAndroidExplorerScreen.java
com.raweng.built.userInterface.pulltorefresh.ILoadingLayout.java
com.raweng.built.userInterface.pulltorefresh.IPullToRefresh.java
com.raweng.built.userInterface.pulltorefresh.LoadingLayoutProxy.java
com.raweng.built.userInterface.pulltorefresh.OverscrollHelper.java
com.raweng.built.userInterface.pulltorefresh.PullToRefreshAdapterViewBase.java
com.raweng.built.userInterface.pulltorefresh.PullToRefreshBase.java
com.raweng.built.userInterface.pulltorefresh.PullToRefreshListView.java
com.raweng.built.userInterface.pulltorefresh.internal.EmptyViewMethodAccessor.java
com.raweng.built.userInterface.pulltorefresh.internal.FlipLoadingLayout.java
com.raweng.built.userInterface.pulltorefresh.internal.IndicatorLayout.java
com.raweng.built.userInterface.pulltorefresh.internal.LoadingLayout.java
com.raweng.built.userInterface.pulltorefresh.internal.RotateLoadingLayout.java
com.raweng.built.userInterface.pulltorefresh.internal.Utils.java
com.raweng.built.userInterface.pulltorefresh.internal.ViewCompat.java