SmsMessage.java revision c38bb60d867c5d61d90b7179a9ed2b2d1848124f
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.telephony.gsm;
18
19import android.os.Parcel;
20import android.telephony.TelephonyManager;
21
22import com.android.internal.telephony.GsmAlphabet;
23import com.android.internal.telephony.EncodeException;
24import com.android.internal.telephony.SmsHeader;
25import com.android.internal.telephony.SmsMessageBase;
26import com.android.internal.telephony.SmsMessageBase.SubmitPduBase;
27
28import java.util.Arrays;
29
30import static android.telephony.TelephonyManager.PHONE_TYPE_CDMA;
31
32
33/**
34 * A Short Message Service message.
35 * @deprecated Replaced by android.telephony.SmsMessage that supports both GSM and CDMA.
36 */
37@Deprecated
38public class SmsMessage {
39    private static final boolean LOCAL_DEBUG = true;
40    private static final String LOG_TAG = "SMS";
41
42    /**
43     * SMS Class enumeration.
44     * See TS 23.038.
45     * @deprecated Use android.telephony.SmsMessage.
46     */
47    @Deprecated
48    public enum MessageClass{
49        UNKNOWN, CLASS_0, CLASS_1, CLASS_2, CLASS_3;
50    }
51
52    /** Unknown encoding scheme (see TS 23.038)
53     * @deprecated Use android.telephony.SmsMessage.
54     */
55    @Deprecated public static final int ENCODING_UNKNOWN = 0;
56
57    /** 7-bit encoding scheme (see TS 23.038)
58     * @deprecated Use android.telephony.SmsMessage.
59     */
60    @Deprecated public static final int ENCODING_7BIT = 1;
61
62    /** 8-bit encoding scheme (see TS 23.038)
63     * @deprecated Use android.telephony.SmsMessage.
64     */
65    @Deprecated public static final int ENCODING_8BIT = 2;
66
67    /** 16-bit encoding scheme (see TS 23.038)
68     * @deprecated Use android.telephony.SmsMessage.
69     */
70    @Deprecated public static final int ENCODING_16BIT = 3;
71
72    /** The maximum number of payload bytes per message
73     * @deprecated Use android.telephony.SmsMessage.
74     */
75    @Deprecated public static final int MAX_USER_DATA_BYTES = 140;
76
77    /**
78     * The maximum number of payload bytes per message if a user data header
79     * is present.  This assumes the header only contains the
80     * CONCATENATED_8_BIT_REFERENCE element.
81     *
82     * @deprecated Use android.telephony.SmsMessage.
83     * @hide pending API Council approval to extend the public API
84     */
85    @Deprecated public static final int MAX_USER_DATA_BYTES_WITH_HEADER = 134;
86
87    /** The maximum number of payload septets per message
88     * @deprecated Use android.telephony.SmsMessage.
89     */
90    @Deprecated public static final int MAX_USER_DATA_SEPTETS = 160;
91
92    /**
93     * The maximum number of payload septets per message if a user data header
94     * is present.  This assumes the header only contains the
95     * CONCATENATED_8_BIT_REFERENCE element.
96     * @deprecated Use android.telephony.SmsMessage.
97     */
98    @Deprecated public static final int MAX_USER_DATA_SEPTETS_WITH_HEADER = 153;
99
100    /** Contains actual SmsMessage. Only public for debugging and for framework layer.
101     * @deprecated Use android.telephony.SmsMessage.
102     * {@hide}
103     */
104    @Deprecated public SmsMessageBase mWrappedSmsMessage;
105
106    /** @deprecated Use android.telephony.SmsMessage. */
107    @Deprecated
108    public static class SubmitPdu {
109        /** @deprecated Use android.telephony.SmsMessage. */
110        @Deprecated public byte[] encodedScAddress; // Null if not applicable.
111        /** @deprecated Use android.telephony.SmsMessage. */
112        @Deprecated public byte[] encodedMessage;
113
114        //Constructor
115        /** @deprecated Use android.telephony.SmsMessage. */
116        @Deprecated
117        public SubmitPdu() {
118        }
119
120        /** @deprecated Use android.telephony.SmsMessage.
121         * {@hide}
122         */
123        @Deprecated
124        protected SubmitPdu(SubmitPduBase spb) {
125            this.encodedMessage = spb.encodedMessage;
126            this.encodedScAddress = spb.encodedScAddress;
127        }
128
129        /** @deprecated Use android.telephony.SmsMessage. */
130        @Deprecated
131        public String toString() {
132            return "SubmitPdu: encodedScAddress = "
133                    + Arrays.toString(encodedScAddress)
134                    + ", encodedMessage = "
135                    + Arrays.toString(encodedMessage);
136        }
137    }
138
139    // Constructor
140    /** @deprecated Use android.telephony.SmsMessage. */
141    @Deprecated
142    public SmsMessage() {
143        this(getSmsFacility());
144    }
145
146    private SmsMessage(SmsMessageBase smb) {
147        mWrappedSmsMessage = smb;
148    }
149
150    /**
151     * Create an SmsMessage from a raw PDU.
152     * @deprecated Use android.telephony.SmsMessage.
153     */
154    @Deprecated
155    public static SmsMessage createFromPdu(byte[] pdu) {
156        SmsMessageBase wrappedMessage;
157        int activePhone = TelephonyManager.getDefault().getCurrentPhoneType();
158
159        if (PHONE_TYPE_CDMA == activePhone) {
160            wrappedMessage = com.android.internal.telephony.cdma.SmsMessage.createFromPdu(pdu);
161        } else {
162            wrappedMessage = com.android.internal.telephony.gsm.SmsMessage.createFromPdu(pdu);
163        }
164
165        return new SmsMessage(wrappedMessage);
166    }
167
168    /**
169     * Get the TP-Layer-Length for the given SMS-SUBMIT PDU Basically, the
170     * length in bytes (not hex chars) less the SMSC header
171     * @deprecated Use android.telephony.SmsMessage.
172     */
173    @Deprecated
174    public static int getTPLayerLengthForPDU(String pdu) {
175        int activePhone = TelephonyManager.getDefault().getCurrentPhoneType();
176
177        if (PHONE_TYPE_CDMA == activePhone) {
178            return com.android.internal.telephony.cdma.SmsMessage.getTPLayerLengthForPDU(pdu);
179        } else {
180            return com.android.internal.telephony.gsm.SmsMessage.getTPLayerLengthForPDU(pdu);
181        }
182    }
183
184    /**
185     * Calculates the number of SMS's required to encode the message body and
186     * the number of characters remaining until the next message, given the
187     * current encoding.
188     *
189     * @param messageBody the message to encode
190     * @param use7bitOnly if true, characters that are not part of the GSM
191     *         alphabet are counted as a single space char.  If false, a
192     *         messageBody containing non-GSM alphabet characters is calculated
193     *         for 16-bit encoding.
194     * @return an int[4] with int[0] being the number of SMS's required, int[1]
195     *         the number of code units used, and int[2] is the number of code
196     *         units remaining until the next message. int[3] is the encoding
197     *         type that should be used for the message.
198     * @deprecated Use android.telephony.SmsMessage.
199     */
200    @Deprecated
201    public static int[] calculateLength(CharSequence messageBody, boolean use7bitOnly) {
202        GsmAlphabet.TextEncodingDetails ted =
203                com.android.internal.telephony.gsm.SmsMessage
204                        .calculateLength(messageBody, use7bitOnly);
205        int ret[] = new int[4];
206        ret[0] = ted.msgCount;
207        ret[1] = ted.codeUnitCount;
208        ret[2] = ted.codeUnitsRemaining;
209        ret[3] = ted.codeUnitSize;
210        return ret;
211    }
212
213    /**
214     * Calculates the number of SMS's required to encode the message body and
215     * the number of characters remaining until the next message, given the
216     * current encoding.
217     *
218     * @param messageBody the message to encode
219     * @param use7bitOnly if true, characters that are not part of the GSM
220     *         alphabet are counted as a single space char.  If false, a
221     *         messageBody containing non-GSM alphabet characters is calculated
222     *         for 16-bit encoding.
223     * @return an int[4] with int[0] being the number of SMS's required, int[1]
224     *         the number of code units used, and int[2] is the number of code
225     *         units remaining until the next message. int[3] is the encoding
226     *         type that should be used for the message.
227     * @deprecated Use android.telephony.SmsMessage.
228     */
229    @Deprecated
230    public static int[] calculateLength(String messageBody, boolean use7bitOnly) {
231        return calculateLength((CharSequence)messageBody, use7bitOnly);
232    }
233
234    /**
235     * Get an SMS-SUBMIT PDU for a destination address and a message
236     *
237     * @param scAddress Service Centre address.  Null means use default.
238     * @return a <code>SubmitPdu</code> containing the encoded SC
239     *         address, if applicable, and the encoded message.
240     *         Returns null on encode error.
241     * @deprecated Use android.telephony.SmsMessage.
242     * @hide
243     */
244    @Deprecated
245    public static SubmitPdu getSubmitPdu(String scAddress,
246            String destinationAddress, String message,
247            boolean statusReportRequested, byte[] header) {
248        SubmitPduBase spb;
249        int activePhone = TelephonyManager.getDefault().getCurrentPhoneType();
250
251        if (PHONE_TYPE_CDMA == activePhone) {
252            spb = com.android.internal.telephony.cdma.SmsMessage.getSubmitPdu(scAddress,
253                    destinationAddress, message, statusReportRequested,
254                    SmsHeader.fromByteArray(header));
255        } else {
256            spb = com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(scAddress,
257                    destinationAddress, message, statusReportRequested, header);
258        }
259
260        return new SubmitPdu(spb);
261    }
262
263    /**
264     * Get an SMS-SUBMIT PDU for a destination address and a message
265     *
266     * @param scAddress Service Centre address.  Null means use default.
267     * @return a <code>SubmitPdu</code> containing the encoded SC
268     *         address, if applicable, and the encoded message.
269     *         Returns null on encode error.
270     * @deprecated Use android.telephony.SmsMessage.
271     */
272    @Deprecated
273    public static SubmitPdu getSubmitPdu(String scAddress,
274            String destinationAddress, String message, boolean statusReportRequested) {
275        SubmitPduBase spb;
276        int activePhone = TelephonyManager.getDefault().getCurrentPhoneType();
277
278        if (PHONE_TYPE_CDMA == activePhone) {
279            spb = com.android.internal.telephony.cdma.SmsMessage.getSubmitPdu(scAddress,
280                    destinationAddress, message, statusReportRequested, null);
281        } else {
282            spb = com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(scAddress,
283                    destinationAddress, message, statusReportRequested);
284        }
285
286        return new SubmitPdu(spb);
287    }
288
289    /**
290     * Get an SMS-SUBMIT PDU for a data message to a destination address &amp; port
291     *
292     * @param scAddress Service Centre address. null == use default
293     * @param destinationAddress the address of the destination for the message
294     * @param destinationPort the port to deliver the message to at the
295     *        destination
296     * @param data the dat for the message
297     * @return a <code>SubmitPdu</code> containing the encoded SC
298     *         address, if applicable, and the encoded message.
299     *         Returns null on encode error.
300     * @deprecated Use android.telephony.SmsMessage.
301     */
302    @Deprecated
303    public static SubmitPdu getSubmitPdu(String scAddress,
304            String destinationAddress, short destinationPort, byte[] data,
305            boolean statusReportRequested) {
306        SubmitPduBase spb;
307        int activePhone = TelephonyManager.getDefault().getCurrentPhoneType();
308
309        if (PHONE_TYPE_CDMA == activePhone) {
310            spb = com.android.internal.telephony.cdma.SmsMessage.getSubmitPdu(scAddress,
311                    destinationAddress, destinationPort, data, statusReportRequested);
312        } else {
313            spb = com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(scAddress,
314                    destinationAddress, destinationPort, data, statusReportRequested);
315        }
316
317        return new SubmitPdu(spb);
318    }
319
320    /**
321     * Returns the address of the SMS service center that relayed this message
322     * or null if there is none.
323     * @deprecated Use android.telephony.SmsMessage.
324     */
325    @Deprecated
326    public String getServiceCenterAddress() {
327        return mWrappedSmsMessage.getServiceCenterAddress();
328    }
329
330    /**
331     * Returns the originating address (sender) of this SMS message in String
332     * form or null if unavailable
333     * @deprecated Use android.telephony.SmsMessage.
334     */
335    @Deprecated
336    public String getOriginatingAddress() {
337        return mWrappedSmsMessage.getOriginatingAddress();
338    }
339
340    /**
341     * Returns the originating address, or email from address if this message
342     * was from an email gateway. Returns null if originating address
343     * unavailable.
344     * @deprecated Use android.telephony.SmsMessage.
345     */
346    @Deprecated
347    public String getDisplayOriginatingAddress() {
348        return mWrappedSmsMessage.getDisplayOriginatingAddress();
349    }
350
351    /**
352     * Returns the message body as a String, if it exists and is text based.
353     * @return message body is there is one, otherwise null
354     * @deprecated Use android.telephony.SmsMessage.
355     */
356    @Deprecated
357    public String getMessageBody() {
358        return mWrappedSmsMessage.getMessageBody();
359    }
360
361    /**
362     * Returns the class of this message.
363     * @deprecated Use android.telephony.SmsMessage.
364     */
365    @Deprecated
366    public MessageClass getMessageClass() {
367        int index = mWrappedSmsMessage.getMessageClass().ordinal();
368
369        return MessageClass.values()[index];
370    }
371
372    /**
373     * Returns the message body, or email message body if this message was from
374     * an email gateway. Returns null if message body unavailable.
375     * @deprecated Use android.telephony.SmsMessage.
376     */
377    @Deprecated
378    public String getDisplayMessageBody() {
379        return mWrappedSmsMessage.getDisplayMessageBody();
380    }
381
382    /**
383     * Unofficial convention of a subject line enclosed in parens empty string
384     * if not present
385     * @deprecated Use android.telephony.SmsMessage.
386     */
387    @Deprecated
388    public String getPseudoSubject() {
389        return mWrappedSmsMessage.getPseudoSubject();
390    }
391
392    /**
393     * Returns the service centre timestamp in currentTimeMillis() format
394     * @deprecated Use android.telephony.SmsMessage.
395     */
396    @Deprecated
397    public long getTimestampMillis() {
398        return mWrappedSmsMessage.getTimestampMillis();
399    }
400
401    /**
402     * Returns true if message is an email.
403     *
404     * @return true if this message came through an email gateway and email
405     *         sender / subject / parsed body are available
406     * @deprecated Use android.telephony.SmsMessage.
407     */
408    @Deprecated
409    public boolean isEmail() {
410        return mWrappedSmsMessage.isEmail();
411    }
412
413     /**
414     * @return if isEmail() is true, body of the email sent through the gateway.
415     *         null otherwise
416     * @deprecated Use android.telephony.SmsMessage.
417     */
418    @Deprecated
419    public String getEmailBody() {
420        return mWrappedSmsMessage.getEmailBody();
421    }
422
423    /**
424     * @return if isEmail() is true, email from address of email sent through
425     *         the gateway. null otherwise
426     * @deprecated Use android.telephony.SmsMessage.
427     */
428    @Deprecated
429    public String getEmailFrom() {
430        return mWrappedSmsMessage.getEmailFrom();
431    }
432
433    /**
434     * Get protocol identifier.
435     * @deprecated Use android.telephony.SmsMessage.
436     */
437    @Deprecated
438    public int getProtocolIdentifier() {
439        return mWrappedSmsMessage.getProtocolIdentifier();
440    }
441
442    /**
443     * See TS 23.040 9.2.3.9 returns true if this is a "replace short message" SMS
444     * @deprecated Use android.telephony.SmsMessage.
445     */
446    @Deprecated
447    public boolean isReplace() {
448        return mWrappedSmsMessage.isReplace();
449    }
450
451    /**
452     * Returns true for CPHS MWI toggle message.
453     *
454     * @return true if this is a CPHS MWI toggle message See CPHS 4.2 section B.4.2
455     * @deprecated Use android.telephony.SmsMessage.
456     */
457    @Deprecated
458    public boolean isCphsMwiMessage() {
459        return mWrappedSmsMessage.isCphsMwiMessage();
460    }
461
462    /**
463     * returns true if this message is a CPHS voicemail / message waiting
464     * indicator (MWI) clear message
465     * @deprecated Use android.telephony.SmsMessage.
466     */
467    @Deprecated
468    public boolean isMWIClearMessage() {
469        return mWrappedSmsMessage.isMWIClearMessage();
470    }
471
472    /**
473     * returns true if this message is a CPHS voicemail / message waiting
474     * indicator (MWI) set message
475     * @deprecated Use android.telephony.SmsMessage.
476     */
477    @Deprecated
478    public boolean isMWISetMessage() {
479        return mWrappedSmsMessage.isMWISetMessage();
480    }
481
482    /**
483     * returns true if this message is a "Message Waiting Indication Group:
484     * Discard Message" notification and should not be stored.
485     * @deprecated Use android.telephony.SmsMessage.
486     */
487    @Deprecated
488    public boolean isMwiDontStore() {
489        return mWrappedSmsMessage.isMwiDontStore();
490    }
491
492    /**
493     * returns the user data section minus the user data header if one was present.
494     * @deprecated Use android.telephony.SmsMessage.
495     */
496    @Deprecated
497    public byte[] getUserData() {
498        return mWrappedSmsMessage.getUserData();
499    }
500
501    /* Not part of the SDK interface and only needed by specific classes:
502       protected SmsHeader getUserDataHeader()
503    */
504
505    /**
506     * Returns the raw PDU for the message.
507     *
508     * @return the raw PDU for the message.
509     * @deprecated Use android.telephony.SmsMessage.
510     */
511    @Deprecated
512    public byte[] getPdu() {
513        return mWrappedSmsMessage.getPdu();
514    }
515
516    /**
517     * Returns the status of the message on the SIM (read, unread, sent, unsent).
518     *
519     * @return the status of the message on the SIM.  These are:
520     *         SmsManager.STATUS_ON_SIM_FREE
521     *         SmsManager.STATUS_ON_SIM_READ
522     *         SmsManager.STATUS_ON_SIM_UNREAD
523     *         SmsManager.STATUS_ON_SIM_SEND
524     *         SmsManager.STATUS_ON_SIM_UNSENT
525     * @deprecated Use android.telephony.SmsMessage and getStatusOnIcc instead.
526     */
527    @Deprecated
528    public int getStatusOnSim() {
529        return mWrappedSmsMessage.getStatusOnIcc();
530    }
531
532    /**
533     * Returns the status of the message on the ICC (read, unread, sent, unsent).
534     *
535     * @return the status of the message on the ICC.  These are:
536     *         SmsManager.STATUS_ON_ICC_FREE
537     *         SmsManager.STATUS_ON_ICC_READ
538     *         SmsManager.STATUS_ON_ICC_UNREAD
539     *         SmsManager.STATUS_ON_ICC_SEND
540     *         SmsManager.STATUS_ON_ICC_UNSENT
541     * @deprecated Use android.telephony.SmsMessage.
542     * @hide
543     */
544    @Deprecated
545    public int getStatusOnIcc() {
546
547        return mWrappedSmsMessage.getStatusOnIcc();
548    }
549
550    /**
551     * Returns the record index of the message on the SIM (1-based index).
552     * @return the record index of the message on the SIM, or -1 if this
553     *         SmsMessage was not created from a SIM SMS EF record.
554     * @deprecated Use android.telephony.SmsMessage and getIndexOnIcc instead.
555     */
556    @Deprecated
557    public int getIndexOnSim() {
558        return mWrappedSmsMessage.getIndexOnIcc();
559    }
560
561    /**
562     * Returns the record index of the message on the ICC (1-based index).
563     * @return the record index of the message on the ICC, or -1 if this
564     *         SmsMessage was not created from a ICC SMS EF record.
565     * @deprecated Use android.telephony.SmsMessage.
566     * @hide
567     */
568    @Deprecated
569    public int getIndexOnIcc() {
570
571        return mWrappedSmsMessage.getIndexOnIcc();
572    }
573
574    /**
575     * GSM:
576     * For an SMS-STATUS-REPORT message, this returns the status field from
577     * the status report.  This field indicates the status of a previously
578     * submitted SMS, if requested.  See TS 23.040, 9.2.3.15 TP-Status for a
579     * description of values.
580     * CDMA:
581     * For not interfering with status codes from GSM, the value is
582     * shifted to the bits 31-16.
583     * The value is composed of an error class (bits 25-24) and a status code (bits 23-16).
584     * Possible codes are described in C.S0015-B, v2.0, 4.5.21.
585     *
586     * @return 0 indicates the previously sent message was received.
587     *         See TS 23.040, 9.9.2.3.15 and C.S0015-B, v2.0, 4.5.21
588     *         for a description of other possible values.
589     * @deprecated Use android.telephony.SmsMessage.
590     */
591    @Deprecated
592    public int getStatus() {
593        return mWrappedSmsMessage.getStatus();
594    }
595
596    /**
597     * Return true iff the message is a SMS-STATUS-REPORT message.
598     * @deprecated Use android.telephony.SmsMessage.
599     */
600    @Deprecated
601    public boolean isStatusReportMessage() {
602        return mWrappedSmsMessage.isStatusReportMessage();
603    }
604
605    /**
606     * Returns true iff the <code>TP-Reply-Path</code> bit is set in
607     * this message.
608     * @deprecated Use android.telephony.SmsMessage.
609     */
610    @Deprecated
611    public boolean isReplyPathPresent() {
612        return mWrappedSmsMessage.isReplyPathPresent();
613    }
614
615    /** This method returns the reference to a specific
616     *  SmsMessage object, which is used for accessing its static methods.
617     * @return Specific SmsMessage.
618     * @deprecated Use android.telephony.SmsMessage.
619     */
620    private static final SmsMessageBase getSmsFacility(){
621        int activePhone = TelephonyManager.getDefault().getCurrentPhoneType();
622        if (PHONE_TYPE_CDMA == activePhone) {
623            return new com.android.internal.telephony.cdma.SmsMessage();
624        } else {
625            return new com.android.internal.telephony.gsm.SmsMessage();
626        }
627    }
628}
629