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