Skip to content
Snippets Groups Projects
Commit 14191672 authored by Zemeng Li's avatar Zemeng Li Committed by Lili Lin
Browse files

Fix NullPointerException to get sim accounts

After the process "android.process.acore" is killed, the null value will be returned by ContentResolver when the api "getSimAccounts" is called, which will throw NullPointerException.

Explicitly get a ContentProviderClient and call on it. When the process is killed, the RemoteException will be catched.

Bug: 231687839
Change-Id: Idb265d9fcc781612e571deae5014729e3ed59b44
parent 3c825a4d
No related branches found
No related tags found
No related merge requests found
......@@ -8410,7 +8410,7 @@ public final class ContactsContract {
extras.putString(KEY_ACCOUNT_NAME, accountName);
extras.putString(KEY_ACCOUNT_TYPE, accountType);
contentResolver.call(ContactsContract.AUTHORITY_URI,
nullSafeCall(contentResolver, ContactsContract.AUTHORITY_URI,
ContactsContract.SimContacts.ADD_SIM_ACCOUNT_METHOD,
null, extras);
}
......@@ -8433,7 +8433,7 @@ public final class ContactsContract {
Bundle extras = new Bundle();
extras.putInt(KEY_SIM_SLOT_INDEX, simSlotIndex);
contentResolver.call(ContactsContract.AUTHORITY_URI,
nullSafeCall(contentResolver, ContactsContract.AUTHORITY_URI,
ContactsContract.SimContacts.REMOVE_SIM_ACCOUNT_METHOD,
null, extras);
}
......@@ -8445,7 +8445,7 @@ public final class ContactsContract {
*/
public static @NonNull List<SimAccount> getSimAccounts(
@NonNull ContentResolver contentResolver) {
Bundle response = contentResolver.call(ContactsContract.AUTHORITY_URI,
Bundle response = nullSafeCall(contentResolver, ContactsContract.AUTHORITY_URI,
ContactsContract.SimContacts.QUERY_SIM_ACCOUNTS_METHOD,
null, null);
List<SimAccount> result = response.getParcelableArrayList(KEY_SIM_ACCOUNTS);
......@@ -9064,7 +9064,8 @@ public final class ContactsContract {
* @param contactId the id of the contact to undemote.
*/
public static void undemote(ContentResolver contentResolver, long contactId) {
contentResolver.call(ContactsContract.AUTHORITY_URI, PinnedPositions.UNDEMOTE_METHOD,
nullSafeCall(contentResolver, ContactsContract.AUTHORITY_URI,
PinnedPositions.UNDEMOTE_METHOD,
String.valueOf(contactId), null);
}
......@@ -10276,4 +10277,13 @@ public final class ContactsContract {
public static final String CONTENT_ITEM_TYPE =
"vnd.android.cursor.item/contact_metadata_sync_state";
}
private static Bundle nullSafeCall(@NonNull ContentResolver resolver, @NonNull Uri uri,
@NonNull String method, @Nullable String arg, @Nullable Bundle extras) {
try (ContentProviderClient client = resolver.acquireContentProviderClient(uri)) {
return client.call(method, arg, extras);
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment