/DVC-AN20_EMUI10.1.1/src/main/java/android/hardware/usb/DeviceFilter.java

https://github.com/dstmath/HWFramework · Java · 268 lines · 255 code · 13 blank · 0 comment · 214 complexity · 706c93940ede9184c1a9a6ab9851ff18 MD5 · raw file

  1. package android.hardware.usb;
  2. import android.util.Slog;
  3. import com.android.internal.util.dump.DualDumpOutputStream;
  4. import java.io.IOException;
  5. import java.util.Objects;
  6. import org.xmlpull.v1.XmlPullParser;
  7. import org.xmlpull.v1.XmlPullParserException;
  8. import org.xmlpull.v1.XmlSerializer;
  9. public class DeviceFilter {
  10. private static final String TAG = DeviceFilter.class.getSimpleName();
  11. public final int mClass;
  12. public final String mManufacturerName;
  13. public final int mProductId;
  14. public final String mProductName;
  15. public final int mProtocol;
  16. public final String mSerialNumber;
  17. public final int mSubclass;
  18. public final int mVendorId;
  19. public DeviceFilter(int vid, int pid, int clasz, int subclass, int protocol, String manufacturer, String product, String serialnum) {
  20. this.mVendorId = vid;
  21. this.mProductId = pid;
  22. this.mClass = clasz;
  23. this.mSubclass = subclass;
  24. this.mProtocol = protocol;
  25. this.mManufacturerName = manufacturer;
  26. this.mProductName = product;
  27. this.mSerialNumber = serialnum;
  28. }
  29. public DeviceFilter(UsbDevice device) {
  30. this.mVendorId = device.getVendorId();
  31. this.mProductId = device.getProductId();
  32. this.mClass = device.getDeviceClass();
  33. this.mSubclass = device.getDeviceSubclass();
  34. this.mProtocol = device.getDeviceProtocol();
  35. this.mManufacturerName = device.getManufacturerName();
  36. this.mProductName = device.getProductName();
  37. this.mSerialNumber = device.getSerialNumber();
  38. }
  39. public static DeviceFilter read(XmlPullParser parser) throws XmlPullParserException, IOException {
  40. int radix;
  41. String value;
  42. XmlPullParser xmlPullParser = parser;
  43. int count = parser.getAttributeCount();
  44. int i = 0;
  45. String serialNumber = null;
  46. String productName = null;
  47. String manufacturerName = null;
  48. int deviceProtocol = -1;
  49. int deviceSubclass = -1;
  50. int deviceClass = -1;
  51. int productId = -1;
  52. int vendorId = -1;
  53. while (i < count) {
  54. String name = xmlPullParser.getAttributeName(i);
  55. String value2 = xmlPullParser.getAttributeValue(i);
  56. if ("manufacturer-name".equals(name)) {
  57. manufacturerName = value2;
  58. } else if ("product-name".equals(name)) {
  59. productName = value2;
  60. } else if ("serial-number".equals(name)) {
  61. serialNumber = value2;
  62. } else {
  63. if (value2 == null || value2.length() <= 2 || value2.charAt(0) != '0' || !(value2.charAt(1) == 'x' || value2.charAt(1) == 'X')) {
  64. radix = 10;
  65. value = value2;
  66. } else {
  67. radix = 16;
  68. value = value2.substring(2);
  69. }
  70. try {
  71. int intValue = Integer.parseInt(value, radix);
  72. if ("vendor-id".equals(name)) {
  73. vendorId = intValue;
  74. } else if ("product-id".equals(name)) {
  75. productId = intValue;
  76. } else if ("class".equals(name)) {
  77. deviceClass = intValue;
  78. } else if ("subclass".equals(name)) {
  79. deviceSubclass = intValue;
  80. } else if ("protocol".equals(name)) {
  81. deviceProtocol = intValue;
  82. }
  83. } catch (NumberFormatException e) {
  84. Slog.e(TAG, "invalid number for field " + name, e);
  85. }
  86. }
  87. i++;
  88. xmlPullParser = parser;
  89. }
  90. return new DeviceFilter(vendorId, productId, deviceClass, deviceSubclass, deviceProtocol, manufacturerName, productName, serialNumber);
  91. }
  92. public void write(XmlSerializer serializer) throws IOException {
  93. serializer.startTag(null, "usb-device");
  94. int i = this.mVendorId;
  95. if (i != -1) {
  96. serializer.attribute(null, "vendor-id", Integer.toString(i));
  97. }
  98. int i2 = this.mProductId;
  99. if (i2 != -1) {
  100. serializer.attribute(null, "product-id", Integer.toString(i2));
  101. }
  102. int i3 = this.mClass;
  103. if (i3 != -1) {
  104. serializer.attribute(null, "class", Integer.toString(i3));
  105. }
  106. int i4 = this.mSubclass;
  107. if (i4 != -1) {
  108. serializer.attribute(null, "subclass", Integer.toString(i4));
  109. }
  110. int i5 = this.mProtocol;
  111. if (i5 != -1) {
  112. serializer.attribute(null, "protocol", Integer.toString(i5));
  113. }
  114. String str = this.mManufacturerName;
  115. if (str != null) {
  116. serializer.attribute(null, "manufacturer-name", str);
  117. }
  118. String str2 = this.mProductName;
  119. if (str2 != null) {
  120. serializer.attribute(null, "product-name", str2);
  121. }
  122. String str3 = this.mSerialNumber;
  123. if (str3 != null) {
  124. serializer.attribute(null, "serial-number", str3);
  125. }
  126. serializer.endTag(null, "usb-device");
  127. }
  128. private boolean matches(int clasz, int subclass, int protocol) {
  129. int i;
  130. int i2;
  131. int i3 = this.mClass;
  132. return (i3 == -1 || clasz == i3) && ((i = this.mSubclass) == -1 || subclass == i) && ((i2 = this.mProtocol) == -1 || protocol == i2);
  133. }
  134. public boolean matches(UsbDevice device) {
  135. if (!(this.mVendorId == -1 || device.getVendorId() == this.mVendorId)) {
  136. return false;
  137. }
  138. if (!(this.mProductId == -1 || device.getProductId() == this.mProductId)) {
  139. return false;
  140. }
  141. if (this.mManufacturerName != null && device.getManufacturerName() == null) {
  142. return false;
  143. }
  144. if (this.mProductName != null && device.getProductName() == null) {
  145. return false;
  146. }
  147. if (this.mSerialNumber != null && device.getSerialNumber() == null) {
  148. return false;
  149. }
  150. if (!(this.mManufacturerName == null || device.getManufacturerName() == null || this.mManufacturerName.equals(device.getManufacturerName()))) {
  151. return false;
  152. }
  153. if (!(this.mProductName == null || device.getProductName() == null || this.mProductName.equals(device.getProductName()))) {
  154. return false;
  155. }
  156. if (!(this.mSerialNumber == null || device.getSerialNumber() == null || this.mSerialNumber.equals(device.getSerialNumber()))) {
  157. return false;
  158. }
  159. if (matches(device.getDeviceClass(), device.getDeviceSubclass(), device.getDeviceProtocol())) {
  160. return true;
  161. }
  162. int count = device.getInterfaceCount();
  163. for (int i = 0; i < count; i++) {
  164. UsbInterface intf = device.getInterface(i);
  165. if (matches(intf.getInterfaceClass(), intf.getInterfaceSubclass(), intf.getInterfaceProtocol())) {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. public boolean contains(DeviceFilter device) {
  172. int i = this.mVendorId;
  173. if (i != -1 && device.mVendorId != i) {
  174. return false;
  175. }
  176. int i2 = this.mProductId;
  177. if (i2 != -1 && device.mProductId != i2) {
  178. return false;
  179. }
  180. String str = this.mManufacturerName;
  181. if (str != null && !Objects.equals(str, device.mManufacturerName)) {
  182. return false;
  183. }
  184. String str2 = this.mProductName;
  185. if (str2 != null && !Objects.equals(str2, device.mProductName)) {
  186. return false;
  187. }
  188. String str3 = this.mSerialNumber;
  189. if (str3 == null || Objects.equals(str3, device.mSerialNumber)) {
  190. return matches(device.mClass, device.mSubclass, device.mProtocol);
  191. }
  192. return false;
  193. }
  194. public boolean equals(Object obj) {
  195. int i;
  196. int i2;
  197. int i3;
  198. int i4;
  199. String str;
  200. String str2;
  201. String str3;
  202. String str4;
  203. String str5;
  204. int i5 = this.mVendorId;
  205. if (i5 == -1 || (i = this.mProductId) == -1 || (i2 = this.mClass) == -1 || (i3 = this.mSubclass) == -1 || (i4 = this.mProtocol) == -1) {
  206. return false;
  207. }
  208. if (obj instanceof DeviceFilter) {
  209. DeviceFilter filter = (DeviceFilter) obj;
  210. if (filter.mVendorId != i5 || filter.mProductId != i || filter.mClass != i2 || filter.mSubclass != i3 || filter.mProtocol != i4) {
  211. return false;
  212. }
  213. if ((filter.mManufacturerName != null && this.mManufacturerName == null) || ((filter.mManufacturerName == null && this.mManufacturerName != null) || ((filter.mProductName != null && this.mProductName == null) || ((filter.mProductName == null && this.mProductName != null) || ((filter.mSerialNumber != null && this.mSerialNumber == null) || (filter.mSerialNumber == null && this.mSerialNumber != null)))))) {
  214. return false;
  215. }
  216. String str6 = filter.mManufacturerName;
  217. if ((str6 == null || (str5 = this.mManufacturerName) == null || str5.equals(str6)) && (((str = filter.mProductName) == null || (str4 = this.mProductName) == null || str4.equals(str)) && ((str2 = filter.mSerialNumber) == null || (str3 = this.mSerialNumber) == null || str3.equals(str2)))) {
  218. return true;
  219. }
  220. return false;
  221. } else if (!(obj instanceof UsbDevice)) {
  222. return false;
  223. } else {
  224. UsbDevice device = (UsbDevice) obj;
  225. if (device.getVendorId() != this.mVendorId || device.getProductId() != this.mProductId || device.getDeviceClass() != this.mClass || device.getDeviceSubclass() != this.mSubclass || device.getDeviceProtocol() != this.mProtocol) {
  226. return false;
  227. }
  228. if ((this.mManufacturerName != null && device.getManufacturerName() == null) || ((this.mManufacturerName == null && device.getManufacturerName() != null) || ((this.mProductName != null && device.getProductName() == null) || ((this.mProductName == null && device.getProductName() != null) || ((this.mSerialNumber != null && device.getSerialNumber() == null) || (this.mSerialNumber == null && device.getSerialNumber() != null)))))) {
  229. return false;
  230. }
  231. if ((device.getManufacturerName() == null || this.mManufacturerName.equals(device.getManufacturerName())) && ((device.getProductName() == null || this.mProductName.equals(device.getProductName())) && (device.getSerialNumber() == null || this.mSerialNumber.equals(device.getSerialNumber())))) {
  232. return true;
  233. }
  234. return false;
  235. }
  236. }
  237. public int hashCode() {
  238. return ((this.mVendorId << 16) | this.mProductId) ^ (((this.mClass << 16) | (this.mSubclass << 8)) | this.mProtocol);
  239. }
  240. public String toString() {
  241. return "DeviceFilter[mVendorId=" + this.mVendorId + ",mProductId=" + this.mProductId + ",mClass=" + this.mClass + ",mSubclass=" + this.mSubclass + ",mProtocol=" + this.mProtocol + ",mManufacturerName=" + this.mManufacturerName + ",mProductName=" + this.mProductName + ",mSerialNumber=" + this.mSerialNumber + "]";
  242. }
  243. public void dump(DualDumpOutputStream dump, String idName, long id) {
  244. long token = dump.start(idName, id);
  245. dump.write("vendor_id", 1120986464257L, this.mVendorId);
  246. dump.write("product_id", 1120986464258L, this.mProductId);
  247. dump.write("class", 1120986464259L, this.mClass);
  248. dump.write("subclass", 1120986464260L, this.mSubclass);
  249. dump.write("protocol", 1120986464261L, this.mProtocol);
  250. dump.write("manufacturer_name", 1138166333446L, this.mManufacturerName);
  251. dump.write("product_name", 1138166333447L, this.mProductName);
  252. dump.write("serial_number", 1138166333448L, this.mSerialNumber);
  253. dump.end(token);
  254. }
  255. }