Android providers的源代码结构如下:
android-2.2-froyo/com/android/providers:
本篇来解析一下telephony这个provider中的代码,首先来看一下它的目录结构:
android-2.2-froyo/com/android/providers/telephony:
以上6个类可以划分为3类:
第一类:数据库管理工具类 (A helper class to manage database),包括:MmsSmsDatabaseHelper.java
第二类:与电话操作相关数据类,包括:Telephony,它是个特殊的类,隐藏类
第三类:内容提供者 (ContentProvider),包括:MmsProvider.java、SmsProvider.java、MmsSmsProvider.java、TelephonyProvider.java,它们都继承于ContentProvider。
下面分别讲解一下以上三大类别的代码:
一、MmsSmsDatabaseHelper.java
由于它的代码有2828行,不宜贴出,这里抽取几个重要变量和方法来解析一下,
public class MmsSmsDatabaseHelper extends SQLiteOpenHelper 说明它是作为数据库管理工具类,
static final String DATABASE_NAME = "mmssms.db"; /// M: Code analyze 006, unknown, for database upgrade. static final int DATABASE_VERSION = 560000;
这里定义了数据库的名称和初始化版本。
/// M: Add for ip message @{ // this need be the first because it is used by later final consts. private static final String UPDATE_THREAD_READ_COUNT = " UPDATE threads SET readcount = " + " (SELECT count(_id)FROM " + " (SELECT DISTINCT date * 1 AS normalized_date, _id, read FROM sms " + " WHERE ((read=1) AND thread_id = new.thread_id AND (type != 3)) " + " UNION SELECT DISTINCT date * 1000 AS normalized_date, pdu._id, read " + " FROM pdu LEFT JOIN pending_msgs ON pdu._id = pending_msgs.msg_id " + " WHERE ((read=1) AND thread_id = new.thread_id AND msg_box != 3 AND (msg_box != 3 " + " AND (m_type = 128 OR m_type = 132 OR m_type = 130)))" + " UNION SELECT DISTINCT date * 1 AS normalized_date, _id, read FROM cellbroadcast " + " WHERE ((read=1) AND thread_id = new.thread_id) ORDER BY normalized_date ASC)) " + " WHERE threads._id = new.thread_id; ";
其中还定义了类似的数据库语句的final型变量,如:UPDATE_THREAD_READ_COUNT_OLD、SMS_UPDATE_THREAD_READ_BODY、UPDATE_THREAD_COUNT_ON_NEW、UPDATE_THREAD_COUNT_ON_OLD、SMS_UPDATE_THREAD_DATE_SNIPPET_COUNT_ON_UPDATE等。
二、Telephony.java
The Telephony provider contains data related to phone operation.
该类的outline:
该类中定义了几个非常重要的内部类,在MmsProvider,SmsProvider,MmsSmsProvider中会用到。
1、接口 TextBasedSmsColumns:Base columns for tables that contain text based SMSs.
2、接口 BaseMmsColumns :Base columns for tables that contain MMSs.
public interface BaseMmsColumns extends BaseColumns 它继承BaseColumns这个接口。
3、接口 CanonicalAddressesColumns :Columns for the "canonical_addresses" table used by MMS and
* SMS."
public interface CanonicalAddressesColumns extends BaseColumns。
4、接口 ThreadsColumns :Columns for the "threads" table used by MMS and SMS.
public interface ThreadsColumns extends BaseColumns.
5、类 Sms:
类结构截图
Sms中定义了6个内部类:Inbox、Sent、Draft、Outbox、Conversations、Intents。
代码:
/** * Contains all text based SMS messages. */ public static final class Sms implements BaseColumns, TextBasedSmsColumns { public static final Cursor query(ContentResolver cr, String[] projection) { return cr.query(CONTENT_URI, projection, null, null, DEFAULT_SORT_ORDER); } public static final Cursor query(ContentResolver cr, String[] projection, String where, String orderBy) { return cr.query(CONTENT_URI, projection, where, null, orderBy == null ? DEFAULT_SORT_ORDER : orderBy); } /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://sms"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; /** * Add an SMS to the given URI. * * @param resolver the content resolver to use * @param uri the URI to add the message to * @param address the address of the sender * @param body the body of the message * @param subject the psuedo-subject of the message * @param date the timestamp for the message * @param read true if the message has been read, false if not * @param deliveryReport true if a delivery report was requested, false if not * @return the URI for the new message */ public static Uri addMessageToUri(ContentResolver resolver, Uri uri, String address, String body, String subject, Long date, boolean read, boolean deliveryReport) { return addMessageToUri(resolver, uri, address, body, subject, date, read, deliveryReport, -1L); } /** * Add an SMS to the given URI with thread_id specified. * * @param resolver the content resolver to use * @param uri the URI to add the message to * @param address the address of the sender * @param body the body of the message * @param subject the psuedo-subject of the message * @param date the timestamp for the message * @param read true if the message has been read, false if not * @param deliveryReport true if a delivery report was requested, false if not * @param threadId the thread_id of the message * @return the URI for the new message */ public static Uri addMessageToUri(ContentResolver resolver, Uri uri, String address, String body, String subject, Long date, boolean read, boolean deliveryReport, long threadId) { ContentValues values = new ContentValues(7); values.put(ADDRESS, address); if (date != null) { values.put(DATE, date); } values.put(READ, read ? Integer.valueOf(1) : Integer.valueOf(0)); values.put(SUBJECT, subject); values.put(BODY, body); if (deliveryReport) { values.put(STATUS, STATUS_PENDING); } if (threadId != -1L) { values.put(THREAD_ID, threadId); } return resolver.insert(uri, values); } /** * Move a message to the given folder. * * @param context the context to use * @param uri the message to move * @param folder the folder to move to * @return true if the operation succeeded */ public static boolean moveMessageToFolder(Context context, Uri uri, int folder, int error) { if (uri == null) { return false; } boolean markAsUnread = false; boolean markAsRead = false; switch(folder) { case MESSAGE_TYPE_INBOX: case MESSAGE_TYPE_DRAFT: break; case MESSAGE_TYPE_OUTBOX: case MESSAGE_TYPE_SENT: markAsRead = true; break; case MESSAGE_TYPE_FAILED: case MESSAGE_TYPE_QUEUED: markAsUnread = true; break; default: return false; } ContentValues values = new ContentValues(3); values.put(TYPE, folder); if (markAsUnread) { values.put(READ, Integer.valueOf(0)); } else if (markAsRead) { values.put(READ, Integer.valueOf(1)); } values.put(ERROR_CODE, error); return 1 == SqliteWrapper.update(context, context.getContentResolver(), uri, values, null, null); } /** * Returns true iff the folder (message type) identifies an * outgoing message. */ public static boolean isOutgoingFolder(int messageType) { return (messageType == MESSAGE_TYPE_FAILED) || (messageType == MESSAGE_TYPE_OUTBOX) || (messageType == MESSAGE_TYPE_SENT) || (messageType == MESSAGE_TYPE_QUEUED); } /** * Contains all text based SMS messages in the SMS app's inbox. */ public static final class Inbox implements BaseColumns, TextBasedSmsColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://sms/inbox"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; /** * Add an SMS to the Draft box. * * @param resolver the content resolver to use * @param address the address of the sender * @param body the body of the message * @param subject the psuedo-subject of the message * @param date the timestamp for the message * @param read true if the message has been read, false if not * @return the URI for the new message */ public static Uri addMessage(ContentResolver resolver, String address, String body, String subject, Long date, boolean read) { return addMessageToUri(resolver, CONTENT_URI, address, body, subject, date, read, false); } } /** * Contains all sent text based SMS messages in the SMS app's. */ public static final class Sent implements BaseColumns, TextBasedSmsColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://sms/sent"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; /** * Add an SMS to the Draft box. * * @param resolver the content resolver to use * @param address the address of the sender * @param body the body of the message * @param subject the psuedo-subject of the message * @param date the timestamp for the message * @return the URI for the new message */ public static Uri addMessage(ContentResolver resolver, String address, String body, String subject, Long date) { return addMessageToUri(resolver, CONTENT_URI, address, body, subject, date, true, false); } } /** * Contains all sent text based SMS messages in the SMS app's. */ public static final class Draft implements BaseColumns, TextBasedSmsColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://sms/draft"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; /** * Add an SMS to the Draft box. * * @param resolver the content resolver to use * @param address the address of the sender * @param body the body of the message * @param subject the psuedo-subject of the message * @param date the timestamp for the message * @return the URI for the new message */ public static Uri addMessage(ContentResolver resolver, String address, String body, String subject, Long date) { return addMessageToUri(resolver, CONTENT_URI, address, body, subject, date, true, false); } /** * Save over an existing draft message. * * @param resolver the content resolver to use * @param uri of existing message * @param body the new body for the draft message * @return true is successful, false otherwise */ public static boolean saveMessage(ContentResolver resolver, Uri uri, String body) { ContentValues values = new ContentValues(2); values.put(BODY, body); values.put(DATE, System.currentTimeMillis()); return resolver.update(uri, values, null, null) == 1; } } /** * Contains all pending outgoing text based SMS messages. */ public static final class Outbox implements BaseColumns, TextBasedSmsColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://sms/outbox"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; /** * Add an SMS to the Out box. * * @param resolver the content resolver to use * @param address the address of the sender * @param body the body of the message * @param subject the psuedo-subject of the message * @param date the timestamp for the message * @param deliveryReport whether a delivery report was requested for the message * @return the URI for the new message */ public static Uri addMessage(ContentResolver resolver, String address, String body, String subject, Long date, boolean deliveryReport, long threadId) { return addMessageToUri(resolver, CONTENT_URI, address, body, subject, date, true, deliveryReport, threadId); } } /** * Contains all sent text-based SMS messages in the SMS app's. */ public static final class Conversations implements BaseColumns, TextBasedSmsColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://sms/conversations"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; /** * The first 45 characters of the body of the message * <P>Type: TEXT</P> */ public static final String SNIPPET = "snippet"; /** * The number of messages in the conversation * <P>Type: INTEGER</P> */ public static final String MESSAGE_COUNT = "msg_count"; } /** * Contains info about SMS related Intents that are broadcast. */ public static final class Intents { /** * Set by BroadcastReceiver. Indicates the message was handled * successfully. */ public static final int RESULT_SMS_HANDLED = 1; /** * Set by BroadcastReceiver. Indicates a generic error while * processing the message. */ public static final int RESULT_SMS_GENERIC_ERROR = 2; /** * Set by BroadcastReceiver. Indicates insufficient memory to store * the message. */ public static final int RESULT_SMS_OUT_OF_MEMORY = 3; /** * Set by BroadcastReceiver. Indicates the message, while * possibly valid, is of a format or encoding that is not * supported. */ public static final int RESULT_SMS_UNSUPPORTED = 4; /** * Broadcast Action: A new text based SMS message has been received * by the device. The intent will have the following extra * values:</p> * * <ul> * <li><em>pdus</em> - An Object[] od byte[]s containing the PDUs * that make up the message.</li> * </ul> * * <p>The extra values can be extracted using * {@link #getMessagesFromIntent(Intent)}.</p> * * <p>If a BroadcastReceiver encounters an error while processing * this intent it should set the result code appropriately.</p> */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; /** * Broadcast Action: A new data based SMS message has been received * by the device. The intent will have the following extra * values:</p> * * <ul> * <li><em>pdus</em> - An Object[] of byte[]s containing the PDUs * that make up the message.</li> * </ul> * * <p>The extra values can be extracted using * {@link #getMessagesFromIntent(Intent)}.</p> * * <p>If a BroadcastReceiver encounters an error while processing * this intent it should set the result code appropriately.</p> */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String DATA_SMS_RECEIVED_ACTION = "android.intent.action.DATA_SMS_RECEIVED"; /** * Broadcast Action: A new WAP PUSH message has been received by the * device. The intent will have the following extra * values:</p> * * <ul> * <li><em>transactionId (Integer)</em> - The WAP transaction ID</li> * <li><em>pduType (Integer)</em> - The WAP PDU type</li> * <li><em>header (byte[])</em> - The header of the message</li> * <li><em>data (byte[])</em> - The data payload of the message</li> * <li><em>contentTypeParameters (HashMap<String,String>)</em> * - Any parameters associated with the content type * (decoded from the WSP Content-Type header)</li> * </ul> * * <p>If a BroadcastReceiver encounters an error while processing * this intent it should set the result code appropriately.</p> * * <p>The contentTypeParameters extra value is map of content parameters keyed by * their names.</p> * * <p>If any unassigned well-known parameters are encountered, the key of the map will * be 'unassigned/0x...', where '...' is the hex value of the unassigned parameter. If * a parameter has No-Value the value in the map will be null.</p> */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String WAP_PUSH_RECEIVED_ACTION = "android.provider.Telephony.WAP_PUSH_RECEIVED"; /** * Broadcast Action: A new Cell Broadcast message has been received * by the device. The intent will have the following extra * values:</p> * * <ul> * <li><em>pdus</em> - An Object[] of byte[]s containing the PDUs * that make up the message.</li> * </ul> * * <p>The extra values can be extracted using * {@link #getMessagesFromIntent(Intent)}.</p> * * <p>If a BroadcastReceiver encounters an error while processing * this intent it should set the result code appropriately.</p> */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String SMS_CB_RECEIVED_ACTION = "android.provider.Telephony.SMS_CB_RECEIVED"; /** * Broadcast Action: A new Emergency Broadcast message has been received * by the device. The intent will have the following extra * values:</p> * * <ul> * <li><em>pdus</em> - An Object[] of byte[]s containing the PDUs * that make up the message.</li> * </ul> * * <p>The extra values can be extracted using * {@link #getMessagesFromIntent(Intent)}.</p> * * <p>If a BroadcastReceiver encounters an error while processing * this intent it should set the result code appropriately.</p> */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String SMS_EMERGENCY_CB_RECEIVED_ACTION = "android.provider.Telephony.SMS_EMERGENCY_CB_RECEIVED"; /** * Broadcast Action: The SIM storage for SMS messages is full. If * space is not freed, messages targeted for the SIM (class 2) may * not be saved. */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String SIM_FULL_ACTION = "android.provider.Telephony.SIM_FULL"; /** * Broadcast Action: An incoming SMS has been rejected by the * telephony framework. This intent is sent in lieu of any * of the RECEIVED_ACTION intents. The intent will have the * following extra value:</p> * * <ul> * <li><em>result</em> - An int result code, eg, * <code>{@link #RESULT_SMS_OUT_OF_MEMORY}</code>, * indicating the error returned to the network.</li> * </ul> */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String SMS_REJECTED_ACTION = "android.provider.Telephony.SMS_REJECTED"; /** * Read the PDUs out of an {@link #SMS_RECEIVED_ACTION} or a * {@link #DATA_SMS_RECEIVED_ACTION} intent. * * @param intent the intent to read from * @return an array of SmsMessages for the PDUs */ public static SmsMessage[] getMessagesFromIntent( Intent intent) { Object[] messages = (Object[]) intent.getSerializableExtra("pdus"); String format = intent.getStringExtra("format"); byte[][] pduObjs = new byte[messages.length][]; for (int i = 0; i < messages.length; i++) { pduObjs[i] = (byte[]) messages[i]; } byte[][] pdus = new byte[pduObjs.length][]; int pduCount = pdus.length; SmsMessage[] msgs = new SmsMessage[pduCount]; for (int i = 0; i < pduCount; i++) { pdus[i] = pduObjs[i]; msgs[i] = SmsMessage.createFromPdu(pdus[i], format); } return msgs; } } }
6、类Mms:
类结构截图
代码:
/** * Contains all MMS messages. */ public static final class Mms implements BaseMmsColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://mms"); public static final Uri REPORT_REQUEST_URI = Uri.withAppendedPath( CONTENT_URI, "report-request"); public static final Uri REPORT_STATUS_URI = Uri.withAppendedPath( CONTENT_URI, "report-status"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; /** * mailbox = name-addr * name-addr = [display-name] angle-addr * angle-addr = [CFWS] "<" addr-spec ">" [CFWS] */ public static final Pattern NAME_ADDR_EMAIL_PATTERN = Pattern.compile("\\s*(\"[^\"]*\"|[^<>\"]+)\\s*<([^<>]+)>\\s*"); /** * quoted-string = [CFWS] * DQUOTE *([FWS] qcontent) [FWS] DQUOTE * [CFWS] */ public static final Pattern QUOTED_STRING_PATTERN = Pattern.compile("\\s*\"([^\"]*)\"\\s*"); public static final Cursor query( ContentResolver cr, String[] projection) { return cr.query(CONTENT_URI, projection, null, null, DEFAULT_SORT_ORDER); } public static final Cursor query( ContentResolver cr, String[] projection, String where, String orderBy) { return cr.query(CONTENT_URI, projection, where, null, orderBy == null ? DEFAULT_SORT_ORDER : orderBy); } public static final String getMessageBoxName(int msgBox) { switch (msgBox) { case MESSAGE_BOX_ALL: return "all"; case MESSAGE_BOX_INBOX: return "inbox"; case MESSAGE_BOX_SENT: return "sent"; case MESSAGE_BOX_DRAFTS: return "drafts"; case MESSAGE_BOX_OUTBOX: return "outbox"; default: throw new IllegalArgumentException("Invalid message box: " + msgBox); } } public static String extractAddrSpec(String address) { Matcher match = NAME_ADDR_EMAIL_PATTERN.matcher(address); if (match.matches()) { return match.group(2); } return address; } /** * Returns true if the address is an email address * * @param address the input address to be tested * @return true if address is an email address */ public static boolean isEmailAddress(String address) { if (TextUtils.isEmpty(address)) { return false; } String s = extractAddrSpec(address); Matcher match = Patterns.EMAIL_ADDRESS.matcher(s); return match.matches(); } /** * Returns true if the number is a Phone number * * @param number the input number to be tested * @return true if number is a Phone number */ public static boolean isPhoneNumber(String number) { if (TextUtils.isEmpty(number)) { return false; } Matcher match = Patterns.PHONE.matcher(number); return match.matches(); } /** * Contains all MMS messages in the MMS app's inbox. */ public static final class Inbox implements BaseMmsColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://mms/inbox"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; } /** * Contains all MMS messages in the MMS app's sent box. */ public static final class Sent implements BaseMmsColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://mms/sent"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; } /** * Contains all MMS messages in the MMS app's drafts box. */ public static final class Draft implements BaseMmsColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://mms/drafts"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; } /** * Contains all MMS messages in the MMS app's outbox. */ public static final class Outbox implements BaseMmsColumns { /** * The content:// style URL for this table */ public static final Uri CONTENT_URI = Uri.parse("content://mms/outbox"); /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = "date DESC"; } public static final class Addr implements BaseColumns { /** * The ID of MM which this address entry belongs to. */ public static final String MSG_ID = "msg_id"; /** * The ID of contact entry in Phone Book. */ public static final String CONTACT_ID = "contact_id"; /** * The address text. */ public static final String ADDRESS = "address"; /** * Type of address, must be one of PduHeaders.BCC, * PduHeaders.CC, PduHeaders.FROM, PduHeaders.TO. */ public static final String TYPE = "type"; /** * Character set of this entry. */ public static final String CHARSET = "charset"; } public static final class Part implements BaseColumns { /** * The identifier of the message which this part belongs to. * <P>Type: INTEGER</P> */ public static final String MSG_ID = "mid"; /** * The order of the part. * <P>Type: INTEGER</P> */ public static final String SEQ = "seq"; /** * The content type of the part. * <P>Type: TEXT</P> */ public static final String CONTENT_TYPE = "ct"; /** * The name of the part. * <P>Type: TEXT</P> */ public static final String NAME = "name"; /** * The charset of the part. * <P>Type: TEXT</P> */ public static final String CHARSET = "chset"; /** * The file name of the part. * <P>Type: TEXT</P> */ public static final String FILENAME = "fn"; /** * The content disposition of the part. * <P>Type: TEXT</P> */ public static final String CONTENT_DISPOSITION = "cd"; /** * The content ID of the part. * <P>Type: INTEGER</P> */ public static final String CONTENT_ID = "cid"; /** * The content location of the part. * <P>Type: INTEGER</P> */ public static final String CONTENT_LOCATION = "cl"; /** * The start of content-type of the message. * <P>Type: INTEGER</P> */ public static final String CT_START = "ctt_s"; /** * The type of content-type of the message. * <P>Type: TEXT</P> */ public static final String CT_TYPE = "ctt_t"; /** * The location(on filesystem) of the binary data of the part. * <P>Type: INTEGER</P> */ public static final String _DATA = "_data"; public static final String TEXT = "text"; } public static final class Rate { public static final Uri CONTENT_URI = Uri.withAppendedPath( Mms.CONTENT_URI, "rate"); /** * When a message was successfully sent. * <P>Type: INTEGER</P> */ public static final String SENT_TIME = "sent_time"; } public static final class Intents { private Intents() { // Non-instantiatable. } /** * The extra field to store the contents of the Intent, * which should be an array of Uri. */ public static final String EXTRA_CONTENTS = "contents"; /** * The extra field to store the type of the contents, * which should be an array of String. */ public static final String EXTRA_TYPES = "types"; /** * The extra field to store the 'Cc' addresses. */ public static final String EXTRA_CC = "cc"; /** * The extra field to store the 'Bcc' addresses; */ public static final String EXTRA_BCC = "bcc"; /** * The extra field to store the 'Subject'. */ public static final String EXTRA_SUBJECT = "subject"; /** * Indicates that the contents of specified URIs were changed. * The application which is showing or caching these contents * should be updated. */ public static final String CONTENT_CHANGED_ACTION = "android.intent.action.CONTENT_CHANGED"; /** * An extra field which stores the URI of deleted contents. */ public static final String DELETED_CONTENTS = "deleted_contents"; } }
7、类MmsSms:
类结构截图
7、类 Threads:
类结构截图
代码:
/** * Helper functions for the "threads" table used by MMS and SMS. */ public static final class Threads implements ThreadsColumns { private static final String[] ID_PROJECTION = { BaseColumns._ID }; private static final String STANDARD_ENCODING = "UTF-8"; private static final Uri THREAD_ID_CONTENT_URI = Uri.parse( "content://mms-sms/threadID"); public static final Uri CONTENT_URI = Uri.withAppendedPath( MmsSms.CONTENT_URI, "conversations"); public static final Uri OBSOLETE_THREADS_URI = Uri.withAppendedPath( CONTENT_URI, "obsolete"); public static final int COMMON_THREAD = 0; public static final int BROADCAST_THREAD = 1; // No one should construct an instance of this class. private Threads() { } /** * This is a single-recipient version of * getOrCreateThreadId. It's convenient for use with SMS * messages. */ public static long getOrCreateThreadId(Context context, String recipient) { Set<String> recipients = new HashSet<String>(); recipients.add(recipient); return getOrCreateThreadId(context, recipients); } /** * Given the recipients list and subject of an unsaved message, * return its thread ID. If the message starts a new thread, * allocate a new thread ID. Otherwise, use the appropriate * existing thread ID. * * Find the thread ID of the same set of recipients (in * any order, without any additions). If one * is found, return it. Otherwise, return a unique thread ID. */ public static long getOrCreateThreadId( Context context, Set<String> recipients) { Uri.Builder uriBuilder = THREAD_ID_CONTENT_URI.buildUpon(); for (String recipient : recipients) { if (Mms.isEmailAddress(recipient)) { recipient = Mms.extractAddrSpec(recipient); } uriBuilder.appendQueryParameter("recipient", recipient); } Uri uri = uriBuilder.build(); //if (DEBUG) Log.v(TAG, "getOrCreateThreadId uri: " + uri); Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(), uri, ID_PROJECTION, null, null, null); if (DEBUG) { Log.v(TAG, "getOrCreateThreadId cursor cnt: " + cursor.getCount()); } if (cursor != null) { try { if (cursor.moveToFirst()) { return cursor.getLong(0); } else { Log.e(TAG, "getOrCreateThreadId returned no rows!"); } } finally { cursor.close(); } } Log.e(TAG, "getOrCreateThreadId failed with uri " + uri.toString()); throw new IllegalArgumentException("Unable to find or allocate a thread ID."); } }
8、类 Carriers:
类结构截图
9、类 Intents:
类结构截图
三、SmsProvider.java
The class to provide base facility to access MMS related content, which is stored in a SQLite database and in the file system.
短信的uri以content://sms开头。
可以使用Telephony中的Sms类提供的公共变量进行构造Uri,甚至使用其中提供的一些addMessage(将信息存入各个不同的信箱)、saveMessage(更新已存在的草稿)、addMessageToUri(插入数据到指定Uri)、moveMessageToFolder(将信息移到某一信箱)方法简化操作。
UriMatcher中配置了27种类型:
AUTHORITY |
PATH
|
类型 |
说明 |
sms |
SMS_ALL |
所有短信 |
|
sms |
# |
SMS_ALL_ID |
指定了_id的短信 |
sms |
inbox |
SMS_INBOX |
位于收件箱的短信 |
sms |
inbox/# |
SMS_INBOX_ID |
位于收件箱的指定了_id的短信 |
sms |
sent |
SMS_SENT |
已成功发送出去的短信 |
sms |
sent/# |
SMS_SENT_ID |
已成功发送出去的指定了_id的短信 |
sms |
draft |
SMS_DRAFT |
位于草稿箱的短信 |
sms |
draft |
SMS_DRAFT_ID |
位于草稿箱的指定了_id的短信 |
sms |
outbox |
SMS_OUTBOX |
已从队列中取出进行处理但未成功发送出去的短信 |
sms |
outbox/# |
SMS_OUTBOX_ID |
已从队列中取出进行处理但未成功发送出去的指定了_id的短信 |
sms |
undelivered |
SMS_UNDELIVERED |
未成功发送出去的短信(包括发送失败、草稿、队列中的) |
sms |
failed |
SMS_FAILED |
发送失败的短信 |
sms |
failed/# |
SMS_FAILED_ID |
发送失败的指定了_id的短信 |
sms |
queued |
SMS_QUEUED |
队列中的短信 |
sms |
conversations |
SMS_CONVERSATIONS |
所有会话包含的短信数量、内容、会话id |
sms |
conversations/* |
SMS_CONVERSATIONS_ID |
指定会话id的短信 |
sms |
raw |
SMS_RAW_MESSAGE |
raw表中的短信(临时短信,用于接收长短信时使用) |
sms |
attachments |
SMS_ATTACHMENT |
往attachments表中插入或查询信息 |
sms |
attachments/# |
SMS_ATTACHMENT_ID |
从attachments表中查询指定sms_id的信息 |
sms |
threadID |
SMS_NEW_THREAD_ID |
往canonical_addresses表中插入信息 |
sms |
threadID/* |
SMS_QUERY_THREAD_ID |
查询canonical_addresses中所有信息 |
sms |
status/# |
SMS_STATUS_ID |
查询或更新指定id的短信(没有什么特殊功能,有可能没用上) |
sms |
sr_pending |
SMS_STATUS_PENDING |
对sr_pending表进行操作时用的 |
sms |
icc |
SMS_ALL_ICC |
所有存在SIM/UIM卡上的短信 |
sms |
icc/# |
SMS_ICC |
指定id的存在SIM/UIM卡上的短信 |
sms |
sim |
SMS_ALL_ICC |
所有存在SIM/UIM卡上的短信 |
sms |
sim/# |
SMS_ICC |
指定id的存在SIM/UIM卡上的短信 |
四、MmsProvider
短信的uri以content://mms开头。
可以使用Telephony中的Mms类提供的公共变量进行构造Uri,甚至使用其中提供的一些query(查询)、isEmailAddress、isPhoneNumber方法简化操作。
UriMatcher中配置了21种类型:
AUTHORITY |
PATH |
类型 |
说明 |
mms |
MMS_ALL |
所有彩信 |
|
mms |
# |
MMS_ALL_ID |
指定了id的彩信 |
mms |
inbox |
MMS_INBOX |
位于收件箱的彩信 |
mms |
inbox/# |
MMS_INBOX_ID |
位于收件箱的指定了_id的彩信 |
mms |
sent |
MMS_SENT |
已成功发送出去的彩信 |
mms |
sent/# |
MMS_SENT_ID |
已成功发送出去的指定了_id的彩信 |
mms |
drafts |
MMS_DRAFTS |
位于草稿箱的彩信 |
mms |
drafts/# |
MMS_DRAFTS_ID |
位于草稿箱的指定了_id的彩信 |
mms |
outbox |
MMS_OUTBOX |
已开始进行处理但未成功发送出去的短信 |
mms |
outbox/# |
MMS_OUTBOX_ID |
已开始进行处理但未成功发送出去的指定了_id的短信 |
mms |
part |
MMS_ALL_PART |
对part表的查询与删除 |
mms |
#/part |
MMS_MSG_PART |
对part表的指定了msg_id的记录的增、删、改、查 |
mms |
part/# |
MMS_PART_ID |
对part表的指定了id的记录的删、改、查 |
mms |
#/addr |
MMS_MSG_ADDR |
对addr表的指定了msg_id的记录的增、删、查 |
mms |
rate |
MMS_SENDING_RATE |
查询rate表中的信息,删除1小时以前的记录并插入新记录 |
mms |
report-status/# |
MMS_REPORT_STATUS |
查询指定id的彩信的状态(SQL语句很复杂) |
mms |
report-request/# |
MMS_REPORT_REQUEST |
查询指定id的,addr表的type为151的彩信(同report-status一起使用) |
mms |
drm |
MMS_DRM_STORAGE |
对drm表的删除与插入 |
mms |
drm/# |
MMS_DRM_STORAGE_ID |
对指定了id的drm表的数据的查询 |
mms |
threads |
MMS_THREADS |
对pdu表按thread_id分组统计(即获取所有包含彩信的thread_id) |
mms |
scrapSpace |
MMS_SCRAP_SPACE |
当添加附件时选择拍照,用于获取临时存储空间时使用 |
五、MmsSmsProvider
该ContentProvider的uri以content://mms-sms开头。
可以使用Telephony中的MmsSms类提供的公共变量进行构造Uri。
UriMatcher中配置了18种类型:
AUTHORITY |
PATH |
类型 |
说明 |
mms-sms |
conversations |
URI_CONVERSATIONS |
用于删除所有会话或查询所有会话(查询分两种模式:1、指定了simple为true,则仅仅从threads表中查询数据;2、未指定,则分别从sms和pdu表查询最新的不算草稿的短信和彩信) |
mms-sms |
complete-conversations |
URI_COMPLETE_CONVERSATION |
查询已完成的消息(包括非草稿短彩信,已完全接收或已发送出去的彩信) |
mms-sms |
conversations/# |
URI_CONVERSATIONS_MESSAGES |
用于查询(指定id的会话的消息)、删除、更新(分别更新pdu和sms表)指定id的会话 |
mms-sms |
conversations/#/recipients |
URI_CONVERSATIONS_RECIPIENTS |
用于查询指定id的会话 |
mms-sms |
conversations/#/subject |
URI_CONVERSATIONS_SUBJECT |
同上 |
mms-sms |
conversations/obsolete |
URI_OBSOLETE_THREADS |
用于删除没有消息的会话 |
mms-sms |
messages/byphone/* |
URI_MESSAGES_BY_PHONE |
用于查询接收者中包含指定电话号码的消息 |
mms-sms |
threadID |
URI_THREAD_ID |
获取指定接收者(附加的查询参数recipient)的会话id,若不存在,新增 |
mms-sms |
canonical-address/# |
URI_CANONICAL_ADDRESS |
查询或更新指定id的canonical_address表中的数据 |
mms-sms |
canonical-addresses |
URI_CANONICAL_ADDRESSES |
查询canonical_address表中的数据 |
mms-sms |
search |
URI_SEARCH |
从短信和彩信中搜索符合给定的pattern的数据 |
mms-sms |
searchSuggest |
URI_SEARCH_SUGGEST |
从word表中搜索符合给定的pattern的数据,上限50条 |
mms-sms |
pending |
URI_PENDING_MSG |
查询(可指定protocol为sms或mms,message为某个msg_id)或更新pending_msgs表 |
mms-sms |
undelivered |
URI_UNDELIVERED_MSG |
查询所有未成功发送的短信与彩信 |
mms-sms |
notifications |
URI_NOTIFICATIONS |
无代码处理 |
mms-sms |
draft |
URI_DRAFT |
查询所有短信与彩信的草稿,最多输出threadid与id列 |
mms-sms |
locked |
URI_FIRST_LOCKED_MESSAGE_ALL |
查询所有会话中的被锁定的消息,仅输出1条,用于确定是否有被锁定的消息 |
mms-sms |
locked/# |
URI_FIRST_LOCKED_MESSAGE_BY_THREAD_ID |
查询指定threadId的会话中的被锁定的消息,仅输出1条,用于确定是否有被锁定的消息 |
六、TelephonyProvider
其实TelephonyProvider是一个工具类,封装了对数据库的操作,负责添加删除查询carriers 的信息。
com.android.providers.telephony.TelephonyProvider
com.android.providers.telephony.TelephonyProvider.delete(Uri, String, String[])
com.android.providers.telephony.TelephonyProvider.getType(Uri)
com.android.providers.telephony.TelephonyProvider.insert(Uri, ContentValues)
com.android.providers.telephony.TelephonyProvider.onCreate()
com.android.providers.telephony.TelephonyProvider.query(Uri, String[], String, String[], String)
com.android.providers.telephony.TelephonyProvider.update(Uri, ContentValues, String, String[])