Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
fix #1830
Browse files Browse the repository at this point in the history
Riot-Im, Google play version 0.7.07 Accelerometer use
  • Loading branch information
ylecollen committed Jan 3, 2018
1 parent ec1f2c1 commit 9da0118
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
9 changes: 5 additions & 4 deletions vector/src/main/java/im/vector/VectorApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class VectorApp extends MultiDexApplication {
/**
* Rage shake detection to send a bug report.
*/
private static final RageShake mRageShake = new RageShake();
private RageShake mRageShake;

/**
* Delay to detect if the application is in background.
Expand Down Expand Up @@ -218,8 +218,6 @@ public void onCreate() {
} catch (Exception e) {
}



mLogsDirectoryFile = new File(getCacheDir().getAbsolutePath() + "/logs");

org.matrix.androidsdk.util.Log.setLogDirectory(mLogsDirectoryFile);
Expand All @@ -236,7 +234,7 @@ public void onCreate() {
Log.d(LOG_TAG, "----------------------------------------------------------------");
Log.d(LOG_TAG, "----------------------------------------------------------------\n\n\n\n");

mRageShake.start(this);
mRageShake = new RageShake(this);

// init the REST client
MXSession.initUserAgent(getApplicationContext());
Expand Down Expand Up @@ -411,6 +409,8 @@ private void suspendApp() {

MyPresenceManager.advertiseAllUnavailable();

mRageShake.stop();

onAppPause();
}

Expand Down Expand Up @@ -541,6 +541,7 @@ private void stopActivityTransitionTimer() {
}

MyPresenceManager.advertiseAllOnline();
mRageShake.start();

mIsCallingInBackground = false;
mIsInBackground = false;
Expand Down
61 changes: 43 additions & 18 deletions vector/src/main/java/im/vector/util/RageShake.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,28 @@ public class RageShake implements SensorEventListener {
// the context
private Context mContext;

// the sensor
private SensorManager mSensorManager;
private Sensor mSensor;
private boolean mIsStarted;

/**
* Constructor
*/
public RageShake() {
public RageShake(Context context) {
mContext = context;

mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

if (null != mSensorManager) {
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}

if (null == mSensor) {
Log.e(LOG_TAG, "No accelerometer in this device. Cannot use rage shake.");
mSensorManager = null;
}

// Samsung devices for some reason seem to be less sensitive than others so the threshold is being
// lowered for them. A possible lead for a better formula is the fact that the sensitivity detected
// with the calculated force below seems to relate to the sample rate: The higher the sample rate,
Expand Down Expand Up @@ -101,19 +119,34 @@ public void onClick(DialogInterface dialog, int which) {
}
}

/**
* Tells if the rage shake feature is supported
* @return true if it is used
*/
private boolean useRageshake() {
return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(mContext.getString(im.vector.R.string.settings_key_use_rage_shake), true);
}

/**
* start the sensor detector
*/
public void start(Context context) {
mContext = context;
public void start() {
if ((null != mSensorManager) && useRageshake() && !VectorApp.isAppInBackground() && !mIsStarted) {
mIsStarted = true;
mLastUpdate = 0;
mLastShake = 0;
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}

SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (s == null) {
Log.e(LOG_TAG, "No accelerometer in this device. Cannot use rage shake.");
return;
/**
* Stop the sensor detector
*/
public void stop() {
if (null != mSensorManager) {
mSensorManager.unregisterListener(this, mSensor);
}
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
mIsStarted = false;
}

@Override
Expand All @@ -136,12 +169,6 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {

@Override
public void onSensorChanged(SensorEvent event) {
// ignore the sensor events when the application is in background
if (VectorApp.isAppInBackground()) {
mLastUpdate = 0;
return;
}

if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
return;
}
Expand Down Expand Up @@ -169,9 +196,7 @@ public void onSensorChanged(SensorEvent event) {
Log.d(LOG_TAG, "Shaking detected.");
mLastShakeTimestamp = System.currentTimeMillis();

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);

if (preferences.getBoolean(mContext.getString(im.vector.R.string.settings_key_use_rage_shake), true)) {
if (useRageshake()) {
promptForReport();
}
} else {
Expand Down

0 comments on commit 9da0118

Please sign in to comment.