1 /*
2  * Copyright (C) 2022 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.hardware.input;
18 
19 import android.annotation.IntRange;
20 import android.annotation.NonNull;
21 import android.annotation.SystemApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 /**
26  * Configurations to create virtual touchscreen.
27  *
28  * @hide
29  */
30 @SystemApi
31 public final class VirtualTouchscreenConfig extends VirtualInputDeviceConfig implements Parcelable {
32 
33     /** The touchscreen width. */
34     private final int mWidth;
35     /** The touchscreen height. */
36     private final int mHeight;
37 
VirtualTouchscreenConfig(@onNull Builder builder)38     private VirtualTouchscreenConfig(@NonNull Builder builder) {
39         super(builder);
40         mWidth = builder.mWidth;
41         mHeight = builder.mHeight;
42     }
43 
VirtualTouchscreenConfig(@onNull Parcel in)44     private VirtualTouchscreenConfig(@NonNull Parcel in) {
45         super(in);
46         mWidth = in.readInt();
47         mHeight = in.readInt();
48     }
49 
50     /** Returns the touchscreen width. */
getWidth()51     public int getWidth() {
52         return mWidth;
53     }
54 
55     /** Returns the touchscreen height. */
getHeight()56     public int getHeight() {
57         return mHeight;
58     }
59 
60     @Override
describeContents()61     public int describeContents() {
62         return 0;
63     }
64 
65     @Override
writeToParcel(@onNull Parcel dest, int flags)66     public void writeToParcel(@NonNull Parcel dest, int flags) {
67         super.writeToParcel(dest, flags);
68         dest.writeInt(mWidth);
69         dest.writeInt(mHeight);
70     }
71 
72     @NonNull
73     public static final Creator<VirtualTouchscreenConfig> CREATOR =
74             new Creator<VirtualTouchscreenConfig>() {
75                 @Override
76                 public VirtualTouchscreenConfig createFromParcel(Parcel in) {
77                     return new VirtualTouchscreenConfig(in);
78                 }
79 
80                 @Override
81                 public VirtualTouchscreenConfig[] newArray(int size) {
82                     return new VirtualTouchscreenConfig[size];
83                 }
84             };
85 
86     /**
87      * Builder for creating a {@link VirtualTouchscreenConfig}.
88      */
89     public static final class Builder extends VirtualInputDeviceConfig.Builder<Builder> {
90         private int mWidth;
91         private int mHeight;
92 
93         /**
94          * Creates a new instance for the given dimensions of the {@link VirtualTouchscreen}.
95          *
96          * <p>The dimensions are not pixels but in the touchscreens raw coordinate space. They do
97          * not necessarily have to correspond to the display size or aspect ratio. In this case the
98          * framework will handle the scaling appropriately.
99          *
100          * @param touchscreenWidth The width of the touchscreen.
101          * @param touchscreenHeight The height of the touchscreen.
102          */
Builder(@ntRangefrom = 1) int touchscreenWidth, @IntRange(from = 1) int touchscreenHeight)103         public Builder(@IntRange(from = 1) int touchscreenWidth,
104                 @IntRange(from = 1) int touchscreenHeight) {
105             if (touchscreenHeight <= 0 || touchscreenWidth <= 0) {
106                 throw new IllegalArgumentException(
107                         "Cannot create a virtual touchscreen, touchscreen dimensions must be "
108                                 + "positive. Got: (" + touchscreenHeight + ", "
109                                 + touchscreenWidth + ")");
110             }
111             mHeight = touchscreenHeight;
112             mWidth = touchscreenWidth;
113         }
114 
115         /**
116          * Builds the {@link VirtualTouchscreenConfig} instance.
117          */
118         @NonNull
build()119         public VirtualTouchscreenConfig build() {
120             return new VirtualTouchscreenConfig(this);
121         }
122     }
123 }
124