Siarhei Vishniakou | 8b8e76d | 2023-07-11 17:56:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 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 test.multideviceinput |
| 18 | |
| 19 | import android.app.Activity |
| 20 | import android.graphics.Color |
| 21 | import android.view.Gravity |
| 22 | import android.view.View |
| 23 | import android.view.ViewGroup |
| 24 | import android.view.WindowInsets.Type |
| 25 | import android.view.WindowManager |
| 26 | |
| 27 | |
| 28 | enum class PointerState { |
| 29 | DOWN, // One or more pointer(s) down, lines are being drawn |
| 30 | HOVER, // Pointer is hovering |
| 31 | NONE, // Nothing is touching or hovering |
| 32 | } |
| 33 | |
| 34 | data class SharedScaledPointerSize( |
| 35 | var lineSize: Float, |
| 36 | var circleSize: Float, |
| 37 | var state: PointerState |
| 38 | ) |
| 39 | |
| 40 | class MainActivity : Activity() { |
| 41 | val TAG = "MultiDeviceInput" |
| 42 | private val leftState = SharedScaledPointerSize(5f, 20f, PointerState.NONE) |
| 43 | private val rightState = SharedScaledPointerSize(5f, 20f, PointerState.NONE) |
| 44 | private lateinit var left: View |
| 45 | private lateinit var right: View |
| 46 | |
| 47 | override fun onResume() { |
| 48 | super.onResume() |
| 49 | |
| 50 | val wm = getSystemService(WindowManager::class.java) |
| 51 | val wmlp = WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION) |
| 52 | wmlp.flags = (wmlp.flags or |
| 53 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or |
| 54 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH) |
| 55 | |
| 56 | val windowMetrics = windowManager.currentWindowMetrics |
| 57 | val insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility(Type.systemBars()) |
| 58 | val width = windowMetrics.bounds.width() - insets.left - insets.right |
| 59 | val height = windowMetrics.bounds.height() - insets.top - insets.bottom |
| 60 | |
| 61 | wmlp.width = width * 24 / 50 |
| 62 | wmlp.height = height * 35 / 50 |
| 63 | |
| 64 | val vglp = ViewGroup.LayoutParams( |
| 65 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 66 | ViewGroup.LayoutParams.WRAP_CONTENT |
| 67 | ) |
| 68 | |
| 69 | wmlp.setTitle("Left -- " + getPackageName()) |
| 70 | wmlp.gravity = Gravity.CENTER_VERTICAL or Gravity.START |
| 71 | left = DrawingView(this, leftState, rightState) |
| 72 | left.setBackgroundColor(Color.LTGRAY) |
| 73 | left.setLayoutParams(vglp) |
| 74 | wm.addView(left, wmlp) |
| 75 | |
| 76 | wmlp.setTitle("Right -- " + getPackageName()) |
| 77 | wmlp.gravity = Gravity.CENTER_VERTICAL or Gravity.END |
| 78 | right = DrawingView(this, rightState, leftState) |
| 79 | right.setBackgroundColor(Color.LTGRAY) |
| 80 | right.setLayoutParams(vglp) |
| 81 | wm.addView(right, wmlp) |
| 82 | } |
| 83 | } |