1 /*
2  * Copyright (C) 2016 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 package android.car.usb.handler;
17 
18 import android.content.ComponentName;
19 import android.hardware.usb.UsbDevice;
20 import com.android.internal.util.Preconditions;
21 
22 /**
23  * Settings for USB device.
24  * @hide
25  */
26 public final class UsbDeviceSettings {
27 
28     private final String mSerialNumber;
29     private final int mVid;
30     private final int mPid;
31     private String mDeviceName;
32     private ComponentName mHandler;
33     private boolean mAoap;
34     private boolean mDefaultHandler;
35 
UsbDeviceSettings(String serialNumber, int vid, int pid)36     UsbDeviceSettings(String serialNumber, int vid, int pid) {
37         Preconditions.checkNotNull(serialNumber);
38 
39         mSerialNumber = serialNumber;
40         mVid = vid;
41         mPid = pid;
42     }
43 
getSerialNumber()44     public String getSerialNumber() {
45         return mSerialNumber;
46     }
47 
getVid()48     public int getVid() {
49         return mVid;
50     }
51 
getPid()52     public int getPid() {
53         return mPid;
54     }
55 
setDeviceName(String deviceName)56     public void setDeviceName(String deviceName) {
57         mDeviceName = deviceName;
58     }
59 
getDeviceName()60     public String getDeviceName() {
61         return mDeviceName;
62     }
63 
setHandler(ComponentName handler)64     public void setHandler(ComponentName handler) {
65         mHandler = handler;
66     }
67 
getHandler()68     public ComponentName getHandler() {
69         return mHandler;
70     }
71 
setAoap(boolean aoap)72     public void setAoap(boolean aoap) {
73         mAoap = aoap;
74     }
75 
getAoap()76     public boolean getAoap() {
77         return mAoap;
78     }
79 
setDefaultHandler(boolean defaultHandler)80     public void setDefaultHandler(boolean defaultHandler) {
81         mDefaultHandler = defaultHandler;
82     }
83 
isDefaultHandler()84     public boolean isDefaultHandler() {
85         return mDefaultHandler;
86     }
87 
88     @Override
toString()89     public String toString() {
90         return "UsbDeviceSettings{serial=" + mSerialNumber + ", vid=" + mVid + ", pid=" + mPid
91                 + ", name=" + mDeviceName + ", handler=" + mHandler.toString() + ", aoap=" + mAoap
92                 + ", default=" + mDefaultHandler + "}";
93     }
94 
95     /**
96      * Checks if setting matches {@code UsbDevice}.
97      */
matchesDevice(UsbDevice device)98     public boolean matchesDevice(UsbDevice device) {
99         return getSerialNumber().equals(device.getSerialNumber());
100     }
101 
102     /**
103      * Creates settings from {@code UsbDevice}.
104      */
constructSettings(UsbDevice device)105     public static UsbDeviceSettings constructSettings(UsbDevice device) {
106         UsbDeviceSettings settings = new UsbDeviceSettings(
107                 device.getSerialNumber(), device.getVendorId(), device.getProductId());
108         settings.setDeviceName(device.getProductName());
109         return settings;
110     }
111 
112     /**
113      * Creates settings from other settings.
114      * <p>
115      * Only basic properties are inherited.
116      */
constructSettings(UsbDeviceSettings origSettings)117     public static UsbDeviceSettings constructSettings(UsbDeviceSettings origSettings) {
118         UsbDeviceSettings settings = new UsbDeviceSettings(
119                 origSettings.getSerialNumber(), origSettings.getVid(), origSettings.getPid());
120         settings.setDeviceName(origSettings.getDeviceName());
121         return settings;
122     }
123 
124     /**
125      * Creates settings.
126      */
constructSettings(String serialNumber, int vid, int pid, String deviceName, ComponentName handler, boolean aoap)127     public static UsbDeviceSettings constructSettings(String serialNumber, int vid, int pid,
128             String deviceName, ComponentName handler, boolean aoap) {
129         UsbDeviceSettings settings = new UsbDeviceSettings(serialNumber, vid, pid);
130         settings.setDeviceName(deviceName);
131         settings.setHandler(handler);
132         settings.setAoap(aoap);
133         return settings;
134     }
135 }
136