1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.view; 18 19 import android.content.Context; 20 import android.os.Build; 21 22 import java.util.ArrayList; 23 import java.util.List; 24 25 /** 26 * Compatibility processor for InputEvents that allows events to be adjusted before and 27 * after it is sent to the application. 28 * 29 * {@hide} 30 */ 31 public class InputEventCompatProcessor { 32 33 protected Context mContext; 34 protected int mTargetSdkVersion; 35 36 /** List of events to be used to return the processed events */ 37 private List<InputEvent> mProcessedEvents; 38 InputEventCompatProcessor(Context context)39 public InputEventCompatProcessor(Context context) { 40 mContext = context; 41 mTargetSdkVersion = context.getApplicationInfo().targetSdkVersion; 42 mProcessedEvents = new ArrayList<>(); 43 } 44 45 /** 46 * Processes the InputEvent for compatibility before it is sent to the app, allowing for the 47 * generation of more than one event if necessary. 48 * 49 * @param e The InputEvent to process 50 * @return The list of adjusted events, or null if no adjustments are needed. Do not keep a 51 * reference to the output as the list is reused. 52 */ processInputEventForCompatibility(InputEvent e)53 public List<InputEvent> processInputEventForCompatibility(InputEvent e) { 54 if (mTargetSdkVersion < Build.VERSION_CODES.M && e instanceof MotionEvent) { 55 mProcessedEvents.clear(); 56 MotionEvent motion = (MotionEvent) e; 57 final int mask = 58 MotionEvent.BUTTON_STYLUS_PRIMARY | MotionEvent.BUTTON_STYLUS_SECONDARY; 59 final int buttonState = motion.getButtonState(); 60 final int compatButtonState = (buttonState & mask) >> 4; 61 if (compatButtonState != 0) { 62 motion.setButtonState(buttonState | compatButtonState); 63 } 64 mProcessedEvents.add(motion); 65 return mProcessedEvents; 66 } 67 return null; 68 } 69 70 /** 71 * Processes the InputEvent for compatibility before it is finished by calling 72 * InputEventReceiver#finishInputEvent(). 73 * 74 * @param e The InputEvent to process 75 * @return The InputEvent to finish, or null if it should not be finished 76 */ processInputEventBeforeFinish(InputEvent e)77 public InputEvent processInputEventBeforeFinish(InputEvent e) { 78 // No changes needed 79 return e; 80 } 81 } 82