SoFunction
Updated on 2025-04-10

Android Mms: Draft management application

After editing a message, if you exit the editing page without sending it, the message will be automatically saved as a draft. That is, when ComposeMessageActivity's onStop(), if it has not been sent, then () will be called to save the information as a draft. During this period, some conditions will also be checked, such as whether the message has been marked as abandoned, or whether it is empty (isWorthSaving). If everything is normal, saveDraft() and use Toast to inform the information has been saved as a draft.
The preservation of drafts is also different for different information, and the processes of text messages and multi-messages are different.
Save the text message as a draft
WorkingMessage will first take out the text message content, then open a new thread to do the next thing, and () will return it. In the thread, you will first make sure that the ThreadId is correct. If there is no correct ThreadId, it will not be saved. Then write the letter into the database and identify the Type as Draft. Finally, the MMS draft owned by this Thread will be deleted, because there can only be one draft in a Thread, so if there is a new SMS draft, then the old MMS draft must be deleted. Similarly, the SMS draft will also be deleted when saving the MMS draft later.
Save MMS as a draft
Similar to saving a text message, ComposeMessageActivity calls() when onStop; () will first refresh the recipient's information, then create a multi-message data structure SendReq, and then start the thread to do other things, saveDraft() will return. In the thread, first ensure that it is a legal Thread, that is, the threadid must be correct. At the same time, the Thread logo should also be marked with a draft. This is managed by a DraftCache. It is a HashMap to identify which Threads contain Draft. If this Thread has no attachments before, then create an attachment for it, that is, write SendReq to the database; on the contrary, if there is an attachment, then update the database and update SendReq and Slideshow and dates to the current information content. Finally, delete the existing text message draft.

The operations on MMS that you should note here are all completed by the classes and tools provided in the .* package in Frameworks. It provides the data structure of MMS supported by Android, puts data (Text, Medias, Files) into the SendReq method PduPart and PduBody, writes SendReq to the database and reads SendReq from the data—through PduPersister. The client's application just creates SendReq, writes data into SendReq using the provided method, uses PduPersister to write to the database and extract it from the database, and finally uses the HTTP protocol to send SendReq out.

There is also a special class DraftCache to manage which Threads contain drafts. Its internal is a HashMap that can identify which Therads contain drafts. Therefore, DraftCache is used in places where drafts are operated. If a Thread contains a draft, it needs to be marked as having a draft; if a Thread information has been sent, it needs to be marked as having a draft.

Traditionally, when managing information in a folder, there will be a folder specifically used to store drafts called Draft Box. Every time you edit the information, no matter who you send it to, you can put it in this draft box. But it can also be found here that unlike the traditional folder-based method, the drafts of Mms in Android are one for each Thread, and there is only one. In other words, it is impossible to store too many drafts. Because Mms in Android manages information in a conversation Thread, and a Thread should only have one word that has not been "finished" in a conversation, so this design is reasonable.