Skip to content

Commit

Permalink
Merge pull request #1424 from crankycoder/hotfix/1.6.1-motionsensor-npe
Browse files Browse the repository at this point in the history
hotfix to patch NPE on motionsensor code
  • Loading branch information
crankycoder committed Jan 22, 2015
2 parents 5359c6f + de0fc6c commit 0e46318
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void onCreate() {
AppGlobals.appVersionCode = BuildConfig.VERSION_CODE;
AppGlobals.appName = this.getResources().getString(R.string.app_name);

AppGlobals.hasSignificantMotionSensor = new MotionSensor(this, null).hasSignificantMotionSensor();
AppGlobals.hasSignificantMotionSensor = (MotionSensor.getSignificantMotionSensor(this) != null);

AsyncUploader.setGlobalUploadListener(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class MotionSensor {

/// Testing code
private static MotionSensor sDebugInstance;

public static void debugMotionDetected() {
if (sDebugInstance.mStopSignificantMotionSensor) {
return;
Expand All @@ -36,36 +37,48 @@ public static void debugMotionDetected() {
}
/// ---

public boolean hasSignificantMotionSensor() {
return mSignificantMotionSensor != null;
public static Sensor getSignificantMotionSensor(Context context) {
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor significantSensor;
if (sensorManager == null) {
AppGlobals.guiLogInfo("No sensor manager.");
return null;
}
if (Build.VERSION.SDK_INT >= 18) {
significantSensor = sensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
if (significantSensor != null) {
AppGlobals.guiLogInfo("Device has significant motion sensor.");
return significantSensor;
}
}
return null;
}

public MotionSensor(Context context, BroadcastReceiver callbackReceiver) {
sDebugInstance = this;
mContext = context;
LocalBroadcastManager.getInstance(context).registerReceiver(callbackReceiver,
new IntentFilter(ACTION_USER_MOTION_DETECTED));
mStopSignificantMotionSensor = false;

mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager == null) {
AppGlobals.guiLogInfo("No sensor manager.");
return;
}

mStopSignificantMotionSensor = false;
if (Build.VERSION.SDK_INT >= 18) {
mSignificantMotionSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
if (mSignificantMotionSensor != null) {
AppGlobals.guiLogInfo("Device has significant motion sensor.");
}
if (callbackReceiver != null) {
LocalBroadcastManager.getInstance(context).registerReceiver(callbackReceiver,
new IntentFilter(ACTION_USER_MOTION_DETECTED));
}

mSignificantMotionSensor = getSignificantMotionSensor(context);

// If no TYPE_SIGNIFICANT_MOTION is available, use alternate means to sense motion
if (mSignificantMotionSensor == null) {
mLegacyMotionSensor = new LegacyMotionSensor(mContext.getApplicationContext());
AppGlobals.guiLogInfo("Device has legacy motion sensor.");
}
}

public void start() {
if (mSignificantMotionSensor == null) {
if (mLegacyMotionSensor != null) {
Expand Down

0 comments on commit 0e46318

Please sign in to comment.