Differential D10905 Diff 28488 src/org/kde/kdeconnect/Plugins/RemoteKeyboardPlugin/RemoteKeyboardPlugin.java
Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/RemoteKeyboardPlugin/RemoteKeyboardPlugin.java
Show First 20 Lines • Show All 46 Lines • ▼ Show 20 Line(s) | 44 | public class RemoteKeyboardPlugin extends Plugin { | |||
---|---|---|---|---|---|
47 | public final static String PACKAGE_TYPE_MOUSEPAD_ECHO = "kdeconnect.mousepad.echo"; | 47 | public final static String PACKAGE_TYPE_MOUSEPAD_ECHO = "kdeconnect.mousepad.echo"; | ||
48 | public final static String PACKAGE_TYPE_MOUSEPAD_KEYBOARDSTATE = "kdeconnect.mousepad.keyboardstate"; | 48 | public final static String PACKAGE_TYPE_MOUSEPAD_KEYBOARDSTATE = "kdeconnect.mousepad.keyboardstate"; | ||
49 | 49 | | |||
50 | /** | 50 | /** | ||
51 | * Track and expose plugin instances to allow for a 'connected'-indicator in the IME: | 51 | * Track and expose plugin instances to allow for a 'connected'-indicator in the IME: | ||
52 | */ | 52 | */ | ||
53 | private static ArrayList<RemoteKeyboardPlugin> instances = new ArrayList<RemoteKeyboardPlugin>(); | 53 | private static ArrayList<RemoteKeyboardPlugin> instances = new ArrayList<RemoteKeyboardPlugin>(); | ||
54 | private static ReentrantLock instancesLock = new ReentrantLock(true); | 54 | private static ReentrantLock instancesLock = new ReentrantLock(true); | ||
55 | | ||||
55 | public static ArrayList<RemoteKeyboardPlugin> getInstances() { | 56 | public static ArrayList<RemoteKeyboardPlugin> getInstances() { | ||
56 | return instances; | 57 | return instances; | ||
57 | } | 58 | } | ||
59 | | ||||
58 | public static ArrayList<RemoteKeyboardPlugin> acquireInstances() { | 60 | public static ArrayList<RemoteKeyboardPlugin> acquireInstances() { | ||
59 | instancesLock.lock(); | 61 | instancesLock.lock(); | ||
60 | return getInstances(); | 62 | return getInstances(); | ||
61 | } | 63 | } | ||
64 | | ||||
62 | public static ArrayList<RemoteKeyboardPlugin> releaseInstances() { | 65 | public static ArrayList<RemoteKeyboardPlugin> releaseInstances() { | ||
63 | instancesLock.unlock(); | 66 | instancesLock.unlock(); | ||
64 | return getInstances(); | 67 | return getInstances(); | ||
65 | } | 68 | } | ||
69 | | ||||
66 | public static boolean isConnected() { | 70 | public static boolean isConnected() { | ||
67 | return instances.size() > 0; | 71 | return instances.size() > 0; | ||
68 | } | 72 | } | ||
69 | 73 | | |||
70 | private static SparseIntArray specialKeyMap = new SparseIntArray(); | 74 | private static SparseIntArray specialKeyMap = new SparseIntArray(); | ||
71 | 75 | | |||
72 | static { | 76 | static { | ||
73 | int i = 0; | 77 | int i = 0; | ||
▲ Show 20 Lines • Show All 131 Lines • ▼ Show 20 Line(s) | |||||
205 | } | 209 | } | ||
206 | 210 | | |||
207 | private int currentCursorPos(ExtractedText extractedText) { | 211 | private int currentCursorPos(ExtractedText extractedText) { | ||
208 | if (extractedText != null) | 212 | if (extractedText != null) | ||
209 | return extractedText.selectionEnd; | 213 | return extractedText.selectionEnd; | ||
210 | return -1; | 214 | return -1; | ||
211 | } | 215 | } | ||
212 | 216 | | |||
213 | private Pair<Integer,Integer> currentSelection(ExtractedText extractedText) { | 217 | private Pair<Integer, Integer> currentSelection(ExtractedText extractedText) { | ||
214 | if (extractedText != null) | 218 | if (extractedText != null) | ||
215 | return new Pair<>(extractedText.selectionStart, extractedText.selectionEnd); | 219 | return new Pair<>(extractedText.selectionStart, extractedText.selectionEnd); | ||
216 | return new Pair<>(-1, -1); | 220 | return new Pair<>(-1, -1); | ||
217 | } | 221 | } | ||
218 | 222 | | |||
219 | private boolean handleSpecialKey(int key, boolean shift, boolean ctrl, boolean alt) { | 223 | private boolean handleSpecialKey(int key, boolean shift, boolean ctrl, boolean alt) { | ||
220 | int keyEvent = specialKeyMap.get(key, 0); | 224 | int keyEvent = specialKeyMap.get(key, 0); | ||
221 | if (keyEvent == 0) | 225 | if (keyEvent == 0) | ||
222 | return false; | 226 | return false; | ||
223 | InputConnection inputConn = RemoteKeyboardService.instance.getCurrentInputConnection(); | 227 | InputConnection inputConn = RemoteKeyboardService.instance.getCurrentInputConnection(); | ||
224 | // Log.d("RemoteKeyboardPlugin", "Handling special key " + key + " translated to " + keyEvent + " shift=" + shift + " ctrl=" + ctrl + " alt=" + alt); | 228 | // Log.d("RemoteKeyboardPlugin", "Handling special key " + key + " translated to " + keyEvent + " shift=" + shift + " ctrl=" + ctrl + " alt=" + alt); | ||
225 | 229 | | |||
226 | // special sequences: | 230 | // special sequences: | ||
227 | if (ctrl && (keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT)) { | 231 | if (ctrl && (keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT)) { | ||
228 | // Ctrl + right -> next word | 232 | // Ctrl + right -> next word | ||
229 | ExtractedText extractedText = inputConn.getExtractedText(new ExtractedTextRequest(), 0); | 233 | ExtractedText extractedText = inputConn.getExtractedText(new ExtractedTextRequest(), 0); | ||
230 | int pos = getCharPos(extractedText, ' ', keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT); | 234 | int pos = getCharPos(extractedText, ' ', keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT); | ||
231 | if (pos == -1) | 235 | if (pos == -1) | ||
232 | pos = currentTextLength(extractedText); | 236 | pos = currentTextLength(extractedText); | ||
233 | else | 237 | else | ||
234 | pos++; | 238 | pos++; | ||
235 | int startPos = pos; | 239 | int startPos = pos; | ||
236 | int endPos = pos; | 240 | int endPos = pos; | ||
237 | if (shift) { // Shift -> select word (otherwise jump) | 241 | if (shift) { // Shift -> select word (otherwise jump) | ||
238 | Pair<Integer,Integer> sel = currentSelection(extractedText); | 242 | Pair<Integer, Integer> sel = currentSelection(extractedText); | ||
239 | int cursor = currentCursorPos(extractedText); | 243 | int cursor = currentCursorPos(extractedText); | ||
240 | // Log.d("RemoteKeyboardPlugin", "Selection (to right): " + sel.first + " / " + sel.second + " cursor: " + cursor); | 244 | // Log.d("RemoteKeyboardPlugin", "Selection (to right): " + sel.first + " / " + sel.second + " cursor: " + cursor); | ||
241 | startPos = cursor; | 245 | startPos = cursor; | ||
242 | if (sel.first < cursor || // active selection from left to right -> grow | 246 | if (sel.first < cursor || // active selection from left to right -> grow | ||
243 | sel.first > sel.second) // active selection from right to left -> shrink | 247 | sel.first > sel.second) // active selection from right to left -> shrink | ||
244 | startPos = sel.first; | 248 | startPos = sel.first; | ||
245 | } | 249 | } | ||
246 | inputConn.setSelection(startPos, endPos); | 250 | inputConn.setSelection(startPos, endPos); | ||
247 | } else if (ctrl && keyEvent == KeyEvent.KEYCODE_DPAD_LEFT) { | 251 | } else if (ctrl && keyEvent == KeyEvent.KEYCODE_DPAD_LEFT) { | ||
248 | // Ctrl + left -> previous word | 252 | // Ctrl + left -> previous word | ||
249 | ExtractedText extractedText = inputConn.getExtractedText(new ExtractedTextRequest(), 0); | 253 | ExtractedText extractedText = inputConn.getExtractedText(new ExtractedTextRequest(), 0); | ||
250 | int pos = getCharPos(extractedText, ' ', keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT); | 254 | int pos = getCharPos(extractedText, ' ', keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT); | ||
251 | if (pos == -1) | 255 | if (pos == -1) | ||
252 | pos = 0; | 256 | pos = 0; | ||
253 | else | 257 | else | ||
254 | pos++; | 258 | pos++; | ||
255 | int startPos = pos; | 259 | int startPos = pos; | ||
256 | int endPos = pos; | 260 | int endPos = pos; | ||
257 | if (shift) { | 261 | if (shift) { | ||
258 | Pair<Integer,Integer> sel = currentSelection(extractedText); | 262 | Pair<Integer, Integer> sel = currentSelection(extractedText); | ||
259 | int cursor = currentCursorPos(extractedText); | 263 | int cursor = currentCursorPos(extractedText); | ||
260 | // Log.d("RemoteKeyboardPlugin", "Selection (to left): " + sel.first + " / " + sel.second + " cursor: " + cursor); | 264 | // Log.d("RemoteKeyboardPlugin", "Selection (to left): " + sel.first + " / " + sel.second + " cursor: " + cursor); | ||
261 | startPos = cursor; | 265 | startPos = cursor; | ||
262 | if (cursor < sel.first || // active selection from right to left -> grow | 266 | if (cursor < sel.first || // active selection from right to left -> grow | ||
263 | sel.first < sel.second) // active selection from right to left -> shrink | 267 | sel.first < sel.second) // active selection from right to left -> shrink | ||
264 | startPos = sel.first; | 268 | startPos = sel.first; | ||
265 | } | 269 | } | ||
266 | inputConn.setSelection(startPos, endPos); | 270 | inputConn.setSelection(startPos, endPos); | ||
Show All 14 Lines | 284 | } else if (keyEvent == KeyEvent.KEYCODE_NUMPAD_ENTER | |||
281 | || keyEvent == KeyEvent.KEYCODE_ENTER) { | 285 | || keyEvent == KeyEvent.KEYCODE_ENTER) { | ||
282 | // Enter key | 286 | // Enter key | ||
283 | EditorInfo editorInfo = RemoteKeyboardService.instance.getCurrentInputEditorInfo(); | 287 | EditorInfo editorInfo = RemoteKeyboardService.instance.getCurrentInputEditorInfo(); | ||
284 | // Log.d("RemoteKeyboardPlugin", "Enter: " + editorInfo.imeOptions); | 288 | // Log.d("RemoteKeyboardPlugin", "Enter: " + editorInfo.imeOptions); | ||
285 | if (editorInfo != null | 289 | if (editorInfo != null | ||
286 | && (((editorInfo.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) | 290 | && (((editorInfo.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) | ||
287 | || ctrl)) { // Ctrl+Return overrides IME_FLAG_NO_ENTER_ACTION (FIXME: make configurable?) | 291 | || ctrl)) { // Ctrl+Return overrides IME_FLAG_NO_ENTER_ACTION (FIXME: make configurable?) | ||
288 | // check for special DONE/GO/etc actions first: | 292 | // check for special DONE/GO/etc actions first: | ||
289 | int[] actions = { EditorInfo.IME_ACTION_GO, EditorInfo.IME_ACTION_NEXT, | 293 | int[] actions = {EditorInfo.IME_ACTION_GO, EditorInfo.IME_ACTION_NEXT, | ||
290 | EditorInfo.IME_ACTION_SEND, EditorInfo.IME_ACTION_SEARCH, | 294 | EditorInfo.IME_ACTION_SEND, EditorInfo.IME_ACTION_SEARCH, | ||
291 | EditorInfo.IME_ACTION_DONE}; // note: DONE should be last or we might hide the ime instead of "go" | 295 | EditorInfo.IME_ACTION_DONE}; // note: DONE should be last or we might hide the ime instead of "go" | ||
292 | for (int i = 0; i < actions.length; i++) { | 296 | for (int i = 0; i < actions.length; i++) { | ||
293 | if ((editorInfo.imeOptions & actions[i]) == actions[i]) { | 297 | if ((editorInfo.imeOptions & actions[i]) == actions[i]) { | ||
294 | // Log.d("RemoteKeyboardPlugin", "Enter-action: " + actions[i]); | 298 | // Log.d("RemoteKeyboardPlugin", "Enter-action: " + actions[i]); | ||
295 | inputConn.performEditorAction(actions[i]); | 299 | inputConn.performEditorAction(actions[i]); | ||
296 | return true; | 300 | return true; | ||
297 | } | 301 | } | ||
▲ Show 20 Lines • Show All 101 Lines • Show Last 20 Lines |