use of android.accounts.AccountManager in project android_frameworks_base by crdroidandroid.
the class BugreportProgressService method findSendToAccount.
/**
* Find the best matching {@link Account} based on build properties.
*/
private static Account findSendToAccount(Context context) {
final AccountManager am = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
String preferredDomain = SystemProperties.get("sendbug.preferred.domain");
if (!preferredDomain.startsWith("@")) {
preferredDomain = "@" + preferredDomain;
}
final Account[] accounts;
try {
accounts = am.getAccounts();
} catch (RuntimeException e) {
Log.e(TAG, "Could not get accounts for preferred domain " + preferredDomain, e);
return null;
}
if (DEBUG)
Log.d(TAG, "Number of accounts: " + accounts.length);
Account foundAccount = null;
for (Account account : accounts) {
if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) {
if (!preferredDomain.isEmpty()) {
// looking
if (account.name.endsWith(preferredDomain)) {
return account;
} else {
foundAccount = account;
}
// if we don't have a preferred domain, just return since it looks like
// an email address
} else {
return account;
}
}
}
return foundAccount;
}
use of android.accounts.AccountManager in project android_frameworks_base by DirtyUnicorns.
the class BugreportProgressService method findSendToAccount.
/**
* Find the best matching {@link Account} based on build properties.
*/
private static Account findSendToAccount(Context context) {
final AccountManager am = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
String preferredDomain = SystemProperties.get("sendbug.preferred.domain");
if (!preferredDomain.startsWith("@")) {
preferredDomain = "@" + preferredDomain;
}
final Account[] accounts;
try {
accounts = am.getAccounts();
} catch (RuntimeException e) {
Log.e(TAG, "Could not get accounts for preferred domain " + preferredDomain, e);
return null;
}
if (DEBUG)
Log.d(TAG, "Number of accounts: " + accounts.length);
Account foundAccount = null;
for (Account account : accounts) {
if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) {
if (!preferredDomain.isEmpty()) {
// looking
if (account.name.endsWith(preferredDomain)) {
return account;
} else {
foundAccount = account;
}
// if we don't have a preferred domain, just return since it looks like
// an email address
} else {
return account;
}
}
}
return foundAccount;
}
use of android.accounts.AccountManager in project PocketHub by pockethub.
the class AccountUtils method updateAccount.
/**
* Update account
*
* @param account
* @param activity
* @return true if account was updated, false otherwise
*/
public static boolean updateAccount(final Account account, final Activity activity) {
int count = UPDATE_COUNT.get();
synchronized (UPDATE_COUNT) {
// while the lock was being waited for
if (count != UPDATE_COUNT.get()) {
return true;
}
AccountManager manager = AccountManager.get(activity);
try {
if (!hasAuthenticator(manager)) {
throw new AuthenticatorConflictException();
}
manager.updateCredentials(account, ACCOUNT_TYPE, null, activity, null, null).getResult();
UPDATE_COUNT.incrementAndGet();
return true;
} catch (OperationCanceledException e) {
Log.d(TAG, "Excepting retrieving account", e);
activity.finish();
return false;
} catch (AccountsException e) {
Log.d(TAG, "Excepting retrieving account", e);
return false;
} catch (AuthenticatorConflictException e) {
activity.runOnUiThread(() -> showConflictMessage(activity));
return false;
} catch (IOException e) {
Log.d(TAG, "Excepting retrieving account", e);
return false;
}
}
}
use of android.accounts.AccountManager in project android_packages_apps_Settings by LineageOS.
the class AccountSyncSettings method requestAccountAccessIfNeeded.
private boolean requestAccountAccessIfNeeded(String packageName) {
if (packageName == null) {
return false;
}
final int uid;
try {
uid = getContext().getPackageManager().getPackageUidAsUser(packageName, mUserHandle.getIdentifier());
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Invalid sync ", e);
return false;
}
AccountManager accountManager = getContext().getSystemService(AccountManager.class);
if (!accountManager.hasAccountAccess(mAccount, packageName, mUserHandle)) {
IntentSender intent = accountManager.createRequestAccountAccessIntentSenderAsUser(mAccount, packageName, mUserHandle);
if (intent != null) {
try {
startIntentSenderForResult(intent, uid, null, 0, 0, 0, null);
return true;
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Error requesting account access", e);
}
}
}
return false;
}
use of android.accounts.AccountManager in project android_packages_apps_Settings by LineageOS.
the class MasterClear method loadAccountList.
private void loadAccountList(final UserManager um) {
View accountsLabel = mContentView.findViewById(R.id.accounts_label);
LinearLayout contents = (LinearLayout) mContentView.findViewById(R.id.accounts);
contents.removeAllViews();
Context context = getActivity();
final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
final int profilesSize = profiles.size();
AccountManager mgr = AccountManager.get(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int accountsCount = 0;
for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
final UserInfo userInfo = profiles.get(profileIndex);
final int profileId = userInfo.id;
final UserHandle userHandle = new UserHandle(profileId);
Account[] accounts = mgr.getAccountsAsUser(profileId);
final int N = accounts.length;
if (N == 0) {
continue;
}
accountsCount += N;
AuthenticatorDescription[] descs = AccountManager.get(context).getAuthenticatorTypesAsUser(profileId);
final int M = descs.length;
if (profilesSize > 1) {
View titleView = Utils.inflateCategoryHeader(inflater, contents);
final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
titleText.setText(userInfo.isManagedProfile() ? R.string.category_work : R.string.category_personal);
contents.addView(titleView);
}
for (int i = 0; i < N; i++) {
Account account = accounts[i];
AuthenticatorDescription desc = null;
for (int j = 0; j < M; j++) {
if (account.type.equals(descs[j].type)) {
desc = descs[j];
break;
}
}
if (desc == null) {
Log.w(TAG, "No descriptor for account name=" + account.name + " type=" + account.type);
continue;
}
Drawable icon = null;
try {
if (desc.iconId != 0) {
Context authContext = context.createPackageContextAsUser(desc.packageName, 0, userHandle);
icon = context.getPackageManager().getUserBadgedIcon(authContext.getDrawable(desc.iconId), userHandle);
}
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Bad package name for account type " + desc.type);
} catch (Resources.NotFoundException e) {
Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
}
if (icon == null) {
icon = context.getPackageManager().getDefaultActivityIcon();
}
View child = inflater.inflate(R.layout.master_clear_account, contents, false);
((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
((TextView) child.findViewById(android.R.id.title)).setText(account.name);
contents.addView(child);
}
}
if (accountsCount > 0) {
accountsLabel.setVisibility(View.VISIBLE);
contents.setVisibility(View.VISIBLE);
}
// Checking for all other users and their profiles if any.
View otherUsers = mContentView.findViewById(R.id.other_users_present);
final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
}
Aggregations