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