Block USSD requests via TelephonyManager when carrier does not allow it.

Some carriers have expressed concern of the new USSD APIs in
TelephonyManager.  Adding the ability to block the execution of USSD
codes using the new APIs via TelephonyManager on a per-carrier basis.
The USSD code will result in the "failed" callback via the API.

Test: Manual
Bug: 62293605
Change-Id: Iffce2a52791b15f65705ec4e999848aa3284e299
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 8afe14f..cf80a11 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -36,6 +36,7 @@
 import android.os.Looper;
 import android.os.Message;
 import android.os.Process;
+import android.os.PersistableBundle;
 import android.os.ResultReceiver;
 import android.os.ServiceManager;
 import android.os.UserHandle;
@@ -54,12 +55,14 @@
 import android.telephony.ModemActivityInfo;
 import android.telephony.NeighboringCellInfo;
 import android.telephony.RadioAccessFamily;
+import android.telephony.Rlog;
 import android.telephony.ServiceState;
 import android.telephony.SmsManager;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyHistogram;
 import android.telephony.TelephonyManager;
+import android.telephony.UssdResponse;
 import android.telephony.VisualVoicemailSmsFilterSettings;
 import android.text.TextUtils;
 import android.util.ArraySet;
@@ -283,6 +286,22 @@
                      Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
                      String ussdRequest =  ussdObject.first;
                      ResultReceiver wrappedCallback = ussdObject.second;
+
+                     if (!isUssdApiAllowed(request.subId)) {
+                         // Carrier does not support use of this API, return failure.
+                         Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
+                         UssdResponse response = new UssdResponse(ussdRequest, null);
+                         Bundle returnData = new Bundle();
+                         returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
+                         wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
+
+                         request.result = true;
+                         synchronized (request) {
+                             request.notifyAll();
+                         }
+                         return;
+                     }
+
                      try {
                          request.result = phone != null ?
                                  phone.handleUssdRequest(ussdRequest, wrappedCallback)
@@ -3809,6 +3828,21 @@
         }
     }
 
+    private boolean isUssdApiAllowed(int subId) {
+        CarrierConfigManager configManager =
+                (CarrierConfigManager) mPhone.getContext().getSystemService(
+                        Context.CARRIER_CONFIG_SERVICE);
+        if (configManager == null) {
+            return false;
+        }
+        PersistableBundle pb = configManager.getConfigForSubId(subId);
+        if (pb == null) {
+            return false;
+        }
+        return pb.getBoolean(
+                CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
+    }
+
     /**
      * Check if phone is in emergency callback mode
      * @return true if phone is in emergency callback mode