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