2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 21:55:10 +00:00

Add gyro switch and mouse buttons

This commit is contained in:
Dmitry Yudin
2023-04-07 02:17:37 +02:00
committed by Albert Vaca Cintora
parent 81fe7adba5
commit c22453afca
3 changed files with 69 additions and 24 deletions

View File

@@ -18,6 +18,15 @@
android:orientation="vertical" android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"> app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/gyro_mouse_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textAppearance="?attr/textAppearanceLabelMedium"
android:paddingHorizontal="@dimen/activity_horizontal_margin"
android:text="Gyro mouse" />
<TextView <TextView
style="@android:style/TextAppearance.Medium" style="@android:style/TextAppearance.Medium"
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -27,12 +36,39 @@
android:padding="12dip" android:padding="12dip"
android:text="@string/mousepad_info" /> android:text="@string/mousepad_info" />
<view <org.kde.kdeconnect.Plugins.MousePadPlugin.KeyListenerView
android:id="@+id/keyListener" android:id="@+id/keyListener"
class="org.kde.kdeconnect.Plugins.MousePadPlugin.KeyListenerView"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="68dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/mouse_click_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
style="@style/KdeConnectButton.IconButton" />
<com.google.android.material.button.MaterialButton
android:id="@+id/mouse_click_middle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
style="@style/KdeConnectButton.IconButton.Secondary" />
<com.google.android.material.button.MaterialButton
android:id="@+id/mouse_click_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
style="@style/KdeConnectButton.IconButton" />
</LinearLayout>
</RelativeLayout> </RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@@ -13,14 +13,5 @@
android:icon="@drawable/ic_edit_note_24dp" android:icon="@drawable/ic_edit_note_24dp"
android:title="@string/open_compose_send" android:title="@string/open_compose_send"
kdeconnect:showAsAction="ifRoom" /> kdeconnect:showAsAction="ifRoom" />
<item
android:id="@+id/menu_right_click"
android:title="@string/right_click"
kdeconnect:showAsAction="never" />
<item
android:id="@+id/menu_middle_click"
android:title="@string/middle_click"
kdeconnect:showAsAction="never" />
</menu> </menu>

View File

@@ -28,8 +28,8 @@ import java.util.List;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import com.google.android.material.materialswitch.MaterialSwitch;
import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect.BackgroundService;
import org.kde.kdeconnect.UserInterface.ThemeUtil;
import org.kde.kdeconnect_tp.R; import org.kde.kdeconnect_tp.R;
import java.util.Objects; import java.util.Objects;
@@ -140,8 +140,17 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
} else { } else {
scrollDirection = 1; scrollDirection = 1;
} }
if ((prefs.getBoolean(getString(R.string.gyro_mouse_enabled), false)) && (mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null)) { boolean isGyroPrefEnabled = prefs.getBoolean(getString(R.string.gyro_mouse_enabled), false);
allowGyro = true; MaterialSwitch gyroSwitch = findViewById(R.id.gyro_mouse_switch);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null) {
if (isGyroPrefEnabled) allowGyro = true;
gyroSwitch.setChecked(isGyroPrefEnabled);
gyroSwitch.setOnCheckedChangeListener((v, checked) -> {
setAllowGyro(checked);
prefs.edit().putBoolean(getString(R.string.gyro_mouse_enabled), checked).apply();
});
} else {
gyroSwitch.setVisibility(View.GONE);
} }
String singleTapSetting = prefs.getString(getString(R.string.mousepad_single_tap_key), String singleTapSetting = prefs.getString(getString(R.string.mousepad_single_tap_key),
getString(R.string.mousepad_default_single)); getString(R.string.mousepad_default_single));
@@ -185,6 +194,10 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
return; return;
} }
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());
final View decorView = getWindow().getDecorView(); final View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(visibility -> { decorView.setOnSystemUiVisibilityChangeListener(visibility -> {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
@@ -202,7 +215,7 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
@Override @Override
protected void onResume() { protected void onResume() {
if (allowGyro == true && gyroEnabled == false) { if (allowGyro && !gyroEnabled) {
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME); mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
gyroEnabled = true; gyroEnabled = true;
} }
@@ -211,7 +224,7 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
@Override @Override
protected void onPause() { protected void onPause() {
if (gyroEnabled == true) { if (gyroEnabled) {
mSensorManager.unregisterListener(this); mSensorManager.unregisterListener(this);
gyroEnabled = false; gyroEnabled = false;
} }
@@ -219,7 +232,7 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
} }
@Override protected void onStop() { @Override protected void onStop() {
if (gyroEnabled == true) { if (gyroEnabled) {
mSensorManager.unregisterListener(this); mSensorManager.unregisterListener(this);
gyroEnabled = false; gyroEnabled = false;
} }
@@ -236,13 +249,7 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId(); int id = item.getItemId();
if (id == R.id.menu_right_click) { if (id == R.id.menu_show_keyboard) {
sendRightClick();
return true;
} else if (id == R.id.menu_middle_click) {
sendMiddleClick();
return true;
} else if (id == R.id.menu_show_keyboard) {
BackgroundService.RunWithPlugin(this, deviceId, MousePadPlugin.class, plugin -> { BackgroundService.RunWithPlugin(this, deviceId, MousePadPlugin.class, plugin -> {
if (plugin.isKeyboardEnabled()) { if (plugin.isKeyboardEnabled()) {
showKeyboard(); showKeyboard();
@@ -474,5 +481,16 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
super.onBackPressed(); super.onBackPressed();
return true; return true;
} }
private void setAllowGyro(Boolean enabled) {
if (allowGyro == enabled) return;
if (enabled) {
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
} else {
mSensorManager.unregisterListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE));
}
allowGyro = enabled;
gyroEnabled = enabled;
}
} }