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:edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticator.java

/**
 * @param context/*w w w  . ja v a2 s  .c  om*/
 * @param accountType
 * @return
 * @deprecated use {@link #getUserUri(Context, Account)} instead.
 */
@Deprecated
public static String getUserUri(Context context, String accountType) {
    final Account[] accounts = AbsLocastAuthenticator.getAccounts(context, accountType);
    if (accounts.length > 0) {
        return AccountManager.get(context).getUserData(accounts[0],
                AbsLocastAuthenticationService.USERDATA_USER_URI);
    }
    return null;
}

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

public static String getUserUri(Context context) {
    final Account[] accounts = Authenticator.getAccounts(context);
    if (accounts.length > 0) {
        return AccountManager.get(context).getUserData(accounts[0], AuthenticationService.USERDATA_USER_URI);
    }/*from  ww  w.  j ava  2s  .  c o  m*/
    return null;
}

From source file:dev.drsoran.moloko.sync.SyncAdapter.java

private Pair<String, String> getCredentials(Account account) {
    final AccountManager accountManager = AccountManager.get(context);

    final String apiKey = accountManager.getUserData(account, Constants.FEAT_API_KEY);
    final String sharedSecret = accountManager.getUserData(account, Constants.FEAT_SHARED_SECRET);

    return Pair.create(apiKey, sharedSecret);
}

From source file:org.pixmob.appengine.client.demo.DemoActivity.java

private void reset() {
    final AccountManager accountManager = AccountManager.get(this);
    final Account[] accounts = accountManager.getAccountsByType("com.google");
    if (accounts.length == 0) {
        accountAdapter = new AccountAdapter(this, new Account[0]);
        setListAdapter(accountAdapter);/*w  w  w . j av  a2 s .  com*/
        showDialog(NO_ACCOUNT_DIALOG);
    } else {
        Arrays.sort(accounts, AccountComparator.INSTANCE);
        accountAdapter = new AccountAdapter(this, accounts);
        setListAdapter(accountAdapter);
    }

    appspotBaseView.setText(appspotBase);
}

From source file:com.example.am.myapplication.ContactAdder.java

/**
 * Called when the activity is first created. Responsible for initializing the UI.
 *//*  w  w  w . ja va  2s  .com*/
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.v(TAG, "Activity State: onCreate()");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_adder);

    // Obtain handles to UI objects
    mAccountSpinner = (Spinner) findViewById(R.id.accountSpinner);
    mContactNameEditText = (EditText) findViewById(R.id.contactNameEditText);
    mContactPhoneEditText = (EditText) findViewById(R.id.contactPhoneEditText);
    mContactEmailEditText = (EditText) findViewById(R.id.contactEmailEditText);
    mContactPhoneTypeSpinner = (Spinner) findViewById(R.id.contactPhoneTypeSpinner);
    mContactEmailTypeSpinner = (Spinner) findViewById(R.id.contactEmailTypeSpinner);
    mContactSaveButton = (Button) findViewById(R.id.contactSaveButton);

    // Prepare list of supported account types
    // Note: Other types are available in ContactsContract.CommonDataKinds
    //       Also, be aware that type IDs differ between Phone and Email, and MUST be computed
    //       separately.
    mContactPhoneTypes = new ArrayList<Integer>();
    mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_HOME);
    mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
    mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
    mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_OTHER);
    mContactEmailTypes = new ArrayList<Integer>();
    mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_HOME);
    mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_WORK);
    mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_MOBILE);
    mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_OTHER);

    // Prepare model for account spinner
    mAccounts = new ArrayList<AccountData>();
    mAccountAdapter = new AccountAdapter(this, mAccounts);
    mAccountSpinner.setAdapter(mAccountAdapter);

    // Populate list of account types for phone
    ArrayAdapter<String> adapter;
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Iterator<Integer> iter;
    iter = mContactPhoneTypes.iterator();
    while (iter.hasNext()) {
        adapter.add(ContactsContract.CommonDataKinds.Phone
                .getTypeLabel(this.getResources(), iter.next(), getString(R.string.undefinedTypeLabel))
                .toString());
    }
    mContactPhoneTypeSpinner.setAdapter(adapter);
    mContactPhoneTypeSpinner.setPrompt(getString(R.string.selectLabel));

    // Populate list of account types for email
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    iter = mContactEmailTypes.iterator();
    while (iter.hasNext()) {
        adapter.add(ContactsContract.CommonDataKinds.Email
                .getTypeLabel(this.getResources(), iter.next(), getString(R.string.undefinedTypeLabel))
                .toString());
    }
    mContactEmailTypeSpinner.setAdapter(adapter);
    mContactEmailTypeSpinner.setPrompt(getString(R.string.selectLabel));

    // Prepare the system account manager. On registering the listener below, we also ask for
    // an initial callback to pre-populate the account list.
    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);

    // Register handlers for UI elements
    mAccountSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long i) {
            updateAccountSelection();
        }

        public void onNothingSelected(AdapterView<?> parent) {
            // We don't need to worry about nothing being selected, since Spinners don't allow
            // this.
        }
    });
    mContactSaveButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            onSaveButtonClicked();
        }
    });
}

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

public static String getUserUri(Context context, Account account) {
    return AccountManager.get(context).getUserData(account, AbsLocastAuthenticationService.USERDATA_USER_URI);
}

From source file:com.bcp.bcp.geofencing.GeofenceTransitionsIntentService.java

/**
 * Gets transition details and returns them as a formatted string.
 *
 * @param context               The app context.
 * @param geofenceTransition    The ID of the geofence transition.
 * @param triggeringGeofences   The geofence(s) triggered.
 * @return The transition details formatted as String.
 *//*from  ww  w.j a  v a2 s .  co  m*/
private String getGeofenceTransitionDetails(Context context, int geofenceTransition,
        List<Geofence> triggeringGeofences) {

    String geofenceTransitionString = getTransitionString(geofenceTransition);

    // Get the Ids of each geofence that was triggered.
    ArrayList triggeringGeofencesIdsList = new ArrayList();
    for (Geofence geofence : triggeringGeofences) {
        triggeringGeofencesIdsList.add(geofence.getRequestId());
    }
    String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);
    Date curDate = new Date();
    SimpleDateFormat format = new SimpleDateFormat(Constants.TIME_FORMAT);
    gEntryDate = format.format(curDate);
    boolean isInserted;

    gaddress = triggeringGeofencesIdsList.toString();
    gstatus = geofenceTransitionString;
    String geoFenceDetailString = geofenceTransitionString + ": " + triggeringGeofencesIdsString;

    Pattern gmailPattern = Patterns.EMAIL_ADDRESS;
    Account[] accounts = AccountManager.get(this).getAccounts();

    for (Account account : accounts) {
        if (gmailPattern.matcher(account.name).matches()) {
            gemail = account.name;
        }
    }

    //insert into new fence breach fusion table for each entry exit //no conditions

    //When switch is ON  and we are breaching a fence (entering) we will write that data to FT When switch is ON and we are breaching fence(exiting) we will write that data to FT as well.

    //status:exit
    if (geofenceTransitionString.equalsIgnoreCase("Exited")) {
        if (mPref.getBoolean("SWITCH", false)) {
            //Switch : ON
            //if switch is ON
            String timeValue = mPref.getString("Time_Interval", "60000");
            long configurableTime = Long.parseLong(timeValue);
            mEditor.putLong("CONFIG TIME", configurableTime);
            mEditor.commit();
            Intent intent = new Intent(this, MyLocationService.class);
            startService(intent);
            if (!mPref.getBoolean(geoFenceDetailString, false)) {
                credentials.insertIntoGeoFusionTables(
                        this.saveGeoFile(gaddress, gstatus, gEntryDate, gemail, "geofile"));
            }

        } else {
        }

    } else if (geofenceTransitionString.equalsIgnoreCase("Entered")) {//status :Entry

        if (mPref.getBoolean("SWITCH", false)) {

            if (!mPref.getBoolean(geoFenceDetailString, false)) {
                credentials.insertIntoGeoFusionTables(
                        this.saveGeoFile(gaddress, gstatus, gEntryDate, gemail, "geofile"));
            }

        } else {//Switch : OFF

        }
    }

    if (!mPref.getBoolean(geoFenceDetailString, false)) {
        isInserted = databaseHandler.addFenceTiming(
                new FenceTiming(triggeringGeofencesIdsList.toString(), geofenceTransitionString, gEntryDate));
        if (isInserted) {
            Log.e("GeofenceonsIS : ", "inserted to db");
        }
    }

    return geoFenceDetailString;
}

From source file:com.lambdasoup.watchlater.test.AddActivityTest.java

private void addAccount(Account account) {
    AccountManager accountManager = AccountManager
            .get(InstrumentationRegistry.getInstrumentation().getContext());
    //noinspection ResourceType
    accountManager.addAccountExplicitly(account, null, null);
}

From source file:com.cerema.cloud2.files.services.FileUploader.java

/**
 * Service initialization/*from  ww  w.  j  a  v  a  2 s .co m*/
 */
@Override
public void onCreate() {
    super.onCreate();
    Log_OC.d(TAG, "Creating service");
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    HandlerThread thread = new HandlerThread("FileUploaderThread", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, this);
    mBinder = new FileUploaderBinder();

    // add AccountsUpdatedListener
    AccountManager am = AccountManager.get(getApplicationContext());
    am.addOnAccountsUpdatedListener(this, null, false);
}

From source file:com.nextgis.maplibui.SelectNGWResourceDialog.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ADDACCOUNT_CODE) {
        if (resultCode != Activity.RESULT_CANCELED) {
            //search new account and add it
            final AccountManager accountManager = AccountManager.get(getActivity());
            Connections connections = mListAdapter.getConnections();
            for (Account account : accountManager.getAccountsByType(NGW_ACCOUNT_TYPE)) {
                boolean find = false;
                for (int i = 0; i < connections.getChildrenCount(); i++) {
                    Connection connection = (Connection) connections.getChild(i);
                    if (null != connection && connection.getName().equals(account.name)) {
                        find = true;//from  w  ww .  j a va2 s .c o  m
                        break;
                    }
                }

                if (!find) {
                    String url = accountManager.getUserData(account, "url");
                    String password = accountManager.getPassword(account);
                    String login = accountManager.getUserData(account, "login");
                    connections.add(new Connection(account.name, login, password, url));
                    mListAdapter.notifyDataSetChanged();
                    break;
                }
            }
        }
    } else
        super.onActivityResult(requestCode, resultCode, data);
}