OmniGears: add slide animation for events chooser

Change-Id: I901a4c07150efd2773d9f4a848086b22ae22f8f6
diff --git a/res/layout/layout_floating_widget.xml b/res/layout/layout_floating_widget.xml
index 06a913a..0009761 100644
--- a/res/layout/layout_floating_widget.xml
+++ b/res/layout/layout_floating_widget.xml
@@ -43,7 +43,7 @@
                 android:layout_height="wrap_content"
                 android:background="@drawable/bg_app_chooser"
                 android:orientation="vertical"
-                android:padding="10dp" />
+                android:padding="@dimen/floating_widget_view_padding" />
         </ScrollView>
     </RelativeLayout>
 </FrameLayout>
diff --git a/res/values/custom_dimens.xml b/res/values/custom_dimens.xml
index 57af2c0..bf8196b 100755
--- a/res/values/custom_dimens.xml
+++ b/res/values/custom_dimens.xml
@@ -21,4 +21,6 @@
     <dimen name="color_preference_height">18dip</dimen>
     <dimen name="drag_grip_ridge_size">6dp</dimen>
     <dimen name="drag_grip_ridge_gap">4dp</dimen>
+    <dimen name="floating_widget_view_padding">10dp</dimen>
+    <dimen name="floating_widget_window_padding">10dp</dimen>
 </resources>
diff --git a/src/org/omnirom/omnigears/service/EventService.java b/src/org/omnirom/omnigears/service/EventService.java
index ed20f9c..0e70ef0 100644
--- a/src/org/omnirom/omnigears/service/EventService.java
+++ b/src/org/omnirom/omnigears/service/EventService.java
@@ -50,6 +50,8 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.WindowManager;
+import android.view.animation.Interpolator;
+import android.view.animation.PathInterpolator;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 
@@ -65,6 +67,8 @@
     private static boolean mIsRunning;
     private static boolean mWiredHeadsetConnected;
     private static boolean mA2DPConnected;
+    private static final int ANIM_DURATION = 300;
+    private static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
 
     private WindowManager mWindowManager;
     private View mFloatingWidget = null;
@@ -154,8 +158,7 @@
                     WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                     PixelFormat.TRANSLUCENT);
 
-            params.x = 25;
-            params.windowAnimations = android.R.style.Animation_Dialog;
+            params.x = context.getResources().getDimensionPixelSize(R.dimen.floating_widget_window_padding);
 
             int chooserPosition = getPrefs(context).getInt(EventServiceSettings.APP_CHOOSER_POSITION, 0);
             if (chooserPosition == 0) {
@@ -195,12 +198,13 @@
             close.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
-                    mWindowManager.removeViewImmediate(mFloatingWidget);
+                    slideAnimation(context, false);
                 }
             });
             linearLayout.addView(close);
 
             mWindowManager.addView(mFloatingWidget, params);
+            slideAnimation(context, true);
 
             final int timeout = getPrefs(context).getInt(EventServiceSettings.APP_CHOOSER_TIMEOUT, 15);
             if (timeout > 0) {
@@ -212,7 +216,7 @@
                                 && mFloatingWidget.getWindowToken() != null);
 
                         if (stillThere) {
-                            mWindowManager.removeViewImmediate(mFloatingWidget);
+                            slideAnimation(context, false);
                         }
                     }
                 }, timeout * 1000);
@@ -339,4 +343,48 @@
     private SharedPreferences getPrefs(Context context) {
         return context.getSharedPreferences(EventServiceSettings.EVENTS_PREFERENCES_NAME, Context.MODE_PRIVATE);
     }
+
+    private int getOverlayWidth(Context context) {
+        return (context.getResources().getDimensionPixelSize(R.dimen.floating_widget_view_padding) +
+                context.getResources().getDimensionPixelSize(android.R.dimen.app_icon_size)) / 2;
+    }
+
+    private void slideAnimation(Context context, final boolean show) {
+        // 0 is left
+        int chooserPosition = getPrefs(context).getInt(EventServiceSettings.APP_CHOOSER_POSITION, 0);
+
+        if (show) {
+            int startValue = 0;
+            if (chooserPosition == 1) {
+                startValue = getOverlayWidth(context);
+            } else {
+                startValue = -getOverlayWidth(context);
+            }
+            mFloatingWidget.setTranslationX(startValue);
+            mFloatingWidget.setAlpha(0);
+            mFloatingWidget.animate()
+                    .alpha(1)
+                    .translationX(0)
+                    .setDuration(ANIM_DURATION)
+                    .setInterpolator(FAST_OUT_SLOW_IN)
+                    .start();
+
+        } else {
+            int endValue = 0;
+            if (chooserPosition == 1) {
+                endValue = getOverlayWidth(context);
+            } else {
+                endValue = -getOverlayWidth(context);
+            }
+            mFloatingWidget.setTranslationX(0);
+            mFloatingWidget.setAlpha(1);
+            mFloatingWidget.animate()
+                    .alpha(0)
+                    .translationX(endValue)
+                    .setDuration(ANIM_DURATION)
+                    .setInterpolator(FAST_OUT_SLOW_IN)
+                    .withEndAction(() -> mWindowManager.removeViewImmediate(mFloatingWidget))
+                    .start();
+        }
+    }
 }