2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 06:05:12 +00:00

Fix applying preferences changes when returned from plugin settings

This commit is contained in:
Dmitry Yudin
2023-04-23 22:50:05 +02:00
parent 8639938584
commit f5b3523ec6

View File

@@ -33,7 +33,13 @@ import org.kde.kdeconnect_tp.R;
import java.util.Objects;
public class MousePadActivity extends AppCompatActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, MousePadGestureDetector.OnGestureListener, SensorEventListener {
public class MousePadActivity
extends AppCompatActivity
implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener,
MousePadGestureDetector.OnGestureListener,
SensorEventListener,
SharedPreferences.OnSharedPreferenceChangeListener {
private String deviceId;
private final static float MinDistanceToSendScroll = 2.5f; // touch gesture scroll
@@ -63,6 +69,8 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
private SharedPreferences prefs = null;
private boolean prefsApplied = false;
enum ClickType {
LEFT, RIGHT, MIDDLE, NONE;
@@ -123,6 +131,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 +148,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 +173,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 +194,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 +203,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 +434,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 +469,69 @@ 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);
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();