blob: a0ad40333fa034496b554c9febf8b969bab6a542 [file] [log] [blame]
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
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
Preeti Ahuja95919342013-10-01 18:18:55 -070019import android.app.ActivityManager;
20import android.app.ActivityManager.RunningTaskInfo;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -050021import android.app.AlertDialog;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080022import android.app.Notification;
fionaxu2c91c752017-04-21 18:11:57 -070023import android.app.NotificationChannel;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080024import android.app.NotificationManager;
25import android.app.PendingIntent;
26import android.app.Service;
Wink Savillee68857d2014-10-17 15:23:05 -070027import android.app.Activity;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080028import android.content.Context;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -050029import android.content.DialogInterface;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080030import android.content.Intent;
Preeti Ahuja95919342013-10-01 18:18:55 -070031import android.content.res.Configuration;
Srikanth Chintala89aa6602014-03-14 16:26:57 +053032import android.content.res.Resources;
33import android.content.res.Resources.NotFoundException;
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +010034import android.graphics.Bitmap;
35import android.graphics.BitmapFactory;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080036import android.net.Uri;
37import android.os.Bundle;
38import android.os.Handler;
39import android.os.IBinder;
40import android.os.Looper;
41import android.os.Message;
Sooraj Sasindrana4160472016-10-12 16:28:25 -070042import android.os.PersistableBundle;
Preeti Ahuja560be362014-11-25 19:38:24 -080043import android.os.PowerManager;
Preeti Ahujaa7cdca22013-10-01 18:20:56 -070044import android.os.SystemProperties;
Srikanth Chintala89aa6602014-03-14 16:26:57 +053045import android.os.Vibrator;
Preeti Ahuja95919342013-10-01 18:18:55 -070046import android.provider.Settings;
Sooraj Sasindrana4160472016-10-12 16:28:25 -070047import android.telephony.CarrierConfigManager;
Wink Saville56469d52009-04-02 01:37:03 -070048import android.telephony.TelephonyManager;
Preeti Ahujaa7cdca22013-10-01 18:20:56 -070049import android.text.TextUtils;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080050import android.view.Gravity;
51import android.view.LayoutInflater;
52import android.view.View;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -050053import android.view.WindowManager;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080054import android.widget.ImageView;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080055import android.widget.TextView;
56import android.widget.Toast;
Wink Savillee68857d2014-10-17 15:23:05 -070057import android.content.IntentFilter;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080058
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070059import com.android.internal.telephony.cat.AppInterface;
Preeti Ahuja95919342013-10-01 18:18:55 -070060import com.android.internal.telephony.cat.LaunchBrowserMode;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070061import com.android.internal.telephony.cat.Menu;
62import com.android.internal.telephony.cat.Item;
63import com.android.internal.telephony.cat.ResultCode;
64import com.android.internal.telephony.cat.CatCmdMessage;
65import com.android.internal.telephony.cat.CatCmdMessage.BrowserSettings;
Preeti Ahuja95919342013-10-01 18:18:55 -070066import com.android.internal.telephony.cat.CatCmdMessage.SetupEventListSettings;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070067import com.android.internal.telephony.cat.CatLog;
68import com.android.internal.telephony.cat.CatResponseMessage;
69import com.android.internal.telephony.cat.TextMessage;
Srikanth Chintala89aa6602014-03-14 16:26:57 +053070import com.android.internal.telephony.cat.ToneSettings;
Wink Saville94e982b2014-07-11 07:38:14 -070071import com.android.internal.telephony.uicc.IccRefreshResponse;
Wink Savillee68857d2014-10-17 15:23:05 -070072import com.android.internal.telephony.PhoneConstants;
Preeti Ahuja95919342013-10-01 18:18:55 -070073import com.android.internal.telephony.GsmAlphabet;
Legler Wuaeefef52014-10-27 00:57:18 +080074import com.android.internal.telephony.cat.CatService;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080075
Preeti Ahujaa7cdca22013-10-01 18:20:56 -070076import java.util.Iterator;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080077import java.util.LinkedList;
Wink Savillee68857d2014-10-17 15:23:05 -070078import java.lang.System;
79import java.util.List;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080080
Preeti Ahuja95919342013-10-01 18:18:55 -070081import static com.android.internal.telephony.cat.CatCmdMessage.
Preeti Ahuja560be362014-11-25 19:38:24 -080082 SetupEventListConstants.IDLE_SCREEN_AVAILABLE_EVENT;
83import static com.android.internal.telephony.cat.CatCmdMessage.
Preeti Ahuja95919342013-10-01 18:18:55 -070084 SetupEventListConstants.LANGUAGE_SELECTION_EVENT;
85
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080086/**
87 * SIM toolkit application level service. Interacts with Telephopny messages,
88 * application's launch and user input from STK UI elements.
Wink Saville79085fc2009-06-09 10:27:23 -070089 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080090 */
91public class StkAppService extends Service implements Runnable {
92
93 // members
Wink Savillee68857d2014-10-17 15:23:05 -070094 protected class StkContext {
95 protected CatCmdMessage mMainCmd = null;
96 protected CatCmdMessage mCurrentCmd = null;
97 protected CatCmdMessage mCurrentMenuCmd = null;
98 protected Menu mCurrentMenu = null;
99 protected String lastSelectedItem = null;
100 protected boolean mMenuIsVisible = false;
101 protected boolean mIsInputPending = false;
102 protected boolean mIsMenuPending = false;
103 protected boolean mIsDialogPending = false;
104 protected boolean responseNeeded = true;
105 protected boolean launchBrowser = false;
106 protected BrowserSettings mBrowserSettings = null;
107 protected LinkedList<DelayedCmd> mCmdsQ = null;
108 protected boolean mCmdInProgress = false;
109 protected int mStkServiceState = STATE_UNKNOWN;
110 protected int mSetupMenuState = STATE_UNKNOWN;
111 protected int mMenuState = StkMenuActivity.STATE_INIT;
112 protected int mOpCode = -1;
113 private Activity mActivityInstance = null;
114 private Activity mDialogInstance = null;
115 private Activity mMainActivityInstance = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700116 private int mSlotId = 0;
Preeti Ahuja95919342013-10-01 18:18:55 -0700117 private SetupEventListSettings mSetupEventListSettings = null;
118 private boolean mClearSelectItem = false;
119 private boolean mDisplayTextDlgIsVisibile = false;
120 private CatCmdMessage mCurrentSetupEventCmd = null;
Preeti Ahuja560be362014-11-25 19:38:24 -0800121 private CatCmdMessage mIdleModeTextCmd = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700122 final synchronized void setPendingActivityInstance(Activity act) {
123 CatLog.d(this, "setPendingActivityInstance act : " + mSlotId + ", " + act);
124 callSetActivityInstMsg(OP_SET_ACT_INST, mSlotId, act);
125 }
126 final synchronized Activity getPendingActivityInstance() {
127 CatLog.d(this, "getPendingActivityInstance act : " + mSlotId + ", " +
128 mActivityInstance);
129 return mActivityInstance;
130 }
131 final synchronized void setPendingDialogInstance(Activity act) {
132 CatLog.d(this, "setPendingDialogInstance act : " + mSlotId + ", " + act);
133 callSetActivityInstMsg(OP_SET_DAL_INST, mSlotId, act);
134 }
135 final synchronized Activity getPendingDialogInstance() {
136 CatLog.d(this, "getPendingDialogInstance act : " + mSlotId + ", " +
137 mDialogInstance);
138 return mDialogInstance;
139 }
140 final synchronized void setMainActivityInstance(Activity act) {
141 CatLog.d(this, "setMainActivityInstance act : " + mSlotId + ", " + act);
142 callSetActivityInstMsg(OP_SET_MAINACT_INST, mSlotId, act);
143 }
144 final synchronized Activity getMainActivityInstance() {
145 CatLog.d(this, "getMainActivityInstance act : " + mSlotId + ", " +
146 mMainActivityInstance);
147 return mMainActivityInstance;
148 }
149 }
150
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800151 private volatile Looper mServiceLooper;
152 private volatile ServiceHandler mServiceHandler;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800153 private Context mContext = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800154 private NotificationManager mNotificationManager = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800155 static StkAppService sInstance = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700156 private AppInterface[] mStkService = null;
157 private StkContext[] mStkContext = null;
158 private int mSimCount = 0;
Preeti Ahuja560be362014-11-25 19:38:24 -0800159 private PowerManager mPowerManager = null;
160 private StkCmdReceiver mStkCmdReceiver = null;
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530161 private TonePlayer mTonePlayer = null;
162 private Vibrator mVibrator = null;
Preeti Ahuja95919342013-10-01 18:18:55 -0700163
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800164 // Used for setting FLAG_ACTIVITY_NO_USER_ACTION when
165 // creating an intent.
166 private enum InitiatedByUserAction {
167 yes, // The action was started via a user initiated action
168 unknown, // Not known for sure if user initated the action
169 }
170
171 // constants
172 static final String OPCODE = "op";
173 static final String CMD_MSG = "cmd message";
174 static final String RES_ID = "response id";
175 static final String MENU_SELECTION = "menu selection";
176 static final String INPUT = "input";
177 static final String HELP = "help";
178 static final String CONFIRMATION = "confirm";
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500179 static final String CHOICE = "choice";
Wink Savillee68857d2014-10-17 15:23:05 -0700180 static final String SLOT_ID = "SLOT_ID";
181 static final String STK_CMD = "STK CMD";
182 static final String STK_DIALOG_URI = "stk://com.android.stk/dialog/";
183 static final String STK_MENU_URI = "stk://com.android.stk/menu/";
184 static final String STK_INPUT_URI = "stk://com.android.stk/input/";
185 static final String STK_TONE_URI = "stk://com.android.stk/tone/";
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530186 static final String FINISH_TONE_ACTIVITY_ACTION =
187 "android.intent.action.stk.finish_activity";
Preeti Ahuja95919342013-10-01 18:18:55 -0700188
189 // These below constants are used for SETUP_EVENT_LIST
190 static final String SETUP_EVENT_TYPE = "event";
191 static final String SETUP_EVENT_CAUSE = "cause";
192
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800193 // operations ids for different service functionality.
194 static final int OP_CMD = 1;
195 static final int OP_RESPONSE = 2;
196 static final int OP_LAUNCH_APP = 3;
197 static final int OP_END_SESSION = 4;
198 static final int OP_BOOT_COMPLETED = 5;
199 private static final int OP_DELAYED_MSG = 6;
Wink Saville94e982b2014-07-11 07:38:14 -0700200 static final int OP_CARD_STATUS_CHANGED = 7;
Wink Savillee68857d2014-10-17 15:23:05 -0700201 static final int OP_SET_ACT_INST = 8;
202 static final int OP_SET_DAL_INST = 9;
203 static final int OP_SET_MAINACT_INST = 10;
Preeti Ahujab3d0e612014-11-20 13:29:25 -0800204 static final int OP_LOCALE_CHANGED = 11;
205 static final int OP_ALPHA_NOTIFY = 12;
Preeti Ahuja560be362014-11-25 19:38:24 -0800206 static final int OP_IDLE_SCREEN = 13;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800207
Preeti Ahuja95919342013-10-01 18:18:55 -0700208 //Invalid SetupEvent
209 static final int INVALID_SETUP_EVENT = 0xFF;
210
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530211 // Message id to signal stop tone due to play tone timeout.
212 private static final int OP_STOP_TONE = 16;
213
214 // Message id to signal stop tone on user keyback.
215 static final int OP_STOP_TONE_USER = 17;
216
217 // Message id to remove stop tone message from queue.
218 private static final int STOP_TONE_WHAT = 100;
219
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800220 // Response ids
221 static final int RES_ID_MENU_SELECTION = 11;
222 static final int RES_ID_INPUT = 12;
223 static final int RES_ID_CONFIRM = 13;
224 static final int RES_ID_DONE = 14;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500225 static final int RES_ID_CHOICE = 15;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800226
227 static final int RES_ID_TIMEOUT = 20;
228 static final int RES_ID_BACKWARD = 21;
229 static final int RES_ID_END_SESSION = 22;
230 static final int RES_ID_EXIT = 23;
Srikanth Chintalaba103002015-11-30 10:49:52 -0800231 static final int RES_ID_ERROR = 24;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800232
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500233 static final int YES = 1;
234 static final int NO = 0;
235
Wink Savillee68857d2014-10-17 15:23:05 -0700236 static final int STATE_UNKNOWN = -1;
237 static final int STATE_NOT_EXIST = 0;
238 static final int STATE_EXIST = 1;
Wink Saville79085fc2009-06-09 10:27:23 -0700239
Wink Savillee68857d2014-10-17 15:23:05 -0700240 private static final String PACKAGE_NAME = "com.android.stk";
241 private static final String STK_MENU_ACTIVITY_NAME = PACKAGE_NAME + ".StkMenuActivity";
242 private static final String STK_INPUT_ACTIVITY_NAME = PACKAGE_NAME + ".StkInputActivity";
243 private static final String STK_DIALOG_ACTIVITY_NAME = PACKAGE_NAME + ".StkDialogActivity";
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800244 // Notification id used to display Idle Mode text in NotificationManager.
245 private static final int STK_NOTIFICATION_ID = 333;
fionaxu2c91c752017-04-21 18:11:57 -0700246 // Notification channel containing all mobile service messages notifications.
247 private static final String STK_NOTIFICATION_CHANNEL_ID = "mobileServiceMessages";
248
Wink Savillee68857d2014-10-17 15:23:05 -0700249 private static final String LOG_TAG = new Object(){}.getClass().getEnclosingClass().getName();
Wink Saville79085fc2009-06-09 10:27:23 -0700250
251 // Inner class used for queuing telephony messages (proactive commands,
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800252 // session end) while the service is busy processing a previous message.
253 private class DelayedCmd {
254 // members
255 int id;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700256 CatCmdMessage msg;
Wink Savillee68857d2014-10-17 15:23:05 -0700257 int slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800258
Wink Savillee68857d2014-10-17 15:23:05 -0700259 DelayedCmd(int id, CatCmdMessage msg, int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800260 this.id = id;
261 this.msg = msg;
Wink Savillee68857d2014-10-17 15:23:05 -0700262 this.slotId = slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800263 }
264 }
265
Preeti Ahujaa7cdca22013-10-01 18:20:56 -0700266 // system property to set the STK specific default url for launch browser proactive cmds
267 private static final String STK_BROWSER_DEFAULT_URL_SYSPROP = "persist.radio.stk.default_url";
268
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800269 @Override
270 public void onCreate() {
Wink Savillee68857d2014-10-17 15:23:05 -0700271 CatLog.d(LOG_TAG, "onCreate()+");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800272 // Initialize members
Wink Savillee68857d2014-10-17 15:23:05 -0700273 int i = 0;
274 mContext = getBaseContext();
275 mSimCount = TelephonyManager.from(mContext).getSimCount();
276 CatLog.d(LOG_TAG, "simCount: " + mSimCount);
277 mStkService = new AppInterface[mSimCount];
278 mStkContext = new StkContext[mSimCount];
Preeti Ahuja560be362014-11-25 19:38:24 -0800279 mPowerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
280 mStkCmdReceiver = new StkCmdReceiver();
281 registerReceiver(mStkCmdReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Wink Savillee68857d2014-10-17 15:23:05 -0700282 for (i = 0; i < mSimCount; i++) {
283 CatLog.d(LOG_TAG, "slotId: " + i);
Legler Wuaeefef52014-10-27 00:57:18 +0800284 mStkService[i] = CatService.getInstance(i);
Wink Savillee68857d2014-10-17 15:23:05 -0700285 mStkContext[i] = new StkContext();
286 mStkContext[i].mSlotId = i;
287 mStkContext[i].mCmdsQ = new LinkedList<DelayedCmd>();
288 }
289
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800290 Thread serviceThread = new Thread(null, this, "Stk App Service");
291 serviceThread.start();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800292 mNotificationManager = (NotificationManager) mContext
293 .getSystemService(Context.NOTIFICATION_SERVICE);
294 sInstance = this;
295 }
296
297 @Override
298 public void onStart(Intent intent, int startId) {
Wink Savillee68857d2014-10-17 15:23:05 -0700299 if (intent == null) {
300 CatLog.d(LOG_TAG, "StkAppService onStart intent is null so return");
Banavathu, Srinivas Naik87cda962012-07-17 15:32:44 +0530301 return;
302 }
303
Wink Savillee68857d2014-10-17 15:23:05 -0700304 Bundle args = intent.getExtras();
305 if (args == null) {
306 CatLog.d(LOG_TAG, "StkAppService onStart args is null so return");
307 return;
308 }
309
310 int op = args.getInt(OPCODE);
311 int slotId = 0;
312 int i = 0;
313 if (op != OP_BOOT_COMPLETED) {
314 slotId = args.getInt(SLOT_ID);
315 }
Cuihtlauac ALVARADObde7f032015-05-29 16:25:12 +0200316 CatLog.d(LOG_TAG, "onStart sim id: " + slotId + ", op: " + op + ", *****");
Wink Savillee68857d2014-10-17 15:23:05 -0700317 if ((slotId >= 0 && slotId < mSimCount) && mStkService[slotId] == null) {
Legler Wuaeefef52014-10-27 00:57:18 +0800318 mStkService[slotId] = CatService.getInstance(slotId);
Wink Savillee68857d2014-10-17 15:23:05 -0700319 if (mStkService[slotId] == null) {
320 CatLog.d(LOG_TAG, "mStkService is: " + mStkContext[slotId].mStkServiceState);
321 mStkContext[slotId].mStkServiceState = STATE_NOT_EXIST;
322 //Check other StkService state.
323 //If all StkServices are not available, stop itself and uninstall apk.
324 for (i = PhoneConstants.SIM_ID_1; i < mSimCount; i++) {
325 if (i != slotId
Tsukasa Goto029332b2016-10-05 17:12:06 +0900326 && (mStkService[i] != null)
Wink Savillee68857d2014-10-17 15:23:05 -0700327 && (mStkContext[i].mStkServiceState == STATE_UNKNOWN
328 || mStkContext[i].mStkServiceState == STATE_EXIST)) {
329 break;
330 }
331 }
332 } else {
333 mStkContext[slotId].mStkServiceState = STATE_EXIST;
334 }
335 if (i == mSimCount) {
336 stopSelf();
337 StkAppInstaller.unInstall(mContext);
338 return;
339 }
340 }
341
Banavathu, Srinivas Naik87cda962012-07-17 15:32:44 +0530342 waitForLooper();
John Wang62acae42009-10-08 11:20:23 -0700343
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800344 Message msg = mServiceHandler.obtainMessage();
Wink Savillee68857d2014-10-17 15:23:05 -0700345 msg.arg1 = op;
346 msg.arg2 = slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800347 switch(msg.arg1) {
348 case OP_CMD:
349 msg.obj = args.getParcelable(CMD_MSG);
350 break;
351 case OP_RESPONSE:
Wink Saville94e982b2014-07-11 07:38:14 -0700352 case OP_CARD_STATUS_CHANGED:
Preeti Ahuja95919342013-10-01 18:18:55 -0700353 case OP_LOCALE_CHANGED:
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +0530354 case OP_ALPHA_NOTIFY:
Preeti Ahuja560be362014-11-25 19:38:24 -0800355 case OP_IDLE_SCREEN:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800356 msg.obj = args;
357 /* falls through */
358 case OP_LAUNCH_APP:
359 case OP_END_SESSION:
360 case OP_BOOT_COMPLETED:
361 break;
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530362 case OP_STOP_TONE_USER:
363 msg.obj = args;
364 msg.what = STOP_TONE_WHAT;
365 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800366 default:
367 return;
368 }
369 mServiceHandler.sendMessage(msg);
370 }
371
372 @Override
373 public void onDestroy() {
Wink Savillee68857d2014-10-17 15:23:05 -0700374 CatLog.d(LOG_TAG, "onDestroy()");
Preeti Ahuja560be362014-11-25 19:38:24 -0800375 if (mStkCmdReceiver != null) {
376 unregisterReceiver(mStkCmdReceiver);
377 mStkCmdReceiver = null;
378 }
379 mPowerManager = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800380 waitForLooper();
381 mServiceLooper.quit();
382 }
383
384 @Override
385 public IBinder onBind(Intent intent) {
386 return null;
387 }
388
389 public void run() {
390 Looper.prepare();
391
392 mServiceLooper = Looper.myLooper();
393 mServiceHandler = new ServiceHandler();
394
395 Looper.loop();
396 }
397
398 /*
399 * Package api used by StkMenuActivity to indicate if its on the foreground.
400 */
Wink Savillee68857d2014-10-17 15:23:05 -0700401 void indicateMenuVisibility(boolean visibility, int slotId) {
402 if (slotId >= 0 && slotId < mSimCount) {
403 mStkContext[slotId].mMenuIsVisible = visibility;
404 }
405 }
406
Preeti Ahuja95919342013-10-01 18:18:55 -0700407 /*
408 * Package api used by StkDialogActivity to indicate if its on the foreground.
409 */
410 void setDisplayTextDlgVisibility(boolean visibility, int slotId) {
411 if (slotId >= 0 && slotId < mSimCount) {
412 mStkContext[slotId].mDisplayTextDlgIsVisibile = visibility;
413 }
414 }
415
Wink Savillee68857d2014-10-17 15:23:05 -0700416 boolean isInputPending(int slotId) {
417 if (slotId >= 0 && slotId < mSimCount) {
418 CatLog.d(LOG_TAG, "isInputFinishBySrv: " + mStkContext[slotId].mIsInputPending);
419 return mStkContext[slotId].mIsInputPending;
420 }
421 return false;
422 }
423
424 boolean isMenuPending(int slotId) {
425 if (slotId >= 0 && slotId < mSimCount) {
426 CatLog.d(LOG_TAG, "isMenuPending: " + mStkContext[slotId].mIsMenuPending);
427 return mStkContext[slotId].mIsMenuPending;
428 }
429 return false;
430 }
431
432 boolean isDialogPending(int slotId) {
433 if (slotId >= 0 && slotId < mSimCount) {
434 CatLog.d(LOG_TAG, "isDialogPending: " + mStkContext[slotId].mIsDialogPending);
435 return mStkContext[slotId].mIsDialogPending;
436 }
437 return false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800438 }
439
440 /*
441 * Package api used by StkMenuActivity to get its Menu parameter.
442 */
Wink Savillee68857d2014-10-17 15:23:05 -0700443 Menu getMenu(int slotId) {
444 CatLog.d(LOG_TAG, "StkAppService, getMenu, sim id: " + slotId);
445 if (slotId >=0 && slotId < mSimCount) {
446 return mStkContext[slotId].mCurrentMenu;
447 } else {
448 return null;
449 }
450 }
451
452 /*
453 * Package api used by StkMenuActivity to get its Main Menu parameter.
454 */
455 Menu getMainMenu(int slotId) {
456 CatLog.d(LOG_TAG, "StkAppService, getMainMenu, sim id: " + slotId);
w30234a08cffe2015-02-04 15:13:25 -0800457 if (slotId >=0 && slotId < mSimCount && (mStkContext[slotId].mMainCmd != null)) {
Wink Savillee68857d2014-10-17 15:23:05 -0700458 return mStkContext[slotId].mMainCmd.getMenu();
459 } else {
460 return null;
461 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800462 }
463
464 /*
465 * Package api used by UI Activities and Dialogs to communicate directly
466 * with the service to deliver state information and parameters.
467 */
468 static StkAppService getInstance() {
469 return sInstance;
470 }
471
472 private void waitForLooper() {
473 while (mServiceHandler == null) {
474 synchronized (this) {
475 try {
476 wait(100);
477 } catch (InterruptedException e) {
478 }
479 }
480 }
481 }
482
483 private final class ServiceHandler extends Handler {
484 @Override
485 public void handleMessage(Message msg) {
Wink Savillee68857d2014-10-17 15:23:05 -0700486 if(null == msg) {
487 CatLog.d(LOG_TAG, "ServiceHandler handleMessage msg is null");
488 return;
489 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800490 int opcode = msg.arg1;
Wink Savillee68857d2014-10-17 15:23:05 -0700491 int slotId = msg.arg2;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800492
Wink Savillee68857d2014-10-17 15:23:05 -0700493 CatLog.d(LOG_TAG, "handleMessage opcode[" + opcode + "], sim id[" + slotId + "]");
494 if (opcode == OP_CMD && msg.obj != null &&
495 ((CatCmdMessage)msg.obj).getCmdType()!= null) {
496 CatLog.d(LOG_TAG, "cmdName[" + ((CatCmdMessage)msg.obj).getCmdType().name() + "]");
497 }
498 mStkContext[slotId].mOpCode = opcode;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800499 switch (opcode) {
500 case OP_LAUNCH_APP:
Wink Savillee68857d2014-10-17 15:23:05 -0700501 if (mStkContext[slotId].mMainCmd == null) {
502 CatLog.d(LOG_TAG, "mMainCmd is null");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800503 // nothing todo when no SET UP MENU command didn't arrive.
504 return;
505 }
Wink Savillee68857d2014-10-17 15:23:05 -0700506 CatLog.d(LOG_TAG, "handleMessage OP_LAUNCH_APP - mCmdInProgress[" +
507 mStkContext[slotId].mCmdInProgress + "]");
508
509 //If there is a pending activity for the slot id,
510 //just finish it and create a new one to handle the pending command.
511 cleanUpInstanceStackBySlot(slotId);
512
Wink Savillee68857d2014-10-17 15:23:05 -0700513 CatLog.d(LOG_TAG, "Current cmd type: " +
514 mStkContext[slotId].mCurrentCmd.getCmdType());
515 //Restore the last command from stack by slot id.
516 restoreInstanceFromStackBySlot(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800517 break;
518 case OP_CMD:
Wink Savillee68857d2014-10-17 15:23:05 -0700519 CatLog.d(LOG_TAG, "[OP_CMD]");
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700520 CatCmdMessage cmdMsg = (CatCmdMessage) msg.obj;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800521 // There are two types of commands:
522 // 1. Interactive - user's response is required.
523 // 2. Informative - display a message, no interaction with the user.
524 //
Wink Saville79085fc2009-06-09 10:27:23 -0700525 // Informative commands can be handled immediately without any delay.
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800526 // Interactive commands can't override each other. So if a command
527 // is already in progress, we need to queue the next command until
528 // the user has responded or a timeout expired.
529 if (!isCmdInteractive(cmdMsg)) {
Wink Savillee68857d2014-10-17 15:23:05 -0700530 handleCmd(cmdMsg, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800531 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700532 if (!mStkContext[slotId].mCmdInProgress) {
533 mStkContext[slotId].mCmdInProgress = true;
534 handleCmd((CatCmdMessage) msg.obj, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800535 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700536 CatLog.d(LOG_TAG, "[Interactive][in progress]");
537 mStkContext[slotId].mCmdsQ.addLast(new DelayedCmd(OP_CMD,
538 (CatCmdMessage) msg.obj, slotId));
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800539 }
540 }
541 break;
542 case OP_RESPONSE:
Preeti Ahuja414bc412013-06-25 19:31:49 -0700543 handleCmdResponse((Bundle) msg.obj, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800544 // call delayed commands if needed.
Wink Savillee68857d2014-10-17 15:23:05 -0700545 if (mStkContext[slotId].mCmdsQ.size() != 0) {
546 callDelayedMsg(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800547 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700548 mStkContext[slotId].mCmdInProgress = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800549 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800550 break;
551 case OP_END_SESSION:
Wink Savillee68857d2014-10-17 15:23:05 -0700552 if (!mStkContext[slotId].mCmdInProgress) {
553 mStkContext[slotId].mCmdInProgress = true;
554 handleSessionEnd(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800555 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700556 mStkContext[slotId].mCmdsQ.addLast(
557 new DelayedCmd(OP_END_SESSION, null, slotId));
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800558 }
559 break;
560 case OP_BOOT_COMPLETED:
Wink Savillee68857d2014-10-17 15:23:05 -0700561 CatLog.d(LOG_TAG, " OP_BOOT_COMPLETED");
562 int i = 0;
563 for (i = PhoneConstants.SIM_ID_1; i < mSimCount; i++) {
564 if (mStkContext[i].mMainCmd != null) {
565 break;
566 }
567 }
568 if (i == mSimCount) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800569 StkAppInstaller.unInstall(mContext);
570 }
571 break;
572 case OP_DELAYED_MSG:
Wink Savillee68857d2014-10-17 15:23:05 -0700573 handleDelayedCmd(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800574 break;
Wink Saville94e982b2014-07-11 07:38:14 -0700575 case OP_CARD_STATUS_CHANGED:
Wink Savillee68857d2014-10-17 15:23:05 -0700576 CatLog.d(LOG_TAG, "Card/Icc Status change received");
577 handleCardStatusChangeAndIccRefresh((Bundle) msg.obj, slotId);
578 break;
579 case OP_SET_ACT_INST:
580 Activity act = new Activity();
581 act = (Activity) msg.obj;
582 CatLog.d(LOG_TAG, "Set activity instance. " + act);
583 mStkContext[slotId].mActivityInstance = act;
584 break;
585 case OP_SET_DAL_INST:
586 Activity dal = new Activity();
587 CatLog.d(LOG_TAG, "Set dialog instance. " + dal);
588 dal = (Activity) msg.obj;
589 mStkContext[slotId].mDialogInstance = dal;
590 break;
591 case OP_SET_MAINACT_INST:
592 Activity mainAct = new Activity();
593 mainAct = (Activity) msg.obj;
594 CatLog.d(LOG_TAG, "Set activity instance. " + mainAct);
595 mStkContext[slotId].mMainActivityInstance = mainAct;
Wink Saville94e982b2014-07-11 07:38:14 -0700596 break;
Preeti Ahuja95919342013-10-01 18:18:55 -0700597 case OP_LOCALE_CHANGED:
598 CatLog.d(this, "Locale Changed");
Yuta Ui1481b172016-02-03 15:34:31 +0900599 for (int slot = PhoneConstants.SIM_ID_1; slot < mSimCount; slot++) {
600 checkForSetupEvent(LANGUAGE_SELECTION_EVENT, (Bundle) msg.obj, slot);
601 }
fionaxu805eb572017-05-02 10:57:30 -0700602 // rename all registered notification channels on locale change
603 createAllChannels();
Preeti Ahuja95919342013-10-01 18:18:55 -0700604 break;
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +0530605 case OP_ALPHA_NOTIFY:
606 handleAlphaNotify((Bundle) msg.obj);
607 break;
Preeti Ahuja560be362014-11-25 19:38:24 -0800608 case OP_IDLE_SCREEN:
609 for (int slot = 0; slot < mSimCount; slot++) {
610 if (mStkContext[slot] != null) {
611 handleIdleScreen(slot);
612 }
613 }
614 break;
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530615 case OP_STOP_TONE_USER:
616 case OP_STOP_TONE:
617 CatLog.d(this, "Stop tone");
618 handleStopTone(msg, slotId);
619 break;
Wink Saville94e982b2014-07-11 07:38:14 -0700620 }
621 }
622
Wink Savillee68857d2014-10-17 15:23:05 -0700623 private void handleCardStatusChangeAndIccRefresh(Bundle args, int slotId) {
Wink Saville94e982b2014-07-11 07:38:14 -0700624 boolean cardStatus = args.getBoolean(AppInterface.CARD_STATUS);
625
Wink Savillee68857d2014-10-17 15:23:05 -0700626 CatLog.d(LOG_TAG, "CardStatus: " + cardStatus);
Wink Saville94e982b2014-07-11 07:38:14 -0700627 if (cardStatus == false) {
Wink Savillee68857d2014-10-17 15:23:05 -0700628 CatLog.d(LOG_TAG, "CARD is ABSENT");
Wink Saville94e982b2014-07-11 07:38:14 -0700629 // Uninstall STKAPP, Clear Idle text, Stop StkAppService
Wink Savillee68857d2014-10-17 15:23:05 -0700630 mNotificationManager.cancel(getNotificationId(slotId));
Srikanth Chintala7c5a04c2016-09-22 19:05:01 +0530631 mStkContext[slotId].mCurrentMenu = null;
632 mStkContext[slotId].mMainCmd = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700633 if (isAllOtherCardsAbsent(slotId)) {
634 CatLog.d(LOG_TAG, "All CARDs are ABSENT");
635 StkAppInstaller.unInstall(mContext);
636 stopSelf();
637 }
Wink Saville94e982b2014-07-11 07:38:14 -0700638 } else {
639 IccRefreshResponse state = new IccRefreshResponse();
640 state.refreshResult = args.getInt(AppInterface.REFRESH_RESULT);
641
Wink Savillee68857d2014-10-17 15:23:05 -0700642 CatLog.d(LOG_TAG, "Icc Refresh Result: "+ state.refreshResult);
Wink Saville94e982b2014-07-11 07:38:14 -0700643 if ((state.refreshResult == IccRefreshResponse.REFRESH_RESULT_INIT) ||
644 (state.refreshResult == IccRefreshResponse.REFRESH_RESULT_RESET)) {
645 // Clear Idle Text
Wink Savillee68857d2014-10-17 15:23:05 -0700646 mNotificationManager.cancel(getNotificationId(slotId));
Wink Saville94e982b2014-07-11 07:38:14 -0700647 }
648
649 if (state.refreshResult == IccRefreshResponse.REFRESH_RESULT_RESET) {
650 // Uninstall STkmenu
Wink Savillee68857d2014-10-17 15:23:05 -0700651 if (isAllOtherCardsAbsent(slotId)) {
652 StkAppInstaller.unInstall(mContext);
653 }
654 mStkContext[slotId].mCurrentMenu = null;
655 mStkContext[slotId].mMainCmd = null;
Wink Saville94e982b2014-07-11 07:38:14 -0700656 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800657 }
658 }
659 }
Wink Savillee68857d2014-10-17 15:23:05 -0700660 /*
661 * Check if all SIMs are absent except the id of slot equals "slotId".
662 */
663 private boolean isAllOtherCardsAbsent(int slotId) {
664 TelephonyManager mTm = (TelephonyManager) mContext.getSystemService(
665 Context.TELEPHONY_SERVICE);
666 int i = 0;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800667
Wink Savillee68857d2014-10-17 15:23:05 -0700668 for (i = 0; i < mSimCount; i++) {
669 if (i != slotId && mTm.hasIccCard(i)) {
670 break;
671 }
672 }
673 if (i == mSimCount) {
674 return true;
675 } else {
676 return false;
677 }
678 }
Preeti Ahuja95919342013-10-01 18:18:55 -0700679
Preeti Ahuja560be362014-11-25 19:38:24 -0800680 /*
681 * If the device is not in an interactive state, we can assume
682 * that the screen is idle.
683 */
684 private boolean isScreenIdle() {
685 return (!mPowerManager.isInteractive());
686 }
687
688 private void handleIdleScreen(int slotId) {
689
690 // If the idle screen event is present in the list need to send the
691 // response to SIM.
692 CatLog.d(this, "Need to send IDLE SCREEN Available event to SIM");
693 checkForSetupEvent(IDLE_SCREEN_AVAILABLE_EVENT, null, slotId);
694
695 if (mStkContext[slotId].mIdleModeTextCmd != null) {
696 launchIdleText(slotId);
697 }
698 }
699
700 private void sendScreenBusyResponse(int slotId) {
701 if (mStkContext[slotId].mCurrentCmd == null) {
702 return;
703 }
704 CatResponseMessage resMsg = new CatResponseMessage(mStkContext[slotId].mCurrentCmd);
705 CatLog.d(this, "SCREEN_BUSY");
706 resMsg.setResultCode(ResultCode.TERMINAL_CRNTLY_UNABLE_TO_PROCESS);
707 mStkService[slotId].onCmdResponse(resMsg);
708 if (mStkContext[slotId].mCmdsQ.size() != 0) {
709 callDelayedMsg(slotId);
710 } else {
711 mStkContext[slotId].mCmdInProgress = false;
712 }
713 }
714
Preeti Ahuja95919342013-10-01 18:18:55 -0700715 private void sendResponse(int resId, int slotId, boolean confirm) {
716 Message msg = mServiceHandler.obtainMessage();
717 msg.arg1 = OP_RESPONSE;
Takanori Nakano49b12722016-02-16 14:34:14 +0900718 msg.arg2 = slotId;
Preeti Ahuja95919342013-10-01 18:18:55 -0700719 Bundle args = new Bundle();
720 args.putInt(StkAppService.RES_ID, resId);
Preeti Ahuja95919342013-10-01 18:18:55 -0700721 args.putBoolean(StkAppService.CONFIRMATION, confirm);
722 msg.obj = args;
723 mServiceHandler.sendMessage(msg);
724 }
725
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700726 private boolean isCmdInteractive(CatCmdMessage cmd) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800727 switch (cmd.getCmdType()) {
728 case SEND_DTMF:
729 case SEND_SMS:
730 case SEND_SS:
731 case SEND_USSD:
732 case SET_UP_IDLE_MODE_TEXT:
733 case SET_UP_MENU:
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500734 case CLOSE_CHANNEL:
735 case RECEIVE_DATA:
736 case SEND_DATA:
Preeti Ahuja95919342013-10-01 18:18:55 -0700737 case SET_UP_EVENT_LIST:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800738 return false;
739 }
740
741 return true;
742 }
743
Wink Savillee68857d2014-10-17 15:23:05 -0700744 private void handleDelayedCmd(int slotId) {
745 CatLog.d(LOG_TAG, "handleDelayedCmd, slotId: " + slotId);
746 if (mStkContext[slotId].mCmdsQ.size() != 0) {
747 DelayedCmd cmd = mStkContext[slotId].mCmdsQ.poll();
748 if (cmd != null) {
749 CatLog.d(LOG_TAG, "handleDelayedCmd - queue size: " +
750 mStkContext[slotId].mCmdsQ.size() +
751 " id: " + cmd.id + "sim id: " + cmd.slotId);
752 switch (cmd.id) {
753 case OP_CMD:
754 handleCmd(cmd.msg, cmd.slotId);
755 break;
756 case OP_END_SESSION:
757 handleSessionEnd(cmd.slotId);
758 break;
759 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800760 }
761 }
762 }
763
Wink Savillee68857d2014-10-17 15:23:05 -0700764 private void callDelayedMsg(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800765 Message msg = mServiceHandler.obtainMessage();
766 msg.arg1 = OP_DELAYED_MSG;
Wink Savillee68857d2014-10-17 15:23:05 -0700767 msg.arg2 = slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800768 mServiceHandler.sendMessage(msg);
769 }
770
Wink Savillee68857d2014-10-17 15:23:05 -0700771 private void callSetActivityInstMsg(int inst_type, int slotId, Object obj) {
772 Message msg = mServiceHandler.obtainMessage();
773 msg.obj = obj;
774 msg.arg1 = inst_type;
775 msg.arg2 = slotId;
776 mServiceHandler.sendMessage(msg);
777 }
778
779 private void handleSessionEnd(int slotId) {
Legler Wuaeefef52014-10-27 00:57:18 +0800780 // We should finish all pending activity if receiving END SESSION command.
781 cleanUpInstanceStackBySlot(slotId);
782
Wink Savillee68857d2014-10-17 15:23:05 -0700783 mStkContext[slotId].mCurrentCmd = mStkContext[slotId].mMainCmd;
784 CatLog.d(LOG_TAG, "[handleSessionEnd] - mCurrentCmd changed to mMainCmd!.");
785 mStkContext[slotId].mCurrentMenuCmd = mStkContext[slotId].mMainCmd;
786 CatLog.d(LOG_TAG, "slotId: " + slotId + ", mMenuState: " +
787 mStkContext[slotId].mMenuState);
788
789 mStkContext[slotId].mIsInputPending = false;
790 mStkContext[slotId].mIsMenuPending = false;
791 mStkContext[slotId].mIsDialogPending = false;
792
Wink Savillee68857d2014-10-17 15:23:05 -0700793 if (mStkContext[slotId].mMainCmd == null) {
794 CatLog.d(LOG_TAG, "[handleSessionEnd][mMainCmd is null!]");
795 }
796 mStkContext[slotId].lastSelectedItem = null;
Wink Saville79085fc2009-06-09 10:27:23 -0700797 // In case of SET UP MENU command which removed the app, don't
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800798 // update the current menu member.
Wink Savillee68857d2014-10-17 15:23:05 -0700799 if (mStkContext[slotId].mCurrentMenu != null && mStkContext[slotId].mMainCmd != null) {
800 mStkContext[slotId].mCurrentMenu = mStkContext[slotId].mMainCmd.getMenu();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800801 }
Wink Savillee68857d2014-10-17 15:23:05 -0700802 CatLog.d(LOG_TAG, "[handleSessionEnd][mMenuState]" + mStkContext[slotId].mMenuIsVisible);
803 // In mutiple instance architecture, the main menu for slotId will be finished when user
804 // goes to the Stk menu of the other SIM. So, we should launch a new instance for the
805 // main menu if the main menu instance has been finished.
806 // If the current menu is secondary menu, we should launch main menu.
807 if (StkMenuActivity.STATE_SECONDARY == mStkContext[slotId].mMenuState) {
808 launchMenuActivity(null, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800809 }
Wink Savillee68857d2014-10-17 15:23:05 -0700810 if (mStkContext[slotId].mCmdsQ.size() != 0) {
811 callDelayedMsg(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800812 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700813 mStkContext[slotId].mCmdInProgress = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800814 }
815 // In case a launch browser command was just confirmed, launch that url.
Wink Savillee68857d2014-10-17 15:23:05 -0700816 if (mStkContext[slotId].launchBrowser) {
817 mStkContext[slotId].launchBrowser = false;
818 launchBrowser(mStkContext[slotId].mBrowserSettings);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800819 }
820 }
821
Preeti Ahuja560be362014-11-25 19:38:24 -0800822 // returns true if any Stk related activity already has focus on the screen
823 private boolean isTopOfStack() {
pkanwareab12f32017-02-06 08:35:03 -0800824 ActivityManager mActivityManager = (ActivityManager) mContext
Preeti Ahuja560be362014-11-25 19:38:24 -0800825 .getSystemService(ACTIVITY_SERVICE);
pkanwareab12f32017-02-06 08:35:03 -0800826 String currentPackageName = null;
827 List<RunningTaskInfo> tasks = mActivityManager.getRunningTasks(1);
828 if (tasks == null || tasks.get(0).topActivity == null) {
829 return false;
830 }
831 currentPackageName = tasks.get(0).topActivity.getPackageName();
Preeti Ahuja560be362014-11-25 19:38:24 -0800832 if (null != currentPackageName) {
833 return currentPackageName.equals(PACKAGE_NAME);
834 }
Preeti Ahuja560be362014-11-25 19:38:24 -0800835 return false;
836 }
837
Sooraj Sasindrana4160472016-10-12 16:28:25 -0700838 /**
839 * Get the boolean config from carrier config manager.
840 *
841 * @param context the context to get carrier service
842 * @param key config key defined in CarrierConfigManager
843 * @return boolean value of corresponding key.
844 */
845 private static boolean getBooleanCarrierConfig(Context context, String key) {
846 CarrierConfigManager configManager = (CarrierConfigManager) context.getSystemService(
847 Context.CARRIER_CONFIG_SERVICE);
848 PersistableBundle b = null;
849 if (configManager != null) {
850 b = configManager.getConfig();
851 }
852 if (b != null) {
853 return b.getBoolean(key);
854 } else {
855 // Return static default defined in CarrierConfigManager.
856 return CarrierConfigManager.getDefaultConfig().getBoolean(key);
857 }
858 }
859
Wink Savillee68857d2014-10-17 15:23:05 -0700860 private void handleCmd(CatCmdMessage cmdMsg, int slotId) {
Preeti Ahuja95919342013-10-01 18:18:55 -0700861
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800862 if (cmdMsg == null) {
863 return;
864 }
865 // save local reference for state tracking.
Wink Savillee68857d2014-10-17 15:23:05 -0700866 mStkContext[slotId].mCurrentCmd = cmdMsg;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800867 boolean waitForUsersResponse = true;
868
Wink Savillee68857d2014-10-17 15:23:05 -0700869 mStkContext[slotId].mIsInputPending = false;
870 mStkContext[slotId].mIsMenuPending = false;
871 mStkContext[slotId].mIsDialogPending = false;
872
873 CatLog.d(LOG_TAG,"[handleCmd]" + cmdMsg.getCmdType().name());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800874 switch (cmdMsg.getCmdType()) {
875 case DISPLAY_TEXT:
876 TextMessage msg = cmdMsg.geTextMessage();
Jeevaka Badrappan854a25c2012-12-01 16:32:03 +0200877 waitForUsersResponse = msg.responseNeeded;
Wink Savillee68857d2014-10-17 15:23:05 -0700878 if (mStkContext[slotId].lastSelectedItem != null) {
879 msg.title = mStkContext[slotId].lastSelectedItem;
880 } else if (mStkContext[slotId].mMainCmd != null){
Yoshiaki Naka28313ba2017-08-04 12:20:53 +0900881 if (!getResources().getBoolean(R.bool.show_menu_title_only_on_menu)) {
882 msg.title = mStkContext[slotId].mMainCmd.getMenu().title;
883 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800884 }
Preeti Ahuja560be362014-11-25 19:38:24 -0800885 //If we receive a low priority Display Text and the device is
886 // not displaying any STK related activity and the screen is not idle
887 // ( that is, device is in an interactive state), then send a screen busy
888 // terminal response. Otherwise display the message. The existing
889 // displayed message shall be updated with the new display text
890 // proactive command (Refer to ETSI TS 102 384 section 27.22.4.1.4.4.2).
891 if (!(msg.isHighPriority || mStkContext[slotId].mMenuIsVisible
892 || mStkContext[slotId].mDisplayTextDlgIsVisibile || isTopOfStack())) {
893 if(!isScreenIdle()) {
894 CatLog.d(LOG_TAG, "Screen is not idle");
895 sendScreenBusyResponse(slotId);
896 } else {
897 launchTextDialog(slotId);
898 }
899 } else {
900 launchTextDialog(slotId);
901 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800902 break;
903 case SELECT_ITEM:
Wink Savillee68857d2014-10-17 15:23:05 -0700904 CatLog.d(LOG_TAG, "SELECT_ITEM +");
905 mStkContext[slotId].mCurrentMenuCmd = mStkContext[slotId].mCurrentCmd;
906 mStkContext[slotId].mCurrentMenu = cmdMsg.getMenu();
907 launchMenuActivity(cmdMsg.getMenu(), slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800908 break;
909 case SET_UP_MENU:
Wink Savillee68857d2014-10-17 15:23:05 -0700910 mStkContext[slotId].mCmdInProgress = false;
911 mStkContext[slotId].mMainCmd = mStkContext[slotId].mCurrentCmd;
912 mStkContext[slotId].mCurrentMenuCmd = mStkContext[slotId].mCurrentCmd;
913 mStkContext[slotId].mCurrentMenu = cmdMsg.getMenu();
914 CatLog.d(LOG_TAG, "SET_UP_MENU [" + removeMenu(slotId) + "]");
915
916 if (removeMenu(slotId)) {
917 int i = 0;
918 CatLog.d(LOG_TAG, "removeMenu() - Uninstall App");
919 mStkContext[slotId].mCurrentMenu = null;
Preeti Ahuja95919342013-10-01 18:18:55 -0700920 mStkContext[slotId].mMainCmd = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700921 //Check other setup menu state. If all setup menu are removed, uninstall apk.
922 for (i = PhoneConstants.SIM_ID_1; i < mSimCount; i++) {
923 if (i != slotId
Ryuto Sawadaf51e5e02016-11-25 17:12:49 +0900924 && (mStkContext[i].mSetupMenuState == STATE_UNKNOWN
925 || mStkContext[i].mSetupMenuState == STATE_EXIST)) {
Wink Savillee68857d2014-10-17 15:23:05 -0700926 CatLog.d(LOG_TAG, "Not Uninstall App:" + i + ","
Ryuto Sawadaf51e5e02016-11-25 17:12:49 +0900927 + mStkContext[i].mSetupMenuState);
Wink Savillee68857d2014-10-17 15:23:05 -0700928 break;
929 }
930 }
931 if (i == mSimCount) {
932 StkAppInstaller.unInstall(mContext);
933 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800934 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700935 CatLog.d(LOG_TAG, "install App");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800936 StkAppInstaller.install(mContext);
937 }
Wink Savillee68857d2014-10-17 15:23:05 -0700938 if (mStkContext[slotId].mMenuIsVisible) {
939 launchMenuActivity(null, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800940 }
941 break;
942 case GET_INPUT:
943 case GET_INKEY:
Wink Savillee68857d2014-10-17 15:23:05 -0700944 launchInputActivity(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800945 break;
946 case SET_UP_IDLE_MODE_TEXT:
947 waitForUsersResponse = false;
Preeti Ahuja560be362014-11-25 19:38:24 -0800948 mStkContext[slotId].mIdleModeTextCmd = mStkContext[slotId].mCurrentCmd;
949 TextMessage idleModeText = mStkContext[slotId].mCurrentCmd.geTextMessage();
950 if (idleModeText == null) {
951 launchIdleText(slotId);
952 mStkContext[slotId].mIdleModeTextCmd = null;
953 }
954 mStkContext[slotId].mCurrentCmd = mStkContext[slotId].mMainCmd;
955 if ((mStkContext[slotId].mIdleModeTextCmd != null) && isScreenIdle()) {
956 CatLog.d(this, "set up idle mode");
957 launchIdleText(slotId);
958 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800959 break;
960 case SEND_DTMF:
961 case SEND_SMS:
962 case SEND_SS:
963 case SEND_USSD:
Preeti Ahuja95919342013-10-01 18:18:55 -0700964 case GET_CHANNEL_STATUS:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800965 waitForUsersResponse = false;
Wink Savillee68857d2014-10-17 15:23:05 -0700966 launchEventMessage(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800967 break;
968 case LAUNCH_BROWSER:
Ryuto Sawada0bdc7192016-04-18 12:10:56 +0900969 // The device setup process should not be interrupted by launching browser.
970 if (Settings.Global.getInt(mContext.getContentResolver(),
971 Settings.Global.DEVICE_PROVISIONED, 0) == 0) {
972 CatLog.d(this, "The command is not performed if the setup has not been completed.");
973 sendScreenBusyResponse(slotId);
974 break;
975 }
Sooraj Sasindrana4160472016-10-12 16:28:25 -0700976
977 /* Check if Carrier would not want to launch browser */
978 if (getBooleanCarrierConfig(mContext,
979 CarrierConfigManager.KEY_STK_DISABLE_LAUNCH_BROWSER_BOOL)) {
980 CatLog.d(this, "Browser is not launched as per carrier.");
981 sendResponse(RES_ID_DONE, slotId, true);
982 break;
983 }
984
Srikanth Chintalaba103002015-11-30 10:49:52 -0800985 mStkContext[slotId].mBrowserSettings =
986 mStkContext[slotId].mCurrentCmd.getBrowserSettings();
987 if (!isUrlAvailableToLaunchBrowser(mStkContext[slotId].mBrowserSettings)) {
988 CatLog.d(this, "Browser url property is not set - send error");
989 sendResponse(RES_ID_ERROR, slotId, true);
Preeti Ahujaa7cdca22013-10-01 18:20:56 -0700990 } else {
Srikanth Chintalaba103002015-11-30 10:49:52 -0800991 TextMessage alphaId = mStkContext[slotId].mCurrentCmd.geTextMessage();
992 if ((alphaId == null) || TextUtils.isEmpty(alphaId.text)) {
993 // don't need user confirmation in this case
994 // just launch the browser or spawn a new tab
995 CatLog.d(this, "user confirmation is not currently needed.\n" +
996 "supressing confirmation dialogue and confirming silently...");
997 mStkContext[slotId].launchBrowser = true;
998 sendResponse(RES_ID_CONFIRM, slotId, true);
999 } else {
1000 launchConfirmationDialog(alphaId, slotId);
1001 }
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001002 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001003 break;
1004 case SET_UP_CALL:
Preeti Ahujadd240102013-08-30 17:25:06 -07001005 TextMessage mesg = mStkContext[slotId].mCurrentCmd.getCallSettings().confirmMsg;
1006 if((mesg != null) && (mesg.text == null || mesg.text.length() == 0)) {
1007 mesg.text = getResources().getString(R.string.default_setup_call_msg);
1008 }
1009 CatLog.d(this, "SET_UP_CALL mesg.text " + mesg.text);
1010 launchConfirmationDialog(mesg, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001011 break;
1012 case PLAY_TONE:
Srikanth Chintala89aa6602014-03-14 16:26:57 +05301013 handlePlayTone(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001014 break;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001015 case OPEN_CHANNEL:
Wink Savillee68857d2014-10-17 15:23:05 -07001016 launchOpenChannelDialog(slotId);
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001017 break;
1018 case CLOSE_CHANNEL:
1019 case RECEIVE_DATA:
1020 case SEND_DATA:
Wink Savillee68857d2014-10-17 15:23:05 -07001021 TextMessage m = mStkContext[slotId].mCurrentCmd.geTextMessage();
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001022
1023 if ((m != null) && (m.text == null)) {
1024 switch(cmdMsg.getCmdType()) {
1025 case CLOSE_CHANNEL:
1026 m.text = getResources().getString(R.string.default_close_channel_msg);
1027 break;
1028 case RECEIVE_DATA:
1029 m.text = getResources().getString(R.string.default_receive_data_msg);
1030 break;
1031 case SEND_DATA:
1032 m.text = getResources().getString(R.string.default_send_data_msg);
1033 break;
1034 }
1035 }
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001036 /*
1037 * Display indication in the form of a toast to the user if required.
1038 */
Wink Savillee68857d2014-10-17 15:23:05 -07001039 launchEventMessage(slotId);
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001040 break;
Preeti Ahuja95919342013-10-01 18:18:55 -07001041 case SET_UP_EVENT_LIST:
1042 mStkContext[slotId].mSetupEventListSettings =
1043 mStkContext[slotId].mCurrentCmd.getSetEventList();
1044 mStkContext[slotId].mCurrentSetupEventCmd = mStkContext[slotId].mCurrentCmd;
1045 mStkContext[slotId].mCurrentCmd = mStkContext[slotId].mMainCmd;
Preeti Ahuja560be362014-11-25 19:38:24 -08001046 if (isScreenIdle()) {
1047 CatLog.d(this," Check if IDLE_SCREEN_AVAILABLE_EVENT is present in List");
1048 checkForSetupEvent(IDLE_SCREEN_AVAILABLE_EVENT, null, slotId);
1049 }
Preeti Ahuja95919342013-10-01 18:18:55 -07001050 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001051 }
1052
1053 if (!waitForUsersResponse) {
Wink Savillee68857d2014-10-17 15:23:05 -07001054 if (mStkContext[slotId].mCmdsQ.size() != 0) {
1055 callDelayedMsg(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001056 } else {
Wink Savillee68857d2014-10-17 15:23:05 -07001057 mStkContext[slotId].mCmdInProgress = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001058 }
1059 }
1060 }
1061
Wink Savillee68857d2014-10-17 15:23:05 -07001062 private void handleCmdResponse(Bundle args, int slotId) {
1063 CatLog.d(LOG_TAG, "handleCmdResponse, sim id: " + slotId);
1064 if (mStkContext[slotId].mCurrentCmd == null) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001065 return;
1066 }
Wink Savillee68857d2014-10-17 15:23:05 -07001067
1068 if (mStkService[slotId] == null) {
Legler Wuaeefef52014-10-27 00:57:18 +08001069 mStkService[slotId] = CatService.getInstance(slotId);
Wink Savillee68857d2014-10-17 15:23:05 -07001070 if (mStkService[slotId] == null) {
Alex Yakavenkad8e2ecd2012-04-20 17:10:15 -07001071 // This should never happen (we should be responding only to a message
1072 // that arrived from StkService). It has to exist by this time
Wink Savillee68857d2014-10-17 15:23:05 -07001073 CatLog.d(LOG_TAG, "Exception! mStkService is null when we need to send response.");
Alex Yakavenkad8e2ecd2012-04-20 17:10:15 -07001074 throw new RuntimeException("mStkService is null when we need to send response");
1075 }
1076 }
1077
Wink Savillee68857d2014-10-17 15:23:05 -07001078 CatResponseMessage resMsg = new CatResponseMessage(mStkContext[slotId].mCurrentCmd);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001079
1080 // set result code
1081 boolean helpRequired = args.getBoolean(HELP, false);
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001082 boolean confirmed = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001083
1084 switch(args.getInt(RES_ID)) {
1085 case RES_ID_MENU_SELECTION:
Wink Savillee68857d2014-10-17 15:23:05 -07001086 CatLog.d(LOG_TAG, "MENU_SELECTION=" + mStkContext[slotId].
1087 mCurrentMenuCmd.getCmdType());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001088 int menuSelection = args.getInt(MENU_SELECTION);
Wink Savillee68857d2014-10-17 15:23:05 -07001089 switch(mStkContext[slotId].mCurrentMenuCmd.getCmdType()) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001090 case SET_UP_MENU:
1091 case SELECT_ITEM:
Wink Savillee68857d2014-10-17 15:23:05 -07001092 mStkContext[slotId].lastSelectedItem = getItemName(menuSelection, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001093 if (helpRequired) {
1094 resMsg.setResultCode(ResultCode.HELP_INFO_REQUIRED);
1095 } else {
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301096 resMsg.setResultCode(mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ?
1097 ResultCode.PRFRMD_ICON_NOT_DISPLAYED : ResultCode.OK);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001098 }
1099 resMsg.setMenuSelection(menuSelection);
1100 break;
1101 }
1102 break;
1103 case RES_ID_INPUT:
Wink Savillee68857d2014-10-17 15:23:05 -07001104 CatLog.d(LOG_TAG, "RES_ID_INPUT");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001105 String input = args.getString(INPUT);
Wink Savillee68857d2014-10-17 15:23:05 -07001106 if (input != null && (null != mStkContext[slotId].mCurrentCmd.geInput()) &&
1107 (mStkContext[slotId].mCurrentCmd.geInput().yesNo)) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001108 boolean yesNoSelection = input
1109 .equals(StkInputActivity.YES_STR_RESPONSE);
1110 resMsg.setYesNo(yesNoSelection);
1111 } else {
1112 if (helpRequired) {
1113 resMsg.setResultCode(ResultCode.HELP_INFO_REQUIRED);
1114 } else {
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301115 resMsg.setResultCode(mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ?
1116 ResultCode.PRFRMD_ICON_NOT_DISPLAYED : ResultCode.OK);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001117 resMsg.setInput(input);
1118 }
1119 }
1120 break;
1121 case RES_ID_CONFIRM:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -07001122 CatLog.d(this, "RES_ID_CONFIRM");
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001123 confirmed = args.getBoolean(CONFIRMATION);
Wink Savillee68857d2014-10-17 15:23:05 -07001124 switch (mStkContext[slotId].mCurrentCmd.getCmdType()) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001125 case DISPLAY_TEXT:
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301126 if (confirmed) {
1127 resMsg.setResultCode(mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ?
1128 ResultCode.PRFRMD_ICON_NOT_DISPLAYED : ResultCode.OK);
1129 } else {
1130 resMsg.setResultCode(ResultCode.UICC_SESSION_TERM_BY_USER);
1131 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001132 break;
1133 case LAUNCH_BROWSER:
1134 resMsg.setResultCode(confirmed ? ResultCode.OK
1135 : ResultCode.UICC_SESSION_TERM_BY_USER);
1136 if (confirmed) {
Wink Savillee68857d2014-10-17 15:23:05 -07001137 mStkContext[slotId].launchBrowser = true;
1138 mStkContext[slotId].mBrowserSettings =
1139 mStkContext[slotId].mCurrentCmd.getBrowserSettings();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001140 }
1141 break;
1142 case SET_UP_CALL:
1143 resMsg.setResultCode(ResultCode.OK);
1144 resMsg.setConfirmation(confirmed);
1145 if (confirmed) {
Wink Savillee68857d2014-10-17 15:23:05 -07001146 launchEventMessage(slotId,
1147 mStkContext[slotId].mCurrentCmd.getCallSettings().callMsg);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001148 }
1149 break;
1150 }
1151 break;
1152 case RES_ID_DONE:
1153 resMsg.setResultCode(ResultCode.OK);
1154 break;
1155 case RES_ID_BACKWARD:
Wink Savillee68857d2014-10-17 15:23:05 -07001156 CatLog.d(LOG_TAG, "RES_ID_BACKWARD");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001157 resMsg.setResultCode(ResultCode.BACKWARD_MOVE_BY_USER);
1158 break;
1159 case RES_ID_END_SESSION:
Wink Savillee68857d2014-10-17 15:23:05 -07001160 CatLog.d(LOG_TAG, "RES_ID_END_SESSION");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001161 resMsg.setResultCode(ResultCode.UICC_SESSION_TERM_BY_USER);
1162 break;
1163 case RES_ID_TIMEOUT:
Wink Savillee68857d2014-10-17 15:23:05 -07001164 CatLog.d(LOG_TAG, "RES_ID_TIMEOUT");
Naveen Kallad5176892009-11-30 12:45:29 -08001165 // GCF test-case 27.22.4.1.1 Expected Sequence 1.5 (DISPLAY TEXT,
1166 // Clear message after delay, successful) expects result code OK.
1167 // If the command qualifier specifies no user response is required
1168 // then send OK instead of NO_RESPONSE_FROM_USER
Wink Savillee68857d2014-10-17 15:23:05 -07001169 if ((mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1170 AppInterface.CommandType.DISPLAY_TEXT.value())
1171 && (mStkContext[slotId].mCurrentCmd.geTextMessage().userClear == false)) {
Naveen Kallad5176892009-11-30 12:45:29 -08001172 resMsg.setResultCode(ResultCode.OK);
1173 } else {
1174 resMsg.setResultCode(ResultCode.NO_RESPONSE_FROM_USER);
1175 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001176 break;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001177 case RES_ID_CHOICE:
1178 int choice = args.getInt(CHOICE);
1179 CatLog.d(this, "User Choice=" + choice);
1180 switch (choice) {
1181 case YES:
1182 resMsg.setResultCode(ResultCode.OK);
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001183 confirmed = true;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001184 break;
1185 case NO:
1186 resMsg.setResultCode(ResultCode.USER_NOT_ACCEPT);
1187 break;
1188 }
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001189
Wink Savillee68857d2014-10-17 15:23:05 -07001190 if (mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1191 AppInterface.CommandType.OPEN_CHANNEL.value()) {
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001192 resMsg.setConfirmation(confirmed);
1193 }
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001194 break;
Srikanth Chintalaba103002015-11-30 10:49:52 -08001195 case RES_ID_ERROR:
1196 CatLog.d(LOG_TAG, "RES_ID_ERROR");
1197 switch (mStkContext[slotId].mCurrentCmd.getCmdType()) {
1198 case LAUNCH_BROWSER:
1199 resMsg.setResultCode(ResultCode.LAUNCH_BROWSER_ERROR);
1200 break;
1201 }
1202 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001203 default:
Wink Savillee68857d2014-10-17 15:23:05 -07001204 CatLog.d(LOG_TAG, "Unknown result id");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001205 return;
1206 }
Wink Savillee68857d2014-10-17 15:23:05 -07001207
1208 if (null != mStkContext[slotId].mCurrentCmd &&
1209 null != mStkContext[slotId].mCurrentCmd.getCmdType()) {
1210 CatLog.d(LOG_TAG, "handleCmdResponse- cmdName[" +
1211 mStkContext[slotId].mCurrentCmd.getCmdType().name() + "]");
1212 }
1213 mStkService[slotId].onCmdResponse(resMsg);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001214 }
1215
1216 /**
1217 * Returns 0 or FLAG_ACTIVITY_NO_USER_ACTION, 0 means the user initiated the action.
1218 *
1219 * @param userAction If the userAction is yes then we always return 0 otherwise
1220 * mMenuIsVisible is used to determine what to return. If mMenuIsVisible is true
1221 * then we are the foreground app and we'll return 0 as from our perspective a
1222 * user action did cause. If it's false than we aren't the foreground app and
1223 * FLAG_ACTIVITY_NO_USER_ACTION is returned.
Wink Saville79085fc2009-06-09 10:27:23 -07001224 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001225 * @return 0 or FLAG_ACTIVITY_NO_USER_ACTION
1226 */
Wink Savillee68857d2014-10-17 15:23:05 -07001227 private int getFlagActivityNoUserAction(InitiatedByUserAction userAction, int slotId) {
1228 return ((userAction == InitiatedByUserAction.yes) | mStkContext[slotId].mMenuIsVisible)
1229 ? 0 : Intent.FLAG_ACTIVITY_NO_USER_ACTION;
1230 }
1231 /**
1232 * This method is used for cleaning up pending instances in stack.
1233 */
1234 private void cleanUpInstanceStackBySlot(int slotId) {
1235 Activity activity = mStkContext[slotId].getPendingActivityInstance();
1236 Activity dialog = mStkContext[slotId].getPendingDialogInstance();
1237 CatLog.d(LOG_TAG, "cleanUpInstanceStackBySlot slotId: " + slotId);
Legler Wuaeefef52014-10-27 00:57:18 +08001238 if (mStkContext[slotId].mCurrentCmd == null) {
1239 CatLog.d(LOG_TAG, "current cmd is null.");
1240 return;
1241 }
Wink Savillee68857d2014-10-17 15:23:05 -07001242 if (activity != null) {
1243 CatLog.d(LOG_TAG, "current cmd type: " +
1244 mStkContext[slotId].mCurrentCmd.getCmdType());
1245 if (mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1246 AppInterface.CommandType.GET_INPUT.value() ||
1247 mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1248 AppInterface.CommandType.GET_INKEY.value()) {
1249 mStkContext[slotId].mIsInputPending = true;
1250 } else if (mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1251 AppInterface.CommandType.SET_UP_MENU.value() ||
1252 mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1253 AppInterface.CommandType.SELECT_ITEM.value()) {
1254 mStkContext[slotId].mIsMenuPending = true;
1255 } else {
1256 }
1257 CatLog.d(LOG_TAG, "finish pending activity.");
1258 activity.finish();
1259 mStkContext[slotId].mActivityInstance = null;
1260 }
1261 if (dialog != null) {
1262 CatLog.d(LOG_TAG, "finish pending dialog.");
1263 mStkContext[slotId].mIsDialogPending = true;
1264 dialog.finish();
1265 mStkContext[slotId].mDialogInstance = null;
1266 }
1267 }
1268 /**
1269 * This method is used for restoring pending instances from stack.
1270 */
1271 private void restoreInstanceFromStackBySlot(int slotId) {
1272 AppInterface.CommandType cmdType = mStkContext[slotId].mCurrentCmd.getCmdType();
1273
1274 CatLog.d(LOG_TAG, "restoreInstanceFromStackBySlot cmdType : " + cmdType);
1275 switch(cmdType) {
1276 case GET_INPUT:
1277 case GET_INKEY:
1278 launchInputActivity(slotId);
1279 //Set mMenuIsVisible to true for showing main menu for
1280 //following session end command.
1281 mStkContext[slotId].mMenuIsVisible = true;
1282 break;
1283 case DISPLAY_TEXT:
1284 launchTextDialog(slotId);
1285 break;
1286 case LAUNCH_BROWSER:
1287 launchConfirmationDialog(mStkContext[slotId].mCurrentCmd.geTextMessage(),
1288 slotId);
1289 break;
1290 case OPEN_CHANNEL:
1291 launchOpenChannelDialog(slotId);
1292 break;
1293 case SET_UP_CALL:
1294 launchConfirmationDialog(mStkContext[slotId].mCurrentCmd.getCallSettings().
1295 confirmMsg, slotId);
1296 break;
1297 case SET_UP_MENU:
1298 case SELECT_ITEM:
1299 launchMenuActivity(null, slotId);
1300 break;
1301 default:
1302 break;
1303 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001304 }
1305
Wink Savillee68857d2014-10-17 15:23:05 -07001306 private void launchMenuActivity(Menu menu, int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001307 Intent newIntent = new Intent(Intent.ACTION_VIEW);
Wink Savillee68857d2014-10-17 15:23:05 -07001308 String targetActivity = STK_MENU_ACTIVITY_NAME;
1309 String uriString = STK_MENU_URI + System.currentTimeMillis();
1310 //Set unique URI to create a new instance of activity for different slotId.
1311 Uri uriData = Uri.parse(uriString);
1312
1313 CatLog.d(LOG_TAG, "launchMenuActivity, slotId: " + slotId + " , " +
1314 uriData.toString() + " , " + mStkContext[slotId].mOpCode + ", "
1315 + mStkContext[slotId].mMenuState);
1316 newIntent.setClassName(PACKAGE_NAME, targetActivity);
1317 int intentFlags = Intent.FLAG_ACTIVITY_NEW_TASK;
1318
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001319 if (menu == null) {
1320 // We assume this was initiated by the user pressing the tool kit icon
Wink Savillee68857d2014-10-17 15:23:05 -07001321 intentFlags |= getFlagActivityNoUserAction(InitiatedByUserAction.yes, slotId);
1322 if (mStkContext[slotId].mOpCode == OP_END_SESSION) {
1323 CatLog.d(LOG_TAG, "launchMenuActivity, return OP_END_SESSION");
1324 mStkContext[slotId].mMenuState = StkMenuActivity.STATE_MAIN;
1325 if (mStkContext[slotId].mMainActivityInstance != null) {
1326 CatLog.d(LOG_TAG, "launchMenuActivity, mMainActivityInstance is not null");
1327 return;
1328 }
Wink Savillee68857d2014-10-17 15:23:05 -07001329 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001330
Wink Savillee68857d2014-10-17 15:23:05 -07001331 //If the last pending menu is secondary menu, "STATE" should be "STATE_SECONDARY".
1332 //Otherwise, it should be "STATE_MAIN".
1333 if (mStkContext[slotId].mOpCode == OP_LAUNCH_APP &&
1334 mStkContext[slotId].mMenuState == StkMenuActivity.STATE_SECONDARY) {
1335 newIntent.putExtra("STATE", StkMenuActivity.STATE_SECONDARY);
1336 } else {
1337 newIntent.putExtra("STATE", StkMenuActivity.STATE_MAIN);
1338 mStkContext[slotId].mMenuState = StkMenuActivity.STATE_MAIN;
1339 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001340 } else {
1341 // We don't know and we'll let getFlagActivityNoUserAction decide.
Wink Savillee68857d2014-10-17 15:23:05 -07001342 intentFlags |= getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001343 newIntent.putExtra("STATE", StkMenuActivity.STATE_SECONDARY);
Wink Savillee68857d2014-10-17 15:23:05 -07001344 mStkContext[slotId].mMenuState = StkMenuActivity.STATE_SECONDARY;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001345 }
Wink Savillee68857d2014-10-17 15:23:05 -07001346 newIntent.putExtra(SLOT_ID, slotId);
1347 newIntent.setData(uriData);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001348 newIntent.setFlags(intentFlags);
1349 mContext.startActivity(newIntent);
1350 }
1351
Wink Savillee68857d2014-10-17 15:23:05 -07001352 private void launchInputActivity(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001353 Intent newIntent = new Intent(Intent.ACTION_VIEW);
Wink Savillee68857d2014-10-17 15:23:05 -07001354 String targetActivity = STK_INPUT_ACTIVITY_NAME;
1355 String uriString = STK_INPUT_URI + System.currentTimeMillis();
1356 //Set unique URI to create a new instance of activity for different slotId.
1357 Uri uriData = Uri.parse(uriString);
1358
1359 CatLog.d(LOG_TAG, "launchInputActivity, slotId: " + slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001360 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
Wink Savillee68857d2014-10-17 15:23:05 -07001361 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1362 newIntent.setClassName(PACKAGE_NAME, targetActivity);
1363 newIntent.putExtra("INPUT", mStkContext[slotId].mCurrentCmd.geInput());
1364 newIntent.putExtra(SLOT_ID, slotId);
1365 newIntent.setData(uriData);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001366 mContext.startActivity(newIntent);
1367 }
1368
Wink Savillee68857d2014-10-17 15:23:05 -07001369 private void launchTextDialog(int slotId) {
1370 CatLog.d(LOG_TAG, "launchTextDialog, slotId: " + slotId);
1371 Intent newIntent = new Intent();
1372 String targetActivity = STK_DIALOG_ACTIVITY_NAME;
1373 int action = getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId);
1374 String uriString = STK_DIALOG_URI + System.currentTimeMillis();
1375 //Set unique URI to create a new instance of activity for different slotId.
1376 Uri uriData = Uri.parse(uriString);
1377 if (newIntent != null) {
1378 newIntent.setClassName(PACKAGE_NAME, targetActivity);
1379 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1380 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
1381 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1382 newIntent.setData(uriData);
1383 newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
1384 newIntent.putExtra(SLOT_ID, slotId);
1385 startActivity(newIntent);
Preeti Ahuja414bc412013-06-25 19:31:49 -07001386 // For display texts with immediate response, send the terminal response
1387 // immediately. responseNeeded will be false, if display text command has
1388 // the immediate response tlv.
1389 if (!mStkContext[slotId].mCurrentCmd.geTextMessage().responseNeeded) {
1390 sendResponse(RES_ID_CONFIRM, slotId, true);
1391 }
Wink Savillee68857d2014-10-17 15:23:05 -07001392 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001393 }
1394
Wink Savillee68857d2014-10-17 15:23:05 -07001395 public boolean isStkDialogActivated(Context context) {
1396 String stkDialogActivity = "com.android.stk.StkDialogActivity";
1397 boolean activated = false;
1398 final ActivityManager am = (ActivityManager) context.getSystemService(
1399 Context.ACTIVITY_SERVICE);
1400 String topActivity = am.getRunningTasks(1).get(0).topActivity.getClassName();
1401
1402 CatLog.d(LOG_TAG, "isStkDialogActivated: " + topActivity);
1403 if (topActivity.equals(stkDialogActivity)) {
1404 activated = true;
1405 }
1406 CatLog.d(LOG_TAG, "activated : " + activated);
1407 return activated;
Johan Hellman3aec01c2011-02-10 10:15:28 +01001408 }
1409
Preeti Ahuja95919342013-10-01 18:18:55 -07001410 private void sendSetUpEventResponse(int event, byte[] addedInfo, int slotId) {
Preeti Ahuja414bc412013-06-25 19:31:49 -07001411 CatLog.d(this, "sendSetUpEventResponse: event : " + event + "slotId = " + slotId);
Preeti Ahuja95919342013-10-01 18:18:55 -07001412
1413 if (mStkContext[slotId].mCurrentSetupEventCmd == null){
1414 CatLog.e(this, "mCurrentSetupEventCmd is null");
1415 return;
1416 }
1417
1418 CatResponseMessage resMsg = new CatResponseMessage(mStkContext[slotId].mCurrentSetupEventCmd);
1419
1420 resMsg.setResultCode(ResultCode.OK);
1421 resMsg.setEventDownload(event, addedInfo);
1422
1423 mStkService[slotId].onCmdResponse(resMsg);
1424 }
1425
1426 private void checkForSetupEvent(int event, Bundle args, int slotId) {
1427 boolean eventPresent = false;
1428 byte[] addedInfo = null;
1429 CatLog.d(this, "Event :" + event);
1430
1431 if (mStkContext[slotId].mSetupEventListSettings != null) {
1432 /* Checks if the event is present in the EventList updated by last
1433 * SetupEventList Proactive Command */
1434 for (int i : mStkContext[slotId].mSetupEventListSettings.eventList) {
1435 if (event == i) {
1436 eventPresent = true;
1437 break;
1438 }
1439 }
1440
1441 /* If Event is present send the response to ICC */
1442 if (eventPresent == true) {
1443 CatLog.d(this, " Event " + event + "exists in the EventList");
1444
1445 switch (event) {
Preeti Ahuja560be362014-11-25 19:38:24 -08001446 case IDLE_SCREEN_AVAILABLE_EVENT:
1447 sendSetUpEventResponse(event, addedInfo, slotId);
1448 removeSetUpEvent(event, slotId);
1449 break;
Preeti Ahuja95919342013-10-01 18:18:55 -07001450 case LANGUAGE_SELECTION_EVENT:
1451 String language = mContext
1452 .getResources().getConfiguration().locale.getLanguage();
1453 CatLog.d(this, "language: " + language);
1454 // Each language code is a pair of alpha-numeric characters.
1455 // Each alpha-numeric character shall be coded on one byte
1456 // using the SMS default 7-bit coded alphabet
1457 addedInfo = GsmAlphabet.stringToGsm8BitPacked(language);
1458 sendSetUpEventResponse(event, addedInfo, slotId);
1459 break;
1460 default:
1461 break;
1462 }
1463 } else {
1464 CatLog.e(this, " Event does not exist in the EventList");
1465 }
1466 } else {
1467 CatLog.e(this, "SetupEventList is not received. Ignoring the event: " + event);
1468 }
1469 }
1470
1471 private void removeSetUpEvent(int event, int slotId) {
1472 CatLog.d(this, "Remove Event :" + event);
1473
1474 if (mStkContext[slotId].mSetupEventListSettings != null) {
1475 /*
1476 * Make new Eventlist without the event
1477 */
1478 for (int i = 0; i < mStkContext[slotId].mSetupEventListSettings.eventList.length; i++) {
1479 if (event == mStkContext[slotId].mSetupEventListSettings.eventList[i]) {
1480 mStkContext[slotId].mSetupEventListSettings.eventList[i] = INVALID_SETUP_EVENT;
1481 break;
1482 }
1483 }
1484 }
1485 }
1486
1487 private void launchEventMessage(int slotId) {
1488 launchEventMessage(slotId, mStkContext[slotId].mCurrentCmd.geTextMessage());
1489 }
1490
Wink Savillee68857d2014-10-17 15:23:05 -07001491 private void launchEventMessage(int slotId, TextMessage msg) {
1492 if (msg == null || (msg.text != null && msg.text.length() == 0)) {
1493 CatLog.d(LOG_TAG, "launchEventMessage return");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001494 return;
1495 }
Wink Savillee68857d2014-10-17 15:23:05 -07001496
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001497 Toast toast = new Toast(mContext.getApplicationContext());
1498 LayoutInflater inflate = (LayoutInflater) mContext
1499 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1500 View v = inflate.inflate(R.layout.stk_event_msg, null);
1501 TextView tv = (TextView) v
1502 .findViewById(com.android.internal.R.id.message);
1503 ImageView iv = (ImageView) v
1504 .findViewById(com.android.internal.R.id.icon);
1505 if (msg.icon != null) {
1506 iv.setImageBitmap(msg.icon);
1507 } else {
1508 iv.setVisibility(View.GONE);
1509 }
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301510 /* In case of 'self explanatory' stkapp should display the specified
1511 * icon in proactive command (but not the alpha string).
1512 * If icon is non-self explanatory and if the icon could not be displayed
1513 * then alpha string or text data should be displayed
1514 * Ref: ETSI 102.223,section 6.5.4
1515 */
1516 if (mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ||
1517 msg.icon == null || !msg.iconSelfExplanatory) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001518 tv.setText(msg.text);
1519 }
1520
1521 toast.setView(v);
1522 toast.setDuration(Toast.LENGTH_LONG);
1523 toast.setGravity(Gravity.BOTTOM, 0, 0);
1524 toast.show();
1525 }
1526
Wink Savillee68857d2014-10-17 15:23:05 -07001527 private void launchConfirmationDialog(TextMessage msg, int slotId) {
1528 msg.title = mStkContext[slotId].lastSelectedItem;
1529 Intent newIntent = new Intent();
1530 String targetActivity = STK_DIALOG_ACTIVITY_NAME;
1531 String uriString = STK_DIALOG_URI + System.currentTimeMillis();
1532 //Set unique URI to create a new instance of activity for different slotId.
1533 Uri uriData = Uri.parse(uriString);
1534
1535 if (newIntent != null) {
1536 newIntent.setClassName(this, targetActivity);
1537 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1538 | Intent.FLAG_ACTIVITY_NO_HISTORY
1539 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
1540 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1541 newIntent.putExtra("TEXT", msg);
1542 newIntent.putExtra(SLOT_ID, slotId);
1543 newIntent.setData(uriData);
1544 startActivity(newIntent);
1545 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001546 }
1547
1548 private void launchBrowser(BrowserSettings settings) {
1549 if (settings == null) {
1550 return;
1551 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001552
Abhishek Adappa840c82f2013-02-26 10:19:49 -08001553 Uri data = null;
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001554 String url;
1555 if (settings.url == null) {
Abhishek Adappa840c82f2013-02-26 10:19:49 -08001556 // if the command did not contain a URL,
1557 // launch the browser to the default homepage.
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001558 CatLog.d(this, "no url data provided by proactive command." +
1559 " launching browser with stk default URL ... ");
1560 url = SystemProperties.get(STK_BROWSER_DEFAULT_URL_SYSPROP,
1561 "http://www.google.com");
1562 } else {
1563 CatLog.d(this, "launch browser command has attached url = " + settings.url);
1564 url = settings.url;
Abhishek Adappa840c82f2013-02-26 10:19:49 -08001565 }
David Brown7c03cfe2011-10-20 15:36:12 -07001566
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001567 if (url.startsWith("http://") || url.startsWith("https://")) {
1568 data = Uri.parse(url);
1569 CatLog.d(this, "launching browser with url = " + url);
1570 } else {
1571 String modifiedUrl = "http://" + url;
1572 data = Uri.parse(modifiedUrl);
1573 CatLog.d(this, "launching browser with modified url = " + modifiedUrl);
1574 }
1575
1576 Intent intent = new Intent(Intent.ACTION_VIEW);
1577 intent.setData(data);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001578 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1579 switch (settings.mode) {
1580 case USE_EXISTING_BROWSER:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001581 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1582 break;
1583 case LAUNCH_NEW_BROWSER:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001584 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1585 break;
1586 case LAUNCH_IF_NOT_ALREADY_LAUNCHED:
1587 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1588 break;
1589 }
1590 // start browser activity
1591 startActivity(intent);
1592 // a small delay, let the browser start, before processing the next command.
Wink Saville79085fc2009-06-09 10:27:23 -07001593 // this is good for scenarios where a related DISPLAY TEXT command is
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001594 // followed immediately.
1595 try {
Ryuto Sawada350aaa62016-06-13 14:47:22 +09001596 Thread.sleep(3000);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001597 } catch (InterruptedException e) {}
1598 }
1599
Wink Savillee68857d2014-10-17 15:23:05 -07001600 private void launchIdleText(int slotId) {
Preeti Ahuja560be362014-11-25 19:38:24 -08001601 TextMessage msg = mStkContext[slotId].mIdleModeTextCmd.geTextMessage();
dujin.cha2a0eb2a2011-11-11 15:03:57 +09001602
Preeti Ahuja95919342013-10-01 18:18:55 -07001603 if (msg == null || msg.text ==null) {
1604 CatLog.d(LOG_TAG, msg == null ? "mCurrent.getTextMessage is NULL"
1605 : "mCurrent.getTextMessage.text is NULL");
Wink Savillee68857d2014-10-17 15:23:05 -07001606 mNotificationManager.cancel(getNotificationId(slotId));
Wink Saville046db4b2011-11-01 20:42:54 -07001607 return;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001608 } else {
Preeti Ahuja95919342013-10-01 18:18:55 -07001609 CatLog.d(LOG_TAG, "launchIdleText - text[" + msg.text
1610 + "] iconSelfExplanatory[" + msg.iconSelfExplanatory
1611 + "] icon[" + msg.icon + "], sim id: " + slotId);
Wink Savillee68857d2014-10-17 15:23:05 -07001612 CatLog.d(LOG_TAG, "Add IdleMode text");
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001613 PendingIntent pendingIntent = PendingIntent.getService(mContext, 0,
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001614 new Intent(mContext, StkAppService.class), 0);
fionaxu805eb572017-05-02 10:57:30 -07001615 createAllChannels();
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001616 final Notification.Builder notificationBuilder = new Notification.Builder(
fionaxu2c91c752017-04-21 18:11:57 -07001617 StkAppService.this, STK_NOTIFICATION_CHANNEL_ID);
Wink Savillee68857d2014-10-17 15:23:05 -07001618 if (mStkContext[slotId].mMainCmd != null &&
1619 mStkContext[slotId].mMainCmd.getMenu() != null) {
1620 notificationBuilder.setContentTitle(mStkContext[slotId].mMainCmd.getMenu().title);
Christopher Posselwhite2ad2ab72013-05-27 07:51:06 +02001621 } else {
1622 notificationBuilder.setContentTitle("");
1623 }
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001624 notificationBuilder
1625 .setSmallIcon(com.android.internal.R.drawable.stat_notify_sim_toolkit);
1626 notificationBuilder.setContentIntent(pendingIntent);
1627 notificationBuilder.setOngoing(true);
1628 // Set text and icon for the status bar and notification body.
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301629 if (mStkContext[slotId].mIdleModeTextCmd.hasIconLoadFailed() ||
1630 !msg.iconSelfExplanatory) {
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001631 notificationBuilder.setContentText(msg.text);
Christopher Posselwhite2ad2ab72013-05-27 07:51:06 +02001632 notificationBuilder.setTicker(msg.text);
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001633 }
1634 if (msg.icon != null) {
1635 notificationBuilder.setLargeIcon(msg.icon);
1636 } else {
1637 Bitmap bitmapIcon = BitmapFactory.decodeResource(StkAppService.this
1638 .getResources().getSystem(),
1639 com.android.internal.R.drawable.stat_notify_sim_toolkit);
1640 notificationBuilder.setLargeIcon(bitmapIcon);
1641 }
Selim Cinek62eb3fe2014-08-27 17:52:23 +02001642 notificationBuilder.setColor(mContext.getResources().getColor(
1643 com.android.internal.R.color.system_notification_accent_color));
Wink Savillee68857d2014-10-17 15:23:05 -07001644 mNotificationManager.notify(getNotificationId(slotId), notificationBuilder.build());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001645 }
1646 }
1647
fionaxu805eb572017-05-02 10:57:30 -07001648 /** Creates the notification channel and registers it with NotificationManager.
1649 * If a channel with the same ID is already registered, NotificationManager will
1650 * ignore this call.
1651 */
1652 private void createAllChannels() {
1653 mNotificationManager.createNotificationChannel(new NotificationChannel(
1654 STK_NOTIFICATION_CHANNEL_ID,
1655 getResources().getString(R.string.stk_channel_name),
1656 NotificationManager.IMPORTANCE_MIN));
1657 }
1658
Wink Savillee68857d2014-10-17 15:23:05 -07001659 private void launchToneDialog(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001660 Intent newIntent = new Intent(this, ToneDialog.class);
Wink Savillee68857d2014-10-17 15:23:05 -07001661 String uriString = STK_TONE_URI + slotId;
1662 Uri uriData = Uri.parse(uriString);
1663 //Set unique URI to create a new instance of activity for different slotId.
1664 CatLog.d(LOG_TAG, "launchToneDialog, slotId: " + slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001665 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1666 | Intent.FLAG_ACTIVITY_NO_HISTORY
1667 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
Wink Savillee68857d2014-10-17 15:23:05 -07001668 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1669 newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
1670 newIntent.putExtra("TONE", mStkContext[slotId].mCurrentCmd.getToneSettings());
1671 newIntent.putExtra(SLOT_ID, slotId);
1672 newIntent.setData(uriData);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001673 startActivity(newIntent);
1674 }
1675
Srikanth Chintala89aa6602014-03-14 16:26:57 +05301676 private void handlePlayTone(int slotId) {
1677 TextMessage toneMsg = mStkContext[slotId].mCurrentCmd.geTextMessage();
1678
1679 boolean showUser = true;
1680 boolean displayDialog = true;
1681 Resources resource = Resources.getSystem();
1682 try {
1683 displayDialog = !resource.getBoolean(
1684 com.android.internal.R.bool.config_stkNoAlphaUsrCnf);
1685 } catch (NotFoundException e) {
1686 displayDialog = true;
1687 }
1688
1689 // As per the spec 3GPP TS 11.14, 6.4.5. Play Tone.
1690 // If there is no alpha identifier tlv present, UE may show the
1691 // user information. 'config_stkNoAlphaUsrCnf' value will decide
1692 // whether to show it or not.
1693 // If alpha identifier tlv is present and its data is null, play only tone
1694 // without showing user any information.
1695 // Alpha Id is Present, but the text data is null.
1696 if ((toneMsg.text != null ) && (toneMsg.text.equals(""))) {
1697 CatLog.d(this, "Alpha identifier data is null, play only tone");
1698 showUser = false;
1699 }
1700 // Alpha Id is not present AND we need to show info to the user.
1701 if (toneMsg.text == null && displayDialog) {
1702 CatLog.d(this, "toneMsg.text " + toneMsg.text
1703 + " Starting ToneDialog activity with default message.");
1704 toneMsg.text = getResources().getString(R.string.default_tone_dialog_msg);
1705 showUser = true;
1706 }
1707 // Dont show user info, if config setting is true.
1708 if (toneMsg.text == null && !displayDialog) {
1709 CatLog.d(this, "config value stkNoAlphaUsrCnf is true");
1710 showUser = false;
1711 }
1712
1713 CatLog.d(this, "toneMsg.text: " + toneMsg.text + "showUser: " +showUser +
1714 "displayDialog: " +displayDialog);
1715 playTone(showUser, slotId);
1716 }
1717
1718 private void playTone(boolean showUserInfo, int slotId) {
1719 // Start playing tone and vibration
1720 ToneSettings settings = mStkContext[slotId].mCurrentCmd.getToneSettings();
1721 if (null == settings) {
1722 CatLog.d(this, "null settings, not playing tone.");
1723 return;
1724 }
1725
1726 mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
1727 mTonePlayer = new TonePlayer();
1728 mTonePlayer.play(settings.tone);
1729 int timeout = StkApp.calculateDurationInMilis(settings.duration);
1730 if (timeout == 0) {
1731 timeout = StkApp.TONE_DEFAULT_TIMEOUT;
1732 }
1733
1734 Message msg = mServiceHandler.obtainMessage();
1735 msg.arg1 = OP_STOP_TONE;
1736 msg.arg2 = slotId;
1737 msg.obj = (Integer)(showUserInfo ? 1 : 0);
1738 msg.what = STOP_TONE_WHAT;
1739 mServiceHandler.sendMessageDelayed(msg, timeout);
1740 if (settings.vibrate) {
1741 mVibrator.vibrate(timeout);
1742 }
1743
1744 // Start Tone dialog Activity to show user the information.
1745 if (showUserInfo) {
1746 Intent newIntent = new Intent(sInstance, ToneDialog.class);
1747 String uriString = STK_TONE_URI + slotId;
1748 Uri uriData = Uri.parse(uriString);
1749 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1750 | Intent.FLAG_ACTIVITY_NO_HISTORY
1751 | Intent.FLAG_ACTIVITY_SINGLE_TOP
1752 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
1753 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1754 newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
1755 newIntent.putExtra(SLOT_ID, slotId);
1756 newIntent.setData(uriData);
1757 startActivity(newIntent);
1758 }
1759 }
1760
1761 private void finishToneDialogActivity() {
1762 Intent finishIntent = new Intent(FINISH_TONE_ACTIVITY_ACTION);
1763 sendBroadcast(finishIntent);
1764 }
1765
1766 private void handleStopTone(Message msg, int slotId) {
1767 int resId = 0;
1768
1769 // Stop the play tone in following cases:
1770 // 1.OP_STOP_TONE: play tone timer expires.
1771 // 2.STOP_TONE_USER: user pressed the back key.
1772 if (msg.arg1 == OP_STOP_TONE) {
1773 resId = RES_ID_DONE;
1774 // Dismiss Tone dialog, after finishing off playing the tone.
1775 int finishActivity = (Integer) msg.obj;
1776 if (finishActivity == 1) finishToneDialogActivity();
1777 } else if (msg.arg1 == OP_STOP_TONE_USER) {
1778 resId = RES_ID_END_SESSION;
1779 }
1780
1781 sendResponse(resId, slotId, true);
1782 mServiceHandler.removeMessages(STOP_TONE_WHAT);
1783 if (mTonePlayer != null) {
1784 mTonePlayer.stop();
1785 mTonePlayer.release();
1786 mTonePlayer = null;
1787 }
1788 if (mVibrator != null) {
1789 mVibrator.cancel();
1790 mVibrator = null;
1791 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001792 }
1793
Takanori Nakano49b12722016-02-16 14:34:14 +09001794 private void launchOpenChannelDialog(final int slotId) {
Wink Savillee68857d2014-10-17 15:23:05 -07001795 TextMessage msg = mStkContext[slotId].mCurrentCmd.geTextMessage();
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001796 if (msg == null) {
Wink Savillee68857d2014-10-17 15:23:05 -07001797 CatLog.d(LOG_TAG, "msg is null, return here");
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001798 return;
1799 }
1800
1801 msg.title = getResources().getString(R.string.stk_dialog_title);
1802 if (msg.text == null) {
1803 msg.text = getResources().getString(R.string.default_open_channel_msg);
1804 }
1805
1806 final AlertDialog dialog = new AlertDialog.Builder(mContext)
1807 .setIconAttribute(android.R.attr.alertDialogIcon)
1808 .setTitle(msg.title)
1809 .setMessage(msg.text)
1810 .setCancelable(false)
1811 .setPositiveButton(getResources().getString(R.string.stk_dialog_accept),
1812 new DialogInterface.OnClickListener() {
1813 public void onClick(DialogInterface dialog, int which) {
1814 Bundle args = new Bundle();
1815 args.putInt(RES_ID, RES_ID_CHOICE);
1816 args.putInt(CHOICE, YES);
1817 Message message = mServiceHandler.obtainMessage();
1818 message.arg1 = OP_RESPONSE;
Takanori Nakano49b12722016-02-16 14:34:14 +09001819 message.arg2 = slotId;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001820 message.obj = args;
1821 mServiceHandler.sendMessage(message);
1822 }
1823 })
1824 .setNegativeButton(getResources().getString(R.string.stk_dialog_reject),
1825 new DialogInterface.OnClickListener() {
1826 public void onClick(DialogInterface dialog, int which) {
1827 Bundle args = new Bundle();
1828 args.putInt(RES_ID, RES_ID_CHOICE);
1829 args.putInt(CHOICE, NO);
1830 Message message = mServiceHandler.obtainMessage();
1831 message.arg1 = OP_RESPONSE;
Takanori Nakano49b12722016-02-16 14:34:14 +09001832 message.arg2 = slotId;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001833 message.obj = args;
1834 mServiceHandler.sendMessage(message);
1835 }
1836 })
1837 .create();
1838
1839 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
1840 if (!mContext.getResources().getBoolean(
1841 com.android.internal.R.bool.config_sf_slowBlur)) {
1842 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1843 }
1844
1845 dialog.show();
1846 }
1847
Wink Savillee68857d2014-10-17 15:23:05 -07001848 private void launchTransientEventMessage(int slotId) {
1849 TextMessage msg = mStkContext[slotId].mCurrentCmd.geTextMessage();
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001850 if (msg == null) {
Wink Savillee68857d2014-10-17 15:23:05 -07001851 CatLog.d(LOG_TAG, "msg is null, return here");
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001852 return;
1853 }
1854
1855 msg.title = getResources().getString(R.string.stk_dialog_title);
1856
1857 final AlertDialog dialog = new AlertDialog.Builder(mContext)
1858 .setIconAttribute(android.R.attr.alertDialogIcon)
1859 .setTitle(msg.title)
1860 .setMessage(msg.text)
1861 .setCancelable(false)
1862 .setPositiveButton(getResources().getString(android.R.string.ok),
1863 new DialogInterface.OnClickListener() {
1864 public void onClick(DialogInterface dialog, int which) {
1865 }
1866 })
1867 .create();
1868
1869 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
1870 if (!mContext.getResources().getBoolean(
1871 com.android.internal.R.bool.config_sf_slowBlur)) {
1872 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1873 }
1874
1875 dialog.show();
1876 }
1877
Wink Savillee68857d2014-10-17 15:23:05 -07001878 private int getNotificationId(int slotId) {
1879 int notifyId = STK_NOTIFICATION_ID;
1880 if (slotId >= 0 && slotId < mSimCount) {
1881 notifyId += slotId;
1882 } else {
1883 CatLog.d(LOG_TAG, "invalid slotId: " + slotId);
1884 }
1885 CatLog.d(LOG_TAG, "getNotificationId, slotId: " + slotId + ", notifyId: " + notifyId);
1886 return notifyId;
1887 }
1888
1889 private String getItemName(int itemId, int slotId) {
1890 Menu menu = mStkContext[slotId].mCurrentCmd.getMenu();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001891 if (menu == null) {
1892 return null;
1893 }
1894 for (Item item : menu.items) {
1895 if (item.id == itemId) {
1896 return item.text;
1897 }
1898 }
1899 return null;
1900 }
1901
Wink Savillee68857d2014-10-17 15:23:05 -07001902 private boolean removeMenu(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001903 try {
Wink Savillee68857d2014-10-17 15:23:05 -07001904 if (mStkContext[slotId].mCurrentMenu.items.size() == 1 &&
1905 mStkContext[slotId].mCurrentMenu.items.get(0) == null) {
1906 mStkContext[slotId].mSetupMenuState = STATE_NOT_EXIST;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001907 return true;
1908 }
1909 } catch (NullPointerException e) {
Wink Savillee68857d2014-10-17 15:23:05 -07001910 CatLog.d(LOG_TAG, "Unable to get Menu's items size");
1911 mStkContext[slotId].mSetupMenuState = STATE_NOT_EXIST;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001912 return true;
1913 }
Wink Savillee68857d2014-10-17 15:23:05 -07001914 mStkContext[slotId].mSetupMenuState = STATE_EXIST;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001915 return false;
1916 }
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +05301917
Wink Savillee68857d2014-10-17 15:23:05 -07001918 StkContext getStkContext(int slotId) {
1919 if (slotId >= 0 && slotId < mSimCount) {
1920 return mStkContext[slotId];
1921 } else {
1922 CatLog.d(LOG_TAG, "invalid slotId: " + slotId);
1923 return null;
1924 }
1925 }
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +05301926
1927 private void handleAlphaNotify(Bundle args) {
1928 String alphaString = args.getString(AppInterface.ALPHA_STRING);
1929
1930 CatLog.d(this, "Alpha string received from card: " + alphaString);
1931 Toast toast = Toast.makeText(sInstance, alphaString, Toast.LENGTH_LONG);
1932 toast.setGravity(Gravity.TOP, 0, 0);
1933 toast.show();
1934 }
Srikanth Chintalaba103002015-11-30 10:49:52 -08001935
1936 private boolean isUrlAvailableToLaunchBrowser(BrowserSettings settings) {
1937 String url = SystemProperties.get(STK_BROWSER_DEFAULT_URL_SYSPROP, "");
1938 if (url == "" && settings.url == null) {
1939 return false;
1940 }
1941 return true;
1942 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001943}