Example usage for com.google.api.client.googleapis.extensions.android.accounts GoogleAccountManager getAccountByName

List of usage examples for com.google.api.client.googleapis.extensions.android.accounts GoogleAccountManager getAccountByName

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.extensions.android.accounts GoogleAccountManager getAccountByName.

Prototype

public Account getAccountByName(String accountName) 

Source Link

Document

Returns the Google account of the given Account#name .

Usage

From source file:com.example.android.notepad.NoteEditor.java

License:Apache License

private void requestSync() {
    if (mAccountName != null && mAccountName.length() > 0) {
        final GoogleAccountManager accountManager = new GoogleAccountManager(this);
        Account account = accountManager.getAccountByName(mAccountName);

        if (account != null) {
            Bundle options = new Bundle();
            options.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
            ContentResolver.requestSync(account, "com.google.provider.NotePad", options);
        }/*from  ww w  . j av a2  s .  c  o  m*/
    }
}

From source file:com.jefftharris.passwdsafe.sync.gdrive.GDriveProvider.java

License:Open Source License

@Override
public Account getAccount(String acctName) {
    GoogleAccountManager acctMgr = new GoogleAccountManager(itsContext);
    return acctMgr.getAccountByName(acctName);
}

From source file:com.jefftharris.passwdsafe.sync.MainActivity.java

License:Open Source License

/** Update the UI when the GDrive account is changed */
private void updateGdriveAccount(Cursor cursor) {
    boolean haveCursor = (cursor != null);
    GuiUtils.setVisible(findViewById(R.id.gdrive_container), haveCursor);
    GuiUtils.setVisible(findViewById(R.id.gdrive_separator), haveCursor);
    if (haveCursor) {
        long id = cursor.getLong(PasswdSafeContract.Providers.PROJECTION_IDX_ID);
        String acct = PasswdSafeContract.Providers.getDisplayName(cursor);
        int freqVal = cursor.getInt(PasswdSafeContract.Providers.PROJECTION_IDX_SYNC_FREQ);
        ProviderSyncFreqPref freq = ProviderSyncFreqPref.freqValueOf(freqVal);
        GoogleAccountManager acctMgr = new GoogleAccountManager(this);
        itsGdriveAccount = acctMgr.getAccountByName(acct);
        itsGdriveUri = ContentUris.withAppendedId(PasswdSafeContract.Providers.CONTENT_URI, id);

        View freqSpinLabel = findViewById(R.id.gdrive_interval_label);
        Spinner freqSpin = (Spinner) findViewById(R.id.gdrive_interval);
        freqSpin.setSelection(freq.getDisplayIdx());
        View syncBtn = findViewById(R.id.gdrive_sync);

        TextView acctView = (TextView) findViewById(R.id.gdrive_acct);
        boolean haveAccount = (itsGdriveAccount != null);
        if (haveAccount) {
            acctView.setText(itsGdriveAccount.name);
        } else {//from  w w w.j a  va  2s  . com
            acctView.setText(getString(R.string.account_not_exists_label, acct));
        }

        freqSpin.setEnabled(haveAccount);
        freqSpinLabel.setEnabled(haveAccount);
        syncBtn.setEnabled(haveAccount);
    } else {
        itsGdriveAccount = null;
        itsGdriveUri = null;
    }
}

From source file:com.jefftharris.passwdsafe.sync.PasswdSafeProvider.java

License:Open Source License

/** Execute a method */
private void doMethod(String[] args) {
    if (args.length < 1) {
        throw new IllegalArgumentException("No method args");
    }/*from   w  w w .j  a v  a2  s  .  co m*/

    String name = args[0];
    if (name.equals(PasswdSafeContract.Methods.METHOD_SYNC)) {
        if (args.length > 2) {
            throw new IllegalArgumentException("Invalid number of args");
        }

        Long id = null;
        if (args.length > 1) {
            Uri providerUri = Uri.parse(args[1]);
            int match = PasswdSafeContract.MATCHER.match(providerUri);
            if (match != PasswdSafeContract.MATCH_PROVIDER) {
                throw new IllegalArgumentException("Invalid provider URI: " + providerUri);
            }

            id = PasswdSafeContract.Providers.getId(providerUri);
        }

        SyncDb syncDb = SyncDb.acquire();
        List<DbProvider> providers;
        try {
            SQLiteDatabase db = syncDb.beginTransaction();
            if (id == null) {
                providers = SyncDb.getProviders(db);
            } else {
                DbProvider provider = SyncDb.getProvider(id, db);
                if (provider == null) {
                    return;
                }
                providers = Collections.singletonList(provider);
            }
            db.setTransactionSuccessful();
        } finally {
            syncDb.endTransactionAndRelease();
        }

        for (DbProvider provider : providers) {
            switch (provider.itsType) {
            case DROPBOX:
            case BOX:
            case ONEDRIVE:
            case OWNCLOUD: {
                Provider providerImpl = ProviderFactory.getProvider(provider.itsType, getContext());
                providerImpl.requestSync(true);
                break;
            }
            case GDRIVE: {
                GoogleAccountManager acctMgr = new GoogleAccountManager(getContext());
                Account acct = acctMgr.getAccountByName(provider.itsAcct);
                Uri uri = ContentUris.withAppendedId(PasswdSafeContract.Providers.CONTENT_URI, provider.itsId);
                ApiCompat.requestManualSync(acct, uri, null);
                break;
            }
            case GDRIVE_PLAY: {
                // TODO play: handle manual sync
                break;
            }
            }
        }
    } else {
        throw new IllegalArgumentException("Unknown method: " + name);
    }
}