1 /*
2  * Copyright (C) 2015 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.Manifest;
20 import android.annotation.CheckResult;
21 import android.annotation.IntDef;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.RequiresPermission;
25 import android.annotation.SystemApi;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 
29 import com.android.internal.annotations.Immutable;
30 
31 import java.lang.StringBuilder;
32 import java.lang.annotation.Retention;
33 import java.lang.annotation.RetentionPolicy;
34 
35 /**
36  * Describes the status of a USB port.
37  *
38  * @hide
39  */
40 @Immutable
41 @SystemApi
42 public final class UsbPortStatus implements Parcelable {
43     private static final String TAG = "UsbPortStatus";
44     private final int mCurrentMode;
45     private final @UsbPowerRole int mCurrentPowerRole;
46     private final @UsbDataRole int mCurrentDataRole;
47     private final int mSupportedRoleCombinations;
48     private final @ContaminantProtectionStatus int mContaminantProtectionStatus;
49     private final @ContaminantDetectionStatus int mContaminantDetectionStatus;
50     private final boolean mPowerTransferLimited;
51     private final @UsbDataStatus int mUsbDataStatus;
52     private final @PowerBrickConnectionStatus int mPowerBrickConnectionStatus;
53     private final @NonNull @ComplianceWarning int[] mComplianceWarnings;
54     private final @PlugState int mPlugState;
55     /**
56      * Holds the DisplayPort Alt Mode info for the Port. This field
57      * is null if the device does not support DisplayPort Alt Mode.
58      */
59     private final @Nullable DisplayPortAltModeInfo mDisplayPortAltModeInfo;
60 
61 
62     /**
63      * Power role: This USB port does not have a power role.
64      */
65     public static final int POWER_ROLE_NONE = 0;
66 
67     /**
68      * Power role: This USB port can act as a source (provide power).
69      */
70     public static final int POWER_ROLE_SOURCE = 1;
71 
72     /**
73      * Power role: This USB port can act as a sink (receive power).
74      */
75     public static final int POWER_ROLE_SINK = 2;
76 
77     @IntDef(prefix = { "POWER_ROLE_" }, value = {
78             POWER_ROLE_NONE,
79             POWER_ROLE_SOURCE,
80             POWER_ROLE_SINK
81     })
82     @Retention(RetentionPolicy.SOURCE)
83     @interface UsbPowerRole{}
84 
85     /**
86      * Power role: This USB port does not have a data role.
87      */
88     public static final int DATA_ROLE_NONE = 0;
89 
90     /**
91      * Data role: This USB port can act as a host (access data services).
92      */
93     public static final int DATA_ROLE_HOST = 1;
94 
95     /**
96      * Data role: This USB port can act as a device (offer data services).
97      */
98     public static final int DATA_ROLE_DEVICE = 2;
99 
100     @IntDef(prefix = { "DATA_ROLE_" }, value = {
101             DATA_ROLE_NONE,
102             DATA_ROLE_HOST,
103             DATA_ROLE_DEVICE
104     })
105     @Retention(RetentionPolicy.SOURCE)
106     @interface UsbDataRole{}
107 
108     /**
109      * There is currently nothing connected to this USB port.
110      */
111     public static final int MODE_NONE = 0;
112 
113     /**
114      * This USB port can act as an upstream facing port (device).
115      *
116      * <p> Implies that the port supports the {@link #POWER_ROLE_SINK} and
117      * {@link #DATA_ROLE_DEVICE} combination of roles (and possibly others as well).
118      */
119     public static final int MODE_UFP = 1 << 0;
120 
121     /**
122      * This USB port can act as a downstream facing port (host).
123      *
124      * <p> Implies that the port supports the {@link #POWER_ROLE_SOURCE} and
125      * {@link #DATA_ROLE_HOST} combination of roles (and possibly others as well).
126      */
127     public static final int MODE_DFP = 1 << 1;
128 
129     /**
130      * This USB port can act either as an downstream facing port (host) or as
131      * an upstream facing port (device).
132      *
133      * <p> Implies that the port supports the {@link #POWER_ROLE_SOURCE} and
134      * {@link #DATA_ROLE_HOST} combination of roles and the {@link #POWER_ROLE_SINK} and
135      * {@link #DATA_ROLE_DEVICE} combination of roles (and possibly others as well).
136      *
137      * @hide
138      */
139     public static final int MODE_DUAL = MODE_UFP | MODE_DFP;
140 
141     /**
142      * This USB port can support USB Type-C Audio accessory.
143      */
144     public static final int MODE_AUDIO_ACCESSORY = 1 << 2;
145 
146     /**
147      * This USB port can support USB Type-C debug accessory.
148      */
149     public static final int MODE_DEBUG_ACCESSORY = 1 << 3;
150 
151    /**
152      * Contaminant presence detection not supported by the device.
153      * @hide
154      */
155     public static final int CONTAMINANT_DETECTION_NOT_SUPPORTED = 0;
156 
157     /**
158      * Contaminant presence detection supported but disabled.
159      * @hide
160      */
161     public static final int CONTAMINANT_DETECTION_DISABLED = 1;
162 
163     /**
164      * Contaminant presence enabled but not detected.
165      * @hide
166      */
167     public static final int CONTAMINANT_DETECTION_NOT_DETECTED = 2;
168 
169     /**
170      * Contaminant presence enabled and detected.
171      * @hide
172      */
173     public static final int CONTAMINANT_DETECTION_DETECTED = 3;
174 
175     /**
176      * Contaminant protection - No action performed upon detection of
177      * contaminant presence.
178      * @hide
179      */
180     public static final int CONTAMINANT_PROTECTION_NONE = 0;
181 
182     /**
183      * Contaminant protection - Port is forced to sink upon detection of
184      * contaminant presence.
185      * @hide
186      */
187     public static final int CONTAMINANT_PROTECTION_SINK = 1 << 0;
188 
189     /**
190      * Contaminant protection - Port is forced to source upon detection of
191      * contaminant presence.
192      * @hide
193      */
194     public static final int CONTAMINANT_PROTECTION_SOURCE = 1 << 1;
195 
196     /**
197      * Contaminant protection - Port is disabled upon detection of
198      * contaminant presence.
199      * @hide
200      */
201     public static final int CONTAMINANT_PROTECTION_FORCE_DISABLE = 1 << 2;
202 
203     /**
204      * Contaminant protection - Port is disabled upon detection of
205      * contaminant presence.
206      * @hide
207      */
208     public static final int CONTAMINANT_PROTECTION_DISABLED = 1 << 3;
209 
210     /**
211      * USB data status is not known.
212      */
213     public static final int DATA_STATUS_UNKNOWN = 0;
214 
215     /**
216      * USB data is enabled.
217      */
218     public static final int DATA_STATUS_ENABLED = 1 << 0;
219 
220     /**
221      * USB data is disabled as the port is too hot.
222      */
223     public static final int DATA_STATUS_DISABLED_OVERHEAT = 1 << 1;
224 
225     /**
226      * USB data is disabled due to contaminated port.
227      */
228     public static final int DATA_STATUS_DISABLED_CONTAMINANT = 1 << 2;
229 
230     /**
231      * This flag indicates that some or all data modes are disabled
232      * due to docking event, and the specific sub-statuses viz.,
233      * {@link #DATA_STATUS_DISABLED_DOCK_HOST_MODE},
234      * {@link #DATA_STATUS_DISABLED_DOCK_DEVICE_MODE}
235      * can be checked for individual modes.
236      */
237     public static final int DATA_STATUS_DISABLED_DOCK = 1 << 3;
238 
239     /**
240      * USB data is disabled by
241      * {@link UsbPort#enableUsbData UsbPort.enableUsbData}.
242      */
243     public static final int DATA_STATUS_DISABLED_FORCE = 1 << 4;
244 
245     /**
246      * USB data is disabled for debug.
247      */
248     public static final int DATA_STATUS_DISABLED_DEBUG = 1 << 5;
249 
250     /**
251      * USB host mode is disabled due to docking event.
252      * {@link #DATA_STATUS_DISABLED_DOCK} will be set as well.
253      */
254     public static final int DATA_STATUS_DISABLED_DOCK_HOST_MODE = 1 << 6;
255 
256     /**
257      * USB device mode is disabled due to docking event.
258      * {@link #DATA_STATUS_DISABLED_DOCK} will be set as well.
259      */
260     public static final int DATA_STATUS_DISABLED_DOCK_DEVICE_MODE = 1 << 7;
261 
262     /**
263      * Unknown whether a power brick is connected.
264      */
265     public static final int POWER_BRICK_STATUS_UNKNOWN = 0;
266 
267     /**
268      * The connected device is a power brick.
269      */
270     public static final int POWER_BRICK_STATUS_CONNECTED = 1;
271 
272     /**
273      * The connected device is not power brick.
274      */
275     public static final int POWER_BRICK_STATUS_DISCONNECTED = 2;
276 
277     /**
278      * Used to indicate attached sources/cables/accessories/ports
279      * that do not match the other warnings below and do not meet the
280      * requirements of specifications including but not limited to
281      * USB Type-C Cable and Connector, Universal Serial Bus
282      * Power Delivery, and Universal Serial Bus 1.x/2.0/3.x/4.0.
283      * In addition, constants introduced after the target sdk will be
284      * remapped into COMPLIANCE_WARNING_OTHER.
285      */
286     public static final int COMPLIANCE_WARNING_OTHER = 1;
287 
288     /**
289      * Used to indicate Type-C port partner
290      * (cable/accessory/source) that identifies itself as debug
291      * accessory source as defined in USB Type-C Cable and
292      * Connector Specification. However, the specification states
293      * that this is meant for debug only and shall not be used for
294      * with commercial products.
295      */
296     public static final int COMPLIANCE_WARNING_DEBUG_ACCESSORY = 2;
297 
298     /**
299      * Used to indicate USB ports that does not
300      * identify itself as one of the charging port types (SDP/CDP
301      * DCP etc) as defined by Battery Charging v1.2 Specification.
302      */
303     public static final int COMPLIANCE_WARNING_BC_1_2 = 3;
304 
305     /**
306      * Used to indicate Type-C sources/cables that are missing pull
307      * up resistors on the CC pins as required by USB Type-C Cable
308      * and Connector Specification.
309      */
310     public static final int COMPLIANCE_WARNING_MISSING_RP = 4;
311 
312     /**
313      * Indicates that the Type-C plug orientation cannot be
314      * determined because the connected state of the device is unknown.
315      */
316     public static final int PLUG_STATE_UNKNOWN = 0;
317 
318     /**
319      * Indicates no Type-C plug is inserted into the device.
320      */
321     public static final int PLUG_STATE_UNPLUGGED = 1;
322 
323     /**
324      * Indicates a Type-C plug is inserted into the device, but
325      * the orientation cannot be determined.
326      */
327     public static final int PLUG_STATE_PLUGGED_ORIENTATION_UNKNOWN = 2;
328 
329     /**
330      * Indicates that the connected plug uses its CC1
331      * pin to manage the Source-to-Sink connection.
332      */
333     public static final int PLUG_STATE_PLUGGED_ORIENTATION_NORMAL = 3;
334 
335     /**
336      * Indicates that the connected plug uses its CC2
337      * pin to manage the Source-to-Sink connection.
338      */
339     public static final int PLUG_STATE_PLUGGED_ORIENTATION_FLIPPED = 4;
340 
341     @IntDef(prefix = { "CONTAMINANT_DETECTION_" }, value = {
342             CONTAMINANT_DETECTION_NOT_SUPPORTED,
343             CONTAMINANT_DETECTION_DISABLED,
344             CONTAMINANT_DETECTION_NOT_DETECTED,
345             CONTAMINANT_DETECTION_DETECTED,
346     })
347     @Retention(RetentionPolicy.SOURCE)
348     @interface ContaminantDetectionStatus{}
349 
350     @IntDef(prefix = { "CONTAMINANT_PROTECTION_" }, flag = true, value = {
351             CONTAMINANT_PROTECTION_NONE,
352             CONTAMINANT_PROTECTION_SINK,
353             CONTAMINANT_PROTECTION_SOURCE,
354             CONTAMINANT_PROTECTION_FORCE_DISABLE,
355             CONTAMINANT_PROTECTION_DISABLED,
356     })
357     @Retention(RetentionPolicy.SOURCE)
358     @interface ContaminantProtectionStatus{}
359 
360     @IntDef(prefix = { "MODE_" }, value = {
361             MODE_NONE,
362             MODE_DFP,
363             MODE_UFP,
364             MODE_AUDIO_ACCESSORY,
365             MODE_DEBUG_ACCESSORY,
366     })
367     @Retention(RetentionPolicy.SOURCE)
368     @interface UsbPortMode{}
369 
370     @IntDef(prefix = { "COMPLIANCE_WARNING_" }, value = {
371             COMPLIANCE_WARNING_OTHER,
372             COMPLIANCE_WARNING_DEBUG_ACCESSORY,
373             COMPLIANCE_WARNING_BC_1_2,
374             COMPLIANCE_WARNING_MISSING_RP,
375     })
376     @Retention(RetentionPolicy.SOURCE)
377     @interface ComplianceWarning{}
378 
379     @IntDef(prefix = { "PLUG_STATE_" }, value = {
380             PLUG_STATE_UNKNOWN,
381             PLUG_STATE_UNPLUGGED,
382             PLUG_STATE_PLUGGED_ORIENTATION_UNKNOWN,
383             PLUG_STATE_PLUGGED_ORIENTATION_NORMAL,
384             PLUG_STATE_PLUGGED_ORIENTATION_FLIPPED,
385     })
386     @Retention(RetentionPolicy.SOURCE)
387     @interface PlugState{}
388 
389     /** @hide */
390     @IntDef(prefix = { "DATA_STATUS_" }, flag = true, value = {
391             DATA_STATUS_UNKNOWN,
392             DATA_STATUS_ENABLED,
393             DATA_STATUS_DISABLED_OVERHEAT,
394             DATA_STATUS_DISABLED_CONTAMINANT,
395             DATA_STATUS_DISABLED_DOCK,
396             DATA_STATUS_DISABLED_DOCK_HOST_MODE,
397             DATA_STATUS_DISABLED_DOCK_DEVICE_MODE,
398             DATA_STATUS_DISABLED_FORCE,
399             DATA_STATUS_DISABLED_DEBUG,
400     })
401     @Retention(RetentionPolicy.SOURCE)
402     @interface UsbDataStatus{}
403 
404     /** @hide */
405     @IntDef(prefix = { "POWER_BRICK_STATUS_" }, value = {
406             POWER_BRICK_STATUS_UNKNOWN,
407             POWER_BRICK_STATUS_DISCONNECTED,
408             POWER_BRICK_STATUS_CONNECTED,
409     })
410     @Retention(RetentionPolicy.SOURCE)
411     @interface PowerBrickConnectionStatus{}
412 
413     /** @hide */
UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole, int supportedRoleCombinations, int contaminantProtectionStatus, int contaminantDetectionStatus, @UsbDataStatus int usbDataStatus, boolean powerTransferLimited, @PowerBrickConnectionStatus int powerBrickConnectionStatus, @NonNull @ComplianceWarning int[] complianceWarnings, int plugState, @Nullable DisplayPortAltModeInfo displayPortAltModeInfo)414     public UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole,
415             int supportedRoleCombinations, int contaminantProtectionStatus,
416             int contaminantDetectionStatus, @UsbDataStatus int usbDataStatus,
417             boolean powerTransferLimited,
418             @PowerBrickConnectionStatus int powerBrickConnectionStatus,
419             @NonNull @ComplianceWarning int[] complianceWarnings,
420             int plugState,
421             @Nullable DisplayPortAltModeInfo displayPortAltModeInfo) {
422         mCurrentMode = currentMode;
423         mCurrentPowerRole = currentPowerRole;
424         mCurrentDataRole = currentDataRole;
425         mSupportedRoleCombinations = supportedRoleCombinations;
426         mContaminantProtectionStatus = contaminantProtectionStatus;
427         mContaminantDetectionStatus = contaminantDetectionStatus;
428 
429         // Older implementations that only set the DISABLED_DOCK_MODE will have the other two
430         // set at the HAL interface level, so the "dock mode only" state shouldn't be visible here.
431         // But the semantics are ensured here.
432         int disabledDockModes = (usbDataStatus &
433             (DATA_STATUS_DISABLED_DOCK_HOST_MODE | DATA_STATUS_DISABLED_DOCK_DEVICE_MODE));
434         if (disabledDockModes != 0) {
435             // Set DATA_STATUS_DISABLED_DOCK when one of DATA_STATUS_DISABLED_DOCK_*_MODE is set
436             usbDataStatus |= DATA_STATUS_DISABLED_DOCK;
437         } else {
438             // Clear DATA_STATUS_DISABLED_DOCK when none of DATA_STATUS_DISABLED_DOCK_*_MODE is set
439             usbDataStatus &= ~DATA_STATUS_DISABLED_DOCK;
440         }
441 
442         mUsbDataStatus = usbDataStatus;
443         mPowerTransferLimited = powerTransferLimited;
444         mPowerBrickConnectionStatus = powerBrickConnectionStatus;
445         mComplianceWarnings = complianceWarnings;
446         mPlugState = plugState;
447         mDisplayPortAltModeInfo = displayPortAltModeInfo;
448     }
449 
450     /** @hide */
UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole, int supportedRoleCombinations, int contaminantProtectionStatus, int contaminantDetectionStatus, @UsbDataStatus int usbDataStatus, boolean powerTransferLimited, @PowerBrickConnectionStatus int powerBrickConnectionStatus)451     public UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole,
452             int supportedRoleCombinations, int contaminantProtectionStatus,
453             int contaminantDetectionStatus, @UsbDataStatus int usbDataStatus,
454             boolean powerTransferLimited,
455             @PowerBrickConnectionStatus int powerBrickConnectionStatus) {
456         this(currentMode, currentPowerRole, currentDataRole, supportedRoleCombinations,
457                 contaminantProtectionStatus, contaminantDetectionStatus,
458                 usbDataStatus, powerTransferLimited, powerBrickConnectionStatus,
459                 new int[] {}, PLUG_STATE_UNKNOWN, null);
460     }
461 
462     /** @hide */
UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole, int supportedRoleCombinations, int contaminantProtectionStatus, int contaminantDetectionStatus)463     public UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole,
464             int supportedRoleCombinations, int contaminantProtectionStatus,
465             int contaminantDetectionStatus) {
466         this(currentMode, currentPowerRole, currentDataRole, supportedRoleCombinations,
467                 contaminantProtectionStatus, contaminantDetectionStatus,
468                 DATA_STATUS_UNKNOWN, false, POWER_BRICK_STATUS_UNKNOWN,
469                 new int[] {}, PLUG_STATE_UNKNOWN, null);
470     }
471 
472     /**
473      * Returns true if there is anything connected to the port.
474      *
475      * @return {@code true} iff there is anything connected to the port.
476      */
isConnected()477     public boolean isConnected() {
478         return mCurrentMode != 0;
479     }
480 
481     /**
482      * Gets the current mode of the port.
483      *
484      * @return The current mode: {@link #MODE_DFP}, {@link #MODE_UFP},
485      * {@link #MODE_AUDIO_ACCESSORY}, {@link #MODE_DEBUG_ACCESSORY}, or {@link {@link #MODE_NONE} if
486      * nothing is connected.
487      */
getCurrentMode()488     public @UsbPortMode int getCurrentMode() {
489         return mCurrentMode;
490     }
491 
492     /**
493      * Gets the current power role of the port.
494      *
495      * @return The current power role: {@link #POWER_ROLE_SOURCE}, {@link #POWER_ROLE_SINK}, or
496      * {@link #POWER_ROLE_NONE} if nothing is connected.
497      */
getCurrentPowerRole()498     public @UsbPowerRole int getCurrentPowerRole() {
499         return mCurrentPowerRole;
500     }
501 
502     /**
503      * Gets the current data role of the port.
504      *
505      * @return The current data role: {@link #DATA_ROLE_HOST}, {@link #DATA_ROLE_DEVICE}, or
506      * {@link #DATA_ROLE_NONE} if nothing is connected.
507      */
getCurrentDataRole()508     public @UsbDataRole int getCurrentDataRole() {
509         return mCurrentDataRole;
510     }
511 
512     /**
513      * Returns true if the specified power and data role combination is supported
514      * given what is currently connected to the port.
515      *
516      * @param powerRole The power role to check: {@link #POWER_ROLE_SOURCE}  or
517      *                  {@link #POWER_ROLE_SINK}, or {@link #POWER_ROLE_NONE} if no power role.
518      * @param dataRole  The data role to check: either {@link #DATA_ROLE_HOST} or
519      *                  {@link #DATA_ROLE_DEVICE}, or {@link #DATA_ROLE_NONE} if no data role.
520      */
isRoleCombinationSupported(@sbPowerRole int powerRole, @UsbDataRole int dataRole)521     public boolean isRoleCombinationSupported(@UsbPowerRole int powerRole,
522             @UsbDataRole int dataRole) {
523         return (mSupportedRoleCombinations &
524                 UsbPort.combineRolesAsBit(powerRole, dataRole)) != 0;
525     }
526 
527     /**
528      * Get the supported role combinations.
529      */
getSupportedRoleCombinations()530     public int getSupportedRoleCombinations() {
531         return mSupportedRoleCombinations;
532     }
533 
534     /**
535      * Returns contaminant detection status.
536      *
537      * @hide
538      */
getContaminantDetectionStatus()539     public @ContaminantDetectionStatus int getContaminantDetectionStatus() {
540         return mContaminantDetectionStatus;
541     }
542 
543     /**
544      * Returns contamiant protection status.
545      *
546      * @hide
547      */
getContaminantProtectionStatus()548     public @ContaminantProtectionStatus int getContaminantProtectionStatus() {
549         return mContaminantProtectionStatus;
550     }
551 
552     /**
553      * Returns UsbData status.
554      *
555      * @return Current USB data status of the port with one or more of the following values
556      *         {@link #DATA_STATUS_UNKNOWN}, {@link #DATA_STATUS_ENABLED},
557      *         {@link #DATA_STATUS_DISABLED_OVERHEAT}, {@link #DATA_STATUS_DISABLED_CONTAMINANT},
558      *         {@link #DATA_STATUS_DISABLED_DOCK}, {@link #DATA_STATUS_DISABLED_FORCE},
559      *         {@link #DATA_STATUS_DISABLED_DEBUG}, {@link #DATA_STATUS_DISABLED_DOCK_HOST_MODE},
560      *         {@link #DATA_STATUS_DISABLED_DOCK_DEVICE_MODE}
561      */
getUsbDataStatus()562     public @UsbDataStatus int getUsbDataStatus() {
563         return mUsbDataStatus;
564     }
565 
566     /**
567      * Returns whether power transfer is limited.
568      *
569      * @return true when power transfer is limited.
570      *         false otherwise.
571      */
isPowerTransferLimited()572     public boolean isPowerTransferLimited() {
573         return mPowerTransferLimited;
574     }
575 
576     /**
577      * Returns the connection status of the power brick.
578      *
579      * @return {@link #POWER_BRICK_STATUS_UNKNOWN}
580      *         or {@link #POWER_BRICK_STATUS_CONNECTED}
581      *         or {@link #POWER_BRICK_STATUS_DISCONNECTED}
582      */
getPowerBrickConnectionStatus()583     public @PowerBrickConnectionStatus int getPowerBrickConnectionStatus() {
584         return mPowerBrickConnectionStatus;
585     }
586 
587     /**
588      * Returns non compliant reasons, if any, for the connected
589      * charger/cable/accessory/USB port.
590      *
591      * @return array including {@link #COMPLIANCE_WARNING_OTHER},
592      *         {@link #COMPLIANCE_WARNING_DEBUG_ACCESSORY},
593      *         {@link #COMPLIANCE_WARNING_BC_1_2},
594      *         or {@link #COMPLIANCE_WARNING_MISSING_RP}
595      */
596     @CheckResult
597     @NonNull
getComplianceWarnings()598     public @ComplianceWarning int[] getComplianceWarnings() {
599         return mComplianceWarnings;
600     }
601 
602     /**
603      * Returns the plug state of the attached cable/adapter.
604      *
605      */
getPlugState()606     public @PlugState int getPlugState() {
607         return mPlugState;
608     }
609 
610     /**
611      * Returns the DisplayPortInfo of the USB Port, if applicable.
612      *
613      * @return an instance of type DisplayPortInfo
614      *         or null if not applicable.
615      */
616     @Nullable
getDisplayPortAltModeInfo()617     public DisplayPortAltModeInfo getDisplayPortAltModeInfo() {
618         return (mDisplayPortAltModeInfo == null) ? null : mDisplayPortAltModeInfo;
619     }
620 
621     @NonNull
622     @Override
toString()623     public String toString() {
624         StringBuilder mString = new StringBuilder("UsbPortStatus{connected=" + isConnected()
625                 + ", currentMode=" + UsbPort.modeToString(mCurrentMode)
626                 + ", currentPowerRole=" + UsbPort.powerRoleToString(mCurrentPowerRole)
627                 + ", currentDataRole=" + UsbPort.dataRoleToString(mCurrentDataRole)
628                 + ", supportedRoleCombinations="
629                         + UsbPort.roleCombinationsToString(mSupportedRoleCombinations)
630                 + ", contaminantDetectionStatus="
631                         + getContaminantDetectionStatus()
632                 + ", contaminantProtectionStatus="
633                         + getContaminantProtectionStatus()
634                 + ", usbDataStatus="
635                         + UsbPort.usbDataStatusToString(getUsbDataStatus())
636                 + ", isPowerTransferLimited="
637                         + isPowerTransferLimited()
638                 + ", powerBrickConnectionStatus="
639                         + UsbPort
640                             .powerBrickConnectionStatusToString(getPowerBrickConnectionStatus())
641                 + ", complianceWarnings="
642                         + UsbPort.complianceWarningsToString(getComplianceWarnings())
643                 + ", plugState="
644                         + getPlugState()
645                 + ", displayPortAltModeInfo="
646                         + mDisplayPortAltModeInfo
647                 + "}");
648         return mString.toString();
649     }
650 
651     @Override
describeContents()652     public int describeContents() {
653         return 0;
654     }
655 
656     @Override
writeToParcel(Parcel dest, int flags)657     public void writeToParcel(Parcel dest, int flags) {
658         dest.writeInt(mCurrentMode);
659         dest.writeInt(mCurrentPowerRole);
660         dest.writeInt(mCurrentDataRole);
661         dest.writeInt(mSupportedRoleCombinations);
662         dest.writeInt(mContaminantProtectionStatus);
663         dest.writeInt(mContaminantDetectionStatus);
664         dest.writeInt(mUsbDataStatus);
665         dest.writeBoolean(mPowerTransferLimited);
666         dest.writeInt(mPowerBrickConnectionStatus);
667         dest.writeIntArray(mComplianceWarnings);
668         dest.writeInt(mPlugState);
669         if (mDisplayPortAltModeInfo == null) {
670             dest.writeBoolean(false);
671         } else {
672             dest.writeBoolean(true);
673             mDisplayPortAltModeInfo.writeToParcel(dest, 0);
674         }
675     }
676 
677     public static final @NonNull Parcelable.Creator<UsbPortStatus> CREATOR =
678             new Parcelable.Creator<UsbPortStatus>() {
679         @Override
680         public UsbPortStatus createFromParcel(Parcel in) {
681             int currentMode = in.readInt();
682             int currentPowerRole = in.readInt();
683             int currentDataRole = in.readInt();
684             int supportedRoleCombinations = in.readInt();
685             int contaminantProtectionStatus = in.readInt();
686             int contaminantDetectionStatus = in.readInt();
687             int usbDataStatus = in.readInt();
688             boolean powerTransferLimited = in.readBoolean();
689             int powerBrickConnectionStatus = in.readInt();
690             @ComplianceWarning int[] complianceWarnings = in.createIntArray();
691             int plugState = in.readInt();
692             boolean supportsDisplayPortAltMode = in.readBoolean();
693             DisplayPortAltModeInfo displayPortAltModeInfo;
694             if (supportsDisplayPortAltMode) {
695                 displayPortAltModeInfo = DisplayPortAltModeInfo.CREATOR.createFromParcel(in);
696             } else {
697                 displayPortAltModeInfo = null;
698             }
699             return new UsbPortStatus(currentMode, currentPowerRole, currentDataRole,
700                     supportedRoleCombinations, contaminantProtectionStatus,
701                     contaminantDetectionStatus, usbDataStatus, powerTransferLimited,
702                     powerBrickConnectionStatus,
703                     complianceWarnings, plugState, displayPortAltModeInfo);
704         }
705 
706         @Override
707         public UsbPortStatus[] newArray(int size) {
708             return new UsbPortStatus[size];
709         }
710     };
711 
712     /**
713      * Builder is used to create {@link UsbPortStatus} objects.
714      *
715      * @hide
716      */
717     public static final class Builder {
718         private @UsbPortMode int mCurrentMode;
719         private @UsbPowerRole int mCurrentPowerRole;
720         private @UsbDataRole int mCurrentDataRole;
721         private int mSupportedRoleCombinations;
722         private @ContaminantProtectionStatus int mContaminantProtectionStatus;
723         private @ContaminantDetectionStatus int mContaminantDetectionStatus;
724         private boolean mPowerTransferLimited;
725         private @UsbDataStatus int mUsbDataStatus;
726         private @PowerBrickConnectionStatus int mPowerBrickConnectionStatus;
727         private @ComplianceWarning int[] mComplianceWarnings;
728         private @PlugState int mPlugState;
729         private @Nullable DisplayPortAltModeInfo mDisplayPortAltModeInfo;
730 
Builder()731         public Builder() {
732             mCurrentMode = MODE_NONE;
733             mCurrentPowerRole = POWER_ROLE_NONE;
734             mCurrentDataRole = DATA_ROLE_NONE;
735             mContaminantProtectionStatus = CONTAMINANT_PROTECTION_NONE;
736             mContaminantDetectionStatus = CONTAMINANT_DETECTION_NOT_SUPPORTED;
737             mUsbDataStatus = DATA_STATUS_UNKNOWN;
738             mPowerBrickConnectionStatus = POWER_BRICK_STATUS_UNKNOWN;
739             mComplianceWarnings = new int[] {};
740             mPlugState = PLUG_STATE_UNKNOWN;
741             mDisplayPortAltModeInfo = null;
742         }
743 
744         /**
745          * Sets the current mode of {@link UsbPortStatus}
746          *
747          * @return Instance of {@link Builder}
748          */
749         @NonNull
setCurrentMode(@sbPortMode int currentMode)750         public Builder setCurrentMode(@UsbPortMode int currentMode) {
751             mCurrentMode = currentMode;
752             return this;
753         }
754 
755         /**
756          * Sets the current power role and data role of {@link UsbPortStatus}
757          *
758          * @return Instance of {@link Builder}
759          */
760         @NonNull
setCurrentRoles(@sbPowerRole int currentPowerRole, @UsbDataRole int currentDataRole)761         public Builder setCurrentRoles(@UsbPowerRole int currentPowerRole,
762                 @UsbDataRole int currentDataRole) {
763             mCurrentPowerRole = currentPowerRole;
764             mCurrentDataRole = currentDataRole;
765             return this;
766         }
767 
768         /**
769          * Sets supported role combinations of {@link UsbPortStatus}
770          *
771          * @return Instance of {@link Builder}
772          */
773         @NonNull
setSupportedRoleCombinations(int supportedRoleCombinations)774         public Builder setSupportedRoleCombinations(int supportedRoleCombinations) {
775             mSupportedRoleCombinations = supportedRoleCombinations;
776             return this;
777         }
778 
779         /**
780          * Sets current contaminant status of {@link UsbPortStatus}
781          *
782          * @return Instance of {@link Builder}
783          */
784         @NonNull
setContaminantStatus( @ontaminantProtectionStatus int contaminantProtectionStatus, @ContaminantDetectionStatus int contaminantDetectionStatus)785         public Builder setContaminantStatus(
786                 @ContaminantProtectionStatus int contaminantProtectionStatus,
787                 @ContaminantDetectionStatus int contaminantDetectionStatus) {
788             mContaminantProtectionStatus = contaminantProtectionStatus;
789             mContaminantDetectionStatus = contaminantDetectionStatus;
790             return this;
791         }
792 
793         /**
794          * Sets power limit power transfer of {@link UsbPortStatus}
795          *
796          * @return Instance of {@link Builder}
797          */
798         @NonNull
setPowerTransferLimited(boolean powerTransferLimited)799         public Builder setPowerTransferLimited(boolean powerTransferLimited) {
800             mPowerTransferLimited = powerTransferLimited;
801             return this;
802         }
803 
804         /**
805          * Sets the USB data status of {@link UsbPortStatus}
806          *
807          * @return Instance of {@link Builder}
808          */
809         @NonNull
setUsbDataStatus(@sbDataStatus int usbDataStatus)810         public Builder setUsbDataStatus(@UsbDataStatus int usbDataStatus) {
811             mUsbDataStatus = usbDataStatus;
812             return this;
813         }
814 
815         /**
816          * Sets the power brick connection status of {@link UsbPortStatus}
817          *
818          * @return Instance of {@link Builder}
819          */
820         @NonNull
setPowerBrickConnectionStatus( @owerBrickConnectionStatus int powerBrickConnectionStatus)821         public Builder setPowerBrickConnectionStatus(
822                 @PowerBrickConnectionStatus int powerBrickConnectionStatus) {
823             mPowerBrickConnectionStatus = powerBrickConnectionStatus;
824             return this;
825         }
826 
827         /**
828          * Sets the non-compliant charger reasons of {@link UsbPortStatus}
829          *
830          * @return Instance of {@link Builder}
831          */
832         @NonNull
setComplianceWarnings( @onNull int[] complianceWarnings)833         public Builder setComplianceWarnings(
834                 @NonNull int[] complianceWarnings) {
835             mComplianceWarnings = complianceWarnings == null ? new int[] {} :
836                     complianceWarnings;
837             return this;
838         }
839 
840         /**
841          * Sets the plug orientation of {@link UsbPortStatus}
842          *
843          * @return Instance of {@link Builder}
844          */
845         @NonNull
setPlugState(int plugState)846         public Builder setPlugState(int plugState) {
847             mPlugState = plugState;
848             return this;
849         }
850 
851         /**
852          * Sets the plug orientation of {@link UsbPortStatus}
853          *
854          * @return Instance of {@link Builder}
855          */
856         @NonNull
setDisplayPortAltModeInfo( @ullable DisplayPortAltModeInfo displayPortAltModeInfo)857         public Builder setDisplayPortAltModeInfo(
858                 @Nullable DisplayPortAltModeInfo displayPortAltModeInfo) {
859             mDisplayPortAltModeInfo = displayPortAltModeInfo;
860             return this;
861         }
862 
863         /**
864          * Creates the {@link UsbPortStatus} object.
865          */
866         @NonNull
build()867         public UsbPortStatus build() {
868             UsbPortStatus status = new UsbPortStatus(mCurrentMode, mCurrentPowerRole,
869                     mCurrentDataRole, mSupportedRoleCombinations, mContaminantProtectionStatus,
870                     mContaminantDetectionStatus, mUsbDataStatus, mPowerTransferLimited,
871                     mPowerBrickConnectionStatus, mComplianceWarnings,
872                     mPlugState, mDisplayPortAltModeInfo);
873             return status;
874         }
875     };
876 }
877