blob: 96d596e781061595688d8adee7c50d9d852ba967 [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;
Takanori Nakano3776e2c2016-10-14 16:54:28 +090048import android.support.v4.content.LocalBroadcastManager;
Sooraj Sasindrana4160472016-10-12 16:28:25 -070049import android.telephony.CarrierConfigManager;
Wink Saville56469d52009-04-02 01:37:03 -070050import android.telephony.TelephonyManager;
Preeti Ahujaa7cdca22013-10-01 18:20:56 -070051import android.text.TextUtils;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080052import android.view.Gravity;
53import android.view.LayoutInflater;
54import android.view.View;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -050055import android.view.WindowManager;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080056import android.widget.ImageView;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080057import android.widget.TextView;
58import android.widget.Toast;
Wink Savillee68857d2014-10-17 15:23:05 -070059import android.content.IntentFilter;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080060
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070061import com.android.internal.telephony.cat.AppInterface;
Preeti Ahuja95919342013-10-01 18:18:55 -070062import com.android.internal.telephony.cat.LaunchBrowserMode;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070063import com.android.internal.telephony.cat.Menu;
64import com.android.internal.telephony.cat.Item;
65import com.android.internal.telephony.cat.ResultCode;
66import com.android.internal.telephony.cat.CatCmdMessage;
67import com.android.internal.telephony.cat.CatCmdMessage.BrowserSettings;
Preeti Ahuja95919342013-10-01 18:18:55 -070068import com.android.internal.telephony.cat.CatCmdMessage.SetupEventListSettings;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070069import com.android.internal.telephony.cat.CatLog;
70import com.android.internal.telephony.cat.CatResponseMessage;
71import com.android.internal.telephony.cat.TextMessage;
Srikanth Chintala89aa6602014-03-14 16:26:57 +053072import com.android.internal.telephony.cat.ToneSettings;
Wink Saville94e982b2014-07-11 07:38:14 -070073import com.android.internal.telephony.uicc.IccRefreshResponse;
Wink Savillee68857d2014-10-17 15:23:05 -070074import com.android.internal.telephony.PhoneConstants;
Preeti Ahuja95919342013-10-01 18:18:55 -070075import com.android.internal.telephony.GsmAlphabet;
Legler Wuaeefef52014-10-27 00:57:18 +080076import com.android.internal.telephony.cat.CatService;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080077
Preeti Ahujaa7cdca22013-10-01 18:20:56 -070078import java.util.Iterator;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080079import java.util.LinkedList;
Wink Savillee68857d2014-10-17 15:23:05 -070080import java.lang.System;
81import java.util.List;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080082
Preeti Ahuja95919342013-10-01 18:18:55 -070083import static com.android.internal.telephony.cat.CatCmdMessage.
Preeti Ahuja560be362014-11-25 19:38:24 -080084 SetupEventListConstants.IDLE_SCREEN_AVAILABLE_EVENT;
85import static com.android.internal.telephony.cat.CatCmdMessage.
Preeti Ahuja95919342013-10-01 18:18:55 -070086 SetupEventListConstants.LANGUAGE_SELECTION_EVENT;
87
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080088/**
89 * SIM toolkit application level service. Interacts with Telephopny messages,
90 * application's launch and user input from STK UI elements.
Wink Saville79085fc2009-06-09 10:27:23 -070091 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080092 */
93public class StkAppService extends Service implements Runnable {
94
95 // members
Wink Savillee68857d2014-10-17 15:23:05 -070096 protected class StkContext {
97 protected CatCmdMessage mMainCmd = null;
98 protected CatCmdMessage mCurrentCmd = null;
99 protected CatCmdMessage mCurrentMenuCmd = null;
100 protected Menu mCurrentMenu = null;
101 protected String lastSelectedItem = null;
102 protected boolean mMenuIsVisible = false;
103 protected boolean mIsInputPending = false;
104 protected boolean mIsMenuPending = false;
105 protected boolean mIsDialogPending = false;
106 protected boolean responseNeeded = true;
107 protected boolean launchBrowser = false;
108 protected BrowserSettings mBrowserSettings = null;
109 protected LinkedList<DelayedCmd> mCmdsQ = null;
110 protected boolean mCmdInProgress = false;
111 protected int mStkServiceState = STATE_UNKNOWN;
112 protected int mSetupMenuState = STATE_UNKNOWN;
113 protected int mMenuState = StkMenuActivity.STATE_INIT;
114 protected int mOpCode = -1;
115 private Activity mActivityInstance = null;
116 private Activity mDialogInstance = null;
117 private Activity mMainActivityInstance = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700118 private int mSlotId = 0;
Preeti Ahuja95919342013-10-01 18:18:55 -0700119 private SetupEventListSettings mSetupEventListSettings = null;
120 private boolean mClearSelectItem = false;
121 private boolean mDisplayTextDlgIsVisibile = false;
122 private CatCmdMessage mCurrentSetupEventCmd = null;
Preeti Ahuja560be362014-11-25 19:38:24 -0800123 private CatCmdMessage mIdleModeTextCmd = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700124 final synchronized void setPendingActivityInstance(Activity act) {
125 CatLog.d(this, "setPendingActivityInstance act : " + mSlotId + ", " + act);
126 callSetActivityInstMsg(OP_SET_ACT_INST, mSlotId, act);
127 }
128 final synchronized Activity getPendingActivityInstance() {
129 CatLog.d(this, "getPendingActivityInstance act : " + mSlotId + ", " +
130 mActivityInstance);
131 return mActivityInstance;
132 }
133 final synchronized void setPendingDialogInstance(Activity act) {
134 CatLog.d(this, "setPendingDialogInstance act : " + mSlotId + ", " + act);
135 callSetActivityInstMsg(OP_SET_DAL_INST, mSlotId, act);
136 }
137 final synchronized Activity getPendingDialogInstance() {
138 CatLog.d(this, "getPendingDialogInstance act : " + mSlotId + ", " +
139 mDialogInstance);
140 return mDialogInstance;
141 }
142 final synchronized void setMainActivityInstance(Activity act) {
143 CatLog.d(this, "setMainActivityInstance act : " + mSlotId + ", " + act);
144 callSetActivityInstMsg(OP_SET_MAINACT_INST, mSlotId, act);
145 }
146 final synchronized Activity getMainActivityInstance() {
147 CatLog.d(this, "getMainActivityInstance act : " + mSlotId + ", " +
148 mMainActivityInstance);
149 return mMainActivityInstance;
150 }
151 }
152
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800153 private volatile Looper mServiceLooper;
154 private volatile ServiceHandler mServiceHandler;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800155 private Context mContext = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800156 private NotificationManager mNotificationManager = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800157 static StkAppService sInstance = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700158 private AppInterface[] mStkService = null;
159 private StkContext[] mStkContext = null;
160 private int mSimCount = 0;
Preeti Ahuja560be362014-11-25 19:38:24 -0800161 private PowerManager mPowerManager = null;
162 private StkCmdReceiver mStkCmdReceiver = null;
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530163 private TonePlayer mTonePlayer = null;
164 private Vibrator mVibrator = null;
Preeti Ahuja95919342013-10-01 18:18:55 -0700165
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800166 // Used for setting FLAG_ACTIVITY_NO_USER_ACTION when
167 // creating an intent.
168 private enum InitiatedByUserAction {
169 yes, // The action was started via a user initiated action
170 unknown, // Not known for sure if user initated the action
171 }
172
173 // constants
174 static final String OPCODE = "op";
175 static final String CMD_MSG = "cmd message";
176 static final String RES_ID = "response id";
177 static final String MENU_SELECTION = "menu selection";
178 static final String INPUT = "input";
179 static final String HELP = "help";
180 static final String CONFIRMATION = "confirm";
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500181 static final String CHOICE = "choice";
Wink Savillee68857d2014-10-17 15:23:05 -0700182 static final String SLOT_ID = "SLOT_ID";
183 static final String STK_CMD = "STK CMD";
184 static final String STK_DIALOG_URI = "stk://com.android.stk/dialog/";
185 static final String STK_MENU_URI = "stk://com.android.stk/menu/";
186 static final String STK_INPUT_URI = "stk://com.android.stk/input/";
187 static final String STK_TONE_URI = "stk://com.android.stk/tone/";
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530188 static final String FINISH_TONE_ACTIVITY_ACTION =
189 "android.intent.action.stk.finish_activity";
Preeti Ahuja95919342013-10-01 18:18:55 -0700190
191 // These below constants are used for SETUP_EVENT_LIST
192 static final String SETUP_EVENT_TYPE = "event";
193 static final String SETUP_EVENT_CAUSE = "cause";
194
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800195 // operations ids for different service functionality.
196 static final int OP_CMD = 1;
197 static final int OP_RESPONSE = 2;
198 static final int OP_LAUNCH_APP = 3;
199 static final int OP_END_SESSION = 4;
200 static final int OP_BOOT_COMPLETED = 5;
201 private static final int OP_DELAYED_MSG = 6;
Wink Saville94e982b2014-07-11 07:38:14 -0700202 static final int OP_CARD_STATUS_CHANGED = 7;
Wink Savillee68857d2014-10-17 15:23:05 -0700203 static final int OP_SET_ACT_INST = 8;
204 static final int OP_SET_DAL_INST = 9;
205 static final int OP_SET_MAINACT_INST = 10;
Preeti Ahujab3d0e612014-11-20 13:29:25 -0800206 static final int OP_LOCALE_CHANGED = 11;
207 static final int OP_ALPHA_NOTIFY = 12;
Preeti Ahuja560be362014-11-25 19:38:24 -0800208 static final int OP_IDLE_SCREEN = 13;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800209
Preeti Ahuja95919342013-10-01 18:18:55 -0700210 //Invalid SetupEvent
211 static final int INVALID_SETUP_EVENT = 0xFF;
212
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530213 // Message id to signal stop tone due to play tone timeout.
214 private static final int OP_STOP_TONE = 16;
215
216 // Message id to signal stop tone on user keyback.
217 static final int OP_STOP_TONE_USER = 17;
218
219 // Message id to remove stop tone message from queue.
220 private static final int STOP_TONE_WHAT = 100;
221
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800222 // Response ids
223 static final int RES_ID_MENU_SELECTION = 11;
224 static final int RES_ID_INPUT = 12;
225 static final int RES_ID_CONFIRM = 13;
226 static final int RES_ID_DONE = 14;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500227 static final int RES_ID_CHOICE = 15;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800228
229 static final int RES_ID_TIMEOUT = 20;
230 static final int RES_ID_BACKWARD = 21;
231 static final int RES_ID_END_SESSION = 22;
232 static final int RES_ID_EXIT = 23;
Srikanth Chintalaba103002015-11-30 10:49:52 -0800233 static final int RES_ID_ERROR = 24;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800234
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500235 static final int YES = 1;
236 static final int NO = 0;
237
Wink Savillee68857d2014-10-17 15:23:05 -0700238 static final int STATE_UNKNOWN = -1;
239 static final int STATE_NOT_EXIST = 0;
240 static final int STATE_EXIST = 1;
Wink Saville79085fc2009-06-09 10:27:23 -0700241
Wink Savillee68857d2014-10-17 15:23:05 -0700242 private static final String PACKAGE_NAME = "com.android.stk";
243 private static final String STK_MENU_ACTIVITY_NAME = PACKAGE_NAME + ".StkMenuActivity";
244 private static final String STK_INPUT_ACTIVITY_NAME = PACKAGE_NAME + ".StkInputActivity";
245 private static final String STK_DIALOG_ACTIVITY_NAME = PACKAGE_NAME + ".StkDialogActivity";
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800246 // Notification id used to display Idle Mode text in NotificationManager.
247 private static final int STK_NOTIFICATION_ID = 333;
fionaxu2c91c752017-04-21 18:11:57 -0700248 // Notification channel containing all mobile service messages notifications.
249 private static final String STK_NOTIFICATION_CHANNEL_ID = "mobileServiceMessages";
250
Wink Savillee68857d2014-10-17 15:23:05 -0700251 private static final String LOG_TAG = new Object(){}.getClass().getEnclosingClass().getName();
Wink Saville79085fc2009-06-09 10:27:23 -0700252
Takanori Nakano3776e2c2016-10-14 16:54:28 +0900253 static final String SESSION_ENDED = "session_ended";
254
Wink Saville79085fc2009-06-09 10:27:23 -0700255 // Inner class used for queuing telephony messages (proactive commands,
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800256 // session end) while the service is busy processing a previous message.
257 private class DelayedCmd {
258 // members
259 int id;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700260 CatCmdMessage msg;
Wink Savillee68857d2014-10-17 15:23:05 -0700261 int slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800262
Wink Savillee68857d2014-10-17 15:23:05 -0700263 DelayedCmd(int id, CatCmdMessage msg, int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800264 this.id = id;
265 this.msg = msg;
Wink Savillee68857d2014-10-17 15:23:05 -0700266 this.slotId = slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800267 }
268 }
269
Preeti Ahujaa7cdca22013-10-01 18:20:56 -0700270 // system property to set the STK specific default url for launch browser proactive cmds
271 private static final String STK_BROWSER_DEFAULT_URL_SYSPROP = "persist.radio.stk.default_url";
272
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800273 @Override
274 public void onCreate() {
Wink Savillee68857d2014-10-17 15:23:05 -0700275 CatLog.d(LOG_TAG, "onCreate()+");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800276 // Initialize members
Wink Savillee68857d2014-10-17 15:23:05 -0700277 int i = 0;
278 mContext = getBaseContext();
279 mSimCount = TelephonyManager.from(mContext).getSimCount();
280 CatLog.d(LOG_TAG, "simCount: " + mSimCount);
281 mStkService = new AppInterface[mSimCount];
282 mStkContext = new StkContext[mSimCount];
Preeti Ahuja560be362014-11-25 19:38:24 -0800283 mPowerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
284 mStkCmdReceiver = new StkCmdReceiver();
285 registerReceiver(mStkCmdReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Wink Savillee68857d2014-10-17 15:23:05 -0700286 for (i = 0; i < mSimCount; i++) {
287 CatLog.d(LOG_TAG, "slotId: " + i);
Legler Wuaeefef52014-10-27 00:57:18 +0800288 mStkService[i] = CatService.getInstance(i);
Wink Savillee68857d2014-10-17 15:23:05 -0700289 mStkContext[i] = new StkContext();
290 mStkContext[i].mSlotId = i;
291 mStkContext[i].mCmdsQ = new LinkedList<DelayedCmd>();
292 }
293
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800294 Thread serviceThread = new Thread(null, this, "Stk App Service");
295 serviceThread.start();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800296 mNotificationManager = (NotificationManager) mContext
297 .getSystemService(Context.NOTIFICATION_SERVICE);
298 sInstance = this;
299 }
300
301 @Override
302 public void onStart(Intent intent, int startId) {
Wink Savillee68857d2014-10-17 15:23:05 -0700303 if (intent == null) {
304 CatLog.d(LOG_TAG, "StkAppService onStart intent is null so return");
Banavathu, Srinivas Naik87cda962012-07-17 15:32:44 +0530305 return;
306 }
307
Wink Savillee68857d2014-10-17 15:23:05 -0700308 Bundle args = intent.getExtras();
309 if (args == null) {
310 CatLog.d(LOG_TAG, "StkAppService onStart args is null so return");
311 return;
312 }
313
314 int op = args.getInt(OPCODE);
315 int slotId = 0;
316 int i = 0;
317 if (op != OP_BOOT_COMPLETED) {
318 slotId = args.getInt(SLOT_ID);
319 }
Cuihtlauac ALVARADObde7f032015-05-29 16:25:12 +0200320 CatLog.d(LOG_TAG, "onStart sim id: " + slotId + ", op: " + op + ", *****");
Wink Savillee68857d2014-10-17 15:23:05 -0700321 if ((slotId >= 0 && slotId < mSimCount) && mStkService[slotId] == null) {
Legler Wuaeefef52014-10-27 00:57:18 +0800322 mStkService[slotId] = CatService.getInstance(slotId);
Wink Savillee68857d2014-10-17 15:23:05 -0700323 if (mStkService[slotId] == null) {
324 CatLog.d(LOG_TAG, "mStkService is: " + mStkContext[slotId].mStkServiceState);
325 mStkContext[slotId].mStkServiceState = STATE_NOT_EXIST;
326 //Check other StkService state.
327 //If all StkServices are not available, stop itself and uninstall apk.
328 for (i = PhoneConstants.SIM_ID_1; i < mSimCount; i++) {
329 if (i != slotId
Tsukasa Goto029332b2016-10-05 17:12:06 +0900330 && (mStkService[i] != null)
Wink Savillee68857d2014-10-17 15:23:05 -0700331 && (mStkContext[i].mStkServiceState == STATE_UNKNOWN
332 || mStkContext[i].mStkServiceState == STATE_EXIST)) {
333 break;
334 }
335 }
336 } else {
337 mStkContext[slotId].mStkServiceState = STATE_EXIST;
338 }
339 if (i == mSimCount) {
340 stopSelf();
341 StkAppInstaller.unInstall(mContext);
342 return;
343 }
344 }
345
Banavathu, Srinivas Naik87cda962012-07-17 15:32:44 +0530346 waitForLooper();
John Wang62acae42009-10-08 11:20:23 -0700347
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800348 Message msg = mServiceHandler.obtainMessage();
Wink Savillee68857d2014-10-17 15:23:05 -0700349 msg.arg1 = op;
350 msg.arg2 = slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800351 switch(msg.arg1) {
352 case OP_CMD:
353 msg.obj = args.getParcelable(CMD_MSG);
354 break;
355 case OP_RESPONSE:
Wink Saville94e982b2014-07-11 07:38:14 -0700356 case OP_CARD_STATUS_CHANGED:
Preeti Ahuja95919342013-10-01 18:18:55 -0700357 case OP_LOCALE_CHANGED:
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +0530358 case OP_ALPHA_NOTIFY:
Preeti Ahuja560be362014-11-25 19:38:24 -0800359 case OP_IDLE_SCREEN:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800360 msg.obj = args;
361 /* falls through */
362 case OP_LAUNCH_APP:
363 case OP_END_SESSION:
364 case OP_BOOT_COMPLETED:
365 break;
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530366 case OP_STOP_TONE_USER:
367 msg.obj = args;
368 msg.what = STOP_TONE_WHAT;
369 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800370 default:
371 return;
372 }
373 mServiceHandler.sendMessage(msg);
374 }
375
376 @Override
377 public void onDestroy() {
Wink Savillee68857d2014-10-17 15:23:05 -0700378 CatLog.d(LOG_TAG, "onDestroy()");
Preeti Ahuja560be362014-11-25 19:38:24 -0800379 if (mStkCmdReceiver != null) {
380 unregisterReceiver(mStkCmdReceiver);
381 mStkCmdReceiver = null;
382 }
383 mPowerManager = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800384 waitForLooper();
385 mServiceLooper.quit();
386 }
387
388 @Override
389 public IBinder onBind(Intent intent) {
390 return null;
391 }
392
393 public void run() {
394 Looper.prepare();
395
396 mServiceLooper = Looper.myLooper();
397 mServiceHandler = new ServiceHandler();
398
399 Looper.loop();
400 }
401
402 /*
403 * Package api used by StkMenuActivity to indicate if its on the foreground.
404 */
Wink Savillee68857d2014-10-17 15:23:05 -0700405 void indicateMenuVisibility(boolean visibility, int slotId) {
406 if (slotId >= 0 && slotId < mSimCount) {
407 mStkContext[slotId].mMenuIsVisible = visibility;
408 }
409 }
410
Preeti Ahuja95919342013-10-01 18:18:55 -0700411 /*
412 * Package api used by StkDialogActivity to indicate if its on the foreground.
413 */
414 void setDisplayTextDlgVisibility(boolean visibility, int slotId) {
415 if (slotId >= 0 && slotId < mSimCount) {
416 mStkContext[slotId].mDisplayTextDlgIsVisibile = visibility;
417 }
418 }
419
Wink Savillee68857d2014-10-17 15:23:05 -0700420 boolean isInputPending(int slotId) {
421 if (slotId >= 0 && slotId < mSimCount) {
422 CatLog.d(LOG_TAG, "isInputFinishBySrv: " + mStkContext[slotId].mIsInputPending);
423 return mStkContext[slotId].mIsInputPending;
424 }
425 return false;
426 }
427
428 boolean isMenuPending(int slotId) {
429 if (slotId >= 0 && slotId < mSimCount) {
430 CatLog.d(LOG_TAG, "isMenuPending: " + mStkContext[slotId].mIsMenuPending);
431 return mStkContext[slotId].mIsMenuPending;
432 }
433 return false;
434 }
435
436 boolean isDialogPending(int slotId) {
437 if (slotId >= 0 && slotId < mSimCount) {
438 CatLog.d(LOG_TAG, "isDialogPending: " + mStkContext[slotId].mIsDialogPending);
439 return mStkContext[slotId].mIsDialogPending;
440 }
441 return false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800442 }
443
Takanori Nakano3776e2c2016-10-14 16:54:28 +0900444 boolean isMainMenuAvailable(int slotId) {
445 if (slotId >= 0 && slotId < mSimCount) {
446 // The main menu can handle the next user operation if the previous session finished.
447 return (mStkContext[slotId].lastSelectedItem == null) ? true : false;
448 }
449 return false;
450 }
451
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800452 /*
453 * Package api used by StkMenuActivity to get its Menu parameter.
454 */
Wink Savillee68857d2014-10-17 15:23:05 -0700455 Menu getMenu(int slotId) {
456 CatLog.d(LOG_TAG, "StkAppService, getMenu, sim id: " + slotId);
457 if (slotId >=0 && slotId < mSimCount) {
458 return mStkContext[slotId].mCurrentMenu;
459 } else {
460 return null;
461 }
462 }
463
464 /*
465 * Package api used by StkMenuActivity to get its Main Menu parameter.
466 */
467 Menu getMainMenu(int slotId) {
468 CatLog.d(LOG_TAG, "StkAppService, getMainMenu, sim id: " + slotId);
w30234a08cffe2015-02-04 15:13:25 -0800469 if (slotId >=0 && slotId < mSimCount && (mStkContext[slotId].mMainCmd != null)) {
Takanori Nakanoc8054ba2016-08-15 19:18:16 +0900470 Menu menu = mStkContext[slotId].mMainCmd.getMenu();
471 if (menu != null && mSimCount > PhoneConstants.MAX_PHONE_COUNT_SINGLE_SIM) {
472 // If alpha identifier or icon identifier with the self-explanatory qualifier is
473 // specified in SET-UP MENU command, it should be more prioritized than preset ones.
474 if (menu.title == null
475 && (menu.titleIcon == null || !menu.titleIconSelfExplanatory)) {
476 StkMenuConfig config = StkMenuConfig.getInstance(getApplicationContext());
477 String label = config.getLabel(slotId);
478 Bitmap icon = config.getIcon(slotId);
479 if (label != null || icon != null) {
480 Parcel parcel = Parcel.obtain();
481 menu.writeToParcel(parcel, 0);
482 parcel.setDataPosition(0);
483 menu = Menu.CREATOR.createFromParcel(parcel);
484 parcel.recycle();
485 menu.title = label;
486 menu.titleIcon = icon;
487 menu.titleIconSelfExplanatory = false;
488 }
489 }
490 }
491 return menu;
Wink Savillee68857d2014-10-17 15:23:05 -0700492 } else {
493 return null;
494 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800495 }
496
497 /*
498 * Package api used by UI Activities and Dialogs to communicate directly
499 * with the service to deliver state information and parameters.
500 */
501 static StkAppService getInstance() {
502 return sInstance;
503 }
504
505 private void waitForLooper() {
506 while (mServiceHandler == null) {
507 synchronized (this) {
508 try {
509 wait(100);
510 } catch (InterruptedException e) {
511 }
512 }
513 }
514 }
515
516 private final class ServiceHandler extends Handler {
517 @Override
518 public void handleMessage(Message msg) {
Wink Savillee68857d2014-10-17 15:23:05 -0700519 if(null == msg) {
520 CatLog.d(LOG_TAG, "ServiceHandler handleMessage msg is null");
521 return;
522 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800523 int opcode = msg.arg1;
Wink Savillee68857d2014-10-17 15:23:05 -0700524 int slotId = msg.arg2;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800525
Wink Savillee68857d2014-10-17 15:23:05 -0700526 CatLog.d(LOG_TAG, "handleMessage opcode[" + opcode + "], sim id[" + slotId + "]");
527 if (opcode == OP_CMD && msg.obj != null &&
528 ((CatCmdMessage)msg.obj).getCmdType()!= null) {
529 CatLog.d(LOG_TAG, "cmdName[" + ((CatCmdMessage)msg.obj).getCmdType().name() + "]");
530 }
531 mStkContext[slotId].mOpCode = opcode;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800532 switch (opcode) {
533 case OP_LAUNCH_APP:
Wink Savillee68857d2014-10-17 15:23:05 -0700534 if (mStkContext[slotId].mMainCmd == null) {
535 CatLog.d(LOG_TAG, "mMainCmd is null");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800536 // nothing todo when no SET UP MENU command didn't arrive.
537 return;
538 }
Wink Savillee68857d2014-10-17 15:23:05 -0700539 CatLog.d(LOG_TAG, "handleMessage OP_LAUNCH_APP - mCmdInProgress[" +
540 mStkContext[slotId].mCmdInProgress + "]");
541
542 //If there is a pending activity for the slot id,
543 //just finish it and create a new one to handle the pending command.
544 cleanUpInstanceStackBySlot(slotId);
545
Wink Savillee68857d2014-10-17 15:23:05 -0700546 CatLog.d(LOG_TAG, "Current cmd type: " +
547 mStkContext[slotId].mCurrentCmd.getCmdType());
548 //Restore the last command from stack by slot id.
549 restoreInstanceFromStackBySlot(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800550 break;
551 case OP_CMD:
Wink Savillee68857d2014-10-17 15:23:05 -0700552 CatLog.d(LOG_TAG, "[OP_CMD]");
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700553 CatCmdMessage cmdMsg = (CatCmdMessage) msg.obj;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800554 // There are two types of commands:
555 // 1. Interactive - user's response is required.
556 // 2. Informative - display a message, no interaction with the user.
557 //
Wink Saville79085fc2009-06-09 10:27:23 -0700558 // Informative commands can be handled immediately without any delay.
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800559 // Interactive commands can't override each other. So if a command
560 // is already in progress, we need to queue the next command until
561 // the user has responded or a timeout expired.
562 if (!isCmdInteractive(cmdMsg)) {
Wink Savillee68857d2014-10-17 15:23:05 -0700563 handleCmd(cmdMsg, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800564 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700565 if (!mStkContext[slotId].mCmdInProgress) {
566 mStkContext[slotId].mCmdInProgress = true;
567 handleCmd((CatCmdMessage) msg.obj, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800568 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700569 CatLog.d(LOG_TAG, "[Interactive][in progress]");
570 mStkContext[slotId].mCmdsQ.addLast(new DelayedCmd(OP_CMD,
571 (CatCmdMessage) msg.obj, slotId));
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800572 }
573 }
574 break;
575 case OP_RESPONSE:
Preeti Ahuja414bc412013-06-25 19:31:49 -0700576 handleCmdResponse((Bundle) msg.obj, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800577 // call delayed commands if needed.
Wink Savillee68857d2014-10-17 15:23:05 -0700578 if (mStkContext[slotId].mCmdsQ.size() != 0) {
579 callDelayedMsg(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800580 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700581 mStkContext[slotId].mCmdInProgress = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800582 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800583 break;
584 case OP_END_SESSION:
Wink Savillee68857d2014-10-17 15:23:05 -0700585 if (!mStkContext[slotId].mCmdInProgress) {
586 mStkContext[slotId].mCmdInProgress = true;
587 handleSessionEnd(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800588 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700589 mStkContext[slotId].mCmdsQ.addLast(
590 new DelayedCmd(OP_END_SESSION, null, slotId));
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800591 }
592 break;
593 case OP_BOOT_COMPLETED:
Wink Savillee68857d2014-10-17 15:23:05 -0700594 CatLog.d(LOG_TAG, " OP_BOOT_COMPLETED");
595 int i = 0;
596 for (i = PhoneConstants.SIM_ID_1; i < mSimCount; i++) {
597 if (mStkContext[i].mMainCmd != null) {
598 break;
599 }
600 }
601 if (i == mSimCount) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800602 StkAppInstaller.unInstall(mContext);
603 }
604 break;
605 case OP_DELAYED_MSG:
Wink Savillee68857d2014-10-17 15:23:05 -0700606 handleDelayedCmd(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800607 break;
Wink Saville94e982b2014-07-11 07:38:14 -0700608 case OP_CARD_STATUS_CHANGED:
Wink Savillee68857d2014-10-17 15:23:05 -0700609 CatLog.d(LOG_TAG, "Card/Icc Status change received");
610 handleCardStatusChangeAndIccRefresh((Bundle) msg.obj, slotId);
611 break;
612 case OP_SET_ACT_INST:
Yoshiaki Naka28d7a722017-10-24 17:32:16 +0900613 Activity act = (Activity) msg.obj;
614 if (mStkContext[slotId].mActivityInstance != act) {
615 CatLog.d(LOG_TAG, "Set activity instance - " + act);
616 Activity previous = mStkContext[slotId].mActivityInstance;
617 mStkContext[slotId].mActivityInstance = act;
618 // Finish the previous one if it has not been finished yet somehow.
619 if (previous != null && !previous.isDestroyed() && !previous.isFinishing()) {
620 CatLog.d(LOG_TAG, "Finish the previous pending activity - " + previous);
621 previous.finish();
622 }
623 }
Wink Savillee68857d2014-10-17 15:23:05 -0700624 break;
625 case OP_SET_DAL_INST:
626 Activity dal = new Activity();
627 CatLog.d(LOG_TAG, "Set dialog instance. " + dal);
628 dal = (Activity) msg.obj;
629 mStkContext[slotId].mDialogInstance = dal;
630 break;
631 case OP_SET_MAINACT_INST:
632 Activity mainAct = new Activity();
633 mainAct = (Activity) msg.obj;
634 CatLog.d(LOG_TAG, "Set activity instance. " + mainAct);
635 mStkContext[slotId].mMainActivityInstance = mainAct;
Wink Saville94e982b2014-07-11 07:38:14 -0700636 break;
Preeti Ahuja95919342013-10-01 18:18:55 -0700637 case OP_LOCALE_CHANGED:
638 CatLog.d(this, "Locale Changed");
Yuta Ui1481b172016-02-03 15:34:31 +0900639 for (int slot = PhoneConstants.SIM_ID_1; slot < mSimCount; slot++) {
640 checkForSetupEvent(LANGUAGE_SELECTION_EVENT, (Bundle) msg.obj, slot);
641 }
fionaxu805eb572017-05-02 10:57:30 -0700642 // rename all registered notification channels on locale change
643 createAllChannels();
Preeti Ahuja95919342013-10-01 18:18:55 -0700644 break;
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +0530645 case OP_ALPHA_NOTIFY:
646 handleAlphaNotify((Bundle) msg.obj);
647 break;
Preeti Ahuja560be362014-11-25 19:38:24 -0800648 case OP_IDLE_SCREEN:
649 for (int slot = 0; slot < mSimCount; slot++) {
650 if (mStkContext[slot] != null) {
651 handleIdleScreen(slot);
652 }
653 }
654 break;
Srikanth Chintala89aa6602014-03-14 16:26:57 +0530655 case OP_STOP_TONE_USER:
656 case OP_STOP_TONE:
657 CatLog.d(this, "Stop tone");
658 handleStopTone(msg, slotId);
659 break;
Wink Saville94e982b2014-07-11 07:38:14 -0700660 }
661 }
662
Wink Savillee68857d2014-10-17 15:23:05 -0700663 private void handleCardStatusChangeAndIccRefresh(Bundle args, int slotId) {
Wink Saville94e982b2014-07-11 07:38:14 -0700664 boolean cardStatus = args.getBoolean(AppInterface.CARD_STATUS);
665
Wink Savillee68857d2014-10-17 15:23:05 -0700666 CatLog.d(LOG_TAG, "CardStatus: " + cardStatus);
Wink Saville94e982b2014-07-11 07:38:14 -0700667 if (cardStatus == false) {
Wink Savillee68857d2014-10-17 15:23:05 -0700668 CatLog.d(LOG_TAG, "CARD is ABSENT");
Wink Saville94e982b2014-07-11 07:38:14 -0700669 // Uninstall STKAPP, Clear Idle text, Stop StkAppService
Wink Savillee68857d2014-10-17 15:23:05 -0700670 mNotificationManager.cancel(getNotificationId(slotId));
Srikanth Chintala7c5a04c2016-09-22 19:05:01 +0530671 mStkContext[slotId].mCurrentMenu = null;
672 mStkContext[slotId].mMainCmd = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700673 if (isAllOtherCardsAbsent(slotId)) {
674 CatLog.d(LOG_TAG, "All CARDs are ABSENT");
675 StkAppInstaller.unInstall(mContext);
676 stopSelf();
677 }
Wink Saville94e982b2014-07-11 07:38:14 -0700678 } else {
679 IccRefreshResponse state = new IccRefreshResponse();
680 state.refreshResult = args.getInt(AppInterface.REFRESH_RESULT);
681
Wink Savillee68857d2014-10-17 15:23:05 -0700682 CatLog.d(LOG_TAG, "Icc Refresh Result: "+ state.refreshResult);
Wink Saville94e982b2014-07-11 07:38:14 -0700683 if ((state.refreshResult == IccRefreshResponse.REFRESH_RESULT_INIT) ||
684 (state.refreshResult == IccRefreshResponse.REFRESH_RESULT_RESET)) {
685 // Clear Idle Text
Wink Savillee68857d2014-10-17 15:23:05 -0700686 mNotificationManager.cancel(getNotificationId(slotId));
Wink Saville94e982b2014-07-11 07:38:14 -0700687 }
688
689 if (state.refreshResult == IccRefreshResponse.REFRESH_RESULT_RESET) {
690 // Uninstall STkmenu
Wink Savillee68857d2014-10-17 15:23:05 -0700691 if (isAllOtherCardsAbsent(slotId)) {
692 StkAppInstaller.unInstall(mContext);
693 }
694 mStkContext[slotId].mCurrentMenu = null;
695 mStkContext[slotId].mMainCmd = null;
Wink Saville94e982b2014-07-11 07:38:14 -0700696 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800697 }
698 }
699 }
Wink Savillee68857d2014-10-17 15:23:05 -0700700 /*
701 * Check if all SIMs are absent except the id of slot equals "slotId".
702 */
703 private boolean isAllOtherCardsAbsent(int slotId) {
704 TelephonyManager mTm = (TelephonyManager) mContext.getSystemService(
705 Context.TELEPHONY_SERVICE);
706 int i = 0;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800707
Wink Savillee68857d2014-10-17 15:23:05 -0700708 for (i = 0; i < mSimCount; i++) {
709 if (i != slotId && mTm.hasIccCard(i)) {
710 break;
711 }
712 }
713 if (i == mSimCount) {
714 return true;
715 } else {
716 return false;
717 }
718 }
Preeti Ahuja95919342013-10-01 18:18:55 -0700719
Preeti Ahuja560be362014-11-25 19:38:24 -0800720 /*
721 * If the device is not in an interactive state, we can assume
722 * that the screen is idle.
723 */
724 private boolean isScreenIdle() {
725 return (!mPowerManager.isInteractive());
726 }
727
728 private void handleIdleScreen(int slotId) {
729
730 // If the idle screen event is present in the list need to send the
731 // response to SIM.
732 CatLog.d(this, "Need to send IDLE SCREEN Available event to SIM");
733 checkForSetupEvent(IDLE_SCREEN_AVAILABLE_EVENT, null, slotId);
734
735 if (mStkContext[slotId].mIdleModeTextCmd != null) {
736 launchIdleText(slotId);
737 }
738 }
739
740 private void sendScreenBusyResponse(int slotId) {
741 if (mStkContext[slotId].mCurrentCmd == null) {
742 return;
743 }
744 CatResponseMessage resMsg = new CatResponseMessage(mStkContext[slotId].mCurrentCmd);
745 CatLog.d(this, "SCREEN_BUSY");
746 resMsg.setResultCode(ResultCode.TERMINAL_CRNTLY_UNABLE_TO_PROCESS);
747 mStkService[slotId].onCmdResponse(resMsg);
748 if (mStkContext[slotId].mCmdsQ.size() != 0) {
749 callDelayedMsg(slotId);
750 } else {
751 mStkContext[slotId].mCmdInProgress = false;
752 }
753 }
754
Preeti Ahuja95919342013-10-01 18:18:55 -0700755 private void sendResponse(int resId, int slotId, boolean confirm) {
756 Message msg = mServiceHandler.obtainMessage();
757 msg.arg1 = OP_RESPONSE;
Takanori Nakano49b12722016-02-16 14:34:14 +0900758 msg.arg2 = slotId;
Preeti Ahuja95919342013-10-01 18:18:55 -0700759 Bundle args = new Bundle();
760 args.putInt(StkAppService.RES_ID, resId);
Preeti Ahuja95919342013-10-01 18:18:55 -0700761 args.putBoolean(StkAppService.CONFIRMATION, confirm);
762 msg.obj = args;
763 mServiceHandler.sendMessage(msg);
764 }
765
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700766 private boolean isCmdInteractive(CatCmdMessage cmd) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800767 switch (cmd.getCmdType()) {
768 case SEND_DTMF:
769 case SEND_SMS:
770 case SEND_SS:
771 case SEND_USSD:
772 case SET_UP_IDLE_MODE_TEXT:
773 case SET_UP_MENU:
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500774 case CLOSE_CHANNEL:
775 case RECEIVE_DATA:
776 case SEND_DATA:
Preeti Ahuja95919342013-10-01 18:18:55 -0700777 case SET_UP_EVENT_LIST:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800778 return false;
779 }
780
781 return true;
782 }
783
Wink Savillee68857d2014-10-17 15:23:05 -0700784 private void handleDelayedCmd(int slotId) {
785 CatLog.d(LOG_TAG, "handleDelayedCmd, slotId: " + slotId);
786 if (mStkContext[slotId].mCmdsQ.size() != 0) {
787 DelayedCmd cmd = mStkContext[slotId].mCmdsQ.poll();
788 if (cmd != null) {
789 CatLog.d(LOG_TAG, "handleDelayedCmd - queue size: " +
790 mStkContext[slotId].mCmdsQ.size() +
791 " id: " + cmd.id + "sim id: " + cmd.slotId);
792 switch (cmd.id) {
793 case OP_CMD:
794 handleCmd(cmd.msg, cmd.slotId);
795 break;
796 case OP_END_SESSION:
797 handleSessionEnd(cmd.slotId);
798 break;
799 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800800 }
801 }
802 }
803
Wink Savillee68857d2014-10-17 15:23:05 -0700804 private void callDelayedMsg(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800805 Message msg = mServiceHandler.obtainMessage();
806 msg.arg1 = OP_DELAYED_MSG;
Wink Savillee68857d2014-10-17 15:23:05 -0700807 msg.arg2 = slotId;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800808 mServiceHandler.sendMessage(msg);
809 }
810
Wink Savillee68857d2014-10-17 15:23:05 -0700811 private void callSetActivityInstMsg(int inst_type, int slotId, Object obj) {
812 Message msg = mServiceHandler.obtainMessage();
813 msg.obj = obj;
814 msg.arg1 = inst_type;
815 msg.arg2 = slotId;
816 mServiceHandler.sendMessage(msg);
817 }
818
819 private void handleSessionEnd(int slotId) {
Legler Wuaeefef52014-10-27 00:57:18 +0800820 // We should finish all pending activity if receiving END SESSION command.
821 cleanUpInstanceStackBySlot(slotId);
822
Wink Savillee68857d2014-10-17 15:23:05 -0700823 mStkContext[slotId].mCurrentCmd = mStkContext[slotId].mMainCmd;
824 CatLog.d(LOG_TAG, "[handleSessionEnd] - mCurrentCmd changed to mMainCmd!.");
825 mStkContext[slotId].mCurrentMenuCmd = mStkContext[slotId].mMainCmd;
826 CatLog.d(LOG_TAG, "slotId: " + slotId + ", mMenuState: " +
827 mStkContext[slotId].mMenuState);
828
829 mStkContext[slotId].mIsInputPending = false;
830 mStkContext[slotId].mIsMenuPending = false;
831 mStkContext[slotId].mIsDialogPending = false;
832
Wink Savillee68857d2014-10-17 15:23:05 -0700833 if (mStkContext[slotId].mMainCmd == null) {
834 CatLog.d(LOG_TAG, "[handleSessionEnd][mMainCmd is null!]");
835 }
836 mStkContext[slotId].lastSelectedItem = null;
Wink Saville79085fc2009-06-09 10:27:23 -0700837 // In case of SET UP MENU command which removed the app, don't
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800838 // update the current menu member.
Wink Savillee68857d2014-10-17 15:23:05 -0700839 if (mStkContext[slotId].mCurrentMenu != null && mStkContext[slotId].mMainCmd != null) {
840 mStkContext[slotId].mCurrentMenu = mStkContext[slotId].mMainCmd.getMenu();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800841 }
Wink Savillee68857d2014-10-17 15:23:05 -0700842 CatLog.d(LOG_TAG, "[handleSessionEnd][mMenuState]" + mStkContext[slotId].mMenuIsVisible);
843 // In mutiple instance architecture, the main menu for slotId will be finished when user
844 // goes to the Stk menu of the other SIM. So, we should launch a new instance for the
845 // main menu if the main menu instance has been finished.
846 // If the current menu is secondary menu, we should launch main menu.
847 if (StkMenuActivity.STATE_SECONDARY == mStkContext[slotId].mMenuState) {
848 launchMenuActivity(null, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800849 }
Takanori Nakano3776e2c2016-10-14 16:54:28 +0900850
851 // Send a local broadcast as a notice that this service handled the session end event.
852 Intent intent = new Intent(SESSION_ENDED);
853 intent.putExtra(SLOT_ID, slotId);
854 LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
855
Wink Savillee68857d2014-10-17 15:23:05 -0700856 if (mStkContext[slotId].mCmdsQ.size() != 0) {
857 callDelayedMsg(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800858 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700859 mStkContext[slotId].mCmdInProgress = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800860 }
861 // In case a launch browser command was just confirmed, launch that url.
Wink Savillee68857d2014-10-17 15:23:05 -0700862 if (mStkContext[slotId].launchBrowser) {
863 mStkContext[slotId].launchBrowser = false;
864 launchBrowser(mStkContext[slotId].mBrowserSettings);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800865 }
866 }
867
Preeti Ahuja560be362014-11-25 19:38:24 -0800868 // returns true if any Stk related activity already has focus on the screen
Yoshiaki Nakacec55b82017-10-20 15:53:43 +0900869 boolean isTopOfStack() {
pkanwareab12f32017-02-06 08:35:03 -0800870 ActivityManager mActivityManager = (ActivityManager) mContext
Preeti Ahuja560be362014-11-25 19:38:24 -0800871 .getSystemService(ACTIVITY_SERVICE);
pkanwareab12f32017-02-06 08:35:03 -0800872 String currentPackageName = null;
873 List<RunningTaskInfo> tasks = mActivityManager.getRunningTasks(1);
874 if (tasks == null || tasks.get(0).topActivity == null) {
875 return false;
876 }
877 currentPackageName = tasks.get(0).topActivity.getPackageName();
Preeti Ahuja560be362014-11-25 19:38:24 -0800878 if (null != currentPackageName) {
879 return currentPackageName.equals(PACKAGE_NAME);
880 }
Preeti Ahuja560be362014-11-25 19:38:24 -0800881 return false;
882 }
883
Sooraj Sasindrana4160472016-10-12 16:28:25 -0700884 /**
885 * Get the boolean config from carrier config manager.
886 *
887 * @param context the context to get carrier service
888 * @param key config key defined in CarrierConfigManager
889 * @return boolean value of corresponding key.
890 */
891 private static boolean getBooleanCarrierConfig(Context context, String key) {
892 CarrierConfigManager configManager = (CarrierConfigManager) context.getSystemService(
893 Context.CARRIER_CONFIG_SERVICE);
894 PersistableBundle b = null;
895 if (configManager != null) {
896 b = configManager.getConfig();
897 }
898 if (b != null) {
899 return b.getBoolean(key);
900 } else {
901 // Return static default defined in CarrierConfigManager.
902 return CarrierConfigManager.getDefaultConfig().getBoolean(key);
903 }
904 }
905
Wink Savillee68857d2014-10-17 15:23:05 -0700906 private void handleCmd(CatCmdMessage cmdMsg, int slotId) {
Preeti Ahuja95919342013-10-01 18:18:55 -0700907
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800908 if (cmdMsg == null) {
909 return;
910 }
911 // save local reference for state tracking.
Wink Savillee68857d2014-10-17 15:23:05 -0700912 mStkContext[slotId].mCurrentCmd = cmdMsg;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800913 boolean waitForUsersResponse = true;
914
Wink Savillee68857d2014-10-17 15:23:05 -0700915 mStkContext[slotId].mIsInputPending = false;
916 mStkContext[slotId].mIsMenuPending = false;
917 mStkContext[slotId].mIsDialogPending = false;
918
919 CatLog.d(LOG_TAG,"[handleCmd]" + cmdMsg.getCmdType().name());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800920 switch (cmdMsg.getCmdType()) {
921 case DISPLAY_TEXT:
922 TextMessage msg = cmdMsg.geTextMessage();
Jeevaka Badrappan854a25c2012-12-01 16:32:03 +0200923 waitForUsersResponse = msg.responseNeeded;
Wink Savillee68857d2014-10-17 15:23:05 -0700924 if (mStkContext[slotId].lastSelectedItem != null) {
925 msg.title = mStkContext[slotId].lastSelectedItem;
926 } else if (mStkContext[slotId].mMainCmd != null){
Yoshiaki Naka28313ba2017-08-04 12:20:53 +0900927 if (!getResources().getBoolean(R.bool.show_menu_title_only_on_menu)) {
928 msg.title = mStkContext[slotId].mMainCmd.getMenu().title;
929 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800930 }
Preeti Ahuja560be362014-11-25 19:38:24 -0800931 //If we receive a low priority Display Text and the device is
932 // not displaying any STK related activity and the screen is not idle
933 // ( that is, device is in an interactive state), then send a screen busy
934 // terminal response. Otherwise display the message. The existing
935 // displayed message shall be updated with the new display text
936 // proactive command (Refer to ETSI TS 102 384 section 27.22.4.1.4.4.2).
937 if (!(msg.isHighPriority || mStkContext[slotId].mMenuIsVisible
938 || mStkContext[slotId].mDisplayTextDlgIsVisibile || isTopOfStack())) {
939 if(!isScreenIdle()) {
940 CatLog.d(LOG_TAG, "Screen is not idle");
941 sendScreenBusyResponse(slotId);
942 } else {
943 launchTextDialog(slotId);
944 }
945 } else {
946 launchTextDialog(slotId);
947 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800948 break;
949 case SELECT_ITEM:
Wink Savillee68857d2014-10-17 15:23:05 -0700950 CatLog.d(LOG_TAG, "SELECT_ITEM +");
951 mStkContext[slotId].mCurrentMenuCmd = mStkContext[slotId].mCurrentCmd;
952 mStkContext[slotId].mCurrentMenu = cmdMsg.getMenu();
953 launchMenuActivity(cmdMsg.getMenu(), slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800954 break;
955 case SET_UP_MENU:
Wink Savillee68857d2014-10-17 15:23:05 -0700956 mStkContext[slotId].mCmdInProgress = false;
957 mStkContext[slotId].mMainCmd = mStkContext[slotId].mCurrentCmd;
958 mStkContext[slotId].mCurrentMenuCmd = mStkContext[slotId].mCurrentCmd;
959 mStkContext[slotId].mCurrentMenu = cmdMsg.getMenu();
960 CatLog.d(LOG_TAG, "SET_UP_MENU [" + removeMenu(slotId) + "]");
961
962 if (removeMenu(slotId)) {
963 int i = 0;
964 CatLog.d(LOG_TAG, "removeMenu() - Uninstall App");
965 mStkContext[slotId].mCurrentMenu = null;
Preeti Ahuja95919342013-10-01 18:18:55 -0700966 mStkContext[slotId].mMainCmd = null;
Wink Savillee68857d2014-10-17 15:23:05 -0700967 //Check other setup menu state. If all setup menu are removed, uninstall apk.
968 for (i = PhoneConstants.SIM_ID_1; i < mSimCount; i++) {
969 if (i != slotId
Ryuto Sawadaf51e5e02016-11-25 17:12:49 +0900970 && (mStkContext[i].mSetupMenuState == STATE_UNKNOWN
971 || mStkContext[i].mSetupMenuState == STATE_EXIST)) {
Wink Savillee68857d2014-10-17 15:23:05 -0700972 CatLog.d(LOG_TAG, "Not Uninstall App:" + i + ","
Ryuto Sawadaf51e5e02016-11-25 17:12:49 +0900973 + mStkContext[i].mSetupMenuState);
Wink Savillee68857d2014-10-17 15:23:05 -0700974 break;
975 }
976 }
977 if (i == mSimCount) {
978 StkAppInstaller.unInstall(mContext);
979 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800980 } else {
Wink Savillee68857d2014-10-17 15:23:05 -0700981 CatLog.d(LOG_TAG, "install App");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800982 StkAppInstaller.install(mContext);
983 }
Wink Savillee68857d2014-10-17 15:23:05 -0700984 if (mStkContext[slotId].mMenuIsVisible) {
985 launchMenuActivity(null, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800986 }
987 break;
988 case GET_INPUT:
989 case GET_INKEY:
Wink Savillee68857d2014-10-17 15:23:05 -0700990 launchInputActivity(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800991 break;
992 case SET_UP_IDLE_MODE_TEXT:
993 waitForUsersResponse = false;
Preeti Ahuja560be362014-11-25 19:38:24 -0800994 mStkContext[slotId].mIdleModeTextCmd = mStkContext[slotId].mCurrentCmd;
995 TextMessage idleModeText = mStkContext[slotId].mCurrentCmd.geTextMessage();
996 if (idleModeText == null) {
997 launchIdleText(slotId);
998 mStkContext[slotId].mIdleModeTextCmd = null;
999 }
1000 mStkContext[slotId].mCurrentCmd = mStkContext[slotId].mMainCmd;
1001 if ((mStkContext[slotId].mIdleModeTextCmd != null) && isScreenIdle()) {
1002 CatLog.d(this, "set up idle mode");
1003 launchIdleText(slotId);
1004 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001005 break;
1006 case SEND_DTMF:
1007 case SEND_SMS:
1008 case SEND_SS:
1009 case SEND_USSD:
Preeti Ahuja95919342013-10-01 18:18:55 -07001010 case GET_CHANNEL_STATUS:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001011 waitForUsersResponse = false;
Wink Savillee68857d2014-10-17 15:23:05 -07001012 launchEventMessage(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001013 break;
1014 case LAUNCH_BROWSER:
Ryuto Sawada0bdc7192016-04-18 12:10:56 +09001015 // The device setup process should not be interrupted by launching browser.
1016 if (Settings.Global.getInt(mContext.getContentResolver(),
1017 Settings.Global.DEVICE_PROVISIONED, 0) == 0) {
1018 CatLog.d(this, "The command is not performed if the setup has not been completed.");
1019 sendScreenBusyResponse(slotId);
1020 break;
1021 }
Sooraj Sasindrana4160472016-10-12 16:28:25 -07001022
1023 /* Check if Carrier would not want to launch browser */
1024 if (getBooleanCarrierConfig(mContext,
1025 CarrierConfigManager.KEY_STK_DISABLE_LAUNCH_BROWSER_BOOL)) {
1026 CatLog.d(this, "Browser is not launched as per carrier.");
1027 sendResponse(RES_ID_DONE, slotId, true);
1028 break;
1029 }
1030
Srikanth Chintalaba103002015-11-30 10:49:52 -08001031 mStkContext[slotId].mBrowserSettings =
1032 mStkContext[slotId].mCurrentCmd.getBrowserSettings();
1033 if (!isUrlAvailableToLaunchBrowser(mStkContext[slotId].mBrowserSettings)) {
1034 CatLog.d(this, "Browser url property is not set - send error");
1035 sendResponse(RES_ID_ERROR, slotId, true);
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001036 } else {
Srikanth Chintalaba103002015-11-30 10:49:52 -08001037 TextMessage alphaId = mStkContext[slotId].mCurrentCmd.geTextMessage();
1038 if ((alphaId == null) || TextUtils.isEmpty(alphaId.text)) {
1039 // don't need user confirmation in this case
1040 // just launch the browser or spawn a new tab
1041 CatLog.d(this, "user confirmation is not currently needed.\n" +
1042 "supressing confirmation dialogue and confirming silently...");
1043 mStkContext[slotId].launchBrowser = true;
1044 sendResponse(RES_ID_CONFIRM, slotId, true);
1045 } else {
1046 launchConfirmationDialog(alphaId, slotId);
1047 }
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001048 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001049 break;
1050 case SET_UP_CALL:
Preeti Ahujadd240102013-08-30 17:25:06 -07001051 TextMessage mesg = mStkContext[slotId].mCurrentCmd.getCallSettings().confirmMsg;
1052 if((mesg != null) && (mesg.text == null || mesg.text.length() == 0)) {
1053 mesg.text = getResources().getString(R.string.default_setup_call_msg);
1054 }
1055 CatLog.d(this, "SET_UP_CALL mesg.text " + mesg.text);
1056 launchConfirmationDialog(mesg, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001057 break;
1058 case PLAY_TONE:
Srikanth Chintala89aa6602014-03-14 16:26:57 +05301059 handlePlayTone(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001060 break;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001061 case OPEN_CHANNEL:
Wink Savillee68857d2014-10-17 15:23:05 -07001062 launchOpenChannelDialog(slotId);
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001063 break;
1064 case CLOSE_CHANNEL:
1065 case RECEIVE_DATA:
1066 case SEND_DATA:
Wink Savillee68857d2014-10-17 15:23:05 -07001067 TextMessage m = mStkContext[slotId].mCurrentCmd.geTextMessage();
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001068
1069 if ((m != null) && (m.text == null)) {
1070 switch(cmdMsg.getCmdType()) {
1071 case CLOSE_CHANNEL:
1072 m.text = getResources().getString(R.string.default_close_channel_msg);
1073 break;
1074 case RECEIVE_DATA:
1075 m.text = getResources().getString(R.string.default_receive_data_msg);
1076 break;
1077 case SEND_DATA:
1078 m.text = getResources().getString(R.string.default_send_data_msg);
1079 break;
1080 }
1081 }
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001082 /*
1083 * Display indication in the form of a toast to the user if required.
1084 */
Wink Savillee68857d2014-10-17 15:23:05 -07001085 launchEventMessage(slotId);
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001086 break;
Preeti Ahuja95919342013-10-01 18:18:55 -07001087 case SET_UP_EVENT_LIST:
1088 mStkContext[slotId].mSetupEventListSettings =
1089 mStkContext[slotId].mCurrentCmd.getSetEventList();
1090 mStkContext[slotId].mCurrentSetupEventCmd = mStkContext[slotId].mCurrentCmd;
1091 mStkContext[slotId].mCurrentCmd = mStkContext[slotId].mMainCmd;
Preeti Ahuja560be362014-11-25 19:38:24 -08001092 if (isScreenIdle()) {
1093 CatLog.d(this," Check if IDLE_SCREEN_AVAILABLE_EVENT is present in List");
1094 checkForSetupEvent(IDLE_SCREEN_AVAILABLE_EVENT, null, slotId);
1095 }
Preeti Ahuja95919342013-10-01 18:18:55 -07001096 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001097 }
1098
1099 if (!waitForUsersResponse) {
Wink Savillee68857d2014-10-17 15:23:05 -07001100 if (mStkContext[slotId].mCmdsQ.size() != 0) {
1101 callDelayedMsg(slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001102 } else {
Wink Savillee68857d2014-10-17 15:23:05 -07001103 mStkContext[slotId].mCmdInProgress = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001104 }
1105 }
1106 }
1107
Wink Savillee68857d2014-10-17 15:23:05 -07001108 private void handleCmdResponse(Bundle args, int slotId) {
1109 CatLog.d(LOG_TAG, "handleCmdResponse, sim id: " + slotId);
1110 if (mStkContext[slotId].mCurrentCmd == null) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001111 return;
1112 }
Wink Savillee68857d2014-10-17 15:23:05 -07001113
1114 if (mStkService[slotId] == null) {
Legler Wuaeefef52014-10-27 00:57:18 +08001115 mStkService[slotId] = CatService.getInstance(slotId);
Wink Savillee68857d2014-10-17 15:23:05 -07001116 if (mStkService[slotId] == null) {
Alex Yakavenkad8e2ecd2012-04-20 17:10:15 -07001117 // This should never happen (we should be responding only to a message
1118 // that arrived from StkService). It has to exist by this time
Wink Savillee68857d2014-10-17 15:23:05 -07001119 CatLog.d(LOG_TAG, "Exception! mStkService is null when we need to send response.");
Alex Yakavenkad8e2ecd2012-04-20 17:10:15 -07001120 throw new RuntimeException("mStkService is null when we need to send response");
1121 }
1122 }
1123
Wink Savillee68857d2014-10-17 15:23:05 -07001124 CatResponseMessage resMsg = new CatResponseMessage(mStkContext[slotId].mCurrentCmd);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001125
1126 // set result code
1127 boolean helpRequired = args.getBoolean(HELP, false);
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001128 boolean confirmed = false;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001129
1130 switch(args.getInt(RES_ID)) {
1131 case RES_ID_MENU_SELECTION:
Wink Savillee68857d2014-10-17 15:23:05 -07001132 CatLog.d(LOG_TAG, "MENU_SELECTION=" + mStkContext[slotId].
1133 mCurrentMenuCmd.getCmdType());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001134 int menuSelection = args.getInt(MENU_SELECTION);
Wink Savillee68857d2014-10-17 15:23:05 -07001135 switch(mStkContext[slotId].mCurrentMenuCmd.getCmdType()) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001136 case SET_UP_MENU:
1137 case SELECT_ITEM:
Wink Savillee68857d2014-10-17 15:23:05 -07001138 mStkContext[slotId].lastSelectedItem = getItemName(menuSelection, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001139 if (helpRequired) {
1140 resMsg.setResultCode(ResultCode.HELP_INFO_REQUIRED);
1141 } else {
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301142 resMsg.setResultCode(mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ?
1143 ResultCode.PRFRMD_ICON_NOT_DISPLAYED : ResultCode.OK);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001144 }
1145 resMsg.setMenuSelection(menuSelection);
1146 break;
1147 }
1148 break;
1149 case RES_ID_INPUT:
Wink Savillee68857d2014-10-17 15:23:05 -07001150 CatLog.d(LOG_TAG, "RES_ID_INPUT");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001151 String input = args.getString(INPUT);
Wink Savillee68857d2014-10-17 15:23:05 -07001152 if (input != null && (null != mStkContext[slotId].mCurrentCmd.geInput()) &&
1153 (mStkContext[slotId].mCurrentCmd.geInput().yesNo)) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001154 boolean yesNoSelection = input
1155 .equals(StkInputActivity.YES_STR_RESPONSE);
1156 resMsg.setYesNo(yesNoSelection);
1157 } else {
1158 if (helpRequired) {
1159 resMsg.setResultCode(ResultCode.HELP_INFO_REQUIRED);
1160 } else {
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301161 resMsg.setResultCode(mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ?
1162 ResultCode.PRFRMD_ICON_NOT_DISPLAYED : ResultCode.OK);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001163 resMsg.setInput(input);
1164 }
1165 }
1166 break;
1167 case RES_ID_CONFIRM:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -07001168 CatLog.d(this, "RES_ID_CONFIRM");
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001169 confirmed = args.getBoolean(CONFIRMATION);
Wink Savillee68857d2014-10-17 15:23:05 -07001170 switch (mStkContext[slotId].mCurrentCmd.getCmdType()) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001171 case DISPLAY_TEXT:
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301172 if (confirmed) {
1173 resMsg.setResultCode(mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ?
1174 ResultCode.PRFRMD_ICON_NOT_DISPLAYED : ResultCode.OK);
1175 } else {
1176 resMsg.setResultCode(ResultCode.UICC_SESSION_TERM_BY_USER);
1177 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001178 break;
1179 case LAUNCH_BROWSER:
1180 resMsg.setResultCode(confirmed ? ResultCode.OK
1181 : ResultCode.UICC_SESSION_TERM_BY_USER);
1182 if (confirmed) {
Wink Savillee68857d2014-10-17 15:23:05 -07001183 mStkContext[slotId].launchBrowser = true;
1184 mStkContext[slotId].mBrowserSettings =
1185 mStkContext[slotId].mCurrentCmd.getBrowserSettings();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001186 }
1187 break;
1188 case SET_UP_CALL:
1189 resMsg.setResultCode(ResultCode.OK);
1190 resMsg.setConfirmation(confirmed);
1191 if (confirmed) {
Wink Savillee68857d2014-10-17 15:23:05 -07001192 launchEventMessage(slotId,
1193 mStkContext[slotId].mCurrentCmd.getCallSettings().callMsg);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001194 }
1195 break;
1196 }
1197 break;
1198 case RES_ID_DONE:
1199 resMsg.setResultCode(ResultCode.OK);
1200 break;
1201 case RES_ID_BACKWARD:
Wink Savillee68857d2014-10-17 15:23:05 -07001202 CatLog.d(LOG_TAG, "RES_ID_BACKWARD");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001203 resMsg.setResultCode(ResultCode.BACKWARD_MOVE_BY_USER);
1204 break;
1205 case RES_ID_END_SESSION:
Wink Savillee68857d2014-10-17 15:23:05 -07001206 CatLog.d(LOG_TAG, "RES_ID_END_SESSION");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001207 resMsg.setResultCode(ResultCode.UICC_SESSION_TERM_BY_USER);
1208 break;
1209 case RES_ID_TIMEOUT:
Wink Savillee68857d2014-10-17 15:23:05 -07001210 CatLog.d(LOG_TAG, "RES_ID_TIMEOUT");
Naveen Kallad5176892009-11-30 12:45:29 -08001211 // GCF test-case 27.22.4.1.1 Expected Sequence 1.5 (DISPLAY TEXT,
1212 // Clear message after delay, successful) expects result code OK.
1213 // If the command qualifier specifies no user response is required
1214 // then send OK instead of NO_RESPONSE_FROM_USER
Wink Savillee68857d2014-10-17 15:23:05 -07001215 if ((mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1216 AppInterface.CommandType.DISPLAY_TEXT.value())
1217 && (mStkContext[slotId].mCurrentCmd.geTextMessage().userClear == false)) {
Naveen Kallad5176892009-11-30 12:45:29 -08001218 resMsg.setResultCode(ResultCode.OK);
1219 } else {
1220 resMsg.setResultCode(ResultCode.NO_RESPONSE_FROM_USER);
1221 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001222 break;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001223 case RES_ID_CHOICE:
1224 int choice = args.getInt(CHOICE);
1225 CatLog.d(this, "User Choice=" + choice);
1226 switch (choice) {
1227 case YES:
1228 resMsg.setResultCode(ResultCode.OK);
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001229 confirmed = true;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001230 break;
1231 case NO:
1232 resMsg.setResultCode(ResultCode.USER_NOT_ACCEPT);
1233 break;
1234 }
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001235
Wink Savillee68857d2014-10-17 15:23:05 -07001236 if (mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1237 AppInterface.CommandType.OPEN_CHANNEL.value()) {
Siddartha Pothapragadacacf7b82012-04-16 19:42:38 -07001238 resMsg.setConfirmation(confirmed);
1239 }
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001240 break;
Srikanth Chintalaba103002015-11-30 10:49:52 -08001241 case RES_ID_ERROR:
1242 CatLog.d(LOG_TAG, "RES_ID_ERROR");
1243 switch (mStkContext[slotId].mCurrentCmd.getCmdType()) {
1244 case LAUNCH_BROWSER:
1245 resMsg.setResultCode(ResultCode.LAUNCH_BROWSER_ERROR);
1246 break;
1247 }
1248 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001249 default:
Wink Savillee68857d2014-10-17 15:23:05 -07001250 CatLog.d(LOG_TAG, "Unknown result id");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001251 return;
1252 }
Wink Savillee68857d2014-10-17 15:23:05 -07001253
1254 if (null != mStkContext[slotId].mCurrentCmd &&
1255 null != mStkContext[slotId].mCurrentCmd.getCmdType()) {
1256 CatLog.d(LOG_TAG, "handleCmdResponse- cmdName[" +
1257 mStkContext[slotId].mCurrentCmd.getCmdType().name() + "]");
1258 }
1259 mStkService[slotId].onCmdResponse(resMsg);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001260 }
1261
1262 /**
1263 * Returns 0 or FLAG_ACTIVITY_NO_USER_ACTION, 0 means the user initiated the action.
1264 *
1265 * @param userAction If the userAction is yes then we always return 0 otherwise
1266 * mMenuIsVisible is used to determine what to return. If mMenuIsVisible is true
1267 * then we are the foreground app and we'll return 0 as from our perspective a
1268 * user action did cause. If it's false than we aren't the foreground app and
1269 * FLAG_ACTIVITY_NO_USER_ACTION is returned.
Wink Saville79085fc2009-06-09 10:27:23 -07001270 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001271 * @return 0 or FLAG_ACTIVITY_NO_USER_ACTION
1272 */
Wink Savillee68857d2014-10-17 15:23:05 -07001273 private int getFlagActivityNoUserAction(InitiatedByUserAction userAction, int slotId) {
1274 return ((userAction == InitiatedByUserAction.yes) | mStkContext[slotId].mMenuIsVisible)
1275 ? 0 : Intent.FLAG_ACTIVITY_NO_USER_ACTION;
1276 }
1277 /**
1278 * This method is used for cleaning up pending instances in stack.
1279 */
1280 private void cleanUpInstanceStackBySlot(int slotId) {
1281 Activity activity = mStkContext[slotId].getPendingActivityInstance();
1282 Activity dialog = mStkContext[slotId].getPendingDialogInstance();
1283 CatLog.d(LOG_TAG, "cleanUpInstanceStackBySlot slotId: " + slotId);
Legler Wuaeefef52014-10-27 00:57:18 +08001284 if (mStkContext[slotId].mCurrentCmd == null) {
1285 CatLog.d(LOG_TAG, "current cmd is null.");
1286 return;
1287 }
Wink Savillee68857d2014-10-17 15:23:05 -07001288 if (activity != null) {
1289 CatLog.d(LOG_TAG, "current cmd type: " +
1290 mStkContext[slotId].mCurrentCmd.getCmdType());
1291 if (mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1292 AppInterface.CommandType.GET_INPUT.value() ||
1293 mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1294 AppInterface.CommandType.GET_INKEY.value()) {
1295 mStkContext[slotId].mIsInputPending = true;
1296 } else if (mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1297 AppInterface.CommandType.SET_UP_MENU.value() ||
1298 mStkContext[slotId].mCurrentCmd.getCmdType().value() ==
1299 AppInterface.CommandType.SELECT_ITEM.value()) {
1300 mStkContext[slotId].mIsMenuPending = true;
1301 } else {
1302 }
1303 CatLog.d(LOG_TAG, "finish pending activity.");
1304 activity.finish();
1305 mStkContext[slotId].mActivityInstance = null;
1306 }
1307 if (dialog != null) {
1308 CatLog.d(LOG_TAG, "finish pending dialog.");
1309 mStkContext[slotId].mIsDialogPending = true;
1310 dialog.finish();
1311 mStkContext[slotId].mDialogInstance = null;
1312 }
1313 }
1314 /**
1315 * This method is used for restoring pending instances from stack.
1316 */
1317 private void restoreInstanceFromStackBySlot(int slotId) {
1318 AppInterface.CommandType cmdType = mStkContext[slotId].mCurrentCmd.getCmdType();
1319
1320 CatLog.d(LOG_TAG, "restoreInstanceFromStackBySlot cmdType : " + cmdType);
1321 switch(cmdType) {
1322 case GET_INPUT:
1323 case GET_INKEY:
1324 launchInputActivity(slotId);
1325 //Set mMenuIsVisible to true for showing main menu for
1326 //following session end command.
1327 mStkContext[slotId].mMenuIsVisible = true;
1328 break;
1329 case DISPLAY_TEXT:
1330 launchTextDialog(slotId);
1331 break;
1332 case LAUNCH_BROWSER:
1333 launchConfirmationDialog(mStkContext[slotId].mCurrentCmd.geTextMessage(),
1334 slotId);
1335 break;
1336 case OPEN_CHANNEL:
1337 launchOpenChannelDialog(slotId);
1338 break;
1339 case SET_UP_CALL:
1340 launchConfirmationDialog(mStkContext[slotId].mCurrentCmd.getCallSettings().
1341 confirmMsg, slotId);
1342 break;
1343 case SET_UP_MENU:
1344 case SELECT_ITEM:
1345 launchMenuActivity(null, slotId);
1346 break;
1347 default:
1348 break;
1349 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001350 }
1351
Wink Savillee68857d2014-10-17 15:23:05 -07001352 private void launchMenuActivity(Menu menu, int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001353 Intent newIntent = new Intent(Intent.ACTION_VIEW);
Wink Savillee68857d2014-10-17 15:23:05 -07001354 String targetActivity = STK_MENU_ACTIVITY_NAME;
1355 String uriString = STK_MENU_URI + System.currentTimeMillis();
1356 //Set unique URI to create a new instance of activity for different slotId.
1357 Uri uriData = Uri.parse(uriString);
1358
1359 CatLog.d(LOG_TAG, "launchMenuActivity, slotId: " + slotId + " , " +
1360 uriData.toString() + " , " + mStkContext[slotId].mOpCode + ", "
1361 + mStkContext[slotId].mMenuState);
1362 newIntent.setClassName(PACKAGE_NAME, targetActivity);
1363 int intentFlags = Intent.FLAG_ACTIVITY_NEW_TASK;
1364
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001365 if (menu == null) {
1366 // We assume this was initiated by the user pressing the tool kit icon
Wink Savillee68857d2014-10-17 15:23:05 -07001367 intentFlags |= getFlagActivityNoUserAction(InitiatedByUserAction.yes, slotId);
1368 if (mStkContext[slotId].mOpCode == OP_END_SESSION) {
1369 CatLog.d(LOG_TAG, "launchMenuActivity, return OP_END_SESSION");
1370 mStkContext[slotId].mMenuState = StkMenuActivity.STATE_MAIN;
1371 if (mStkContext[slotId].mMainActivityInstance != null) {
1372 CatLog.d(LOG_TAG, "launchMenuActivity, mMainActivityInstance is not null");
1373 return;
1374 }
Wink Savillee68857d2014-10-17 15:23:05 -07001375 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001376
Wink Savillee68857d2014-10-17 15:23:05 -07001377 //If the last pending menu is secondary menu, "STATE" should be "STATE_SECONDARY".
1378 //Otherwise, it should be "STATE_MAIN".
1379 if (mStkContext[slotId].mOpCode == OP_LAUNCH_APP &&
1380 mStkContext[slotId].mMenuState == StkMenuActivity.STATE_SECONDARY) {
1381 newIntent.putExtra("STATE", StkMenuActivity.STATE_SECONDARY);
1382 } else {
1383 newIntent.putExtra("STATE", StkMenuActivity.STATE_MAIN);
1384 mStkContext[slotId].mMenuState = StkMenuActivity.STATE_MAIN;
1385 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001386 } else {
1387 // We don't know and we'll let getFlagActivityNoUserAction decide.
Wink Savillee68857d2014-10-17 15:23:05 -07001388 intentFlags |= getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001389 newIntent.putExtra("STATE", StkMenuActivity.STATE_SECONDARY);
Wink Savillee68857d2014-10-17 15:23:05 -07001390 mStkContext[slotId].mMenuState = StkMenuActivity.STATE_SECONDARY;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001391 }
Wink Savillee68857d2014-10-17 15:23:05 -07001392 newIntent.putExtra(SLOT_ID, slotId);
1393 newIntent.setData(uriData);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001394 newIntent.setFlags(intentFlags);
1395 mContext.startActivity(newIntent);
1396 }
1397
Wink Savillee68857d2014-10-17 15:23:05 -07001398 private void launchInputActivity(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001399 Intent newIntent = new Intent(Intent.ACTION_VIEW);
Wink Savillee68857d2014-10-17 15:23:05 -07001400 String targetActivity = STK_INPUT_ACTIVITY_NAME;
1401 String uriString = STK_INPUT_URI + System.currentTimeMillis();
1402 //Set unique URI to create a new instance of activity for different slotId.
1403 Uri uriData = Uri.parse(uriString);
1404
1405 CatLog.d(LOG_TAG, "launchInputActivity, slotId: " + slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001406 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
Wink Savillee68857d2014-10-17 15:23:05 -07001407 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1408 newIntent.setClassName(PACKAGE_NAME, targetActivity);
1409 newIntent.putExtra("INPUT", mStkContext[slotId].mCurrentCmd.geInput());
1410 newIntent.putExtra(SLOT_ID, slotId);
1411 newIntent.setData(uriData);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001412 mContext.startActivity(newIntent);
1413 }
1414
Wink Savillee68857d2014-10-17 15:23:05 -07001415 private void launchTextDialog(int slotId) {
1416 CatLog.d(LOG_TAG, "launchTextDialog, slotId: " + slotId);
1417 Intent newIntent = new Intent();
1418 String targetActivity = STK_DIALOG_ACTIVITY_NAME;
1419 int action = getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId);
1420 String uriString = STK_DIALOG_URI + System.currentTimeMillis();
1421 //Set unique URI to create a new instance of activity for different slotId.
1422 Uri uriData = Uri.parse(uriString);
1423 if (newIntent != null) {
1424 newIntent.setClassName(PACKAGE_NAME, targetActivity);
1425 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1426 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
1427 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1428 newIntent.setData(uriData);
1429 newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
1430 newIntent.putExtra(SLOT_ID, slotId);
1431 startActivity(newIntent);
Preeti Ahuja414bc412013-06-25 19:31:49 -07001432 // For display texts with immediate response, send the terminal response
1433 // immediately. responseNeeded will be false, if display text command has
1434 // the immediate response tlv.
1435 if (!mStkContext[slotId].mCurrentCmd.geTextMessage().responseNeeded) {
1436 sendResponse(RES_ID_CONFIRM, slotId, true);
1437 }
Wink Savillee68857d2014-10-17 15:23:05 -07001438 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001439 }
1440
Wink Savillee68857d2014-10-17 15:23:05 -07001441 public boolean isStkDialogActivated(Context context) {
1442 String stkDialogActivity = "com.android.stk.StkDialogActivity";
1443 boolean activated = false;
1444 final ActivityManager am = (ActivityManager) context.getSystemService(
1445 Context.ACTIVITY_SERVICE);
1446 String topActivity = am.getRunningTasks(1).get(0).topActivity.getClassName();
1447
1448 CatLog.d(LOG_TAG, "isStkDialogActivated: " + topActivity);
1449 if (topActivity.equals(stkDialogActivity)) {
1450 activated = true;
1451 }
1452 CatLog.d(LOG_TAG, "activated : " + activated);
1453 return activated;
Johan Hellman3aec01c2011-02-10 10:15:28 +01001454 }
1455
Preeti Ahuja95919342013-10-01 18:18:55 -07001456 private void sendSetUpEventResponse(int event, byte[] addedInfo, int slotId) {
Preeti Ahuja414bc412013-06-25 19:31:49 -07001457 CatLog.d(this, "sendSetUpEventResponse: event : " + event + "slotId = " + slotId);
Preeti Ahuja95919342013-10-01 18:18:55 -07001458
1459 if (mStkContext[slotId].mCurrentSetupEventCmd == null){
1460 CatLog.e(this, "mCurrentSetupEventCmd is null");
1461 return;
1462 }
1463
1464 CatResponseMessage resMsg = new CatResponseMessage(mStkContext[slotId].mCurrentSetupEventCmd);
1465
1466 resMsg.setResultCode(ResultCode.OK);
1467 resMsg.setEventDownload(event, addedInfo);
1468
1469 mStkService[slotId].onCmdResponse(resMsg);
1470 }
1471
1472 private void checkForSetupEvent(int event, Bundle args, int slotId) {
1473 boolean eventPresent = false;
1474 byte[] addedInfo = null;
1475 CatLog.d(this, "Event :" + event);
1476
1477 if (mStkContext[slotId].mSetupEventListSettings != null) {
1478 /* Checks if the event is present in the EventList updated by last
1479 * SetupEventList Proactive Command */
1480 for (int i : mStkContext[slotId].mSetupEventListSettings.eventList) {
1481 if (event == i) {
1482 eventPresent = true;
1483 break;
1484 }
1485 }
1486
1487 /* If Event is present send the response to ICC */
1488 if (eventPresent == true) {
1489 CatLog.d(this, " Event " + event + "exists in the EventList");
1490
1491 switch (event) {
Preeti Ahuja560be362014-11-25 19:38:24 -08001492 case IDLE_SCREEN_AVAILABLE_EVENT:
1493 sendSetUpEventResponse(event, addedInfo, slotId);
1494 removeSetUpEvent(event, slotId);
1495 break;
Preeti Ahuja95919342013-10-01 18:18:55 -07001496 case LANGUAGE_SELECTION_EVENT:
1497 String language = mContext
1498 .getResources().getConfiguration().locale.getLanguage();
1499 CatLog.d(this, "language: " + language);
1500 // Each language code is a pair of alpha-numeric characters.
1501 // Each alpha-numeric character shall be coded on one byte
1502 // using the SMS default 7-bit coded alphabet
1503 addedInfo = GsmAlphabet.stringToGsm8BitPacked(language);
1504 sendSetUpEventResponse(event, addedInfo, slotId);
1505 break;
1506 default:
1507 break;
1508 }
1509 } else {
1510 CatLog.e(this, " Event does not exist in the EventList");
1511 }
1512 } else {
1513 CatLog.e(this, "SetupEventList is not received. Ignoring the event: " + event);
1514 }
1515 }
1516
1517 private void removeSetUpEvent(int event, int slotId) {
1518 CatLog.d(this, "Remove Event :" + event);
1519
1520 if (mStkContext[slotId].mSetupEventListSettings != null) {
1521 /*
1522 * Make new Eventlist without the event
1523 */
1524 for (int i = 0; i < mStkContext[slotId].mSetupEventListSettings.eventList.length; i++) {
1525 if (event == mStkContext[slotId].mSetupEventListSettings.eventList[i]) {
1526 mStkContext[slotId].mSetupEventListSettings.eventList[i] = INVALID_SETUP_EVENT;
1527 break;
1528 }
1529 }
1530 }
1531 }
1532
1533 private void launchEventMessage(int slotId) {
1534 launchEventMessage(slotId, mStkContext[slotId].mCurrentCmd.geTextMessage());
1535 }
1536
Wink Savillee68857d2014-10-17 15:23:05 -07001537 private void launchEventMessage(int slotId, TextMessage msg) {
1538 if (msg == null || (msg.text != null && msg.text.length() == 0)) {
1539 CatLog.d(LOG_TAG, "launchEventMessage return");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001540 return;
1541 }
Wink Savillee68857d2014-10-17 15:23:05 -07001542
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001543 Toast toast = new Toast(mContext.getApplicationContext());
1544 LayoutInflater inflate = (LayoutInflater) mContext
1545 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1546 View v = inflate.inflate(R.layout.stk_event_msg, null);
1547 TextView tv = (TextView) v
1548 .findViewById(com.android.internal.R.id.message);
1549 ImageView iv = (ImageView) v
1550 .findViewById(com.android.internal.R.id.icon);
1551 if (msg.icon != null) {
1552 iv.setImageBitmap(msg.icon);
1553 } else {
1554 iv.setVisibility(View.GONE);
1555 }
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301556 /* In case of 'self explanatory' stkapp should display the specified
1557 * icon in proactive command (but not the alpha string).
1558 * If icon is non-self explanatory and if the icon could not be displayed
1559 * then alpha string or text data should be displayed
1560 * Ref: ETSI 102.223,section 6.5.4
1561 */
1562 if (mStkContext[slotId].mCurrentCmd.hasIconLoadFailed() ||
1563 msg.icon == null || !msg.iconSelfExplanatory) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001564 tv.setText(msg.text);
1565 }
1566
1567 toast.setView(v);
1568 toast.setDuration(Toast.LENGTH_LONG);
1569 toast.setGravity(Gravity.BOTTOM, 0, 0);
1570 toast.show();
1571 }
1572
Wink Savillee68857d2014-10-17 15:23:05 -07001573 private void launchConfirmationDialog(TextMessage msg, int slotId) {
1574 msg.title = mStkContext[slotId].lastSelectedItem;
1575 Intent newIntent = new Intent();
1576 String targetActivity = STK_DIALOG_ACTIVITY_NAME;
1577 String uriString = STK_DIALOG_URI + System.currentTimeMillis();
1578 //Set unique URI to create a new instance of activity for different slotId.
1579 Uri uriData = Uri.parse(uriString);
1580
1581 if (newIntent != null) {
1582 newIntent.setClassName(this, targetActivity);
1583 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1584 | Intent.FLAG_ACTIVITY_NO_HISTORY
1585 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
1586 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1587 newIntent.putExtra("TEXT", msg);
1588 newIntent.putExtra(SLOT_ID, slotId);
1589 newIntent.setData(uriData);
1590 startActivity(newIntent);
1591 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001592 }
1593
1594 private void launchBrowser(BrowserSettings settings) {
1595 if (settings == null) {
1596 return;
1597 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001598
Abhishek Adappa840c82f2013-02-26 10:19:49 -08001599 Uri data = null;
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001600 String url;
1601 if (settings.url == null) {
Abhishek Adappa840c82f2013-02-26 10:19:49 -08001602 // if the command did not contain a URL,
1603 // launch the browser to the default homepage.
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001604 CatLog.d(this, "no url data provided by proactive command." +
1605 " launching browser with stk default URL ... ");
1606 url = SystemProperties.get(STK_BROWSER_DEFAULT_URL_SYSPROP,
1607 "http://www.google.com");
1608 } else {
1609 CatLog.d(this, "launch browser command has attached url = " + settings.url);
1610 url = settings.url;
Abhishek Adappa840c82f2013-02-26 10:19:49 -08001611 }
David Brown7c03cfe2011-10-20 15:36:12 -07001612
Preeti Ahujaa7cdca22013-10-01 18:20:56 -07001613 if (url.startsWith("http://") || url.startsWith("https://")) {
1614 data = Uri.parse(url);
1615 CatLog.d(this, "launching browser with url = " + url);
1616 } else {
1617 String modifiedUrl = "http://" + url;
1618 data = Uri.parse(modifiedUrl);
1619 CatLog.d(this, "launching browser with modified url = " + modifiedUrl);
1620 }
1621
1622 Intent intent = new Intent(Intent.ACTION_VIEW);
1623 intent.setData(data);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001624 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1625 switch (settings.mode) {
1626 case USE_EXISTING_BROWSER:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001627 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1628 break;
1629 case LAUNCH_NEW_BROWSER:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001630 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1631 break;
1632 case LAUNCH_IF_NOT_ALREADY_LAUNCHED:
1633 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1634 break;
1635 }
1636 // start browser activity
1637 startActivity(intent);
1638 // a small delay, let the browser start, before processing the next command.
Wink Saville79085fc2009-06-09 10:27:23 -07001639 // this is good for scenarios where a related DISPLAY TEXT command is
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001640 // followed immediately.
1641 try {
Ryuto Sawada350aaa62016-06-13 14:47:22 +09001642 Thread.sleep(3000);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001643 } catch (InterruptedException e) {}
1644 }
1645
Wink Savillee68857d2014-10-17 15:23:05 -07001646 private void launchIdleText(int slotId) {
Preeti Ahuja560be362014-11-25 19:38:24 -08001647 TextMessage msg = mStkContext[slotId].mIdleModeTextCmd.geTextMessage();
dujin.cha2a0eb2a2011-11-11 15:03:57 +09001648
Preeti Ahuja95919342013-10-01 18:18:55 -07001649 if (msg == null || msg.text ==null) {
1650 CatLog.d(LOG_TAG, msg == null ? "mCurrent.getTextMessage is NULL"
1651 : "mCurrent.getTextMessage.text is NULL");
Wink Savillee68857d2014-10-17 15:23:05 -07001652 mNotificationManager.cancel(getNotificationId(slotId));
Wink Saville046db4b2011-11-01 20:42:54 -07001653 return;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001654 } else {
Preeti Ahuja95919342013-10-01 18:18:55 -07001655 CatLog.d(LOG_TAG, "launchIdleText - text[" + msg.text
1656 + "] iconSelfExplanatory[" + msg.iconSelfExplanatory
1657 + "] icon[" + msg.icon + "], sim id: " + slotId);
Wink Savillee68857d2014-10-17 15:23:05 -07001658 CatLog.d(LOG_TAG, "Add IdleMode text");
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001659 PendingIntent pendingIntent = PendingIntent.getService(mContext, 0,
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001660 new Intent(mContext, StkAppService.class), 0);
fionaxu805eb572017-05-02 10:57:30 -07001661 createAllChannels();
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001662 final Notification.Builder notificationBuilder = new Notification.Builder(
fionaxu2c91c752017-04-21 18:11:57 -07001663 StkAppService.this, STK_NOTIFICATION_CHANNEL_ID);
Wink Savillee68857d2014-10-17 15:23:05 -07001664 if (mStkContext[slotId].mMainCmd != null &&
1665 mStkContext[slotId].mMainCmd.getMenu() != null) {
1666 notificationBuilder.setContentTitle(mStkContext[slotId].mMainCmd.getMenu().title);
Christopher Posselwhite2ad2ab72013-05-27 07:51:06 +02001667 } else {
1668 notificationBuilder.setContentTitle("");
1669 }
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001670 notificationBuilder
1671 .setSmallIcon(com.android.internal.R.drawable.stat_notify_sim_toolkit);
1672 notificationBuilder.setContentIntent(pendingIntent);
1673 notificationBuilder.setOngoing(true);
1674 // Set text and icon for the status bar and notification body.
Preeti Ahujaf33bf392013-09-28 19:11:31 +05301675 if (mStkContext[slotId].mIdleModeTextCmd.hasIconLoadFailed() ||
1676 !msg.iconSelfExplanatory) {
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001677 notificationBuilder.setContentText(msg.text);
Christopher Posselwhite2ad2ab72013-05-27 07:51:06 +02001678 notificationBuilder.setTicker(msg.text);
Alex Khlivnuik50e0a1d2013-01-25 12:50:48 +01001679 }
1680 if (msg.icon != null) {
1681 notificationBuilder.setLargeIcon(msg.icon);
1682 } else {
1683 Bitmap bitmapIcon = BitmapFactory.decodeResource(StkAppService.this
1684 .getResources().getSystem(),
1685 com.android.internal.R.drawable.stat_notify_sim_toolkit);
1686 notificationBuilder.setLargeIcon(bitmapIcon);
1687 }
Selim Cinek62eb3fe2014-08-27 17:52:23 +02001688 notificationBuilder.setColor(mContext.getResources().getColor(
1689 com.android.internal.R.color.system_notification_accent_color));
Wink Savillee68857d2014-10-17 15:23:05 -07001690 mNotificationManager.notify(getNotificationId(slotId), notificationBuilder.build());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001691 }
1692 }
1693
fionaxu805eb572017-05-02 10:57:30 -07001694 /** Creates the notification channel and registers it with NotificationManager.
1695 * If a channel with the same ID is already registered, NotificationManager will
1696 * ignore this call.
1697 */
1698 private void createAllChannels() {
1699 mNotificationManager.createNotificationChannel(new NotificationChannel(
1700 STK_NOTIFICATION_CHANNEL_ID,
1701 getResources().getString(R.string.stk_channel_name),
1702 NotificationManager.IMPORTANCE_MIN));
1703 }
1704
Wink Savillee68857d2014-10-17 15:23:05 -07001705 private void launchToneDialog(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001706 Intent newIntent = new Intent(this, ToneDialog.class);
Wink Savillee68857d2014-10-17 15:23:05 -07001707 String uriString = STK_TONE_URI + slotId;
1708 Uri uriData = Uri.parse(uriString);
1709 //Set unique URI to create a new instance of activity for different slotId.
1710 CatLog.d(LOG_TAG, "launchToneDialog, slotId: " + slotId);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001711 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1712 | Intent.FLAG_ACTIVITY_NO_HISTORY
1713 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
Wink Savillee68857d2014-10-17 15:23:05 -07001714 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1715 newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
1716 newIntent.putExtra("TONE", mStkContext[slotId].mCurrentCmd.getToneSettings());
1717 newIntent.putExtra(SLOT_ID, slotId);
1718 newIntent.setData(uriData);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001719 startActivity(newIntent);
1720 }
1721
Srikanth Chintala89aa6602014-03-14 16:26:57 +05301722 private void handlePlayTone(int slotId) {
1723 TextMessage toneMsg = mStkContext[slotId].mCurrentCmd.geTextMessage();
1724
1725 boolean showUser = true;
1726 boolean displayDialog = true;
1727 Resources resource = Resources.getSystem();
1728 try {
1729 displayDialog = !resource.getBoolean(
1730 com.android.internal.R.bool.config_stkNoAlphaUsrCnf);
1731 } catch (NotFoundException e) {
1732 displayDialog = true;
1733 }
1734
1735 // As per the spec 3GPP TS 11.14, 6.4.5. Play Tone.
1736 // If there is no alpha identifier tlv present, UE may show the
1737 // user information. 'config_stkNoAlphaUsrCnf' value will decide
1738 // whether to show it or not.
1739 // If alpha identifier tlv is present and its data is null, play only tone
1740 // without showing user any information.
1741 // Alpha Id is Present, but the text data is null.
1742 if ((toneMsg.text != null ) && (toneMsg.text.equals(""))) {
1743 CatLog.d(this, "Alpha identifier data is null, play only tone");
1744 showUser = false;
1745 }
1746 // Alpha Id is not present AND we need to show info to the user.
1747 if (toneMsg.text == null && displayDialog) {
1748 CatLog.d(this, "toneMsg.text " + toneMsg.text
1749 + " Starting ToneDialog activity with default message.");
1750 toneMsg.text = getResources().getString(R.string.default_tone_dialog_msg);
1751 showUser = true;
1752 }
1753 // Dont show user info, if config setting is true.
1754 if (toneMsg.text == null && !displayDialog) {
1755 CatLog.d(this, "config value stkNoAlphaUsrCnf is true");
1756 showUser = false;
1757 }
1758
1759 CatLog.d(this, "toneMsg.text: " + toneMsg.text + "showUser: " +showUser +
1760 "displayDialog: " +displayDialog);
1761 playTone(showUser, slotId);
1762 }
1763
1764 private void playTone(boolean showUserInfo, int slotId) {
1765 // Start playing tone and vibration
1766 ToneSettings settings = mStkContext[slotId].mCurrentCmd.getToneSettings();
1767 if (null == settings) {
1768 CatLog.d(this, "null settings, not playing tone.");
1769 return;
1770 }
1771
1772 mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
1773 mTonePlayer = new TonePlayer();
1774 mTonePlayer.play(settings.tone);
1775 int timeout = StkApp.calculateDurationInMilis(settings.duration);
1776 if (timeout == 0) {
1777 timeout = StkApp.TONE_DEFAULT_TIMEOUT;
1778 }
1779
1780 Message msg = mServiceHandler.obtainMessage();
1781 msg.arg1 = OP_STOP_TONE;
1782 msg.arg2 = slotId;
1783 msg.obj = (Integer)(showUserInfo ? 1 : 0);
1784 msg.what = STOP_TONE_WHAT;
1785 mServiceHandler.sendMessageDelayed(msg, timeout);
1786 if (settings.vibrate) {
1787 mVibrator.vibrate(timeout);
1788 }
1789
1790 // Start Tone dialog Activity to show user the information.
1791 if (showUserInfo) {
1792 Intent newIntent = new Intent(sInstance, ToneDialog.class);
1793 String uriString = STK_TONE_URI + slotId;
1794 Uri uriData = Uri.parse(uriString);
1795 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1796 | Intent.FLAG_ACTIVITY_NO_HISTORY
1797 | Intent.FLAG_ACTIVITY_SINGLE_TOP
1798 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
1799 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
1800 newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
1801 newIntent.putExtra(SLOT_ID, slotId);
1802 newIntent.setData(uriData);
1803 startActivity(newIntent);
1804 }
1805 }
1806
1807 private void finishToneDialogActivity() {
1808 Intent finishIntent = new Intent(FINISH_TONE_ACTIVITY_ACTION);
1809 sendBroadcast(finishIntent);
1810 }
1811
1812 private void handleStopTone(Message msg, int slotId) {
1813 int resId = 0;
1814
1815 // Stop the play tone in following cases:
1816 // 1.OP_STOP_TONE: play tone timer expires.
1817 // 2.STOP_TONE_USER: user pressed the back key.
1818 if (msg.arg1 == OP_STOP_TONE) {
1819 resId = RES_ID_DONE;
1820 // Dismiss Tone dialog, after finishing off playing the tone.
1821 int finishActivity = (Integer) msg.obj;
1822 if (finishActivity == 1) finishToneDialogActivity();
1823 } else if (msg.arg1 == OP_STOP_TONE_USER) {
1824 resId = RES_ID_END_SESSION;
1825 }
1826
1827 sendResponse(resId, slotId, true);
1828 mServiceHandler.removeMessages(STOP_TONE_WHAT);
1829 if (mTonePlayer != null) {
1830 mTonePlayer.stop();
1831 mTonePlayer.release();
1832 mTonePlayer = null;
1833 }
1834 if (mVibrator != null) {
1835 mVibrator.cancel();
1836 mVibrator = null;
1837 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001838 }
1839
Takanori Nakano49b12722016-02-16 14:34:14 +09001840 private void launchOpenChannelDialog(final int slotId) {
Wink Savillee68857d2014-10-17 15:23:05 -07001841 TextMessage msg = mStkContext[slotId].mCurrentCmd.geTextMessage();
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001842 if (msg == null) {
Wink Savillee68857d2014-10-17 15:23:05 -07001843 CatLog.d(LOG_TAG, "msg is null, return here");
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001844 return;
1845 }
1846
1847 msg.title = getResources().getString(R.string.stk_dialog_title);
1848 if (msg.text == null) {
1849 msg.text = getResources().getString(R.string.default_open_channel_msg);
1850 }
1851
1852 final AlertDialog dialog = new AlertDialog.Builder(mContext)
1853 .setIconAttribute(android.R.attr.alertDialogIcon)
1854 .setTitle(msg.title)
1855 .setMessage(msg.text)
1856 .setCancelable(false)
1857 .setPositiveButton(getResources().getString(R.string.stk_dialog_accept),
1858 new DialogInterface.OnClickListener() {
1859 public void onClick(DialogInterface dialog, int which) {
1860 Bundle args = new Bundle();
1861 args.putInt(RES_ID, RES_ID_CHOICE);
1862 args.putInt(CHOICE, YES);
1863 Message message = mServiceHandler.obtainMessage();
1864 message.arg1 = OP_RESPONSE;
Takanori Nakano49b12722016-02-16 14:34:14 +09001865 message.arg2 = slotId;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001866 message.obj = args;
1867 mServiceHandler.sendMessage(message);
1868 }
1869 })
1870 .setNegativeButton(getResources().getString(R.string.stk_dialog_reject),
1871 new DialogInterface.OnClickListener() {
1872 public void onClick(DialogInterface dialog, int which) {
1873 Bundle args = new Bundle();
1874 args.putInt(RES_ID, RES_ID_CHOICE);
1875 args.putInt(CHOICE, NO);
1876 Message message = mServiceHandler.obtainMessage();
1877 message.arg1 = OP_RESPONSE;
Takanori Nakano49b12722016-02-16 14:34:14 +09001878 message.arg2 = slotId;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001879 message.obj = args;
1880 mServiceHandler.sendMessage(message);
1881 }
1882 })
1883 .create();
1884
1885 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
1886 if (!mContext.getResources().getBoolean(
1887 com.android.internal.R.bool.config_sf_slowBlur)) {
1888 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1889 }
1890
1891 dialog.show();
1892 }
1893
Wink Savillee68857d2014-10-17 15:23:05 -07001894 private void launchTransientEventMessage(int slotId) {
1895 TextMessage msg = mStkContext[slotId].mCurrentCmd.geTextMessage();
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001896 if (msg == null) {
Wink Savillee68857d2014-10-17 15:23:05 -07001897 CatLog.d(LOG_TAG, "msg is null, return here");
Kazuhiro Ondo764167c2011-10-21 16:05:05 -05001898 return;
1899 }
1900
1901 msg.title = getResources().getString(R.string.stk_dialog_title);
1902
1903 final AlertDialog dialog = new AlertDialog.Builder(mContext)
1904 .setIconAttribute(android.R.attr.alertDialogIcon)
1905 .setTitle(msg.title)
1906 .setMessage(msg.text)
1907 .setCancelable(false)
1908 .setPositiveButton(getResources().getString(android.R.string.ok),
1909 new DialogInterface.OnClickListener() {
1910 public void onClick(DialogInterface dialog, int which) {
1911 }
1912 })
1913 .create();
1914
1915 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
1916 if (!mContext.getResources().getBoolean(
1917 com.android.internal.R.bool.config_sf_slowBlur)) {
1918 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1919 }
1920
1921 dialog.show();
1922 }
1923
Wink Savillee68857d2014-10-17 15:23:05 -07001924 private int getNotificationId(int slotId) {
1925 int notifyId = STK_NOTIFICATION_ID;
1926 if (slotId >= 0 && slotId < mSimCount) {
1927 notifyId += slotId;
1928 } else {
1929 CatLog.d(LOG_TAG, "invalid slotId: " + slotId);
1930 }
1931 CatLog.d(LOG_TAG, "getNotificationId, slotId: " + slotId + ", notifyId: " + notifyId);
1932 return notifyId;
1933 }
1934
1935 private String getItemName(int itemId, int slotId) {
1936 Menu menu = mStkContext[slotId].mCurrentCmd.getMenu();
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001937 if (menu == null) {
1938 return null;
1939 }
1940 for (Item item : menu.items) {
1941 if (item.id == itemId) {
1942 return item.text;
1943 }
1944 }
1945 return null;
1946 }
1947
Wink Savillee68857d2014-10-17 15:23:05 -07001948 private boolean removeMenu(int slotId) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001949 try {
Wink Savillee68857d2014-10-17 15:23:05 -07001950 if (mStkContext[slotId].mCurrentMenu.items.size() == 1 &&
1951 mStkContext[slotId].mCurrentMenu.items.get(0) == null) {
1952 mStkContext[slotId].mSetupMenuState = STATE_NOT_EXIST;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001953 return true;
1954 }
1955 } catch (NullPointerException e) {
Wink Savillee68857d2014-10-17 15:23:05 -07001956 CatLog.d(LOG_TAG, "Unable to get Menu's items size");
1957 mStkContext[slotId].mSetupMenuState = STATE_NOT_EXIST;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001958 return true;
1959 }
Wink Savillee68857d2014-10-17 15:23:05 -07001960 mStkContext[slotId].mSetupMenuState = STATE_EXIST;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001961 return false;
1962 }
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +05301963
Wink Savillee68857d2014-10-17 15:23:05 -07001964 StkContext getStkContext(int slotId) {
1965 if (slotId >= 0 && slotId < mSimCount) {
1966 return mStkContext[slotId];
1967 } else {
1968 CatLog.d(LOG_TAG, "invalid slotId: " + slotId);
1969 return null;
1970 }
1971 }
Preeti Ahuja0f4cf2f2012-08-09 11:45:08 +05301972
1973 private void handleAlphaNotify(Bundle args) {
1974 String alphaString = args.getString(AppInterface.ALPHA_STRING);
1975
1976 CatLog.d(this, "Alpha string received from card: " + alphaString);
1977 Toast toast = Toast.makeText(sInstance, alphaString, Toast.LENGTH_LONG);
1978 toast.setGravity(Gravity.TOP, 0, 0);
1979 toast.show();
1980 }
Srikanth Chintalaba103002015-11-30 10:49:52 -08001981
1982 private boolean isUrlAvailableToLaunchBrowser(BrowserSettings settings) {
1983 String url = SystemProperties.get(STK_BROWSER_DEFAULT_URL_SYSPROP, "");
1984 if (url == "" && settings.url == null) {
1985 return false;
1986 }
1987 return true;
1988 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -08001989}