FileDocCategorySizeDatePackage
SmsAssembler.javaAPI DocAndroid 1.5 API5207Wed May 06 22:42:46 BST 2009com.android.im.imps

SmsAssembler

public class SmsAssembler extends Object implements com.android.im.engine.SmsService.SmsListener

Fields Summary
private static final Pattern
sPreamplePattern
private com.android.im.engine.SmsService.SmsListener
mListener
private HashMap
mPtsCache
Constructors Summary
public SmsAssembler()


      
        mPtsCache = new HashMap<String, RawPtsData>();
    
Methods Summary
private java.lang.StringextractPreamble(byte[] data)

        int N = data.length;
        int preambleIndex = 0;
        while (data[preambleIndex] != ' " && preambleIndex < N) {
            preambleIndex++;
        }

        if (preambleIndex >= N) {
            return null;
        }

        try {
            return new String(data, 0, preambleIndex, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // impossible
            return null;
        }
    
private voidnotifyAssembledSms(byte[] data)

        if (mListener != null) {
            mListener.onIncomingSms(data);
        }
    
public voidonIncomingSms(byte[] data)

        String preamble = extractPreamble(data);
        if (preamble == null) {
            ImpsLog.logError("Received non PTS SMS");
            return;
        }

        Matcher m = sPreamplePattern.matcher(preamble);
        if (!m.matches()) {
            ImpsLog.logError("Received non PTS SMS");
            return;
        }
        String dd = m.group(4);
        if (dd == null || dd.length() == 0) {
            notifyAssembledSms(data);
        } else {
            int totalSegmentsCount = dd.charAt(1) - 'a" + 1;
            int index = dd.charAt(0) - 'a";
            if (index < 0 || index >= totalSegmentsCount) {
                ImpsLog.logError("Invalid multiple SMSes identifier");
                return;
            }

            String transId = m.group(3);
            RawPtsData pts = mPtsCache.get(transId);
            if (pts == null) {
                pts = new RawPtsData(preamble.length(), totalSegmentsCount);
                mPtsCache.put(transId, pts);
            }

            pts.setSegment(index, data);
            if (pts.isAllSegmentsReceived()) {
                mPtsCache.remove(transId);
                notifyAssembledSms(pts.assemble());
            }
        }
    
public voidsetSmsListener(com.android.im.engine.SmsService.SmsListener listener)

        mListener = listener;