Merge "Do not inform user when the alpha identifier is a null data."
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ea50e85..c33b98f 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -50,6 +50,8 @@
 
     <!-- Title text used for system dialog from STK app -->
     <string name="stk_dialog_title">SIM ToolKit</string>
+    <!-- Default message when no alpha tag in Play Tone -->
+    <string name="default_tone_dialog_msg">Playing Tone</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 -->
diff --git a/src/com/android/stk/StkApp.java b/src/com/android/stk/StkApp.java
index 4484493..9d653c1 100644
--- a/src/com/android/stk/StkApp.java
+++ b/src/com/android/stk/StkApp.java
@@ -40,7 +40,7 @@
     static final int UI_TIMEOUT = (30 * 1000);
 
     // Tone default timeout - 2 seconds
-    static final int TONE_DFEAULT_TIMEOUT = (2 * 1000);
+    static final int TONE_DEFAULT_TIMEOUT = (2 * 1000);
 
     public static final String TAG = "STK App";
 
diff --git a/src/com/android/stk/StkAppService.java b/src/com/android/stk/StkAppService.java
index 91ae3d3..edd4306 100755
--- a/src/com/android/stk/StkAppService.java
+++ b/src/com/android/stk/StkAppService.java
@@ -32,6 +32,8 @@
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.res.Configuration;
+import android.content.res.Resources;
+import android.content.res.Resources.NotFoundException;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.net.Uri;
@@ -43,6 +45,7 @@
 import android.os.PersistableBundle;
 import android.os.PowerManager;
 import android.os.SystemProperties;
+import android.os.Vibrator;
 import android.provider.Settings;
 import android.telephony.CarrierConfigManager;
 import android.telephony.TelephonyManager;
@@ -73,6 +76,7 @@
 import com.android.internal.telephony.cat.CatLog;
 import com.android.internal.telephony.cat.CatResponseMessage;
 import com.android.internal.telephony.cat.TextMessage;
+import com.android.internal.telephony.cat.ToneSettings;
 import com.android.internal.telephony.uicc.IccRefreshResponse;
 import com.android.internal.telephony.uicc.IccCardStatus.CardState;
 import com.android.internal.telephony.PhoneConstants;
@@ -167,6 +171,8 @@
     private int mSimCount = 0;
     private PowerManager mPowerManager = null;
     private StkCmdReceiver mStkCmdReceiver = null;
+    private TonePlayer mTonePlayer = null;
+    private Vibrator mVibrator = null;
 
     // Used for setting FLAG_ACTIVITY_NO_USER_ACTION when
     // creating an intent.
@@ -190,6 +196,8 @@
     static final String STK_MENU_URI = "stk://com.android.stk/menu/";
     static final String STK_INPUT_URI = "stk://com.android.stk/input/";
     static final String STK_TONE_URI = "stk://com.android.stk/tone/";
+    static final String FINISH_TONE_ACTIVITY_ACTION =
+                                "android.intent.action.stk.finish_activity";
 
     // These below constants are used for SETUP_EVENT_LIST
     static final String SETUP_EVENT_TYPE = "event";
@@ -213,6 +221,15 @@
     //Invalid SetupEvent
     static final int INVALID_SETUP_EVENT = 0xFF;
 
+    // Message id to signal stop tone due to play tone timeout.
+    private static final int OP_STOP_TONE = 16;
+
+    // Message id to signal stop tone on user keyback.
+    static final int OP_STOP_TONE_USER = 17;
+
+    // Message id to remove stop tone message from queue.
+    private static final int STOP_TONE_WHAT = 100;
+
     // Response ids
     static final int RES_ID_MENU_SELECTION = 11;
     static final int RES_ID_INPUT = 12;
@@ -351,6 +368,10 @@
         case OP_END_SESSION:
         case OP_BOOT_COMPLETED:
             break;
+        case OP_STOP_TONE_USER:
+            msg.obj = args;
+            msg.what = STOP_TONE_WHAT;
+            break;
         default:
             return;
         }
@@ -598,6 +619,11 @@
                     }
                 }
                 break;
+            case OP_STOP_TONE_USER:
+            case OP_STOP_TONE:
+                CatLog.d(this, "Stop tone");
+                handleStopTone(msg, slotId);
+                break;
             }
         }
 
@@ -988,7 +1014,7 @@
             launchConfirmationDialog(mesg, slotId);
             break;
         case PLAY_TONE:
-            launchToneDialog(slotId);
+            handlePlayTone(slotId);
             break;
         case OPEN_CHANNEL:
             launchOpenChannelDialog(slotId);
@@ -1616,21 +1642,122 @@
         }
     }
 
-    private void launchToneDialog(int slotId) {
-        Intent newIntent = new Intent(this, ToneDialog.class);
-        String uriString = STK_TONE_URI + slotId;
-        Uri uriData = Uri.parse(uriString);
-        //Set unique URI to create a new instance of activity for different slotId.
-        CatLog.d(LOG_TAG, "launchToneDialog, slotId: " + slotId);
-        newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
-                | Intent.FLAG_ACTIVITY_NO_HISTORY
-                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
-                | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
-        newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
-        newIntent.putExtra("TONE", mStkContext[slotId].mCurrentCmd.getToneSettings());
-        newIntent.putExtra(SLOT_ID, slotId);
-        newIntent.setData(uriData);
-        startActivity(newIntent);
+    private void handlePlayTone(int slotId) {
+        TextMessage toneMsg = mStkContext[slotId].mCurrentCmd.geTextMessage();
+
+        boolean showUser = true;
+        boolean displayDialog = true;
+        Resources resource = Resources.getSystem();
+        try {
+            displayDialog = !resource.getBoolean(
+                    com.android.internal.R.bool.config_stkNoAlphaUsrCnf);
+        } catch (NotFoundException e) {
+            displayDialog = true;
+        }
+
+        // As per the spec 3GPP TS 11.14, 6.4.5. Play Tone.
+        // If there is no alpha identifier tlv present, UE may show the
+        // user information. 'config_stkNoAlphaUsrCnf' value will decide
+        // whether to show it or not.
+        // If alpha identifier tlv is present and its data is null, play only tone
+        // without showing user any information.
+        // Alpha Id is Present, but the text data is null.
+        if ((toneMsg.text != null ) && (toneMsg.text.equals(""))) {
+            CatLog.d(this, "Alpha identifier data is null, play only tone");
+            showUser = false;
+        }
+        // Alpha Id is not present AND we need to show info to the user.
+        if (toneMsg.text == null && displayDialog) {
+            CatLog.d(this, "toneMsg.text " + toneMsg.text
+                    + " Starting ToneDialog activity with default message.");
+            toneMsg.text = getResources().getString(R.string.default_tone_dialog_msg);
+            showUser = true;
+        }
+        // Dont show user info, if config setting is true.
+        if (toneMsg.text == null && !displayDialog) {
+            CatLog.d(this, "config value stkNoAlphaUsrCnf is true");
+            showUser = false;
+        }
+
+        CatLog.d(this, "toneMsg.text: " + toneMsg.text + "showUser: " +showUser +
+                "displayDialog: " +displayDialog);
+        playTone(showUser, slotId);
+    }
+
+    private void playTone(boolean showUserInfo, int slotId) {
+        // Start playing tone and vibration
+        ToneSettings settings = mStkContext[slotId].mCurrentCmd.getToneSettings();
+        if (null == settings) {
+            CatLog.d(this, "null settings, not playing tone.");
+            return;
+        }
+
+        mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
+        mTonePlayer = new TonePlayer();
+        mTonePlayer.play(settings.tone);
+        int timeout = StkApp.calculateDurationInMilis(settings.duration);
+        if (timeout == 0) {
+            timeout = StkApp.TONE_DEFAULT_TIMEOUT;
+        }
+
+        Message msg = mServiceHandler.obtainMessage();
+        msg.arg1 = OP_STOP_TONE;
+        msg.arg2 = slotId;
+        msg.obj = (Integer)(showUserInfo ? 1 : 0);
+        msg.what = STOP_TONE_WHAT;
+        mServiceHandler.sendMessageDelayed(msg, timeout);
+        if (settings.vibrate) {
+            mVibrator.vibrate(timeout);
+        }
+
+        // Start Tone dialog Activity to show user the information.
+        if (showUserInfo) {
+            Intent newIntent = new Intent(sInstance, ToneDialog.class);
+            String uriString = STK_TONE_URI + slotId;
+            Uri uriData = Uri.parse(uriString);
+            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
+                    | Intent.FLAG_ACTIVITY_NO_HISTORY
+                    | Intent.FLAG_ACTIVITY_SINGLE_TOP
+                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
+                    | getFlagActivityNoUserAction(InitiatedByUserAction.unknown, slotId));
+            newIntent.putExtra("TEXT", mStkContext[slotId].mCurrentCmd.geTextMessage());
+            newIntent.putExtra(SLOT_ID, slotId);
+            newIntent.setData(uriData);
+            startActivity(newIntent);
+        }
+    }
+
+    private void finishToneDialogActivity() {
+        Intent finishIntent = new Intent(FINISH_TONE_ACTIVITY_ACTION);
+        sendBroadcast(finishIntent);
+    }
+
+    private void handleStopTone(Message msg, int slotId) {
+        int resId = 0;
+
+        // Stop the play tone in following cases:
+        // 1.OP_STOP_TONE: play tone timer expires.
+        // 2.STOP_TONE_USER: user pressed the back key.
+        if (msg.arg1 == OP_STOP_TONE) {
+            resId = RES_ID_DONE;
+            // Dismiss Tone dialog, after finishing off playing the tone.
+            int finishActivity = (Integer) msg.obj;
+            if (finishActivity == 1) finishToneDialogActivity();
+        } else if (msg.arg1 == OP_STOP_TONE_USER) {
+            resId = RES_ID_END_SESSION;
+        }
+
+        sendResponse(resId, slotId, true);
+        mServiceHandler.removeMessages(STOP_TONE_WHAT);
+        if (mTonePlayer != null)  {
+            mTonePlayer.stop();
+            mTonePlayer.release();
+            mTonePlayer = null;
+        }
+        if (mVibrator != null) {
+            mVibrator.cancel();
+            mVibrator = null;
+        }
     }
 
     private void launchOpenChannelDialog(final int slotId) {
diff --git a/src/com/android/stk/ToneDialog.java b/src/com/android/stk/ToneDialog.java
index 834d575..40660f5 100755
--- a/src/com/android/stk/ToneDialog.java
+++ b/src/com/android/stk/ToneDialog.java
@@ -17,11 +17,13 @@
 package com.android.stk;
 
 import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
-import android.os.Vibrator;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
 import android.view.View;
@@ -29,49 +31,28 @@
 import android.widget.TextView;
 import com.android.internal.telephony.cat.CatLog;
 import com.android.internal.telephony.cat.TextMessage;
-import com.android.internal.telephony.cat.ToneSettings;
+import com.android.internal.telephony.cat.CatLog;
 
 /**
- * Activity used for PLAY TONE command.
+ * Activity used to display tone dialog.
  *
  */
 public class ToneDialog extends Activity {
     TextMessage toneMsg = null;
-    ToneSettings settings = null;
-    TonePlayer player = null;
     int mSlotId = -1;
-    boolean mIsResponseSent = false;
 
     private static final String LOG_TAG = new Object(){}.getClass().getEnclosingClass().getName();
 
-    /**
-     * Handler used to stop tones from playing when the duration ends.
-     */
-    Handler mToneStopper = new Handler() {
-        @Override
-        public void handleMessage(Message msg) {
-            switch (msg.what) {
-            case MSG_ID_STOP_TONE:
-                sendResponse(StkAppService.RES_ID_DONE);
-                finish();
-                break;
-            }
-        }
-    };
-
-    Vibrator mVibrator;
-
-    // Message id to signal tone duration timeout.
-    private static final int MSG_ID_STOP_TONE = 0xda;
-
     @Override
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
 
         CatLog.d(LOG_TAG, "onCreate");
-        mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
-
         initFromIntent(getIntent());
+        // Register receiver
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(StkAppService.FINISH_TONE_ACTIVITY_ACTION);
+        registerReceiver(mFinishActivityReceiver, filter);
 
         // remove window title
         View title = findViewById(com.android.internal.R.id.title);
@@ -98,50 +79,21 @@
         if (toneMsg.iconSelfExplanatory && toneMsg.icon != null) {
             tv.setVisibility(View.GONE);
         }
-
-        // Start playing tone and vibration
-        if (null == settings) {
-            CatLog.d(LOG_TAG, "onCreate - null settings - finish");
-            finish();
-            return;
-        }
-
-        player = new TonePlayer();
-        player.play(settings.tone);
-        int timeout = StkApp.calculateDurationInMilis(settings.duration);
-        if (timeout == 0) {
-            timeout = StkApp.TONE_DFEAULT_TIMEOUT;
-        }
-        mToneStopper.sendEmptyMessageDelayed(MSG_ID_STOP_TONE, timeout);
-        if (settings.vibrate && mVibrator != null) {
-            mVibrator.vibrate(timeout);
-        }
     }
 
     @Override
     protected void onDestroy() {
-        super.onDestroy();
         CatLog.d(LOG_TAG, "onDestroy");
-
-        mToneStopper.removeMessages(MSG_ID_STOP_TONE);
-        if (!mIsResponseSent) {
-            sendResponse(StkAppService.RES_ID_END_SESSION);
-        }
-
-        if (null != player) {
-            player.stop();
-            player.release();
-        }
-        if (null != mVibrator) {
-            mVibrator.cancel();
-        }
+        // Unregister receiver
+        unregisterReceiver(mFinishActivityReceiver);
+        super.onDestroy();
     }
 
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         switch (keyCode) {
         case KeyEvent.KEYCODE_BACK:
-            sendResponse(StkAppService.RES_ID_END_SESSION);
+            sendStopTone();
             finish();
             break;
         }
@@ -152,28 +104,38 @@
     public boolean onTouchEvent(MotionEvent event) {
         switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:
-            sendResponse(StkAppService.RES_ID_END_SESSION);
+            sendStopTone();
             finish();
             return true;
         }
         return super.onTouchEvent(event);
     }
 
+    private BroadcastReceiver mFinishActivityReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            // Intent received from StkAppService to finish ToneDialog activity,
+            // after finishing off playing the tone.
+            if (intent.getAction().equals(StkAppService.FINISH_TONE_ACTIVITY_ACTION)) {
+                CatLog.d(this, "Finishing Tone dialog activity");
+                finish();
+            }
+        }
+    };
+
     private void initFromIntent(Intent intent) {
         if (intent == null) {
             finish();
         }
         toneMsg = intent.getParcelableExtra("TEXT");
-        settings = intent.getParcelableExtra("TONE");
         mSlotId = intent.getIntExtra(StkAppService.SLOT_ID, -1);
     }
 
-    private void sendResponse(int resId) {
+    // Send stop playing tone to StkAppService, when user presses back key.
+    private void sendStopTone() {
         Bundle args = new Bundle();
-        args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
+        args.putInt(StkAppService.OPCODE, StkAppService.OP_STOP_TONE_USER);
         args.putInt(StkAppService.SLOT_ID, mSlotId);
-        args.putInt(StkAppService.RES_ID, resId);
         startService(new Intent(this, StkAppService.class).putExtras(args));
-        mIsResponseSent = true;
     }
 }