From 1419167262f06e25bd0bd2f9f0d98a226511851e Mon Sep 17 00:00:00 2001 From: Zemeng Li Date: Fri, 24 Jun 2022 14:25:00 +0800 Subject: 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 --- core/java/android/provider/ContactsContract.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/core/java/android/provider/ContactsContract.java b/core/java/android/provider/ContactsContract.java index 3c1b4ba8f8b5..0012572540ae 100644 --- a/core/java/android/provider/ContactsContract.java +++ b/core/java/android/provider/ContactsContract.java @@ -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 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 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(); + } + } } -- cgit v1.2.3-59-g8ed1b