Example usage for android.accounts AccountManager get

List of usage examples for android.accounts AccountManager get

Introduction

In this page you can find the example usage for android.accounts AccountManager get.

Prototype

public static AccountManager get(Context context) 

Source Link

Document

Gets an AccountManager instance associated with a Context.

Usage

From source file:com.samsung.android.remindme.jsonrpc.AuthenticatedJsonRpcJavaClient.java

public void blockingAuthenticateAccount(final Account account, final int needAuthAction,
        boolean forceReauthenticate) throws AuthenticationException, OperationCanceledException,
        RequestedUserAuthenticationException, InvalidAuthTokenException {

    String existingToken = mTokenStoreHelper.getToken(account);
    if (!forceReauthenticate && existingToken != null) {
        BasicClientCookie c = new BasicClientCookie("ACSID", existingToken);
        try {//from  w  w  w  .j a v  a2  s. co m
            c.setDomain(new URI(Config.SERVER_BASE_URL).getHost());
            mHttpClient.getCookieStore().addCookie(c);
            return;
        } catch (URISyntaxException e) {
        }
    }

    // Get an auth token for this account.
    AccountManager am = AccountManager.get(mContext);
    Bundle authBundle = null;
    String authToken = null;

    // Block on getting the auth token result.
    try {
        authBundle = am.getAuthToken(account, APPENGINE_SERVICE_NAME, needAuthAction == NEED_AUTH_NOTIFICATION,
                null, null).getResult();
    } catch (IOException e) {
        throw new AuthenticationException("IOException while getting auth token.", e);
    } catch (AuthenticatorException e) {
        throw new AuthenticationException("AuthenticatorException while getting auth token.", e);
    }

    if (authBundle.containsKey(AccountManager.KEY_INTENT) && needAuthAction == NEED_AUTH_INTENT) {
        Intent authRequestIntent = (Intent) authBundle.get(AccountManager.KEY_INTENT);
        mContext.startActivity(authRequestIntent);
        throw new RequestedUserAuthenticationException();
    } else if (authBundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
        authToken = authBundle.getString(AccountManager.KEY_AUTHTOKEN);
        System.out.println(authToken);
        System.out.println(AccountManager.KEY_AUTHTOKEN);
    }

    if (authToken == null) {
        throw new AuthenticationException("Retrieved auth token was null.");
    }

    try {
        blockingAuthenticateWithToken(account, authToken);
    } catch (InvalidAuthTokenException e) {
        am.invalidateAuthToken(account.type, authToken);
        throw e;
    }
}

From source file:com.alphabetbloc.accessmrs.services.SyncManager.java

public static void syncData() {
    if (App.DEBUG)
        Log.v(TAG, "SyncData is Requested");
    AccountManager accountManager = AccountManager.get(App.getApp());
    Account[] accounts = accountManager.getAccountsByType(App.getApp().getString(R.string.app_account_type));
    if (accounts.length > 0) {

        sStartSync.set(true);//  ww  w.  ja  v  a  2 s. c o  m

        Bundle bundle = new Bundle();
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_FORCE, true);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);

        // //this resets the scheduled sync
        ContentResolver.requestSync(accounts[0], App.getApp().getString(R.string.app_provider_authority),
                bundle);
    } else
        UiUtils.toastAlert(App.getApp().getString(R.string.sync_error),
                App.getApp().getString(R.string.no_account_setup));
}

From source file:com.pindroid.authenticator.AuthenticatorActivity.java

/**
 * {@inheritDoc}/*  w w  w.  j a  v  a  2 s. c  o m*/
 */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    mAccountAuthenticatorResponse = getIntent()
            .getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);

    if (mAccountAuthenticatorResponse != null) {
        mAccountAuthenticatorResponse.onRequestContinued();
    }

    mAccountManager = AccountManager.get(this);
    final Intent intent = getIntent();
    mUsername = intent.getStringExtra(PARAM_USERNAME);
    mRequestNewAccount = mUsername == null;
    mConfirmCredentials = intent.getBooleanExtra(PARAM_CONFIRMCREDENTIALS, false);

    setContentView(R.layout.login_activity);

    mMessage = (TextView) findViewById(R.id.message);
    mUsernameEdit = (EditText) findViewById(R.id.username_edit);
    mPasswordEdit = (EditText) findViewById(R.id.password_edit);

    mPasswordEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                startLogin();
                return true;
            } else {
                return false;
            }
        }
    });

    if (!TextUtils.isEmpty(mUsername)) {
        mUsernameEdit.setText(mUsername);
        mPasswordEdit.requestFocus();
    }
}

From source file:com.ntsync.android.sync.activities.KeyPasswordActivity.java

@Override
protected void onResume() {
    super.onResume();
    // Check if Account is available
    AccountManager acm = AccountManager.get(this);
    boolean found = false;
    for (Account accounts : acm.getAccounts()) {
        if (Constants.ACCOUNT_TYPE.equals(accounts.type) && TextUtils.equals(accounts.name, mUsername)) {
            found = true;/*  ww  w . j a v a  2s.c o m*/
        }
    }
    if (!found) {
        clearNotification(mUsername);
        setResult(RESULT_CANCELED);
        finish();
        return;
    }
}

From source file:com.hemou.android.account.AccountUtils.java

public static void updateAcntDataTotally(Context ctx, UsrSelfDTO dto) {
    if (ctx == null)
        throw new IllegalArgumentException();
    Account acnt = getAccount(ctx);//  w ww.  ja v  a  2  s . c om
    if (acnt == null) {
        LogUtils.e("The user aliving should be added to the AccountManager framework......");
        return;
    }
    AccountManager.get(ctx).setUserData(acnt, Intents.EXTRA.USER, StrUtils.obj2Str(dto));
}

From source file:com.citrus.sdk.operations.OneClicksignup.java

public static final String getDefaultGmail(Activity activity) {
    String myEmailid = "";
    Account[] accounts = AccountManager.get(activity).getAccountsByType("com.google");
    if (accounts.length != 0) {
        myEmailid = accounts[0].name;//  www .  j av  a 2s.c o  m
    }
    return myEmailid;
}

From source file:net.translatewiki.app.TranslateWikiApp.java

public Boolean revalidateAuthToken() {
    AccountManager accountManager = AccountManager.get(this);
    Account curAccount = getCurrentAccount();

    if (curAccount == null) {
        return false; // This should never happen
    }//from w  w w .  j av  a 2 s  .c om

    accountManager.invalidateAuthToken(getString(R.string.account_type_identifier), api.getAuthCookie());
    try {
        String authCookie = accountManager.blockingGetAuthToken(curAccount, "", false);
        api.setAuthCookie(authCookie);
        return true;
    } catch (OperationCanceledException e) {
        e.printStackTrace();
        return false;
    } catch (AuthenticatorException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.rukman.emde.smsgroups.authenticator.GMSAuthenticatorActivity.java

/**
 * {@inheritDoc}// w  ww .  ja  v a 2s . c om
 */
@Override
public void onCreate(Bundle icicle) {

    Log.i(TAG, "onCreate(" + icicle + ")");
    super.onCreate(icicle);
    mAccountManager = AccountManager.get(this);
    Log.i(TAG, "loading data from Intent");
    final Intent intent = getIntent();
    mUsername = intent.getStringExtra(PARAM_USERNAME);
    mIsUnknownAccountName = mUsername == null;
    mConfirmCredentials = intent.getBooleanExtra(PARAM_CONFIRM_CREDENTIALS, false);
    Log.i(TAG, "IsUnknownAccount? : " + mIsUnknownAccountName);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    setContentView(R.layout.login_activity);
    getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_dialog_alert);
    mMessage = (TextView) findViewById(R.id.message);
    mUsernameEdit = (EditText) findViewById(R.id.username_edit);
    mPasswordEdit = (EditText) findViewById(R.id.password_edit);
    mEmailEdit = (EditText) findViewById(R.id.email_edit);
    mPhoneEdit = (EditText) findViewById(R.id.phone_edit);
    ((CheckBox) findViewById(R.id.new_account_box)).setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            findViewById(R.id.registration_layout).setVisibility(isChecked ? View.VISIBLE : View.GONE);
        }

    });
    if (!TextUtils.isEmpty(mUsername)) {
        mUsernameEdit.setText(mUsername);
    }
    mMessage.setText(getMessage());
}

From source file:com.prestomation.android.sospy.monitor.AppEngineClient.java

public String getASCIDCookie(boolean https) throws Exception {

    // Get auth token for account
    Account account = new Account(mAccountName, "com.google");
    String authToken = getAuthToken(mContext, account);
    if (authToken == null) {
        throw new PendingAuthException(mAccountName);
    }//from  w  ww. ja  va2 s  .com
    AccountManager accountManager = AccountManager.get(mContext);
    accountManager.invalidateAuthToken(account.type, authToken);
    authToken = getAuthToken(mContext, account);

    // Get ACSID cookie
    DefaultHttpClient client = new DefaultHttpClient();
    String continueURL = BASE_URL;
    String sURI = AUTH_URL + "?continue=" + URLEncoder.encode(continueURL, "UTF-8") + "&auth=" + authToken;
    if (https == false) {
        sURI = sURI.replace("https", "http");
    }
    URI uri = new URI(sURI);
    HttpGet method = new HttpGet(uri);
    final HttpParams getParams = new BasicHttpParams();
    HttpClientParams.setRedirecting(getParams, false); // continue is not
    // used
    method.setParams(getParams);

    HttpResponse res = client.execute(method);
    Header[] headers = res.getHeaders("Set-Cookie");
    if (res.getStatusLine().getStatusCode() != 302 || headers.length == 0) {
        return res.toString();
    }

    String ascidCookie = null;
    for (Header header : headers) {
        if (header.getValue().indexOf("ACSID=") >= 0) {
            // let's parse it
            String value = header.getValue();
            String[] pairs = value.split(";");
            ascidCookie = pairs[0];
        }
    }
    Log.i(TAG, "Received ASCIDCookie: " + ascidCookie);
    return ascidCookie;
}