First cut at reverting to tap-to-share.

A lot more polish to go, but this gest the basic tap-to-share
behavior working again.

Change-Id: Ic1f1df4200c9a1424018df7602fe5cb278cdc1e6
diff --git a/res/drawable-hdpi/howto_sharetap.png b/res/drawable-hdpi/howto_sharetap.png
deleted file mode 100644
index b78329d..0000000
--- a/res/drawable-hdpi/howto_sharetap.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-hdpi/tap1.png b/res/drawable-hdpi/tap1.png
deleted file mode 100644
index 2395c0f..0000000
--- a/res/drawable-hdpi/tap1.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-hdpi/tap2.png b/res/drawable-hdpi/tap2.png
deleted file mode 100644
index afaa243..0000000
--- a/res/drawable-hdpi/tap2.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-hdpi/tap3.png b/res/drawable-hdpi/tap3.png
deleted file mode 100644
index a6b530e..0000000
--- a/res/drawable-hdpi/tap3.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-hdpi/tap4.png b/res/drawable-hdpi/tap4.png
deleted file mode 100644
index b07d8a6..0000000
--- a/res/drawable-hdpi/tap4.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-hdpi/tap5.png b/res/drawable-hdpi/tap5.png
deleted file mode 100644
index f1c8825..0000000
--- a/res/drawable-hdpi/tap5.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-hdpi/tap6.png b/res/drawable-hdpi/tap6.png
deleted file mode 100644
index cc22a0b..0000000
--- a/res/drawable-hdpi/tap6.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable/sharetap_anim.xml b/res/drawable/sharetap_anim.xml
deleted file mode 100644
index 6aa7d4f..0000000
--- a/res/drawable/sharetap_anim.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-/*
-** Copyright 2011, 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.
-*/
--->
-<animation-list
-        xmlns:android="http://schemas.android.com/apk/res/android"
-        android:oneshot="false">
-    <item android:drawable="@drawable/tap1" android:duration="200" />
-    <item android:drawable="@drawable/tap2" android:duration="200" />
-    <item android:drawable="@drawable/tap3" android:duration="200" />
-    <item android:drawable="@drawable/tap4" android:duration="200" />
-    <item android:drawable="@drawable/tap5" android:duration="200" />
-    <item android:drawable="@drawable/tap6" android:duration="600" />
-</animation-list>
diff --git a/src/com/android/nfc/HoldingItWrongUi.java b/src/com/android/nfc/HoldingItWrongUi.java
deleted file mode 100644
index 429421d..0000000
--- a/src/com/android/nfc/HoldingItWrongUi.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright (C) 2011 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.nfc;
-
-import android.app.AlertDialog;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.graphics.drawable.AnimationDrawable;
-import android.view.View;
-import android.view.WindowManager;
-import android.widget.ImageView;
-
-import com.android.nfc3.R;
-
-public class HoldingItWrongUi implements DialogInterface.OnDismissListener {
-
-    AlertDialog mDialog;
-    ImageView mImageView;
-
-    /** Must call from UI thread */
-    public void show(Context context) {
-        if (mDialog != null) {
-            return;
-        }
-
-        View v = View.inflate(context, R.layout.holding_it_wrong, null);
-
-        mImageView = (ImageView) v.findViewById(R.id.image);
-        mImageView.setBackgroundResource(R.drawable.sharetap_anim);
-
-
-        AlertDialog.Builder b = new AlertDialog.Builder(context);
-        b.setCancelable(false);
-        b.setView(v);
-
-        AlertDialog d = b.create();
-        d.setOnDismissListener(this);
-        d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
-        d.show();
-        mImageView.post(new Runnable() {
-            @Override
-            public void run() {
-                AnimationDrawable anim = (AnimationDrawable) mImageView.getBackground();
-                anim.start();
-            }
-        });
-        mDialog = d;
-    }
-
-    /** Must call from UI thread */
-    public void dismiss() {
-        if (mDialog != null) {
-            mDialog.dismiss();
-            mDialog = null;
-        }
-    }
-
-    @Override
-    public void onDismiss(DialogInterface dialog) {
-        mDialog = null;
-    }
-}
diff --git a/src/com/android/nfc/P2pEventManager.java b/src/com/android/nfc/P2pEventManager.java
index cb77783..6853c54 100644
--- a/src/com/android/nfc/P2pEventManager.java
+++ b/src/com/android/nfc/P2pEventManager.java
@@ -54,95 +54,16 @@
     final int mErrorSound;
     final SoundPool mSoundPool; // playback synchronized on this
     final Vibrator mVibrator;
-    final TiltDetector mTiltDetector;
     final NotificationManager mNotificationManager;
-    final HoldingItWrongUi mHoldingItWrongUi;
     final SendUi mSendUi;
 
     // only used on UI thread
     boolean mPrefsFirstShare;
     boolean mSending;
 
-    /** Detect if the screen is facing up or down */
-    class TiltDetector implements SensorEventListener {
-        /**
-         * Percent tilt required before triggering detection.
-         * 100 indicates the device must be exactly face-down.
-         */
-        static final int THRESHOLD_PERCENT = 75;
-        /**
-         * Alpha (smoothing) factor of the low-pass filter.
-         * Decreasing helps latency in case the device is already
-         * tilted from the beginning, but makes the filter more sensitive
-         * to large changes.
-         */
-        static final float LPF_ALPHA = 0.9f;
-        /**
-         * Mininum number of samples to take before acting on them.
-         */
-        static final int MINIMUM_SAMPLES = 10;
-        final SensorManager mSensorManager;
-        final Sensor mSensor;
-
-        // Only used on UI thread
-        boolean mSensorEnabled;
-        float mLastValue;
-        int mNumSamples;
-
-        public TiltDetector(Context context) {
-            mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
-            mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
-            mSensorEnabled = false;
-        }
-        public void enable() {
-            if (mSensorEnabled) {
-                return;
-            }
-            mSensorEnabled = true;
-            mLastValue = 0;
-            mNumSamples = 0;
-            mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
-        }
-        public void disable() {
-            if (!mSensorEnabled) {
-                return;
-            }
-            mSensorManager.unregisterListener(this, mSensor);
-            mSensorEnabled = false;
-        }
-        @Override
-        public void onSensorChanged(SensorEvent event) {
-            // always called on UI thread
-            if (!mSensorEnabled) {
-                return;
-            }
-            if (mNumSamples == 0) {
-                mLastValue = event.values[2];
-            } else {
-                // low-pass filter to get a better estimate of gravity
-                mLastValue = LPF_ALPHA * mLastValue + (1 - LPF_ALPHA) * event.values[2];
-            }
-            final boolean triggered = 100.0 * mLastValue / SensorManager.GRAVITY_EARTH > THRESHOLD_PERCENT;
-            mNumSamples++;
-            if (DBG) Log.d(TAG, "z=" + mLastValue + (triggered ? " TRIGGERED" : ""));
-            if (mNumSamples == MINIMUM_SAMPLES && !triggered) {
-                // you're holding it wrong
-                mHoldingItWrongUi.show(mContext);
-            }
-            if (mNumSamples >= MINIMUM_SAMPLES && triggered) {
-                disable();
-                onSendConfirmed();
-            }
-            return;
-        }
-        @Override
-        public void onAccuracyChanged(Sensor arg0, int arg1) { }
-    };
-
     public P2pEventManager(Context context, P2pEventListener.Callback callback) {
         mContext = context;
         mCallback = callback;
-        mTiltDetector = new TiltDetector(mContext);
         mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
         mStartSound = mSoundPool.load(mContext, R.raw.start, 1);
         mEndSound = mSoundPool.load(mContext, R.raw.end, 1);
@@ -163,7 +84,6 @@
         }
 
         mSending = false;
-        mHoldingItWrongUi = new HoldingItWrongUi();
         mSendUi = new SendUi(context, this);
     }
 
@@ -174,7 +94,7 @@
 
     @Override
     public void onP2pSendConfirmationRequested() {
-        mTiltDetector.enable();
+        mSendUi.showPreSend();
     }
 
     @Override
@@ -188,34 +108,23 @@
 
     @Override
     public void onP2pReceiveComplete() {
-        mHoldingItWrongUi.dismiss();
-        mTiltDetector.disable();
         mVibrator.vibrate(VIBRATION_PATTERN, -1);
         playSound(mEndSound);
     }
 
     @Override
     public void onP2pOutOfRange() {
-        mHoldingItWrongUi.dismiss();
-        mTiltDetector.disable();
         if (mSending) {
             playSound(mErrorSound);
-            mSendUi.dismiss();
             mSending = false;
         }
+        mSendUi.dismiss();
         mSendUi.releaseScreenshot();
     }
 
-    void onSendConfirmed() {
-        mVibrator.vibrate(VIBRATION_PATTERN, -1);
-        playSound(mStartSound);
-        mHoldingItWrongUi.dismiss();
-        mSending = true;
-        mSendUi.showPreSend();
-    }
-
     @Override
-    public void onPreFinished() {
+    public void onSendConfirmed() {
+        mSending = true;
         mCallback.onP2pSendConfirmed();
     }
 
diff --git a/src/com/android/nfc/SendUi.java b/src/com/android/nfc/SendUi.java
index a9acb96..dc3e4a2 100644
--- a/src/com/android/nfc/SendUi.java
+++ b/src/com/android/nfc/SendUi.java
@@ -30,22 +30,24 @@
 import android.util.DisplayMetrics;
 import android.view.Display;
 import android.view.LayoutInflater;
+import android.view.MotionEvent;
 import android.view.Surface;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.WindowManager;
 import android.widget.ImageView;
 
+import android.util.Log;
 /**
  * All methods must be called on UI thread
  */
-public class SendUi implements Animator.AnimatorListener {
+public class SendUi implements Animator.AnimatorListener, View.OnTouchListener {
 
     static final float[] PRE_SCREENSHOT_SCALE = {1.0f, 0.6f};
     static final int PRE_DURATION_MS = 50;
 
     static final float[] POST_SCREENSHOT_SCALE = {0.6f, 0.0f};
-    static final int POST_DURATION_MS = 200;
+    static final int POST_DURATION_MS = 600;
 
     // all members are only used on UI thread
     final WindowManager mWindowManager;
@@ -65,7 +67,7 @@
     boolean mAttached;
 
     interface Callback {
-        public void onPreFinished();
+        public void onSendConfirmed();
     }
 
     public SendUi(Context context, Callback callback) {
@@ -81,6 +83,7 @@
                 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         mScreenshotLayout = mLayoutInflater.inflate(R.layout.screenshot, null);
         mScreenshotView = (ImageView) mScreenshotLayout.findViewById(R.id.screenshot);
+        mScreenshotView.setOnTouchListener(this);
         mScreenshotLayout.setFocusable(true);
 
         mWindowLayoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
@@ -89,8 +92,8 @@
                 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
                 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED_SYSTEM
                 | WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING
-                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
-                | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
+                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
+//                | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                 PixelFormat.OPAQUE);
         mWindowLayoutParams.token = new Binder();
 
@@ -120,6 +123,7 @@
         if (mScreenshotBitmap == null || mAttached) {
             return;
         }
+        Log.e("npelly", "1");
         mScreenshotView.setImageBitmap(mScreenshotBitmap);
         mScreenshotLayout.requestFocus();
         mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams);
@@ -217,9 +221,7 @@
 
     @Override
     public void onAnimationEnd(Animator animation) {
-        if (animation == mPreAnimator) {
-            mCallback.onPreFinished();
-        } else if (animation == mPostAnimator) {
+        if (animation == mPostAnimator) {
             dismiss();
         }
     }
@@ -229,4 +231,14 @@
 
     @Override
     public void onAnimationRepeat(Animator animation) {  }
+
+    @Override
+    public boolean onTouch(View v, MotionEvent event) {
+        if (!mAttached) {
+            return false;
+        }
+        mPreAnimator.end();
+        mCallback.onSendConfirmed();
+        return true;
+    }
 }