下面我们来看一下构建Uri中的重点。
WorkingMessage.java
public Uri saveAsMms(boolean notify) {
if (DEBUG) LogTag.debug("saveAsMms mConversation=%s", mConversation);
// If we have discarded the message, just bail out.
if (mDiscarded) {
LogTag.warn("saveAsMms mDiscarded: true mConversation: " + mConversation +
" returning NULL uri and bailing");
return null;
}
// FORCE_MMS behaves as sort of an "invisible attachment", making
// the message seem non-empty (and thus not discarded). This bit
// is sticky until the last other MMS bit is removed, at which
// point the message will fall back to SMS.
updateState(FORCE_MMS, true, notify);
// Collect our state to be written to disk.
prepareForSave(true /* notify */);
try {
// Make sure we are saving to the correct thread ID.
DraftCache.getInstance().setSavingDraft(true);
if (!mConversation.getRecipients().isEmpty()) {
mConversation.ensureThreadId();
}
mConversation.setDraftState(true);
PduPersister persister = PduPersister.getPduPersister(mActivity);
SendReq sendReq = makeSendReq(mConversation, mSubject);
// If we don't already have a Uri lying around, make a new one. If we do
// have one already, make sure it is synced to disk.
if (mMessageUri == null) {
mMessageUri = createDraftMmsMessage(persister, sendReq, mSlideshow, null);
} else {
updateDraftMmsMessage(mMessageUri, persister, mSlideshow, sendReq);
}
mHasMmsDraft = true;
} finally {
DraftCache.getInstance().setSavingDraft(false);
}
return mMessageUri;
}
段代码会在多种情况下调用,故而不好分析它的具体作用。代码中先判断mDiscarded是否为true,这个值在我们删除会话,舍弃信息,退出Activity等情况下会为true,我们添加幻灯片附件,因而此处为false。
我们首先来看看updateState(FORCE_MMS,true, notify);
private void updateState(int state, boolean on, boolean notify) {
if (!sMmsEnabled) {
// If Mms isn't enabled, the rest of the Messaging UI should not be using any
// feature that would cause us to to turn on any Mms flag and show the
// "Converting to multimedia..." message.
return;
}
int oldState = mMmsState;
if (on) {
mMmsState |= state;
} else {
mMmsState &= ~state;
}
// If we are clearing the last bit that is not FORCE_MMS,
// expire the FORCE_MMS bit.
if (mMmsState == FORCE_MMS && ((oldState & ~FORCE_MMS) > 0)) {
mMmsState = 0;
}
// Notify the listener if we are moving from SMS to MMS
// or vice versa.
if (notify) {
if (oldState == 0 && mMmsState != 0) {
mStatusListener.onProtocolChanged(true);
} else if (oldState != 0 && mMmsState == 0) {
mStatusListener.onProtocolChanged(false);
}
}
if (oldState != mMmsState) {
if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) LogTag.debug("updateState: %s%s = %s",
on ? "+" : "-",
stateString(state), stateString(mMmsState));
}
}
