SPenSupport.java revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.content.browser;
6
7import android.content.Context;
8import android.content.pm.FeatureInfo;
9import android.os.Build;
10import android.view.MotionEvent;
11
12/**
13 * Support S-Pen event detection and conversion.
14 */
15public final class SPenSupport {
16
17    // These values are obtained from Samsung.
18    private static final int SPEN_ACTION_DOWN = 211;
19    private static final int SPEN_ACTION_UP = 212;
20    private static final int SPEN_ACTION_MOVE = 213;
21    private static final int SPEN_ACTION_CANCEL = 214;
22    private static Boolean sIsSPenSupported;
23
24    /**
25     * @return Whether SPen is supported on the device.
26     */
27    public static boolean isSPenSupported(Context context) {
28        if (sIsSPenSupported == null) {
29            sIsSPenSupported = detectSPenSupport(context);
30        }
31        return sIsSPenSupported.booleanValue();
32    }
33
34    private static boolean detectSPenSupport(Context context) {
35        if (!"SAMSUNG".equalsIgnoreCase(Build.MANUFACTURER)) {
36            return false;
37        }
38
39        final FeatureInfo[] infos = context.getPackageManager().getSystemAvailableFeatures();
40        for (FeatureInfo info : infos) {
41            if ("com.sec.feature.spen_usp".equalsIgnoreCase(info.name)) {
42                return true;
43            }
44        }
45        return false;
46    }
47
48    /**
49     * Convert SPen event action into normal event action.
50     *
51     * @param eventActionMasked Input event action. It is assumed that it is masked as the values
52                                cannot be ORed.
53     * @return Event action after the conversion.
54     */
55    public static int convertSPenEventAction(int eventActionMasked) {
56        // S-Pen support: convert to normal stylus event handling
57        switch (eventActionMasked) {
58            case SPEN_ACTION_DOWN:
59                return MotionEvent.ACTION_DOWN;
60            case SPEN_ACTION_UP:
61                return MotionEvent.ACTION_UP;
62            case SPEN_ACTION_MOVE:
63                return MotionEvent.ACTION_MOVE;
64            case SPEN_ACTION_CANCEL:
65                return MotionEvent.ACTION_CANCEL;
66            default:
67                return eventActionMasked;
68        }
69    }
70}
71