Add support of dislaying Alpha tag for BIP commands

Display dialogs when BIP related proactive commands are received.
"Open Channel" command will requre user input and a response will
be sent to Cat Service.

"Close Channel", "Send Data" and "Receive Data" command will
just show an alart dialog to notify the event to the user.

Bug:5165510
Change-Id: I350bdc7dfee2947cc0a4c7771ab4972e768c6ff9
diff --git a/res/values/strings.xml b/res/values/strings.xml
index bcce41f..bf26d73 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -45,4 +45,18 @@
     <string name="stk_app_state">Application state</string>
     <string name="enable_app">Enabled</string>
     <string name="disable_app">Disabled</string>
+
+    <!-- Title text used for system dialog from STK app -->
+    <string name="stk_dialog_title">SIM ToolKit</string>
+    <!-- Default message when no alpha in OPEN_CHANNEL -->
+    <string name="default_open_channel_msg">Open Channel?</string>
+    <!-- Default message when no alpha in SEND_DATA -->
+    <string name="default_send_data_msg">Sending Data</string>
+    <!-- Default message when no alpha in RECEIVE_DATA -->
+    <string name="default_receive_data_msg">Receiving Data</string>
+    <!-- Default message when no alpha in CLOSE_CHANNEL-->
+    <string name="default_close_channel_msg">Channel Closed</string>
+    <!-- Button texts used in STK dialog -->
+    <string name="stk_dialog_accept">YES</string>
+    <string name="stk_dialog_reject">NO</string>
 </resources>
diff --git a/src/com/android/stk/StkAppService.java b/src/com/android/stk/StkAppService.java
index 54698dc..3289918 100644
--- a/src/com/android/stk/StkAppService.java
+++ b/src/com/android/stk/StkAppService.java
@@ -16,11 +16,13 @@
 
 package com.android.stk;
 
+import android.app.AlertDialog;
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.Service;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;
@@ -32,6 +34,8 @@
 import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.Window;
+import android.view.WindowManager;
 import android.widget.ImageView;
 import android.widget.RemoteViews;
 import android.widget.TextView;
@@ -90,6 +94,7 @@
     static final String INPUT = "input";
     static final String HELP = "help";
     static final String CONFIRMATION = "confirm";
+    static final String CHOICE = "choice";
 
     // operations ids for different service functionality.
     static final int OP_CMD = 1;
@@ -104,12 +109,16 @@
     static final int RES_ID_INPUT = 12;
     static final int RES_ID_CONFIRM = 13;
     static final int RES_ID_DONE = 14;
+    static final int RES_ID_CHOICE = 15;
 
     static final int RES_ID_TIMEOUT = 20;
     static final int RES_ID_BACKWARD = 21;
     static final int RES_ID_END_SESSION = 22;
     static final int RES_ID_EXIT = 23;
 
+    static final int YES = 1;
+    static final int NO = 0;
+
     private static final String PACKAGE_NAME = "com.android.stk";
     private static final String MENU_ACTIVITY_NAME =
                                         PACKAGE_NAME + ".StkMenuActivity";
@@ -323,6 +332,9 @@
         case SEND_USSD:
         case SET_UP_IDLE_MODE_TEXT:
         case SET_UP_MENU:
+        case CLOSE_CHANNEL:
+        case RECEIVE_DATA:
+        case SEND_DATA:
             return false;
         }
 
@@ -438,6 +450,29 @@
         case PLAY_TONE:
             launchToneDialog();
             break;
+        case OPEN_CHANNEL:
+            launchOpenChannelDialog();
+            break;
+        case CLOSE_CHANNEL:
+        case RECEIVE_DATA:
+        case SEND_DATA:
+            TextMessage m = mCurrentCmd.geTextMessage();
+
+            if ((m != null) && (m.text == null)) {
+                switch(cmdMsg.getCmdType()) {
+                case CLOSE_CHANNEL:
+                    m.text = getResources().getString(R.string.default_close_channel_msg);
+                    break;
+                case RECEIVE_DATA:
+                    m.text = getResources().getString(R.string.default_receive_data_msg);
+                    break;
+                case SEND_DATA:
+                    m.text = getResources().getString(R.string.default_send_data_msg);
+                    break;
+                }
+            }
+            launchTransientEventMessage();
+            break;
         }
 
         if (!waitForUsersResponse) {
@@ -542,6 +577,18 @@
                 resMsg.setResultCode(ResultCode.NO_RESPONSE_FROM_USER);
             }
             break;
+        case RES_ID_CHOICE:
+            int choice = args.getInt(CHOICE);
+            CatLog.d(this, "User Choice=" + choice);
+            switch (choice) {
+                case YES:
+                    resMsg.setResultCode(ResultCode.OK);
+                    break;
+                case NO:
+                    resMsg.setResultCode(ResultCode.USER_NOT_ACCEPT);
+                    break;
+            }
+            break;
         default:
             CatLog.d(this, "Unknown result id");
             return;
@@ -764,6 +811,88 @@
         startActivity(newIntent);
     }
 
+    private void launchOpenChannelDialog() {
+        TextMessage msg = mCurrentCmd.geTextMessage();
+        if (msg == null) {
+            CatLog.d(this, "msg is null, return here");
+            return;
+        }
+
+        msg.title = getResources().getString(R.string.stk_dialog_title);
+        if (msg.text == null) {
+            msg.text = getResources().getString(R.string.default_open_channel_msg);
+        }
+
+        final AlertDialog dialog = new AlertDialog.Builder(mContext)
+                    .setIconAttribute(android.R.attr.alertDialogIcon)
+                    .setTitle(msg.title)
+                    .setMessage(msg.text)
+                    .setCancelable(false)
+                    .setPositiveButton(getResources().getString(R.string.stk_dialog_accept),
+                                       new DialogInterface.OnClickListener() {
+                        public void onClick(DialogInterface dialog, int which) {
+                            Bundle args = new Bundle();
+                            args.putInt(RES_ID, RES_ID_CHOICE);
+                            args.putInt(CHOICE, YES);
+                            Message message = mServiceHandler.obtainMessage();
+                            message.arg1 = OP_RESPONSE;
+                            message.obj = args;
+                            mServiceHandler.sendMessage(message);
+                        }
+                    })
+                    .setNegativeButton(getResources().getString(R.string.stk_dialog_reject),
+                                       new DialogInterface.OnClickListener() {
+                        public void onClick(DialogInterface dialog, int which) {
+                            Bundle args = new Bundle();
+                            args.putInt(RES_ID, RES_ID_CHOICE);
+                            args.putInt(CHOICE, NO);
+                            Message message = mServiceHandler.obtainMessage();
+                            message.arg1 = OP_RESPONSE;
+                            message.obj = args;
+                            mServiceHandler.sendMessage(message);
+                        }
+                    })
+                    .create();
+
+        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
+        if (!mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_sf_slowBlur)) {
+            dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
+        }
+
+        dialog.show();
+    }
+
+    private void launchTransientEventMessage() {
+        TextMessage msg = mCurrentCmd.geTextMessage();
+        if (msg == null) {
+            CatLog.d(this, "msg is null, return here");
+            return;
+        }
+
+        msg.title = getResources().getString(R.string.stk_dialog_title);
+
+        final AlertDialog dialog = new AlertDialog.Builder(mContext)
+                    .setIconAttribute(android.R.attr.alertDialogIcon)
+                    .setTitle(msg.title)
+                    .setMessage(msg.text)
+                    .setCancelable(false)
+                    .setPositiveButton(getResources().getString(android.R.string.ok),
+                                       new DialogInterface.OnClickListener() {
+                        public void onClick(DialogInterface dialog, int which) {
+                        }
+                    })
+                    .create();
+
+        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
+        if (!mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_sf_slowBlur)) {
+            dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
+        }
+
+        dialog.show();
+    }
+
     private String getItemName(int itemId) {
         Menu menu = mCurrentCmd.getMenu();
         if (menu == null) {