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 public String getAddressString() { 83 return address; 84 } 85 86 /** 87 * Returns true if this is an alphanumeric address 88 */ 89 public boolean isAlphanumeric() { 90 return ton == TON_ALPHANUMERIC; 91 } 92 93 public boolean isNetworkSpecific() { 94 return ton == TON_NETWORK; 95 } 96 97 /** 98 * Returns true of this is a valid CPHS voice message waiting indicator 99 * address 100 */ 101 public boolean isCphsVoiceMessageIndicatorAddress() { 102 // CPHS-style MWI message 103 // See CPHS 4.7 B.4.2.1 104 // 105 // Basically: 106 // 107 // - Originating address should be 4 bytes long and alphanumeric 108 // - Decode will result with two chars: 109 // - Char 1 110 // 76543210 111 // ^ set/clear indicator (0 = clear) 112 // ^^^ type of indicator (000 = voice) 113 // ^^^^ must be equal to 0001 114 // - Char 2: 115 // 76543210 116 // ^ line number (0 = line 1) 117 // ^^^^^^^ set to 0 118 // 119 // Remember, since the alpha address is stored in 7-bit compact form, 120 // the "line number" is really the top bit of the first address value 121 // byte 122 123 return (origBytes[OFFSET_ADDRESS_LENGTH] & 0xff) == 4 124 && isAlphanumeric() && (origBytes[OFFSET_TOA] & 0x0f) == 0; 125 } 126 127 /** 128 * Returns true if this is a valid CPHS voice message waiting indicator 129 * address indicating a "set" of "indicator 1" of type "voice message 130 * waiting" 131 */ 132 public boolean isCphsVoiceMessageSet() { 133 // 0x11 means "set" "voice message waiting" "indicator 1" 134 return isCphsVoiceMessageIndicatorAddress() 135 && (origBytes[OFFSET_ADDRESS_VALUE] & 0xff) == 0x11; 136 137 } 138 139 /** 140 * Returns true if this is a valid CPHS voice message waiting indicator 141 * address indicating a "clear" of "indicator 1" of type "voice message 142 * waiting" 143 */ 144 public boolean isCphsVoiceMessageClear() { 145 // 0x10 means "clear" "voice message waiting" "indicator 1" 146 return isCphsVoiceMessageIndicatorAddress() 147 && (origBytes[OFFSET_ADDRESS_VALUE] & 0xff) == 0x10; 148 149 } 150} 151