1/* 2 * Copyright (C) 2006-2007 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.android.internal.telephony.gsm.stk; 18 19import com.android.internal.telephony.EncodeException; 20import com.android.internal.telephony.GsmAlphabet; 21 22import java.io.ByteArrayOutputStream; 23import java.io.UnsupportedEncodingException; 24 25abstract class ResponseData { 26 /** 27 * Format the data appropriate for TERMINAL RESPONSE and write it into 28 * the ByteArrayOutputStream object. 29 */ 30 public abstract void format(ByteArrayOutputStream buf); 31} 32 33class SelectItemResponseData extends ResponseData { 34 // members 35 private int id; 36 37 public SelectItemResponseData(int id) { 38 super(); 39 this.id = id; 40 } 41 42 @Override 43 public void format(ByteArrayOutputStream buf) { 44 // Item identifier object 45 int tag = 0x80 | ComprehensionTlvTag.ITEM_ID.value(); 46 buf.write(tag); // tag 47 buf.write(1); // length 48 buf.write(id); // identifier of item chosen 49 } 50} 51 52class GetInkeyInputResponseData extends ResponseData { 53 // members 54 private boolean mIsUcs2; 55 private boolean mIsPacked; 56 private boolean mIsYesNo; 57 private boolean mYesNoResponse; 58 public String mInData; 59 60 // GetInKey Yes/No response characters constants. 61 protected static final byte GET_INKEY_YES = 0x01; 62 protected static final byte GET_INKEY_NO = 0x00; 63 64 public GetInkeyInputResponseData(String inData, boolean ucs2, boolean packed) { 65 super(); 66 this.mIsUcs2 = ucs2; 67 this.mIsPacked = packed; 68 this.mInData = inData; 69 this.mIsYesNo = false; 70 } 71 72 public GetInkeyInputResponseData(boolean yesNoResponse) { 73 super(); 74 this.mIsUcs2 = false; 75 this.mIsPacked = false; 76 this.mInData = ""; 77 this.mIsYesNo = true; 78 this.mYesNoResponse = yesNoResponse; 79 } 80 81 @Override 82 public void format(ByteArrayOutputStream buf) { 83 if (buf == null) { 84 return; 85 } 86 87 // Text string object 88 int tag = 0x80 | ComprehensionTlvTag.TEXT_STRING.value(); 89 buf.write(tag); // tag 90 91 byte[] data; 92 93 if (mIsYesNo) { 94 data = new byte[1]; 95 data[0] = mYesNoResponse ? GET_INKEY_YES : GET_INKEY_NO; 96 } else if (mInData != null && mInData.length() > 0) { 97 try { 98 if (mIsUcs2) { 99 data = mInData.getBytes("UTF-16"); 100 } else if (mIsPacked) { 101 int size = mInData.length(); 102 103 byte[] tempData = GsmAlphabet 104 .stringToGsm7BitPacked(mInData); 105 data = new byte[size]; 106 // Since stringToGsm7BitPacked() set byte 0 in the 107 // returned byte array to the count of septets used... 108 // copy to a new array without byte 0. 109 System.arraycopy(tempData, 1, data, 0, size); 110 } else { 111 data = GsmAlphabet.stringToGsm8BitPacked(mInData); 112 } 113 } catch (UnsupportedEncodingException e) { 114 data = new byte[0]; 115 } catch (EncodeException e) { 116 data = new byte[0]; 117 } 118 } else { 119 data = new byte[0]; 120 } 121 122 // length - one more for data coding scheme. 123 buf.write(data.length + 1); 124 125 // data coding scheme 126 if (mIsUcs2) { 127 buf.write(0x08); // UCS2 128 } else if (mIsPacked) { 129 buf.write(0x00); // 7 bit packed 130 } else { 131 buf.write(0x04); // 8 bit unpacked 132 } 133 134 for (byte b : data) { 135 buf.write(b); 136 } 137 } 138} 139 140 141