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
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user