Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import android.os.Bundle;

import android.preference.PreferenceManager;

public class Main {
    private static final String DOCS_SCOPE = "oauth2:https://docs.google.com/feeds/";

    /**
     * This version is for running in the foreground. It shows the user an access request screen.
     * @param accountManager
     * @param account
     * @param ota
     * @param activity
     */
    public static void getAuthTokenFromAccountManager(AccountManager accountManager, Account account,
            AccountManagerCallback<Bundle> ota, Activity activity) {
        Bundle bundle = new Bundle();
        accountManager.getAuthToken(account, // Account to use
                DOCS_SCOPE, // Authorization scope
                bundle, // Authenticator-specific options
                activity, // Your activity
                ota, // Callback called when a token is successfully acquired
                null); // Callback called if an error occurs
    }

    /**
     * This version is for running in the background. It may show the user an access request screen in the
     * notification area for the user to authorize the app
     * @param accountManager
     * @param account
     * @param ota
     * @param activity
     */
    public static void getAuthTokenFromAccountManager(AccountManager accountManager, Account account,
            AccountManagerCallback<Bundle> ota) {

        accountManager.getAuthToken(account, // Account retrieved using getAccountsByType()
                DOCS_SCOPE, // Authorization scope
                true, // Callback
                ota, // Callback called when a token is successfully acquired
                null); // Callback called if an error occurs    
    }

    /**
     * Gets the stored authToken, which may be expired
     * @param applicationContext
     * @return the authToken, which may be expired
     */
    public static String getAuthToken(Context applicationContext) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
        return prefs.getString("GDOCS_AUTH_TOKEN", "");
    }
}