/** Return a Cursor containing just one message from the ICC. */ private Cursor getSingleMessageFromIcc(String messageIndexString) { int messageIndex = -1; try { Integer.parseInt(messageIndexString); } catch (NumberFormatException exception) { throw new IllegalArgumentException("Bad SMS ICC ID: " + messageIndexString); } ArrayList<SmsMessage> messages; final SmsManager smsManager = SmsManager.getDefault(); // Use phone id to avoid AppOps uid mismatch in telephony long token = Binder.clearCallingIdentity(); try { messages = smsManager.getAllMessagesFromIcc(); } finally { Binder.restoreCallingIdentity(token); } if (messages == null) { throw new IllegalArgumentException("ICC message not retrieved"); } final SmsMessage message = messages.get(messageIndex); if (message == null) { throw new IllegalArgumentException("Message not retrieved. ID: " + messageIndexString); } MatrixCursor cursor = new MatrixCursor(ICC_COLUMNS, 1); cursor.addRow(convertIccToSms(message, 0)); return withIccNotificationUri(cursor); }
/** Return a Cursor listing all the messages stored on the ICC. */ private Cursor getAllMessagesFromIcc() { SmsManager smsManager = SmsManager.getDefault(); ArrayList<SmsMessage> messages; // use phone app permissions to avoid UID mismatch in AppOpsManager.noteOp() call long token = Binder.clearCallingIdentity(); try { messages = smsManager.getAllMessagesFromIcc(); } finally { Binder.restoreCallingIdentity(token); } final int count = messages.size(); MatrixCursor cursor = new MatrixCursor(ICC_COLUMNS, count); for (int i = 0; i < count; i++) { SmsMessage message = messages.get(i); if (message != null) { cursor.addRow(convertIccToSms(message, i)); } } return withIccNotificationUri(cursor); }