Connecting answer call action in UI to service.

Also starting pinging animation for glow pad.

Change-Id: Ifac94582b8448c288a07577db02633bf9452ee56
diff --git a/InCallUI/res/layout/answer_fragment.xml b/InCallUI/res/layout/answer_fragment.xml
index bfe2303..b334a12 100644
--- a/InCallUI/res/layout/answer_fragment.xml
+++ b/InCallUI/res/layout/answer_fragment.xml
@@ -1,18 +1,19 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2013 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
+<!--
+  ~ Copyright (C) 2013 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
 
 <!-- TODO(klp): move out to separate file -->
 <com.android.incallui.AnswerUi
@@ -27,7 +28,8 @@
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true"
         android:gravity="center"
-        android:layout_gravity="center_vertical"
+        android:layout_gravity="bottom|center_horizontal"
+        android:background="#000"
 
         dc:targetDrawables="@array/incoming_call_widget_3way_targets"
         dc:targetDescriptions="@array/incoming_call_widget_3way_target_descriptions"
diff --git a/InCallUI/res/layout/incall_screen.xml b/InCallUI/res/layout/incall_screen.xml
index 01180ee..d9f7bdf 100644
--- a/InCallUI/res/layout/incall_screen.xml
+++ b/InCallUI/res/layout/incall_screen.xml
@@ -15,17 +15,14 @@
 -->
 
 <!-- In-call Phone UI; see InCallActivity.java. -->
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-              xmlns:dc="http://schemas.android.com/apk/res-auto"
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
-              android:orientation="vertical">
+              android:orientation="vertical"
+        android:id="@+id/main">
 
-    <View android:layout_width="wrap_content"
-          android:layout_height="0dp"
-          android:layout_weight="1"
+    <View android:layout_width="match_parent"
+          android:layout_height="match_parent"
           android:background="#ddd"/>
 
-
-
-</LinearLayout>
+</FrameLayout>
diff --git a/InCallUI/src/com/android/incallui/AnswerFragment.java b/InCallUI/src/com/android/incallui/AnswerFragment.java
index a80fa93..743fa58 100644
--- a/InCallUI/src/com/android/incallui/AnswerFragment.java
+++ b/InCallUI/src/com/android/incallui/AnswerFragment.java
@@ -17,6 +17,7 @@
 package com.android.incallui;
 
 import android.app.Fragment;
+import android.app.FragmentTransaction;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -27,14 +28,23 @@
  */
 public class AnswerFragment extends Fragment {
 
-    AnswerPresenter mPresenter;
-
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
         final AnswerUi ui = (AnswerUi) inflater.inflate(R.layout.answer_fragment, container,
                 false);
-        mPresenter = new AnswerPresenter(ui);
+        final AnswerPresenter presenter = new AnswerPresenter(ui, new AnswerPresenter.Listener() {
+            @Override
+            public void onAnswered() {
+                close();
+            }
+        });
         return ui;
     }
+
+    private void close() {
+        final FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
+        fragmentTransaction.remove(this);
+        fragmentTransaction.commit();
+    }
 }
diff --git a/InCallUI/src/com/android/incallui/AnswerPresenter.java b/InCallUI/src/com/android/incallui/AnswerPresenter.java
index 2377195..8d06c89 100644
--- a/InCallUI/src/com/android/incallui/AnswerPresenter.java
+++ b/InCallUI/src/com/android/incallui/AnswerPresenter.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
 package com.android.incallui;
 
 /**
@@ -6,26 +22,34 @@
 public class AnswerPresenter {
 
     private Ui mUi;
+    private Listener mListener;
 
-    public AnswerPresenter(Ui ui) {
+    public AnswerPresenter(Ui ui, Listener listener) {
         this.mUi = ui;
+        this.mListener = listener;
 
         mUi.setPresenter(this);
     }
 
     public void onAnswer() {
-
+        // TODO(klp): hook in call id.
+        CallMonitorService.answerCall(1);
+        mListener.onAnswered();
     }
 
     public void onDecline() {
-
+        mListener.onAnswered();
     }
 
     public void onText() {
-
+        mListener.onAnswered();
     }
 
     public interface Ui {
         void setPresenter(AnswerPresenter presenter);
     }
+
+    public interface Listener {
+        void onAnswered();
+    }
 }
diff --git a/InCallUI/src/com/android/incallui/AnswerUi.java b/InCallUI/src/com/android/incallui/AnswerUi.java
index f501153..295f9b8 100644
--- a/InCallUI/src/com/android/incallui/AnswerUi.java
+++ b/InCallUI/src/com/android/incallui/AnswerUi.java
@@ -1,6 +1,25 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
 package com.android.incallui;
 
 import android.content.Context;
+import android.os.Handler;
+import android.os.Message;
+import android.os.Parcelable;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.View;
@@ -14,8 +33,26 @@
         GlowPadView.OnTriggerListener {
 
     private static final String TAG = AnswerUi.class.getSimpleName();
+    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
+
+    // Parameters for the GlowPadView "ping" animation; see triggerPing().
+    private static final int PING_MESSAGE_WHAT = 101;
+    private static final boolean ENABLE_PING_AUTO_REPEAT = true;
+    private static final long PING_REPEAT_DELAY_MS = 1200;
+
+    private final Handler mPingHandler = new Handler() {
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case PING_MESSAGE_WHAT:
+                    triggerPing();
+                    break;
+            }
+        }
+    };
 
     private AnswerPresenter mPresenter;
+    private boolean mPingEnabled = true;
 
     public AnswerUi(Context context) {
         super(context);
@@ -26,17 +63,70 @@
     }
 
     @Override
-    public void onGrabbed(View v, int handle) {
+    protected void onFinishInflate() {
+        logD("onFinishInflate()");
+        super.onFinishInflate();
+        setOnTriggerListener(this);
+        startPing();
+    }
 
+    @Override
+    protected void onWindowVisibilityChanged(int visibility) {
+        logD("Visibility changed " + visibility);
+        super.onWindowVisibilityChanged(visibility);
+        switch (visibility) {
+            case View.VISIBLE:
+                startPing();
+                break;
+            case View.INVISIBLE:
+            case View.GONE:
+                stopPing();
+                break;
+        }
+    }
+
+    @Override
+    protected Parcelable onSaveInstanceState() {
+        logD("onSaveInstanceState()");
+        // TODO: evaluate this versus stopping during fragment onPause/onResume
+        stopPing();
+        return super.onSaveInstanceState();
+    }
+
+    public void startPing() {
+        mPingEnabled = true;
+        triggerPing();
+    }
+
+    public void stopPing() {
+        mPingEnabled = false;
+    }
+
+    private void triggerPing() {
+        if (mPingEnabled) {
+            ping();
+
+            if (ENABLE_PING_AUTO_REPEAT) {
+                mPingHandler.sendEmptyMessageDelayed(PING_MESSAGE_WHAT, PING_REPEAT_DELAY_MS);
+            }
+        }
+    }
+
+    @Override
+    public void onGrabbed(View v, int handle) {
+        logD("onGrabbed()");
+        stopPing();
     }
 
     @Override
     public void onReleased(View v, int handle) {
-
+        logD("onReleased()");
+        startPing();
     }
 
     @Override
     public void onTrigger(View v, int target) {
+        logD("onTrigger()");
         final int resId = getResourceIdForTarget(target);
         switch (resId) {
             case R.drawable.ic_lockscreen_answer:
@@ -69,4 +159,9 @@
         mPresenter = listener;
     }
 
+    private void logD(String msg) {
+        if (DEBUG) {
+            Log.d(TAG, msg);
+        }
+    }
 }
diff --git a/InCallUI/src/com/android/incallui/CallMonitorService.java b/InCallUI/src/com/android/incallui/CallMonitorService.java
index 4554ace..1af2c7b 100644
--- a/InCallUI/src/com/android/incallui/CallMonitorService.java
+++ b/InCallUI/src/com/android/incallui/CallMonitorService.java
@@ -17,17 +17,14 @@
 package com.android.incallui;
 
 import android.app.Service;
-import android.content.Context;
 import android.content.Intent;
-import android.os.Handler;
 import android.os.IBinder;
-import android.os.Message;
 import android.os.RemoteException;
 import android.util.Log;
-import android.widget.Toast;
 
-import com.android.services.telephony.common.ICallMonitorService;
+import com.android.internal.util.Preconditions;
 import com.android.services.telephony.common.ICallCommandService;
+import com.android.services.telephony.common.ICallMonitorService;
 
 /**
  * Service used to listen for call state changes.
@@ -37,15 +34,11 @@
     private static final String TAG = CallMonitorService.class.getSimpleName();
     private static final boolean DBG = false; // TODO: Have a shared location for this.
 
-    private MainHandler mMainHandler;
-    private ICallCommandService mCallCommandService;
-
-    private static final int DO_SHOW_ALERT = 1;
+    private static ICallCommandService mCallCommandService;
 
     @Override
     public void onCreate() {
         super.onCreate();
-        mMainHandler = new MainHandler();
     }
 
     @Override
@@ -67,56 +60,18 @@
 
         @Override
         public void onIncomingCall(int callId) {
-            final Message msg = mMainHandler.obtainMessage(DO_SHOW_ALERT, 0, 0,
-                    "Incoming call with call Id: " + callId);
-            mMainHandler.sendMessage(msg);
-
             final Intent intent = new Intent(getApplication(), InCallActivity.class);
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             startActivity(intent);
         }
     };
 
-    /**
-     * Handles messages from the Service methods so that they get called on the
-     * main thread.  Service methods by default are handled in background threads.
-     */
-    class MainHandler extends Handler {
-        MainHandler() {
-            super(getApplicationContext().getMainLooper(), null, true);
-        }
+    // TODO(klp): Not sure if static call is ok. Might need to switch to normal service binding.
+    public static void answerCall(int callId) {
+        Preconditions.checkState(mCallCommandService != null);
 
-        @Override
-        public void handleMessage(Message msg) {
-            executeMessage(msg);
-        }
-    }
-
-    private void showAlert(String message) {
-        Context context = getApplicationContext();
-        int duration = Toast.LENGTH_SHORT;
-
-        Toast.makeText(context, message, duration).show();
-    }
-
-    private void executeMessage(Message msg) {
-        logD("executeMessage(" + msg.what + ")");
-        switch (msg.what) {
-            case DO_SHOW_ALERT:
-                showAlert((String) msg.obj);
-                answerCall(0);
-                break;
-            default:
-                break;
-
-        }
-    }
-
-    private void answerCall(int callId) {
         try {
-            if (mCallCommandService != null) {
-                mCallCommandService.answerCall(callId);
-            }
+            mCallCommandService.answerCall(callId);
         } catch (RemoteException e) {
             Log.e(TAG, "answerCall : " + e);
         }
@@ -127,4 +82,4 @@
             Log.d(TAG, message);
         }
     }
- }
+}
diff --git a/InCallUI/src/com/android/incallui/InCallActivity.java b/InCallUI/src/com/android/incallui/InCallActivity.java
index ea06af8..67fe1ca 100644
--- a/InCallUI/src/com/android/incallui/InCallActivity.java
+++ b/InCallUI/src/com/android/incallui/InCallActivity.java
@@ -17,6 +17,7 @@
 package com.android.incallui;
 
 import android.app.Activity;
+import android.app.FragmentTransaction;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
@@ -54,17 +55,16 @@
         // Inflate everything in incall_screen.xml and add it to the screen.
         setContentView(R.layout.incall_screen);
 
-        // Initialize the UI
-        //findViewById(R.id.callCard);
-        //findViewById(R.id.inCallTouchUi);
-        //ViewStub stub = (ViewStub) findViewById(R.id.dtmf_twelve_key_dialer_stub);
-
         logD("onCreate(): exit");
     }
 
     @Override
     protected void onResume() {
         logD("onResume()...");
+        final AnswerFragment answerFragment = new AnswerFragment();
+        final FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
+        fragmentTransaction.add(R.id.main, answerFragment);
+        fragmentTransaction.commit();
         super.onResume();
     }