Blanket copy of PhoneApp to services/Telephony.

First phase of splitting out InCallUI from PhoneApp.

Change-Id: I237341c4ff00e96c677caa4580b251ef3432931b
diff --git a/src/com/android/phone/CallWaitingCheckBoxPreference.java b/src/com/android/phone/CallWaitingCheckBoxPreference.java
new file mode 100644
index 0000000..a2f5c70
--- /dev/null
+++ b/src/com/android/phone/CallWaitingCheckBoxPreference.java
@@ -0,0 +1,134 @@
+package com.android.phone;
+
+import com.android.internal.telephony.CommandException;
+import com.android.internal.telephony.Phone;
+
+import static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
+
+import android.content.Context;
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.os.Message;
+import android.preference.CheckBoxPreference;
+import android.util.AttributeSet;
+import android.util.Log;
+
+import com.android.internal.telephony.Phone;
+
+public class CallWaitingCheckBoxPreference extends CheckBoxPreference {
+    private static final String LOG_TAG = "CallWaitingCheckBoxPreference";
+    private final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
+
+    private final MyHandler mHandler = new MyHandler();
+    private final Phone mPhone;
+    private TimeConsumingPreferenceListener mTcpListener;
+
+    public CallWaitingCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+
+        mPhone = PhoneGlobals.getPhone();
+    }
+
+    public CallWaitingCheckBoxPreference(Context context, AttributeSet attrs) {
+        this(context, attrs, com.android.internal.R.attr.checkBoxPreferenceStyle);
+    }
+
+    public CallWaitingCheckBoxPreference(Context context) {
+        this(context, null);
+    }
+
+    /* package */ void init(TimeConsumingPreferenceListener listener, boolean skipReading) {
+        mTcpListener = listener;
+
+        if (!skipReading) {
+            mPhone.getCallWaiting(mHandler.obtainMessage(MyHandler.MESSAGE_GET_CALL_WAITING,
+                    MyHandler.MESSAGE_GET_CALL_WAITING, MyHandler.MESSAGE_GET_CALL_WAITING));
+            if (mTcpListener != null) {
+                mTcpListener.onStarted(this, true);
+            }
+        }
+    }
+
+    @Override
+    protected void onClick() {
+        super.onClick();
+
+        mPhone.setCallWaiting(isChecked(),
+                mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_WAITING));
+        if (mTcpListener != null) {
+            mTcpListener.onStarted(this, false);
+        }
+    }
+
+    private class MyHandler extends Handler {
+        static final int MESSAGE_GET_CALL_WAITING = 0;
+        static final int MESSAGE_SET_CALL_WAITING = 1;
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case MESSAGE_GET_CALL_WAITING:
+                    handleGetCallWaitingResponse(msg);
+                    break;
+                case MESSAGE_SET_CALL_WAITING:
+                    handleSetCallWaitingResponse(msg);
+                    break;
+            }
+        }
+
+        private void handleGetCallWaitingResponse(Message msg) {
+            AsyncResult ar = (AsyncResult) msg.obj;
+
+            if (mTcpListener != null) {
+                if (msg.arg2 == MESSAGE_SET_CALL_WAITING) {
+                    mTcpListener.onFinished(CallWaitingCheckBoxPreference.this, false);
+                } else {
+                    mTcpListener.onFinished(CallWaitingCheckBoxPreference.this, true);
+                }
+            }
+
+            if (ar.exception != null) {
+                if (DBG) {
+                    Log.d(LOG_TAG, "handleGetCallWaitingResponse: ar.exception=" + ar.exception);
+                }
+                if (mTcpListener != null) {
+                    mTcpListener.onException(CallWaitingCheckBoxPreference.this,
+                            (CommandException)ar.exception);
+                }
+            } else if (ar.userObj instanceof Throwable) {
+                if (mTcpListener != null) {
+                    mTcpListener.onError(CallWaitingCheckBoxPreference.this, RESPONSE_ERROR);
+                }
+            } else {
+                if (DBG) {
+                    Log.d(LOG_TAG, "handleGetCallWaitingResponse: CW state successfully queried.");
+                }
+                int[] cwArray = (int[])ar.result;
+                // If cwArray[0] is = 1, then cwArray[1] must follow,
+                // with the TS 27.007 service class bit vector of services
+                // for which call waiting is enabled.
+                try {
+                    setChecked(((cwArray[0] == 1) && ((cwArray[1] & 0x01) == 0x01)));
+                } catch (ArrayIndexOutOfBoundsException e) {
+                    Log.e(LOG_TAG, "handleGetCallWaitingResponse: improper result: err ="
+                            + e.getMessage());
+                }
+            }
+        }
+
+        private void handleSetCallWaitingResponse(Message msg) {
+            AsyncResult ar = (AsyncResult) msg.obj;
+
+            if (ar.exception != null) {
+                if (DBG) {
+                    Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception=" + ar.exception);
+                }
+                //setEnabled(false);
+            }
+            if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get");
+
+            mPhone.getCallWaiting(obtainMessage(MESSAGE_GET_CALL_WAITING,
+                    MESSAGE_SET_CALL_WAITING, MESSAGE_SET_CALL_WAITING, ar.exception));
+        }
+    }
+}