blob: 21315dbf7165a2fb3d0d897353292908741bf053 [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;
Takanori Nakanoc8054ba2016-08-15 19:18:16 +090042import android.os.Parcel;
Sooraj Sasindrana4160472016-10-12 16:28:25 -070043import android.os.PersistableBundle;
Preeti Ahuja560be362014-11-25 19:38:24 -080044import android.os.PowerManager;
Preeti Ahujaa7cdca22013-10-01 18:20:56 -070045import android.os.SystemProperties;
Srikanth Chintala89aa6602014-03-14 16:26:57 +053046import android.os.Vibrator;
Preeti Ahuja95919342013-10-01 18:18:55 -070047import android.provider.Settings;
Sooraj Sasindrana4160472016-10-12 16:28:25 -070048import android.telephony.CarrierConfigManager;
Wink Saville56469d52009-04-02 01:37:03 -070049import android.telephony.TelephonyManager;
Preeti Ahujaa7cdca22013-10-01 18:20:56 -070050import android.text.TextUtils;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080051import android.view.Gravity;
52import android.view.LayoutInflater;
53import android.view.View;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -050054import android.view.WindowManager;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080055import android.widget.ImageView;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080056import android.widget.TextView;
57import android.widget.Toast;
Wink Savillee68857d2014-10-17 15:23:05 -070058import android.content.IntentFilter;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080059
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070060import com.android.internal.telephony.cat.AppInterface;
Preeti Ahuja95919342013-10-01 18:18:55 -070061import com.android.internal.telephony.cat.LaunchBrowserMode;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070062import com.android.internal.telephony.cat.Menu;
63import com.android.internal.telephony.cat.Item;
64import com.android.internal.telephony.cat.ResultCode;
65import com.android.internal.telephony.cat.CatCmdMessage;
66import com.android.internal.telephony.cat.CatCmdMessage.BrowserSettings;
Preeti Ahuja95919342013-10-01 18:18:55 -070067import com.android.internal.telephony.cat.CatCmdMessage.SetupEventListSettings;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070068import com.android.internal.telephony.cat.CatLog;
69import com.android.internal.telephony.cat.CatResponseMessage;
70import com.android.internal.telephony.cat.TextMessage;
Srikanth Chintala89aa6602014-03-14 16:26:57 +053071import com.android.internal.telephony.cat.ToneSettings;
Wink Saville94e982b2014-07-11 07:38:14 -070072import com.android.internal.telephony.uicc.IccRefreshResponse;
Wink Savillee68857d2014-10-17 15:23:05 -070073import com.android.internal.telephony.PhoneConstants;
Preeti Ahuja95919342013-10-01 18:18:55 -070074import com.android.internal.telephony.GsmAlphabet;
Legler Wuaeefef52014-10-27 00:57:18 +080075import com.android.internal.telephony.cat.CatService;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080076
Preeti Ahujaa7cdca22013-10-01 18:20:56 -070077import java.util.Iterator;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080078import java.util.LinkedList;
Wink Savillee68857d2014-10-17 15:23:05 -070079import java.lang.System;
80import java.util.List;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080081
Preeti Ahuja95919342013-10-01 18:18:55 -070082import static com.android.internal.telephony.cat.CatCmdMessage.
Preeti Ahuja560be362014-11-25 19:38:24 -080083 SetupEventListConstants.IDLE_SCREEN_AVAILABLE_EVENT;
84import static com.android.internal.telephony.cat.CatCmdMessage.
Preeti Ahuja95919342013-10-01 18:18:55 -070085 SetupEventListConstants.LANGUAGE_SELECTION_EVENT;
86
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080087/**
88 * SIM toolkit application level service. Interacts with Telephopny messages,
89 * application's launch and user input from STK UI elements.
Wink Saville79085fc2009-06-09 10:27:23 -070090 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080091 */
92public class StkAppService extends Service implements Runnable {
93
94 // members
Wink Savillee68857d2014-10-17 15:23:05 -070095 protected class StkContext {
96 protected CatCmdMessage mMainCmd = null;
97 protected CatCmdMessage mCurrentCmd = null;
98 protected CatCmdMessage mCurrentMenuCmd = null;
99 protected Menu mCurrentMenu = null;
100 protected String lastSelectedItem = null;
101 protected boolean mMenuIsVisible = false;
102 protected boolean mIsInputPending = false;
103 protected boolean mIsMenuPending = false;
104 protected boolean mIsDialogPending = false;
105 protected boolean responseNeeded = true;
106 protected boolean launchBrowser = false;
107 protected BrowserSettings mBrowserSettings = null;
108 protected LinkedList<DelayedCmd> mCmdsQ = null;
109 protected boolean mCmdInProgress = false;
110 protected int mStkServiceState = STATE_UNKNOWN;
111 protected int mSetupMenuState = STATE_UNKNOWN;
112 protected int mMenuState = StkMenuActivity.STATE_INIT;
113 protected int mOpCode = -1;
114 private Activity mActivityInstance = null;
115 private Activity mDialogInstance = null;
116 private Activity mMainActivityInstance = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700117 private int mSlotId = 0;
Preeti Ahuja95919342013-10-01 18:18:55 -0700118 private SetupEventListSettings mSetupEventListSettings = null;
119 private boolean mClearSelectItem = false;
120 private boolean mDisplayTextDlgIsVisibile = false;
121 private CatCmdMessage mCurrentSetupEventCmd = null;
Preeti Ahuja560be362014-11-25 19:38:24 -0800122 private CatCmdMessage mIdleModeTextCmd = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700123 final synchronized void setPendingActivityInstance(Activity act) {
124 CatLog.d(this, "setPendingActivityInstance act : " + mSlotId + ", " + act);
125 callSetActivityInstMsg(OP_SET_ACT_INST, mSlotId, act);
126 }
127 final synchronized Activity getPendingActivityInstance() {
128 CatLog.d(this, "getPendingActivityInstance act : " + mSlotId + ", " +
129 mActivityInstance);
130 return mActivityInstance;
131 }
132 final synchronized void setPendingDialogInstance(Activity act) {
133 CatLog.d(this, "setPendingDialogInstance act : " + mSlotId + ", " + act);
134 callSetActivityInstMsg(OP_SET_DAL_INST, mSlotId, act);
135 }
136 final synchronized Activity getPendingDialogInstance() {
137 CatLog.d(this, "getPendingDialogInstance act : " + mSlotId + ", " +
138 mDialogInstance);
139 return mDialogInstance;
140 }
141 final synchronized void setMainActivityInstance(Activity act) {
142 CatLog.d(this, "setMainActivityInstance act : " + mSlotId + ", " + act);
143 callSetActivityInstMsg(OP_SET_MAINACT_INST, mSlotId, act);
144 }
145 final synchronized Activity getMainActivityInstance() {
146 CatLog.d(this, "getMainActivityInstance act : " + mSlotId + ", " +
147 mMainActivityInstance);
148 return mMainActivityInstance;
149 }
150 }
151
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800152 private volatile Looper mServiceLooper;
153 private volatile ServiceHandler mServiceHandler;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800154 private Context mContext = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800155 private NotificationManager mNotificationManager = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800156 static StkAppService sInstance = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700157 private AppInterface[] mStkService = null;
158 private StkContext[] mStkContext = null;
159 private int mSimCount = 0;
Preeti Ahuja560be362014-11-25 19:38:24 -0800160 private PowerManager mPowerManager = null;
161 private StkCmdReceiver mStkCmdReceiver = null;
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530162 private TonePlayer mTonePlayer = null;
163 private Vibrator mVibrator = null;
Preeti Ahuja95919342013-10-01 18:18:55 -0700164
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800165 // Used for setting FLAG_ACTIVITY_NO_USER_ACTION when
166 // creating an intent.
167 private enum InitiatedByUserAction {
168 yes, // The action was started via a user initiated action
169 unknown, // Not known for sure if user initated the action
170 }
171
172 // constants
173 static final String OPCODE = "op";
174 static final String CMD_MSG = "cmd message";
175 static final String RES_ID = "response id";
176 static final String MENU_SELECTION = "menu selection";
177 static final String INPUT = "input";
178 static final String HELP = "help";
179 static final String CONFIRMATION = "confirm";
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500180 static final String CHOICE = "choice";
Wink Savillee68857d2014-10-17 15:23:05 -0700181 static final String SLOT_ID = "SLOT_ID";
182 static final String STK_CMD = "STK CMD";
183 static final String STK_DIALOG_URI = "stk://com.android.stk/dialog/";
184 static final String STK_MENU_URI = "stk://com.android.stk/menu/";
185 static final String STK_INPUT_URI = "stk://com.android.stk/input/";
186 static final String STK_TONE_URI = "stk://com.android.stk/tone/";
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530187 static final String FINISH_TONE_ACTIVITY_ACTION =
188 "android.intent.action.stk.finish_activity";
Preeti Ahuja95919342013-10-01 18:18:55 -0700189
190 // These below constants are used for SETUP_EVENT_LIST
191 static final String SETUP_EVENT_TYPE = "event";
192 static final String SETUP_EVENT_CAUSE = "cause";
193
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800194 // operations ids for different service functionality.
195 static final int OP_CMD = 1;
196 static final int OP_RESPONSE = 2;
197 static final int OP_LAUNCH_APP = 3;
198 static final int OP_END_SESSION = 4;
199 static final int OP_BOOT_COMPLETED = 5;
200 private static final int OP_DELAYED_MSG = 6;
Wink Saville94e982b2014-07-11 07:38:14 -0700201 static final int OP_CARD_STATUS_CHANGED = 7;
Wink Savillee68857d2014-10-17 15:23:05 -0700202 static final int OP_SET_ACT_INST = 8;
203 static final int OP_SET_DAL_INST = 9;
204 static final int OP_SET_MAINACT_INST = 10;
Preeti Ahujab3d0e612014-11-20 13:29:25 -0800205 static final int OP_LOCALE_CHANGED = 11;
206 static final int OP_ALPHA_NOTIFY = 12;
Preeti Ahuja560be362014-11-25 19:38:24 -0800207 static final int OP_IDLE_SCREEN = 13;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800208
Preeti Ahuja95919342013-10-01 18:18:55 -0700209 //Invalid SetupEvent
210 static final int INVALID_SETUP_EVENT = 0xFF;
211
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530212 // Message id to signal stop tone due to play tone timeout.
213 private static final int OP_STOP_TONE = 16;
214
215 // Message id to signal stop tone on user keyback.
216 static final int OP_STOP_TONE_USER = 17;
217
218 // Message id to remove stop tone message from queue.
219 private static final int STOP_TONE_WHAT = 100;
220
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800221 // Response ids
222 static final int RES_ID_MENU_SELECTION = 11;
223 static final int RES_ID_INPUT = 12;
224 static final int RES_ID_CONFIRM = 13;
225 static final int RES_ID_DONE = 14;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500226 static final int RES_ID_CHOICE = 15;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800227
228 static final int RES_ID_TIMEOUT = 20;
229 static final int RES_ID_BACKWARD = 21;
230 static final int RES_ID_END_SESSION = 22;
231 static final int RES_ID_EXIT = 23;
Srikanth Chintalaba103002015-11-30 10:49:52 -0800232 static final int RES_ID_ERROR = 24;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800233
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500234 static final int YES = 1;
235 static final int NO = 0;
236
Wink Savillee68857d2014-10-17 15:23:05 -0700237 static final int STATE_UNKNOWN = -1;
238 static final int STATE_NOT_EXIST = 0;
239 static final int STATE_EXIST = 1;
Wink Saville79085fc2009-06-09 10:27:23 -0700240
Wink Savillee68857d2014-10-17 15:23:05 -0700241 private static final String PACKAGE_NAME = "com.android.stk";
242 private static final String STK_MENU_ACTIVITY_NAME = PACKAGE_NAME + ".StkMenuActivity";
243 private static final String STK_INPUT_ACTIVITY_NAME = PACKAGE_NAME + ".StkInputActivity";
244 private static final String STK_DIALOG_ACTIVITY_NAME = PACKAGE_NAME + ".StkDialogActivity";
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800245 // Notification id used to display Idle Mode text in NotificationManager.
246 private static final int STK_NOTIFICATION_ID = 333;
fionaxu2c91c752017-04-21 18:11:57 -0700247 // Notification channel containing all mobile service messages notifications.
248 private static final String STK_NOTIFICATION_CHANNEL_ID = "mobileServiceMessages";
249
Wink Savillee68857d2014-10-17 15:23:05 -0700250 private static final String LOG_TAG = new Object(){}.getClass().getEnclosingClass().getName();
Wink Saville79085fc2009-06-09 10:27:23 -0700251
252 // Inner class used for queuing telephony messages (proactive commands,
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800253 // session end) while the service is busy processing a previous message.
254 private class DelayedCmd {
255 // members
256 int id;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700257 CatCmdMessage msg;
Wink Savillee68857d2014-10-17 15:23:05 -0700258 int slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800259
Wink Savillee68857d2014-10-17 15:23:05 -0700260 DelayedCmd(int id, CatCmdMessage msg, int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800261 this.id = id;
262 this.msg = msg;
Wink Savillee68857d2014-10-17 15:23:05 -0700263 this.slotId = slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800264 }
265 }
266
Preeti Ahujaa7cdca22013-10-01 18:20:56 -0700267 // system property to set the STK specific default url for launch browser proactive cmds
268 private static final String STK_BROWSER_DEFAULT_URL_SYSPROP = "persist.radio.stk.default_url";
269
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800270 @Override
271 public void onCreate() {
Wink Savillee68857d2014-10-17 15:23:05 -0700272 CatLog.d(LOG_TAG, "onCreate()+");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800273 // Initialize members
Wink Savillee68857d2014-10-17 15:23:05 -0700274 int i = 0;
275 mContext = getBaseContext();
276 mSimCount = TelephonyManager.from(mContext).getSimCount();
277 CatLog.d(LOG_TAG, "simCount: " + mSimCount);
278 mStkService = new AppInterface[mSimCount];
279 mStkContext = new StkContext[mSimCount];
Preeti Ahuja560be362014-11-25 19:38:24 -0800280 mPowerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
281 mStkCmdReceiver = new StkCmdReceiver();
282 registerReceiver(mStkCmdReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Wink Savillee68857d2014-10-17 15:23:05 -0700283 for (i = 0; i < mSimCount; i++) {
284 CatLog.d(LOG_TAG, "slotId: " + i);
Legler Wuaeefef52014-10-27 00:57:18 +0800285 mStkService[i] = CatService.getInstance(i);
Wink Savillee68857d2014-10-17 15:23:05 -0700286 mStkContext[i] = new StkContext();
287 mStkContext[i].mSlotId = i;
288 mStkContext[i].mCmdsQ = new LinkedList<DelayedCmd>();
289 }
290
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800291 Thread serviceThread = new Thread(null, this, "Stk App Service");
292 serviceThread.start();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800293 mNotificationManager = (NotificationManager) mContext
294 .getSystemService(Context.NOTIFICATION_SERVICE);
295 sInstance = this;
296 }
297
298 @Override
299 public void onStart(Intent intent, int startId) {
Wink Savillee68857d2014-10-17 15:23:05 -0700300 if (intent == null) {
301 CatLog.d(LOG_TAG, "StkAppService onStart intent is null so return");
Banavathu, Srinivas Naik87cda962012-07-17 15:32:44 +0530302 return;
303 }
304
Wink Savillee68857d2014-10-17 15:23:05 -0700305 Bundle args = intent.getExtras();
306 if (args == null) {
307 CatLog.d(LOG_TAG, "StkAppService onStart args is null so return");
308 return;
309 }
310
311 int op = args.getInt(OPCODE);
312 int slotId = 0;
313 int i = 0;
314 if (op != OP_BOOT_COMPLETED) {
315 slotId = args.getInt(SLOT_ID);
316 }
Cuihtlauac ALVARADObde7f032015-05-29 16:25:12 +0200317 CatLog.d(LOG_TAG, "onStart sim id: " + slotId + ", op: " + op + ", *****");
Wink Savillee68857d2014-10-17 15:23:05 -0700318 if ((slotId >= 0 && slotId < mSimCount) && mStkService[slotId] == null) {
Legler Wuaeefef52014-10-27 00:57:18 +0800319 mStkService[slotId] = CatService.getInstance(slotId);
Wink Savillee68857d2014-10-17 15:23:05 -0700320 if (mStkService[slotId] == null) {
321 CatLog.d(LOG_TAG, "mStkService is: " + mStkContext[slotId].mStkServiceState);
322 mStkContext[slotId].mStkServiceState = STATE_NOT_EXIST;
323 //Check other StkService state.
324 //If all StkServices are not available, stop itself and uninstall apk.
325 for (i = PhoneConstants.SIM_ID_1; i < mSimCount; i++) {
326 if (i != slotId
Tsukasa Goto029332b2016-10-05 17:12:06 +0900327 && (mStkService[i] != null)
Wink Savillee68857d2014-10-17 15:23:05 -0700328 && (mStkContext[i].mStkServiceState == STATE_UNKNOWN
329 || mStkContext[i].mStkServiceState == STATE_EXIST)) {
330 break;
331 }
332 }
333 } else {
334 mStkContext[slotId].mStkServiceState = STATE_EXIST;
335 }
336 if (i == mSimCount) {
337 stopSelf();
338 StkAppInstaller.unInstall(mContext);
339 return;
340 }
341 }
342
Banavathu, Srinivas Naik87cda962012-07-17 15:32:44 +0530343 waitForLooper();
John Wang62acae42009-10-08 11:20:23 -0700344
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800345 Message msg = mServiceHandler.obtainMessage();
Wink Savillee68857d2014-10-17 15:23:05 -0700346 msg.arg1 = op;
347 msg.arg2 = slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800348 switch(msg.arg1) {
349 case OP_CMD:
350 msg.obj = args.getParcelable(CMD_MSG);
351 break;
352 case OP_RESPONSE:
Wink Saville94e982b2014-07-11 07:38:14 -0700353 case OP_CARD_STATUS_CHANGED:
Preeti Ahuja95919342013-10-01 18:18:55 -0700354 case OP_LOCALE_CHANGED:
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +0530355 case OP_ALPHA_NOTIFY:
Preeti Ahuja560be362014-11-25 19:38:24 -0800356 case OP_IDLE_SCREEN:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800357 msg.obj = args;
358 /* falls through */
359 case OP_LAUNCH_APP:
360 case OP_END_SESSION:
361 case OP_BOOT_COMPLETED:
362 break;
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530363 case OP_STOP_TONE_USER:
364 msg.obj = args;
365 msg.what = STOP_TONE_WHAT;
366 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800367 default:
368 return;
369 }
370 mServiceHandler.sendMessage(msg);
371 }
372
373 @Override
374 public void onDestroy() {
Wink Savillee68857d2014-10-17 15:23:05 -0700375 CatLog.d(LOG_TAG, "onDestroy()");
Preeti Ahuja560be362014-11-25 19:38:24 -0800376 if (mStkCmdReceiver != null) {
377 unregisterReceiver(mStkCmdReceiver);
378 mStkCmdReceiver = null;
379 }
380 mPowerManager = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800381 waitForLooper();
382 mServiceLooper.quit();
383 }
384
385 @Override
386 public IBinder onBind(Intent intent) {
387 return null;
388 }
389
390 public void run() {
391 Looper.prepare();
392
393 mServiceLooper = Looper.myLooper();
394 mServiceHandler = new ServiceHandler();
395
396 Looper.loop();
397 }
398
399 /*
400 * Package api used by StkMenuActivity to indicate if its on the foreground.
401 */
Wink Savillee68857d2014-10-17 15:23:05 -0700402 void indicateMenuVisibility(boolean visibility, int slotId) {
403 if (slotId >= 0 && slotId < mSimCount) {
404 mStkContext[slotId].mMenuIsVisible = visibility;
405 }
406 }
407
Preeti Ahuja95919342013-10-01 18:18:55 -0700408 /*
409 * Package api used by StkDialogActivity to indicate if its on the foreground.
410 */
411 void setDisplayTextDlgVisibility(boolean visibility, int slotId) {
412 if (slotId >= 0 && slotId < mSimCount) {
413 mStkContext[slotId].mDisplayTextDlgIsVisibile = visibility;
414 }
415 }
416
Wink Savillee68857d2014-10-17 15:23:05 -0700417 boolean isInputPending(int slotId) {
418 if (slotId >= 0 && slotId < mSimCount) {
419 CatLog.d(LOG_TAG, "isInputFinishBySrv: " + mStkContext[slotId].mIsInputPending);
420 return mStkContext[slotId].mIsInputPending;
421 }
422 return false;
423 }
424
425 boolean isMenuPending(int slotId) {
426 if (slotId >= 0 && slotId < mSimCount) {
427 CatLog.d(LOG_TAG, "isMenuPending: " + mStkContext[slotId].mIsMenuPending);
428 return mStkContext[slotId].mIsMenuPending;
429 }
430 return false;
431 }
432
433 boolean isDialogPending(int slotId) {
434 if (slotId >= 0 && slotId < mSimCount) {
435 CatLog.d(LOG_TAG, "isDialogPending: " + mStkContext[slotId].mIsDialogPending);
436 return mStkContext[slotId].mIsDialogPending;
437 }
438 return false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800439 }
440
441 /*
442 * Package api used by StkMenuActivity to get its Menu parameter.
443 */
Wink Savillee68857d2014-10-17 15:23:05 -0700444 Menu getMenu(int slotId) {
445 CatLog.d(LOG_TAG, "StkAppService, getMenu, sim id: " + slotId);
446 if (slotId >=0 && slotId < mSimCount) {
447 return mStkContext[slotId].mCurrentMenu;
448 } else {
449 return null;
450 }
451 }
452
453 /*
454 * Package api used by StkMenuActivity to get its Main Menu parameter.
455 */
456 Menu getMainMenu(int slotId) {
457 CatLog.d(LOG_TAG, "StkAppService, getMainMenu, sim id: " + slotId);
w30234a08cffe2015-02-04 15:13:25 -0800458 if (slotId >=0 && slotId < mSimCount && (mStkContext[slotId].mMainCmd != null)) {
Takanori Nakanoc8054ba2016-08-15 19:18:16 +0900459 Menu menu = mStkContext[slotId].mMainCmd.getMenu();
460 if (menu != null && mSimCount > PhoneConstants.MAX_PHONE_COUNT_SINGLE_SIM) {
461 // If alpha identifier or icon identifier with the self-explanatory qualifier is
462 // specified in SET-UP MENU command, it should be more prioritized than preset ones.
463 if (menu.title == null
464 && (menu.titleIcon == null || !menu.titleIconSelfExplanatory)) {
465 StkMenuConfig config = StkMenuConfig.getInstance(getApplicationContext());
466 String label = config.getLabel(slotId);
467 Bitmap icon = config.getIcon(slotId);
468 if (label != null || icon != null) {
469 Parcel parcel = Parcel.obtain();
470 menu.writeToParcel(parcel, 0);
471 parcel.setDataPosition(0);
472 menu = Menu.CREATOR.createFromParcel(parcel);
473 parcel.recycle();
474 menu.title = label;
475 menu.titleIcon = icon;
476 menu.titleIconSelfExplanatory = false;
477 }
478 }
479 }
480 return menu;
Wink Savillee68857d2014-10-17 15:23:05 -0700481 } else {
482 return null;
483 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800484 }
485
486 /*
487 * Package api used by UI Activities and Dialogs to communicate directly
488 * with the service to deliver state information and parameters.
489 */
490 static StkAppService getInstance() {
491 return sInstance;
492 }
493
494 private void waitForLooper() {
495 while (mServiceHandler == null) {
496 synchronized (this) {
497 try {
498 wait(100);
499 } catch (InterruptedException e) {
500 }
501 }
502 }
503 }
504
505 private final class ServiceHandler extends Handler {
506 @Override
507 public void handleMessage(Message msg) {
Wink Savillee68857d2014-10-17 15:23:05 -0700508 if(null == msg) {
509 CatLog.d(LOG_TAG, "ServiceHandler handleMessage msg is null");
510 return;
511 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800512 int opcode = msg.arg1;
Wink Savillee68857d2014-10-17 15:23:05 -0700513 int slotId = msg.arg2;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800514
Wink Savillee68857d2014-10-17 15:23:05 -0700515 CatLog.d(LOG_TAG, "handleMessage opcode[" + opcode + "], sim id[" + slotId + "]");
516 if (opcode == OP_CMD && msg.obj != null &&
517 ((CatCmdMessage)msg.obj).getCmdType()!= null) {
518 CatLog.d(LOG_TAG, "cmdName[" + ((CatCmdMessage)msg.obj).getCmdType().name() + "]");
519 }
520 mStkContext[slotId].mOpCode = opcode;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800521 switch (opcode) {
522 case OP_LAUNCH_APP:
Wink Savillee68857d2014-10-17 15:23:05 -0700523 if (mStkContext[slotId].mMainCmd == null) {
524 CatLog.d(LOG_TAG, "mMainCmd is null");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800525 // nothing todo when no SET UP MENU command didn't arrive.
526 return;
527 }
Wink Savillee68857d2014-10-17 15:23:05 -0700528 CatLog.d(LOG_TAG, "handleMessage OP_LAUNCH_APP - mCmdInProgress[" +
529 mStkContext[slotId].mCmdInProgress + "]");
530
531 //If there is a pending activity for the slot id,
532 //just finish it and create a new one to handle the pending command.
533 cleanUpInstanceStackBySlot(slotId);
534
Wink Savillee68857d2014-10-17 15:23:05 -0700535 CatLog.d(LOG_TAG, "Current cmd type: " +
536 mStkContext[slotId].mCurrentCmd.getCmdType());
537 //Restore the last command from stack by slot id.
538 restoreInstanceFromStackBySlot(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800539 break;
540 case OP_CMD:
Wink Savillee68857d2014-10-17 15:23:05 -0700541 CatLog.d(LOG_TAG, "[OP_CMD]");
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700542 CatCmdMessage cmdMsg = (CatCmdMessage) msg.obj;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800543 // There are two types of commands:
544 // 1. Interactive - user's response is required.
545 // 2. Informative - display a message, no interaction with the user.
546 //
Wink Saville79085fc2009-06-09 10:27:23 -0700547 // Informative commands can be handled immediately without any delay.
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800548 // Interactive commands can't override each other. So if a command
549 // is already in progress, we need to queue the next command until
550 // the user has responded or a timeout expired.
551 if (!isCmdInteractive(cmdMsg)) {
Wink Savillee68857d2014-10-17 15:23:05 -0700552 handleCmd(cmdMsg, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800553 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700554 if (!mStkContext[slotId].mCmdInProgress) {
555 mStkContext[slotId].mCmdInProgress = true;
556 handleCmd((CatCmdMessage) msg.obj, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800557 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700558 CatLog.d(LOG_TAG, "[Interactive][in progress]");
559 mStkContext[slotId].mCmdsQ.addLast(new DelayedCmd(OP_CMD,
560 (CatCmdMessage) msg.obj, slotId));
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800561 }
562 }
563 break;
564 case OP_RESPONSE:
Preeti Ahuja414bc412013-06-25 19:31:49 -0700565 handleCmdResponse((Bundle) msg.obj, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800566 // call delayed commands if needed.
Wink Savillee68857d2014-10-17 15:23:05 -0700567 if (mStkContext[slotId].mCmdsQ.size() != 0) {
568 callDelayedMsg(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800569 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700570 mStkContext[slotId].mCmdInProgress = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800571 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800572 break;
573 case OP_END_SESSION:
Wink Savillee68857d2014-10-17 15:23:05 -0700574 if (!mStkContext[slotId].mCmdInProgress) {
575 mStkContext[slotId].mCmdInProgress = true;
576 handleSessionEnd(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800577 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700578 mStkContext[slotId].mCmdsQ.addLast(
579 new DelayedCmd(OP_END_SESSION, null, slotId));
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800580 }
581 break;
582 case OP_BOOT_COMPLETED:
Wink Savillee68857d2014-10-17 15:23:05 -0700583 CatLog.d(LOG_TAG, " OP_BOOT_COMPLETED");
584 int i = 0;
585 for (i = PhoneConstants.SIM_ID_1; i < mSimCount; i++) {
586 if (mStkContext[i].mMainCmd != null) {
587 break;
588 }
589 }
590 if (i == mSimCount) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800591 StkAppInstaller.unInstall(mContext);
592 }
593 break;
594 case OP_DELAYED_MSG:
Wink Savillee68857d2014-10-17 15:23:05 -0700595 handleDelayedCmd(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800596 break;
Wink Saville94e982b2014-07-11 07:38:14 -0700597 case OP_CARD_STATUS_CHANGED:
Wink Savillee68857d2014-10-17 15:23:05 -0700598 CatLog.d(LOG_TAG, "Card/Icc Status change received");
599 handleCardStatusChangeAndIccRefresh((Bundle) msg.obj, slotId);
600 break;
601 case OP_SET_ACT_INST:
602 Activity act = new Activity();
603 act = (Activity) msg.obj;
604 CatLog.d(LOG_TAG, "Set activity instance. " + act);
605 mStkContext[slotId].mActivityInstance = act;
606 break;
607 case OP_SET_DAL_INST:
608 Activity dal = new Activity();
609 CatLog.d(LOG_TAG, "Set dialog instance. " + dal);
610 dal = (Activity) msg.obj;
611 mStkContext[slotId].mDialogInstance = dal;
612 break;
613 case OP_SET_MAINACT_INST:
614 Activity mainAct = new Activity();
615 mainAct = (Activity) msg.obj;
616 CatLog.d(LOG_TAG, "Set activity instance. " + mainAct);
617 mStkContext[slotId].mMainActivityInstance = mainAct;
Wink Saville94e982b2014-07-11 07:38:14 -0700618 break;
Preeti Ahuja95919342013-10-01 18:18:55 -0700619 case OP_LOCALE_CHANGED:
620 CatLog.d(this, "Locale Changed");
Yuta Ui1481b172016-02-03 15:34:31 +0900621 for (int slot = PhoneConstants.SIM_ID_1; slot < mSimCount; slot++) {
622 checkForSetupEvent(LANGUAGE_SELECTION_EVENT, (Bundle) msg.obj, slot);
623 }
fionaxu805eb572017-05-02 10:57:30 -0700624 // rename all registered notification channels on locale change
625 createAllChannels();
Preeti Ahuja95919342013-10-01 18:18:55 -0700626 break;
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +0530627 case OP_ALPHA_NOTIFY:
628 handleAlphaNotify((Bundle) msg.obj);
629 break;
Preeti Ahuja560be362014-11-25 19:38:24 -0800630 case OP_IDLE_SCREEN:
631 for (int slot = 0; slot < mSimCount; slot++) {
632 if (mStkContext[slot] != null) {
633 handleIdleScreen(slot);
634 }
635 }
636 break;
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530637 case OP_STOP_TONE_USER:
638 case OP_STOP_TONE:
639 CatLog.d(this, "Stop tone");
640 handleStopTone(msg, slotId);
641 break;
Wink Saville94e982b2014-07-11 07:38:14 -0700642 }
643 }
644
Wink Savillee68857d2014-10-17 15:23:05 -0700645 private void handleCardStatusChangeAndIccRefresh(Bundle args, int slotId) {
Wink Saville94e982b2014-07-11 07:38:14 -0700646 boolean cardStatus = args.getBoolean(AppInterface.CARD_STATUS);
647
Wink Savillee68857d2014-10-17 15:23:05 -0700648 CatLog.d(LOG_TAG, "CardStatus: " + cardStatus);
Wink Saville94e982b2014-07-11 07:38:14 -0700649 if (cardStatus == false) {
Wink Savillee68857d2014-10-17 15:23:05 -0700650 CatLog.d(LOG_TAG, "CARD is ABSENT");
Wink Saville94e982b2014-07-11 07:38:14 -0700651 // Uninstall STKAPP, Clear Idle text, Stop StkAppService
Wink Savillee68857d2014-10-17 15:23:05 -0700652 mNotificationManager.cancel(getNotificationId(slotId));
Srikanth Chintala7c5a04c2016-09-22 19:05:01 +0530653 mStkContext[slotId].mCurrentMenu = null;
654 mStkContext[slotId].mMainCmd = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700655 if (isAllOtherCardsAbsent(slotId)) {
656 CatLog.d(LOG_TAG, "All CARDs are ABSENT");
657 StkAppInstaller.unInstall(mContext);
658 stopSelf();
659 }
Wink Saville94e982b2014-07-11 07:38:14 -0700660 } else {
661 IccRefreshResponse state = new IccRefreshResponse();
662 state.refreshResult = args.getInt(AppInterface.REFRESH_RESULT);
663
Wink Savillee68857d2014-10-17 15:23:05 -0700664 CatLog.d(LOG_TAG, "Icc Refresh Result: "+ state.refreshResult);
Wink Saville94e982b2014-07-11 07:38:14 -0700665 if ((state.refreshResult == IccRefreshResponse.REFRESH_RESULT_INIT) ||
666 (state.refreshResult == IccRefreshResponse.REFRESH_RESULT_RESET)) {
667 // Clear Idle Text
Wink Savillee68857d2014-10-17 15:23:05 -0700668 mNotificationManager.cancel(getNotificationId(slotId));
Wink Saville94e982b2014-07-11 07:38:14 -0700669 }
670
671 if (state.refreshResult == IccRefreshResponse.REFRESH_RESULT_RESET) {
672 // Uninstall STkmenu
Wink Savillee68857d2014-10-17 15:23:05 -0700673 if (isAllOtherCardsAbsent(slotId)) {
674 StkAppInstaller.unInstall(mContext);
675 }
676 mStkContext[slotId].mCurrentMenu = null;
677 mStkContext[slotId].mMainCmd = null;
Wink Saville94e982b2014-07-11 07:38:14 -0700678 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800679 }
680 }
681 }
Wink Savillee68857d2014-10-17 15:23:05 -0700682 /*
683 * Check if all SIMs are absent except the id of slot equals "slotId".
684 */
685 private boolean isAllOtherCardsAbsent(int slotId) {
686 TelephonyManager mTm = (TelephonyManager) mContext.getSystemService(
687 Context.TELEPHONY_SERVICE);
688 int i = 0;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800689
Wink Savillee68857d2014-10-17 15:23:05 -0700690 for (i = 0; i < mSimCount; i++) {
691 if (i != slotId && mTm.hasIccCard(i)) {
692 break;
693 }
694 }
695 if (i == mSimCount) {
696 return true;
697 } else {
698 return false;
699 }
700 }
Preeti Ahuja95919342013-10-01 18:18:55 -0700701
Preeti Ahuja560be362014-11-25 19:38:24 -0800702 /*
703 * If the device is not in an interactive state, we can assume
704 * that the screen is idle.
705 */
706 private boolean isScreenIdle() {
707 return (!mPowerManager.isInteractive());
708 }
709
710 private void handleIdleScreen(int slotId) {
711
712 // If the idle screen event is present in the list need to send the
713 // response to SIM.
714 CatLog.d(this, "Need to send IDLE SCREEN Available event to SIM");
715 checkForSetupEvent(IDLE_SCREEN_AVAILABLE_EVENT, null, slotId);
716
717 if (mStkContext[slotId].mIdleModeTextCmd != null) {
718 launchIdleText(slotId);
719 }
720 }
721
722 private void sendScreenBusyResponse(int slotId) {
723 if (mStkContext[slotId].mCurrentCmd == null) {
724 return;
725 }
726 CatResponseMessage resMsg = new CatResponseMessage(mStkContext[slotId].mCurrentCmd);
727 CatLog.d(this, "SCREEN_BUSY");
728 resMsg.setResultCode(ResultCode.TERMINAL_CRNTLY_UNABLE_TO_PROCESS);
729 mStkService[slotId].onCmdResponse(resMsg);
730 if (mStkContext[slotId].mCmdsQ.size() != 0) {
731 callDelayedMsg(slotId);
732 } else {
733 mStkContext[slotId].mCmdInProgress = false;
734 }
735 }
736
Preeti Ahuja95919342013-10-01 18:18:55 -0700737 private void sendResponse(int resId, int slotId, boolean confirm) {
738 Message msg = mServiceHandler.obtainMessage();
739 msg.arg1 = OP_RESPONSE;
Takanori Nakano49b12722016-02-16 14:34:14 +0900740 msg.arg2 = slotId;
Preeti Ahuja95919342013-10-01 18:18:55 -0700741 Bundle args = new Bundle();
742 args.putInt(StkAppService.RES_ID, resId);
Preeti Ahuja95919342013-10-01 18:18:55 -0700743 args.putBoolean(StkAppService.CONFIRMATION, confirm);
744 msg.obj = args;
745 mServiceHandler.sendMessage(msg);
746 }
747
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700748 private boolean isCmdInteractive(CatCmdMessage cmd) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800749 switch (cmd.getCmdType()) {
750 case SEND_DTMF:
751 case SEND_SMS:
752 case SEND_SS:
753 case SEND_USSD:
754 case SET_UP_IDLE_MODE_TEXT:
755 case SET_UP_MENU:
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500756 case CLOSE_CHANNEL:
757 case RECEIVE_DATA:
758 case SEND_DATA:
Preeti Ahuja95919342013-10-01 18:18:55 -0700759 case SET_UP_EVENT_LIST:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800760 return false;
761 }
762
763 return true;
764 }
765
Wink Savillee68857d2014-10-17 15:23:05 -0700766 private void handleDelayedCmd(int slotId) {
767 CatLog.d(LOG_TAG, "handleDelayedCmd, slotId: " + slotId);
768 if (mStkContext[slotId].mCmdsQ.size() != 0) {
769 DelayedCmd cmd = mStkContext[slotId].mCmdsQ.poll();
770 if (cmd != null) {
771 CatLog.d(LOG_TAG, "handleDelayedCmd - queue size: " +
772 mStkContext[slotId].mCmdsQ.size() +
773 " id: " + cmd.id + "sim id: " + cmd.slotId);
774 switch (cmd.id) {
775 case OP_CMD:
776 handleCmd(cmd.msg, cmd.slotId);
777 break;
778 case OP_END_SESSION:
779 handleSessionEnd(cmd.slotId);
780 break;
781 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800782 }
783 }
784 }
785
Wink Savillee68857d2014-10-17 15:23:05 -0700786 private void callDelayedMsg(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800787 Message msg = mServiceHandler.obtainMessage();
788 msg.arg1 = OP_DELAYED_MSG;
Wink Savillee68857d2014-10-17 15:23:05 -0700789 msg.arg2 = slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800790 mServiceHandler.sendMessage(msg);
791 }
792
Wink Savillee68857d2014-10-17 15:23:05 -0700793 private void callSetActivityInstMsg(int inst_type, int slotId, Object obj) {
794 Message msg = mServiceHandler.obtainMessage();
795 msg.obj = obj;
796 msg.arg1 = inst_type;
797 msg.arg2 = slotId;
798 mServiceHandler.sendMessage(msg);
799 }
800
801 private void handleSessionEnd(int slotId) {
Legler Wuaeefef52014-10-27 00:57:18 +0800802 // We should finish all pending activity if receiving END SESSION command.
803 cleanUpInstanceStackBySlot(slotId);
804
Wink Savillee68857d2014-10-17 15:23:05 -0700805 mStkContext[slotId].mCurrentCmd = mStkContext[slotId].mMainCmd;
806 CatLog.d(LOG_TAG, "[handleSessionEnd] - mCurrentCmd changed to mMainCmd!.");
807 mStkContext[slotId].mCurrentMenuCmd = mStkContext[slotId].mMainCmd;
808 CatLog.d(LOG_TAG, "slotId: " + slotId + ", mMenuState: " +
809 mStkContext[slotId].mMenuState);
810
811 mStkContext[slotId].mIsInputPending = false;
812 mStkContext[slotId].mIsMenuPending = false;
813 mStkContext[slotId].mIsDialogPending = false;
814
Wink Savillee68857d2014-10-17 15:23:05 -0700815 if (mStkContext[slotId].mMainCmd == null) {
816 CatLog.d(LOG_TAG, "[handleSessionEnd][mMainCmd is null!]");
817 }
818 mStkContext[slotId].lastSelectedItem = null;
Wink Saville79085fc2009-06-09 10:27:23 -0700819 // In case of SET UP MENU command which removed the app, don't
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800820 // update the current menu member.
Wink Savillee68857d2014-10-17 15:23:05 -0700821 if (mStkContext[slotId].mCurrentMenu != null && mStkContext[slotId].mMainCmd != null) {
822 mStkContext[slotId].mCurrentMenu = mStkContext[slotId].mMainCmd.getMenu();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800823 }
Wink Savillee68857d2014-10-17 15:23:05 -0700824 CatLog.d(LOG_TAG, "[handleSessionEnd][mMenuState]" + mStkContext[slotId].mMenuIsVisible);
825 // In mutiple instance architecture, the main menu for slotId will be finished when user
826 // goes to the Stk menu of the other SIM. So, we should launch a new instance for the
827 // main menu if the main menu instance has been finished.
828 // If the current menu is secondary menu, we should launch main menu.
829 if (StkMenuActivity.STATE_SECONDARY == mStkContext[slotId].mMenuState) {
830 launchMenuActivity(null, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800831 }
Wink Savillee68857d2014-10-17 15:23:05 -0700832 if (mStkContext[slotId].mCmdsQ.size() != 0) {
833 callDelayedMsg(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800834 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700835 mStkContext[slotId].mCmdInProgress = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800836 }
837 // In case a launch browser command was just confirmed, launch that url.
Wink Savillee68857d2014-10-17 15:23:05 -0700838 if (mStkContext[slotId].launchBrowser) {
839 mStkContext[slotId].launchBrowser = false;
840 launchBrowser(mStkContext[slotId].mBrowserSettings);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800841 }
842 }
843
Preeti Ahuja560be362014-11-25 19:38:24 -0800844 // returns true if any Stk related activity already has focus on the screen
845 private boolean isTopOfStack() {
pkanwareab12f32017-02-06 08:35:03 -0800846 ActivityManager mActivityManager = (ActivityManager) mContext
Preeti Ahuja560be362014-11-25 19:38:24 -0800847 .getSystemService(ACTIVITY_SERVICE);
pkanwareab12f32017-02-06 08:35:03 -0800848 String currentPackageName = null;
849 List<RunningTaskInfo> tasks = mActivityManager.getRunningTasks(1);
850 if (tasks == null || tasks.get(0).topActivity == null) {
851 return false;
852 }
853 currentPackageName = tasks.get(0).topActivity.getPackageName();
Preeti Ahuja560be362014-11-25 19:38:24 -0800854 if (null != currentPackageName) {
855 return currentPackageName.equals(PACKAGE_NAME);
856 }
Preeti Ahuja560be362014-11-25 19:38:24 -0800857 return false;
858 }
859
Sooraj Sasindrana4160472016-10-12 16:28:25 -0700860 /**
861 * Get the boolean config from carrier config manager.
862 *
863 * @param context the context to get carrier service
864 * @param key config key defined in CarrierConfigManager
865 * @return boolean value of corresponding key.
866 */
867 private static boolean getBooleanCarrierConfig(Context context, String key) {
868 CarrierConfigManager configManager = (CarrierConfigManager) context.getSystemService(
869 Context.CARRIER_CONFIG_SERVICE);
870 PersistableBundle b = null;
871 if (configManager != null) {
872 b = configManager.getConfig();
873 }
874 if (b != null) {
875 return b.getBoolean(key);
876 } else {
877 // Return static default defined in CarrierConfigManager.
878 return CarrierConfigManager.getDefaultConfig().getBoolean(key);
879 }
880 }
881
Wink Savillee68857d2014-10-17 15:23:05 -0700882 private void handleCmd(CatCmdMessage cmdMsg, int slotId) {
Preeti Ahuja95919342013-10-01 18:18:55 -0700883
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800884 if (cmdMsg == null) {
885 return;
886 }
887 // save local reference for state tracking.
Wink Savillee68857d2014-10-17 15:23:05 -0700888 mStkContext[slotId].mCurrentCmd = cmdMsg;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800889 boolean waitForUsersResponse = true;
890
Wink Savillee68857d2014-10-17 15:23:05 -0700891 mStkContext[slotId].mIsInputPending = false;
892 mStkContext[slotId].mIsMenuPending = false;
893 mStkContext[slotId].mIsDialogPending = false;
894
895 CatLog.d(LOG_TAG,"[handleCmd]" + cmdMsg.getCmdType().name());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800896 switch (cmdMsg.getCmdType()) {
897 case DISPLAY_TEXT:
898 TextMessage msg = cmdMsg.geTextMessage();
Jeevaka Badrappan854a25c2012-12-01 16:32:03 +0200899 waitForUsersResponse = msg.responseNeeded;
Wink Savillee68857d2014-10-17 15:23:05 -0700900 if (mStkContext[slotId].lastSelectedItem != null) {
901 msg.title = mStkContext[slotId].lastSelectedItem;
902 } else if (mStkContext[slotId].mMainCmd != null){
Yoshiaki Naka28313ba2017-08-04 12:20:53 +0900903 if (!getResources().getBoolean(R.bool.show_menu_title_only_on_menu)) {
904 msg.title = mStkContext[slotId].mMainCmd.getMenu().title;
905 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800906 }
Preeti Ahuja560be362014-11-25 19:38:24 -0800907 //If we receive a low priority Display Text and the device is
908 // not displaying any STK related activity and the screen is not idle
909 // ( that is, device is in an interactive state), then send a screen busy
910 // terminal response. Otherwise display the message. The existing
911 // displayed message shall be updated with the new display text
912 // proactive command (Refer to ETSI TS 102 384 section 27.22.4.1.4.4.2).
913 if (!(msg.isHighPriority || mStkContext[slotId].mMenuIsVisible
914 || mStkContext[slotId].mDisplayTextDlgIsVisibile || isTopOfStack())) {
915 if(!isScreenIdle()) {
916 CatLog.d(LOG_TAG, "Screen is not idle");
917 sendScreenBusyResponse(slotId);
918 } else {
919 launchTextDialog(slotId);
920 }
921 } else {
922 launchTextDialog(slotId);
923 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800924 break;
925 case SELECT_ITEM:
Wink Savillee68857d2014-10-17 15:23:05 -0700926 CatLog.d(LOG_TAG, "SELECT_ITEM +");
927 mStkContext[slotId].mCurrentMenuCmd = mStkContext[slotId].mCurrentCmd;
928 mStkContext[slotId].mCurrentMenu = cmdMsg.getMenu();
929 launchMenuActivity(cmdMsg.getMenu(), slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800930 break;
931 case SET_UP_MENU:
Wink Savillee68857d2014-10-17 15:23:05 -0700932 mStkContext[slotId].mCmdInProgress = false;
933 mStkContext[slotId].mMainCmd = mStkContext[slotId].mCurrentCmd;
934 mStkContext[slotId].mCurrentMenuCmd = mStkContext[slotId].mCurrentCmd;
935 mStkContext[slotId].mCurrentMenu = cmdMsg.getMenu();
936 CatLog.d(LOG_TAG, "SET_UP_MENU [" + removeMenu(slotId) + "]");
937
938 if (removeMenu(slotId)) {
939 int i = 0;
940 CatLog.d(LOG_TAG, "removeMenu() - Uninstall App");
941 mStkContext[slotId].mCurrentMenu = null;
Preeti Ahuja95919342013-10-01 18:18:55 -0700942 mStkContext[slotId].mMainCmd = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700943 //Check other setup menu state. If all setup menu are removed, uninstall apk.
944 for (i = PhoneConstants.SIM_ID_1; i < mSimCount; i++) {
945 if (i != slotId
Ryuto Sawadaf51e5e02016-11-25 17:12:49 +0900946 && (mStkContext[i].mSetupMenuState == STATE_UNKNOWN
947 || mStkContext[i].mSetupMenuState == STATE_EXIST)) {
Wink Savillee68857d2014-10-17 15:23:05 -0700948 CatLog.d(LOG_TAG, "Not Uninstall App:" + i + ","
Ryuto Sawadaf51e5e02016-11-25 17:12:49 +0900949 + mStkContext[i].mSetupMenuState);
Wink Savillee68857d2014-10-17 15:23:05 -0700950 break;
951 }
952 }
953 if (i == mSimCount) {
954 StkAppInstaller.unInstall(mContext);
955 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800956 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700957 CatLog.d(LOG_TAG, "install App");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800958 StkAppInstaller.install(mContext);
959 }
Wink Savillee68857d2014-10-17 15:23:05 -0700960 if (mStkContext[slotId].mMenuIsVisible) {
961 launchMenuActivity(null, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800962 }
963 break;
964 case GET_INPUT:
965 case GET_INKEY:
Wink Savillee68857d2014-10-17 15:23:05 -0700966 launchInputActivity(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800967 break;
968 case SET_UP_IDLE_MODE_TEXT:
969 waitForUsersResponse = false;
Preeti Ahuja560be362014-11-25 19:38:24 -0800970 mStkContext[slotId].mIdleModeTextCmd = mStkContext[slotId].mCurrentCmd;
971 TextMessage idleModeText = mStkContext[slotId].mCurrentCmd.geTextMessage();
972 if (idleModeText == null) {
973 launchIdleText(slotId);
974 mStkContext[slotId].mIdleModeTextCmd = null;
975 }
976 mStkContext[slotId].mCurrentCmd = mStkContext[slotId].mMainCmd;
977 if ((mStkContext[slotId].mIdleModeTextCmd != null) && isScreenIdle()) {
978 CatLog.d(this, "set up idle mode");
979 launchIdleText(slotId);
980 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800981 break;
982 case SEND_DTMF:
983 case SEND_SMS:
984 case SEND_SS:
985 case SEND_USSD:
Preeti Ahuja95919342013-10-01 18:18:55 -0700986 case GET_CHANNEL_STATUS:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800987 waitForUsersResponse = false;
Wink Savillee68857d2014-10-17 15:23:05 -0700988 launchEventMessage(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800989 break;
990 case LAUNCH_BROWSER:
Ryuto Sawada0bdc7192016-04-18 12:10:56 +0900991 // The device setup process should not be interrupted by launching browser.
992 if (Settings.Global.getInt(mContext.getContentResolver(),
993 Settings.Global.DEVICE_PROVISIONED, 0) == 0) {
994 CatLog.d(this, "The command is not performed if the setup has not been completed.");
995 sendScreenBusyResponse(slotId);
996 break;
997 }
Sooraj Sasindrana4160472016-10-12 16:28:25 -0700998
999 /* Check if Carrier would not want to launch browser */
1000 if (getBooleanCarrierConfig(mContext,
1001 CarrierConfigManager.KEY_STK_DISABLE_LAUNCH_BROWSER_BOOL)) {
1002 CatLog.d(this, "Browser is not launched as per carrier.");
1003 sendResponse(RES_ID_DONE, slotId, true);
1004 break;
1005 }
1006
Srikanth Chintalaba103002015-11-30 10:49:52 -08001007 mStkContext[slotId].mBrowserSettings =
1008 mStkContext[slotId].mCurrentCmd.getBrowserSettings();
1009 if (!isUrlAvailableToLaunchBrowser(mStkContext[slotId].mBrowserSettings)) {
1010 CatLog.d(this, "Browser url property is not set - send error");
1011 sendResponse(RES_ID_ERROR, slotId, true);
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001012 } else {
Srikanth Chintalaba103002015-11-30 10:49:52 -08001013 TextMessage alphaId = mStkContext[slotId].mCurrentCmd.geTextMessage();
1014 if ((alphaId == null) || TextUtils.isEmpty(alphaId.text)) {
1015 // don't need user confirmation in this case
1016 // just launch the browser or spawn a new tab
1017 CatLog.d(this, "user confirmation is not currently needed.\n" +
1018 "supressing confirmation dialogue and confirming silently...");
1019 mStkContext[slotId].launchBrowser = true;
1020 sendResponse(RES_ID_CONFIRM, slotId, true);
1021 } else {
1022 launchConfirmationDialog(alphaId, slotId);
1023 }
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001024 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001025 break;
1026 case SET_UP_CALL:
Preeti Ahujadd240102013-08-30 17:25:06 -07001027 TextMessage mesg = mStkContext[slotId].mCurrentCmd.getCallSettings().confirmMsg;
1028 if((mesg != null) && (mesg.text == null || mesg.text.length() == 0)) {
1029 mesg.text = getResources().getString(R.string.default_setup_call_msg);
1030 }
1031 CatLog.d(this, "SET_UP_CALL mesg.text " + mesg.text);
1032 launchConfirmationDialog(mesg, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001033 break;
1034 case PLAY_TONE:
Srikanth Chintala89aa6602014-03-14 16:26:57 +05301035 handlePlayTone(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001036 break;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001037 case OPEN_CHANNEL:
Wink Savillee68857d2014-10-17 15:23:05 -07001038 launchOpenChannelDialog(slotId);
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001039 break;
1040 case CLOSE_CHANNEL:
1041 case RECEIVE_DATA:
1042 case SEND_DATA:
Wink Savillee68857d2014-10-17 15:23:05 -07001043 TextMessage m = mStkContext[slotId].mCurrentCmd.geTextMessage();
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001044
1045 if ((m != null) && (m.text == null)) {
1046 switch(cmdMsg.getCmdType()) {
1047 case CLOSE_CHANNEL:
1048 m.text = getResources().getString(R.string.default_close_channel_msg);
1049 break;
1050 case RECEIVE_DATA:
1051 m.text = getResources().getString(R.string.default_receive_data_msg);
1052 break;
1053 case SEND_DATA:
1054 m.text = getResources().getString(R.string.default_send_data_msg);
1055 break;
1056 }
1057 }
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001058 /*
1059 * Display indication in the form of a toast to the user if required.
1060 */
Wink Savillee68857d2014-10-17 15:23:05 -07001061 launchEventMessage(slotId);
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001062 break;
Preeti Ahuja95919342013-10-01 18:18:55 -07001063 case SET_UP_EVENT_LIST:
1064 mStkContext[slotId].mSetupEventListSettings =
1065 mStkContext[slotId].mCurrentCmd.getSetEventList();
1066 mStkContext[slotId].mCurrentSetupEventCmd = mStkContext[slotId].mCurrentCmd;
1067 mStkContext[slotId].mCurrentCmd = mStkContext[slotId].mMainCmd;
Preeti Ahuja560be362014-11-25 19:38:24 -08001068 if (isScreenIdle()) {
1069 CatLog.d(this," Check if IDLE_SCREEN_AVAILABLE_EVENT is present in List");
1070 checkForSetupEvent(IDLE_SCREEN_AVAILABLE_EVENT, null, slotId);
1071 }
Preeti Ahuja95919342013-10-01 18:18:55 -07001072 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001073 }
1074
1075 if (!waitForUsersResponse) {
Wink Savillee68857d2014-10-17 15:23:05 -07001076 if (mStkContext[slotId].mCmdsQ.size() != 0) {
1077 callDelayedMsg(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001078 } else {
Wink Savillee68857d2014-10-17 15:23:05 -07001079 mStkContext[slotId].mCmdInProgress = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001080 }
1081 }
1082 }
1083
Wink Savillee68857d2014-10-17 15:23:05 -07001084 private void handleCmdResponse(Bundle args, int slotId) {
1085 CatLog.d(LOG_TAG, "handleCmdResponse, sim id: " + slotId);
1086 if (mStkContext[slotId].mCurrentCmd == null) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001087 return;
1088 }
Wink Savillee68857d2014-10-17 15:23:05 -07001089
1090 if (mStkService[slotId] == null) {
Legler Wuaeefef52014-10-27 00:57:18 +08001091 mStkService[slotId] = CatService.getInstance(slotId);
Wink Savillee68857d2014-10-17 15:23:05 -07001092 if (mStkService[slotId] == null) {
Alex Yakavenkad8e2ecd2012-04-20 17:10:15 -07001093 // This should never happen (we should be responding only to a message
1094 // that arrived from StkService). It has to exist by this time
Wink Savillee68857d2014-10-17 15:23:05 -07001095 CatLog.d(LOG_TAG, "Exception! mStkService is null when we need to send response.");
Alex Yakavenkad8e2ecd2012-04-20 17:10:15 -07001096 throw new RuntimeException("mStkService is null when we need to send response");
1097 }
1098 }
1099
Wink Savillee68857d2014-10-17 15:23:05 -07001100 CatResponseMessage resMsg = new CatResponseMessage(mStkContext[slotId].mCurrentCmd);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001101
1102 // set result code
1103 boolean helpRequired = args.getBoolean(HELP, false);
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001104 boolean confirmed = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001105
1106 switch(args.getInt(RES_ID)) {
1107 case RES_ID_MENU_SELECTION:
Wink Savillee68857d2014-10-17 15:23:05 -07001108 CatLog.d(LOG_TAG, "MENU_SELECTION=" + mStkContext[slotId].
1109 mCurrentMenuCmd.getCmdType());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001110 int menuSelection = args.getInt(MENU_SELECTION);
Wink Savillee68857d2014-10-17 15:23:05 -07001111 switch(mStkContext[slotId].mCurrentMenuCmd.getCmdType()) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001112 case SET_UP_MENU:
1113 case SELECT_ITEM:
Wink Savillee68857d2014-10-17 15:23:05 -07001114 mStkContext[slotId].lastSelectedItem = getItemName(menuSelection, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001115 if (helpRequired) {
1116 resMsg.setResultCode(ResultCode.HELP_INFO_REQUIRED);
1117 } else {
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301118 resMsg.setResultCode(mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ?
1119 ResultCode.PRFRMD_ICON_NOT_DISPLAYED : ResultCode.OK);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001120 }
1121 resMsg.setMenuSelection(menuSelection);
1122 break;
1123 }
1124 break;
1125 case RES_ID_INPUT:
Wink Savillee68857d2014-10-17 15:23:05 -07001126 CatLog.d(LOG_TAG, "RES_ID_INPUT");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001127 String input = args.getString(INPUT);
Wink Savillee68857d2014-10-17 15:23:05 -07001128 if (input != null && (null != mStkContext[slotId].mCurrentCmd.geInput()) &&
1129 (mStkContext[slotId].mCurrentCmd.geInput().yesNo)) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001130 boolean yesNoSelection = input
1131 .equals(StkInputActivity.YES_STR_RESPONSE);
1132 resMsg.setYesNo(yesNoSelection);
1133 } else {
1134 if (helpRequired) {
1135 resMsg.setResultCode(ResultCode.HELP_INFO_REQUIRED);
1136 } else {
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301137 resMsg.setResultCode(mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ?
1138 ResultCode.PRFRMD_ICON_NOT_DISPLAYED : ResultCode.OK);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001139 resMsg.setInput(input);
1140 }
1141 }
1142 break;
1143 case RES_ID_CONFIRM:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -07001144 CatLog.d(this, "RES_ID_CONFIRM");
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001145 confirmed = args.getBoolean(CONFIRMATION);
Wink Savillee68857d2014-10-17 15:23:05 -07001146 switch (mStkContext[slotId].mCurrentCmd.getCmdType()) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001147 case DISPLAY_TEXT:
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301148 if (confirmed) {
1149 resMsg.setResultCode(mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ?
1150 ResultCode.PRFRMD_ICON_NOT_DISPLAYED : ResultCode.OK);
1151 } else {
1152 resMsg.setResultCode(ResultCode.UICC_SESSION_TERM_BY_USER);
1153 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001154 break;
1155 case LAUNCH_BROWSER:
1156 resMsg.setResultCode(confirmed ? ResultCode.OK
1157 : ResultCode.UICC_SESSION_TERM_BY_USER);
1158 if (confirmed) {
Wink Savillee68857d2014-10-17 15:23:05 -07001159 mStkContext[slotId].launchBrowser = true;
1160 mStkContext[slotId].mBrowserSettings =
1161 mStkContext[slotId].mCurrentCmd.getBrowserSettings();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001162 }
1163 break;
1164 case SET_UP_CALL:
1165 resMsg.setResultCode(ResultCode.OK);
1166 resMsg.setConfirmation(confirmed);
1167 if (confirmed) {
Wink Savillee68857d2014-10-17 15:23:05 -07001168 launchEventMessage(slotId,
1169 mStkContext[slotId].mCurrentCmd.getCallSettings().callMsg);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001170 }
1171 break;
1172 }
1173 break;
1174 case RES_ID_DONE:
1175 resMsg.setResultCode(ResultCode.OK);
1176 break;
1177 case RES_ID_BACKWARD:
Wink Savillee68857d2014-10-17 15:23:05 -07001178 CatLog.d(LOG_TAG, "RES_ID_BACKWARD");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001179 resMsg.setResultCode(ResultCode.BACKWARD_MOVE_BY_USER);
1180 break;
1181 case RES_ID_END_SESSION:
Wink Savillee68857d2014-10-17 15:23:05 -07001182 CatLog.d(LOG_TAG, "RES_ID_END_SESSION");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001183 resMsg.setResultCode(ResultCode.UICC_SESSION_TERM_BY_USER);
1184 break;
1185 case RES_ID_TIMEOUT:
Wink Savillee68857d2014-10-17 15:23:05 -07001186 CatLog.d(LOG_TAG, "RES_ID_TIMEOUT");
Naveen Kallad5176892009-11-30 12:45:29 -08001187 // GCF test-case 27.22.4.1.1 Expected Sequence 1.5 (DISPLAY TEXT,
1188 // Clear message after delay, successful) expects result code OK.
1189 // If the command qualifier specifies no user response is required
1190 // then send OK instead of NO_RESPONSE_FROM_USER
Wink Savillee68857d2014-10-17 15:23:05 -07001191 if ((mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1192 AppInterface.CommandType.DISPLAY_TEXT.value())
1193 && (mStkContext[slotId].mCurrentCmd.geTextMessage().userClear == false)) {
Naveen Kallad5176892009-11-30 12:45:29 -08001194 resMsg.setResultCode(ResultCode.OK);
1195 } else {
1196 resMsg.setResultCode(ResultCode.NO_RESPONSE_FROM_USER);
1197 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001198 break;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001199 case RES_ID_CHOICE:
1200 int choice = args.getInt(CHOICE);
1201 CatLog.d(this, "User Choice=" + choice);
1202 switch (choice) {
1203 case YES:
1204 resMsg.setResultCode(ResultCode.OK);
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001205 confirmed = true;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001206 break;
1207 case NO:
1208 resMsg.setResultCode(ResultCode.USER_NOT_ACCEPT);
1209 break;
1210 }
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001211
Wink Savillee68857d2014-10-17 15:23:05 -07001212 if (mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1213 AppInterface.CommandType.OPEN_CHANNEL.value()) {
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001214 resMsg.setConfirmation(confirmed);
1215 }
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001216 break;
Srikanth Chintalaba103002015-11-30 10:49:52 -08001217 case RES_ID_ERROR:
1218 CatLog.d(LOG_TAG, "RES_ID_ERROR");
1219 switch (mStkContext[slotId].mCurrentCmd.getCmdType()) {
1220 case LAUNCH_BROWSER:
1221 resMsg.setResultCode(ResultCode.LAUNCH_BROWSER_ERROR);
1222 break;
1223 }
1224 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001225 default:
Wink Savillee68857d2014-10-17 15:23:05 -07001226 CatLog.d(LOG_TAG, "Unknown result id");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001227 return;
1228 }
Wink Savillee68857d2014-10-17 15:23:05 -07001229
1230 if (null != mStkContext[slotId].mCurrentCmd &&
1231 null != mStkContext[slotId].mCurrentCmd.getCmdType()) {
1232 CatLog.d(LOG_TAG, "handleCmdResponse- cmdName[" +
1233 mStkContext[slotId].mCurrentCmd.getCmdType().name() + "]");
1234 }
1235 mStkService[slotId].onCmdResponse(resMsg);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001236 }
1237
1238 /**
1239 * Returns 0 or FLAG_ACTIVITY_NO_USER_ACTION, 0 means the user initiated the action.
1240 *
1241 * @param userAction If the userAction is yes then we always return 0 otherwise
1242 * mMenuIsVisible is used to determine what to return. If mMenuIsVisible is true
1243 * then we are the foreground app and we'll return 0 as from our perspective a
1244 * user action did cause. If it's false than we aren't the foreground app and
1245 * FLAG_ACTIVITY_NO_USER_ACTION is returned.
Wink Saville79085fc2009-06-09 10:27:23 -07001246 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001247 * @return 0 or FLAG_ACTIVITY_NO_USER_ACTION
1248 */
Wink Savillee68857d2014-10-17 15:23:05 -07001249 private int getFlagActivityNoUserAction(InitiatedByUserAction userAction, int slotId) {
1250 return ((userAction == InitiatedByUserAction.yes) | mStkContext[slotId].mMenuIsVisible)
1251 ? 0 : Intent.FLAG_ACTIVITY_NO_USER_ACTION;
1252 }
1253 /**
1254 * This method is used for cleaning up pending instances in stack.
1255 */
1256 private void cleanUpInstanceStackBySlot(int slotId) {
1257 Activity activity = mStkContext[slotId].getPendingActivityInstance();
1258 Activity dialog = mStkContext[slotId].getPendingDialogInstance();
1259 CatLog.d(LOG_TAG, "cleanUpInstanceStackBySlot slotId: " + slotId);
Legler Wuaeefef52014-10-27 00:57:18 +08001260 if (mStkContext[slotId].mCurrentCmd == null) {
1261 CatLog.d(LOG_TAG, "current cmd is null.");
1262 return;
1263 }
Wink Savillee68857d2014-10-17 15:23:05 -07001264 if (activity != null) {
1265 CatLog.d(LOG_TAG, "current cmd type: " +
1266 mStkContext[slotId].mCurrentCmd.getCmdType());
1267 if (mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1268 AppInterface.CommandType.GET_INPUT.value() ||
1269 mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1270 AppInterface.CommandType.GET_INKEY.value()) {
1271 mStkContext[slotId].mIsInputPending = true;
1272 } else if (mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1273 AppInterface.CommandType.SET_UP_MENU.value() ||
1274 mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1275 AppInterface.CommandType.SELECT_ITEM.value()) {
1276 mStkContext[slotId].mIsMenuPending = true;
1277 } else {
1278 }
1279 CatLog.d(LOG_TAG, "finish pending activity.");
1280 activity.finish();
1281 mStkContext[slotId].mActivityInstance = null;
1282 }
1283 if (dialog != null) {
1284 CatLog.d(LOG_TAG, "finish pending dialog.");
1285 mStkContext[slotId].mIsDialogPending = true;
1286 dialog.finish();
1287 mStkContext[slotId].mDialogInstance = null;
1288 }
1289 }
1290 /**
1291 * This method is used for restoring pending instances from stack.
1292 */
1293 private void restoreInstanceFromStackBySlot(int slotId) {
1294 AppInterface.CommandType cmdType = mStkContext[slotId].mCurrentCmd.getCmdType();
1295
1296 CatLog.d(LOG_TAG, "restoreInstanceFromStackBySlot cmdType : " + cmdType);
1297 switch(cmdType) {
1298 case GET_INPUT:
1299 case GET_INKEY:
1300 launchInputActivity(slotId);
1301 //Set mMenuIsVisible to true for showing main menu for
1302 //following session end command.
1303 mStkContext[slotId].mMenuIsVisible = true;
1304 break;
1305 case DISPLAY_TEXT:
1306 launchTextDialog(slotId);
1307 break;
1308 case LAUNCH_BROWSER:
1309 launchConfirmationDialog(mStkContext[slotId].mCurrentCmd.geTextMessage(),
1310 slotId);
1311 break;
1312 case OPEN_CHANNEL:
1313 launchOpenChannelDialog(slotId);
1314 break;
1315 case SET_UP_CALL:
1316 launchConfirmationDialog(mStkContext[slotId].mCurrentCmd.getCallSettings().
1317 confirmMsg, slotId);
1318 break;
1319 case SET_UP_MENU:
1320 case SELECT_ITEM:
1321 launchMenuActivity(null, slotId);
1322 break;
1323 default:
1324 break;
1325 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001326 }
1327
Wink Savillee68857d2014-10-17 15:23:05 -07001328 private void launchMenuActivity(Menu menu, int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001329 Intent newIntent = new Intent(Intent.ACTION_VIEW);
Wink Savillee68857d2014-10-17 15:23:05 -07001330 String targetActivity = STK_MENU_ACTIVITY_NAME;
1331 String uriString = STK_MENU_URI + System.currentTimeMillis();
1332 //Set unique URI to create a new instance of activity for different slotId.
1333 Uri uriData = Uri.parse(uriString);
1334
1335 CatLog.d(LOG_TAG, "launchMenuActivity, slotId: " + slotId + " , " +
1336 uriData.toString() + " , " + mStkContext[slotId].mOpCode + ", "
1337 + mStkContext[slotId].mMenuState);
1338 newIntent.setClassName(PACKAGE_NAME, targetActivity);
1339 int intentFlags = Intent.FLAG_ACTIVITY_NEW_TASK;
1340
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001341 if (menu == null) {
1342 // We assume this was initiated by the user pressing the tool kit icon
Wink Savillee68857d2014-10-17 15:23:05 -07001343 intentFlags |= getFlagActivityNoUserAction(InitiatedByUserAction.yes, slotId);
1344 if (mStkContext[slotId].mOpCode == OP_END_SESSION) {
1345 CatLog.d(LOG_TAG, "launchMenuActivity, return OP_END_SESSION");
1346 mStkContext[slotId].mMenuState = StkMenuActivity.STATE_MAIN;
1347 if (mStkContext[slotId].mMainActivityInstance != null) {
1348 CatLog.d(LOG_TAG, "launchMenuActivity, mMainActivityInstance is not null");
1349 return;
1350 }
Wink Savillee68857d2014-10-17 15:23:05 -07001351 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001352
Wink Savillee68857d2014-10-17 15:23:05 -07001353 //If the last pending menu is secondary menu, "STATE" should be "STATE_SECONDARY".
1354 //Otherwise, it should be "STATE_MAIN".
1355 if (mStkContext[slotId].mOpCode == OP_LAUNCH_APP &&
1356 mStkContext[slotId].mMenuState == StkMenuActivity.STATE_SECONDARY) {
1357 newIntent.putExtra("STATE", StkMenuActivity.STATE_SECONDARY);
1358 } else {
1359 newIntent.putExtra("STATE", StkMenuActivity.STATE_MAIN);
1360 mStkContext[slotId].mMenuState = StkMenuActivity.STATE_MAIN;
1361 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001362 } else {
1363 // We don't know and we'll let getFlagActivityNoUserAction decide.
Wink Savillee68857d2014-10-17 15:23:05 -07001364 intentFlags |= getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001365 newIntent.putExtra("STATE", StkMenuActivity.STATE_SECONDARY);
Wink Savillee68857d2014-10-17 15:23:05 -07001366 mStkContext[slotId].mMenuState = StkMenuActivity.STATE_SECONDARY;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001367 }
Wink Savillee68857d2014-10-17 15:23:05 -07001368 newIntent.putExtra(SLOT_ID, slotId);
1369 newIntent.setData(uriData);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001370 newIntent.setFlags(intentFlags);
1371 mContext.startActivity(newIntent);
1372 }
1373
Wink Savillee68857d2014-10-17 15:23:05 -07001374 private void launchInputActivity(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001375 Intent newIntent = new Intent(Intent.ACTION_VIEW);
Wink Savillee68857d2014-10-17 15:23:05 -07001376 String targetActivity = STK_INPUT_ACTIVITY_NAME;
1377 String uriString = STK_INPUT_URI + System.currentTimeMillis();
1378 //Set unique URI to create a new instance of activity for different slotId.
1379 Uri uriData = Uri.parse(uriString);
1380
1381 CatLog.d(LOG_TAG, "launchInputActivity, slotId: " + slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001382 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
Wink Savillee68857d2014-10-17 15:23:05 -07001383 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1384 newIntent.setClassName(PACKAGE_NAME, targetActivity);
1385 newIntent.putExtra("INPUT", mStkContext[slotId].mCurrentCmd.geInput());
1386 newIntent.putExtra(SLOT_ID, slotId);
1387 newIntent.setData(uriData);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001388 mContext.startActivity(newIntent);
1389 }
1390
Wink Savillee68857d2014-10-17 15:23:05 -07001391 private void launchTextDialog(int slotId) {
1392 CatLog.d(LOG_TAG, "launchTextDialog, slotId: " + slotId);
1393 Intent newIntent = new Intent();
1394 String targetActivity = STK_DIALOG_ACTIVITY_NAME;
1395 int action = getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId);
1396 String uriString = STK_DIALOG_URI + System.currentTimeMillis();
1397 //Set unique URI to create a new instance of activity for different slotId.
1398 Uri uriData = Uri.parse(uriString);
1399 if (newIntent != null) {
1400 newIntent.setClassName(PACKAGE_NAME, targetActivity);
1401 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1402 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
1403 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1404 newIntent.setData(uriData);
1405 newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
1406 newIntent.putExtra(SLOT_ID, slotId);
1407 startActivity(newIntent);
Preeti Ahuja414bc412013-06-25 19:31:49 -07001408 // For display texts with immediate response, send the terminal response
1409 // immediately. responseNeeded will be false, if display text command has
1410 // the immediate response tlv.
1411 if (!mStkContext[slotId].mCurrentCmd.geTextMessage().responseNeeded) {
1412 sendResponse(RES_ID_CONFIRM, slotId, true);
1413 }
Wink Savillee68857d2014-10-17 15:23:05 -07001414 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001415 }
1416
Wink Savillee68857d2014-10-17 15:23:05 -07001417 public boolean isStkDialogActivated(Context context) {
1418 String stkDialogActivity = "com.android.stk.StkDialogActivity";
1419 boolean activated = false;
1420 final ActivityManager am = (ActivityManager) context.getSystemService(
1421 Context.ACTIVITY_SERVICE);
1422 String topActivity = am.getRunningTasks(1).get(0).topActivity.getClassName();
1423
1424 CatLog.d(LOG_TAG, "isStkDialogActivated: " + topActivity);
1425 if (topActivity.equals(stkDialogActivity)) {
1426 activated = true;
1427 }
1428 CatLog.d(LOG_TAG, "activated : " + activated);
1429 return activated;
Johan Hellman3aec01c2011-02-10 10:15:28 +01001430 }
1431
Preeti Ahuja95919342013-10-01 18:18:55 -07001432 private void sendSetUpEventResponse(int event, byte[] addedInfo, int slotId) {
Preeti Ahuja414bc412013-06-25 19:31:49 -07001433 CatLog.d(this, "sendSetUpEventResponse: event : " + event + "slotId = " + slotId);
Preeti Ahuja95919342013-10-01 18:18:55 -07001434
1435 if (mStkContext[slotId].mCurrentSetupEventCmd == null){
1436 CatLog.e(this, "mCurrentSetupEventCmd is null");
1437 return;
1438 }
1439
1440 CatResponseMessage resMsg = new CatResponseMessage(mStkContext[slotId].mCurrentSetupEventCmd);
1441
1442 resMsg.setResultCode(ResultCode.OK);
1443 resMsg.setEventDownload(event, addedInfo);
1444
1445 mStkService[slotId].onCmdResponse(resMsg);
1446 }
1447
1448 private void checkForSetupEvent(int event, Bundle args, int slotId) {
1449 boolean eventPresent = false;
1450 byte[] addedInfo = null;
1451 CatLog.d(this, "Event :" + event);
1452
1453 if (mStkContext[slotId].mSetupEventListSettings != null) {
1454 /* Checks if the event is present in the EventList updated by last
1455 * SetupEventList Proactive Command */
1456 for (int i : mStkContext[slotId].mSetupEventListSettings.eventList) {
1457 if (event == i) {
1458 eventPresent = true;
1459 break;
1460 }
1461 }
1462
1463 /* If Event is present send the response to ICC */
1464 if (eventPresent == true) {
1465 CatLog.d(this, " Event " + event + "exists in the EventList");
1466
1467 switch (event) {
Preeti Ahuja560be362014-11-25 19:38:24 -08001468 case IDLE_SCREEN_AVAILABLE_EVENT:
1469 sendSetUpEventResponse(event, addedInfo, slotId);
1470 removeSetUpEvent(event, slotId);
1471 break;
Preeti Ahuja95919342013-10-01 18:18:55 -07001472 case LANGUAGE_SELECTION_EVENT:
1473 String language = mContext
1474 .getResources().getConfiguration().locale.getLanguage();
1475 CatLog.d(this, "language: " + language);
1476 // Each language code is a pair of alpha-numeric characters.
1477 // Each alpha-numeric character shall be coded on one byte
1478 // using the SMS default 7-bit coded alphabet
1479 addedInfo = GsmAlphabet.stringToGsm8BitPacked(language);
1480 sendSetUpEventResponse(event, addedInfo, slotId);
1481 break;
1482 default:
1483 break;
1484 }
1485 } else {
1486 CatLog.e(this, " Event does not exist in the EventList");
1487 }
1488 } else {
1489 CatLog.e(this, "SetupEventList is not received. Ignoring the event: " + event);
1490 }
1491 }
1492
1493 private void removeSetUpEvent(int event, int slotId) {
1494 CatLog.d(this, "Remove Event :" + event);
1495
1496 if (mStkContext[slotId].mSetupEventListSettings != null) {
1497 /*
1498 * Make new Eventlist without the event
1499 */
1500 for (int i = 0; i < mStkContext[slotId].mSetupEventListSettings.eventList.length; i++) {
1501 if (event == mStkContext[slotId].mSetupEventListSettings.eventList[i]) {
1502 mStkContext[slotId].mSetupEventListSettings.eventList[i] = INVALID_SETUP_EVENT;
1503 break;
1504 }
1505 }
1506 }
1507 }
1508
1509 private void launchEventMessage(int slotId) {
1510 launchEventMessage(slotId, mStkContext[slotId].mCurrentCmd.geTextMessage());
1511 }
1512
Wink Savillee68857d2014-10-17 15:23:05 -07001513 private void launchEventMessage(int slotId, TextMessage msg) {
1514 if (msg == null || (msg.text != null && msg.text.length() == 0)) {
1515 CatLog.d(LOG_TAG, "launchEventMessage return");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001516 return;
1517 }
Wink Savillee68857d2014-10-17 15:23:05 -07001518
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001519 Toast toast = new Toast(mContext.getApplicationContext());
1520 LayoutInflater inflate = (LayoutInflater) mContext
1521 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1522 View v = inflate.inflate(R.layout.stk_event_msg, null);
1523 TextView tv = (TextView) v
1524 .findViewById(com.android.internal.R.id.message);
1525 ImageView iv = (ImageView) v
1526 .findViewById(com.android.internal.R.id.icon);
1527 if (msg.icon != null) {
1528 iv.setImageBitmap(msg.icon);
1529 } else {
1530 iv.setVisibility(View.GONE);
1531 }
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301532 /* In case of 'self explanatory' stkapp should display the specified
1533 * icon in proactive command (but not the alpha string).
1534 * If icon is non-self explanatory and if the icon could not be displayed
1535 * then alpha string or text data should be displayed
1536 * Ref: ETSI 102.223,section 6.5.4
1537 */
1538 if (mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ||
1539 msg.icon == null || !msg.iconSelfExplanatory) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001540 tv.setText(msg.text);
1541 }
1542
1543 toast.setView(v);
1544 toast.setDuration(Toast.LENGTH_LONG);
1545 toast.setGravity(Gravity.BOTTOM, 0, 0);
1546 toast.show();
1547 }
1548
Wink Savillee68857d2014-10-17 15:23:05 -07001549 private void launchConfirmationDialog(TextMessage msg, int slotId) {
1550 msg.title = mStkContext[slotId].lastSelectedItem;
1551 Intent newIntent = new Intent();
1552 String targetActivity = STK_DIALOG_ACTIVITY_NAME;
1553 String uriString = STK_DIALOG_URI + System.currentTimeMillis();
1554 //Set unique URI to create a new instance of activity for different slotId.
1555 Uri uriData = Uri.parse(uriString);
1556
1557 if (newIntent != null) {
1558 newIntent.setClassName(this, targetActivity);
1559 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1560 | Intent.FLAG_ACTIVITY_NO_HISTORY
1561 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
1562 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1563 newIntent.putExtra("TEXT", msg);
1564 newIntent.putExtra(SLOT_ID, slotId);
1565 newIntent.setData(uriData);
1566 startActivity(newIntent);
1567 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001568 }
1569
1570 private void launchBrowser(BrowserSettings settings) {
1571 if (settings == null) {
1572 return;
1573 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001574
Abhishek Adappa840c82f2013-02-26 10:19:49 -08001575 Uri data = null;
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001576 String url;
1577 if (settings.url == null) {
Abhishek Adappa840c82f2013-02-26 10:19:49 -08001578 // if the command did not contain a URL,
1579 // launch the browser to the default homepage.
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001580 CatLog.d(this, "no url data provided by proactive command." +
1581 " launching browser with stk default URL ... ");
1582 url = SystemProperties.get(STK_BROWSER_DEFAULT_URL_SYSPROP,
1583 "http://www.google.com");
1584 } else {
1585 CatLog.d(this, "launch browser command has attached url = " + settings.url);
1586 url = settings.url;
Abhishek Adappa840c82f2013-02-26 10:19:49 -08001587 }
David Brown7c03cfe2011-10-20 15:36:12 -07001588
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001589 if (url.startsWith("http://") || url.startsWith("https://")) {
1590 data = Uri.parse(url);
1591 CatLog.d(this, "launching browser with url = " + url);
1592 } else {
1593 String modifiedUrl = "http://" + url;
1594 data = Uri.parse(modifiedUrl);
1595 CatLog.d(this, "launching browser with modified url = " + modifiedUrl);
1596 }
1597
1598 Intent intent = new Intent(Intent.ACTION_VIEW);
1599 intent.setData(data);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001600 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1601 switch (settings.mode) {
1602 case USE_EXISTING_BROWSER:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001603 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1604 break;
1605 case LAUNCH_NEW_BROWSER:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001606 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1607 break;
1608 case LAUNCH_IF_NOT_ALREADY_LAUNCHED:
1609 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1610 break;
1611 }
1612 // start browser activity
1613 startActivity(intent);
1614 // a small delay, let the browser start, before processing the next command.
Wink Saville79085fc2009-06-09 10:27:23 -07001615 // this is good for scenarios where a related DISPLAY TEXT command is
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001616 // followed immediately.
1617 try {
Ryuto Sawada350aaa62016-06-13 14:47:22 +09001618 Thread.sleep(3000);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001619 } catch (InterruptedException e) {}
1620 }
1621
Wink Savillee68857d2014-10-17 15:23:05 -07001622 private void launchIdleText(int slotId) {
Preeti Ahuja560be362014-11-25 19:38:24 -08001623 TextMessage msg = mStkContext[slotId].mIdleModeTextCmd.geTextMessage();
dujin.cha2a0eb2a2011-11-11 15:03:57 +09001624
Preeti Ahuja95919342013-10-01 18:18:55 -07001625 if (msg == null || msg.text ==null) {
1626 CatLog.d(LOG_TAG, msg == null ? "mCurrent.getTextMessage is NULL"
1627 : "mCurrent.getTextMessage.text is NULL");
Wink Savillee68857d2014-10-17 15:23:05 -07001628 mNotificationManager.cancel(getNotificationId(slotId));
Wink Saville046db4b2011-11-01 20:42:54 -07001629 return;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001630 } else {
Preeti Ahuja95919342013-10-01 18:18:55 -07001631 CatLog.d(LOG_TAG, "launchIdleText - text[" + msg.text
1632 + "] iconSelfExplanatory[" + msg.iconSelfExplanatory
1633 + "] icon[" + msg.icon + "], sim id: " + slotId);
Wink Savillee68857d2014-10-17 15:23:05 -07001634 CatLog.d(LOG_TAG, "Add IdleMode text");
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001635 PendingIntent pendingIntent = PendingIntent.getService(mContext, 0,
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001636 new Intent(mContext, StkAppService.class), 0);
fionaxu805eb572017-05-02 10:57:30 -07001637 createAllChannels();
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001638 final Notification.Builder notificationBuilder = new Notification.Builder(
fionaxu2c91c752017-04-21 18:11:57 -07001639 StkAppService.this, STK_NOTIFICATION_CHANNEL_ID);
Wink Savillee68857d2014-10-17 15:23:05 -07001640 if (mStkContext[slotId].mMainCmd != null &&
1641 mStkContext[slotId].mMainCmd.getMenu() != null) {
1642 notificationBuilder.setContentTitle(mStkContext[slotId].mMainCmd.getMenu().title);
Christopher Posselwhite2ad2ab72013-05-27 07:51:06 +02001643 } else {
1644 notificationBuilder.setContentTitle("");
1645 }
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001646 notificationBuilder
1647 .setSmallIcon(com.android.internal.R.drawable.stat_notify_sim_toolkit);
1648 notificationBuilder.setContentIntent(pendingIntent);
1649 notificationBuilder.setOngoing(true);
1650 // Set text and icon for the status bar and notification body.
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301651 if (mStkContext[slotId].mIdleModeTextCmd.hasIconLoadFailed() ||
1652 !msg.iconSelfExplanatory) {
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001653 notificationBuilder.setContentText(msg.text);
Christopher Posselwhite2ad2ab72013-05-27 07:51:06 +02001654 notificationBuilder.setTicker(msg.text);
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001655 }
1656 if (msg.icon != null) {
1657 notificationBuilder.setLargeIcon(msg.icon);
1658 } else {
1659 Bitmap bitmapIcon = BitmapFactory.decodeResource(StkAppService.this
1660 .getResources().getSystem(),
1661 com.android.internal.R.drawable.stat_notify_sim_toolkit);
1662 notificationBuilder.setLargeIcon(bitmapIcon);
1663 }
Selim Cinek62eb3fe2014-08-27 17:52:23 +02001664 notificationBuilder.setColor(mContext.getResources().getColor(
1665 com.android.internal.R.color.system_notification_accent_color));
Wink Savillee68857d2014-10-17 15:23:05 -07001666 mNotificationManager.notify(getNotificationId(slotId), notificationBuilder.build());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001667 }
1668 }
1669
fionaxu805eb572017-05-02 10:57:30 -07001670 /** Creates the notification channel and registers it with NotificationManager.
1671 * If a channel with the same ID is already registered, NotificationManager will
1672 * ignore this call.
1673 */
1674 private void createAllChannels() {
1675 mNotificationManager.createNotificationChannel(new NotificationChannel(
1676 STK_NOTIFICATION_CHANNEL_ID,
1677 getResources().getString(R.string.stk_channel_name),
1678 NotificationManager.IMPORTANCE_MIN));
1679 }
1680
Wink Savillee68857d2014-10-17 15:23:05 -07001681 private void launchToneDialog(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001682 Intent newIntent = new Intent(this, ToneDialog.class);
Wink Savillee68857d2014-10-17 15:23:05 -07001683 String uriString = STK_TONE_URI + slotId;
1684 Uri uriData = Uri.parse(uriString);
1685 //Set unique URI to create a new instance of activity for different slotId.
1686 CatLog.d(LOG_TAG, "launchToneDialog, slotId: " + slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001687 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1688 | Intent.FLAG_ACTIVITY_NO_HISTORY
1689 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
Wink Savillee68857d2014-10-17 15:23:05 -07001690 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1691 newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
1692 newIntent.putExtra("TONE", mStkContext[slotId].mCurrentCmd.getToneSettings());
1693 newIntent.putExtra(SLOT_ID, slotId);
1694 newIntent.setData(uriData);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001695 startActivity(newIntent);
1696 }
1697
Srikanth Chintala89aa6602014-03-14 16:26:57 +05301698 private void handlePlayTone(int slotId) {
1699 TextMessage toneMsg = mStkContext[slotId].mCurrentCmd.geTextMessage();
1700
1701 boolean showUser = true;
1702 boolean displayDialog = true;
1703 Resources resource = Resources.getSystem();
1704 try {
1705 displayDialog = !resource.getBoolean(
1706 com.android.internal.R.bool.config_stkNoAlphaUsrCnf);
1707 } catch (NotFoundException e) {
1708 displayDialog = true;
1709 }
1710
1711 // As per the spec 3GPP TS 11.14, 6.4.5. Play Tone.
1712 // If there is no alpha identifier tlv present, UE may show the
1713 // user information. 'config_stkNoAlphaUsrCnf' value will decide
1714 // whether to show it or not.
1715 // If alpha identifier tlv is present and its data is null, play only tone
1716 // without showing user any information.
1717 // Alpha Id is Present, but the text data is null.
1718 if ((toneMsg.text != null ) && (toneMsg.text.equals(""))) {
1719 CatLog.d(this, "Alpha identifier data is null, play only tone");
1720 showUser = false;
1721 }
1722 // Alpha Id is not present AND we need to show info to the user.
1723 if (toneMsg.text == null && displayDialog) {
1724 CatLog.d(this, "toneMsg.text " + toneMsg.text
1725 + " Starting ToneDialog activity with default message.");
1726 toneMsg.text = getResources().getString(R.string.default_tone_dialog_msg);
1727 showUser = true;
1728 }
1729 // Dont show user info, if config setting is true.
1730 if (toneMsg.text == null && !displayDialog) {
1731 CatLog.d(this, "config value stkNoAlphaUsrCnf is true");
1732 showUser = false;
1733 }
1734
1735 CatLog.d(this, "toneMsg.text: " + toneMsg.text + "showUser: " +showUser +
1736 "displayDialog: " +displayDialog);
1737 playTone(showUser, slotId);
1738 }
1739
1740 private void playTone(boolean showUserInfo, int slotId) {
1741 // Start playing tone and vibration
1742 ToneSettings settings = mStkContext[slotId].mCurrentCmd.getToneSettings();
1743 if (null == settings) {
1744 CatLog.d(this, "null settings, not playing tone.");
1745 return;
1746 }
1747
1748 mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
1749 mTonePlayer = new TonePlayer();
1750 mTonePlayer.play(settings.tone);
1751 int timeout = StkApp.calculateDurationInMilis(settings.duration);
1752 if (timeout == 0) {
1753 timeout = StkApp.TONE_DEFAULT_TIMEOUT;
1754 }
1755
1756 Message msg = mServiceHandler.obtainMessage();
1757 msg.arg1 = OP_STOP_TONE;
1758 msg.arg2 = slotId;
1759 msg.obj = (Integer)(showUserInfo ? 1 : 0);
1760 msg.what = STOP_TONE_WHAT;
1761 mServiceHandler.sendMessageDelayed(msg, timeout);
1762 if (settings.vibrate) {
1763 mVibrator.vibrate(timeout);
1764 }
1765
1766 // Start Tone dialog Activity to show user the information.
1767 if (showUserInfo) {
1768 Intent newIntent = new Intent(sInstance, ToneDialog.class);
1769 String uriString = STK_TONE_URI + slotId;
1770 Uri uriData = Uri.parse(uriString);
1771 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1772 | Intent.FLAG_ACTIVITY_NO_HISTORY
1773 | Intent.FLAG_ACTIVITY_SINGLE_TOP
1774 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
1775 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1776 newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
1777 newIntent.putExtra(SLOT_ID, slotId);
1778 newIntent.setData(uriData);
1779 startActivity(newIntent);
1780 }
1781 }
1782
1783 private void finishToneDialogActivity() {
1784 Intent finishIntent = new Intent(FINISH_TONE_ACTIVITY_ACTION);
1785 sendBroadcast(finishIntent);
1786 }
1787
1788 private void handleStopTone(Message msg, int slotId) {
1789 int resId = 0;
1790
1791 // Stop the play tone in following cases:
1792 // 1.OP_STOP_TONE: play tone timer expires.
1793 // 2.STOP_TONE_USER: user pressed the back key.
1794 if (msg.arg1 == OP_STOP_TONE) {
1795 resId = RES_ID_DONE;
1796 // Dismiss Tone dialog, after finishing off playing the tone.
1797 int finishActivity = (Integer) msg.obj;
1798 if (finishActivity == 1) finishToneDialogActivity();
1799 } else if (msg.arg1 == OP_STOP_TONE_USER) {
1800 resId = RES_ID_END_SESSION;
1801 }
1802
1803 sendResponse(resId, slotId, true);
1804 mServiceHandler.removeMessages(STOP_TONE_WHAT);
1805 if (mTonePlayer != null) {
1806 mTonePlayer.stop();
1807 mTonePlayer.release();
1808 mTonePlayer = null;
1809 }
1810 if (mVibrator != null) {
1811 mVibrator.cancel();
1812 mVibrator = null;
1813 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001814 }
1815
Takanori Nakano49b12722016-02-16 14:34:14 +09001816 private void launchOpenChannelDialog(final int slotId) {
Wink Savillee68857d2014-10-17 15:23:05 -07001817 TextMessage msg = mStkContext[slotId].mCurrentCmd.geTextMessage();
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001818 if (msg == null) {
Wink Savillee68857d2014-10-17 15:23:05 -07001819 CatLog.d(LOG_TAG, "msg is null, return here");
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001820 return;
1821 }
1822
1823 msg.title = getResources().getString(R.string.stk_dialog_title);
1824 if (msg.text == null) {
1825 msg.text = getResources().getString(R.string.default_open_channel_msg);
1826 }
1827
1828 final AlertDialog dialog = new AlertDialog.Builder(mContext)
1829 .setIconAttribute(android.R.attr.alertDialogIcon)
1830 .setTitle(msg.title)
1831 .setMessage(msg.text)
1832 .setCancelable(false)
1833 .setPositiveButton(getResources().getString(R.string.stk_dialog_accept),
1834 new DialogInterface.OnClickListener() {
1835 public void onClick(DialogInterface dialog, int which) {
1836 Bundle args = new Bundle();
1837 args.putInt(RES_ID, RES_ID_CHOICE);
1838 args.putInt(CHOICE, YES);
1839 Message message = mServiceHandler.obtainMessage();
1840 message.arg1 = OP_RESPONSE;
Takanori Nakano49b12722016-02-16 14:34:14 +09001841 message.arg2 = slotId;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001842 message.obj = args;
1843 mServiceHandler.sendMessage(message);
1844 }
1845 })
1846 .setNegativeButton(getResources().getString(R.string.stk_dialog_reject),
1847 new DialogInterface.OnClickListener() {
1848 public void onClick(DialogInterface dialog, int which) {
1849 Bundle args = new Bundle();
1850 args.putInt(RES_ID, RES_ID_CHOICE);
1851 args.putInt(CHOICE, NO);
1852 Message message = mServiceHandler.obtainMessage();
1853 message.arg1 = OP_RESPONSE;
Takanori Nakano49b12722016-02-16 14:34:14 +09001854 message.arg2 = slotId;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001855 message.obj = args;
1856 mServiceHandler.sendMessage(message);
1857 }
1858 })
1859 .create();
1860
1861 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
1862 if (!mContext.getResources().getBoolean(
1863 com.android.internal.R.bool.config_sf_slowBlur)) {
1864 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1865 }
1866
1867 dialog.show();
1868 }
1869
Wink Savillee68857d2014-10-17 15:23:05 -07001870 private void launchTransientEventMessage(int slotId) {
1871 TextMessage msg = mStkContext[slotId].mCurrentCmd.geTextMessage();
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001872 if (msg == null) {
Wink Savillee68857d2014-10-17 15:23:05 -07001873 CatLog.d(LOG_TAG, "msg is null, return here");
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001874 return;
1875 }
1876
1877 msg.title = getResources().getString(R.string.stk_dialog_title);
1878
1879 final AlertDialog dialog = new AlertDialog.Builder(mContext)
1880 .setIconAttribute(android.R.attr.alertDialogIcon)
1881 .setTitle(msg.title)
1882 .setMessage(msg.text)
1883 .setCancelable(false)
1884 .setPositiveButton(getResources().getString(android.R.string.ok),
1885 new DialogInterface.OnClickListener() {
1886 public void onClick(DialogInterface dialog, int which) {
1887 }
1888 })
1889 .create();
1890
1891 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
1892 if (!mContext.getResources().getBoolean(
1893 com.android.internal.R.bool.config_sf_slowBlur)) {
1894 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1895 }
1896
1897 dialog.show();
1898 }
1899
Wink Savillee68857d2014-10-17 15:23:05 -07001900 private int getNotificationId(int slotId) {
1901 int notifyId = STK_NOTIFICATION_ID;
1902 if (slotId >= 0 && slotId < mSimCount) {
1903 notifyId += slotId;
1904 } else {
1905 CatLog.d(LOG_TAG, "invalid slotId: " + slotId);
1906 }
1907 CatLog.d(LOG_TAG, "getNotificationId, slotId: " + slotId + ", notifyId: " + notifyId);
1908 return notifyId;
1909 }
1910
1911 private String getItemName(int itemId, int slotId) {
1912 Menu menu = mStkContext[slotId].mCurrentCmd.getMenu();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001913 if (menu == null) {
1914 return null;
1915 }
1916 for (Item item : menu.items) {
1917 if (item.id == itemId) {
1918 return item.text;
1919 }
1920 }
1921 return null;
1922 }
1923
Wink Savillee68857d2014-10-17 15:23:05 -07001924 private boolean removeMenu(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001925 try {
Wink Savillee68857d2014-10-17 15:23:05 -07001926 if (mStkContext[slotId].mCurrentMenu.items.size() == 1 &&
1927 mStkContext[slotId].mCurrentMenu.items.get(0) == null) {
1928 mStkContext[slotId].mSetupMenuState = STATE_NOT_EXIST;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001929 return true;
1930 }
1931 } catch (NullPointerException e) {
Wink Savillee68857d2014-10-17 15:23:05 -07001932 CatLog.d(LOG_TAG, "Unable to get Menu's items size");
1933 mStkContext[slotId].mSetupMenuState = STATE_NOT_EXIST;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001934 return true;
1935 }
Wink Savillee68857d2014-10-17 15:23:05 -07001936 mStkContext[slotId].mSetupMenuState = STATE_EXIST;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001937 return false;
1938 }
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +05301939
Wink Savillee68857d2014-10-17 15:23:05 -07001940 StkContext getStkContext(int slotId) {
1941 if (slotId >= 0 && slotId < mSimCount) {
1942 return mStkContext[slotId];
1943 } else {
1944 CatLog.d(LOG_TAG, "invalid slotId: " + slotId);
1945 return null;
1946 }
1947 }
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +05301948
1949 private void handleAlphaNotify(Bundle args) {
1950 String alphaString = args.getString(AppInterface.ALPHA_STRING);
1951
1952 CatLog.d(this, "Alpha string received from card: " + alphaString);
1953 Toast toast = Toast.makeText(sInstance, alphaString, Toast.LENGTH_LONG);
1954 toast.setGravity(Gravity.TOP, 0, 0);
1955 toast.show();
1956 }
Srikanth Chintalaba103002015-11-30 10:49:52 -08001957
1958 private boolean isUrlAvailableToLaunchBrowser(BrowserSettings settings) {
1959 String url = SystemProperties.get(STK_BROWSER_DEFAULT_URL_SYSPROP, "");
1960 if (url == "" && settings.url == null) {
1961 return false;
1962 }
1963 return true;
1964 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001965}