To sync caller id ussd response vaule to caller id ss vaule.

The feature syncs the value to ss after user set caller id by ussd.

Bug: 112177857
Test: manual - test case as below :
Insert docomo sim and enable carrier config
(caller_id_ussd_enhance_bool), and then dial the caller id ussd
command to set activate/deactivate.
To check caller id's value in UI page is the same as value by
ussd. (PASS)
To check ussd response message is correct string.(PASS)

Change-Id: I9bc2b07ae59c8fc445352c779c2a1d9765bcd813
diff --git a/src/com/android/phone/PhoneUtils.java b/src/com/android/phone/PhoneUtils.java
index 3306d4b..9b118ff 100644
--- a/src/com/android/phone/PhoneUtils.java
+++ b/src/com/android/phone/PhoneUtils.java
@@ -19,16 +19,17 @@
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.ProgressDialog;
-import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.net.Uri;
 import android.os.Handler;
 import android.os.Message;
+import android.os.PersistableBundle;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.VideoProfile;
+import android.telephony.CarrierConfigManager;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.SubscriptionManager;
 import android.text.TextUtils;
@@ -54,6 +55,7 @@
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.TelephonyCapabilities;
 import com.android.phone.CallGatewayManager.RawGatewayInfo;
+import com.android.phone.settings.SuppServicesUiUtil;
 
 import java.util.Arrays;
 import java.util.List;
@@ -384,6 +386,22 @@
                 text = null;
                 break;
             case COMPLETE:
+                PersistableBundle b = null;
+                if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
+                    b = app.getCarrierConfigForSubId(
+                            phone.getSubId());
+                } else {
+                    b = app.getCarrierConfig();
+                }
+
+                if (b.getBoolean(CarrierConfigManager.KEY_USE_CALLER_ID_USSD_BOOL)) {
+                    text = SuppServicesUiUtil.handleCallerIdUssdResponse(app, context, phone,
+                            mmiCode);
+                    if (mmiCode.getMessage() != null && !text.equals(mmiCode.getMessage())) {
+                        break;
+                    }
+                }
+
                 if (app.getPUKEntryActivity() != null) {
                     // if an attempt to unPUK the device was made, we specify
                     // the title and the message here.
@@ -560,7 +578,15 @@
         }
     }
 
-    private static void createUssdDialog(PhoneGlobals app, Context context, CharSequence text,
+    /**
+     * It displays the message dialog for user about the mmi code result message.
+     *
+     * @param app This is {@link PhoneGlobals}
+     * @param context Context to get strings.
+     * @param text This is message's result.
+     * @param windowType The new window type. {@link WindowManager.LayoutParams}.
+     */
+    public static void createUssdDialog(PhoneGlobals app, Context context, CharSequence text,
             int windowType) {
         log("displayMMIComplete: MMI code has finished running.");
 
diff --git a/src/com/android/phone/settings/SuppServicesUiUtil.java b/src/com/android/phone/settings/SuppServicesUiUtil.java
index 7c647f4..8cb37a5 100644
--- a/src/com/android/phone/settings/SuppServicesUiUtil.java
+++ b/src/com/android/phone/settings/SuppServicesUiUtil.java
@@ -22,23 +22,35 @@
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.WindowManager;
 
+import com.android.internal.telephony.MmiCode;
 import com.android.internal.telephony.Phone;
+import com.android.phone.CarrierXmlParser;
 import com.android.phone.GsmUmtsAdditionalCallOptions;
 import com.android.phone.GsmUmtsCallOptions;
+import com.android.phone.PhoneGlobals;
+import com.android.phone.PhoneUtils;
 import com.android.phone.R;
 
+import java.util.HashMap;
+
 /**
  * Utility class to help supplementary service functions and UI.
  */
 public class SuppServicesUiUtil {
     static final String LOG_TAG = "SuppServicesUiUtil";
 
+    private static final String CLIR_ACTIVATE = "#31#";
+    private static final String CLIR_DEACTIVATE = "*31#";
+
     /**
      * show dialog for supplementary services over ut precaution.
      *
-     * @param context The context.
-     * @param phone   The Phone object.
+     * @param context       The context.
+     * @param phone         The Phone object.
      * @param preferenceKey The preference's key.
      */
     public static Dialog showBlockingSuppServicesDialog(Context context, Phone phone,
@@ -140,4 +152,88 @@
         return telephonyManager.isNetworkRoaming(phone.getSubId())
                 && !phone.getDataRoamingEnabled();
     }
+
+    /**
+     * To handle caller id's ussd response message which sets caller id activate or deactivate,
+     * and then sync caller id's ussd value to ss value if this command successful.
+     *
+     * @param context context to get strings.
+     * @param mmiCode MMI result.
+     * @return Text from response message is displayed on dialog .
+     * @hide
+     */
+    public static CharSequence handleCallerIdUssdResponse(PhoneGlobals app, Context context,
+            Phone phone, MmiCode mmiCode) {
+        if (TextUtils.isEmpty(mmiCode.getDialString())) {
+            return mmiCode.getMessage();
+        }
+
+        TelephonyManager telephonyManager = new TelephonyManager(context, phone.getSubId());
+        int carrierId = telephonyManager.getSimCarrierId();
+        if (carrierId == TelephonyManager.UNKNOWN_CARRIER_ID) {
+            return mmiCode.getMessage();
+        }
+
+        CarrierXmlParser carrierXmlParser = new CarrierXmlParser(context, carrierId);
+        CarrierXmlParser.SsEntry.SSAction ssAction = carrierXmlParser.getCallerIdUssdCommandAction(
+                mmiCode.getDialString());
+        Log.d(LOG_TAG, "handleCallerIdUssdResponse: ssAction =" + ssAction);
+
+        if (ssAction == CarrierXmlParser.SsEntry.SSAction.UNKNOWN) {
+            return mmiCode.getMessage();
+        }
+
+        HashMap<String, String> analysisResult = carrierXmlParser.getFeature(
+                CarrierXmlParser.FEATURE_CALLER_ID)
+                .getResponseSet(ssAction,
+                        mmiCode.getMessage().toString());
+        Log.d(LOG_TAG, "handleCallerIdUssdResponse: analysisResult =" + analysisResult);
+        if (analysisResult.get(CarrierXmlParser.TAG_RESPONSE_STATUS).equals(
+                CarrierXmlParser.TAG_COMMAND_RESULT_DEFINITION_OK)) {
+
+            new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    TelephonyManager.UssdResponseCallback ussdCallback =
+                            new TelephonyManager.UssdResponseCallback() {
+                                @Override
+                                public void onReceiveUssdResponse(
+                                        final TelephonyManager telephonyManager,
+                                        String request, CharSequence response) {
+                                    Log.d(LOG_TAG, "handleCallerIdUssdResponse: response ="
+                                            + response.toString());
+                                    PhoneUtils.createUssdDialog(app, context, response.toString(),
+                                            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
+                                }
+
+                                @Override
+                                public void onReceiveUssdResponseFailed(
+                                        final TelephonyManager telephonyManager,
+                                        String request, int failureCode) {
+                                    Log.d(LOG_TAG, "handleCallerIdUssdResponse: failureCode ="
+                                            + failureCode);
+                                    PhoneUtils.createUssdDialog(app, context,
+                                            context.getText(R.string.response_error),
+                                            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
+                                }
+                            };
+
+                    String clir = "";
+                    if (ssAction == CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE) {
+                        clir = CLIR_ACTIVATE;
+                    } else {
+                        clir = CLIR_DEACTIVATE;
+                    }
+                    TelephonyManager telephonyManager =
+                            (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
+                    telephonyManager.sendUssdRequest(clir, ussdCallback, null);
+                }
+            }).start();
+
+            return "";
+        } else {
+            return context.getText(
+                    com.android.internal.R.string.mmiError);
+        }
+    }
 }