Android providers的源代碼結構如下:
android-2.2-froyo/com/android/providers:
本篇來解析一下telephony這個provider中的代碼,首先來看一下它的目錄結構:
android-2.2-froyo/com/android/providers/telephony:
- MmsProvider.java
- MmsSmsDatabaseHelper.java
- MmsSmsProvider.java
- SmsProvider.java
- TelephonyProvider.java
- Telephony.java (隱藏類,在Android 的source源代碼中可以找到,如:sources\android-14\android\provider\Telephony.java )
以上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[])