From 2c104cf650052b544edc384f55798aacb44efbeb Mon Sep 17 00:00:00 2001 From: Javier Date: Sun, 16 Feb 2020 23:52:36 +0100 Subject: fix non-sticky service --- app/src/main/java/com/javispedro/vndroid/KeyEventOutput.java | 12 +++++++++--- app/src/main/java/com/javispedro/vndroid/ServerService.java | 9 ++++----- .../main/java/com/javispedro/vndroid/SettingsActivity.java | 12 ++++++++---- app/src/main/res/xml/controlservice.xml | 6 ++++-- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/javispedro/vndroid/KeyEventOutput.java b/app/src/main/java/com/javispedro/vndroid/KeyEventOutput.java index 3ec4319..f72fe49 100644 --- a/app/src/main/java/com/javispedro/vndroid/KeyEventOutput.java +++ b/app/src/main/java/com/javispedro/vndroid/KeyEventOutput.java @@ -22,6 +22,8 @@ public class KeyEventOutput implements KeyActionHandler { ControlService service = ControlService.getInstance(); if (service != null) { return service.findFocus(AccessibilityNodeInfo.FOCUS_INPUT); + } else { + Log.w(TAG, "accessibility service not enabled, cannot control keyboard"); } return null; } @@ -32,7 +34,7 @@ public class KeyEventOutput implements KeyActionHandler { } public void postKeyEvent(int key, boolean state) { - if (state) Log.d(TAG, "keysym pressed: " + Integer.toHexString(key)); + if (state) Log.d(TAG, "keysym pressed: 0x" + Integer.toHexString(key)); for (KeyHandler handler : handlers) { boolean handled; if (state) { @@ -49,8 +51,12 @@ public class KeyEventOutput implements KeyActionHandler { @Override public void postText(CharSequence text) { AccessibilityNodeInfo node = getFocusNode(); - if (node == null || !node.isEditable()) { - Log.d(TAG, "no input focus or not editable"); + if (node == null) { + Log.d(TAG, "no input focus"); + return; + } + if (!node.isEditable()) { + Log.d(TAG, "input focus is not editable"); return; } diff --git a/app/src/main/java/com/javispedro/vndroid/ServerService.java b/app/src/main/java/com/javispedro/vndroid/ServerService.java index b39f175..f8c8f9b 100644 --- a/app/src/main/java/com/javispedro/vndroid/ServerService.java +++ b/app/src/main/java/com/javispedro/vndroid/ServerService.java @@ -23,7 +23,7 @@ import java.util.List; public class ServerService extends Service { private static final String TAG = ServerService.class.getSimpleName(); - public static final String ACTION_START_SERVER = "ACTION_START_SERVER"; + public static final String ACTION_INIT_SERVICE = "ACTION_INIT_SERVICE"; public static final String ACTION_STOP_SERVER = "ACTION_STOP_SERVER"; public class ServerBinder extends Binder { @@ -67,11 +67,10 @@ public class ServerService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { - Log.d(TAG, "onStartCommand intent=" + intent); switch (intent.getAction()) { - case ACTION_START_SERVER: - startServer(); - return START_REDELIVER_INTENT; + case ACTION_INIT_SERVICE: + // Nothing to do + return START_STICKY; case ACTION_STOP_SERVER: stopServer(); return START_NOT_STICKY; diff --git a/app/src/main/java/com/javispedro/vndroid/SettingsActivity.java b/app/src/main/java/com/javispedro/vndroid/SettingsActivity.java index 676af3d..f595eac 100644 --- a/app/src/main/java/com/javispedro/vndroid/SettingsActivity.java +++ b/app/src/main/java/com/javispedro/vndroid/SettingsActivity.java @@ -2,7 +2,6 @@ package com.javispedro.vndroid; import android.app.Activity; import android.content.ComponentName; -import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.media.projection.MediaProjectionManager; @@ -83,7 +82,6 @@ public class SettingsActivity extends AppCompatActivity { } private void setServerEnabled(boolean state) { - Log.d(TAG, "setServerEnabled: " + state); ServerService server = serverConnection.getServer(); if (state) { if (server == null) { @@ -183,12 +181,18 @@ public class SettingsActivity extends AppCompatActivity { public void bind(Activity activity, ServerService.ServerStatusCallback callback) { this.callback = callback; - activity.bindService(new Intent(activity, ServerService.class), this, Context.BIND_AUTO_CREATE); + Intent intent = new Intent(activity, ServerService.class); + intent.setAction(ServerService.ACTION_INIT_SERVICE); + activity.startService(intent); + activity.bindService(intent, this, 0); } public void close(Activity activity) { - server.setServerStatusCallback(null); + if (server != null) { + server.setServerStatusCallback(null); + } activity.unbindService(this); + callback = null; } public boolean connected() { diff --git a/app/src/main/res/xml/controlservice.xml b/app/src/main/res/xml/controlservice.xml index 85078a7..ef6071e 100644 --- a/app/src/main/res/xml/controlservice.xml +++ b/app/src/main/res/xml/controlservice.xml @@ -1,6 +1,8 @@ \ No newline at end of file + android:accessibilityFlags="flagDefault|flagRetrieveInteractiveWindows" + /> \ No newline at end of file -- cgit v1.2.3