1 /*
2  * Copyright (C) 2006 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 
17 package com.android.internal.telephony.gsm;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.telephony.PhoneNumberUtils;
21 
22 import com.android.internal.telephony.GsmAlphabet;
23 import com.android.internal.telephony.SmsAddress;
24 
25 import java.text.ParseException;
26 
27 public class GsmSmsAddress extends SmsAddress {
28 
29     static final int OFFSET_ADDRESS_LENGTH = 0;
30 
31     static final int OFFSET_TOA = 1;
32 
33     static final int OFFSET_ADDRESS_VALUE = 2;
34 
35     /**
36      * New GsmSmsAddress from TS 23.040 9.1.2.5 Address Field
37      *
38      * @param offset the offset of the Address-Length byte
39      * @param length the length in bytes rounded up, e.g. "2 +
40      *        (addressLength + 1) / 2"
41      * @throws ParseException
42      */
43     @UnsupportedAppUsage
GsmSmsAddress(byte[] data, int offset, int length)44     public GsmSmsAddress(byte[] data, int offset, int length) throws ParseException {
45         origBytes = new byte[length];
46         System.arraycopy(data, offset, origBytes, 0, length);
47 
48         // addressLength is the count of semi-octets, not bytes
49         int addressLength = origBytes[OFFSET_ADDRESS_LENGTH] & 0xff;
50 
51         int toa = origBytes[OFFSET_TOA] & 0xff;
52         ton = 0x7 & (toa >> 4);
53 
54         // TOA must have its high bit set
55         if ((toa & 0x80) != 0x80) {
56             throw new ParseException("Invalid TOA - high bit must be set. toa = " + toa,
57                     offset + OFFSET_TOA);
58         }
59 
60         if (isAlphanumeric()) {
61             // An alphanumeric address
62             int countSeptets = addressLength * 4 / 7;
63 
64             address = GsmAlphabet.gsm7BitPackedToString(origBytes,
65                     OFFSET_ADDRESS_VALUE, countSeptets);
66         } else {
67             // TS 23.040 9.1.2.5 says
68             // that "the MS shall interpret reserved values as 'Unknown'
69             // but shall store them exactly as received"
70 
71             byte lastByte = origBytes[length - 1];
72 
73             if ((addressLength & 1) == 1) {
74                 // Make sure the final unused BCD digit is 0xf
75                 origBytes[length - 1] |= 0xf0;
76             }
77             address = PhoneNumberUtils.calledPartyBCDToString(
78                     origBytes,
79                     OFFSET_TOA,
80                     length - OFFSET_TOA,
81                     PhoneNumberUtils.BCD_EXTENDED_TYPE_CALLED_PARTY);
82 
83             // And restore origBytes
84             origBytes[length - 1] = lastByte;
85         }
86     }
87 
88     @Override
getAddressString()89     public String getAddressString() {
90         return address;
91     }
92 
93     /**
94      * Returns true if this is an alphanumeric address
95      */
96     @Override
isAlphanumeric()97     public boolean isAlphanumeric() {
98         return ton == TON_ALPHANUMERIC;
99     }
100 
101     @Override
isNetworkSpecific()102     public boolean isNetworkSpecific() {
103         return ton == TON_NETWORK;
104     }
105 
106     /**
107      * Returns true of this is a valid CPHS voice message waiting indicator
108      * address
109      */
isCphsVoiceMessageIndicatorAddress()110     public boolean isCphsVoiceMessageIndicatorAddress() {
111         // CPHS-style MWI message
112         // See CPHS 4.7 B.4.2.1
113         //
114         // Basically:
115         //
116         // - Originating address should be 4 bytes long and alphanumeric
117         // - Decode will result with two chars:
118         // - Char 1
119         // 76543210
120         // ^ set/clear indicator (0 = clear)
121         // ^^^ type of indicator (000 = voice)
122         // ^^^^ must be equal to 0001
123         // - Char 2:
124         // 76543210
125         // ^ line number (0 = line 1)
126         // ^^^^^^^ set to 0
127         //
128         // Remember, since the alpha address is stored in 7-bit compact form,
129         // the "line number" is really the top bit of the first address value
130         // byte
131 
132         return (origBytes[OFFSET_ADDRESS_LENGTH] & 0xff) == 4
133                 && isAlphanumeric() && (origBytes[OFFSET_TOA] & 0x0f) == 0;
134     }
135 
136     /**
137      * Returns true if this is a valid CPHS voice message waiting indicator
138      * address indicating a "set" of "indicator 1" of type "voice message
139      * waiting"
140      */
141     @UnsupportedAppUsage
isCphsVoiceMessageSet()142     public boolean isCphsVoiceMessageSet() {
143         // 0x11 means "set" "voice message waiting" "indicator 1"
144         return isCphsVoiceMessageIndicatorAddress()
145                 && (origBytes[OFFSET_ADDRESS_VALUE] & 0xff) == 0x11;
146 
147     }
148 
149     /**
150      * Returns true if this is a valid CPHS voice message waiting indicator
151      * address indicating a "clear" of "indicator 1" of type "voice message
152      * waiting"
153      */
154     @UnsupportedAppUsage
isCphsVoiceMessageClear()155     public boolean isCphsVoiceMessageClear() {
156         // 0x10 means "clear" "voice message waiting" "indicator 1"
157         return isCphsVoiceMessageIndicatorAddress()
158                 && (origBytes[OFFSET_ADDRESS_VALUE] & 0xff) == 0x10;
159 
160     }
161 }
162