Provide config for excluding CDMA activation code.

Bug: 19296388
Change-Id: Ic6c484b8e29bd1c60a3366e6d43ded80930cad2f
diff --git a/res/values/config.xml b/res/values/config.xml
index 881762d..25b23a1 100755
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -152,4 +152,8 @@
 
     <!-- After a CDMA conference call is merged, the swap button should be displayed. -->
     <bool name="support_swap_after_merge" translatable="false">true</bool>
+
+    <!-- Disables dialing "*228" (OTASP provisioning) on CDMA carriers where it is not supported or
+         is potentially harmful by locking the SIM to 3G. -->
+    <bool name="config_disable_cdma_activation_code">false</bool>
 </resources>
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index 57f40e9..38ba75c 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -17,7 +17,9 @@
 package com.android.services.telephony;
 
 import android.content.ComponentName;
+import android.content.Context;
 import android.content.Intent;
+import android.content.res.Configuration;
 import android.net.Uri;
 import android.telecom.Connection;
 import android.telecom.ConnectionRequest;
@@ -26,6 +28,8 @@
 import android.telecom.PhoneAccountHandle;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.ServiceState;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 
@@ -38,21 +42,29 @@
 import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.cdma.CDMAPhone;
 import com.android.phone.MMIDialogActivity;
+import com.android.phone.R;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
+import java.util.regex.Pattern;
 
 /**
  * Service for making GSM and CDMA connections.
  */
 public class TelephonyConnectionService extends ConnectionService {
+
+    // If configured, reject attempts to dial numbers matching this pattern.
+    private static final Pattern CDMA_ACTIVATION_CODE_REGEX_PATTERN =
+            Pattern.compile("\\*228[0-9]{0,2}");
+
     private final TelephonyConferenceController mTelephonyConferenceController =
             new TelephonyConferenceController(this);
     private final CdmaConferenceController mCdmaConferenceController =
             new CdmaConferenceController(this);
     private final ImsConferenceController mImsConferenceController =
             new ImsConferenceController(this);
+
     private ComponentName mExpectedComponentName = null;
     private EmergencyCallHelper mEmergencyCallHelper;
     private EmergencyTonePlayer mEmergencyTonePlayer;
@@ -116,6 +128,7 @@
             // Convert voicemail: to tel:
             handle = Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null);
         } else {
+            final Phone phone = getPhoneForAccount(request.getAccountHandle(), false);
             if (!PhoneAccount.SCHEME_TEL.equals(scheme)) {
                 Log.d(this, "onCreateOutgoingConnection, Handle %s is not type tel", scheme);
                 return Connection.createFailedConnection(
@@ -132,6 +145,28 @@
                                 android.telephony.DisconnectCause.INVALID_NUMBER,
                                 "Unable to parse number"));
             }
+
+            // Obtain the configuration for the outgoing phone's SIM. If the outgoing number
+            // matches the *228 regex pattern, fail the call. This number is used for OTASP, and
+            // when dialed would lock LTE SIMs to 3G if not prohibited..
+            SubscriptionManager subManager = SubscriptionManager.from(phone.getContext());
+            SubscriptionInfo subInfo = subManager.getActiveSubscriptionInfo(phone.getSubId());
+            if (subInfo != null) {
+                Configuration config = new Configuration();
+                config.mcc = subInfo.getMcc();
+                config.mnc = subInfo.getMnc();
+                Context subContext = phone.getContext().createConfigurationContext(config);
+
+                if (subContext.getResources() != null && subContext.getResources()
+                        .getBoolean(R.bool.config_disable_cdma_activation_code)) {
+                    if (CDMA_ACTIVATION_CODE_REGEX_PATTERN.matcher(number).matches()) {
+                        return Connection.createFailedConnection(
+                                DisconnectCauseUtil.toTelecomDisconnectCause(
+                                        android.telephony.DisconnectCause.INVALID_NUMBER,
+                                        "Tried to dial *228"));
+                    }
+                }
+            }
         }
 
         boolean isEmergencyNumber = PhoneNumberUtils.isPotentialEmergencyNumber(number);