Merge "Revert "Stk: Add Support for alpha in REFRESH command.""
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index e750e93..1cb476d 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -17,66 +17,67 @@
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
-		package="com.android.stk"
-		android:sharedUserId="android.uid.phone">
+        package="com.android.stk"
+        android:sharedUserId="android.uid.phone">
 
     <original-package android:name="com.android.stk" />
 
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 
-	<application android:icon="@drawable/ic_launcher_sim_toolkit"
-		android:label="@string/app_name"
-		android:clearTaskOnLaunch="true"
-		android:process="com.android.phone">
+    <application android:icon="@drawable/ic_launcher_sim_toolkit"
+        android:label="@string/app_name"
+        android:clearTaskOnLaunch="true"
+        android:process="com.android.phone">
 
-		<activity android:name="StkLauncherActivity"
-			android:label="@string/app_name"
-			android:enabled="false"	>
-			<intent-filter>
-				<action android:name="android.intent.action.MAIN" />
-				<category android:name="android.intent.category.LAUNCHER" />
-			</intent-filter>
-		</activity>
+        <activity android:name="StkLauncherActivity"
+            android:label="@string/app_name"
+            android:enabled="false">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
 
-		<activity android:name="StkMenuActivity"
+        <activity android:name="StkMenuActivity"
+             android:configChanges="orientation|screenSize|keyboardHidden|mnc|mcc"
              android:launchMode="singleTop">
-	         <intent-filter>
-	             <action android:name="android.intent.action.VIEW" />
-	             <action android:name="android.intent.action.PICK" />
-	             <category android:name="android.intent.category.DEFAULT" />
-	         </intent-filter>
+             <intent-filter>
+                 <action android:name="android.intent.action.VIEW" />
+                 <action android:name="android.intent.action.PICK" />
+                 <category android:name="android.intent.category.DEFAULT" />
+             </intent-filter>
          </activity>
 
          <activity android:name="StkInputActivity"
-            android:configChanges="orientation|screenSize">
-			<intent-filter>
-		        <action android:name="android.intent.action.VIEW" />
-		        <action android:name="android.intent.action.EDIT" />
-		        <category android:name="android.intent.category.DEFAULT" />
-		    </intent-filter>
-		</activity>
+            android:configChanges="orientation|screenSize|keyboardHidden">
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+                <action android:name="android.intent.action.EDIT" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
 
         <activity android:name="StkDialogActivity"
             android:theme="@android:style/Theme.Dialog"
-            android:configChanges="orientation|screenSize">
+            android:configChanges="orientation|screenSize|keyboardHidden">
         </activity>
 
         <activity android:name="ToneDialog"
             android:theme="@android:style/Theme.Dialog">
         </activity>
 
-		<!-- SIM Toolkit settings activity -->
-		<activity android:name="StkSettings"
-			android:label="@string/app_name">
-			<intent-filter>
-				<action android:name="android.intent.action.VIEW" />
-				<action android:name="android.intent.action.MAIN" />
-				<category
-					android:name="android.intent.category.DEVELOPMENT_PREFERENCE" />
-			</intent-filter>
-		</activity>
+        <!-- SIM Toolkit settings activity -->
+        <activity android:name="StkSettings"
+            android:label="@string/app_name">
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+                <action android:name="android.intent.action.MAIN" />
+                <category
+                    android:name="android.intent.category.DEVELOPMENT_PREFERENCE" />
+            </intent-filter>
+        </activity>
 
-		<receiver android:name="com.android.stk.StkCmdReceiver">
+        <receiver android:name="com.android.stk.StkCmdReceiver">
             <intent-filter>
                 <action android:name= "android.intent.action.stk.command" />
                 <action android:name= "android.intent.action.stk.session_end" />
@@ -84,7 +85,7 @@
         </receiver>
 
         <receiver android:name="com.android.stk.BootCompletedReceiver"
-                    androidprv:primaryUserOnly="true">
+            androidprv:primaryUserOnly="true">
             <intent-filter>
                 <action android:name="android.intent.action.BOOT_COMPLETED" />
             </intent-filter>
@@ -92,5 +93,5 @@
 
         <service android:name="StkAppService" />
 
-	</application>
+    </application>
 </manifest>
diff --git a/res/values-pt-rPT/strings.xml b/res/values-pt-rPT/strings.xml
index 06bbae7..1c5e6b9 100644
--- a/res/values-pt-rPT/strings.xml
+++ b/res/values-pt-rPT/strings.xml
@@ -30,7 +30,7 @@
     <string name="digits" msgid="1746796679021682388">"Dígitos (0-9, *, #, +)"</string>
     <string name="default_call_setup_msg" msgid="7960194995759849940">"Chamada em curso..."</string>
     <string name="stk_app_state" msgid="18582277302584082">"Estado da aplicação"</string>
-    <string name="enable_app" msgid="3701224550936728203">"Activado"</string>
+    <string name="enable_app" msgid="3701224550936728203">"Ativado"</string>
     <string name="disable_app" msgid="6725081975611415214">"Desativado"</string>
     <string name="stk_dialog_title" msgid="6954825385456886726">"Toolkit SIM"</string>
     <string name="default_open_channel_msg" msgid="2216070254100295924">"Abrir Canal?"</string>
diff --git a/res/values-ro/strings.xml b/res/values-ro/strings.xml
index e18e17c..64e6795 100644
--- a/res/values-ro/strings.xml
+++ b/res/values-ro/strings.xml
@@ -17,23 +17,23 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <string name="app_name" msgid="8396461461738434341">"SIM Toolkit"</string>
-    <string name="menu_end_session" msgid="7368541737750982217">"Sfârşitul sesiunii"</string>
+    <string name="menu_end_session" msgid="7368541737750982217">"Sfârșitul sesiunii"</string>
     <string name="help" msgid="9209980881219397319">"Ajutor"</string>
     <string name="menu_back" msgid="301227740977453364">"Înapoi"</string>
     <string name="service_name" msgid="6860303747999592491">"Nume serviciu"</string>
     <string name="stk_no_service" msgid="5427773116839809131">"Niciun serviciu disponibil"</string>
     <string name="button_ok" msgid="5760722312558549555">"OK"</string>
-    <string name="button_cancel" msgid="1997847915560800261">"Anulaţi"</string>
+    <string name="button_cancel" msgid="1997847915560800261">"Anulați"</string>
     <string name="button_yes" msgid="624420260648337203">"Da"</string>
     <string name="button_no" msgid="1824959157582605627">"Nu"</string>
     <string name="alphabet" msgid="1583185545938805861">"Alfabete"</string>
     <string name="digits" msgid="1746796679021682388">"Cifre (0-9, *, #, +)"</string>
     <string name="default_call_setup_msg" msgid="7960194995759849940">"Se apelează..."</string>
-    <string name="stk_app_state" msgid="18582277302584082">"Starea aplicaţiei"</string>
+    <string name="stk_app_state" msgid="18582277302584082">"Starea aplicației"</string>
     <string name="enable_app" msgid="3701224550936728203">"Activat"</string>
     <string name="disable_app" msgid="6725081975611415214">"Dezactivat"</string>
     <string name="stk_dialog_title" msgid="6954825385456886726">"SIM Toolkit"</string>
-    <string name="default_open_channel_msg" msgid="2216070254100295924">"Deschideţi canalul?"</string>
+    <string name="default_open_channel_msg" msgid="2216070254100295924">"Deschideți canalul?"</string>
     <string name="default_send_data_msg" msgid="6011219698689931272">"Se trimit date"</string>
     <string name="default_receive_data_msg" msgid="618096941772010682">"Se primesc date"</string>
     <string name="default_close_channel_msg" msgid="765364262263839824">"Canal închis"</string>
diff --git a/src/com/android/stk/StkAppService.java b/src/com/android/stk/StkAppService.java
index 9ed4555..d98c14b 100644
--- a/src/com/android/stk/StkAppService.java
+++ b/src/com/android/stk/StkAppService.java
@@ -164,6 +164,7 @@
         if (mStkService == null) {
             stopSelf();
             CatLog.d(this, " Unable to get Service handle");
+            StkAppInstaller.unInstall(mContext);
             return;
         }
 
@@ -662,7 +663,7 @@
     private void launchTextDialog() {
         Intent newIntent = new Intent(this, StkDialogActivity.class);
         newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
-                | Intent.FLAG_ACTIVITY_CLEAR_TASK
+                | Intent.FLAG_ACTIVITY_CLEAR_TOP
                 | Intent.FLAG_ACTIVITY_NO_HISTORY
                 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
                 | getFlagActivityNoUserAction(InitiatedByUserAction.unknown));
@@ -779,7 +780,11 @@
 
             final Notification.Builder notificationBuilder = new Notification.Builder(
                     StkAppService.this);
-            notificationBuilder.setContentTitle("");
+            if (mMainCmd != null && mMainCmd.getMenu() != null) {
+                notificationBuilder.setContentTitle(mMainCmd.getMenu().title);
+            } else {
+                notificationBuilder.setContentTitle("");
+            }
             notificationBuilder
                     .setSmallIcon(com.android.internal.R.drawable.stat_notify_sim_toolkit);
             notificationBuilder.setContentIntent(pendingIntent);
@@ -787,6 +792,7 @@
             // Set text and icon for the status bar and notification body.
             if (!msg.iconSelfExplanatory) {
                 notificationBuilder.setContentText(msg.text);
+                notificationBuilder.setTicker(msg.text);
             }
             if (msg.icon != null) {
                 notificationBuilder.setLargeIcon(msg.icon);
diff --git a/src/com/android/stk/StkDialogActivity.java b/src/com/android/stk/StkDialogActivity.java
index 127f51b..b15e8df 100644
--- a/src/com/android/stk/StkDialogActivity.java
+++ b/src/com/android/stk/StkDialogActivity.java
@@ -16,6 +16,7 @@
 
 package com.android.stk;
 
+import com.android.internal.telephony.cat.CatLog;
 import com.android.internal.telephony.cat.TextMessage;
 
 import android.app.Activity;
@@ -126,6 +127,23 @@
     @Override
     public void onResume() {
         super.onResume();
+
+        /*
+         * The user should be shown the message forever or until some high
+         * priority event occurs (such as incoming call, MMI code execution
+         * etc as mentioned in ETSI 102.223, 6.4.1).
+         *
+         * Since mTextMsg.responseNeeded is false (because the response has
+         * already been sent) and duration of the dialog is zero and userClear
+         * is true, don't set the timeout.
+         */
+        if (!mTextMsg.responseNeeded &&
+                StkApp.calculateDurationInMilis(mTextMsg.duration) == 0 &&
+                mTextMsg.userClear) {
+            CatLog.d(this, "User should clear text..show message forever");
+            return;
+        }
+
         startTimeOut(mTextMsg.userClear);
     }
 
diff --git a/src/com/android/stk/StkMenuActivity.java b/src/com/android/stk/StkMenuActivity.java
index aac1a12..34c7026 100644
--- a/src/com/android/stk/StkMenuActivity.java
+++ b/src/com/android/stk/StkMenuActivity.java
@@ -22,10 +22,13 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
+import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
 import android.view.KeyEvent;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.Window;
+import android.widget.AdapterView;
 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.ProgressBar;
@@ -41,7 +44,7 @@
  * menu content.
  *
  */
-public class StkMenuActivity extends ListActivity {
+public class StkMenuActivity extends ListActivity implements View.OnCreateContextMenuListener {
     private Context mContext;
     private Menu mStkMenu = null;
     private int mState = STATE_MAIN;
@@ -59,6 +62,7 @@
 
     // message id for time out
     private static final int MSG_ID_TIMEOUT = 1;
+    private static final int CONTEXT_MENU_HELP = 0;
 
     Handler mTimeoutHandler = new Handler() {
         @Override
@@ -89,6 +93,8 @@
 
         initFromIntent(getIntent());
         mAcceptUsersInput = true;
+
+        getListView().setOnCreateContextMenuListener(this);
     }
 
     @Override
@@ -175,6 +181,7 @@
 
     @Override
     public void onDestroy() {
+        getListView().setOnCreateContextMenuListener(null);
         super.onDestroy();
 
         CatLog.d(this, "onDestroy");
@@ -235,6 +242,46 @@
     }
 
     @Override
+    public void onCreateContextMenu(ContextMenu menu, View v,
+            ContextMenuInfo menuInfo) {
+        CatLog.d(this, "onCreateContextMenu");
+        boolean helpVisible = false;
+        if (mStkMenu != null) {
+            helpVisible = mStkMenu.helpAvailable;
+        }
+        if (helpVisible) {
+            CatLog.d(this, "add menu");
+            menu.add(0, CONTEXT_MENU_HELP, 0, R.string.help);
+        }
+    }
+
+    @Override
+    public boolean onContextItemSelected(MenuItem item) {
+        AdapterView.AdapterContextMenuInfo info;
+        try {
+            info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
+        } catch (ClassCastException e) {
+            return false;
+        }
+        switch (item.getItemId()) {
+            case CONTEXT_MENU_HELP:
+                cancelTimeOut();
+                mAcceptUsersInput = false;
+                int position = info.position;
+                CatLog.d(this, "Position:" + position);
+                Item stkItem = getSelectedItem(position);
+                if (stkItem != null) {
+                    CatLog.d(this, "item id:" + stkItem.id);
+                    sendResponse(StkAppService.RES_ID_MENU_SELECTION, stkItem.id, true);
+                }
+                return true;
+
+            default:
+                return super.onContextItemSelected(item);
+        }
+    }
+
+    @Override
     protected void onSaveInstanceState(Bundle outState) {
         outState.putInt("STATE", mState);
         outState.putParcelable("MENU", mStkMenu);
@@ -263,22 +310,20 @@
     private void displayMenu() {
 
         if (mStkMenu != null) {
+            String title = mStkMenu.title == null ? getString(R.string.app_name) : mStkMenu.title;
             // Display title & title icon
             if (mStkMenu.titleIcon != null) {
                 mTitleIconView.setImageBitmap(mStkMenu.titleIcon);
                 mTitleIconView.setVisibility(View.VISIBLE);
-            } else {
-                mTitleIconView.setVisibility(View.GONE);
-            }
-            if (!mStkMenu.titleIconSelfExplanatory) {
-                mTitleTextView.setVisibility(View.VISIBLE);
-                if (mStkMenu.title == null) {
-                    mTitleTextView.setText(R.string.app_name);
-                } else {
-                    mTitleTextView.setText(mStkMenu.title);
+                mTitleTextView.setVisibility(View.INVISIBLE);
+                if (!mStkMenu.titleIconSelfExplanatory) {
+                    mTitleTextView.setText(title);
+                    mTitleTextView.setVisibility(View.VISIBLE);
                 }
             } else {
-                mTitleTextView.setVisibility(View.INVISIBLE);
+                mTitleIconView.setVisibility(View.GONE);
+                mTitleTextView.setVisibility(View.VISIBLE);
+                mTitleTextView.setText(title);
             }
             // create an array adapter for the menu list
             StkMenuAdapter adapter = new StkMenuAdapter(this,
diff --git a/src/com/android/stk/ToneDialog.java b/src/com/android/stk/ToneDialog.java
index 0ad6ee8..b68340b 100644
--- a/src/com/android/stk/ToneDialog.java
+++ b/src/com/android/stk/ToneDialog.java
@@ -23,6 +23,7 @@
 import android.os.Message;
 import android.os.Vibrator;
 import android.view.KeyEvent;
+import android.view.MotionEvent;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.TextView;
@@ -120,6 +121,17 @@
         return false;
     }
 
+    @Override
+    public boolean onTouchEvent(MotionEvent event) {
+        switch (event.getAction()) {
+        case MotionEvent.ACTION_DOWN:
+            sendResponse(StkAppService.RES_ID_END_SESSION);
+            finish();
+            return true;
+        }
+        return super.onTouchEvent(event);
+    }
+
     private void initFromIntent(Intent intent) {
         if (intent == null) {
             finish();