Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reflection to detect stylus via com.htc.pen.PenEvent #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/com/google/android/apps/markers/Slate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.apps.markers;

import java.util.ArrayList;
import java.lang.reflect.Method;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
Expand Down Expand Up @@ -124,6 +125,21 @@ public class Slate extends View {
private int mMemClass;
private boolean mLowMem;

private static Class<?> sHTCPenEventClass = null;
private static Method sHTCIsPenEventMethod = null;
static {
if ("HTC".equalsIgnoreCase(Build.MANUFACTURER) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
try {
sHTCPenEventClass = Class.forName(
"com.htc.pen.PenEvent");
sHTCIsPenEventMethod = sHTCPenEventClass.getDeclaredMethod(
"isPenEvent", MotionEvent.class);
} catch (Exception e) {
}
}
}

public interface SlateListener {
void strokeStarted();
void strokeEnded();
Expand Down Expand Up @@ -863,20 +879,31 @@ final static int getToolTypeCompat(MotionEvent me, int index) {
if (hasToolType()) {
return me.getToolType(index);
}

// dirty hack for the HTC Flyer
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
if ("flyer".equals(Build.HARDWARE)) {
if (me.getSize(index) <= 0.1f) {
// with very high probability this is the stylus
return MotionEvent.TOOL_TYPE_STYLUS;
}
}

if (isHTCPenEvent(me)) {
return MotionEvent.TOOL_TYPE_STYLUS;
}

return MotionEvent.TOOL_TYPE_FINGER;
}

/**
* Use reflection to determine if this is a pen event from certain HTC
* devices such as the Flyer and Jetstream.
*/
private static boolean isHTCPenEvent(MotionEvent me) {
if (sHTCPenEventClass == null || sHTCIsPenEventMethod == null) {
return false;
}

try {
return (Boolean) sHTCIsPenEventMethod.invoke(sHTCPenEventClass, me);
} catch (Exception e) {
}

return false;
}

PointF getCenter(MotionEvent event, PointF out) {
int P = event.getPointerCount();
PointF pt = ((out == null) ? new PointF() : out);
Expand Down