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