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