Created
July 2, 2018 12:22
-
-
Save kantapp/eaec609775c6bac23a73cd4149a9657f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.kantapp.cameraapp"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"></activity> | |
<activity android:name=".SplashScreen"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<activity android:name=".UserForm"></activity> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kantapp.cameraapp; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.hardware.Sensor; | |
import android.hardware.SensorEvent; | |
import android.hardware.SensorEventListener; | |
import android.hardware.SensorListener; | |
import android.hardware.SensorManager; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
// The following are used for the shake detection | |
private SensorManager mSensorManager; | |
private Sensor mAccelerometer; | |
private ShakeDetector mShakeDetector; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_user_form); | |
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); | |
mAccelerometer = mSensorManager | |
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | |
mShakeDetector = new ShakeDetector(); | |
mShakeDetector.setOnShakeListener(new ShakeDetector.OnShakeListener() { | |
@Override | |
public void onShake(int count) { | |
Toast.makeText(UserForm.this, "Hello", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
// Add the following line to register the Session Manager Listener onResume | |
mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI); | |
} | |
@Override | |
public void onPause() { | |
// Add the following line to unregister the Sensor Manager onPause | |
mSensorManager.unregisterListener(mShakeDetector); | |
super.onPause(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kantapp.cameraapp; | |
import android.hardware.Sensor; | |
import android.hardware.SensorEvent; | |
import android.hardware.SensorEventListener; | |
import android.hardware.SensorManager; | |
import android.util.FloatMath; | |
public class ShakeDetector implements SensorEventListener { | |
private static final float SHAKE_THRESHOLD_GRAVITY = 2.7F; | |
private static final int SHAKE_SLOP_TIME_MS = 500; | |
private static final int SHAKE_COUNT_RESET_TIME_MS = 3000; | |
private OnShakeListener mListener; | |
private long mShakeTimestamp; | |
private int mShakeCount; | |
public void setOnShakeListener(OnShakeListener listener) { | |
this.mListener = listener; | |
} | |
public interface OnShakeListener { | |
public void onShake(int count); | |
} | |
@Override | |
public void onAccuracyChanged(Sensor sensor, int accuracy) { | |
// ignore | |
} | |
@Override | |
public void onSensorChanged(SensorEvent event) { | |
if (mListener != null) { | |
float x = event.values[0]; | |
float y = event.values[1]; | |
float z = event.values[2]; | |
float gX = x / SensorManager.GRAVITY_EARTH; | |
float gY = y / SensorManager.GRAVITY_EARTH; | |
float gZ = z / SensorManager.GRAVITY_EARTH; | |
// gForce will be close to 1 when there is no movement. | |
float gForce = (float) Math.sqrt(gX * gX + gY * gY + gZ * gZ); | |
if (gForce > SHAKE_THRESHOLD_GRAVITY) { | |
final long now = System.currentTimeMillis(); | |
// ignore shake events too close to each other (500ms) | |
if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) { | |
return; | |
} | |
// reset the shake count after 3 seconds of no shakes | |
if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) { | |
mShakeCount = 0; | |
} | |
mShakeTimestamp = now; | |
mShakeCount++; | |
mListener.onShake(mShakeCount); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment