1 /* 2 * Copyright (C) 2019 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 com.google.android.car.multidisplaytest.touch; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.graphics.Color; 22 import android.graphics.Paint; 23 import android.graphics.Point; 24 import android.util.AttributeSet; 25 import android.util.Log; 26 import android.view.MotionEvent; 27 import android.view.View; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 32 /** 33 * Copied from EmbeddedKitchenSinkApp 34 * Show touch points as circles; allow multiple touch points 35 * Set LOG_ONLY = false to test on physical device. 36 */ 37 public class TouchPointView extends View { 38 private static final String TAG = TouchPointView.class.getSimpleName(); 39 private static final boolean LOG_ONLY = false; 40 private static final int[] COLORS = { 41 Color.RED, 42 Color.GREEN, 43 Color.BLUE, 44 Color.YELLOW, 45 Color.MAGENTA, 46 Color.BLACK, 47 Color.DKGRAY 48 }; 49 50 private final List<Finger> mFingers; 51 private final Paint mPaint; 52 TouchPointView(Context context, AttributeSet attrs)53 public TouchPointView(Context context, AttributeSet attrs) { 54 this(context, attrs, 0); 55 } 56 TouchPointView(Context context, AttributeSet attrs, int defStyle)57 public TouchPointView(Context context, AttributeSet attrs, int defStyle) { 58 super(context, attrs, defStyle); 59 mFingers = new ArrayList<Finger>(); 60 61 mPaint = new Paint(); 62 mPaint.setStyle(Paint.Style.FILL); 63 } 64 65 @Override onTouchEvent(MotionEvent event)66 public boolean onTouchEvent(MotionEvent event) { 67 if (LOG_ONLY) { 68 logTouchEvents(event); 69 return true; 70 } 71 mFingers.clear(); 72 if (event.getActionMasked() == MotionEvent.ACTION_UP) { 73 invalidate(); 74 return true; 75 } 76 for (int i = 0; i < event.getPointerCount(); i++) { 77 int pointerId = event.getPointerId(i); 78 int pointerIndex = event.findPointerIndex(pointerId); 79 Finger finger = new Finger(); 80 finger.point = new Point((int) event.getX(pointerIndex), 81 (int) event.getY(pointerIndex)); 82 finger.pointerId = pointerId; 83 84 mFingers.add(finger); 85 } 86 invalidate(); 87 return true; 88 } 89 logTouchEvents(MotionEvent event)90 private void logTouchEvents(MotionEvent event) { 91 if (event.getActionMasked() != MotionEvent.ACTION_UP) { 92 return; 93 } 94 95 for (int i = 0; i < event.getPointerCount(); i++) { 96 int pointerId = event.getPointerId(i); 97 int pointerIndex = event.findPointerIndex(pointerId); 98 long downTime = event.getDownTime(); 99 long eventTime = event.getEventTime(); 100 Log.d(TAG, "TouchUp [x=" + event.getX(pointerIndex) + ", y=" + event.getY(pointerIndex) 101 + " , pointerId=" + pointerId + ", pointerIndex=" + pointerIndex + ", duration=" 102 + (eventTime - downTime) + "]"); 103 } 104 } 105 106 @Override onDraw(Canvas canvas)107 public void onDraw(Canvas canvas) { 108 if (LOG_ONLY) { 109 return; 110 } 111 int radius = canvas.getWidth() / 100; 112 for (Finger finger: mFingers) { 113 Point point = finger.point; 114 int color = COLORS[finger.pointerId % COLORS.length]; 115 mPaint.setColor(color); 116 canvas.drawCircle(point.x, point.y, radius, mPaint); 117 } 118 } 119 120 private static final class Finger { 121 public Point point; 122 public int pointerId; 123 } 124 } 125