Android Open Source - android-google-spreadsheets-api Spreadsheet O Auth Activity






From Project

Back to project page android-google-spreadsheets-api.

License

The source code is released under:

Apache License

If you think the Android project android-google-spreadsheets-api 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.github.spreadsheets.android.api.oauth;
/*from ww w .  java 2 s  .c  o  m*/
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
import com.google.android.gms.auth.UserRecoverableAuthException;

import java.io.IOException;

/**
 * Activity to manage getting Google OAuth token
 * Needs to have EXTRA_EMAIL and EXTRA_SCOPE extras set
 */
public class SpreadsheetOAuthActivity extends Activity {
    public static final String EXTRA_EMAIL = "extra_email";
    public static final String EXTRA_SCOPE = "extra_scope";
    public static final String EXTRA_RESULT_MESSAGE = "extra_result_message";
    public static final String EXTRA_TOKEN = "extra_token";

    static final int REQUEST_CODE_RECOVER_FROM_AUTH_ERROR = 1001;

    private String scope;
    private String email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle bundle = getIntent().getExtras();
        assert bundle != null;
        if (bundle.containsKey(EXTRA_EMAIL) && bundle.containsKey(EXTRA_SCOPE)) {
            scope = bundle.getString(EXTRA_SCOPE);
            email = bundle.getString(EXTRA_EMAIL);
            getAndUseAuthTokenInAsyncTask();
        } else {
            setResult(RESULT_CANCELED);
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_RECOVER_FROM_AUTH_ERROR) {
            handleAuthorizeResult(resultCode, data);
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void handleAuthorizeResult(int resultCode, Intent data) {
        if (data == null) {
            setResult(RESULT_CANCELED);
        }
        if (resultCode == RESULT_OK) {
            getAndUseAuthTokenBlocking();
        }
        if (resultCode == RESULT_CANCELED) {
            setResult(RESULT_CANCELED);
        }
    }

    // Example of how to use the GoogleAuthUtil in a blocking, non-main thread context
    void getAndUseAuthTokenBlocking() {
        try {
            // Retrieve a token for the given account and scope. It will always return either
            // a non-empty String or throw an exception.
            final String token = GoogleAuthUtil.getToken(this, email, scope);
            final Intent intent = new Intent();
            intent.putExtra(EXTRA_TOKEN, token);
            setResult(RESULT_OK, intent);
        } catch (GooglePlayServicesAvailabilityException playEx) {
            final Intent intent = new Intent();
            intent.putExtra(EXTRA_RESULT_MESSAGE, "GooglePlayServicesAvailable");
            setResult(RESULT_CANCELED, intent);
        } catch (UserRecoverableAuthException userAuthEx) {
            // Start the user recoverable action using the intent returned by
            // getIntent()
            startActivityForResult(userAuthEx.getIntent(), REQUEST_CODE_RECOVER_FROM_AUTH_ERROR);
            setResult(RESULT_OK);
        } catch (IOException transientEx) {
            // network or server error, the call is expected to succeed if you try again later.
            // Don't attempt to call again immediately - the request is likely to
            // fail, you'll hit quotas or back-off.
            final Intent intent = new Intent();
            intent.putExtra(EXTRA_RESULT_MESSAGE, "IOException");
            setResult(RESULT_CANCELED, intent);
        } catch (GoogleAuthException authEx) {
            // Failure. The call is not expected to ever succeed so it should not be
            // retried.
            final Intent intent = new Intent();
            intent.putExtra(EXTRA_RESULT_MESSAGE, "GoogleAuthException");
            setResult(RESULT_CANCELED, intent);
        }
        finish();
    }

    void getAndUseAuthTokenInAsyncTask() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                getAndUseAuthTokenBlocking();
                return null;
            }
        }.execute();
    }
}




Java Source Code List

com.example.android.spreadsheet.sample.MainActivity.java
com.github.spreadsheets.android.api.model.Author.java
com.github.spreadsheets.android.api.model.BatchOperation.java
com.github.spreadsheets.android.api.model.BatchStatus.java
com.github.spreadsheets.android.api.model.CellEntry.java
com.github.spreadsheets.android.api.model.CellFeed.java
com.github.spreadsheets.android.api.model.Cell.java
com.github.spreadsheets.android.api.model.Content.java
com.github.spreadsheets.android.api.model.Entry.java
com.github.spreadsheets.android.api.model.Feed.java
com.github.spreadsheets.android.api.model.Link.java
com.github.spreadsheets.android.api.model.ListEntry.java
com.github.spreadsheets.android.api.model.ListFeed.java
com.github.spreadsheets.android.api.model.List.java
com.github.spreadsheets.android.api.model.SpreadsheetEntry.java
com.github.spreadsheets.android.api.model.SpreadsheetFeed.java
com.github.spreadsheets.android.api.model.SpreadsheetUrl.java
com.github.spreadsheets.android.api.model.WorksheetData.java
com.github.spreadsheets.android.api.model.WorksheetEntry.java
com.github.spreadsheets.android.api.model.WorksheetFeed.java
com.github.spreadsheets.android.api.oauth.SpreadsheetOAuthActivity.java
com.github.spreadsheets.android.api.oauth.SpreadsheetOAuth.java
com.github.spreadsheets.android.api.requests.CellEntryRequest.java
com.github.spreadsheets.android.api.requests.CellFeedRequest.java
com.github.spreadsheets.android.api.requests.ListEntryRequest.java
com.github.spreadsheets.android.api.requests.ListFeedRequest.java
com.github.spreadsheets.android.api.requests.SpreadsheetEntryRequest.java
com.github.spreadsheets.android.api.requests.SpreadsheetFeedRequest.java
com.github.spreadsheets.android.api.requests.SpreadsheetRequest.java
com.github.spreadsheets.android.api.requests.WorksheetEntryRequest.java
com.github.spreadsheets.android.api.requests.WorksheetFeedRequest.java