Add button in "hidden" settings to flip "VoLTE Provisioned" flag

VoLTE Provisioned flag can be lost when new device image is flashed. Currently
there is no way to trigger Verizon provisioning update from device. This patch
adds button in hidden settings which allow to manually change value of the
flag.

Bug: 19038362
Change-Id: I23f13004dbcf9eaf711c66261696772b1d7bfd6e
diff --git a/Android.mk b/Android.mk
index 425ae47..8888800 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,7 +1,7 @@
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
-LOCAL_JAVA_LIBRARIES := bouncycastle conscrypt telephony-common
+LOCAL_JAVA_LIBRARIES := bouncycastle conscrypt telephony-common ims-common
 LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4 android-support-v13 jsr305
 
 LOCAL_MODULE_TAGS := optional
diff --git a/res/layout/radio_info.xml b/res/layout/radio_info.xml
index d81dad6..a288224 100644
--- a/res/layout/radio_info.xml
+++ b/res/layout/radio_info.xml
@@ -226,6 +226,14 @@
                 android:layout_height="wrap_content"
                 />
 
+        <!-- VoLTE provisioned -->
+        <Button android:id="@+id/volte_provisioned_flag"
+                android:textSize="14sp"
+                android:layout_marginTop="8dip"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                />
+
         <!-- SMS over IMS -->
         <Button android:id="@+id/sms_over_ims"
                 android:textSize="14sp"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 10f289c..c8e2a1b 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -72,6 +72,11 @@
     <!-- Phone IMS registration required off. Only shown in diagnostic screen, so precise translation is not needed. -->
     <string name="ims_reg_required_off">Turn off IMS registration required</string>
 
+    <!-- VoLTE provisioning flag on. Only shown in diagnostic screen, so precise translation is not needed. -->
+    <string name="volte_provisioned_flag_on">Turn on VoLTE provisioned flag</string>
+    <!-- VoLTE provisioning flag off. Only shown in diagnostic screen, so precise translation is not needed. -->
+    <string name="volte_provisioned_flag_off">Turn off VoLTE provisioned flag</string>
+
     <!-- Phone ram dump on. Only shown in diagnostic screen, so precise translation is not needed. -->
     <string name="lte_ram_dump_on">Turn on lte ram dump</string>
     <!-- Phone ram dump off. Only shown in diagnostic screen, so precise translation is not needed. -->
diff --git a/src/com/android/settings/RadioInfo.java b/src/com/android/settings/RadioInfo.java
index 378221c..a296564 100644
--- a/src/com/android/settings/RadioInfo.java
+++ b/src/com/android/settings/RadioInfo.java
@@ -17,6 +17,7 @@
 package com.android.settings;
 
 import android.app.Activity;
+import android.app.QueuedWork;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
@@ -55,6 +56,9 @@
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.PhoneStateIntentReceiver;
 import com.android.internal.telephony.TelephonyProperties;
+import com.android.ims.ImsConfig;
+import com.android.ims.ImsException;
+import com.android.ims.ImsManager;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
@@ -195,6 +199,7 @@
                 case EVENT_SERVICE_STATE_CHANGED:
                     updateServiceState();
                     updatePowerState();
+                    updateImsVoLteProvisionedState();
                     break;
 
                 case EVENT_QUERY_PREFERRED_TYPE_DONE:
@@ -303,6 +308,9 @@
         imsRegRequiredButton = (Button) findViewById(R.id.ims_reg_required);
         imsRegRequiredButton.setOnClickListener(mImsRegRequiredHandler);
 
+        imsVoLteProvisionedButton = (Button) findViewById(R.id.volte_provisioned_flag);
+        imsVoLteProvisionedButton.setOnClickListener(mImsVoLteProvisionedHandler);
+
         smsOverImsButton = (Button) findViewById(R.id.sms_over_ims);
         smsOverImsButton.setOnClickListener(mSmsOverImsHandler);
 
@@ -360,6 +368,7 @@
         updatePowerState();
         updateCellInfoListRate();
         updateImsRegRequiredState();
+        updateImsVoLteProvisionedState();
         updateSmsOverImsState();
         updateLteRamDumpState();
         updateProperties();
@@ -1002,6 +1011,49 @@
         return SystemProperties.getBoolean(PROPERTY_SMS_OVER_IMS, false);
     }
 
+    private Button imsVoLteProvisionedButton;
+    OnClickListener mImsVoLteProvisionedHandler = new OnClickListener() {
+        @Override
+        public void onClick(View v) {
+            log(String.format("toggle VoLTE provisioned: currently %s",
+                    (isImsVoLteProvisioned() ? "on":"off")));
+            final boolean newValue = !isImsVoLteProvisioned();
+            if (phone != null) {
+                final ImsManager imsManager = ImsManager.getInstance(phone.getContext(), phone.getSubId());
+                if (imsManager != null) {
+                    QueuedWork.singleThreadExecutor().submit(new Runnable() {
+                        public void run() {
+                            try {
+                                imsManager.getConfigInterface().setProvisionedValue(
+                                        ImsConfig.ConfigConstants.VLT_SETTING_ENABLED,
+                                        newValue? 1 : 0);
+                            } catch (ImsException e) {
+                                Log.e(TAG, "setImsVoLteProvisioned() exception:", e);
+                            }
+                        }
+                    });
+                }
+            }
+            updateImsVoLteProvisionedState();
+        }
+    };
+
+    private boolean isImsVoLteProvisioned() {
+        if (phone != null) {
+            ImsManager imsManager = ImsManager.getInstance(phone.getContext(), phone.getSubId());
+            return imsManager.isVolteProvisionedOnDevice(phone.getContext());
+        }
+        return false;
+    }
+
+    private void updateImsVoLteProvisionedState() {
+        log("updateImsVoLteProvisionedState isImsVoLteProvisioned()=" + isImsVoLteProvisioned());
+        String buttonText = isImsVoLteProvisioned() ?
+                getString(R.string.volte_provisioned_flag_off) :
+                getString(R.string.volte_provisioned_flag_on);
+        imsVoLteProvisionedButton.setText(buttonText);
+    }
+
     private void updateSmsOverImsState() {
         log("updateSmsOverImsState isSmsOverImsEnabled()=" + isSmsOverImsEnabled());
         String buttonText = isSmsOverImsEnabled() ?