Back to project page Blufpoker.
The source code is released under:
Apache License
If you think the Android project Blufpoker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.stofkat.blufpoker; //from w w w . ja v a 2 s.co m import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import android.app.Activity; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.media.AudioManager; import android.media.SoundPool; import android.media.SoundPool.OnLoadCompleteListener; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.widget.Spinner; public class BlufPokerActivity extends Activity implements SensorEventListener { private SoundPool soundPool; private SensorManager sensorManager; private Spinner numberOfPlayersSpinner; private Spinner numberOfLivesSpinner; private int soundID; boolean loaded = false; private Intent intent; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); intent = new Intent(this, AndroidMergeSurfaceView.class); handler = new Handler(); setContentView(R.layout.activity_bluf_poker); // Why the fuck do I need these casts? numberOfPlayersSpinner = (Spinner) findViewById(R.id.numberOfPlayersSpinner); numberOfLivesSpinner = (Spinner) findViewById(R.id.numberOfLivesSpinner); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); Sensor acceloMeterSensor = sensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener(this, acceloMeterSensor, SensorManager.SENSOR_DELAY_NORMAL); // Set the hardware buttons to control the music this.setVolumeControlStream(AudioManager.STREAM_MUSIC); // Load the sound soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { loaded = true; } }); soundID = soundPool.load(this, R.raw.diceroll, 1); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.bluf_poker, menu); return true; } private void playShakeSound() { // Getting the user sound settings AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); float actualVolume = (float) audioManager .getStreamVolume(AudioManager.STREAM_MUSIC); float maxVolume = (float) audioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = actualVolume / maxVolume; // Is the sound loaded already? if (loaded) { soundPool.play(soundID, volume, volume, 1, 0, 1f); } } public AtomicLong lastUpdate; public AtomicLong timestampOffirstShake; public AtomicInteger numberOfTimesShaked = new AtomicInteger(0); public volatile float last_x, last_y, last_z; @Override public void onSensorChanged(SensorEvent event) { final int SHAKE_THRESHOLD = 800; if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { long currentTime = System.currentTimeMillis(); if (lastUpdate == null) { lastUpdate = new AtomicLong(currentTime); } if ((currentTime - lastUpdate.get()) > 100) { long diffTime = (currentTime - lastUpdate.get()); lastUpdate.set(currentTime); float x, y, z; x = event.values[0]; y = event.values[1]; z = event.values[2]; float xyz = x + y + z; float xyzMinusLastXYZ = xyz - last_x - last_y - last_z; float speed = Math.abs(xyzMinusLastXYZ) / diffTime * 10000; if (speed > SHAKE_THRESHOLD) { System.out.println("shake detected w/ speed: " + speed + ", x: " + x + ", y=" + y + ", z=" + z); if (numberOfTimesShaked.incrementAndGet() == 3) { playShakeSound(); handler.post(new Runnable() { public void run() { BlufPokerActivity.this.finishActivity(0); intent.putExtra(IntentKeys.NUMBER_OF_PLAYERS, Integer.parseInt(numberOfPlayersSpinner.getSelectedItem().toString())); intent.putExtra(IntentKeys.NUMBER_OF_LIVES, Integer.parseInt(numberOfLivesSpinner.getSelectedItem().toString())); startActivity(intent); } }); return; } } last_x = x; last_y = y; last_z = z; } } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} }