aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTingting <tingtingw@google.com>2021-11-17 23:19:31 +0000
committerTingting <tingtingw@google.com>2021-11-18 21:57:01 +0000
commit7c94fba31988343706f5f3abf0c0231aa1a8ef21 (patch)
tree54f1060084b027cf7af867d1b0bf17d36ddae6bf /src
parentd588700689e0a79a5bd9a06da47872e2858568ad (diff)
downloadContactsProvider-7c94fba31988343706f5f3abf0c0231aa1a8ef21.tar.gz
Add account check for setDefaultAccount API.
Test: atest ContactsProviderTests. Bug: 188347310 Change-Id: I9ded7fcd4956b48877ed6ce4293b3791e6b551f0
Diffstat (limited to 'src')
-rw-r--r--src/com/android/providers/contacts/ContactsProvider2.java63
1 files changed, 39 insertions, 24 deletions
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index 1168d526..e135208c 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -2400,33 +2400,48 @@ public class ContactsProvider2 extends AbstractContactsProvider
return response;
} else if (Settings.SET_DEFAULT_ACCOUNT_METHOD.equals(method)) {
- ContactsPermissions.enforceCallingOrSelfPermission(getContext(),
- SET_DEFAULT_ACCOUNT_PERMISSION);
- final String accountName = extras.getString(Settings.ACCOUNT_NAME);
- final String accountType = extras.getString(Settings.ACCOUNT_TYPE);
- final String dataSet = extras.getString(Settings.DATA_SET);
+ return setDefaultAccountSetting(extras);
+ }
+ return null;
+ }
- if (TextUtils.isEmpty(accountName) ^ TextUtils.isEmpty(accountType)) {
- throw new IllegalArgumentException(
- "Must specify both or neither of ACCOUNT_NAME and ACCOUNT_TYPE");
- }
- if (!TextUtils.isEmpty(dataSet)) {
- throw new IllegalArgumentException(
- "Cannot set default account with non-null data set.");
- }
+ private Bundle setDefaultAccountSetting(Bundle extras) {
+ ContactsPermissions.enforceCallingOrSelfPermission(getContext(),
+ SET_DEFAULT_ACCOUNT_PERMISSION);
+ final String accountName = extras.getString(Settings.ACCOUNT_NAME);
+ final String accountType = extras.getString(Settings.ACCOUNT_TYPE);
+ final String dataSet = extras.getString(Settings.DATA_SET);
- final Bundle response = new Bundle();
- final SQLiteDatabase db = mDbHelper.get().getWritableDatabase();
- db.beginTransaction();
- try {
- mDbHelper.get().setDefaultAccount(accountName, accountType);
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- return response;
+ if (TextUtils.isEmpty(accountName) ^ TextUtils.isEmpty(accountType)) {
+ throw new IllegalArgumentException(
+ "Must specify both or neither of ACCOUNT_NAME and ACCOUNT_TYPE");
}
- return null;
+ if (!TextUtils.isEmpty(dataSet)) {
+ throw new IllegalArgumentException(
+ "Cannot set default account with non-null data set.");
+ }
+
+ AccountWithDataSet accountWithDataSet = new AccountWithDataSet(
+ accountName, accountType, dataSet);
+ Account[] systemAccounts = AccountManager.get(getContext()).getAccounts();
+ List<SimAccount> simAccounts = mDbHelper.get().getAllSimAccounts();
+ if (!accountWithDataSet.isLocalAccount()
+ && !accountWithDataSet.inSystemAccounts(systemAccounts)
+ && !accountWithDataSet.inSimAccounts(simAccounts)) {
+ throw new IllegalArgumentException(
+ "Cannot set default account for invalid accounts.");
+ }
+
+ final Bundle response = new Bundle();
+ final SQLiteDatabase db = mDbHelper.get().getWritableDatabase();
+ db.beginTransaction();
+ try {
+ mDbHelper.get().setDefaultAccount(accountName, accountType);
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ return response;
}
/**