Example usage for com.google.api.client.googleapis.extensions.android.gms.auth GoogleAccountCredential setSelectedAccountName

List of usage examples for com.google.api.client.googleapis.extensions.android.gms.auth GoogleAccountCredential setSelectedAccountName

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.extensions.android.gms.auth GoogleAccountCredential setSelectedAccountName.

Prototype

public final GoogleAccountCredential setSelectedAccountName(String accountName) 

Source Link

Document

Sets the selected Google account name (e-mail address) -- for example "johndoe@gmail.com" -- or null for none.

Usage

From source file:br.com.bioscada.apps.biotracks.io.sendtogoogle.SendToGoogleUtils.java

License:Apache License

/**
 * Gets the google account credential./*from www.  j  a  va 2 s  .  c o  m*/
 * 
 * @param context the context
 * @param accountName the account name
 * @param scope the scope
 */
public static GoogleAccountCredential getGoogleAccountCredential(Context context, String accountName,
        String scope) throws IOException, GoogleAuthException {
    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
    credential.setSelectedAccountName(accountName);
    credential.getToken();
    return credential;
}

From source file:br.com.bioscada.apps.biotracks.io.sendtogoogle.SendToGoogleUtils.java

License:Apache License

/**
 * Gets the OAuth2 token.//  w w  w.  j av a 2  s  . co  m
 * 
 * @param context the context
 * @param accountName the account name
 * @param scope the scope
 */
public static String getToken(Context context, String accountName, String scope)
        throws IOException, GoogleAuthException {
    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
    credential.setSelectedAccountName(accountName);
    return credential.getToken();
}

From source file:com.battlelancer.seriesguide.backend.HexagonTools.java

License:Apache License

/**
 * Checks if the given Google account exists and can be accessed.
 *//*from w  ww.  ja  va 2  s  .com*/
public static boolean validateAccount(Context context, String accountName) {
    GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context.getApplicationContext(),
            HexagonSettings.AUDIENCE);
    // set account, credential tries to fetch Google account from Android AccountManager
    credential.setSelectedAccountName(accountName);

    if (credential.getSelectedAccountName() == null) {
        Timber.e("validateAccount: failed to get Google account from AccountManager.");
        return false;
    }

    return true;
}

From source file:com.bufarini.reminders.ui.Reminders.java

License:Apache License

public static Tasks getGoogleTasksService(final Context context, String accountName) {
    final GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context,
            ListManager.TASKS_SCOPES);/*from   w  w  w  .  j  a v  a  2  s. c  o m*/
    credential.setSelectedAccountName(accountName);
    Tasks googleService = new Tasks.Builder(TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(DateUtils.getAppName(context))
            .setHttpRequestInitializer(new HttpRequestInitializer() {
                @Override
                public void initialize(HttpRequest httpRequest) {
                    credential.initialize(httpRequest);
                    httpRequest.setConnectTimeout(3 * 1000); // 3 seconds connect timeout
                    httpRequest.setReadTimeout(3 * 1000); // 3 seconds read timeout
                }
            }).build();
    return googleService;
}

From source file:com.bufarini.reminders.ui.tasklists.ListManager.java

License:Apache License

private void authorise(final String accountName, final int requestCode,
        final IfAlreadyAuthorised ifAlreadyAuthorised, final ImageButton syncButton) {
    if (!isAccountAuthorised(accountName)) {
        final Activity activity = getActivity();
        GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(activity, TASKS_SCOPES);
        credential.setSelectedAccountName(accountName);
        final Tasks googleService = new Tasks.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(),
                credential).setApplicationName(DateUtils.getAppName(activity)).build();
        new Thread(new Runnable() {
            public void run() {
                try {
                    googleService.tasklists().list().execute();
                    saveAuthorisationForAccount(accountName);
                    isSyncWithGTasksEnabled = true;
                    ifAlreadyAuthorised.doAction();
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            syncButton.setVisibility(View.VISIBLE);
                        }// w w w .  j a v  a 2  s  .co m
                    });
                } catch (UserRecoverableAuthIOException userRecoverableException) {
                    startActivityForResult(userRecoverableException.getIntent(), requestCode);
                } catch (IOException e) {
                    Log.e(LOGTAG,
                            "authorise() :: cannot contact google servers for account +\"" + accountName + "\"",
                            e);
                }
            }
        }).start();
    } else
        ifAlreadyAuthorised.doAction();
}

From source file:com.dsna.android.main.MainActivity.java

License:Apache License

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo_activity_main);

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    _initMenu();/*  www .j  ava 2 s .  c o  m*/
    mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
    mDrawer.setDrawerListener(mDrawerToggle);

    //-----------------------------------------------------------------
    //BaseFragment baseFragment = null;
    if (savedInstanceState != null) {
        mSelectedFragment = savedInstanceState.getInt(BUNDLE_SELECTEDFRAGMENT);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (fragmentManager.findFragmentById(R.id.fragment_main) == null)
            mBaseFragment = selectFragment(mSelectedFragment);
        //if (mBaseFragment==null)
        //    mBaseFragment = selectFragment(mSelectedFragment);
    } else {
        mBaseFragment = new NewFeedsFragment(feeds);
        openFragment(mBaseFragment);
    }

    // Store the booting information to pass to the service
    mBootIp = getIntent().getStringExtra(bIp);
    mBootPort = getIntent().getStringExtra(bPort);
    mBindPort = getIntent().getStringExtra(biPort);
    mUsername = getIntent().getStringExtra(uName);

    // Initiate database helper
    dbHelper = new DatabaseHandler(this, mUsername);

    // Initiate cipher parameters
    publicKeys = null;
    secretKeys = null;
    ps06 = new PS06();
    cd07 = new IBBECD07();

    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(this,
            java.util.Arrays.asList(DriveScopes.DRIVE));
    credential.setSelectedAccountName(mUsername);
    new googleCloudAuthorizationRequestTask().execute(credential);

}

From source file:com.flowzr.budget.holo.export.flowzr.GoogleDrivePictureClient.java

License:Open Source License

public static Drive create(Context context) throws IOException, GoogleAuthException, ImportExportException {
    String googleDriveAccount = MyPreferences.getGoogleDriveAccount(context);
    if (googleDriveAccount == null) {
        throw new ImportExportException(R.string.google_drive_account_required);
    }/*from   w  w  w.j a v a 2 s . c o  m*/
    try {
        List<String> scope = new ArrayList<String>();
        scope.add(DriveScopes.DRIVE_FILE);
        if (MyPreferences.isGoogleDriveFullReadonly(context)) {
            scope.add(DriveScopes.DRIVE_READONLY);
        }
        GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
        credential.setSelectedAccountName(googleDriveAccount);
        credential.getToken();
        return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
    } catch (UserRecoverableAuthException e) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent authorizationIntent = e.getIntent();
        authorizationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_FROM_BACKGROUND);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, authorizationIntent, 0);
        Notification notification = new NotificationCompat.Builder(context)
                .setSmallIcon(android.R.drawable.ic_dialog_alert)
                .setTicker(context.getString(R.string.google_drive_permission_requested))
                .setContentTitle(context.getString(R.string.google_drive_permission_requested))
                .setContentText(context.getString(R.string.google_drive_permission_requested_for_account,
                        googleDriveAccount))
                .setContentIntent(pendingIntent).setAutoCancel(true).build();
        notificationManager.notify(0, notification);
        throw new ImportExportException(R.string.google_drive_permission_required);
    }
}

From source file:com.flowzr.export.flowzr.GoogleDrivePictureClient.java

License:Open Source License

public static Drive create(Context context) throws IOException, ImportExportException, GoogleAuthException {
    String googleDriveAccount = MyPreferences.getGoogleDriveAccount(context);
    if (googleDriveAccount == null) {
        throw new ImportExportException(R.string.google_drive_account_required);
    }//from  w w  w  . j a va 2s . c  om
    try {
        List<String> scope = new ArrayList<String>();
        scope.add(DriveScopes.DRIVE_FILE);
        if (MyPreferences.isGoogleDriveFullReadonly(context)) {
            scope.add(DriveScopes.DRIVE_READONLY);
        }
        GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
        credential.setSelectedAccountName(googleDriveAccount);
        credential.getToken();
        return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
    } catch (UserRecoverableAuthException e) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent authorizationIntent = e.getIntent();
        authorizationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_FROM_BACKGROUND);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, authorizationIntent, 0);
        Notification notification = new NotificationCompat.Builder(context)
                .setSmallIcon(android.R.drawable.ic_dialog_alert)
                .setTicker(context.getString(R.string.google_drive_permission_requested))
                .setContentTitle(context.getString(R.string.google_drive_permission_requested))
                .setContentText(context.getString(R.string.google_drive_permission_requested_for_account,
                        googleDriveAccount))
                .setContentIntent(pendingIntent).setAutoCancel(true).build();
        notificationManager.notify(0, notification);
        throw new ImportExportException(R.string.google_drive_permission_required);
    }
}

From source file:com.google.cloud.genomics.android.CredentialActivity.java

License:Apache License

private static GoogleAccountCredential getCredential(Context context) {
    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context,
            Collections.singleton(GenomicsScopes.GENOMICS));
    credential.setSelectedAccountName(SettingsActivity.getAccountName(context));
    return credential;
}

From source file:com.google.cloud.solutions.griddler.android.providers.DataProvider.java

License:Open Source License

/**
 * Constructor/* ww w. j a va 2s.  c  o m*/
 *
 * @param context The context
 */
public DataProvider(Context context) {
    settings = new ApplicationSettings(context);

    GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context,
            GameBackendSettings.AUDIENCE_ID);
    credential.setSelectedAccountName(settings.getSelectedAccountName());

    Griddler.Builder builder = new Griddler.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(),
            credential);
    builder.setApplicationName(context.getString(R.string.app_name));
    builder.setRootUrl(GameBackendSettings.DEFAULT_ROOT_URL);
    service = builder.build();
}