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 
ParcelableUsbPort(@onNull String id, int supportedModes, int supportedContaminantProtectionModes, boolean supportsEnableContaminantPresenceProtection, boolean supportsEnableContaminantPresenceDetection)38     private ParcelableUsbPort(@NonNull String id, int supportedModes,
39             int supportedContaminantProtectionModes,
40             boolean supportsEnableContaminantPresenceProtection,
41             boolean supportsEnableContaminantPresenceDetection) {
42         mId = id;
43         mSupportedModes = supportedModes;
44         mSupportedContaminantProtectionModes = supportedContaminantProtectionModes;
45         mSupportsEnableContaminantPresenceProtection =
46                 supportsEnableContaminantPresenceProtection;
47         mSupportsEnableContaminantPresenceDetection =
48                 supportsEnableContaminantPresenceDetection;
49     }
50 
51     /**
52      * Create the parcelable version of a {@link UsbPort}.
53      *
54      * @param port The port to create a parcealable version of
55      *
56      * @return The parcelable version of the port
57      */
of(@onNull UsbPort port)58     public static @NonNull ParcelableUsbPort of(@NonNull UsbPort port) {
59         return new ParcelableUsbPort(port.getId(), port.getSupportedModes(),
60                 port.getSupportedContaminantProtectionModes(),
61                 port.supportsEnableContaminantPresenceProtection(),
62                 port.supportsEnableContaminantPresenceDetection());
63     }
64 
65     /**
66      * Create a {@link UsbPort} from this object.
67      *
68      * @param usbManager A link to the usbManager in the current context
69      *
70      * @return The UsbPort for this object
71      */
getUsbPort(@onNull UsbManager usbManager)72     public @NonNull UsbPort getUsbPort(@NonNull UsbManager usbManager) {
73         return new UsbPort(usbManager, mId, mSupportedModes, mSupportedContaminantProtectionModes,
74                 mSupportsEnableContaminantPresenceProtection,
75                 mSupportsEnableContaminantPresenceDetection);
76     }
77 
78     @Override
describeContents()79     public int describeContents() {
80         return 0;
81     }
82 
83     @Override
writeToParcel(Parcel dest, int flags)84     public void writeToParcel(Parcel dest, int flags) {
85         dest.writeString(mId);
86         dest.writeInt(mSupportedModes);
87         dest.writeInt(mSupportedContaminantProtectionModes);
88         dest.writeBoolean(mSupportsEnableContaminantPresenceProtection);
89         dest.writeBoolean(mSupportsEnableContaminantPresenceDetection);
90     }
91 
92     public static final @android.annotation.NonNull Creator<ParcelableUsbPort> CREATOR =
93             new Creator<ParcelableUsbPort>() {
94                 @Override
95                 public ParcelableUsbPort createFromParcel(Parcel in) {
96                     String id = in.readString();
97                     int supportedModes = in.readInt();
98                     int supportedContaminantProtectionModes = in.readInt();
99                     boolean supportsEnableContaminantPresenceProtection = in.readBoolean();
100                     boolean supportsEnableContaminantPresenceDetection = in.readBoolean();
101 
102                     return new ParcelableUsbPort(id, supportedModes,
103                             supportedContaminantProtectionModes,
104                             supportsEnableContaminantPresenceProtection,
105                             supportsEnableContaminantPresenceDetection);
106                 }
107 
108                 @Override
109                 public ParcelableUsbPort[] newArray(int size) {
110                     return new ParcelableUsbPort[size];
111                 }
112             };
113 }
114