Home | History | Annotate | Download | only in stk
      1 /*
      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 
     17 package com.android.stk;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Notification;
     21 import android.app.NotificationManager;
     22 import android.app.PendingIntent;
     23 import android.app.Service;
     24 import android.content.Context;
     25 import android.content.DialogInterface;
     26 import android.content.Intent;
     27 import android.net.Uri;
     28 import android.os.Bundle;
     29 import android.os.Handler;
     30 import android.os.IBinder;
     31 import android.os.Looper;
     32 import android.os.Message;
     33 import android.telephony.TelephonyManager;
     34 import android.view.Gravity;
     35 import android.view.LayoutInflater;
     36 import android.view.View;
     37 import android.view.Window;
     38 import android.view.WindowManager;
     39 import android.widget.ImageView;
     40 import android.widget.RemoteViews;
     41 import android.widget.TextView;
     42 import android.widget.Toast;
     43 
     44 import com.android.internal.telephony.cat.AppInterface;
     45 import com.android.internal.telephony.cat.Menu;
     46 import com.android.internal.telephony.cat.Item;
     47 import com.android.internal.telephony.cat.Input;
     48 import com.android.internal.telephony.cat.ResultCode;
     49 import com.android.internal.telephony.cat.CatCmdMessage;
     50 import com.android.internal.telephony.cat.CatCmdMessage.BrowserSettings;
     51 import com.android.internal.telephony.cat.CatLog;
     52 import com.android.internal.telephony.cat.CatResponseMessage;
     53 import com.android.internal.telephony.cat.TextMessage;
     54 
     55 import 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.
     60  *
     61  */
     62 public 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;
     69     private CatCmdMessage mMainCmd = null;
     70     private CatCmdMessage mCurrentCmd = null;
     71     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";
     97     static final String CHOICE = "choice";
     98 
     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;
    112     static final int RES_ID_CHOICE = 15;
    113 
    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 
    119     static final int YES = 1;
    120     static final int NO = 0;
    121 
    122     private static final String PACKAGE_NAME = "com.android.stk";
    123     private static final String MENU_ACTIVITY_NAME =
    124                                         PACKAGE_NAME + ".StkMenuActivity";
    125     private static final String INPUT_ACTIVITY_NAME =
    126                                         PACKAGE_NAME + ".StkInputActivity";
    127 
    128     // Notification id used to display Idle Mode text in NotificationManager.
    129     private static final int STK_NOTIFICATION_ID = 333;
    130 
    131     // Inner class used for queuing telephony messages (proactive commands,
    132     // session end) while the service is busy processing a previous message.
    133     private class DelayedCmd {
    134         // members
    135         int id;
    136         CatCmdMessage msg;
    137 
    138         DelayedCmd(int id, CatCmdMessage msg) {
    139             this.id = id;
    140             this.msg = msg;
    141         }
    142     }
    143 
    144     @Override
    145     public void onCreate() {
    146         // Initialize members
    147         // 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
    149         mStkService = com.android.internal.telephony.cat.CatService
    150                 .getInstance();
    151 
    152         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 
    165         // onStart() method can be passed a null intent
    166         // TODO: replace onStart() with onStartCommand()
    167         if (intent == null) {
    168             return;
    169         }
    170 
    171         Bundle args = intent.getExtras();
    172 
    173         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:
    263                 CatCmdMessage cmdMsg = (CatCmdMessage) msg.obj;
    264                 // 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                 //
    268                 // Informative commands can be handled immediately without any delay.
    269                 // 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;
    277                         handleCmd((CatCmdMessage) msg.obj);
    278                     } else {
    279                         mCmdsQ.addLast(new DelayedCmd(OP_CMD,
    280                                 (CatCmdMessage) msg.obj));
    281                     }
    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:
    306                 CatLog.d(this, "OP_BOOT_COMPLETED");
    307                 if (mMainCmd == null) {
    308                     StkAppInstaller.unInstall(mContext);
    309                 }
    310                 break;
    311             case OP_DELAYED_MSG:
    312                 handleDelayedCmd();
    313                 break;
    314             }
    315         }
    316     }
    317 
    318     private boolean isCmdInteractive(CatCmdMessage cmd) {
    319         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:
    326         case CLOSE_CHANNEL:
    327         case RECEIVE_DATA:
    328         case SEND_DATA:
    329             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;
    358         // In case of SET UP MENU command which removed the app, don't
    359         // 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 
    378     private void handleCmd(CatCmdMessage cmdMsg) {
    379         if (cmdMsg == null) {
    380             return;
    381         }
    382         // save local reference for state tracking.
    383         mCurrentCmd = cmdMsg;
    384         boolean waitForUsersResponse = true;
    385 
    386         CatLog.d(this, cmdMsg.getCmdType().name());
    387         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()) {
    409                 CatLog.d(this, "Uninstall App");
    410                 mCurrentMenu = null;
    411                 StkAppInstaller.unInstall(mContext);
    412             } else {
    413                 CatLog.d(this, "Install App");
    414                 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;
    444         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;
    467         }
    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         }
    482         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 
    491         CatResponseMessage resMsg = new CatResponseMessage(mCurrentCmd);
    492 
    493         // set result code
    494         boolean helpRequired = args.getBoolean(HELP, false);
    495 
    496         switch(args.getInt(RES_ID)) {
    497         case RES_ID_MENU_SELECTION:
    498             CatLog.d(this, "RES_ID_MENU_SELECTION");
    499             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:
    514             CatLog.d(this, "RES_ID_INPUT");
    515             String input = args.getString(INPUT);
    516             Input cmdInput = mCurrentCmd.geInput();
    517             if (cmdInput != null && cmdInput.yesNo) {
    518                 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:
    531             CatLog.d(this, "RES_ID_CONFIRM");
    532             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) {
    550                     launchCallMsg();
    551                 }
    552                 break;
    553             }
    554             break;
    555         case RES_ID_DONE:
    556             resMsg.setResultCode(ResultCode.OK);
    557             break;
    558         case RES_ID_BACKWARD:
    559             CatLog.d(this, "RES_ID_BACKWARD");
    560             resMsg.setResultCode(ResultCode.BACKWARD_MOVE_BY_USER);
    561             break;
    562         case RES_ID_END_SESSION:
    563             CatLog.d(this, "RES_ID_END_SESSION");
    564             resMsg.setResultCode(ResultCode.UICC_SESSION_TERM_BY_USER);
    565             break;
    566         case RES_ID_TIMEOUT:
    567             CatLog.d(this, "RES_ID_TIMEOUT");
    568             // 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             }
    579             break;
    580         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;
    592         default:
    593             CatLog.d(this, "Unknown result id");
    594             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.
    607      *
    608      * @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() {
    656         TextMessage msg = mCurrentCmd.geTextMessage();
    657         if (msg == null || msg.text == null) {
    658             return;
    659         }
    660         Toast toast = new Toast(mContext.getApplicationContext());
    661         LayoutInflater inflate = (LayoutInflater) mContext
    662                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    663         View v = inflate.inflate(R.layout.stk_event_msg, null);
    664         TextView tv = (TextView) v
    665                 .findViewById(com.android.internal.R.id.message);
    666         ImageView iv = (ImageView) v
    667                 .findViewById(com.android.internal.R.id.icon);
    668         if (msg.icon != null) {
    669             iv.setImageBitmap(msg.icon);
    670         } else {
    671             iv.setVisibility(View.GONE);
    672         }
    673         if (!msg.iconSelfExplanatory) {
    674             tv.setText(msg.text);
    675         }
    676 
    677         toast.setView(v);
    678         toast.setDuration(Toast.LENGTH_LONG);
    679         toast.setGravity(Gravity.BOTTOM, 0, 0);
    680         toast.show();
    681     }
    682 
    683     private void launchConfirmationDialog(TextMessage msg) {
    684         msg.title = lastSelectedItem;
    685         Intent newIntent = new Intent(this, StkDialogActivity.class);
    686         newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
    687                 | Intent.FLAG_ACTIVITY_NO_HISTORY
    688                 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    689                 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown));
    690         newIntent.putExtra("TEXT", msg);
    691         startActivity(newIntent);
    692     }
    693 
    694     private void launchBrowser(BrowserSettings settings) {
    695         if (settings == null) {
    696             return;
    697         }
    698 
    699         Intent intent = new Intent(Intent.ACTION_VIEW);
    700 
    701         Uri data;
    702         if (settings.url != null) {
    703             CatLog.d(this, "settings.url = " + settings.url);
    704             if ((settings.url.startsWith("http://") || (settings.url.startsWith("https://")))) {
    705                 data = Uri.parse(settings.url);
    706             } else {
    707                 String modifiedUrl = "http://" + settings.url;
    708                 CatLog.d(this, "modifiedUrl = " + modifiedUrl);
    709                 data = Uri.parse(modifiedUrl);
    710             }
    711         } else {
    712             // If no URL specified, just bring up the "home page".
    713             //
    714             // (Note we need to specify *something* in the intent's data field
    715             // here, since if you fire off a VIEW intent with no data at all
    716             // you'll get an activity chooser rather than the browser.  There's
    717             // no specific URI that means "use the default home page", so
    718             // instead let's just explicitly bring up http://google.com.)
    719             data = Uri.parse("http://google.com/");
    720         }
    721         intent.setData(data);
    722 
    723         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    724         switch (settings.mode) {
    725         case USE_EXISTING_BROWSER:
    726             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    727             break;
    728         case LAUNCH_NEW_BROWSER:
    729             intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    730             break;
    731         case LAUNCH_IF_NOT_ALREADY_LAUNCHED:
    732             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    733             break;
    734         }
    735         // start browser activity
    736         startActivity(intent);
    737         // a small delay, let the browser start, before processing the next command.
    738         // this is good for scenarios where a related DISPLAY TEXT command is
    739         // followed immediately.
    740         try {
    741             Thread.sleep(10000);
    742         } catch (InterruptedException e) {}
    743     }
    744 
    745     private void launchCallMsg() {
    746         TextMessage msg = mCurrentCmd.getCallSettings().callMsg;
    747         if (msg.text == null || msg.text.length() == 0) {
    748             return;
    749         }
    750         msg.title = lastSelectedItem;
    751 
    752         Toast toast = Toast.makeText(mContext.getApplicationContext(), msg.text,
    753                 Toast.LENGTH_LONG);
    754         toast.setGravity(Gravity.BOTTOM, 0, 0);
    755         toast.show();
    756     }
    757 
    758     private void launchIdleText() {
    759         TextMessage msg = mCurrentCmd.geTextMessage();
    760 
    761         if (msg == null) {
    762             CatLog.d(this, "mCurrent.getTextMessage is NULL");
    763             mNotificationManager.cancel(STK_NOTIFICATION_ID);
    764             return;
    765         }
    766         if (msg.text == null) {
    767             mNotificationManager.cancel(STK_NOTIFICATION_ID);
    768         } else {
    769             Notification notification = new Notification();
    770             RemoteViews contentView = new RemoteViews(
    771                     PACKAGE_NAME,
    772                     com.android.internal.R.layout.status_bar_latest_event_content);
    773 
    774             notification.flags |= Notification.FLAG_NO_CLEAR;
    775             notification.icon = com.android.internal.R.drawable.stat_notify_sim_toolkit;
    776             // Set text and icon for the status bar and notification body.
    777             if (!msg.iconSelfExplanatory) {
    778                 notification.tickerText = msg.text;
    779                 contentView.setTextViewText(com.android.internal.R.id.text,
    780                         msg.text);
    781             }
    782             if (msg.icon != null) {
    783                 contentView.setImageViewBitmap(com.android.internal.R.id.icon,
    784                         msg.icon);
    785             } else {
    786                 contentView
    787                         .setImageViewResource(
    788                                 com.android.internal.R.id.icon,
    789                                 com.android.internal.R.drawable.stat_notify_sim_toolkit);
    790             }
    791             notification.contentView = contentView;
    792             notification.contentIntent = PendingIntent.getService(mContext, 0,
    793                     new Intent(mContext, StkAppService.class), 0);
    794 
    795             mNotificationManager.notify(STK_NOTIFICATION_ID, notification);
    796         }
    797     }
    798 
    799     private void launchToneDialog() {
    800         Intent newIntent = new Intent(this, ToneDialog.class);
    801         newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
    802                 | Intent.FLAG_ACTIVITY_NO_HISTORY
    803                 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    804                 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown));
    805         newIntent.putExtra("TEXT", mCurrentCmd.geTextMessage());
    806         newIntent.putExtra("TONE", mCurrentCmd.getToneSettings());
    807         startActivity(newIntent);
    808     }
    809 
    810     private void launchOpenChannelDialog() {
    811         TextMessage msg = mCurrentCmd.geTextMessage();
    812         if (msg == null) {
    813             CatLog.d(this, "msg is null, return here");
    814             return;
    815         }
    816 
    817         msg.title = getResources().getString(R.string.stk_dialog_title);
    818         if (msg.text == null) {
    819             msg.text = getResources().getString(R.string.default_open_channel_msg);
    820         }
    821 
    822         final AlertDialog dialog = new AlertDialog.Builder(mContext)
    823                     .setIconAttribute(android.R.attr.alertDialogIcon)
    824                     .setTitle(msg.title)
    825                     .setMessage(msg.text)
    826                     .setCancelable(false)
    827                     .setPositiveButton(getResources().getString(R.string.stk_dialog_accept),
    828                                        new DialogInterface.OnClickListener() {
    829                         public void onClick(DialogInterface dialog, int which) {
    830                             Bundle args = new Bundle();
    831                             args.putInt(RES_ID, RES_ID_CHOICE);
    832                             args.putInt(CHOICE, YES);
    833                             Message message = mServiceHandler.obtainMessage();
    834                             message.arg1 = OP_RESPONSE;
    835                             message.obj = args;
    836                             mServiceHandler.sendMessage(message);
    837                         }
    838                     })
    839                     .setNegativeButton(getResources().getString(R.string.stk_dialog_reject),
    840                                        new DialogInterface.OnClickListener() {
    841                         public void onClick(DialogInterface dialog, int which) {
    842                             Bundle args = new Bundle();
    843                             args.putInt(RES_ID, RES_ID_CHOICE);
    844                             args.putInt(CHOICE, NO);
    845                             Message message = mServiceHandler.obtainMessage();
    846                             message.arg1 = OP_RESPONSE;
    847                             message.obj = args;
    848                             mServiceHandler.sendMessage(message);
    849                         }
    850                     })
    851                     .create();
    852 
    853         dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    854         if (!mContext.getResources().getBoolean(
    855                 com.android.internal.R.bool.config_sf_slowBlur)) {
    856             dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
    857         }
    858 
    859         dialog.show();
    860     }
    861 
    862     private void launchTransientEventMessage() {
    863         TextMessage msg = mCurrentCmd.geTextMessage();
    864         if (msg == null) {
    865             CatLog.d(this, "msg is null, return here");
    866             return;
    867         }
    868 
    869         msg.title = getResources().getString(R.string.stk_dialog_title);
    870 
    871         final AlertDialog dialog = new AlertDialog.Builder(mContext)
    872                     .setIconAttribute(android.R.attr.alertDialogIcon)
    873                     .setTitle(msg.title)
    874                     .setMessage(msg.text)
    875                     .setCancelable(false)
    876                     .setPositiveButton(getResources().getString(android.R.string.ok),
    877                                        new DialogInterface.OnClickListener() {
    878                         public void onClick(DialogInterface dialog, int which) {
    879                         }
    880                     })
    881                     .create();
    882 
    883         dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    884         if (!mContext.getResources().getBoolean(
    885                 com.android.internal.R.bool.config_sf_slowBlur)) {
    886             dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
    887         }
    888 
    889         dialog.show();
    890     }
    891 
    892     private String getItemName(int itemId) {
    893         Menu menu = mCurrentCmd.getMenu();
    894         if (menu == null) {
    895             return null;
    896         }
    897         for (Item item : menu.items) {
    898             if (item.id == itemId) {
    899                 return item.text;
    900             }
    901         }
    902         return null;
    903     }
    904 
    905     private boolean removeMenu() {
    906         try {
    907             if (mCurrentMenu.items.size() == 1 &&
    908                 mCurrentMenu.items.get(0) == null) {
    909                 return true;
    910             }
    911         } catch (NullPointerException e) {
    912             CatLog.d(this, "Unable to get Menu's items size");
    913             return true;
    914         }
    915         return false;
    916     }
    917 }
    918