Android 4.4 Email 点击视频附件查看的简单流程
4.4中附件的view是在 MessageAttachmentBar 中进行显示的。
1.当点击附件的时候调用onClick方法。
if (res == R.id.preview_attachment) {
previewAttachment();
} else if (res == R.id.save_attachment) {//点击保存
if (mAttachment.canSave()) {//判断是否可以保存,判断是不是已经下载
mActionHandler.startDownloadingAttachment(AttachmentDestination.EXTERNAL);//开始下载,下载到外部,
mSaveClicked = true;
Analytics.getInstance().sendEvent(
"save_attachment", Utils.normalizeMimeType(mAttachment.getContentType()),
"attachment_bar", mAttachment.size);
}
} else if (res == R.id.download_again) {//再次下载
if (mAttachment.isPresentLocally()) {//判断是不是该附件已经保存
mActionHandler.showDownloadingDialog();
mActionHandler.startRedownloadingAttachment(mAttachment);
Analytics.getInstance().sendEvent("redownload_attachment",
Utils.normalizeMimeType(mAttachment.getContentType()), "attachment_bar",
mAttachment.size);
}
} else if (res == R.id.cancel_attachment) {//取消下载
mActionHandler.cancelAttachment();
mSaveClicked = false;
Analytics.getInstance().sendEvent(
"cancel_attachment", Utils.normalizeMimeType(mAttachment.getContentType()),
"attachment_bar", mAttachment.size);
。。。。。
} else {
。。。。
// view or play.
else if (MimeType.isViewable(
getContext(), mAttachment.contentUri, mAttachment.getContentType())) {//此附件开始查看
mActionHandler.showAttachment(AttachmentDestination.CACHE);
action = "attachment_bar";
}
。。。。
if (action != null) {
Analytics.getInstance()
.sendEvent("view_attachment", mime, action, mAttachment.size);
}
}
return true;
2.附件可以下载的时候调用
mActionHandler.showAttachment(AttachmentDestination.CACHE);
name="code" class="html">AttachmentDestination.CACHE:表示下载到内部存储。
mActionHander 是AttachmentActionHandler
public void showAttachment(int destination) {
if (mView == null) {
return;
}
if (mAttachment.isPresentLocally() &&
(destination == AttachmentDestination.CACHE ||
mAttachment.destination == destination)) {//判断此附件是不是已经下载,若已经下载可以直接就行查看。
mView.viewAttachment();
} else {
showDownloadingDialog();//显示下载的对话框
startDownloadingAttachment(destination);//开始下载附件,更新数据库
}
}
3.查看附件调用了startDownloadingAttachment,实现如下:
private void startDownloadingAttachment(
Attachment attachment, int destination, int rendition, int additionalPriority,
boolean delayDownload) {
final ContentValues params = new ContentValues(5);
params.put(AttachmentColumns.STATE, AttachmentState.DOWNLOADING);//更新状态
params.put(AttachmentColumns.DESTINATION, destination);//下载的方向
params.put(AttachmentContentValueKeys.RENDITION, rendition);
params.put(AttachmentContentValueKeys.ADDITIONAL_PRIORITY, additionalPriority);
params.put(AttachmentContentValueKeys.DELAY_DOWNLOAD, delayDownload);//是否延迟下载
mCommandHandler.sendCommand(attachment.uri, params);
}
4.mCommandHandler.sendCommand(attachment.uri, params); 是一个异步更新数据库的操作,最后将更新数据库。调用了EmailProvider。
5.EmailProvider中对于Attachment数据库进行更新操作之后,将会触发对于Attachment表格的监听。
private final AttachmentService DEFAULT_ATTACHMENT_SERVICE = new AttachmentService() {
@Override
public void attachmentChanged(Context context, long id, int flags) {
// The default implementation delegates to the real service.
AttachmentDownloadService.attachmentChanged(context, id, flags);//启动下载的服务
}
};
6.AttachmentDownloadService 是对于邮件附件下载的service。