summaryrefslogtreecommitdiff
path: root/samples/ApiDemos/src/com/example/android/apis/view/GameView.java
diff options
context:
space:
mode:
authorJeff Brown <jeffbrown@google.com>2012-04-13 17:05:53 -0700
committerJeff Brown <jeffbrown@google.com>2012-04-13 17:08:35 -0700
commiteb1b7a61af6a37aa416ded9db42000a4faf9b66c (patch)
tree8ea7ada7e9fa4639f51c6baac5120a3910c99d68 /samples/ApiDemos/src/com/example/android/apis/view/GameView.java
parenta4920b722da0cec434a6fd2cb2f3b236c82ca7aa (diff)
Use new InputDevice.getVibrator() API.
Bug: 6334179 Change-Id: I1e592fd1a58a8d0789548346aee1aff6ddb9d684
Diffstat (limited to 'samples/ApiDemos/src/com/example/android/apis/view/GameView.java')
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/view/GameView.java27
1 files changed, 27 insertions, 0 deletions
diff --git a/samples/ApiDemos/src/com/example/android/apis/view/GameView.java b/samples/ApiDemos/src/com/example/android/apis/view/GameView.java
index 9fe236c3a..2904147ef 100644
--- a/samples/ApiDemos/src/com/example/android/apis/view/GameView.java
+++ b/samples/ApiDemos/src/com/example/android/apis/view/GameView.java
@@ -23,6 +23,7 @@ import android.graphics.Path;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.os.SystemClock;
+import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.InputDevice;
import android.view.KeyEvent;
@@ -36,6 +37,10 @@ import java.util.Random;
/**
* A trivial joystick based physics game to demonstrate joystick handling.
*
+ * If the game controller has a vibrator, then it is used to provide feedback
+ * when a bullet is fired or the ship crashes into an obstacle. Otherwise, the
+ * system vibrator is used for that purpose.
+ *
* @see GameControllerInput
*/
public class GameView extends View {
@@ -307,6 +312,8 @@ public class GameView extends View {
bullet.setVelocity(mShip.getBulletVelocityX(mBulletSpeed),
mShip.getBulletVelocityY(mBulletSpeed));
mBullets.add(bullet);
+
+ getVibrator().vibrate(20);
}
}
@@ -316,12 +323,26 @@ public class GameView extends View {
}
}
+ private void crash() {
+ getVibrator().vibrate(new long[] { 0, 20, 20, 40, 40, 80, 40, 300 }, -1);
+ }
+
private void reset() {
mShip = new Ship();
mBullets.clear();
mObstacles.clear();
}
+ private Vibrator getVibrator() {
+ if (mLastInputDevice != null) {
+ Vibrator vibrator = mLastInputDevice.getVibrator();
+ if (vibrator.hasVibrator()) {
+ return vibrator;
+ }
+ }
+ return (Vibrator)getContext().getSystemService(Context.VIBRATOR_SERVICE);
+ }
+
void animateFrame() {
long currentStepTime = SystemClock.uptimeMillis();
step(currentStepTime);
@@ -680,6 +701,12 @@ public class GameView extends View {
public float getDestroyAnimDuration() {
return 1.0f;
}
+
+ @Override
+ public void destroy() {
+ super.destroy();
+ crash();
+ }
}
private class Bullet extends Sprite {