The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 3 | * |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 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 |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 7 | * |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 9 | * |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.stk; |
| 18 | |
Takanori Nakano | 4854435 | 2016-12-02 18:32:59 +0900 | [diff] [blame] | 19 | import android.app.ActionBar; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 20 | import android.app.Activity; |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 21 | import android.app.AlarmManager; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 24 | import android.content.res.Configuration; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 25 | import android.os.Bundle; |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 26 | import android.os.SystemClock; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 27 | import android.text.Editable; |
| 28 | import android.text.InputFilter; |
Yoshiaki Naka | dd0dbb1 | 2017-08-03 21:20:38 +0900 | [diff] [blame] | 29 | import android.text.TextUtils; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 30 | import android.text.TextWatcher; |
| 31 | import android.text.method.PasswordTransformationMethod; |
| 32 | import android.view.KeyEvent; |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 33 | import android.view.Menu; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 34 | import android.view.MenuItem; |
| 35 | import android.view.View; |
Kazuhiro Uto | 1287091 | 2018-07-18 17:43:56 +0900 | [diff] [blame] | 36 | import android.view.WindowManager; |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 37 | import android.view.inputmethod.EditorInfo; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 38 | import android.widget.Button; |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 39 | import android.widget.EditText; |
Takanori Nakano | 4854435 | 2016-12-02 18:32:59 +0900 | [diff] [blame] | 40 | import android.widget.ImageView; |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 41 | import android.widget.PopupMenu; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 42 | import android.widget.TextView; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 43 | import android.widget.TextView.BufferType; |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 44 | |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 45 | import com.android.internal.telephony.cat.CatLog; |
Alex Yakavenka | d41f1d9 | 2010-07-12 14:13:13 -0700 | [diff] [blame] | 46 | import com.android.internal.telephony.cat.Input; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Display a request for a text input a long with a text edit form. |
| 50 | */ |
| 51 | public class StkInputActivity extends Activity implements View.OnClickListener, |
| 52 | TextWatcher { |
| 53 | |
| 54 | // Members |
| 55 | private int mState; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 56 | private EditText mTextIn = null; |
| 57 | private TextView mPromptView = null; |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 58 | private View mMoreOptions = null; |
| 59 | private PopupMenu mPopupMenu = null; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 60 | private View mYesNoLayout = null; |
| 61 | private View mNormalLayout = null; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 62 | |
| 63 | // Constants |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 64 | private static final String className = new Object(){}.getClass().getEnclosingClass().getName(); |
| 65 | private static final String LOG_TAG = className.substring(className.lastIndexOf('.') + 1); |
| 66 | |
| 67 | private Input mStkInput = null; |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 68 | // Constants |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 69 | private static final int STATE_TEXT = 1; |
| 70 | private static final int STATE_YES_NO = 2; |
| 71 | |
| 72 | static final String YES_STR_RESPONSE = "YES"; |
| 73 | static final String NO_STR_RESPONSE = "NO"; |
| 74 | |
| 75 | // Font size factor values. |
| 76 | static final float NORMAL_FONT_FACTOR = 1; |
| 77 | static final float LARGE_FONT_FACTOR = 2; |
| 78 | static final float SMALL_FONT_FACTOR = (1 / 2); |
| 79 | |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 80 | // Keys for saving the state of the activity in the bundle |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 81 | private static final String RESPONSE_SENT_KEY = "response_sent"; |
| 82 | private static final String INPUT_STRING_KEY = "input_string"; |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 83 | private static final String ALARM_TIME_KEY = "alarm_time"; |
Yoshiaki Naka | 03a79d9 | 2018-08-09 18:41:31 +0900 | [diff] [blame^] | 84 | private static final String PENDING = "pending"; |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 85 | |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 86 | private static final String INPUT_ALARM_TAG = LOG_TAG; |
| 87 | private static final long NO_INPUT_ALARM = -1; |
| 88 | private long mAlarmTime = NO_INPUT_ALARM; |
| 89 | |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 90 | private StkAppService appService = StkAppService.getInstance(); |
| 91 | |
| 92 | private boolean mIsResponseSent = false; |
Yoshiaki Naka | 03a79d9 | 2018-08-09 18:41:31 +0900 | [diff] [blame^] | 93 | // Determines whether this is in the pending state. |
| 94 | private boolean mIsPending = false; |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 95 | private int mSlotId = -1; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 96 | |
| 97 | // Click listener to handle buttons press.. |
| 98 | public void onClick(View v) { |
| 99 | String input = null; |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 100 | if (mIsResponseSent) { |
| 101 | CatLog.d(LOG_TAG, "Already responded"); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 102 | return; |
| 103 | } |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 104 | |
| 105 | switch (v.getId()) { |
| 106 | case R.id.button_ok: |
| 107 | // Check that text entered is valid . |
| 108 | if (!verfiyTypedText()) { |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 109 | CatLog.d(LOG_TAG, "handleClick, invalid text"); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | input = mTextIn.getText().toString(); |
| 113 | break; |
Takanori Nakano | 260df9a | 2017-12-05 22:26:30 +0900 | [diff] [blame] | 114 | case R.id.button_cancel: |
Takanori Nakano | 260df9a | 2017-12-05 22:26:30 +0900 | [diff] [blame] | 115 | sendResponse(StkAppService.RES_ID_END_SESSION); |
Yoshiaki Naka | 03a79d9 | 2018-08-09 18:41:31 +0900 | [diff] [blame^] | 116 | finish(); |
Takanori Nakano | 260df9a | 2017-12-05 22:26:30 +0900 | [diff] [blame] | 117 | return; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 118 | // Yes/No layout buttons. |
| 119 | case R.id.button_yes: |
| 120 | input = YES_STR_RESPONSE; |
| 121 | break; |
| 122 | case R.id.button_no: |
| 123 | input = NO_STR_RESPONSE; |
| 124 | break; |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 125 | case R.id.more: |
| 126 | if (mPopupMenu == null) { |
| 127 | mPopupMenu = new PopupMenu(this, v); |
| 128 | Menu menu = mPopupMenu.getMenu(); |
| 129 | createOptionsMenuInternal(menu); |
| 130 | prepareOptionsMenuInternal(menu); |
| 131 | mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { |
| 132 | public boolean onMenuItemClick(MenuItem item) { |
| 133 | optionsItemSelectedInternal(item); |
| 134 | return true; |
| 135 | } |
| 136 | }); |
| 137 | mPopupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() { |
| 138 | public void onDismiss(PopupMenu menu) { |
| 139 | mPopupMenu = null; |
| 140 | } |
| 141 | }); |
| 142 | mPopupMenu.show(); |
| 143 | } |
| 144 | return; |
| 145 | default: |
| 146 | break; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 147 | } |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 148 | CatLog.d(LOG_TAG, "handleClick, ready to response"); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 149 | sendResponse(StkAppService.RES_ID_INPUT, input, false); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 153 | public void onCreate(Bundle savedInstanceState) { |
| 154 | super.onCreate(savedInstanceState); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 155 | |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 156 | CatLog.d(LOG_TAG, "onCreate - mIsResponseSent[" + mIsResponseSent + "]"); |
| 157 | |
Ryuto Sawada | ba9b86b | 2016-10-03 14:03:16 +0900 | [diff] [blame] | 158 | // appService can be null if this activity is automatically recreated by the system |
| 159 | // with the saved instance state right after the phone process is killed. |
| 160 | if (appService == null) { |
| 161 | CatLog.d(LOG_TAG, "onCreate - appService is null"); |
| 162 | finish(); |
| 163 | return; |
| 164 | } |
| 165 | |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 166 | ActionBar actionBar = null; |
| 167 | if (getResources().getBoolean(R.bool.show_menu_title_only_on_menu)) { |
| 168 | actionBar = getActionBar(); |
| 169 | if (actionBar != null) { |
| 170 | actionBar.hide(); |
| 171 | } |
| 172 | } |
Takanori Nakano | 4854435 | 2016-12-02 18:32:59 +0900 | [diff] [blame] | 173 | |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 174 | // Set the layout for this activity. |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 175 | setContentView(R.layout.stk_input); |
| 176 | |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 177 | if (actionBar != null) { |
| 178 | mMoreOptions = findViewById(R.id.more); |
| 179 | mMoreOptions.setVisibility(View.VISIBLE); |
| 180 | mMoreOptions.setOnClickListener(this); |
| 181 | } |
| 182 | |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 183 | // Initialize members |
| 184 | mTextIn = (EditText) this.findViewById(R.id.in_text); |
| 185 | mPromptView = (TextView) this.findViewById(R.id.prompt); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 186 | // Set buttons listeners. |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 187 | Button okButton = (Button) findViewById(R.id.button_ok); |
Takanori Nakano | 260df9a | 2017-12-05 22:26:30 +0900 | [diff] [blame] | 188 | Button cancelButton = (Button) findViewById(R.id.button_cancel); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 189 | Button yesButton = (Button) findViewById(R.id.button_yes); |
| 190 | Button noButton = (Button) findViewById(R.id.button_no); |
| 191 | |
| 192 | okButton.setOnClickListener(this); |
Takanori Nakano | 260df9a | 2017-12-05 22:26:30 +0900 | [diff] [blame] | 193 | cancelButton.setOnClickListener(this); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 194 | yesButton.setOnClickListener(this); |
| 195 | noButton.setOnClickListener(this); |
| 196 | |
| 197 | mYesNoLayout = findViewById(R.id.yes_no_layout); |
| 198 | mNormalLayout = findViewById(R.id.normal_layout); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 199 | initFromIntent(getIntent()); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
| 203 | protected void onPostCreate(Bundle savedInstanceState) { |
| 204 | super.onPostCreate(savedInstanceState); |
| 205 | |
| 206 | mTextIn.addTextChangedListener(this); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void onResume() { |
| 211 | super.onResume(); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 212 | CatLog.d(LOG_TAG, "onResume - mIsResponseSent[" + mIsResponseSent + |
| 213 | "], slot id: " + mSlotId); |
Yoshiaki Naka | 03a79d9 | 2018-08-09 18:41:31 +0900 | [diff] [blame^] | 214 | // If the terminal has already sent response to the card when this activity is resumed, |
| 215 | // keep this as a pending activity as this should be finished when the session ends. |
| 216 | if (!mIsResponseSent) { |
| 217 | setPendingState(false); |
| 218 | } |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 219 | |
| 220 | if (mAlarmTime == NO_INPUT_ALARM) { |
| 221 | startTimeOut(); |
| 222 | } |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public void onPause() { |
| 227 | super.onPause(); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 228 | CatLog.d(LOG_TAG, "onPause - mIsResponseSent[" + mIsResponseSent + "]"); |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 229 | if (mPopupMenu != null) { |
| 230 | mPopupMenu.dismiss(); |
| 231 | } |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 232 | } |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 233 | |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 234 | @Override |
| 235 | public void onStop() { |
| 236 | super.onStop(); |
| 237 | CatLog.d(LOG_TAG, "onStop - mIsResponseSent[" + mIsResponseSent + "]"); |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 238 | |
Yoshiaki Naka | 03a79d9 | 2018-08-09 18:41:31 +0900 | [diff] [blame^] | 239 | // Nothing should be done here if this activity is being finished or restarted now. |
| 240 | if (isFinishing() || isChangingConfigurations()) { |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
Yoshiaki Naka | 9de7afa | 2018-08-15 19:07:35 +0900 | [diff] [blame] | 244 | if (mIsResponseSent) { |
| 245 | // It is unnecessary to keep this activity if the response was already sent and |
| 246 | // the dialog activity is NOT on the top of this activity. |
| 247 | if (!appService.isStkDialogActivated()) { |
| 248 | finish(); |
| 249 | } |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 250 | } else { |
Yoshiaki Naka | 03a79d9 | 2018-08-09 18:41:31 +0900 | [diff] [blame^] | 251 | // This should be registered as the pending activity here |
Yoshiaki Naka | 9de7afa | 2018-08-15 19:07:35 +0900 | [diff] [blame] | 252 | // only when no response has been sent back to the card. |
Yoshiaki Naka | 03a79d9 | 2018-08-09 18:41:31 +0900 | [diff] [blame^] | 253 | setPendingState(true); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void onDestroy() { |
| 259 | super.onDestroy(); |
| 260 | CatLog.d(LOG_TAG, "onDestroy - before Send End Session mIsResponseSent[" + |
| 261 | mIsResponseSent + " , " + mSlotId + "]"); |
Ryuto Sawada | ba9b86b | 2016-10-03 14:03:16 +0900 | [diff] [blame] | 262 | if (appService == null) { |
| 263 | return; |
| 264 | } |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 265 | // Avoid sending the terminal response while the activty is being restarted |
| 266 | // due to some kind of configuration change. |
| 267 | if (!isChangingConfigurations()) { |
| 268 | // If the input activity is finished by stkappservice |
| 269 | // when receiving OP_LAUNCH_APP from the other SIM, we can not send TR here, |
| 270 | // since the input cmd is waiting user to process. |
| 271 | if (!mIsResponseSent && !appService.isInputPending(mSlotId)) { |
| 272 | CatLog.d(LOG_TAG, "handleDestroy - Send End Session"); |
| 273 | sendResponse(StkAppService.RES_ID_END_SESSION); |
| 274 | } |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 275 | } |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 276 | cancelTimeOut(); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Override |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 280 | public void onConfigurationChanged(Configuration newConfig) { |
| 281 | super.onConfigurationChanged(newConfig); |
| 282 | if (mPopupMenu != null) { |
| 283 | mPopupMenu.dismiss(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | @Override |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 288 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 289 | if (mIsResponseSent) { |
| 290 | CatLog.d(LOG_TAG, "Already responded"); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 291 | return true; |
| 292 | } |
| 293 | |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 294 | switch (keyCode) { |
| 295 | case KeyEvent.KEYCODE_BACK: |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 296 | CatLog.d(LOG_TAG, "onKeyDown - KEYCODE_BACK"); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 297 | sendResponse(StkAppService.RES_ID_BACKWARD, null, false); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 298 | return true; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 299 | } |
| 300 | return super.onKeyDown(keyCode, event); |
| 301 | } |
| 302 | |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 303 | void sendResponse(int resId) { |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 304 | sendResponse(resId, null, false); |
| 305 | } |
| 306 | |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 307 | void sendResponse(int resId, String input, boolean help) { |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 308 | cancelTimeOut(); |
| 309 | |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 310 | if (mSlotId == -1) { |
| 311 | CatLog.d(LOG_TAG, "slot id is invalid"); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | if (StkAppService.getInstance() == null) { |
| 316 | CatLog.d(LOG_TAG, "StkAppService is null, Ignore response: id is " + resId); |
| 317 | return; |
| 318 | } |
| 319 | |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 320 | if (mMoreOptions != null) { |
| 321 | mMoreOptions.setVisibility(View.INVISIBLE); |
| 322 | } |
| 323 | |
Cuihtlauac ALVARADO | bde7f03 | 2015-05-29 16:25:12 +0200 | [diff] [blame] | 324 | CatLog.d(LOG_TAG, "sendResponse resID[" + resId + "] input[*****] help[" |
| 325 | + help + "]"); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 326 | mIsResponseSent = true; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 327 | Bundle args = new Bundle(); |
| 328 | args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 329 | args.putInt(StkAppService.SLOT_ID, mSlotId); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 330 | args.putInt(StkAppService.RES_ID, resId); |
| 331 | if (input != null) { |
| 332 | args.putString(StkAppService.INPUT, input); |
| 333 | } |
| 334 | args.putBoolean(StkAppService.HELP, help); |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 335 | startService(new Intent(this, StkAppService.class).putExtras(args)); |
Yoshiaki Naka | 03a79d9 | 2018-08-09 18:41:31 +0900 | [diff] [blame^] | 336 | |
| 337 | // This instance should be set as a pending activity and finished by the service |
| 338 | if (resId != StkAppService.RES_ID_END_SESSION) { |
| 339 | setPendingState(true); |
| 340 | } |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public boolean onCreateOptionsMenu(android.view.Menu menu) { |
| 345 | super.onCreateOptionsMenu(menu); |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 346 | createOptionsMenuInternal(menu); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 347 | return true; |
| 348 | } |
| 349 | |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 350 | private void createOptionsMenuInternal(Menu menu) { |
| 351 | menu.add(Menu.NONE, StkApp.MENU_ID_END_SESSION, 1, R.string.menu_end_session); |
| 352 | menu.add(0, StkApp.MENU_ID_HELP, 2, R.string.help); |
| 353 | } |
| 354 | |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 355 | @Override |
| 356 | public boolean onPrepareOptionsMenu(android.view.Menu menu) { |
| 357 | super.onPrepareOptionsMenu(menu); |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 358 | prepareOptionsMenuInternal(menu); |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | private void prepareOptionsMenuInternal(Menu menu) { |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 363 | menu.findItem(StkApp.MENU_ID_END_SESSION).setVisible(true); |
| 364 | menu.findItem(StkApp.MENU_ID_HELP).setVisible(mStkInput.helpAvailable); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | @Override |
| 368 | public boolean onOptionsItemSelected(MenuItem item) { |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 369 | if (optionsItemSelectedInternal(item)) { |
| 370 | return true; |
| 371 | } |
| 372 | return super.onOptionsItemSelected(item); |
| 373 | } |
| 374 | |
| 375 | private boolean optionsItemSelectedInternal(MenuItem item) { |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 376 | if (mIsResponseSent) { |
| 377 | CatLog.d(LOG_TAG, "Already responded"); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 378 | return true; |
| 379 | } |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 380 | switch (item.getItemId()) { |
| 381 | case StkApp.MENU_ID_END_SESSION: |
| 382 | sendResponse(StkAppService.RES_ID_END_SESSION); |
| 383 | finish(); |
| 384 | return true; |
| 385 | case StkApp.MENU_ID_HELP: |
| 386 | sendResponse(StkAppService.RES_ID_INPUT, "", true); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 387 | return true; |
| 388 | } |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 389 | return false; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 392 | @Override |
| 393 | protected void onSaveInstanceState(Bundle outState) { |
| 394 | CatLog.d(LOG_TAG, "onSaveInstanceState: " + mSlotId); |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 395 | outState.putBoolean(RESPONSE_SENT_KEY, mIsResponseSent); |
| 396 | outState.putString(INPUT_STRING_KEY, mTextIn.getText().toString()); |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 397 | outState.putLong(ALARM_TIME_KEY, mAlarmTime); |
Yoshiaki Naka | 03a79d9 | 2018-08-09 18:41:31 +0900 | [diff] [blame^] | 398 | outState.putBoolean(PENDING, mIsPending); |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | @Override |
| 402 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 403 | CatLog.d(LOG_TAG, "onRestoreInstanceState: " + mSlotId); |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 404 | |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 405 | mIsResponseSent = savedInstanceState.getBoolean(RESPONSE_SENT_KEY); |
| 406 | if (mIsResponseSent && (mMoreOptions != null)) { |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 407 | mMoreOptions.setVisibility(View.INVISIBLE); |
| 408 | } |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 409 | |
Takanori Nakano | 1a672e6 | 2017-10-19 19:49:38 +0900 | [diff] [blame] | 410 | String savedString = savedInstanceState.getString(INPUT_STRING_KEY); |
| 411 | if (!TextUtils.isEmpty(savedString)) { |
| 412 | mTextIn.setText(savedString); |
| 413 | } |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 414 | |
| 415 | mAlarmTime = savedInstanceState.getLong(ALARM_TIME_KEY, NO_INPUT_ALARM); |
| 416 | if (mAlarmTime != NO_INPUT_ALARM) { |
| 417 | startTimeOut(); |
| 418 | } |
Yoshiaki Naka | 03a79d9 | 2018-08-09 18:41:31 +0900 | [diff] [blame^] | 419 | |
| 420 | if (!mIsResponseSent && !savedInstanceState.getBoolean(PENDING)) { |
| 421 | // If this is in the foreground and no response has been sent to the card, |
| 422 | // this must not be registered as pending activity by the previous instance. |
| 423 | // No need to renew nor clear pending activity in this case. |
| 424 | } else { |
| 425 | // Renew the instance of the pending activity. |
| 426 | setPendingState(true); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | private void setPendingState(boolean on) { |
| 431 | if (mIsPending != on) { |
| 432 | appService.getStkContext(mSlotId).setPendingActivityInstance(on ? this : null); |
| 433 | mIsPending = on; |
| 434 | } |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 435 | } |
| 436 | |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 437 | public void beforeTextChanged(CharSequence s, int start, int count, |
| 438 | int after) { |
| 439 | } |
| 440 | |
| 441 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 442 | // Reset timeout. |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 443 | cancelTimeOut(); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 444 | startTimeOut(); |
| 445 | } |
| 446 | |
| 447 | public void afterTextChanged(Editable s) { |
| 448 | } |
| 449 | |
| 450 | private boolean verfiyTypedText() { |
| 451 | // If not enough input was typed in stay on the edit screen. |
| 452 | if (mTextIn.getText().length() < mStkInput.minLen) { |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | private void cancelTimeOut() { |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 460 | if (mAlarmTime != NO_INPUT_ALARM) { |
| 461 | CatLog.d(LOG_TAG, "cancelTimeOut - slot id: " + mSlotId); |
| 462 | AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); |
| 463 | am.cancel(mAlarmListener); |
| 464 | mAlarmTime = NO_INPUT_ALARM; |
| 465 | } |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | private void startTimeOut() { |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 469 | // No need to set alarm if device sent TERMINAL RESPONSE already. |
| 470 | if (mIsResponseSent) { |
| 471 | return; |
Abhishek Adappa | c7c3a46 | 2013-02-14 13:36:57 -0800 | [diff] [blame] | 472 | } |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 473 | |
| 474 | if (mAlarmTime == NO_INPUT_ALARM) { |
| 475 | int duration = StkApp.calculateDurationInMilis(mStkInput.duration); |
| 476 | if (duration <= 0) { |
| 477 | duration = StkApp.UI_TIMEOUT; |
| 478 | } |
| 479 | mAlarmTime = SystemClock.elapsedRealtime() + duration; |
| 480 | } |
| 481 | |
| 482 | CatLog.d(LOG_TAG, "startTimeOut: " + mAlarmTime + "ms, slot id: " + mSlotId); |
| 483 | AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); |
| 484 | am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, mAlarmTime, INPUT_ALARM_TAG, |
| 485 | mAlarmListener, null); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | private void configInputDisplay() { |
| 489 | TextView numOfCharsView = (TextView) findViewById(R.id.num_of_chars); |
| 490 | TextView inTypeView = (TextView) findViewById(R.id.input_type); |
| 491 | |
| 492 | int inTypeId = R.string.alphabet; |
| 493 | |
| 494 | // set the prompt. |
Yoshiaki Naka | dd0dbb1 | 2017-08-03 21:20:38 +0900 | [diff] [blame] | 495 | if ((mStkInput.icon == null || !mStkInput.iconSelfExplanatory) |
| 496 | && !TextUtils.isEmpty(mStkInput.text)) { |
Takeshi Shinohara | c658900 | 2013-05-27 11:01:06 +0900 | [diff] [blame] | 497 | mPromptView.setText(mStkInput.text); |
Yoshiaki Naka | dd0dbb1 | 2017-08-03 21:20:38 +0900 | [diff] [blame] | 498 | mPromptView.setVisibility(View.VISIBLE); |
Takeshi Shinohara | c658900 | 2013-05-27 11:01:06 +0900 | [diff] [blame] | 499 | } |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 500 | |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 501 | // Set input type (alphabet/digit) info close to the InText form. |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 502 | if (mStkInput.digitOnly) { |
| 503 | mTextIn.setKeyListener(StkDigitsKeyListener.getInstance()); |
| 504 | inTypeId = R.string.digits; |
| 505 | } |
| 506 | inTypeView.setText(inTypeId); |
| 507 | |
Yoshiaki Naka | 28313ba | 2017-08-04 12:20:53 +0900 | [diff] [blame] | 508 | setTitle(R.string.app_name); |
Takanori Nakano | 4854435 | 2016-12-02 18:32:59 +0900 | [diff] [blame] | 509 | |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 510 | if (mStkInput.icon != null) { |
Yoshiaki Naka | dd0dbb1 | 2017-08-03 21:20:38 +0900 | [diff] [blame] | 511 | ImageView imageView = (ImageView) findViewById(R.id.icon); |
Takanori Nakano | 4854435 | 2016-12-02 18:32:59 +0900 | [diff] [blame] | 512 | imageView.setImageBitmap(mStkInput.icon); |
| 513 | imageView.setVisibility(View.VISIBLE); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // Handle specific global and text attributes. |
| 517 | switch (mState) { |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 518 | case STATE_TEXT: |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 519 | int maxLen = mStkInput.maxLen; |
| 520 | int minLen = mStkInput.minLen; |
| 521 | mTextIn.setFilters(new InputFilter[] {new InputFilter.LengthFilter( |
| 522 | maxLen)}); |
Wink Saville | 79085fc | 2009-06-09 10:27:23 -0700 | [diff] [blame] | 523 | |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 524 | // Set number of chars info. |
| 525 | String lengthLimit = String.valueOf(minLen); |
| 526 | if (maxLen != minLen) { |
| 527 | lengthLimit = minLen + " - " + maxLen; |
| 528 | } |
| 529 | numOfCharsView.setText(lengthLimit); |
| 530 | |
| 531 | if (!mStkInput.echo) { |
Yujing Gu | 6b9716c | 2014-04-23 13:50:50 +0800 | [diff] [blame] | 532 | mTextIn.setTransformationMethod(PasswordTransformationMethod |
| 533 | .getInstance()); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 534 | } |
Srikanth Chintala | a091b73 | 2016-02-04 14:46:43 +0530 | [diff] [blame] | 535 | mTextIn.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN); |
Kazuhiro Uto | 1287091 | 2018-07-18 17:43:56 +0900 | [diff] [blame] | 536 | // Request the initial focus on the edit box and show the software keyboard. |
| 537 | mTextIn.requestFocus(); |
| 538 | getWindow().setSoftInputMode( |
| 539 | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 540 | // Set default text if present. |
| 541 | if (mStkInput.defaultText != null) { |
| 542 | mTextIn.setText(mStkInput.defaultText); |
| 543 | } else { |
| 544 | // make sure the text is cleared |
| 545 | mTextIn.setText("", BufferType.EDITABLE); |
| 546 | } |
| 547 | |
| 548 | break; |
| 549 | case STATE_YES_NO: |
| 550 | // Set display mode - normal / yes-no layout |
| 551 | mYesNoLayout.setVisibility(View.VISIBLE); |
| 552 | mNormalLayout.setVisibility(View.GONE); |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | |
Wink Saville | e68857d | 2014-10-17 15:23:05 -0700 | [diff] [blame] | 557 | private void initFromIntent(Intent intent) { |
| 558 | // Get the calling intent type: text/key, and setup the |
| 559 | // display parameters. |
| 560 | CatLog.d(LOG_TAG, "initFromIntent - slot id: " + mSlotId); |
| 561 | if (intent != null) { |
| 562 | mStkInput = intent.getParcelableExtra("INPUT"); |
| 563 | mSlotId = intent.getIntExtra(StkAppService.SLOT_ID, -1); |
| 564 | CatLog.d(LOG_TAG, "onCreate - slot id: " + mSlotId); |
| 565 | if (mStkInput == null) { |
| 566 | finish(); |
| 567 | } else { |
| 568 | mState = mStkInput.yesNo ? STATE_YES_NO : |
| 569 | STATE_TEXT; |
| 570 | configInputDisplay(); |
| 571 | } |
| 572 | } else { |
| 573 | finish(); |
| 574 | } |
| 575 | } |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 576 | |
| 577 | private final AlarmManager.OnAlarmListener mAlarmListener = |
| 578 | new AlarmManager.OnAlarmListener() { |
| 579 | @Override |
| 580 | public void onAlarm() { |
| 581 | CatLog.d(LOG_TAG, "The alarm time is reached"); |
| 582 | mAlarmTime = NO_INPUT_ALARM; |
Yoshiaki Naka | a95f5c8 | 2016-09-02 11:27:05 +0900 | [diff] [blame] | 583 | sendResponse(StkAppService.RES_ID_TIMEOUT); |
| 584 | } |
| 585 | }; |
The Android Open Source Project | 9d9730a | 2009-03-03 19:32:37 -0800 | [diff] [blame] | 586 | } |