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.hardware.usb;
18 
19 import android.annotation.NonNull;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import com.android.internal.annotations.Immutable;
24 
25 /**
26  * A parcelable wrapper to send UsbPorts over binders.
27  *
28  * @hide
29  */
30 @Immutable
31 public final class ParcelableUsbPort implements Parcelable {
32     private final @NonNull String mId;
33     private final int mSupportedModes;
34     private final int mSupportedContaminantProtectionModes;
35     private final boolean mSupportsEnableContaminantPresenceProtection;
36     private final boolean mSupportsEnableContaminantPresenceDetection;
37     private final boolean mSupportsComplianceWarnings;
38     private final int mSupportedAltModesMask;
39 
ParcelableUsbPort(@onNull String id, int supportedModes, int supportedContaminantProtectionModes, boolean supportsEnableContaminantPresenceProtection, boolean supportsEnableContaminantPresenceDetection, boolean supportsComplianceWarnings, int supportedAltModesMask)40     private ParcelableUsbPort(@NonNull String id, int supportedModes,
41             int supportedContaminantProtectionModes,
42             boolean supportsEnableContaminantPresenceProtection,
43             boolean supportsEnableContaminantPresenceDetection,
44             boolean supportsComplianceWarnings,
45             int supportedAltModesMask) {
46         mId = id;
47         mSupportedModes = supportedModes;
48         mSupportedContaminantProtectionModes = supportedContaminantProtectionModes;
49         mSupportsEnableContaminantPresenceProtection =
50                 supportsEnableContaminantPresenceProtection;
51         mSupportsEnableContaminantPresenceDetection =
52                 supportsEnableContaminantPresenceDetection;
53         mSupportsComplianceWarnings =
54                 supportsComplianceWarnings;
55         mSupportedAltModesMask = supportedAltModesMask;
56     }
57 
58     /**
59      * Create the parcelable version of a {@link UsbPort}.
60      *
61      * @param port The port to create a parcealable version of
62      *
63      * @return The parcelable version of the port
64      */
of(@onNull UsbPort port)65     public static @NonNull ParcelableUsbPort of(@NonNull UsbPort port) {
66         return new ParcelableUsbPort(port.getId(), port.getSupportedModes(),
67                 port.getSupportedContaminantProtectionModes(),
68                 port.supportsEnableContaminantPresenceProtection(),
69                 port.supportsEnableContaminantPresenceDetection(),
70                 port.supportsComplianceWarnings(),
71                 port.getSupportedAltModesMask());
72     }
73 
74     /**
75      * Create a {@link UsbPort} from this object.
76      *
77      * @param usbManager A link to the usbManager in the current context
78      *
79      * @return The UsbPort for this object
80      */
getUsbPort(@onNull UsbManager usbManager)81     public @NonNull UsbPort getUsbPort(@NonNull UsbManager usbManager) {
82         return new UsbPort(usbManager, mId, mSupportedModes, mSupportedContaminantProtectionModes,
83                 mSupportsEnableContaminantPresenceProtection,
84                 mSupportsEnableContaminantPresenceDetection,
85                 mSupportsComplianceWarnings,
86                 mSupportedAltModesMask);
87     }
88 
89     @Override
describeContents()90     public int describeContents() {
91         return 0;
92     }
93 
94     @Override
writeToParcel(Parcel dest, int flags)95     public void writeToParcel(Parcel dest, int flags) {
96         dest.writeString(mId);
97         dest.writeInt(mSupportedModes);
98         dest.writeInt(mSupportedContaminantProtectionModes);
99         dest.writeBoolean(mSupportsEnableContaminantPresenceProtection);
100         dest.writeBoolean(mSupportsEnableContaminantPresenceDetection);
101         dest.writeBoolean(mSupportsComplianceWarnings);
102         dest.writeInt(mSupportedAltModesMask);
103     }
104 
105     public static final @android.annotation.NonNull Creator<ParcelableUsbPort> CREATOR =
106             new Creator<ParcelableUsbPort>() {
107                 @Override
108                 public ParcelableUsbPort createFromParcel(Parcel in) {
109                     String id = in.readString();
110                     int supportedModes = in.readInt();
111                     int supportedContaminantProtectionModes = in.readInt();
112                     boolean supportsEnableContaminantPresenceProtection = in.readBoolean();
113                     boolean supportsEnableContaminantPresenceDetection = in.readBoolean();
114                     boolean supportsComplianceWarnings = in.readBoolean();
115                     int supportedAltModesMask = in.readInt();
116 
117                     return new ParcelableUsbPort(id, supportedModes,
118                             supportedContaminantProtectionModes,
119                             supportsEnableContaminantPresenceProtection,
120                             supportsEnableContaminantPresenceDetection,
121                             supportsComplianceWarnings,
122                             supportedAltModesMask);
123                 }
124 
125                 @Override
126                 public ParcelableUsbPort[] newArray(int size) {
127                     return new ParcelableUsbPort[size];
128                 }
129             };
130 }
131