Example usage for android.accounts AccountManager getAccountsByType

List of usage examples for android.accounts AccountManager getAccountsByType

Introduction

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

Prototype

@NonNull
public Account[] getAccountsByType(String type) 

Source Link

Document

Lists all accounts of particular type visible to the caller.

Usage

From source file:com.piusvelte.cloudset.android.AccountsFragment.java

private void getAccountNames() {
    adapter.clear();/*from w w  w .  j a v  a 2  s.  c  om*/
    AccountManager accountManager = AccountManager.get(getActivity().getApplicationContext());
    Account[] accounts = accountManager.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);

    String[] names = new String[accounts.length];
    for (int i = 0; i < names.length; i++) {
        adapter.add(accounts[i].name);
    }

    adapter.notifyDataSetChanged();
}

From source file:com.gigathinking.simpleapplock.RegisterDevice.java

private boolean registerDevice() {
    String username;/*from  w ww  . j a  va  2 s  .c  o  m*/
    String device = Build.MODEL;
    String version = Build.VERSION.RELEASE;
    if (prefs.getString(mContext.getString(R.string.user_name), null) == null) {
        AccountManager manager = AccountManager.get(mContext);
        Account[] accounts = manager != null ? manager.getAccountsByType("com.google") : new Account[0];
        if (accounts.length == 0) {
            return false;
        }
        username = accounts[0].name.split("@")[0];
        prefs.edit().putString(mContext.getString(R.string.user_name), username).commit();
    } else {
        username = prefs.getString(mContext.getString(R.string.user_name), "");
    }

    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 5000);
    HttpConnectionParams.setSoTimeout(params, 10000);
    HttpClient client = new DefaultHttpClient(params);
    try {
        String post;
        if (prefs.getString(RegisterDevice.PROPERTY_REG_ID_OLD, null) == null) {
            post = ServerInfo.RESET_SERVER + "/register_device.php?user=" + username + "&deviceid=" + regID
                    + "&devicename=" + URLEncoder.encode(device, "utf-8") + "&version=" + version;
        } else {
            post = ServerInfo.RESET_SERVER + "/update_device_id.php?dev_id="
                    + prefs.getString(PROPERTY_REG_ID_OLD, "") + "&dev_id_new="
                    + prefs.getString(PROPERTY_REG_ID, "");
        }
        HttpPost httpPost = new HttpPost(post);
        HttpEntity localHttpEntity = client.execute(httpPost).getEntity();
        if (localHttpEntity != null) {
            int res = Integer
                    .valueOf(new BufferedReader(new InputStreamReader(localHttpEntity.getContent(), "UTF-8"))
                            .readLine());
            prefs.edit().putBoolean(mContext.getString(R.string.register_complete), true).commit();
            return res != -1;
        }
    } catch (HttpHostConnectException e) {
        return false;
    } catch (IOException localIOException) {
        return false;
    }
    return true;
}

From source file:com.aniruddhc.acemusic.player.WelcomeActivity.GooglePlayMusicFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mContext = getActivity().getApplicationContext();
    mApp = (Common) mContext;/*from  w  w w.j a v  a  2 s.  co m*/
    View rootView = (View) inflater.inflate(R.layout.fragment_welcome_screen_5, null);

    welcomeHeader = (TextView) rootView.findViewById(R.id.welcome_header);
    welcomeHeader.setTypeface(TypefaceHelper.getTypeface(getActivity(), "Roboto-Light"));

    welcomeText1 = (TextView) rootView.findViewById(R.id.welcome_text_1);
    welcomeText1.setTypeface(TypefaceHelper.getTypeface(getActivity(), "Roboto-Regular"));

    googlePlayMusicDisclaimer = (TextView) rootView.findViewById(R.id.google_play_music_disclaimer);
    googlePlayMusicDisclaimer.setTypeface(TypefaceHelper.getTypeface(getActivity(), "Roboto-Regular"));

    radioGroup = (RadioGroup) rootView.findViewById(R.id.google_play_music_radio_group);

    final AccountManager accountManager = AccountManager.get(getActivity().getApplicationContext());
    final Account[] accounts = accountManager.getAccountsByType("com.google");
    final int size = accounts.length + 1; //We're adding 1 here to account (no pun intended) for the extra "Don't use Google Play Music" option.

    final RadioButton[] radioButton = new RadioButton[size];

    //Add a new radio button the group for each username.
    for (int i = 0; i < size; i++) {
        radioButton[i] = new RadioButton(getActivity());
        radioGroup.addView(radioButton[i]);

        //The first radio button will always be "Don't use Google Play Music".
        if (i == 0) {
            radioButton[i].setChecked(true);
            radioButton[i].setText(R.string.dont_use_google_play_music);
        } else {
            radioButton[i].setText(accounts[i - 1].name);
        }

        radioButton[i].setTag(i);
        radioButton[i].setTypeface(TypefaceHelper.getTypeface(mContext, "Roboto-Regular"));

    }

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            int radioButtonID = group.getCheckedRadioButtonId();
            View radioButton = group.findViewById(radioButtonID);
            int index = group.indexOfChild(radioButton);

            if (index != 0) {

                account = accounts[index - 1];
                mApp.getSharedPreferences().edit().putString("GOOGLE_PLAY_MUSIC_ACCOUNT", account.name)
                        .commit();

                AsyncGoogleMusicAuthenticationTask task = new AsyncGoogleMusicAuthenticationTask(mContext,
                        getActivity(), true, account.name);

                task.execute();
            } else {
                mApp.getSharedPreferences().edit().putString("GOOGLE_PLAY_MUSIC_ACCOUNT", "").commit();
                mApp.getSharedPreferences().edit().putBoolean("GOOGLE_PLAY_MUSIC_ENABLED", false).commit();
            }

        }

    });

    return rootView;
}

From source file:com.arthackday.killerapp.util.Util.java

public String getGoogleAuth(String type) {
    AccountManager mgr = AccountManager.get(activity);
    Account[] accts = mgr.getAccountsByType("com.google");

    if (accts.length == 0) {
        return null;
    }/*from  ww  w.j a v a2s. co m*/

    try {
        Account acct = accts[0];
        Log.d(LOG_TAG, "acct name=" + acct.name);
        AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(acct, type, null, activity, null,
                null);

        Bundle authTokenBundle = accountManagerFuture.getResult();

        if (authTokenBundle.containsKey(AccountManager.KEY_INTENT)) {
            Intent authRequestIntent = (Intent) authTokenBundle.get(AccountManager.KEY_INTENT);
            activity.startActivity(authRequestIntent);
        }

        return authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        e.printStackTrace();
    } catch (AuthenticatorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.sociotech.fishification.ui.fragments.CreateFishFragment.java

@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {

    String userName = null;//from  w  ww  .j  a  va2  s.c o m
    cursor.moveToFirst();
    if (!cursor.isAfterLast()) {

        if (cursorLoader.getId() == 0) {

            userName = cursor.getString(ProfileQuery.DISPLAY_NAME);

        } else {
            userName = cursor.getString(ProfileQuery.ADDRESS);
        }
    }

    if (userName != null && !userName.isEmpty()) {

        // Set UserName
        setEditText(R.id.authorNameText, userName);

    } else if (cursorLoader.getId() == 0) {

        // Select primary e-mail address
        getActivity().getSupportLoaderManager().initLoader(1, null, this);

    } else {

        // Parse accounts for user name or e-mail address
        AccountManager am = AccountManager.get(getActivity());
        Account[] accounts = am.getAccountsByType("XING");
        if (accounts != null && accounts.length > 0) {
            setEditText(R.id.authorNameText, accounts[0].name);
        } else {
            accounts = am.getAccounts();
            if (accounts != null && accounts.length > 0) {
                setEditText(R.id.authorNameText, accounts[0].name);
            }
        }
    }
}

From source file:org.tomahawk.tomahawk_android.dialogs.GMusicConfigDialog.java

/**
 * Called when this {@link android.support.v4.app.DialogFragment} is being created
 *///from  w w  w . ja  v  a 2s .  co m
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mScriptResolver = (ScriptResolver) PipeLine.get().getResolver(TomahawkApp.PLUGINNAME_GMUSIC);

    TextView headerTextView = (TextView) addScrollingViewToFrame(R.layout.config_textview);
    headerTextView.setText(mScriptResolver.getDescription());

    TextView infoTextView = (TextView) addScrollingViewToFrame(R.layout.config_textview);
    infoTextView.setText(R.string.gmusic_info_text);

    String loggedInAccountName = null;
    Map<String, Object> config = mScriptResolver.getConfig();
    if (config.get("email") instanceof String) {
        loggedInAccountName = (String) config.get("email");
    }

    mRadioGroup = (RadioGroup) addScrollingViewToFrame(R.layout.config_radiogroup);
    final AccountManager accountManager = AccountManager.get(TomahawkApp.getContext());
    final Account[] accounts = accountManager.getAccountsByType("com.google");
    mAccountMap = new HashMap<>();
    LayoutInflater inflater = getActivity().getLayoutInflater();
    for (Account account : accounts) {
        RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.config_radiobutton, mRadioGroup,
                false);
        radioButton.setText(account.name);
        mRadioGroup.addView(radioButton);
        mAccountMap.put(radioButton.getId(), account);
        if (loggedInAccountName != null && account.name.equals(loggedInAccountName)) {
            mRadioGroup.check(radioButton.getId());
        }
    }

    setDialogTitle(mScriptResolver.getName());
    setStatus(mScriptResolver);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(getDialogView());
    return builder.create();
}

From source file:app.hacked.ChatFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    queue = Volley.newRequestQueue(getActivity());
    AccountManager am = AccountManager.get(getActivity());
    accounts = am.getAccountsByType("com.google");
    //chatMessages = new ArrayList<ChatMessage>();
}

From source file:com.flowzr.budget.holo.export.flowzr.FlowzrBillTask.java

protected Object work(Context context, DatabaseAdapter dba, String... params) throws ImportExportException {

    AccountManager accountManager = AccountManager.get(context);
    android.accounts.Account[] accounts = accountManager.getAccountsByType("com.google");

    String accountName = MyPreferences.getFlowzrAccount(context);
    if (accountName == null) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, FlowzrSyncActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        Builder mNotifyBuilder = new NotificationCompat.Builder(context);
        mNotifyBuilder.setContentIntent(contentIntent).setSmallIcon(R.drawable.icon)
                .setWhen(System.currentTimeMillis()).setAutoCancel(true)
                .setContentTitle(context.getString(R.string.flowzr_sync))
                .setContentText(context.getString(R.string.flowzr_choose_account));
        nm.notify(0, mNotifyBuilder.build());
        Log.i("Flowzr", "account name is null");
        throw new ImportExportException(R.string.flowzr_choose_account);
    }/*from  w w  w  . j a  va 2s  .  c o m*/
    Account useCredential = null;
    for (int i = 0; i < accounts.length; i++) {
        if (accountName.equals(((android.accounts.Account) accounts[i]).name)) {
            useCredential = accounts[i];
        }
    }
    AccountManager.get(context).getAuthToken(useCredential, "ah", null, (Activity) context,
            new GetAuthTokenCallback(), null);

    return null;
}

From source file:org.pixmob.feedme.ui.SelectAccountDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Activity activity = getActivity();
    final AccountManager accountManager = (AccountManager) activity.getSystemService(Context.ACCOUNT_SERVICE);

    // Get Google accounts which are registered on this device.
    final Account[] accounts = accountManager.getAccountsByType(GOOGLE_ACCOUNT);
    if (accounts.length == 0) {
        // Found no Google account: an error dialog is displayed.
        return new AlertDialog.Builder(activity).setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(R.string.error).setMessage(R.string.no_google_account_found)
                .setPositiveButton(android.R.string.ok, null).create();
    }/*from  w ww.ja  v  a 2 s .  c o  m*/

    // Sort accounts by name.
    final String[] accountNames = new String[accounts.length];
    for (int i = 0; i < accountNames.length; ++i) {
        accountNames[i] = accounts[i].name;
    }
    Arrays.sort(accountNames);

    // The current account is selected by default.
    int checkedIndex = -1;
    if (previousAccount != null) {
        for (int i = 0; checkedIndex == -1 && i < accountNames.length; ++i) {
            if (accountNames[i].equals(previousAccount)) {
                checkedIndex = i;
            }
        }
    }

    return new AlertDialog.Builder(activity).setTitle(R.string.select_google_account)
            .setSingleChoiceItems(accountNames, checkedIndex, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    final OnAccountSelectedListener l = listener != null ? listener.get() : null;
                    if (l != null) {
                        l.onAccountSelected(accountNames[which]);
                    }
                    dismiss();
                }
            }).create();
}

From source file:com.jdom.get.stuff.done.android.AndroidApplicationContextFactory.java

public Set<String> getAvailableSyncAccounts() {
    AccountManager accountManager = AccountManager.get(activity);
    Account[] accounts = accountManager.getAccountsByType("com.google");

    Set<String> accountSet = new TreeSet<String>();
    for (Account account : accounts) {
        accountSet.add(account.name);//from   w  ww .  j  a  v  a2 s  . c o m
    }
    return accountSet;
}