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

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

Introduction

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

Prototype

public static GoogleAccountCredential usingOAuth2(Context context, Collection<String> scopes) 

Source Link

Document

Constructs a new instance using OAuth 2.0 scopes.

Usage

From source file:com.androidexperiments.sprayscape.unitydriveplugin.GoogleDriveUnityPlayerActivity.java

License:Apache License

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy

    mUnityPlayer = new UnityPlayer(this);
    setContentView(mUnityPlayer);/*from   www . j a v a 2s  . co m*/
    mUnityPlayer.requestFocus();

    credential = GoogleAccountCredential
            .usingOAuth2(getApplicationContext(),
                    Arrays.asList(DRIVE_FILE_SCOPE, DRIVE_APPFOLDER_SCOPE, PLUS_EMAIL_SCOPE))
            .setBackOff(new ExponentialBackOff());
}

From source file:com.andyscan.gdaademo.GooDrive.java

License:Open Source License

/************************************************************************************************
 * initialize Google Drive Api//from   w ww  . j  a v a 2s  .c  o  m
 * @param ctx   activity context
 * @param email  GOO account
 */
static void init(MainActivity ctx, String email) {
    UT.lg("init GDAA ");
    if (ctx != null && email != null) {
        //connect(false);
        mGAC = new GoogleApiClient.Builder(ctx).addApi(Drive.API).addScope(Drive.SCOPE_FILE)
                .setAccountName(email).addConnectionCallbacks(ctx).addOnConnectionFailedListener(ctx).build();

        mGOOSvc = new com.google.api.services.drive.Drive.Builder(AndroidHttp.newCompatibleTransport(),
                new GsonFactory(), GoogleAccountCredential
                        .usingOAuth2(ctx, Arrays.asList(DriveScopes.DRIVE_FILE)).setSelectedAccountName(email))
                                .build();

    }

}

From source file:com.bangz.shotrecorder.MainActivity.java

License:Apache License

private boolean backup() throws IOException {

    dbfileUri = Uri.fromFile(getDatabasePath(ShotRecordProvider.DATABASE_NAME));

    backup_or_restore = REQUEST_FOR_BACKUP_GOOGLE_DRIVE;

    // Check Google Play serevice is installed
    int r = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (r != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(r)) {
            GooglePlayServicesUtil.getErrorDialog(r, this, REQUEST_INSTALL_GOOGLEPLAYSERVICE).show();
        } else {//from  www  . j  ava 2  s.c  o m
            Toast.makeText(this, R.string.unsupport_google_drive, Toast.LENGTH_LONG).show();
            return false;
        }
    } else {
        // Author Google account
        String[] scopes = { DriveScopes.DRIVE, DriveScopes.DRIVE_APPDATA };
        credential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(scopes));
        startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
    }

    return true;
}

From source file:com.bangz.shotrecorder.MainActivity.java

License:Apache License

private boolean restore() throws IOException {
    //String topath = "/data/data/com.bangz.shotrecorder/databases/"+"shotrecord.db" ;
    //String topath = getFilesDir() + "/databases/shotrecord.db" ;
    dbfileUri = Uri.fromFile(getDatabasePath(ShotRecordProvider.DATABASE_NAME));

    backup_or_restore = REQUEST_FOR_RESTORE_GOOGLE_DRIVE;

    if (googleDriveService == null) {
        int r = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (r != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(r)) {
                GooglePlayServicesUtil.getErrorDialog(r, this, REQUEST_INSTALL_GOOGLEPLAYSERVICE).show();
            } else {
                Toast.makeText(this, R.string.unsupport_google_drive, Toast.LENGTH_LONG).show();
                return false;
            }/*  w  ww  .  j  a va2  s  .c o  m*/
        } else {
            String[] scopes = { DriveScopes.DRIVE_APPDATA };
            credential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(scopes));
            startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
        }
    } else {
        restoreFileFromDrive();
    }

    return true;
}

From source file:com.bingzer.android.driven.gdrive.app.GoogleDriveActivity.java

License:Apache License

private static GoogleAccountCredential createGoogleAccountCredential(Context context) {
    List<String> list = new ArrayList<String>();
    list.add(DriveScopes.DRIVE);/*  w w  w. j  ava  2  s .  c o  m*/

    return GoogleAccountCredential.usingOAuth2(context, list);
}

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  ww w. java2 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);
                        }//from   w ww  .j a  v a 2 s  .  c o  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();//from w w w.  ja  v  a 2 s . c  om
    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);
    }/* ww w .j a v  a 2 s  . 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.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 ww 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);
    }
}