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.NonNull;
20 import android.annotation.SystemApi;
21 import android.os.Parcel;
22 
23 /**
24  * Common configurations to create virtual input devices.
25  *
26  * @hide
27  */
28 @SystemApi
29 public abstract class VirtualInputDeviceConfig {
30     /** The vendor id uniquely identifies the company who manufactured the device. */
31     private final int mVendorId;
32     /**
33      * The product id uniquely identifies which product within the address space of a given vendor,
34      * identified by the device's vendor id.
35      */
36     private final int mProductId;
37     /** The associated display ID of the virtual input device. */
38     private final int mAssociatedDisplayId;
39     /** The name of the virtual input device. */
40     @NonNull
41     private final String mInputDeviceName;
42 
VirtualInputDeviceConfig(@onNull Builder<? extends Builder<?>> builder)43     protected VirtualInputDeviceConfig(@NonNull Builder<? extends Builder<?>> builder) {
44         mVendorId = builder.mVendorId;
45         mProductId = builder.mProductId;
46         mAssociatedDisplayId = builder.mAssociatedDisplayId;
47         mInputDeviceName = builder.mInputDeviceName;
48     }
49 
VirtualInputDeviceConfig(@onNull Parcel in)50     protected VirtualInputDeviceConfig(@NonNull Parcel in) {
51         mVendorId = in.readInt();
52         mProductId = in.readInt();
53         mAssociatedDisplayId = in.readInt();
54         mInputDeviceName = in.readString8();
55     }
56 
57     /**
58      * The vendor id uniquely identifies the company who manufactured the device.
59      */
getVendorId()60     public int getVendorId() {
61         return mVendorId;
62     }
63 
64     /**
65      * The product id uniquely identifies which product within the address space of a given vendor,
66      * identified by the device's vendor id.
67      */
getProductId()68     public int getProductId() {
69         return mProductId;
70     }
71 
72     /**
73      * The associated display ID of the virtual input device.
74      */
getAssociatedDisplayId()75     public int getAssociatedDisplayId() {
76         return mAssociatedDisplayId;
77     }
78 
79     /**
80      * The name of the virtual input device.
81      */
82     @NonNull
getInputDeviceName()83     public String getInputDeviceName() {
84         return mInputDeviceName;
85     }
86 
writeToParcel(@onNull Parcel dest, int flags)87     void writeToParcel(@NonNull Parcel dest, int flags) {
88         dest.writeInt(mVendorId);
89         dest.writeInt(mProductId);
90         dest.writeInt(mAssociatedDisplayId);
91         dest.writeString8(mInputDeviceName);
92     }
93 
94     /**
95      * A builder for {@link VirtualInputDeviceConfig}
96      *
97      * @param <T> The subclass to be built.
98      */
99     @SuppressWarnings({"StaticFinalBuilder", "MissingBuildMethod"})
100     public abstract static class Builder<T extends Builder<T>> {
101 
102         private int mVendorId;
103         private int mProductId;
104         private int mAssociatedDisplayId;
105         @NonNull
106         private String mInputDeviceName;
107 
108         /** @see VirtualInputDeviceConfig#getVendorId(). */
109         @NonNull
setVendorId(int vendorId)110         public T setVendorId(int vendorId) {
111             mVendorId = vendorId;
112             return self();
113         }
114 
115 
116         /** @see VirtualInputDeviceConfig#getProductId(). */
117         @NonNull
setProductId(int productId)118         public T setProductId(int productId) {
119             mProductId = productId;
120             return self();
121         }
122 
123         /** @see VirtualInputDeviceConfig#getAssociatedDisplayId(). */
124         @NonNull
setAssociatedDisplayId(int displayId)125         public T setAssociatedDisplayId(int displayId) {
126             mAssociatedDisplayId = displayId;
127             return self();
128         }
129 
130         /** @see VirtualInputDeviceConfig#getInputDeviceName(). */
131         @NonNull
setInputDeviceName(@onNull String deviceName)132         public T setInputDeviceName(@NonNull String deviceName) {
133             mInputDeviceName = deviceName;
134             return self();
135         }
136 
137         /**
138          * Each subclass should return itself to allow the builder to chain properly
139          */
self()140         T self() {
141             return (T) this;
142         }
143     }
144 }
145