diff --git a/src/se/bitcraze/crazyfliecontrol/controller/GyroscopeController.java b/src/se/bitcraze/crazyfliecontrol/controller/GyroscopeController.java index 2f73f3e..5973cf8 100644 --- a/src/se/bitcraze/crazyfliecontrol/controller/GyroscopeController.java +++ b/src/se/bitcraze/crazyfliecontrol/controller/GyroscopeController.java @@ -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; @@ -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(); } diff --git a/src/se/bitcraze/crazyfliecontrol/prefs/PreferencesActivity.java b/src/se/bitcraze/crazyfliecontrol/prefs/PreferencesActivity.java index c449f49..9b84e76 100644 --- a/src/se/bitcraze/crazyfliecontrol/prefs/PreferencesActivity.java +++ b/src/se/bitcraze/crazyfliecontrol/prefs/PreferencesActivity.java @@ -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); @@ -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 @@ -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); @@ -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); @@ -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); } @@ -501,6 +514,7 @@ private void resetPreference(String pKey, boolean pDefaultValue) { public void onResume() { super.onResume(); getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); + checkGyroSensors(); } @Override