View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
35  package org.marre.sms;
36  
37  import java.util.Iterator;
38  import java.util.LinkedList;
39  
40  /***
41   * Represents a "Message Waiting" sms.
42   * 
43   * As described in TS 23.040-650 section 9.2.3.24.2 "Special SMS Message Indication".
44   * 
45   * On a Sony-Ericsson T610 these messages can be used to display different types of icons in the
46   * notification bar.
47   *  
48   * @author Markus Eriksson
49   * @version $Id: SmsMsgWaitingMessage.java,v 1.2 2005/05/12 07:50:16 c95men Exp $
50   */
51  public class SmsMsgWaitingMessage extends SmsTextMessage
52  {
53      /*** Message waiting type : VOICE */
54      public static final int TYPE_VOICE = 0;
55      /*** Message waiting type : FAX */
56      public static final int TYPE_FAX = 1;
57      /*** Message waiting type : EMAIL */
58      public static final int TYPE_EMAIL = 2;
59      /*** Message waiting type : VIDEO */
60      public static final int TYPE_VIDEO = 3;
61      
62      private static final int OPT_PROFILE_MASK = 0x03;
63      /*** Profile ID 1. (Default) */
64      public static final int OPT_PROFILE_ID_1 = 0x00;
65      /*** Profile ID 2. */
66      public static final int OPT_PROFILE_ID_2 = 0x01;
67      /*** Profile ID 3. */
68      public static final int OPT_PROFILE_ID_3 = 0x02;
69      /*** Profile ID 4. */
70      public static final int OPT_PROFILE_ID_4 = 0x03;
71      
72      /*** Store message in the phone memory. */
73      public static final int OPT_STORE_MSG = 0x04;    
74      
75      /***
76       * Represents one message waiting udh.
77       */
78      private class MsgWaiting
79      {
80          int type;
81          int count;
82          int options;
83          
84          private MsgWaiting(int type, int count, int options)
85          {
86              this.type = type;
87              this.count = count;
88              this.options = options;
89          }
90      }
91      
92      /***
93       * List of MsgWaiting "objects".
94       */
95      protected LinkedList messages_ = new LinkedList();
96      
97      /***
98       * Creates an empty message.
99       */
100     public SmsMsgWaitingMessage()
101     {
102         this("", SmsDcs.ALPHABET_8BIT);
103     }
104     
105     /***
106      * Creates an message with the supplied text (GSM charset).
107      * 
108      * @param text Description of this message.
109      */
110     public SmsMsgWaitingMessage(String text)
111     {
112         this(text, SmsDcs.ALPHABET_GSM);
113     }
114     
115     /***
116      * Creates an message with the supplied text and alphabet.
117      * 
118      * @param text Description of this message
119      * @param alphabet Alphabet to use. Valid values are SmsDcs.ALPHABET_*.
120      */
121     public SmsMsgWaitingMessage(String text, int alphabet)
122     {
123         super(text, SmsDcs.getGeneralDataCodingDcs(alphabet, SmsDcs.MSG_CLASS_UNKNOWN));
124     }
125     
126     /***
127      * Adds a message waiting.
128      * 
129      * @param type Type of message that is waiting. Can be any of TYPE_*.
130      * @param count Number of messages waiting for retrieval.
131      */
132     public void addMsgWaiting(int type, int count)
133     {
134         addMsgWaiting(type, count, 0);
135     }
136     
137     /***
138      * Adds a message waiting.
139      * 
140      * @param type Type of message that is waiting. Can be any of TYPE_*.
141      * @param count Number of messages waiting for retrieval.
142      * @param options Bitfield of OPT_ options.
143      */
144     public void addMsgWaiting(int type, int count, int options)
145     {
146         // Check input parameters
147         switch (type)
148         {
149         case TYPE_VOICE:
150         case TYPE_FAX:
151         case TYPE_EMAIL:
152         case TYPE_VIDEO:
153             // Valid values
154             break;
155             
156         default:
157             throw new IllegalArgumentException("Invalid type.");
158         }
159         
160         // count can be at most 255.
161         if (count > 255)
162         {
163             count = 255;
164         }
165         
166         messages_.add(new MsgWaiting(type, count, options));
167     }
168 
169     /***
170      * Creates a "Message waiting" UDH element using UDH_IEI_SPECIAL_MESSAGE.
171      * <p>
172      * If more than one type of message is required to be indicated within
173      * one SMS message, then multiple "Message waiting" UDH elements must
174      * be used.
175      * <p>
176      * <b>Special handling in concatenated messages:</b><br>
177      * <i>
178      * "In the case where this IEI is to be used in a concatenated SM then the
179      * IEI, its associated IEI length and IEI data shall be contained in the
180      * first segment of the concatenated SM. The IEI, its associated IEI length
181      * and IEI data should also be contained in every subsequent segment of the
182      * concatenated SM although this is not mandatory. However, in the case
183      * where these elements are not contained in every subsequent segment of
184      * the concatenated SM and where an out of sequence segment delivery
185      * occurs or where the first segment is not delivered then processing
186      * difficulties may arise at the receiving entity which may result in
187      * the concatenated SM being totally or partially discarded."
188      * </i>
189      *
190      * @param msgWaiting The MsgWaiting to convert
191      * @return A SmsUdhElement
192      */
193     protected SmsUdhElement getMessageWaitingUdh(MsgWaiting msgWaiting)
194     {
195         byte[] udh = new byte[2];
196 
197         // Bit 0 and 1 indicate the basic indication type.
198         // Bit 4, 3 and 2 indicate the extended message indication type.
199         switch (msgWaiting.type)
200         {
201         case TYPE_VOICE: udh[0] = 0x00; break;
202         case TYPE_FAX:   udh[0] = 0x01; break;
203         case TYPE_EMAIL: udh[0] = 0x02; break;
204         case TYPE_VIDEO: udh[0] = 0x07; break;
205 
206         default:
207             throw new RuntimeException("Invalid message type.");
208         }
209         
210         // Bit 6 and 5 indicates the profile ID of the Multiple Subscriber Profile.
211         switch (msgWaiting.options & OPT_PROFILE_MASK)
212         {
213         case OPT_PROFILE_ID_1: udh[0] |= 0x00; break;
214         case OPT_PROFILE_ID_2: udh[0] |= 0x20; break;
215         case OPT_PROFILE_ID_3: udh[0] |= 0x40; break;
216         case OPT_PROFILE_ID_4: udh[0] |= 0x60; break;
217             
218         default:
219             throw new RuntimeException("Invalid option.");
220         }
221         
222         // Bit 7 indicates if the message shall be stored.
223         if ((msgWaiting.options & OPT_STORE_MSG) != 0)
224         {
225             udh[0] |= (byte) (0x80);
226         }
227 
228         // Octet 2 contains the number of messages waiting
229         udh[1] = (byte) (msgWaiting.count & 0xff);
230 
231         return new SmsUdhElement(SmsConstants.UDH_IEI_SPECIAL_MESSAGE, udh);
232     }
233     
234     /***
235      * Builds a udh element for this message.
236      * 
237      * @see org.marre.sms.SmsTextMessage#getUdhElements()
238      */
239     public SmsUdhElement[] getUdhElements()
240     {
241         SmsUdhElement udhElements[] = null;
242         int msgCount = messages_.size();
243         
244         if (msgCount > 0)
245         {
246             udhElements = new SmsUdhElement[messages_.size()];
247             int i = 0;
248          
249             for(Iterator j = messages_.iterator(); j.hasNext(); i++)
250             {
251                 MsgWaiting msgWaiting = (MsgWaiting) j.next();
252                 udhElements[i] = getMessageWaitingUdh(msgWaiting);
253             }
254             
255         }
256         
257         return udhElements;
258     }
259 }