blob: 82e0257f9734f708aebc7665c9bef2595dddb931 [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
Kazuhiro Ondo764167c2011-10-21 16:05:05 -050019import android.app.AlertDialog;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080020import android.app.Notification;
21import android.app.NotificationManager;
22import android.app.PendingIntent;
23import android.app.Service;
24import android.content.Context;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -050025import android.content.DialogInterface;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080026import android.content.Intent;
27import android.net.Uri;
28import android.os.Bundle;
29import android.os.Handler;
30import android.os.IBinder;
31import android.os.Looper;
32import android.os.Message;
Wink Saville56469d52009-04-02 01:37:03 -070033import android.telephony.TelephonyManager;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080034import android.view.Gravity;
35import android.view.LayoutInflater;
36import android.view.View;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -050037import android.view.Window;
38import android.view.WindowManager;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080039import android.widget.ImageView;
40import android.widget.RemoteViews;
41import android.widget.TextView;
42import android.widget.Toast;
43
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070044import com.android.internal.telephony.cat.AppInterface;
45import com.android.internal.telephony.cat.Menu;
46import com.android.internal.telephony.cat.Item;
Pierre Fröjd97503262010-11-08 13:59:36 +010047import com.android.internal.telephony.cat.Input;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070048import com.android.internal.telephony.cat.ResultCode;
49import com.android.internal.telephony.cat.CatCmdMessage;
50import com.android.internal.telephony.cat.CatCmdMessage.BrowserSettings;
51import com.android.internal.telephony.cat.CatLog;
52import com.android.internal.telephony.cat.CatResponseMessage;
53import com.android.internal.telephony.cat.TextMessage;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080054
55import java.util.LinkedList;
56
57/**
58 * SIM toolkit application level service. Interacts with Telephopny messages,
59 * application's launch and user input from STK UI elements.
Wink Saville79085fc2009-06-09 10:27:23 -070060 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080061 */
62public class StkAppService extends Service implements Runnable {
63
64 // members
65 private volatile Looper mServiceLooper;
66 private volatile ServiceHandler mServiceHandler;
67 private AppInterface mStkService;
68 private Context mContext = null;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -070069 private CatCmdMessage mMainCmd = null;
70 private CatCmdMessage mCurrentCmd = null;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080071 private Menu mCurrentMenu = null;
72 private String lastSelectedItem = null;
73 private boolean mMenuIsVisibile = false;
74 private boolean responseNeeded = true;
75 private boolean mCmdInProgress = false;
76 private NotificationManager mNotificationManager = null;
77 private LinkedList<DelayedCmd> mCmdsQ = null;
78 private boolean launchBrowser = false;
79 private BrowserSettings mBrowserSettings = null;
80 static StkAppService sInstance = null;
81
82 // Used for setting FLAG_ACTIVITY_NO_USER_ACTION when
83 // creating an intent.
84 private enum InitiatedByUserAction {
85 yes, // The action was started via a user initiated action
86 unknown, // Not known for sure if user initated the action
87 }
88
89 // constants
90 static final String OPCODE = "op";
91 static final String CMD_MSG = "cmd message";
92 static final String RES_ID = "response id";
93 static final String MENU_SELECTION = "menu selection";
94 static final String INPUT = "input";
95 static final String HELP = "help";
96 static final String CONFIRMATION = "confirm";
Kazuhiro Ondo764167c2011-10-21 16:05:05 -050097 static final String CHOICE = "choice";
The Android Open Source Project9d9730a2009-03-03 19:32:37 -080098
99 // operations ids for different service functionality.
100 static final int OP_CMD = 1;
101 static final int OP_RESPONSE = 2;
102 static final int OP_LAUNCH_APP = 3;
103 static final int OP_END_SESSION = 4;
104 static final int OP_BOOT_COMPLETED = 5;
105 private static final int OP_DELAYED_MSG = 6;
106
107 // Response ids
108 static final int RES_ID_MENU_SELECTION = 11;
109 static final int RES_ID_INPUT = 12;
110 static final int RES_ID_CONFIRM = 13;
111 static final int RES_ID_DONE = 14;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500112 static final int RES_ID_CHOICE = 15;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800113
114 static final int RES_ID_TIMEOUT = 20;
115 static final int RES_ID_BACKWARD = 21;
116 static final int RES_ID_END_SESSION = 22;
117 static final int RES_ID_EXIT = 23;
118
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500119 static final int YES = 1;
120 static final int NO = 0;
121
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800122 private static final String PACKAGE_NAME = "com.android.stk";
Wink Saville79085fc2009-06-09 10:27:23 -0700123 private static final String MENU_ACTIVITY_NAME =
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800124 PACKAGE_NAME + ".StkMenuActivity";
Wink Saville79085fc2009-06-09 10:27:23 -0700125 private static final String INPUT_ACTIVITY_NAME =
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800126 PACKAGE_NAME + ".StkInputActivity";
Wink Saville79085fc2009-06-09 10:27:23 -0700127
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800128 // Notification id used to display Idle Mode text in NotificationManager.
129 private static final int STK_NOTIFICATION_ID = 333;
Wink Saville79085fc2009-06-09 10:27:23 -0700130
131 // Inner class used for queuing telephony messages (proactive commands,
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800132 // session end) while the service is busy processing a previous message.
133 private class DelayedCmd {
134 // members
135 int id;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700136 CatCmdMessage msg;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800137
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700138 DelayedCmd(int id, CatCmdMessage msg) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800139 this.id = id;
140 this.msg = msg;
141 }
142 }
143
144 @Override
145 public void onCreate() {
146 // Initialize members
Alex Yakavenkad8e2ecd2012-04-20 17:10:15 -0700147 // This can return null if StkService is not yet instantiated, but it's ok
148 // If this is null we will do getInstance before we need to use this
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700149 mStkService = com.android.internal.telephony.cat.CatService
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800150 .getInstance();
Wink Saville36eddd52009-05-29 13:56:28 -0700151
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800152 mCmdsQ = new LinkedList<DelayedCmd>();
153 Thread serviceThread = new Thread(null, this, "Stk App Service");
154 serviceThread.start();
155 mContext = getBaseContext();
156 mNotificationManager = (NotificationManager) mContext
157 .getSystemService(Context.NOTIFICATION_SERVICE);
158 sInstance = this;
159 }
160
161 @Override
162 public void onStart(Intent intent, int startId) {
163 waitForLooper();
164
John Wang62acae42009-10-08 11:20:23 -0700165 // onStart() method can be passed a null intent
166 // TODO: replace onStart() with onStartCommand()
167 if (intent == null) {
168 return;
169 }
170
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800171 Bundle args = intent.getExtras();
John Wang62acae42009-10-08 11:20:23 -0700172
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800173 if (args == null) {
174 return;
175 }
176
177 Message msg = mServiceHandler.obtainMessage();
178 msg.arg1 = args.getInt(OPCODE);
179 switch(msg.arg1) {
180 case OP_CMD:
181 msg.obj = args.getParcelable(CMD_MSG);
182 break;
183 case OP_RESPONSE:
184 msg.obj = args;
185 /* falls through */
186 case OP_LAUNCH_APP:
187 case OP_END_SESSION:
188 case OP_BOOT_COMPLETED:
189 break;
190 default:
191 return;
192 }
193 mServiceHandler.sendMessage(msg);
194 }
195
196 @Override
197 public void onDestroy() {
198 waitForLooper();
199 mServiceLooper.quit();
200 }
201
202 @Override
203 public IBinder onBind(Intent intent) {
204 return null;
205 }
206
207 public void run() {
208 Looper.prepare();
209
210 mServiceLooper = Looper.myLooper();
211 mServiceHandler = new ServiceHandler();
212
213 Looper.loop();
214 }
215
216 /*
217 * Package api used by StkMenuActivity to indicate if its on the foreground.
218 */
219 void indicateMenuVisibility(boolean visibility) {
220 mMenuIsVisibile = visibility;
221 }
222
223 /*
224 * Package api used by StkMenuActivity to get its Menu parameter.
225 */
226 Menu getMenu() {
227 return mCurrentMenu;
228 }
229
230 /*
231 * Package api used by UI Activities and Dialogs to communicate directly
232 * with the service to deliver state information and parameters.
233 */
234 static StkAppService getInstance() {
235 return sInstance;
236 }
237
238 private void waitForLooper() {
239 while (mServiceHandler == null) {
240 synchronized (this) {
241 try {
242 wait(100);
243 } catch (InterruptedException e) {
244 }
245 }
246 }
247 }
248
249 private final class ServiceHandler extends Handler {
250 @Override
251 public void handleMessage(Message msg) {
252 int opcode = msg.arg1;
253
254 switch (opcode) {
255 case OP_LAUNCH_APP:
256 if (mMainCmd == null) {
257 // nothing todo when no SET UP MENU command didn't arrive.
258 return;
259 }
260 launchMenuActivity(null);
261 break;
262 case OP_CMD:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700263 CatCmdMessage cmdMsg = (CatCmdMessage) msg.obj;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800264 // There are two types of commands:
265 // 1. Interactive - user's response is required.
266 // 2. Informative - display a message, no interaction with the user.
267 //
Wink Saville79085fc2009-06-09 10:27:23 -0700268 // Informative commands can be handled immediately without any delay.
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800269 // Interactive commands can't override each other. So if a command
270 // is already in progress, we need to queue the next command until
271 // the user has responded or a timeout expired.
272 if (!isCmdInteractive(cmdMsg)) {
273 handleCmd(cmdMsg);
274 } else {
275 if (!mCmdInProgress) {
276 mCmdInProgress = true;
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700277 handleCmd((CatCmdMessage) msg.obj);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800278 } else {
279 mCmdsQ.addLast(new DelayedCmd(OP_CMD,
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700280 (CatCmdMessage) msg.obj));
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800281 }
282 }
283 break;
284 case OP_RESPONSE:
285 if (responseNeeded) {
286 handleCmdResponse((Bundle) msg.obj);
287 }
288 // call delayed commands if needed.
289 if (mCmdsQ.size() != 0) {
290 callDelayedMsg();
291 } else {
292 mCmdInProgress = false;
293 }
294 // reset response needed state var to its original value.
295 responseNeeded = true;
296 break;
297 case OP_END_SESSION:
298 if (!mCmdInProgress) {
299 mCmdInProgress = true;
300 handleSessionEnd();
301 } else {
302 mCmdsQ.addLast(new DelayedCmd(OP_END_SESSION, null));
303 }
304 break;
305 case OP_BOOT_COMPLETED:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700306 CatLog.d(this, "OP_BOOT_COMPLETED");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800307 if (mMainCmd == null) {
308 StkAppInstaller.unInstall(mContext);
309 }
310 break;
311 case OP_DELAYED_MSG:
312 handleDelayedCmd();
313 break;
314 }
315 }
316 }
317
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700318 private boolean isCmdInteractive(CatCmdMessage cmd) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800319 switch (cmd.getCmdType()) {
320 case SEND_DTMF:
321 case SEND_SMS:
322 case SEND_SS:
323 case SEND_USSD:
324 case SET_UP_IDLE_MODE_TEXT:
325 case SET_UP_MENU:
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500326 case CLOSE_CHANNEL:
327 case RECEIVE_DATA:
328 case SEND_DATA:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800329 return false;
330 }
331
332 return true;
333 }
334
335 private void handleDelayedCmd() {
336 if (mCmdsQ.size() != 0) {
337 DelayedCmd cmd = mCmdsQ.poll();
338 switch (cmd.id) {
339 case OP_CMD:
340 handleCmd(cmd.msg);
341 break;
342 case OP_END_SESSION:
343 handleSessionEnd();
344 break;
345 }
346 }
347 }
348
349 private void callDelayedMsg() {
350 Message msg = mServiceHandler.obtainMessage();
351 msg.arg1 = OP_DELAYED_MSG;
352 mServiceHandler.sendMessage(msg);
353 }
354
355 private void handleSessionEnd() {
356 mCurrentCmd = mMainCmd;
357 lastSelectedItem = null;
Wink Saville79085fc2009-06-09 10:27:23 -0700358 // In case of SET UP MENU command which removed the app, don't
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800359 // update the current menu member.
360 if (mCurrentMenu != null && mMainCmd != null) {
361 mCurrentMenu = mMainCmd.getMenu();
362 }
363 if (mMenuIsVisibile) {
364 launchMenuActivity(null);
365 }
366 if (mCmdsQ.size() != 0) {
367 callDelayedMsg();
368 } else {
369 mCmdInProgress = false;
370 }
371 // In case a launch browser command was just confirmed, launch that url.
372 if (launchBrowser) {
373 launchBrowser = false;
374 launchBrowser(mBrowserSettings);
375 }
376 }
377
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700378 private void handleCmd(CatCmdMessage cmdMsg) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800379 if (cmdMsg == null) {
380 return;
381 }
382 // save local reference for state tracking.
383 mCurrentCmd = cmdMsg;
384 boolean waitForUsersResponse = true;
385
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700386 CatLog.d(this, cmdMsg.getCmdType().name());
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800387 switch (cmdMsg.getCmdType()) {
388 case DISPLAY_TEXT:
389 TextMessage msg = cmdMsg.geTextMessage();
390 responseNeeded = msg.responseNeeded;
391 if (lastSelectedItem != null) {
392 msg.title = lastSelectedItem;
393 } else if (mMainCmd != null){
394 msg.title = mMainCmd.getMenu().title;
395 } else {
396 // TODO: get the carrier name from the SIM
397 msg.title = "";
398 }
399 launchTextDialog();
400 break;
401 case SELECT_ITEM:
402 mCurrentMenu = cmdMsg.getMenu();
403 launchMenuActivity(cmdMsg.getMenu());
404 break;
405 case SET_UP_MENU:
406 mMainCmd = mCurrentCmd;
407 mCurrentMenu = cmdMsg.getMenu();
408 if (removeMenu()) {
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700409 CatLog.d(this, "Uninstall App");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800410 mCurrentMenu = null;
411 StkAppInstaller.unInstall(mContext);
412 } else {
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700413 CatLog.d(this, "Install App");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800414 StkAppInstaller.install(mContext);
415 }
416 if (mMenuIsVisibile) {
417 launchMenuActivity(null);
418 }
419 break;
420 case GET_INPUT:
421 case GET_INKEY:
422 launchInputActivity();
423 break;
424 case SET_UP_IDLE_MODE_TEXT:
425 waitForUsersResponse = false;
426 launchIdleText();
427 break;
428 case SEND_DTMF:
429 case SEND_SMS:
430 case SEND_SS:
431 case SEND_USSD:
432 waitForUsersResponse = false;
433 launchEventMessage();
434 break;
435 case LAUNCH_BROWSER:
436 launchConfirmationDialog(mCurrentCmd.geTextMessage());
437 break;
438 case SET_UP_CALL:
439 launchConfirmationDialog(mCurrentCmd.getCallSettings().confirmMsg);
440 break;
441 case PLAY_TONE:
442 launchToneDialog();
443 break;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500444 case OPEN_CHANNEL:
445 launchOpenChannelDialog();
446 break;
447 case CLOSE_CHANNEL:
448 case RECEIVE_DATA:
449 case SEND_DATA:
450 TextMessage m = mCurrentCmd.geTextMessage();
451
452 if ((m != null) && (m.text == null)) {
453 switch(cmdMsg.getCmdType()) {
454 case CLOSE_CHANNEL:
455 m.text = getResources().getString(R.string.default_close_channel_msg);
456 break;
457 case RECEIVE_DATA:
458 m.text = getResources().getString(R.string.default_receive_data_msg);
459 break;
460 case SEND_DATA:
461 m.text = getResources().getString(R.string.default_send_data_msg);
462 break;
463 }
464 }
465 launchTransientEventMessage();
466 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800467 }
468
469 if (!waitForUsersResponse) {
470 if (mCmdsQ.size() != 0) {
471 callDelayedMsg();
472 } else {
473 mCmdInProgress = false;
474 }
475 }
476 }
477
478 private void handleCmdResponse(Bundle args) {
479 if (mCurrentCmd == null) {
480 return;
481 }
Alex Yakavenkad8e2ecd2012-04-20 17:10:15 -0700482 if (mStkService == null) {
483 mStkService = com.android.internal.telephony.cat.CatService.getInstance();
484 if (mStkService == null) {
485 // This should never happen (we should be responding only to a message
486 // that arrived from StkService). It has to exist by this time
487 throw new RuntimeException("mStkService is null when we need to send response");
488 }
489 }
490
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700491 CatResponseMessage resMsg = new CatResponseMessage(mCurrentCmd);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800492
493 // set result code
494 boolean helpRequired = args.getBoolean(HELP, false);
495
496 switch(args.getInt(RES_ID)) {
497 case RES_ID_MENU_SELECTION:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700498 CatLog.d(this, "RES_ID_MENU_SELECTION");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800499 int menuSelection = args.getInt(MENU_SELECTION);
500 switch(mCurrentCmd.getCmdType()) {
501 case SET_UP_MENU:
502 case SELECT_ITEM:
503 lastSelectedItem = getItemName(menuSelection);
504 if (helpRequired) {
505 resMsg.setResultCode(ResultCode.HELP_INFO_REQUIRED);
506 } else {
507 resMsg.setResultCode(ResultCode.OK);
508 }
509 resMsg.setMenuSelection(menuSelection);
510 break;
511 }
512 break;
513 case RES_ID_INPUT:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700514 CatLog.d(this, "RES_ID_INPUT");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800515 String input = args.getString(INPUT);
Pierre Fröjd97503262010-11-08 13:59:36 +0100516 Input cmdInput = mCurrentCmd.geInput();
517 if (cmdInput != null && cmdInput.yesNo) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800518 boolean yesNoSelection = input
519 .equals(StkInputActivity.YES_STR_RESPONSE);
520 resMsg.setYesNo(yesNoSelection);
521 } else {
522 if (helpRequired) {
523 resMsg.setResultCode(ResultCode.HELP_INFO_REQUIRED);
524 } else {
525 resMsg.setResultCode(ResultCode.OK);
526 resMsg.setInput(input);
527 }
528 }
529 break;
530 case RES_ID_CONFIRM:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700531 CatLog.d(this, "RES_ID_CONFIRM");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800532 boolean confirmed = args.getBoolean(CONFIRMATION);
533 switch (mCurrentCmd.getCmdType()) {
534 case DISPLAY_TEXT:
535 resMsg.setResultCode(confirmed ? ResultCode.OK
536 : ResultCode.UICC_SESSION_TERM_BY_USER);
537 break;
538 case LAUNCH_BROWSER:
539 resMsg.setResultCode(confirmed ? ResultCode.OK
540 : ResultCode.UICC_SESSION_TERM_BY_USER);
541 if (confirmed) {
542 launchBrowser = true;
543 mBrowserSettings = mCurrentCmd.getBrowserSettings();
544 }
545 break;
546 case SET_UP_CALL:
547 resMsg.setResultCode(ResultCode.OK);
548 resMsg.setConfirmation(confirmed);
549 if (confirmed) {
Johan Hellman3aec01c2011-02-10 10:15:28 +0100550 launchEventMessage(mCurrentCmd.getCallSettings().callMsg);
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800551 }
552 break;
553 }
554 break;
555 case RES_ID_DONE:
556 resMsg.setResultCode(ResultCode.OK);
557 break;
558 case RES_ID_BACKWARD:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700559 CatLog.d(this, "RES_ID_BACKWARD");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800560 resMsg.setResultCode(ResultCode.BACKWARD_MOVE_BY_USER);
561 break;
562 case RES_ID_END_SESSION:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700563 CatLog.d(this, "RES_ID_END_SESSION");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800564 resMsg.setResultCode(ResultCode.UICC_SESSION_TERM_BY_USER);
565 break;
566 case RES_ID_TIMEOUT:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700567 CatLog.d(this, "RES_ID_TIMEOUT");
Naveen Kallad5176892009-11-30 12:45:29 -0800568 // GCF test-case 27.22.4.1.1 Expected Sequence 1.5 (DISPLAY TEXT,
569 // Clear message after delay, successful) expects result code OK.
570 // If the command qualifier specifies no user response is required
571 // then send OK instead of NO_RESPONSE_FROM_USER
572 if ((mCurrentCmd.getCmdType().value() == AppInterface.CommandType.DISPLAY_TEXT
573 .value())
574 && (mCurrentCmd.geTextMessage().userClear == false)) {
575 resMsg.setResultCode(ResultCode.OK);
576 } else {
577 resMsg.setResultCode(ResultCode.NO_RESPONSE_FROM_USER);
578 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800579 break;
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500580 case RES_ID_CHOICE:
581 int choice = args.getInt(CHOICE);
582 CatLog.d(this, "User Choice=" + choice);
583 switch (choice) {
584 case YES:
585 resMsg.setResultCode(ResultCode.OK);
586 break;
587 case NO:
588 resMsg.setResultCode(ResultCode.USER_NOT_ACCEPT);
589 break;
590 }
591 break;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800592 default:
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700593 CatLog.d(this, "Unknown result id");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800594 return;
595 }
596 mStkService.onCmdResponse(resMsg);
597 }
598
599 /**
600 * Returns 0 or FLAG_ACTIVITY_NO_USER_ACTION, 0 means the user initiated the action.
601 *
602 * @param userAction If the userAction is yes then we always return 0 otherwise
603 * mMenuIsVisible is used to determine what to return. If mMenuIsVisible is true
604 * then we are the foreground app and we'll return 0 as from our perspective a
605 * user action did cause. If it's false than we aren't the foreground app and
606 * FLAG_ACTIVITY_NO_USER_ACTION is returned.
Wink Saville79085fc2009-06-09 10:27:23 -0700607 *
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800608 * @return 0 or FLAG_ACTIVITY_NO_USER_ACTION
609 */
610 private int getFlagActivityNoUserAction(InitiatedByUserAction userAction) {
611 return ((userAction == InitiatedByUserAction.yes) | mMenuIsVisibile) ?
612 0 : Intent.FLAG_ACTIVITY_NO_USER_ACTION;
613 }
614
615 private void launchMenuActivity(Menu menu) {
616 Intent newIntent = new Intent(Intent.ACTION_VIEW);
617 newIntent.setClassName(PACKAGE_NAME, MENU_ACTIVITY_NAME);
618 int intentFlags = Intent.FLAG_ACTIVITY_NEW_TASK
619 | Intent.FLAG_ACTIVITY_CLEAR_TOP;
620 if (menu == null) {
621 // We assume this was initiated by the user pressing the tool kit icon
622 intentFlags |= getFlagActivityNoUserAction(InitiatedByUserAction.yes);
623
624 newIntent.putExtra("STATE", StkMenuActivity.STATE_MAIN);
625 } else {
626 // We don't know and we'll let getFlagActivityNoUserAction decide.
627 intentFlags |= getFlagActivityNoUserAction(InitiatedByUserAction.unknown);
628
629 newIntent.putExtra("STATE", StkMenuActivity.STATE_SECONDARY);
630 }
631 newIntent.setFlags(intentFlags);
632 mContext.startActivity(newIntent);
633 }
634
635 private void launchInputActivity() {
636 Intent newIntent = new Intent(Intent.ACTION_VIEW);
637 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
638 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown));
639 newIntent.setClassName(PACKAGE_NAME, INPUT_ACTIVITY_NAME);
640 newIntent.putExtra("INPUT", mCurrentCmd.geInput());
641 mContext.startActivity(newIntent);
642 }
643
644 private void launchTextDialog() {
645 Intent newIntent = new Intent(this, StkDialogActivity.class);
646 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
647 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK
648 | Intent.FLAG_ACTIVITY_NO_HISTORY
649 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
650 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown));
651 newIntent.putExtra("TEXT", mCurrentCmd.geTextMessage());
652 startActivity(newIntent);
653 }
654
655 private void launchEventMessage() {
Johan Hellman3aec01c2011-02-10 10:15:28 +0100656 launchEventMessage(mCurrentCmd.geTextMessage());
657 }
658
659 private void launchEventMessage(TextMessage msg) {
John Josephb22e7d82009-12-15 14:53:17 -0800660 if (msg == null || msg.text == null) {
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800661 return;
662 }
663 Toast toast = new Toast(mContext.getApplicationContext());
664 LayoutInflater inflate = (LayoutInflater) mContext
665 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
666 View v = inflate.inflate(R.layout.stk_event_msg, null);
667 TextView tv = (TextView) v
668 .findViewById(com.android.internal.R.id.message);
669 ImageView iv = (ImageView) v
670 .findViewById(com.android.internal.R.id.icon);
671 if (msg.icon != null) {
672 iv.setImageBitmap(msg.icon);
673 } else {
674 iv.setVisibility(View.GONE);
675 }
676 if (!msg.iconSelfExplanatory) {
677 tv.setText(msg.text);
678 }
679
680 toast.setView(v);
681 toast.setDuration(Toast.LENGTH_LONG);
682 toast.setGravity(Gravity.BOTTOM, 0, 0);
683 toast.show();
684 }
685
686 private void launchConfirmationDialog(TextMessage msg) {
687 msg.title = lastSelectedItem;
688 Intent newIntent = new Intent(this, StkDialogActivity.class);
689 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
690 | Intent.FLAG_ACTIVITY_NO_HISTORY
691 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
692 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown));
693 newIntent.putExtra("TEXT", msg);
694 startActivity(newIntent);
695 }
696
697 private void launchBrowser(BrowserSettings settings) {
698 if (settings == null) {
699 return;
700 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800701
David Brown7c03cfe2011-10-20 15:36:12 -0700702 Intent intent = new Intent(Intent.ACTION_VIEW);
703
704 Uri data;
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800705 if (settings.url != null) {
dujin.cha486c1d02011-11-02 22:14:25 +0900706 CatLog.d(this, "settings.url = " + settings.url);
dujin.cha2a0eb2a2011-11-11 15:03:57 +0900707 if ((settings.url.startsWith("http://") || (settings.url.startsWith("https://")))) {
dujin.cha486c1d02011-11-02 22:14:25 +0900708 data = Uri.parse(settings.url);
709 } else {
710 String modifiedUrl = "http://" + settings.url;
711 CatLog.d(this, "modifiedUrl = " + modifiedUrl);
712 data = Uri.parse(modifiedUrl);
713 }
David Brown7c03cfe2011-10-20 15:36:12 -0700714 } else {
715 // If no URL specified, just bring up the "home page".
716 //
717 // (Note we need to specify *something* in the intent's data field
718 // here, since if you fire off a VIEW intent with no data at all
719 // you'll get an activity chooser rather than the browser. There's
720 // no specific URI that means "use the default home page", so
721 // instead let's just explicitly bring up http://google.com.)
722 data = Uri.parse("http://google.com/");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800723 }
724 intent.setData(data);
David Brown7c03cfe2011-10-20 15:36:12 -0700725
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800726 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
727 switch (settings.mode) {
728 case USE_EXISTING_BROWSER:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800729 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
730 break;
731 case LAUNCH_NEW_BROWSER:
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800732 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
733 break;
734 case LAUNCH_IF_NOT_ALREADY_LAUNCHED:
735 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
736 break;
737 }
738 // start browser activity
739 startActivity(intent);
740 // a small delay, let the browser start, before processing the next command.
Wink Saville79085fc2009-06-09 10:27:23 -0700741 // this is good for scenarios where a related DISPLAY TEXT command is
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800742 // followed immediately.
743 try {
744 Thread.sleep(10000);
745 } catch (InterruptedException e) {}
746 }
747
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800748 private void launchIdleText() {
749 TextMessage msg = mCurrentCmd.geTextMessage();
dujin.cha2a0eb2a2011-11-11 15:03:57 +0900750
Wink Saville046db4b2011-11-01 20:42:54 -0700751 if (msg == null) {
dujin.cha2a0eb2a2011-11-11 15:03:57 +0900752 CatLog.d(this, "mCurrent.getTextMessage is NULL");
753 mNotificationManager.cancel(STK_NOTIFICATION_ID);
Wink Saville046db4b2011-11-01 20:42:54 -0700754 return;
755 }
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800756 if (msg.text == null) {
757 mNotificationManager.cancel(STK_NOTIFICATION_ID);
758 } else {
759 Notification notification = new Notification();
760 RemoteViews contentView = new RemoteViews(
761 PACKAGE_NAME,
762 com.android.internal.R.layout.status_bar_latest_event_content);
763
764 notification.flags |= Notification.FLAG_NO_CLEAR;
765 notification.icon = com.android.internal.R.drawable.stat_notify_sim_toolkit;
766 // Set text and icon for the status bar and notification body.
767 if (!msg.iconSelfExplanatory) {
768 notification.tickerText = msg.text;
769 contentView.setTextViewText(com.android.internal.R.id.text,
770 msg.text);
771 }
772 if (msg.icon != null) {
773 contentView.setImageViewBitmap(com.android.internal.R.id.icon,
774 msg.icon);
775 } else {
776 contentView
777 .setImageViewResource(
778 com.android.internal.R.id.icon,
779 com.android.internal.R.drawable.stat_notify_sim_toolkit);
780 }
781 notification.contentView = contentView;
782 notification.contentIntent = PendingIntent.getService(mContext, 0,
783 new Intent(mContext, StkAppService.class), 0);
784
785 mNotificationManager.notify(STK_NOTIFICATION_ID, notification);
786 }
787 }
788
789 private void launchToneDialog() {
790 Intent newIntent = new Intent(this, ToneDialog.class);
791 newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
792 | Intent.FLAG_ACTIVITY_NO_HISTORY
793 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
794 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown));
795 newIntent.putExtra("TEXT", mCurrentCmd.geTextMessage());
796 newIntent.putExtra("TONE", mCurrentCmd.getToneSettings());
797 startActivity(newIntent);
798 }
799
Kazuhiro Ondo764167c2011-10-21 16:05:05 -0500800 private void launchOpenChannelDialog() {
801 TextMessage msg = mCurrentCmd.geTextMessage();
802 if (msg == null) {
803 CatLog.d(this, "msg is null, return here");
804 return;
805 }
806
807 msg.title = getResources().getString(R.string.stk_dialog_title);
808 if (msg.text == null) {
809 msg.text = getResources().getString(R.string.default_open_channel_msg);
810 }
811
812 final AlertDialog dialog = new AlertDialog.Builder(mContext)
813 .setIconAttribute(android.R.attr.alertDialogIcon)
814 .setTitle(msg.title)
815 .setMessage(msg.text)
816 .setCancelable(false)
817 .setPositiveButton(getResources().getString(R.string.stk_dialog_accept),
818 new DialogInterface.OnClickListener() {
819 public void onClick(DialogInterface dialog, int which) {
820 Bundle args = new Bundle();
821 args.putInt(RES_ID, RES_ID_CHOICE);
822 args.putInt(CHOICE, YES);
823 Message message = mServiceHandler.obtainMessage();
824 message.arg1 = OP_RESPONSE;
825 message.obj = args;
826 mServiceHandler.sendMessage(message);
827 }
828 })
829 .setNegativeButton(getResources().getString(R.string.stk_dialog_reject),
830 new DialogInterface.OnClickListener() {
831 public void onClick(DialogInterface dialog, int which) {
832 Bundle args = new Bundle();
833 args.putInt(RES_ID, RES_ID_CHOICE);
834 args.putInt(CHOICE, NO);
835 Message message = mServiceHandler.obtainMessage();
836 message.arg1 = OP_RESPONSE;
837 message.obj = args;
838 mServiceHandler.sendMessage(message);
839 }
840 })
841 .create();
842
843 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
844 if (!mContext.getResources().getBoolean(
845 com.android.internal.R.bool.config_sf_slowBlur)) {
846 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
847 }
848
849 dialog.show();
850 }
851
852 private void launchTransientEventMessage() {
853 TextMessage msg = mCurrentCmd.geTextMessage();
854 if (msg == null) {
855 CatLog.d(this, "msg is null, return here");
856 return;
857 }
858
859 msg.title = getResources().getString(R.string.stk_dialog_title);
860
861 final AlertDialog dialog = new AlertDialog.Builder(mContext)
862 .setIconAttribute(android.R.attr.alertDialogIcon)
863 .setTitle(msg.title)
864 .setMessage(msg.text)
865 .setCancelable(false)
866 .setPositiveButton(getResources().getString(android.R.string.ok),
867 new DialogInterface.OnClickListener() {
868 public void onClick(DialogInterface dialog, int which) {
869 }
870 })
871 .create();
872
873 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
874 if (!mContext.getResources().getBoolean(
875 com.android.internal.R.bool.config_sf_slowBlur)) {
876 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
877 }
878
879 dialog.show();
880 }
881
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800882 private String getItemName(int itemId) {
883 Menu menu = mCurrentCmd.getMenu();
884 if (menu == null) {
885 return null;
886 }
887 for (Item item : menu.items) {
888 if (item.id == itemId) {
889 return item.text;
890 }
891 }
892 return null;
893 }
894
895 private boolean removeMenu() {
896 try {
Wink Saville79085fc2009-06-09 10:27:23 -0700897 if (mCurrentMenu.items.size() == 1 &&
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800898 mCurrentMenu.items.get(0) == null) {
899 return true;
900 }
901 } catch (NullPointerException e) {
Alex Yakavenkad41f1d92010-07-12 14:13:13 -0700902 CatLog.d(this, "Unable to get Menu's items size");
The Android Open Source Project9d9730a2009-03-03 19:32:37 -0800903 return true;
904 }
905 return false;
906 }
907}