org.chromium.sync.signin.AccountManagerHelper Java Examples

The following examples show how to use org.chromium.sync.signin.AccountManagerHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InvalidationService.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void requestAuthToken(final PendingIntent pendingIntent,
        @Nullable String invalidAuthToken) {
    @Nullable Account account = ChromeSigninController.get(this).getSignedInUser();
    if (account == null) {
        // This should never happen, because this code should only be run if a user is
        // signed-in.
        Log.w(TAG, "No signed-in user; cannot send message to data center");
        return;
    }

    // Attempt to retrieve a token for the user. This method will also invalidate
    // invalidAuthToken if it is non-null.
    AccountManagerHelper.get(this).getNewAuthTokenFromForeground(
            account, invalidAuthToken, getOAuth2ScopeWithType(),
            new AccountManagerHelper.GetAuthTokenCallback() {
                @Override
                public void tokenAvailable(String token) {
                    if (token != null) {
                        setAuthToken(InvalidationService.this.getApplicationContext(),
                                pendingIntent, token, getOAuth2ScopeWithType());
                    }
                }
            });
}
 
Example #2
Source File: ChildAccountService.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Checks for the presence of child accounts on the device.
 *
 * @param callback A callback which will be called with the result.
 */
public static void checkHasChildAccount(Context context, final Callback<Boolean> callback) {
    ThreadUtils.assertOnUiThread();
    if (!nativeIsChildAccountDetectionEnabled()) {
        callback.onResult(false);
        return;
    }
    final AccountManagerHelper helper = AccountManagerHelper.get(context);
    helper.getGoogleAccounts(new Callback<Account[]>() {
        @Override
        public void onResult(Account[] accounts) {
            if (accounts.length != 1) {
                callback.onResult(false);
            } else {
                helper.checkChildAccount(accounts[0], callback);
            }
        }
    });
}
 
Example #3
Source File: SyncController.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Trigger Chromium sign in of the given account.
 *
 * This also ensure that sync setup is not in progress anymore, so sync will start after
 * sync initialization has happened.
 *
 * @param activity the current activity.
 * @param accountName the full account name.
 */
public void signIn(Activity activity, String accountName) {
    final Account account = AccountManagerHelper.createAccountFromName(accountName);

    // The SigninManager handles most of the sign-in flow, and doFinishSignIn handles the
    // Chromium testshell specific details.
    SigninManager signinManager = SigninManager.get(mContext);
    signinManager.onFirstRunCheckDone();
    final boolean passive = false;
    signinManager.startSignIn(activity, account, passive, new SigninManager.Observer() {
        @Override
        public void onSigninComplete() {
            SigninManager.get(mContext).logInSignedInUser();
            mProfileSyncService.setSetupInProgress(false);
            mProfileSyncService.syncSignIn();
            start();
        }

        @Override
        public void onSigninCancelled() {
            stop();
        }
    });
}
 
Example #4
Source File: OAuth2TokenService.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Called by native to retrieve OAuth2 tokens.
 *
 * @param username The native username (full address).
 * @param scope The scope to get an auth token for (without Android-style 'oauth2:' prefix).
 * @param nativeCallback The pointer to the native callback that should be run upon completion.
 */
@CalledByNative
public static void getOAuth2AuthToken(
        Context context, String username, String scope, final int nativeCallback) {
    Account account = getAccountOrNullFromUsername(context, username);
    if (account == null) {
        nativeOAuth2TokenFetched(null, false, nativeCallback);
        return;
    }
    String oauth2Scope = OAUTH2_SCOPE_PREFIX + scope;

    AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
    accountManagerHelper.getAuthTokenFromForeground(
        null, account, oauth2Scope, new AccountManagerHelper.GetAuthTokenCallback() {
            @Override
            public void tokenAvailable(String token) {
                nativeOAuth2TokenFetched(
                    token, token != null, nativeCallback);
            }
        });
}
 
Example #5
Source File: SigninHelper.java    From delion with Apache License 2.0 6 votes vote down vote up
private void performResignin(String newName) {
    // This is the correct account now.
    final Account account = AccountManagerHelper.createAccountFromName(newName);

    mSigninManager.signIn(account, null, new SignInCallback() {
        @Override
        public void onSignInComplete() {
            if (mProfileSyncService != null) {
                mProfileSyncService.setSetupInProgress(false);
            }
            validateAccountSettings(true);
        }

        @Override
        public void onSignInAborted() {}
    });
}
 
Example #6
Source File: SyncStatusHelper.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Register with Android Sync Manager. This is what causes the "Chrome" option to appear in
 * Settings -> Accounts / Sync .
 *
 * @param account the account to enable Chrome sync on
 */
private void makeSyncable(Account account) {
    synchronized (mCachedSettings) {
        mCachedSettings.setIsSyncable(account);
    }

    StrictMode.ThreadPolicy oldPolicy = temporarilyAllowDiskWritesAndDiskReads();
    // Disable the syncability of Chrome for all other accounts. Don't use
    // our cache as we're touching many accounts that aren't signed in, so this saves
    // extra calls to Android sync configuration.
    Account[] googleAccounts = AccountManagerHelper.get(mApplicationContext).
            getGoogleAccounts();
    for (Account accountToSetNotSyncable : googleAccounts) {
        if (!accountToSetNotSyncable.equals(account) &&
                mSyncContentResolverWrapper.getIsSyncable(
                        accountToSetNotSyncable, mContractAuthority) > 0) {
            mSyncContentResolverWrapper.setIsSyncable(accountToSetNotSyncable,
                    mContractAuthority, 0);
        }
    }
    StrictMode.setThreadPolicy(oldPolicy);
}
 
Example #7
Source File: InvalidationService.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void requestAuthToken(final PendingIntent pendingIntent,
        @Nullable String invalidAuthToken) {
    @Nullable Account account = ChromeSigninController.get(this).getSignedInUser();
    if (account == null) {
        // This should never happen, because this code should only be run if a user is
        // signed-in.
        Log.w(TAG, "No signed-in user; cannot send message to data center");
        return;
    }

    // Attempt to retrieve a token for the user. This method will also invalidate
    // invalidAuthToken if it is non-null.
    AccountManagerHelper.get(this).getNewAuthTokenFromForeground(
            account, invalidAuthToken, getOAuth2ScopeWithType(),
            new AccountManagerHelper.GetAuthTokenCallback() {
                @Override
                public void tokenAvailable(String token) {
                    if (token != null) {
                        setAuthToken(InvalidationService.this.getApplicationContext(),
                                pendingIntent, token, getOAuth2ScopeWithType());
                    }
                }
            });
}
 
Example #8
Source File: SyncController.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Trigger Chromium sign in of the given account.
 *
 * This also ensure that sync setup is not in progress anymore, so sync will start after
 * sync initialization has happened.
 *
 * @param activity the current activity.
 * @param accountName the full account name.
 */
public void signIn(Activity activity, String accountName) {
    final Account account = AccountManagerHelper.createAccountFromName(accountName);

    // The SigninManager handles most of the sign-in flow, and doFinishSignIn handles the
    // Chromium testshell specific details.
    SigninManager signinManager = SigninManager.get(mContext);
    signinManager.onFirstRunCheckDone();
    final boolean passive = false;
    signinManager.startSignIn(activity, account, passive, new SigninManager.Observer() {
        @Override
        public void onSigninComplete() {
            SigninManager.get(mContext).logInSignedInUser();
            mProfileSyncService.setSetupInProgress(false);
            mProfileSyncService.syncSignIn();
            start();
        }

        @Override
        public void onSigninCancelled() {
            stop();
        }
    });
}
 
Example #9
Source File: OAuth2TokenService.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Called by native to retrieve OAuth2 tokens.
 *
 * @param username The native username (full address).
 * @param scope The scope to get an auth token for (without Android-style 'oauth2:' prefix).
 * @param nativeCallback The pointer to the native callback that should be run upon completion.
 */
@CalledByNative
public static void getOAuth2AuthToken(
        Context context, String username, String scope, final int nativeCallback) {
    Account account = getAccountOrNullFromUsername(context, username);
    if (account == null) {
        nativeOAuth2TokenFetched(null, false, nativeCallback);
        return;
    }
    String oauth2Scope = OAUTH2_SCOPE_PREFIX + scope;

    AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
    accountManagerHelper.getAuthTokenFromForeground(
        null, account, oauth2Scope, new AccountManagerHelper.GetAuthTokenCallback() {
            @Override
            public void tokenAvailable(String token) {
                nativeOAuth2TokenFetched(
                    token, token != null, nativeCallback);
            }
        });
}
 
Example #10
Source File: SyncStatusHelper.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Register with Android Sync Manager. This is what causes the "Chrome" option to appear in
 * Settings -> Accounts / Sync .
 *
 * @param account the account to enable Chrome sync on
 */
private void makeSyncable(Account account) {
    synchronized (mCachedSettings) {
        mCachedSettings.setIsSyncable(account);
    }

    StrictMode.ThreadPolicy oldPolicy = temporarilyAllowDiskWritesAndDiskReads();
    // Disable the syncability of Chrome for all other accounts. Don't use
    // our cache as we're touching many accounts that aren't signed in, so this saves
    // extra calls to Android sync configuration.
    Account[] googleAccounts = AccountManagerHelper.get(mApplicationContext).
            getGoogleAccounts();
    for (Account accountToSetNotSyncable : googleAccounts) {
        if (!accountToSetNotSyncable.equals(account) &&
                mSyncContentResolverWrapper.getIsSyncable(
                        accountToSetNotSyncable, mContractAuthority) > 0) {
            mSyncContentResolverWrapper.setIsSyncable(accountToSetNotSyncable,
                    mContractAuthority, 0);
        }
    }
    StrictMode.setThreadPolicy(oldPolicy);
}
 
Example #11
Source File: AccountChooserFragment.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    List<String> accountsList = AccountManagerHelper.get(getActivity()).getGoogleAccountNames();
    mAccounts = accountsList.toArray(new String[accountsList.size()]);
    return new AlertDialog.Builder(getActivity(), AlertDialog.THEME_HOLO_LIGHT)
            .setTitle(R.string.signin_select_account)
            .setSingleChoiceItems(mAccounts, mSelectedAccount, this)
            .setPositiveButton(R.string.signin_sign_in, this)
            .setNegativeButton(R.string.signin_cancel, this)
            .create();
}
 
Example #12
Source File: OAuth2TokenService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static Account getAccountOrNullFromUsername(Context context, String username) {
    if (username == null) {
        Log.e(TAG, "Username is null");
        return null;
    }

    AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
    Account account = accountManagerHelper.getAccountFromName(username);
    if (account == null) {
        Log.e(TAG, "Account not found for provided username.");
        return null;
    }
    return account;
}
 
Example #13
Source File: OAuth2TokenService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Called by native to list the accounts with OAuth2 refresh tokens.
 */
@CalledByNative
public static String[] getAccounts(Context context) {
    AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
    java.util.List<String> accountNames = accountManagerHelper.getGoogleAccountNames();
    return accountNames.toArray(new String[accountNames.size()]);
}
 
Example #14
Source File: OAuth2TokenService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
* Called by native to invalidate an OAuth2 token.
*/
@CalledByNative
public static void invalidateOAuth2AuthToken(Context context, String accessToken) {
    if (accessToken != null) {
        AccountManagerHelper.get(context).invalidateAuthToken(accessToken);
    }
}
 
Example #15
Source File: DelayedSyncController.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Resume any syncs that were delayed while Chromium was backgrounded.
 */
public boolean resumeDelayedSyncs(final Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String accountName = prefs.getString(DELAYED_ACCOUNT_NAME, null);
    if (accountName == null) {
        Log.d(TAG, "No delayed sync.");
        return false;
    } else {
        Log.d(TAG, "Handling delayed sync.");
        Account account = AccountManagerHelper.createAccountFromName(accountName);
        requestSyncOnBackgroundThread(context, account);
        return true;
    }
}
 
Example #16
Source File: OAuth2TokenService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
* Called by native to invalidate an OAuth2 token.
*/
@CalledByNative
public static void invalidateOAuth2AuthToken(Context context, String accessToken) {
    if (accessToken != null) {
        AccountManagerHelper.get(context).invalidateAuthToken(accessToken);
    }
}
 
Example #17
Source File: DelayedSyncController.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Resume any syncs that were delayed while Chromium was backgrounded.
 */
public boolean resumeDelayedSyncs(final Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String accountName = prefs.getString(DELAYED_ACCOUNT_NAME, null);
    if (accountName == null) {
        Log.d(TAG, "No delayed sync.");
        return false;
    } else {
        Log.d(TAG, "Handling delayed sync.");
        Account account = AccountManagerHelper.createAccountFromName(accountName);
        requestSyncOnBackgroundThread(context, account);
        return true;
    }
}
 
Example #18
Source File: OAuth2TokenService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Called by native to list the accounts with OAuth2 refresh tokens.
 */
@CalledByNative
public static String[] getAccounts(Context context) {
    AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
    java.util.List<String> accountNames = accountManagerHelper.getGoogleAccountNames();
    return accountNames.toArray(new String[accountNames.size()]);
}
 
Example #19
Source File: OAuth2TokenService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static Account getAccountOrNullFromUsername(Context context, String username) {
    if (username == null) {
        Log.e(TAG, "Username is null");
        return null;
    }

    AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
    Account account = accountManagerHelper.getAccountFromName(username);
    if (account == null) {
        Log.e(TAG, "Account not found for provided username.");
        return null;
    }
    return account;
}
 
Example #20
Source File: AccountChooserFragment.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    List<String> accountsList = AccountManagerHelper.get(getActivity()).getGoogleAccountNames();
    mAccounts = accountsList.toArray(new String[accountsList.size()]);
    return new AlertDialog.Builder(getActivity(), AlertDialog.THEME_HOLO_LIGHT)
            .setTitle(R.string.signin_select_account)
            .setSingleChoiceItems(mAccounts, mSelectedAccount, this)
            .setPositiveButton(R.string.signin_sign_in, this)
            .setNegativeButton(R.string.signin_cancel, this)
            .create();
}
 
Example #21
Source File: DelayedInvalidationsController.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Notify any invalidations that were delayed while Chromium was backgrounded.
 * @return whether there were any invalidations pending to be notified.
 */
public boolean notifyPendingInvalidations(final Context context) {
    SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
    String accountName = prefs.getString(DELAYED_ACCOUNT_NAME, null);
    if (accountName == null) {
        Log.d(TAG, "No pending invalidations.");
        return false;
    } else {
        Log.d(TAG, "Handling pending invalidations.");
        Account account = AccountManagerHelper.createAccountFromName(accountName);
        List<Bundle> bundles = popPendingInvalidations(context);
        notifyInvalidationsOnBackgroundThread(context, account, bundles);
        return true;
    }
}
 
Example #22
Source File: ProfileDataCache.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Initiate fetching the user accounts data (images and the full name).
 * Fetched data will be sent to observers of ProfileDownloader.
 */
public void update() {
    if (mProfile == null) return;

    Account[] accounts = AccountManagerHelper.get(mContext).getGoogleAccounts();
    for (int i = 0; i < accounts.length; i++) {
        if (mCacheEntries.get(accounts[i].name) == null) {
            ProfileDownloader.startFetchingAccountInfoFor(
                    mContext, mProfile, accounts[i].name, mImageSizePx, true);
        }
    }
}
 
Example #23
Source File: AccountManagementFragment.java    From delion with Apache License 2.0 5 votes vote down vote up
private void updateAccountsList() {
    PreferenceScreen prefScreen = getPreferenceScreen();
    if (prefScreen == null) return;

    for (int i = 0; i < mAccountsListPreferences.size(); i++) {
        prefScreen.removePreference(mAccountsListPreferences.get(i));
    }
    mAccountsListPreferences.clear();

    final Preferences activity = (Preferences) getActivity();
    Account[] accounts = AccountManagerHelper.get(activity).getGoogleAccounts();
    int nextPrefOrder = FIRST_ACCOUNT_PREF_ORDER;

    for (Account account : accounts) {
        ChromeBasePreference pref = new ChromeBasePreference(activity);
        pref.setSelectable(false);
        pref.setTitle(account.name);

        boolean isChildAccount = ChildAccountService.isChildAccount();

        pref.setIcon(new BitmapDrawable(getResources(),
                isChildAccount ? getBadgedUserPicture(account.name, getResources()) :
                    getUserPicture(account.name, getResources())));

        pref.setOrder(nextPrefOrder++);
        prefScreen.addPreference(pref);
        mAccountsListPreferences.add(pref);
    }
}
 
Example #24
Source File: InvalidationGcmUpstreamSender.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public void deliverMessage(final String to, final Bundle data) {
    @Nullable
    Account account = ChromeSigninController.get(this).getSignedInUser();
    if (account == null) {
        // This should never happen, because this code should only be run if a user is
        // signed-in.
        Log.w(TAG, "No signed-in user; cannot send message to data center");
        return;
    }

    final Bundle dataToSend = createDeepCopy(data);
    final Context applicationContext = getApplicationContext();

    // Attempt to retrieve a token for the user.
    OAuth2TokenService.getOAuth2AccessToken(this, account,
            SyncConstants.CHROME_SYNC_OAUTH2_SCOPE,
            new AccountManagerHelper.GetAuthTokenCallback() {
                @Override
                public void tokenAvailable(final String token) {
                    new AsyncTask<Void, Void, Void>() {
                        @Override
                        protected Void doInBackground(Void... voids) {
                            sendUpstreamMessage(to, dataToSend, token, applicationContext);
                            return null;
                        }
                    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                }

                @Override
                public void tokenUnavailable(boolean isTransientError) {
                    GcmUma.recordGcmUpstreamHistogram(
                            getApplicationContext(), GcmUma.UMA_UPSTREAM_TOKEN_REQUEST_FAILED);
                }
            });
}
 
Example #25
Source File: SigninManager.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Same as above but retrieves the Account object for the given accountName.
 */
public void signIn(String accountName, @Nullable final Activity activity,
        @Nullable final SignInCallback callback) {
    AccountManagerHelper.get(mContext).getAccountFromName(accountName, new Callback<Account>() {
        @Override
        public void onResult(Account account) {
            signIn(account, activity, callback);
        }
    });
}
 
Example #26
Source File: OAuth2TokenService.java    From delion with Apache License 2.0 5 votes vote down vote up
private static Account getAccountOrNullFromUsername(Context context, String username) {
    if (username == null) {
        Log.e(TAG, "Username is null");
        return null;
    }

    AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
    Account account = accountManagerHelper.getAccountFromName(username);
    if (account == null) {
        Log.e(TAG, "Account not found for provided username.");
        return null;
    }
    return account;
}
 
Example #27
Source File: OAuth2TokenService.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Called by native to list the activite account names in the OS.
 */
@VisibleForTesting
@CalledByNative
public static String[] getSystemAccountNames(Context context) {
    AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
    java.util.List<String> accountNames = accountManagerHelper.getGoogleAccountNames();
    return accountNames.toArray(new String[accountNames.size()]);
}
 
Example #28
Source File: OAuth2TokenService.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Called by native to retrieve OAuth2 tokens.
 *
 * @param username The native username (full address).
 * @param scope The scope to get an auth token for (without Android-style 'oauth2:' prefix).
 * @param nativeCallback The pointer to the native callback that should be run upon completion.
 */
@CalledByNative
public static void getOAuth2AuthToken(
        Context context, String username, String scope, final long nativeCallback) {
    Account account = getAccountOrNullFromUsername(context, username);
    if (account == null) {
        ThreadUtils.postOnUiThread(new Runnable() {
            @Override
            public void run() {
                nativeOAuth2TokenFetched(null, false, nativeCallback);
            }
        });
        return;
    }
    String oauth2Scope = OAUTH2_SCOPE_PREFIX + scope;

    AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
    accountManagerHelper.getAuthToken(
            account, oauth2Scope, new AccountManagerHelper.GetAuthTokenCallback() {
                @Override
                public void tokenAvailable(String token) {
                    nativeOAuth2TokenFetched(token, false, nativeCallback);
                }

                @Override
                public void tokenUnavailable(boolean isTransientError) {
                    nativeOAuth2TokenFetched(null, isTransientError, nativeCallback);
                }
            });
}
 
Example #29
Source File: OAuth2TokenService.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Called by native to check wether the account has an OAuth2 refresh token.
 */
@CalledByNative
public static boolean hasOAuth2RefreshToken(Context context, String accountName) {
    // Temporarily allowing disk read while fixing. TODO: http://crbug.com/618096.
    // This function is called in RefreshTokenIsAvailable of OAuth2TokenService which is
    // expected to be called in the UI thread synchronously.
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
    try {
        return AccountManagerHelper.get(context).hasAccountForName(accountName);
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }
}
 
Example #30
Source File: OAuth2TokenService.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
* Called by native to invalidate an OAuth2 token.
*/
@CalledByNative
public static void invalidateOAuth2AuthToken(Context context, String accessToken) {
    if (accessToken != null) {
        AccountManagerHelper.get(context).invalidateAuthToken(accessToken);
    }
}