Delay the continuous call waiting USSD commands

When the device receives the response of SET call waiting command, then
the device starts the the GET call waiting command. Since the frameworks
has the limitation, it can't handle two continuous USSDs if the AP sends
the commands too quickly. Therefore, the UI sets the delay about 1 sec.

Bug: 199244910
Test: build pass and request for the tester verify with this patch at
the bug.

Change-Id: Ieacbda965aa99bf85be92b01035384fbc6a3787f
diff --git a/src/com/android/phone/CallWaitingSwitchPreference.java b/src/com/android/phone/CallWaitingSwitchPreference.java
index 01dd3b2..609488c 100644
--- a/src/com/android/phone/CallWaitingSwitchPreference.java
+++ b/src/com/android/phone/CallWaitingSwitchPreference.java
@@ -6,28 +6,34 @@
 import android.content.Context;
 import android.os.Handler;
 import android.os.Message;
+import android.os.PersistableBundle;
 import android.preference.SwitchPreference;
+import android.telephony.CarrierConfigManager;
 import android.telephony.TelephonyManager;
 import android.util.AttributeSet;
 import android.util.Log;
 
 import com.android.internal.telephony.Phone;
 
-import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
 
 public class CallWaitingSwitchPreference extends SwitchPreference {
     private static final String LOG_TAG = "CallWaitingSwitchPreference";
+    private static final int DELAY_MILLIS_FOR_USSD = 1000;
     private final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
 
     private final MyHandler mHandler = new MyHandler();
     private Phone mPhone;
     private TimeConsumingPreferenceListener mTcpListener;
-    private Executor mExecutor;
+    private ScheduledExecutorService mExecutor;
     private TelephonyManager mTelephonyManager;
     private boolean mIsDuringUpdateProcess = false;
     private int mUpdateStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
     private int mQueryStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
+    private boolean mUssdMode = false;
 
     public CallWaitingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
@@ -45,9 +51,14 @@
             TimeConsumingPreferenceListener listener, boolean skipReading, Phone phone) {
         mPhone = phone;
         mTcpListener = listener;
-        mExecutor = Executors.newSingleThreadExecutor();
+        mExecutor = Executors.newSingleThreadScheduledExecutor();
         mTelephonyManager = getContext().getSystemService(
                 TelephonyManager.class).createForSubscriptionId(phone.getSubId());
+        CarrierConfigManager configManager = getContext().getSystemService(
+                CarrierConfigManager.class);
+        PersistableBundle bundle = configManager.getConfigForSubId(phone.getSubId());
+        mUssdMode = (bundle != null) ? bundle.getBoolean(
+                CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false) : false;
 
         if (!skipReading) {
             Log.d(LOG_TAG, "init getCallWaitingStatus");
@@ -67,7 +78,23 @@
     private void updateStatusCallBack(int result) {
         Log.d(LOG_TAG, "updateStatusCallBack: CW state " + result + ", and re get");
         mUpdateStatus = result;
-        mTelephonyManager.getCallWaitingStatus(mExecutor, this::queryStatusCallBack);
+        if (mUssdMode) {
+            Log.d(LOG_TAG, "updateStatusCallBack: USSD mode needs to wait 1s since Framework"
+                    + " has the limitation");
+            Consumer<Integer> resultListener = this::queryStatusCallBack;
+            try {
+                mExecutor.schedule(new Runnable() {
+                    @Override
+                    public void run() {
+                        mTelephonyManager.getCallWaitingStatus(mExecutor, resultListener);
+                    }
+                }, DELAY_MILLIS_FOR_USSD, TimeUnit.MILLISECONDS);
+            } catch (Exception e) {
+                Log.d(LOG_TAG, "Exception while waiting: " + e);
+            }
+        } else {
+            mTelephonyManager.getCallWaitingStatus(mExecutor, this::queryStatusCallBack);
+        }
     }
 
     @Override