Skip to content

Commit

Permalink
Deal with missing gyro or accelerometer sensors more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
fredg02 committed Apr 8, 2015
1 parent 380230e commit 2fd75d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

import com.MobileAnarchy.Android.Widgets.Joystick.DualJoystickView;

Expand All @@ -59,23 +60,30 @@ public GyroscopeController(Controls controls, MainActivity activity, DualJoystic
if (mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) != null) {
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
mSeListener = new RotationVectorListener();
} else {
} else if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSeListener = new AccelerometerListener();
}
if (mSensor != null) {
Log.i("GyroscopeController", "Gyro sensor type: " + mSensor.getName());
}
}

@Override
public void enable() {
super.enable();
mSensorManager.registerListener(mSeListener, mSensor, SensorManager.SENSOR_DELAY_UI);
if (mSensor != null && mSeListener != null) {
mSensorManager.registerListener(mSeListener, mSensor, SensorManager.SENSOR_DELAY_UI);
}
}

@Override
public void disable() {
mSensorRoll = 0;
mSensorPitch = 0;
mSensorManager.unregisterListener(mSeListener);
if (mSeListener != null) {
mSensorManager.unregisterListener(mSeListener);
}
super.disable();
}

Expand Down
38 changes: 26 additions & 12 deletions src/se/bitcraze/crazyfliecontrol/prefs/PreferencesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ public static class PreferencesFragment extends PreferenceFragment implements On

private String[] mDatarateStrings;

private boolean mNoGyroSensor = false;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -219,15 +221,6 @@ public boolean onPreferenceClick(Preference preference) {
mPitchTrimPlusBtnDefaultValue = setInitialSummaryAndReturnDefaultValue(KEY_PREF_PITCHTRIM_PLUS_BTN, R.string.preferences_pitchtrim_plus_btn_defaultValue);
mPitchTrimMinusBtnDefaultValue = setInitialSummaryAndReturnDefaultValue(KEY_PREF_PITCHTRIM_MINUS_BTN, R.string.preferences_pitchtrim_minus_btn_defaultValue);

//Test the available sensors
SensorManager mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) == null && mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) == null) {
CheckBoxPreference pref = (CheckBoxPreference) findPreference(KEY_PREF_USE_GYRO_BOOL);
pref.setEnabled(false);
pref.setChecked(false);
resetPreference(KEY_PREF_USE_GYRO_BOOL, false);
}

findPreference(KEY_PREF_RESET_BTN).setOnPreferenceClickListener(new OnPreferenceClickListener() {

@Override
Expand Down Expand Up @@ -272,6 +265,24 @@ public boolean onPreferenceClick(Preference preference) {
});
}

private void checkGyroSensors() {
//Test the available sensors
SensorManager mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
CheckBoxPreference pref = (CheckBoxPreference) findPreference(KEY_PREF_USE_GYRO_BOOL);

if (mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) == null && mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) == null) {
pref.setEnabled(false);
pref.setChecked(false);
resetPreference(KEY_PREF_USE_GYRO_BOOL, false);
mNoGyroSensor = true;
pref.setSummaryOff("No gyro or accelerometer sensors found");
Log.i(LOG_TAG, "No gyro or accelerometer sensors found");
} else {
pref.setEnabled(true);
mNoGyroSensor = false;
}
}

private void setRadioStats() {
Preference pref = findPreference(KEY_PREF_RADIO_STATS);
String defaultValue = getResources().getString(R.string.preferences_radio_stats_summary);
Expand Down Expand Up @@ -337,10 +348,10 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
// automatically activate the screen rotation lock preference
CheckBoxPreference screenRotationLock = (CheckBoxPreference) findPreference(KEY_PREF_SCREEN_ROTATION_LOCK_BOOL);
if (useGyro) {
Toast.makeText(getActivity(), "Activated screen rotation lock...", Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "Activating screen rotation lock...", Toast.LENGTH_LONG).show();
screenRotationLock.setSummary("Locked because gyroscope is used as controller.");
} else {
Toast.makeText(getActivity(), "Deactivated screen rotation lock...", Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "Deactivating screen rotation lock...", Toast.LENGTH_LONG).show();
screenRotationLock.setSummary("");
}
screenRotationLock.setChecked(useGyro);
Expand Down Expand Up @@ -449,7 +460,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
private void setControllerSpecificPreferences() {
String controllerDefaultValue = getResources().getString(R.string.preferences_controller_defaultValue);
int controllerIndex = Integer.parseInt(mSharedPreferences.getString(KEY_PREF_CONTROLLER, controllerDefaultValue));
findPreference(KEY_PREF_USE_GYRO_BOOL).setEnabled(controllerIndex == 0);
if (!mNoGyroSensor) {
findPreference(KEY_PREF_USE_GYRO_BOOL).setEnabled(controllerIndex == 0);
}
findPreference(KEY_PREF_BTN_SCREEN).setEnabled(controllerIndex == 1);
findPreference(KEY_PREF_TOUCH_THRUST_FULL_TRAVEL).setEnabled(controllerIndex == 0);
}
Expand Down Expand Up @@ -501,6 +514,7 @@ private void resetPreference(String pKey, boolean pDefaultValue) {
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
checkGyroSensors();
}

@Override
Expand Down

0 comments on commit 2fd75d5

Please sign in to comment.