summaryrefslogtreecommitdiffstats
path: root/src/com/android/nfc/P2pEventManager.java
diff options
context:
space:
mode:
authorNick Pelly <npelly@google.com>2011-08-17 22:23:12 -0700
committerNick Pelly <npelly@google.com>2011-08-17 23:52:25 -0700
commit5438ab0e17064e20870ac893a2dd2b9e1219eef1 (patch)
tree706f865c371256b2f5c9a4786f2e42477c907204 /src/com/android/nfc/P2pEventManager.java
parent0ea442671a5b3c178f522214c8d854b8021c7606 (diff)
downloadpackages_apps_nfc-5438ab0e17064e20870ac893a2dd2b9e1219eef1.zip
packages_apps_nfc-5438ab0e17064e20870ac893a2dd2b9e1219eef1.tar.gz
packages_apps_nfc-5438ab0e17064e20870ac893a2dd2b9e1219eef1.tar.bz2
0-click UI update
o New start sound o Vibrate only when - starting send - send success - receive success o Play sound only when - starting send - send success - send failure (out of range before complete) - receive success o Introduce HoldingItWrongUi with the new image o P2pAnimationActivity -> SendUi, and a huge cleanup of it - no longer an activity. It took 350ms to inflate the views on first onCreate() as an activity, but we can cache this ahead of time as a window. - Use ObjectAnimator instead of ViewAnimator, much less code o Use ACCELEROMETER instead of GRAVITY to get faster tilt detection. TODO (in order of priority) o LLCP has started crashing a lot again. About 1/10 times I come into P2P range the stack crashes now on my Nexus S TMO. At first I thought it was because I was doing animations during LLCP send, which might affect timing to pn544 due to CPU load. So I changed my first implementation (one animation) to split it into a pre-send and post-send, so we don't do any work during the actual send. Still seems to crash though. We really need to make it solid again. o Change notification text and settings text+image as per Rachel's email o Do some simple low-pass filter on ACCELEROMETER so we don't get caught out when the user shakes the phone o Plumb through a starting receive callback, and play sound + vibrate o Prevent touch during send animations. Not a big deal right now because we complete it so fast. o Prevent notification shade from being pulled during HoldingItWrongUi and SendUi o Prevent orientation change during SendUi Change-Id: Ieec1efb741244c68270e34a712c15c58621b1446
Diffstat (limited to 'src/com/android/nfc/P2pEventManager.java')
-rw-r--r--src/com/android/nfc/P2pEventManager.java169
1 files changed, 71 insertions, 98 deletions
diff --git a/src/com/android/nfc/P2pEventManager.java b/src/com/android/nfc/P2pEventManager.java
index 05fc234..66d8a92 100644
--- a/src/com/android/nfc/P2pEventManager.java
+++ b/src/com/android/nfc/P2pEventManager.java
@@ -37,7 +37,7 @@ import com.android.nfc3.R;
/**
* Manages vibration, sound and animation for P2P events.
*/
-public class P2pEventManager implements P2pEventListener {
+public class P2pEventManager implements P2pEventListener, SendUi.Callback {
static final String TAG = "NfcP2pEventManager";
static final boolean DBG = true;
@@ -46,9 +46,6 @@ public class P2pEventManager implements P2pEventListener {
static final long[] VIBRATION_PATTERN = {0, 100, 10000};
- public static final boolean TILT_ENABLED = true;
- public static final boolean TAP_ENABLED = true;
-
final Context mContext;
final P2pEventListener.Callback mCallback;
final SharedPreferences mPrefs;
@@ -59,10 +56,12 @@ public class P2pEventManager implements P2pEventListener {
final Vibrator mVibrator;
final TiltDetector mTiltDetector;
final NotificationManager mNotificationManager;
+ final HoldingItWrongUi mHoldingItWrongUi;
+ final SendUi mSendUi;
// only used on UI thread
boolean mPrefsFirstShare;
- boolean mAnimating;
+ boolean mSending;
/** Detect if the screen is facing up or down */
class TiltDetector implements SensorEventListener {
@@ -76,15 +75,12 @@ public class P2pEventManager implements P2pEventListener {
// Only used on UI thread
boolean mSensorEnabled;
- boolean mTriggerEnabled;
float mLastValue;
public TiltDetector(Context context) {
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
- mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
+ mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorEnabled = false;
- mTriggerEnabled = false;
- mLastValue = Float.MIN_VALUE;
}
public void enable() {
if (mSensorEnabled) {
@@ -94,37 +90,33 @@ public class P2pEventManager implements P2pEventListener {
mLastValue = Float.MIN_VALUE;
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI);
}
- public boolean enableTrigger() {
- if (!mSensorEnabled || mTriggerEnabled) {
- return false;
- }
- mTriggerEnabled = true;
- return checkTrigger();
- }
public void disable() {
if (!mSensorEnabled) {
return;
}
mSensorManager.unregisterListener(this, mSensor);
mSensorEnabled = false;
- mTriggerEnabled = false;
- }
- boolean checkTrigger() {
- if (!mTriggerEnabled ||
- 100.0 * mLastValue / SensorManager.GRAVITY_EARTH < THRESHOLD_PERCENT) {
- return false;
- }
- disable();
- mVibrator.vibrate(VIBRATION_PATTERN, -1);
- mCallback.onP2pSendConfirmed();
- return true;
}
@Override
public void onSensorChanged(SensorEvent event) {
// always called on UI thread
- mLastValue = event.values[2];
- if (DBG) Log.d(TAG, "z=" + mLastValue);
- checkTrigger();
+ if (!mSensorEnabled) {
+ return;
+ }
+ final float z = event.values[2];
+ final boolean triggered = 100.0 * z / SensorManager.GRAVITY_EARTH > THRESHOLD_PERCENT;
+ //TODO: apply a low pass filter so we get something closer to real gravity
+ if (DBG) Log.d(TAG, "z=" + z + (triggered ? " TRIGGERED" : ""));
+ if (mLastValue == Float.MIN_VALUE && !triggered) {
+ // Received first value, and you're holding it wrong
+ mHoldingItWrongUi.show(mContext);
+ }
+ mLastValue = z;
+ if (triggered) {
+ disable();
+ onSendConfirmed();
+ }
+ return;
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) { }
@@ -153,105 +145,86 @@ public class P2pEventManager implements P2pEventListener {
mPrefsFirstShare = false;
}
- P2pAnimationActivity.setCallback(mCallback);
- mAnimating = false;
+ mSending = false;
+ mHoldingItWrongUi = new HoldingItWrongUi();
+ mSendUi = new SendUi(context, this);
}
@Override
public void onP2pInRange() {
- if (TILT_ENABLED) {
- mTiltDetector.enable();
- }
- mVibrator.vibrate(VIBRATION_PATTERN, -1);
- P2pAnimationActivity.makeScreenshot(mContext);
+ mSendUi.takeScreenshot();
}
@Override
public void onP2pSendConfirmationRequested() {
- if (TILT_ENABLED && mTiltDetector.enableTrigger()) {
- return;
- }
-
- mAnimating = true;
- // Start the animation activity
- Intent animIntent = new Intent();
- animIntent.setClass(mContext, P2pAnimationActivity.class);
- animIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NEW_TASK);
- mContext.startActivity(animIntent);
- playSound(mStartSound);
+ mTiltDetector.enable();
}
@Override
public void onP2pSendComplete() {
- playSound(mEndSound);
checkFirstShare();
- finish(true, false);
+ playSound(mEndSound);
+ mVibrator.vibrate(VIBRATION_PATTERN, -1);
+ mSendUi.showPostSend();
+ mSending = false;
}
@Override
public void onP2pReceiveComplete() {
- if (TILT_ENABLED) {
- mTiltDetector.disable();
- }
- finish(false, true);
+ mHoldingItWrongUi.dismiss();
+ mTiltDetector.disable();
+ mVibrator.vibrate(VIBRATION_PATTERN, -1);
+ playSound(mEndSound);
}
@Override
public void onP2pOutOfRange() {
- if (TILT_ENABLED) {
- mTiltDetector.disable();
- }
- if (mAnimating) {
+ mHoldingItWrongUi.dismiss();
+ mTiltDetector.disable();
+ if (mSending) {
playSound(mErrorSound);
+ mSendUi.dismiss();
+ mSending = false;
}
- finish(false, false);
+ mSendUi.releaseScreenshot();
}
- /**
- * Finish up the animation, if running.
- * Must be called on the UI thread.
- */
- void finish(boolean sendSuccess, boolean receiveSuccess) {
- if (!mAnimating) {
- return;
- }
- if (sendSuccess) {
- P2pAnimationActivity.finishWithSend();
- } else if (receiveSuccess) {
- P2pAnimationActivity.finishWithReceive();
- } else {
- P2pAnimationActivity.finishWithFailure();
- }
- mAnimating = false;
+ void onSendConfirmed() {
+ mVibrator.vibrate(VIBRATION_PATTERN, -1);
+ playSound(mStartSound);
+ mHoldingItWrongUi.dismiss();
+ mSending = true;
+ mSendUi.showPreSend();
}
- /** If first time, display up a notification */
- void checkFirstShare() {
- synchronized (this) {
- if (mPrefsFirstShare) {
- mPrefsFirstShare = false;
- SharedPreferences.Editor editor = mPrefs.edit();
- editor.putBoolean(PREF_FIRST_SHARE, false);
- editor.apply();
+ @Override
+ public void onPreFinished() {
+ mCallback.onP2pSendConfirmed();
+ }
- Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
- PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent,
- PendingIntent.FLAG_UPDATE_CURRENT);
- Notification notification = new Notification.Builder(mContext)
- .setContentTitle(mContext.getString(R.string.first_share_title))
- .setContentText(mContext.getString(R.string.first_share_text))
- .setContentIntent(pi)
- .setSmallIcon(R.drawable.stat_sys_nfc)
- .setAutoCancel(true)
- .getNotification();
- mNotificationManager.notify(NOTIFICATION_FIRST_SHARE, notification);
- }
+ /** If first time, display a notification */
+ void checkFirstShare() {
+ if (mPrefsFirstShare) {
+ mPrefsFirstShare = false;
+ SharedPreferences.Editor editor = mPrefs.edit();
+ editor.putBoolean(PREF_FIRST_SHARE, false);
+ editor.apply();
+
+ Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
+ PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent,
+ PendingIntent.FLAG_UPDATE_CURRENT);
+ Notification notification = new Notification.Builder(mContext)
+ .setContentTitle(mContext.getString(R.string.first_share_title))
+ .setContentText(mContext.getString(R.string.first_share_text))
+ .setContentIntent(pi)
+ .setSmallIcon(R.drawable.stat_sys_nfc)
+ .setAutoCancel(true)
+ .getNotification();
+ mNotificationManager.notify(NOTIFICATION_FIRST_SHARE, notification);
}
}
void playSound(int sound) {
- synchronized (this) {
- mSoundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f);
- }
+ mSoundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f);
}
}