diff --git a/res/values/strings.xml b/res/values/strings.xml index 9a8990a2..1bccfae9 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -75,6 +75,9 @@ mousepad_scroll_direction gyro_mouse_enabled mouse_buttons_enabled + Enable gyroscope mouse + Gyroscope sensitivity + gyro_mouse_sensitivity Left click Right click diff --git a/res/xml/mousepadplugin_preferences.xml b/res/xml/mousepadplugin_preferences.xml index e31f67d9..37d4f272 100644 --- a/res/xml/mousepadplugin_preferences.xml +++ b/res/xml/mousepadplugin_preferences.xml @@ -61,7 +61,14 @@ android:id="@+id/gyro_mouse_enabled" android:defaultValue="false" android:key="@string/gyro_mouse_enabled" - android:title="Gyro mouse" /> + android:title="@string/gyro_mouse_enabled_title" /> + + -0.25) { X = 0; } else { - X = X * mCurrentSensitivity * displayDpiMultiplier; + X = X * (gyroscopeSensitivity/100.0f); } if (Y < 0.25 && Y > -0.25) { Y = 0; } else { - Y = Y * mCurrentSensitivity * displayDpiMultiplier; + Y = Y * (gyroscopeSensitivity/100.0f); } final float nX = X; final float nY = Y; - BackgroundService.RunWithPlugin(this, deviceId, MousePadPlugin.class, plugin -> { - plugin.sendMouseDelta(nX, nY); - }); + BackgroundService.RunWithPlugin(this, deviceId, MousePadPlugin.class, plugin -> plugin.sendMouseDelta(nX, nY)); } @Override @@ -123,6 +130,10 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); + findViewById(R.id.mouse_click_left).setOnClickListener(v -> sendLeftClick()); + findViewById(R.id.mouse_click_middle).setOnClickListener(v -> sendMiddleClick()); + findViewById(R.id.mouse_click_right).setOnClickListener(v -> sendRightClick()); + deviceId = getIntent().getStringExtra("deviceId"); getWindow().getDecorView().setHapticFeedbackEnabled(true); @@ -136,57 +147,13 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect keyListenerView.setDeviceId(deviceId); prefs = PreferenceManager.getDefaultSharedPreferences(this); + prefs.registerOnSharedPreferenceChangeListener(this); - if (prefs.getBoolean(getString(R.string.mousepad_scroll_direction), false)) { - scrollDirection = -1; - } else { - scrollDirection = 1; - } - if (mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null - && prefs.getBoolean(getString(R.string.gyro_mouse_enabled), false)) { - allowGyro = true; - } - String singleTapSetting = prefs.getString(getString(R.string.mousepad_single_tap_key), - getString(R.string.mousepad_default_single)); - String doubleTapSetting = prefs.getString(getString(R.string.mousepad_double_tap_key), - getString(R.string.mousepad_default_double)); - String tripleTapSetting = prefs.getString(getString(R.string.mousepad_triple_tap_key), - getString(R.string.mousepad_default_triple)); - String sensitivitySetting = prefs.getString(getString(R.string.mousepad_sensitivity_key), - getString(R.string.mousepad_default_sensitivity)); - - String accelerationProfileName = prefs.getString(getString(R.string.mousepad_acceleration_profile_key), - getString(R.string.mousepad_default_acceleration_profile)); - - mPointerAccelerationProfile = PointerAccelerationProfileFactory.getProfileWithName(accelerationProfileName); - - singleTapAction = ClickType.fromString(singleTapSetting); - doubleTapAction = ClickType.fromString(doubleTapSetting); - tripleTapAction = ClickType.fromString(tripleTapSetting); + applyPrefs(); //Technically xdpi and ydpi should be handled separately, //but since ydpi is usually almost equal to xdpi, only xdpi is used for the multiplier. displayDpiMultiplier = StandardDpi / getResources().getDisplayMetrics().xdpi; - switch (sensitivitySetting) { - case "slowest": - mCurrentSensitivity = 0.2f; - break; - case "aboveSlowest": - mCurrentSensitivity = 0.5f; - break; - case "default": - mCurrentSensitivity = 1.0f; - break; - case "aboveDefault": - mCurrentSensitivity = 1.5f; - break; - case "fastest": - mCurrentSensitivity = 2.0f; - break; - default: - mCurrentSensitivity = 1.0f; - return; - } final View decorView = getWindow().getDecorView(); decorView.setOnSystemUiVisibilityChangeListener(visibility -> { @@ -205,19 +172,13 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect @Override protected void onResume() { + applyPrefs(); + if (allowGyro && !gyroEnabled) { mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME); gyroEnabled = true; } - if (prefs.getBoolean(getString(R.string.mousepad_mouse_buttons_enabled_pref), true)) { - findViewById(R.id.mouse_buttons).setVisibility(View.VISIBLE); - findViewById(R.id.mouse_click_left).setOnClickListener(v -> sendLeftClick()); - findViewById(R.id.mouse_click_middle).setOnClickListener(v -> sendMiddleClick()); - findViewById(R.id.mouse_click_right).setOnClickListener(v -> sendRightClick()); - } else { - findViewById(R.id.mouse_buttons).setVisibility(View.GONE); - } invalidateMenu(); super.onResume(); @@ -232,7 +193,8 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect super.onPause(); } - @Override protected void onStop() { + @Override + protected void onStop() { if (gyroEnabled) { mSensorManager.unregisterListener(this); gyroEnabled = false; @@ -240,6 +202,12 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect super.onStop(); } + @Override + protected void onDestroy() { + prefs.unregisterOnSharedPreferenceChangeListener(this); + super.onDestroy(); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); @@ -465,6 +433,11 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect return true; } + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (prefsApplied) prefsApplied = false; + } + private void sendLeftClick() { BackgroundService.RunWithPlugin(this, deviceId, MousePadPlugin.class, MousePadPlugin::sendLeftClick); @@ -495,6 +468,70 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect startActivity(intent); } + private void applyPrefs() { + if (prefsApplied) return; + + if (prefs.getBoolean(getString(R.string.mousepad_scroll_direction), false)) { + scrollDirection = -1; + } else { + scrollDirection = 1; + } + + allowGyro = isGyroSensorAvailable() && prefs.getBoolean(getString(R.string.gyro_mouse_enabled), false); + if (allowGyro) gyroscopeSensitivity = prefs.getInt(getString(R.string.gyro_mouse_sensitivity), 100); + + String singleTapSetting = prefs.getString(getString(R.string.mousepad_single_tap_key), + getString(R.string.mousepad_default_single)); + String doubleTapSetting = prefs.getString(getString(R.string.mousepad_double_tap_key), + getString(R.string.mousepad_default_double)); + String tripleTapSetting = prefs.getString(getString(R.string.mousepad_triple_tap_key), + getString(R.string.mousepad_default_triple)); + String sensitivitySetting = prefs.getString(getString(R.string.mousepad_sensitivity_key), + getString(R.string.mousepad_default_sensitivity)); + + String accelerationProfileName = prefs.getString(getString(R.string.mousepad_acceleration_profile_key), + getString(R.string.mousepad_default_acceleration_profile)); + + mPointerAccelerationProfile = PointerAccelerationProfileFactory.getProfileWithName(accelerationProfileName); + + singleTapAction = ClickType.fromString(singleTapSetting); + doubleTapAction = ClickType.fromString(doubleTapSetting); + tripleTapAction = ClickType.fromString(tripleTapSetting); + + switch (sensitivitySetting) { + case "slowest": + mCurrentSensitivity = 0.2f; + break; + case "aboveSlowest": + mCurrentSensitivity = 0.5f; + break; + case "default": + mCurrentSensitivity = 1.0f; + break; + case "aboveDefault": + mCurrentSensitivity = 1.5f; + break; + case "fastest": + mCurrentSensitivity = 2.0f; + break; + default: + mCurrentSensitivity = 1.0f; + return; + } + + if (prefs.getBoolean(getString(R.string.mousepad_mouse_buttons_enabled_pref), true)) { + findViewById(R.id.mouse_buttons).setVisibility(View.VISIBLE); + } else { + findViewById(R.id.mouse_buttons).setVisibility(View.GONE); + } + + prefsApplied = true; + } + + private boolean isGyroSensorAvailable() { + return mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null; + } + @Override public boolean onSupportNavigateUp() { super.onBackPressed();