android短彩信幻灯片异步加载机制
记不清是android 4.0之后还是4.1之后,浏览信息时,彩信幻灯片不再随着信息内容一并显示,而是在信息内容显示后,开启后台线程,异步加载彩信幻灯片,加载完毕之后再显示附件。为什么要这么设计那?主要是为了解决彩信显示缓慢的问题。在原先的设计中,彩信想要显示,首先要做准备工作,准备工作包括从数据库中加载信息的内容,收件人,发送时间,主题,类型,状态报告等基础内容,其中还包括了一项费时的操作,那就是加载彩信幻灯片附件。只有上述工作全部完成之后彩信才会显示在界面上,用户才可以进行浏览。这种设计非常容易卡机,有时还会引起ANR(应用程序无相应),尤其当我们的运营商要求彩信幻灯片支持20页(默认10页),这个问题就更加严重,长时间不显示信息,严重影响性能。其实从功能设计上来说,用户首先希望看到的是彩信收件人,彩信主题,发送时间,发送状态报告等基础信息,然后才会选择查看幻灯片等多媒体附件。我们完全可以将幻灯片的加载滞后。androd目前给出了这样的一个设计。
首先根据当前回话,查询数据库,加载信息条目。每一个信息条目,每一条信息条目,用视图MessageListItem来显示,信息内容数据包装在MessageItem中。
MessageListAdapter.java
public GenericPdu load(Uri uri) throws MmsException { GenericPdu pdu = null; PduCacheEntry cacheEntry = null; int msgBox = 0; long threadId = -1; try { synchronized(PDU_CACHE_INSTANCE) { if (PDU_CACHE_INSTANCE.isUpdating(uri)) { if (LOCAL_LOGV) { Log.v(TAG, "load: " + uri + " blocked by isUpdating()"); } try { PDU_CACHE_INSTANCE.wait(); } catch (InterruptedException e) { Log.e(TAG, "load: ", e); } cacheEntry = PDU_CACHE_INSTANCE.get(uri); if (cacheEntry != null) { return cacheEntry.getPdu(); } } // Tell the cache to indicate to other callers that this item // is currently being updated. PDU_CACHE_INSTANCE.setUpdating(uri, true); } Cursor c = SqliteWrapper.query(mContext, mContentResolver, uri,//加载彩信数据库中的内容 PDU_PROJECTION, null, null, null); PduHeaders headers = new PduHeaders(); Set<Entry<Integer, Integer>> set; long msgId = ContentUris.parseId(uri); try { if ((c == null) || (c.getCount() != 1) || !c.moveToFirst()) { throw new MmsException("Bad uri: " + uri); } msgBox = c.getInt(PDU_COLUMN_MESSAGE_BOX); threadId = c.getLong(PDU_COLUMN_THREAD_ID); set = ENCODED_STRING_COLUMN_INDEX_MAP.entrySet();//解析数据库中的内容到PduHeaders中。 for (Entry<Integer, Integer> e : set) { setEncodedStringValueToHeaders( c, e.getValue(), headers, e.getKey()); } set = TEXT_STRING_COLUMN_INDEX_MAP.entrySet(); for (Entry<Integer, Integer> e : set) { setTextStringToHeaders( c, e.getValue(), headers, e.getKey()); } set = OCTET_COLUMN_INDEX_MAP.entrySet(); for (Entry<Integer, Integer> e : set) { setOctetToHeaders( c, e.getValue(), headers, e.getKey()); } set = LONG_COLUMN_INDEX_MAP.entrySet(); for (Entry<Integer, Integer> e : set) { setLongToHeaders( c, e.getValue(), headers, e.getKey()); } } finally { if (c != null) { c.close(); } } // Check whether 'msgId' has been assigned a valid value. if (msgId == -1L) { throw new MmsException("Error! ID of the message: -1."); } // Load address information of the MM. loadAddress(msgId, headers); int msgType = headers.getOctet(PduHeaders.MESSAGE_TYPE); PduBody body = new PduBody(); // For PDU which type is M_retrieve.conf or Send.req, we should // load multiparts and put them into the body of the PDU. if ((msgType == PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF) || (msgType == PduHeaders.MESSAGE_TYPE_SEND_REQ)) {//构建pduBody PduPart[] parts = loadParts(msgId);//这部分相信大家可以看懂,同样是加载数据库 if (parts != null) { int partsNum = parts.length; for (int i = 0; i < partsNum; i++) { body.addPart(parts[i]); } } } switch (msgType) {//根据msgType,构建GeniricPdu的实例子类型 case PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND: pdu = new NotificationInd(headers); break; case PduHeaders.MESSAGE_TYPE_DELIVERY_IND: pdu = new DeliveryInd(headers); break; case PduHeaders.MESSAGE_TYPE_READ_ORIG_IND: pdu = new ReadOrigInd(headers); break; case PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF: pdu = new RetrieveConf(headers, body); break; case PduHeaders.MESSAGE_TYPE_SEND_REQ: pdu = new SendReq(headers, body); break; case PduHeaders.MESSAGE_TYPE_ACKNOWLEDGE_IND: pdu = new AcknowledgeInd(headers); break; case PduHeaders.MESSAGE_TYPE_NOTIFYRESP_IND: pdu = new NotifyRespInd(headers); break; case PduHeaders.MESSAGE_TYPE_READ_REC_IND: pdu = new ReadRecInd(headers); break; case PduHeaders.MESSAGE_TYPE_SEND_CONF: case PduHeaders.MESSAGE_TYPE_FORWARD_REQ: case PduHeaders.MESSAGE_TYPE_FORWARD_CONF: case PduHeaders.MESSAGE_TYPE_MBOX_STORE_REQ: case PduHeaders.MESSAGE_TYPE_MBOX_STORE_CONF: case PduHeaders.MESSAGE_TYPE_MBOX_VIEW_REQ: case PduHeaders.MESSAGE_TYPE_MBOX_VIEW_CONF: case PduHeaders.MESSAGE_TYPE_MBOX_UPLOAD_REQ: case PduHeaders.MESSAGE_TYPE_MBOX_UPLOAD_CONF: case PduHeaders.MESSAGE_TYPE_MBOX_DELETE_REQ: case PduHeaders.MESSAGE_TYPE_MBOX_DELETE_CONF: case PduHeaders.MESSAGE_TYPE_MBOX_DESCR: case PduHeaders.MESSAGE_TYPE_DELETE_REQ: case PduHeaders.MESSAGE_TYPE_DELETE_CONF: case PduHeaders.MESSAGE_TYPE_CANCEL_REQ: case PduHeaders.MESSAGE_TYPE_CANCEL_CONF: throw new MmsException( "Unsupported PDU type: " + Integer.toHexString(msgType)); default: throw new MmsException( "Unrecognized PDU type: " + Integer.toHexString(msgType)); } } finally { synchronized(PDU_CACHE_INSTANCE) { if (pdu != null) { assert(PDU_CACHE_INSTANCE.get(uri) == null); // Update the cache entry with the real info cacheEntry = new PduCacheEntry(pdu, msgBox, threadId); PDU_CACHE_INSTANCE.put(uri, cacheEntry); } PDU_CACHE_INSTANCE.setUpdating(uri, false); PDU_CACHE_INSTANCE.notifyAll(); // tell anybody waiting on this entry to go ahead } } return pdu; }