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.android.projectz.teamrocket.thebusapp.util.Device.java

/**
 * permette di prendere il probabile indirizzo email del telefono
 *
 * @param context//from  w ww .j a  v  a2s.com
 * @return
 */
public static String getDeviceEMAIL(Context context) {
    AccountManager manager = AccountManager.get(context);
    if (ActivityCompat.checkSelfPermission(context,
            Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
        return "ERR_PERMISSION";
    }
    Account[] accounts = manager.getAccountsByType("com.google");
    List<String> possibleEmails = new LinkedList<String>();

    for (Account account : accounts) {
        possibleEmails.add(account.name);
    }
    if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
        String email = possibleEmails.get(0);
        String[] parts = email.split("@");

        if (parts.length > 1)
            return parts[0];
    }
    return null;
}

From source file:com.pindroid.service.AccountService.java

@Override
protected void onHandleIntent(Intent intent) {
    mAccountManager = AccountManager.get(this);

    Account[] accounts = mAccountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
    ArrayList<String> accountsList = new ArrayList<String>();
    for (int i = 0; i < accounts.length; i++) {
        accountsList.add(accounts[i].name);
    }/* ww w. j  a v  a2 s . co  m*/

    BookmarkManager.TruncateBookmarks(accountsList, this, true);
    TagManager.TruncateOldTags(accountsList, this);

    if (accounts.length > 0) {
        for (Account a : accounts) {
            Log.d("AS Handle", "Getting Token for " + a.name);

            try {
                String token = PinboardApi.getSecretToken(a, this);
                mAccountManager.setUserData(a, Constants.PREFS_SECRET_TOKEN, token);
            } catch (AuthenticationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TooManyRequestsException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:net.alchemiestick.katana.winehqappdb.Metrics.java

private String getAcc() {
    Account[] accounts = AccountManager.get(this.main).getAccountsByType("com.google");
    String devID = Secure.getString(this.main.getContentResolver(), Secure.ANDROID_ID);
    String myEmailid = accounts[0].name + ":" + devID;
    return myEmailid;
}

From source file:com.manning.androidhacks.hack023.HackApplication.java

public Account getCurrentAccount() {
    AccountManager accountManager = AccountManager.get(this);
    Account[] accounts = accountManager.getAccountsByType(AuthenticatorActivity.PARAM_ACCOUNT_TYPE);

    if (accounts.length > 0) {
        return accounts[0];
    } else {/*from w w  w . j  av a2 s . com*/
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return null;
    }
}

From source file:edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticator.java

public static Account[] getAccounts(Context context, String accountType) {
    return AccountManager.get(context).getAccountsByType(accountType);
}

From source file:net.heroicefforts.viable.android.rep.it.auth.Authenticate.java

/**
 * Attempts to authenticate the user using a pre-existing stored authentication token.  If an account exists, but no such token 
 * exists, then the user will be prompted by the account authenticator to re-enter their Google credentials to generate the new token.
 * //from w  w w.j  a v a  2  s. c  o m
 * @param act the calling activity
 * @return the authentication token for the requested service or null if there is no Google Account.
 * @throws AuthenticatorException if an error occurs during authentication.
 * @throws OperationCanceledException
 * @throws IOException
 */
public static String authenticate(Activity act, String serviceCode)
        throws AuthenticatorException, OperationCanceledException, IOException {
    AccountManager mgr = AccountManager.get(act);
    Account[] accts = mgr.getAccountsByType(GCLAccountAuthenticator.ACCT_TYPE);
    if (accts.length > 0) {
        Account acct = accts[0];
        AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(acct, serviceCode, null, act, null,
                null);
        Bundle authTokenBundle = accountManagerFuture.getResult();
        String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();

        return authToken;
    } else {
        Log.e(TAG, "No google accounts registered for this device.");
        return null;
    }
}

From source file:com.geertvanderploeg.kiekeboek.syncadapter.SyncAdapter.java

public SyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);
    this.context = context;
    mAccountManager = AccountManager.get(context);
}

From source file:cc.vileda.sipgatesync.contacts.SipgateContactSyncAdapter.java

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {/*from  w ww  .j av a2  s  . c  o m*/
    Log.d("SipgateContactSyncAdapt", "onPerformSync()");
    AccountManager accountManager = AccountManager.get(getContext());
    final String oauth = accountManager.peekAuthToken(account, "oauth");
    Log.d("SipgateContactSyncAdapt", oauth == null ? "NO oauth!" : "Got token");
    final JSONArray users = SipgateApi.getContacts(oauth);
    assert users != null;

    Log.d("SipgateContactSyncAdapt", String.format("fetched %d contacts", users.length()));

    Uri rawContactUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon()
            .appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, account.name)
            .appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type).build();
    Cursor c1 = mContentResolver.query(rawContactUri, null, null, null, null);
    Map<String, Boolean> localContacts = new HashMap<>();
    while (c1 != null && c1.moveToNext()) {
        localContacts.put(c1.getString(c1.getColumnIndex(ContactsContract.RawContacts.SOURCE_ID)), false);
    }

    for (int i = 0; i < users.length(); i++) {
        try {
            final JSONObject user = users.getJSONObject(i);
            final String id = user.getString("uuid");
            if (localContacts.containsKey(id)) {
                localContacts.put(id, true);
                continue;
            }
            final String firstname = user.getString("firstname");

            final JSONArray emails = user.getJSONArray("emails");
            final JSONArray mobile = user.getJSONArray("mobile");
            final JSONArray landline = user.getJSONArray("landline");
            final JSONArray fax = user.getJSONArray("fax");

            Log.d("SipgateContactSyncAdapt", String.format("adding id: %s %s", id, firstname));

            ContactManager.addContact(id, firstname, user.getString("lastname"), jsonArrayToList(emails),
                    jsonArrayToList(mobile), jsonArrayToList(landline), jsonArrayToList(fax), mContentResolver,
                    account.name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    for (Map.Entry<String, Boolean> contact : localContacts.entrySet()) {
        if (!contact.getValue()) {
            ContactManager.deleteContact(contact.getKey(), mContentResolver, account.name);
        }
    }
}

From source file:com.pindroid.activity.FragmentBaseActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    app = (PindroidApplication) getApplicationContext();

    mAccountManager = AccountManager.get(this);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setHomeButtonEnabled(true);
    }//from   w  ww  .jav a  2s  .co m

    Intent intent = getIntent();

    if (Intent.ACTION_SEARCH.equals(intent.getAction()) && !intent.hasExtra("MainSearchResults")) {
        if (intent.hasExtra("username"))
            app.setUsername(intent.getStringExtra("username"));

        if (intent.hasExtra(SearchManager.QUERY)) {
            //Intent i = new Intent(this, MainSearchResults.class);
            //i.putExtras(intent.getExtras());
            //startActivity(i);
            //finish();
        } else {
            onSearchRequested();
        }
    }

    //init();
}

From source file:edu.mit.mobile.android.locast.accounts.Authenticator.java

public static Account[] getAccounts(Context context) {
    return AccountManager.get(context).getAccountsByType(AuthenticationService.ACCOUNT_TYPE);
}