Skip to content

Commit a781b20

Browse files
SPenActions: Add support for batched motion packets
Seen in newer firmware versions of the S Pen. Co-authored-by: programminghoch10 <hoch10@kabelbw.de> Change-Id: Ic4400ae262dbae3bfb2f3511e3157df099d0f3c2
1 parent 522e93a commit a781b20

2 files changed

Lines changed: 50 additions & 24 deletions

File tree

packages/SPenActions/src/org/lineageos/spenactions/MotionEvent.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 The LineageOS Project
2+
* SPDX-FileCopyrightText: 2021-2026 The LineageOS Project
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -37,13 +37,18 @@ public short getDY() {
3737
return mDy;
3838
}
3939

40-
public static MotionEvent fromTypeData(int type, byte[] data) {
40+
public static MotionEvent fromData(byte[] data, int offset) {
41+
if (data == null || data.length < offset + 5) {
42+
return null;
43+
}
44+
45+
int type = data[offset] & 0xFF;
4146
if (type != 0x0F) {
4247
return null;
4348
}
4449

45-
short dx = extractShortValue(data, 1);
46-
short dy = extractShortValue(data, 3);
50+
short dx = extractShortValue(data, offset + 1);
51+
short dy = extractShortValue(data, offset + 3);
4752

4853
return new MotionEvent(Action.MOVE, dx, dy);
4954
}

packages/SPenActions/src/org/lineageos/spenactions/SPenGattCallback.java

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,35 +139,56 @@ private void setCharacteristicNotification(BluetoothGatt gatt,
139139
}
140140

141141
private void handleButtonEvent(byte[] data) {
142+
if (data == null || data.length == 0) {
143+
return;
144+
}
145+
142146
int type = data[0] & 0xFF;
143147

144148
if (DEBUG) Log.d(LOG_TAG, "BUTTON_EVENT raw: len=" + data.length +
145149
" data=" + Arrays.toString(data));
146150

147-
ButtonAction button = ButtonAction.fromType(type);
148-
if (button != null) {
149-
switch (button.getAction()) {
150-
case DOWN:
151-
mButtonDownTime = SystemClock.uptimeMillis();
152-
break;
153-
case UP:
154-
if (!mButtonHadGesture) {
155-
SPenMode mode = SPenMode.valueOfPref(SettingsUtils.getSwitchPreference(
156-
mContext, SettingsUtils.SPEN_MODE, "0"));
157-
// Can be used for e.g taking photos
158-
sendEvent(mButtonDownTime, KeyEvent.ACTION_DOWN, mode.getButtonKey());
159-
sendEvent(KeyEvent.ACTION_UP, mode.getButtonKey());
160-
}
161-
mButtonHadGesture = false;
162-
break;
163-
default:
164-
break;
151+
// Batched packets (newer firmware)
152+
if (type == 0xF0 && data.length >= 8) {
153+
// Motion records start at offset 2
154+
for (int i = 2; i + 5 < data.length; i += 6) {
155+
MotionEvent move = MotionEvent.fromData(data, i);
156+
handleGesture(move);
165157
}
166158
return;
167159
}
168160

169-
MotionEvent move = MotionEvent.fromTypeData(type, data);
170-
handleGesture(move);
161+
// Legacy motion handling (for old firmware)
162+
if (type == 0x0F && data.length >= 5) {
163+
MotionEvent move = MotionEvent.fromData(data, 0);
164+
handleGesture(move);
165+
return;
166+
}
167+
168+
// Button events (single-byte packets)
169+
ButtonAction button = ButtonAction.fromType(type);
170+
if (button == null) {
171+
Log.w(LOG_TAG, "Unknown BUTTON_EVENT type: " + type);
172+
return;
173+
}
174+
175+
switch (button.getAction()) {
176+
case DOWN:
177+
mButtonDownTime = SystemClock.uptimeMillis();
178+
break;
179+
case UP:
180+
if (!mButtonHadGesture) {
181+
SPenMode mode = SPenMode.valueOfPref(SettingsUtils.getSwitchPreference(
182+
mContext, SettingsUtils.SPEN_MODE, "0"));
183+
// Can be used for e.g taking photos
184+
sendEvent(mButtonDownTime, KeyEvent.ACTION_DOWN, mode.getButtonKey());
185+
sendEvent(KeyEvent.ACTION_UP, mode.getButtonKey());
186+
}
187+
mButtonHadGesture = false;
188+
break;
189+
default:
190+
break;
191+
}
171192
}
172193

173194
private void handleGesture(MotionEvent move) {

0 commit comments

Comments
 (0)