blob: 4dbad0057ed508bb0c7adb459ec708ee12722da8 [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");
Preeti Ahuja03be6672012-08-30 19:21:25 +0530126 cancelTimeOut();
Wink Savillee68857d2014-10-17 15:23:05 -0700127 appService.getStkContext(mSlotId).setPendingActivityInstance(this);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800128 sendResponse(StkAppService.RES_ID_INPUT, input, false);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800129 }
130
131 @Override
132 public void onCreate(Bundle icicle) {
133 super.onCreate(icicle);
134
Wink Savillee68857d2014-10-17 15:23:05 -0700135 CatLog.d(LOG_TAG, "onCreate - mIsResponseSent[" + mIsResponseSent + "]");
136
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800137 // Set the layout for this activity.
Wink Savillee68857d2014-10-17 15:23:05 -0700138 requestWindowFeature(Window.FEATURE_LEFT_ICON);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800139 setContentView(R.layout.stk_input);
140
141 // Initialize members
142 mTextIn = (EditText) this.findViewById(R.id.in_text);
143 mPromptView = (TextView) this.findViewById(R.id.prompt);
Wink Savillee68857d2014-10-17 15:23:05 -0700144 mInstance = this;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800145 // Set buttons listeners.
Wink Saville79085fc2009-06-09 10:27:23 -0700146 Button okButton = (Button) findViewById(R.id.button_ok);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800147 Button yesButton = (Button) findViewById(R.id.button_yes);
148 Button noButton = (Button) findViewById(R.id.button_no);
149
150 okButton.setOnClickListener(this);
151 yesButton.setOnClickListener(this);
152 noButton.setOnClickListener(this);
153
154 mYesNoLayout = findViewById(R.id.yes_no_layout);
155 mNormalLayout = findViewById(R.id.normal_layout);
Wink Savillee68857d2014-10-17 15:23:05 -0700156 initFromIntent(getIntent());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800157 mContext = getBaseContext();
Wink Savillee68857d2014-10-17 15:23:05 -0700158 mAcceptUsersInput = true;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800159 }
160
161 @Override
162 protected void onPostCreate(Bundle savedInstanceState) {
163 super.onPostCreate(savedInstanceState);
164
165 mTextIn.addTextChangedListener(this);
166 }
167
168 @Override
169 public void onResume() {
170 super.onResume();
Wink Savillee68857d2014-10-17 15:23:05 -0700171 CatLog.d(LOG_TAG, "onResume - mIsResponseSent[" + mIsResponseSent +
172 "], slot id: " + mSlotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800173 startTimeOut();
Wink Savillee68857d2014-10-17 15:23:05 -0700174 appService.getStkContext(mSlotId).setPendingActivityInstance(null);
hoonsung.parkcba29962015-07-06 23:17:38 +0900175 if (mIsResponseSent) {
176 cancelTimeOut();
177 finish();
178 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800179 }
180
181 @Override
182 public void onPause() {
183 super.onPause();
Wink Savillee68857d2014-10-17 15:23:05 -0700184 CatLog.d(LOG_TAG, "onPause - mIsResponseSent[" + mIsResponseSent + "]");
185 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800186
Wink Savillee68857d2014-10-17 15:23:05 -0700187 @Override
188 public void onStop() {
189 super.onStop();
190 CatLog.d(LOG_TAG, "onStop - mIsResponseSent[" + mIsResponseSent + "]");
191 if (mIsResponseSent) {
192 cancelTimeOut();
193 finish();
194 } else {
195 appService.getStkContext(mSlotId).setPendingActivityInstance(this);
196 }
197 }
198
199 @Override
200 public void onDestroy() {
201 super.onDestroy();
202 CatLog.d(LOG_TAG, "onDestroy - before Send End Session mIsResponseSent[" +
203 mIsResponseSent + " , " + mSlotId + "]");
204 //If the input activity is finished by stkappservice
205 //when receiving OP_LAUNCH_APP from the other SIM, we can not send TR here
206 //, since the input cmd is waiting user to process.
207 if (!mIsResponseSent && !appService.isInputPending(mSlotId)) {
208 CatLog.d(LOG_TAG, "handleDestroy - Send End Session");
209 sendResponse(StkAppService.RES_ID_END_SESSION);
210 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800211 cancelTimeOut();
212 }
213
214 @Override
215 public boolean onKeyDown(int keyCode, KeyEvent event) {
Wink Savillee68857d2014-10-17 15:23:05 -0700216 if (!mAcceptUsersInput) {
217 CatLog.d(LOG_TAG, "mAcceptUsersInput:false");
218 return true;
219 }
220
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800221 switch (keyCode) {
222 case KeyEvent.KEYCODE_BACK:
Wink Savillee68857d2014-10-17 15:23:05 -0700223 CatLog.d(LOG_TAG, "onKeyDown - KEYCODE_BACK");
224 mAcceptUsersInput = false;
Preeti Ahuja03be6672012-08-30 19:21:25 +0530225 cancelTimeOut();
Wink Savillee68857d2014-10-17 15:23:05 -0700226 appService.getStkContext(mSlotId).setPendingActivityInstance(this);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800227 sendResponse(StkAppService.RES_ID_BACKWARD, null, false);
Wink Savillee68857d2014-10-17 15:23:05 -0700228 return true;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800229 }
230 return super.onKeyDown(keyCode, event);
231 }
232
Wink Savillee68857d2014-10-17 15:23:05 -0700233 void sendResponse(int resId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800234 sendResponse(resId, null, false);
235 }
236
Wink Savillee68857d2014-10-17 15:23:05 -0700237 void sendResponse(int resId, String input, boolean help) {
238 if (mSlotId == -1) {
239 CatLog.d(LOG_TAG, "slot id is invalid");
240 return;
241 }
242
243 if (StkAppService.getInstance() == null) {
244 CatLog.d(LOG_TAG, "StkAppService is null, Ignore response: id is " + resId);
245 return;
246 }
247
Cuihtlauac ALVARADObde7f032015-05-29 16:25:12 +0200248 CatLog.d(LOG_TAG, "sendResponse resID[" + resId + "] input[*****] help["
249 + help + "]");
Wink Savillee68857d2014-10-17 15:23:05 -0700250 mIsResponseSent = true;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800251 Bundle args = new Bundle();
252 args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
Wink Savillee68857d2014-10-17 15:23:05 -0700253 args.putInt(StkAppService.SLOT_ID, mSlotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800254 args.putInt(StkAppService.RES_ID, resId);
255 if (input != null) {
256 args.putString(StkAppService.INPUT, input);
257 }
258 args.putBoolean(StkAppService.HELP, help);
259 mContext.startService(new Intent(mContext, StkAppService.class)
260 .putExtras(args));
261 }
262
263 @Override
264 public boolean onCreateOptionsMenu(android.view.Menu menu) {
265 super.onCreateOptionsMenu(menu);
266 menu.add(android.view.Menu.NONE, StkApp.MENU_ID_END_SESSION, 1,
267 R.string.menu_end_session);
268 menu.add(0, StkApp.MENU_ID_HELP, 2, R.string.help);
269
270 return true;
271 }
272
273 @Override
274 public boolean onPrepareOptionsMenu(android.view.Menu menu) {
275 super.onPrepareOptionsMenu(menu);
276 menu.findItem(StkApp.MENU_ID_END_SESSION).setVisible(true);
277 menu.findItem(StkApp.MENU_ID_HELP).setVisible(mStkInput.helpAvailable);
278
279 return true;
280 }
281
282 @Override
283 public boolean onOptionsItemSelected(MenuItem item) {
Wink Savillee68857d2014-10-17 15:23:05 -0700284 if (!mAcceptUsersInput) {
285 CatLog.d(LOG_TAG, "mAcceptUsersInput:false");
286 return true;
287 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800288 switch (item.getItemId()) {
289 case StkApp.MENU_ID_END_SESSION:
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_END_SESSION);
293 finish();
294 return true;
295 case StkApp.MENU_ID_HELP:
Wink Savillee68857d2014-10-17 15:23:05 -0700296 mAcceptUsersInput = false;
297 cancelTimeOut();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800298 sendResponse(StkAppService.RES_ID_INPUT, "", true);
299 finish();
300 return true;
301 }
302 return super.onOptionsItemSelected(item);
303 }
304
Wink Savillee68857d2014-10-17 15:23:05 -0700305 @Override
306 protected void onSaveInstanceState(Bundle outState) {
307 CatLog.d(LOG_TAG, "onSaveInstanceState: " + mSlotId);
308 outState.putBoolean("ACCEPT_USERS_INPUT", mAcceptUsersInput);
309 }
310
311 @Override
312 protected void onRestoreInstanceState(Bundle savedInstanceState) {
313 CatLog.d(LOG_TAG, "onRestoreInstanceState: " + mSlotId);
314 mAcceptUsersInput = savedInstanceState.getBoolean("ACCEPT_USERS_INPUT");
315 }
316
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800317 public void beforeTextChanged(CharSequence s, int start, int count,
318 int after) {
319 }
320
321 public void onTextChanged(CharSequence s, int start, int before, int count) {
322 // Reset timeout.
323 startTimeOut();
324 }
325
326 public void afterTextChanged(Editable s) {
327 }
328
329 private boolean verfiyTypedText() {
330 // If not enough input was typed in stay on the edit screen.
331 if (mTextIn.getText().length() < mStkInput.minLen) {
332 return false;
333 }
334
335 return true;
336 }
337
338 private void cancelTimeOut() {
339 mTimeoutHandler.removeMessages(MSG_ID_TIMEOUT);
340 }
341
342 private void startTimeOut() {
Abhishek Adappac7c3a462013-02-14 13:36:57 -0800343 int duration = StkApp.calculateDurationInMilis(mStkInput.duration);
344
345 if (duration <= 0) {
346 duration = StkApp.UI_TIMEOUT;
347 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800348 cancelTimeOut();
349 mTimeoutHandler.sendMessageDelayed(mTimeoutHandler
Abhishek Adappac7c3a462013-02-14 13:36:57 -0800350 .obtainMessage(MSG_ID_TIMEOUT), duration);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800351 }
352
353 private void configInputDisplay() {
354 TextView numOfCharsView = (TextView) findViewById(R.id.num_of_chars);
355 TextView inTypeView = (TextView) findViewById(R.id.input_type);
356
357 int inTypeId = R.string.alphabet;
358
359 // set the prompt.
Takeshi Shinoharac6589002013-05-27 11:01:06 +0900360 if (mStkInput.iconSelfExplanatory && mStkInput.icon != null) {
361 mPromptView.setVisibility(View.GONE);
362 } else {
363 mPromptView.setText(mStkInput.text);
364 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800365
Wink Saville79085fc2009-06-09 10:27:23 -0700366 // Set input type (alphabet/digit) info close to the InText form.
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800367 if (mStkInput.digitOnly) {
368 mTextIn.setKeyListener(StkDigitsKeyListener.getInstance());
369 inTypeId = R.string.digits;
370 }
371 inTypeView.setText(inTypeId);
372
373 if (mStkInput.icon != null) {
374 setFeatureDrawable(Window.FEATURE_LEFT_ICON, new BitmapDrawable(
375 mStkInput.icon));
376 }
377
378 // Handle specific global and text attributes.
379 switch (mState) {
Wink Saville79085fc2009-06-09 10:27:23 -0700380 case STATE_TEXT:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800381 int maxLen = mStkInput.maxLen;
382 int minLen = mStkInput.minLen;
383 mTextIn.setFilters(new InputFilter[] {new InputFilter.LengthFilter(
384 maxLen)});
Wink Saville79085fc2009-06-09 10:27:23 -0700385
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800386 // Set number of chars info.
387 String lengthLimit = String.valueOf(minLen);
388 if (maxLen != minLen) {
389 lengthLimit = minLen + " - " + maxLen;
390 }
391 numOfCharsView.setText(lengthLimit);
392
393 if (!mStkInput.echo) {
duho.roee36b6f2013-09-03 15:15:16 +0900394 mTextIn.setInputType(InputType.TYPE_CLASS_NUMBER
395 | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800396 }
397 // Set default text if present.
398 if (mStkInput.defaultText != null) {
399 mTextIn.setText(mStkInput.defaultText);
400 } else {
401 // make sure the text is cleared
402 mTextIn.setText("", BufferType.EDITABLE);
403 }
404
405 break;
406 case STATE_YES_NO:
407 // Set display mode - normal / yes-no layout
408 mYesNoLayout.setVisibility(View.VISIBLE);
409 mNormalLayout.setVisibility(View.GONE);
410 break;
411 }
412 }
413
414 private float getFontSizeFactor(FontSize size) {
Wink Saville79085fc2009-06-09 10:27:23 -0700415 final float[] fontSizes =
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800416 {NORMAL_FONT_FACTOR, LARGE_FONT_FACTOR, SMALL_FONT_FACTOR};
417
418 return fontSizes[size.ordinal()];
419 }
Wink Savillee68857d2014-10-17 15:23:05 -0700420
421 private void initFromIntent(Intent intent) {
422 // Get the calling intent type: text/key, and setup the
423 // display parameters.
424 CatLog.d(LOG_TAG, "initFromIntent - slot id: " + mSlotId);
425 if (intent != null) {
426 mStkInput = intent.getParcelableExtra("INPUT");
427 mSlotId = intent.getIntExtra(StkAppService.SLOT_ID, -1);
428 CatLog.d(LOG_TAG, "onCreate - slot id: " + mSlotId);
429 if (mStkInput == null) {
430 finish();
431 } else {
432 mState = mStkInput.yesNo ? STATE_YES_NO :
433 STATE_TEXT;
434 configInputDisplay();
435 }
436 } else {
437 finish();
438 }
439 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800440}