blob: bfaa36dfa288a63c5e7c49c279941b71d4138c11 [file] [log] [blame]
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
Wink Saville79085fc2009-06-09 10:27:23 -07003 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08004 * 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
Wink Saville79085fc2009-06-09 10:27:23 -07007 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Wink Saville79085fc2009-06-09 10:27:23 -07009 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080010 * 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.stk;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.graphics.drawable.BitmapDrawable;
23import android.os.Bundle;
24import android.os.Handler;
25import android.os.Message;
26import android.text.Editable;
27import android.text.InputFilter;
duho.roee36b6f2013-09-03 15:15:16 +090028import android.text.InputType;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080029import android.text.TextWatcher;
30import android.text.method.PasswordTransformationMethod;
31import android.view.KeyEvent;
32import android.view.MenuItem;
33import android.view.View;
34import android.view.Window;
35import android.widget.Button;
36import android.widget.TextView;
37import android.widget.EditText;
38import android.widget.TextView.BufferType;
Wink Savillee68857d2014-10-17 15:23:05 -070039import com.android.internal.telephony.cat.CatLog;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070040import com.android.internal.telephony.cat.FontSize;
41import com.android.internal.telephony.cat.Input;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080042
43/**
44 * Display a request for a text input a long with a text edit form.
45 */
46public class StkInputActivity extends Activity implements View.OnClickListener,
47 TextWatcher {
48
49 // Members
50 private int mState;
51 private Context mContext;
52 private EditText mTextIn = null;
53 private TextView mPromptView = null;
54 private View mYesNoLayout = null;
55 private View mNormalLayout = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080056
57 // Constants
Wink Savillee68857d2014-10-17 15:23:05 -070058 private static final String className = new Object(){}.getClass().getEnclosingClass().getName();
59 private static final String LOG_TAG = className.substring(className.lastIndexOf('.') + 1);
60
61 private Input mStkInput = null;
62 private boolean mAcceptUsersInput = true;
63 // Constants
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080064 private static final int STATE_TEXT = 1;
65 private static final int STATE_YES_NO = 2;
66
67 static final String YES_STR_RESPONSE = "YES";
68 static final String NO_STR_RESPONSE = "NO";
69
70 // Font size factor values.
71 static final float NORMAL_FONT_FACTOR = 1;
72 static final float LARGE_FONT_FACTOR = 2;
73 static final float SMALL_FONT_FACTOR = (1 / 2);
74
Wink Saville79085fc2009-06-09 10:27:23 -070075 // message id for time out
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080076 private static final int MSG_ID_TIMEOUT = 1;
Wink Savillee68857d2014-10-17 15:23:05 -070077 private StkAppService appService = StkAppService.getInstance();
78
79 private boolean mIsResponseSent = false;
80 private int mSlotId = -1;
81 Activity mInstance = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080082
83 Handler mTimeoutHandler = new Handler() {
84 @Override
85 public void handleMessage(Message msg) {
86 switch(msg.what) {
87 case MSG_ID_TIMEOUT:
Wink Savillee68857d2014-10-17 15:23:05 -070088 CatLog.d(LOG_TAG, "Msg timeout.");
89 mAcceptUsersInput = false;
90 appService.getStkContext(mSlotId).setPendingActivityInstance(mInstance);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080091 sendResponse(StkAppService.RES_ID_TIMEOUT);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080092 break;
93 }
94 }
95 };
96
97 // Click listener to handle buttons press..
98 public void onClick(View v) {
99 String input = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700100 if (!mAcceptUsersInput) {
101 CatLog.d(LOG_TAG, "mAcceptUsersInput:false");
102 return;
103 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800104
105 switch (v.getId()) {
106 case R.id.button_ok:
107 // Check that text entered is valid .
108 if (!verfiyTypedText()) {
Wink Savillee68857d2014-10-17 15:23:05 -0700109 CatLog.d(LOG_TAG, "handleClick, invalid text");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800110 return;
111 }
Wink Savillee68857d2014-10-17 15:23:05 -0700112 mAcceptUsersInput = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800113 input = mTextIn.getText().toString();
114 break;
115 // Yes/No layout buttons.
116 case R.id.button_yes:
Wink Savillee68857d2014-10-17 15:23:05 -0700117 mAcceptUsersInput = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800118 input = YES_STR_RESPONSE;
119 break;
120 case R.id.button_no:
Wink Savillee68857d2014-10-17 15:23:05 -0700121 mAcceptUsersInput = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800122 input = NO_STR_RESPONSE;
123 break;
124 }
Wink Savillee68857d2014-10-17 15:23:05 -0700125 CatLog.d(LOG_TAG, "handleClick, ready to response");
126 appService.getStkContext(mSlotId).setPendingActivityInstance(this);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800127 sendResponse(StkAppService.RES_ID_INPUT, input, false);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800128 }
129
130 @Override
131 public void onCreate(Bundle icicle) {
132 super.onCreate(icicle);
133
Wink Savillee68857d2014-10-17 15:23:05 -0700134 CatLog.d(LOG_TAG, "onCreate - mIsResponseSent[" + mIsResponseSent + "]");
135
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800136 // Set the layout for this activity.
Wink Savillee68857d2014-10-17 15:23:05 -0700137 requestWindowFeature(Window.FEATURE_LEFT_ICON);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800138 setContentView(R.layout.stk_input);
139
140 // Initialize members
141 mTextIn = (EditText) this.findViewById(R.id.in_text);
142 mPromptView = (TextView) this.findViewById(R.id.prompt);
Wink Savillee68857d2014-10-17 15:23:05 -0700143 mInstance = this;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800144 // Set buttons listeners.
Wink Saville79085fc2009-06-09 10:27:23 -0700145 Button okButton = (Button) findViewById(R.id.button_ok);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800146 Button yesButton = (Button) findViewById(R.id.button_yes);
147 Button noButton = (Button) findViewById(R.id.button_no);
148
149 okButton.setOnClickListener(this);
150 yesButton.setOnClickListener(this);
151 noButton.setOnClickListener(this);
152
153 mYesNoLayout = findViewById(R.id.yes_no_layout);
154 mNormalLayout = findViewById(R.id.normal_layout);
Wink Savillee68857d2014-10-17 15:23:05 -0700155 initFromIntent(getIntent());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800156 mContext = getBaseContext();
Wink Savillee68857d2014-10-17 15:23:05 -0700157 mAcceptUsersInput = true;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800158 }
159
160 @Override
161 protected void onPostCreate(Bundle savedInstanceState) {
162 super.onPostCreate(savedInstanceState);
163
164 mTextIn.addTextChangedListener(this);
165 }
166
167 @Override
168 public void onResume() {
169 super.onResume();
Wink Savillee68857d2014-10-17 15:23:05 -0700170 CatLog.d(LOG_TAG, "onResume - mIsResponseSent[" + mIsResponseSent +
171 "], slot id: " + mSlotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800172 startTimeOut();
Wink Savillee68857d2014-10-17 15:23:05 -0700173 appService.getStkContext(mSlotId).setPendingActivityInstance(null);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800174 }
175
176 @Override
177 public void onPause() {
178 super.onPause();
Wink Savillee68857d2014-10-17 15:23:05 -0700179 CatLog.d(LOG_TAG, "onPause - mIsResponseSent[" + mIsResponseSent + "]");
180 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800181
Wink Savillee68857d2014-10-17 15:23:05 -0700182 @Override
183 public void onStop() {
184 super.onStop();
185 CatLog.d(LOG_TAG, "onStop - mIsResponseSent[" + mIsResponseSent + "]");
186 if (mIsResponseSent) {
187 cancelTimeOut();
188 finish();
189 } else {
190 appService.getStkContext(mSlotId).setPendingActivityInstance(this);
191 }
192 }
193
194 @Override
195 public void onDestroy() {
196 super.onDestroy();
197 CatLog.d(LOG_TAG, "onDestroy - before Send End Session mIsResponseSent[" +
198 mIsResponseSent + " , " + mSlotId + "]");
199 //If the input activity is finished by stkappservice
200 //when receiving OP_LAUNCH_APP from the other SIM, we can not send TR here
201 //, since the input cmd is waiting user to process.
202 if (!mIsResponseSent && !appService.isInputPending(mSlotId)) {
203 CatLog.d(LOG_TAG, "handleDestroy - Send End Session");
204 sendResponse(StkAppService.RES_ID_END_SESSION);
205 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800206 cancelTimeOut();
207 }
208
209 @Override
210 public boolean onKeyDown(int keyCode, KeyEvent event) {
Wink Savillee68857d2014-10-17 15:23:05 -0700211 if (!mAcceptUsersInput) {
212 CatLog.d(LOG_TAG, "mAcceptUsersInput:false");
213 return true;
214 }
215
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800216 switch (keyCode) {
217 case KeyEvent.KEYCODE_BACK:
Wink Savillee68857d2014-10-17 15:23:05 -0700218 CatLog.d(LOG_TAG, "onKeyDown - KEYCODE_BACK");
219 mAcceptUsersInput = false;
220 appService.getStkContext(mSlotId).setPendingActivityInstance(this);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800221 sendResponse(StkAppService.RES_ID_BACKWARD, null, false);
Wink Savillee68857d2014-10-17 15:23:05 -0700222 return true;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800223 }
224 return super.onKeyDown(keyCode, event);
225 }
226
Wink Savillee68857d2014-10-17 15:23:05 -0700227 void sendResponse(int resId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800228 sendResponse(resId, null, false);
229 }
230
Wink Savillee68857d2014-10-17 15:23:05 -0700231 void sendResponse(int resId, String input, boolean help) {
232 if (mSlotId == -1) {
233 CatLog.d(LOG_TAG, "slot id is invalid");
234 return;
235 }
236
237 if (StkAppService.getInstance() == null) {
238 CatLog.d(LOG_TAG, "StkAppService is null, Ignore response: id is " + resId);
239 return;
240 }
241
242 CatLog.d(LOG_TAG, "sendResponse resID[" + resId + "] input[" + input +
243 "] help[" + help + "]");
244 mIsResponseSent = true;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800245 Bundle args = new Bundle();
246 args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
Wink Savillee68857d2014-10-17 15:23:05 -0700247 args.putInt(StkAppService.SLOT_ID, mSlotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800248 args.putInt(StkAppService.RES_ID, resId);
249 if (input != null) {
250 args.putString(StkAppService.INPUT, input);
251 }
252 args.putBoolean(StkAppService.HELP, help);
253 mContext.startService(new Intent(mContext, StkAppService.class)
254 .putExtras(args));
255 }
256
257 @Override
258 public boolean onCreateOptionsMenu(android.view.Menu menu) {
259 super.onCreateOptionsMenu(menu);
260 menu.add(android.view.Menu.NONE, StkApp.MENU_ID_END_SESSION, 1,
261 R.string.menu_end_session);
262 menu.add(0, StkApp.MENU_ID_HELP, 2, R.string.help);
263
264 return true;
265 }
266
267 @Override
268 public boolean onPrepareOptionsMenu(android.view.Menu menu) {
269 super.onPrepareOptionsMenu(menu);
270 menu.findItem(StkApp.MENU_ID_END_SESSION).setVisible(true);
271 menu.findItem(StkApp.MENU_ID_HELP).setVisible(mStkInput.helpAvailable);
272
273 return true;
274 }
275
276 @Override
277 public boolean onOptionsItemSelected(MenuItem item) {
Wink Savillee68857d2014-10-17 15:23:05 -0700278 if (!mAcceptUsersInput) {
279 CatLog.d(LOG_TAG, "mAcceptUsersInput:false");
280 return true;
281 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800282 switch (item.getItemId()) {
283 case StkApp.MENU_ID_END_SESSION:
Wink Savillee68857d2014-10-17 15:23:05 -0700284 mAcceptUsersInput = false;
285 cancelTimeOut();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800286 sendResponse(StkAppService.RES_ID_END_SESSION);
287 finish();
288 return true;
289 case StkApp.MENU_ID_HELP:
Wink Savillee68857d2014-10-17 15:23:05 -0700290 mAcceptUsersInput = false;
291 cancelTimeOut();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800292 sendResponse(StkAppService.RES_ID_INPUT, "", true);
293 finish();
294 return true;
295 }
296 return super.onOptionsItemSelected(item);
297 }
298
Wink Savillee68857d2014-10-17 15:23:05 -0700299 @Override
300 protected void onSaveInstanceState(Bundle outState) {
301 CatLog.d(LOG_TAG, "onSaveInstanceState: " + mSlotId);
302 outState.putBoolean("ACCEPT_USERS_INPUT", mAcceptUsersInput);
303 }
304
305 @Override
306 protected void onRestoreInstanceState(Bundle savedInstanceState) {
307 CatLog.d(LOG_TAG, "onRestoreInstanceState: " + mSlotId);
308 mAcceptUsersInput = savedInstanceState.getBoolean("ACCEPT_USERS_INPUT");
309 }
310
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800311 public void beforeTextChanged(CharSequence s, int start, int count,
312 int after) {
313 }
314
315 public void onTextChanged(CharSequence s, int start, int before, int count) {
316 // Reset timeout.
317 startTimeOut();
318 }
319
320 public void afterTextChanged(Editable s) {
321 }
322
323 private boolean verfiyTypedText() {
324 // If not enough input was typed in stay on the edit screen.
325 if (mTextIn.getText().length() < mStkInput.minLen) {
326 return false;
327 }
328
329 return true;
330 }
331
332 private void cancelTimeOut() {
333 mTimeoutHandler.removeMessages(MSG_ID_TIMEOUT);
334 }
335
336 private void startTimeOut() {
Abhishek Adappac7c3a462013-02-14 13:36:57 -0800337 int duration = StkApp.calculateDurationInMilis(mStkInput.duration);
338
339 if (duration <= 0) {
340 duration = StkApp.UI_TIMEOUT;
341 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800342 cancelTimeOut();
343 mTimeoutHandler.sendMessageDelayed(mTimeoutHandler
Abhishek Adappac7c3a462013-02-14 13:36:57 -0800344 .obtainMessage(MSG_ID_TIMEOUT), duration);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800345 }
346
347 private void configInputDisplay() {
348 TextView numOfCharsView = (TextView) findViewById(R.id.num_of_chars);
349 TextView inTypeView = (TextView) findViewById(R.id.input_type);
350
351 int inTypeId = R.string.alphabet;
352
353 // set the prompt.
354 mPromptView.setText(mStkInput.text);
355
Wink Saville79085fc2009-06-09 10:27:23 -0700356 // Set input type (alphabet/digit) info close to the InText form.
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800357 if (mStkInput.digitOnly) {
358 mTextIn.setKeyListener(StkDigitsKeyListener.getInstance());
359 inTypeId = R.string.digits;
360 }
361 inTypeView.setText(inTypeId);
362
363 if (mStkInput.icon != null) {
364 setFeatureDrawable(Window.FEATURE_LEFT_ICON, new BitmapDrawable(
365 mStkInput.icon));
366 }
367
368 // Handle specific global and text attributes.
369 switch (mState) {
Wink Saville79085fc2009-06-09 10:27:23 -0700370 case STATE_TEXT:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800371 int maxLen = mStkInput.maxLen;
372 int minLen = mStkInput.minLen;
373 mTextIn.setFilters(new InputFilter[] {new InputFilter.LengthFilter(
374 maxLen)});
Wink Saville79085fc2009-06-09 10:27:23 -0700375
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800376 // Set number of chars info.
377 String lengthLimit = String.valueOf(minLen);
378 if (maxLen != minLen) {
379 lengthLimit = minLen + " - " + maxLen;
380 }
381 numOfCharsView.setText(lengthLimit);
382
383 if (!mStkInput.echo) {
duho.roee36b6f2013-09-03 15:15:16 +0900384 mTextIn.setInputType(InputType.TYPE_CLASS_NUMBER
385 | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800386 }
387 // Set default text if present.
388 if (mStkInput.defaultText != null) {
389 mTextIn.setText(mStkInput.defaultText);
390 } else {
391 // make sure the text is cleared
392 mTextIn.setText("", BufferType.EDITABLE);
393 }
394
395 break;
396 case STATE_YES_NO:
397 // Set display mode - normal / yes-no layout
398 mYesNoLayout.setVisibility(View.VISIBLE);
399 mNormalLayout.setVisibility(View.GONE);
400 break;
401 }
402 }
403
404 private float getFontSizeFactor(FontSize size) {
Wink Saville79085fc2009-06-09 10:27:23 -0700405 final float[] fontSizes =
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800406 {NORMAL_FONT_FACTOR, LARGE_FONT_FACTOR, SMALL_FONT_FACTOR};
407
408 return fontSizes[size.ordinal()];
409 }
Wink Savillee68857d2014-10-17 15:23:05 -0700410
411 private void initFromIntent(Intent intent) {
412 // Get the calling intent type: text/key, and setup the
413 // display parameters.
414 CatLog.d(LOG_TAG, "initFromIntent - slot id: " + mSlotId);
415 if (intent != null) {
416 mStkInput = intent.getParcelableExtra("INPUT");
417 mSlotId = intent.getIntExtra(StkAppService.SLOT_ID, -1);
418 CatLog.d(LOG_TAG, "onCreate - slot id: " + mSlotId);
419 if (mStkInput == null) {
420 finish();
421 } else {
422 mState = mStkInput.yesNo ? STATE_YES_NO :
423 STATE_TEXT;
424 configInputDisplay();
425 }
426 } else {
427 finish();
428 }
429 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800430}