use of android.accounts.AccountManager in project android-bootstrap by AndroidBootstrap.
the class TestUserAccountUtil method ensureValidAccountAvailable.
/**
* Checks the device has a valid Bootstrap (on parse.com via the example API),
* account, if not, adds one using the test credentials found in system
* property 'bootstrap.test.api.key'.
*
* The credentials can be passed on the command line like this: mvn
* -bootstrap.test.api.key=0123456789abcdef0123456789abcdef install
*
* @param instrumentation
* taken from the test context
* @return true if valid account credentials are available
*/
public static boolean ensureValidAccountAvailable(Instrumentation instrumentation) {
Context c = instrumentation.getContext();
AccountManager accountManager = AccountManager.get(instrumentation.getTargetContext());
for (Account account : accountManager.getAccountsByType(BOOTSTRAP_ACCOUNT_TYPE)) {
if (accountManager.peekAuthToken(account, AUTHTOKEN_TYPE) != null) {
Ln.i("Using existing account : " + account.name);
// we have a valid account that has successfully authenticated
return true;
}
}
String testApiKey = c.getString(R.string.test_account_api_key);
String truncatedApiKey = testApiKey.substring(0, 4) + "…";
if (!testApiKey.matches("\\p{XDigit}{32}")) {
Ln.w("No valid test account credentials in bootstrap.test.api.key : " + truncatedApiKey);
return false;
}
Ln.i("Adding test account using supplied api key credential : " + truncatedApiKey);
Account account = new Account("test@example.com", BOOTSTRAP_ACCOUNT_TYPE);
// this test account will not have a valid password
accountManager.addAccountExplicitly(account, null, null);
accountManager.setAuthToken(account, AUTHTOKEN_TYPE, testApiKey);
return true;
}
use of android.accounts.AccountManager in project android_frameworks_base by AOSPA.
the class SuggestionParser method satisfiesRequiredAccount.
public boolean satisfiesRequiredAccount(Tile suggestion) {
String requiredAccountType = suggestion.metaData.getString(META_DATA_REQUIRE_ACCOUNT);
if (requiredAccountType == null) {
return true;
}
AccountManager accountManager = AccountManager.get(mContext);
Account[] accounts = accountManager.getAccountsByType(requiredAccountType);
return accounts.length > 0;
}
use of android.accounts.AccountManager in project platform_frameworks_base by android.
the class SuggestionParser method satisfiesRequiredAccount.
public boolean satisfiesRequiredAccount(Tile suggestion) {
String requiredAccountType = suggestion.metaData.getString(META_DATA_REQUIRE_ACCOUNT);
if (requiredAccountType == null) {
return true;
}
AccountManager accountManager = AccountManager.get(mContext);
Account[] accounts = accountManager.getAccountsByType(requiredAccountType);
return accounts.length > 0;
}
use of android.accounts.AccountManager in project platform_frameworks_base by android.
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 platform_frameworks_base by android.
the class DevicePolicyManagerService method hasIncompatibleAccountsLocked.
/**
* Return true if a given user has any accounts that'll prevent installing a device or profile
* owner {@code owner}.
* - If the user has no accounts, then return false.
* - Otherwise, if the owner is unknown (== null), or is not test-only, then return true.
* - Otherwise, if there's any account that does not have ..._ALLOWED, or does have
* ..._DISALLOWED, return true.
* - Otherwise return false.
*/
private boolean hasIncompatibleAccountsLocked(int userId, @Nullable ComponentName owner) {
final long token = mInjector.binderClearCallingIdentity();
try {
final AccountManager am = AccountManager.get(mContext);
final Account[] accounts = am.getAccountsAsUser(userId);
if (accounts.length == 0) {
return false;
}
final String[] feature_allow = { DevicePolicyManager.ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED };
final String[] feature_disallow = { DevicePolicyManager.ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_DISALLOWED };
// Even if we find incompatible accounts along the way, we still check all accounts
// for logging.
boolean compatible = true;
for (Account account : accounts) {
if (hasAccountFeatures(am, account, feature_disallow)) {
Log.e(LOG_TAG, account + " has " + feature_disallow[0]);
compatible = false;
}
if (!hasAccountFeatures(am, account, feature_allow)) {
Log.e(LOG_TAG, account + " doesn't have " + feature_allow[0]);
compatible = false;
}
}
if (compatible) {
Log.w(LOG_TAG, "All accounts are compatible");
} else {
Log.e(LOG_TAG, "Found incompatible accounts");
}
// Then check if the owner is test-only.
String log;
if (owner == null) {
// Owner is unknown. Suppose it's not test-only
compatible = false;
log = "Only test-only device/profile owner can be installed with accounts";
} else if (isAdminTestOnlyLocked(owner, userId)) {
if (compatible) {
log = "Installing test-only owner " + owner;
} else {
log = "Can't install test-only owner " + owner + " with incompatible accounts";
}
} else {
compatible = false;
log = "Can't install non test-only owner " + owner + " with accounts";
}
if (compatible) {
Log.w(LOG_TAG, log);
} else {
Log.e(LOG_TAG, log);
}
return !compatible;
} finally {
mInjector.binderRestoreCallingIdentity(token);
}
}
Aggregations