Commit 38caa183 authored by Albert Vaca's avatar Albert Vaca

WIP Pointer feature in presenter plugin

parent 56598124
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mpris_control_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:padding="12dp"
android:orientation="vertical">
<Space
<TextView
style="@android:style/TextAppearance.Medium"
android:id="@+id/textView"
android:layout_weight="0"
android:layout_marginBottom="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/top_space"/>
android:text="@string/presenter_lock_tip" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="6dp"
android:orientation="horizontal"
android:layout_below="@id/top_space"
android:layout_above="@id/textView">
android:layout_weight="1">
<ImageButton
android:id="@+id/previous_button"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:contentDescription="@string/mpris_rew"
android:layout_weight="0.25"
android:layout_marginRight="3dp"
android:layout_marginEnd="3dp"
android:src="@drawable/ic_previous_black"
android:theme="@style/DisableableButton" />
......@@ -33,6 +39,8 @@
android:id="@+id/next_button"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="3dp"
android:layout_marginStart="3dp"
android:layout_weight="0.25"
android:contentDescription="@string/mpris_ff"
android:src="@drawable/ic_next_black"
......@@ -40,13 +48,16 @@
</LinearLayout>
<TextView
style="@android:style/TextAppearance.Medium"
android:padding="12dip"
android:id="@+id/textView"
<Button
android:id="@+id/pointer_button"
android:layout_width="match_parent"
android:layout_marginBottom="6dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/presenter_lock_tip" />
android:layout_weight="0.30"
android:visibility="gone"
android:text="@string/presenter_pointer"
android:theme="@style/DisableableButton" />
</RelativeLayout>
</LinearLayout>
......@@ -361,4 +361,5 @@
<string name="findmyphone_preference_key_ringtone" translatable="false">findmyphone_ringtone</string>
<string name="no_app_for_opening">No suitable app found to open this file</string>
<string name="presenter_pointer">Pointer</string>
</resources>
......@@ -21,28 +21,90 @@
package org.kde.kdeconnect.Plugins.PresenterPlugin;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorEventListener2;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import org.kde.kdeconnect.BackgroundService;
import org.kde.kdeconnect.UserInterface.ThemeUtil;
import org.kde.kdeconnect_tp.R;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.media.VolumeProviderCompat;
public class PresenterActivity extends AppCompatActivity {
import static androidx.core.math.MathUtils.clamp;
public class PresenterActivity extends AppCompatActivity implements SensorEventListener {
private MediaSessionCompat mMediaSession;
private PresenterPlugin plugin;
private SensorManager sensorManager;
private float xPos, yPos;
private float xOffset, yOffset;
// TODO: Decide if move to desktop
static final float xMax = 800, yMax = 600;
static final float sensitivity = 0.05f;
static final float amplitude = 1.f;
public void gyroscopeEvent(SensorEvent event) {
xPos += -event.values[2];
yPos += -event.values[0];
float percentX = clamp((xPos - xOffset) * sensitivity, -amplitude, amplitude)/amplitude;
float percentY = clamp((yPos - yOffset) * sensitivity, -amplitude, amplitude)/amplitude;
plugin.sendPointer(percentX, percentY);
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
gyroscopeEvent(event);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//Ignored
}
void enablePointer() {
if (sensorManager != null) {
return; //Already enabled
}
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
findViewById(R.id.pointer_button).setVisibility(View.VISIBLE);
findViewById(R.id.pointer_button).setOnTouchListener((v, event) -> {
if(event.getAction() == MotionEvent.ACTION_DOWN){
yOffset = yPos;
xOffset = xPos;
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
v.performClick(); // The linter complains if this is not called
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
sensorManager.unregisterListener(this);
}
return true;
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -56,6 +118,12 @@ public class PresenterActivity extends AppCompatActivity {
this.plugin = plugin;
findViewById(R.id.next_button).setOnClickListener(v -> plugin.sendNext());
findViewById(R.id.previous_button).setOnClickListener(v -> plugin.sendPrevious());
plugin.setPointerEnabledCallback(new Handler() {
@Override
public void handleMessage(Message msg) {
enablePointer();
}
});
}));
}
@Override
......@@ -87,12 +155,18 @@ public class PresenterActivity extends AppCompatActivity {
mMediaSession.setActive(true);
return;
}
createMediaSession(); //Mediasession will keep
createMediaSession();
}
@Override
protected void onStop() {
super.onStop();
if (sensorManager != null) {
// Make sure we don't leave the listener on
sensorManager.unregisterListener(this);
}
BackgroundService.removeGuiInUseCounter(this);
if (mMediaSession != null) {
......
......@@ -24,6 +24,8 @@ package org.kde.kdeconnect.Plugins.PresenterPlugin;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import org.kde.kdeconnect.NetworkPacket;
......@@ -38,8 +40,13 @@ import static org.kde.kdeconnect.Plugins.MousePadPlugin.KeyListenerView.SpecialK
@PluginFactory.LoadablePlugin
public class PresenterPlugin extends Plugin {
private final static String PACKET_TYPE_PRESENTER = "kdeconnect.presenter";
private final static String PACKET_TYPE_PRESENTER_CONFIG = "kdeconnect.presenter.config";
private final static String PACKET_TYPE_MOUSEPAD_REQUEST = "kdeconnect.mousepad.request";
private boolean pointerEnabled = false;
private Handler pointerEnabledCallback = null;
@Override
public String getDisplayName() {
return context.getString(R.string.pref_plugin_presenter);
......@@ -72,14 +79,26 @@ public class PresenterPlugin extends Plugin {
parentActivity.startActivity(intent);
}
@Override
public boolean onPacketReceived(NetworkPacket np) {
if (np.getBoolean("presenter_enabled", false)) {
pointerEnabled = true;
if (pointerEnabledCallback != null) {
pointerEnabledCallback.dispatchMessage(new Message());
}
return true;
}
return false;
}
@Override
public String[] getSupportedPacketTypes() {
return new String[0];
return new String[]{PACKET_TYPE_PRESENTER_CONFIG};
}
@Override
public String[] getOutgoingPacketTypes() {
return new String[]{PACKET_TYPE_MOUSEPAD_REQUEST};
return new String[]{PACKET_TYPE_MOUSEPAD_REQUEST, PACKET_TYPE_PRESENTER};
}
@Override
......@@ -87,6 +106,13 @@ public class PresenterPlugin extends Plugin {
return context.getString(R.string.pref_plugin_presenter);
}
public void setPointerEnabledCallback(Handler handler) {
this.pointerEnabledCallback = handler;
if (pointerEnabled) {
handler.dispatchMessage(new Message());
}
}
public void sendNext() {
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_PAGE_DOWN));
......@@ -111,4 +137,11 @@ public class PresenterPlugin extends Plugin {
device.sendPacket(np);
}
public void sendPointer(float percentX, float percentY) {
NetworkPacket np = new NetworkPacket(PACKET_TYPE_PRESENTER);
np.set("px", percentX);
np.set("py", percentY);
device.sendPacket(np);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment