15a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann/*
25a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * Copyright 2017 The Android Open Source Project
35a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann *
45a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * Licensed under the Apache License, Version 2.0 (the "License");
55a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * you may not use this file except in compliance with the License.
65a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * You may obtain a copy of the License at
75a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann *
85a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann *      http://www.apache.org/licenses/LICENSE-2.0
95a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann *
105a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * Unless required by applicable law or agreed to in writing, software
115a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * distributed under the License is distributed on an "AS IS" BASIS,
125a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * See the License for the specific language governing permissions and
145a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * limitations under the License.
155a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann */
165a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
175a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmannpackage android.hardware.usb;
185a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
19371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmannimport android.annotation.NonNull;
20371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmannimport android.service.usb.UsbAccessoryFilterProto;
21371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann
22371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmannimport com.android.internal.util.dump.DualDumpOutputStream;
23371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann
245a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmannimport org.xmlpull.v1.XmlPullParser;
255a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmannimport org.xmlpull.v1.XmlPullParserException;
265a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmannimport org.xmlpull.v1.XmlSerializer;
275a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
285a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmannimport java.io.IOException;
295a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmannimport java.util.Objects;
305a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
315a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann/**
325a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * This class is used to describe a USB accessory.
335a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * When used in HashMaps all values must be specified,
345a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * but wildcards can be used for any of the fields in
355a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * the package meta-data.
365a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann *
375a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann * @hide
385a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann */
395a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmannpublic class AccessoryFilter {
405a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    // USB accessory manufacturer (or null for unspecified)
415a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public final String mManufacturer;
425a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    // USB accessory model (or null for unspecified)
435a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public final String mModel;
445a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    // USB accessory version (or null for unspecified)
455a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public final String mVersion;
465a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
475a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public AccessoryFilter(String manufacturer, String model, String version) {
485a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        mManufacturer = manufacturer;
495a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        mModel = model;
505a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        mVersion = version;
515a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    }
525a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
535a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public AccessoryFilter(UsbAccessory accessory) {
545a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        mManufacturer = accessory.getManufacturer();
555a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        mModel = accessory.getModel();
565a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        mVersion = accessory.getVersion();
575a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    }
585a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
595a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public static AccessoryFilter read(XmlPullParser parser)
605a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            throws XmlPullParserException, IOException {
615a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        String manufacturer = null;
625a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        String model = null;
635a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        String version = null;
645a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
655a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        int count = parser.getAttributeCount();
665a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        for (int i = 0; i < count; i++) {
675a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            String name = parser.getAttributeName(i);
685a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            String value = parser.getAttributeValue(i);
695a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
705a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            if ("manufacturer".equals(name)) {
715a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                manufacturer = value;
725a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            } else if ("model".equals(name)) {
735a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                model = value;
745a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            } else if ("version".equals(name)) {
755a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                version = value;
765a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            }
775a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        }
785a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        return new AccessoryFilter(manufacturer, model, version);
795a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    }
805a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
815a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public void write(XmlSerializer serializer)throws IOException {
825a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        serializer.startTag(null, "usb-accessory");
835a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        if (mManufacturer != null) {
845a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            serializer.attribute(null, "manufacturer", mManufacturer);
855a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        }
865a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        if (mModel != null) {
875a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            serializer.attribute(null, "model", mModel);
885a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        }
895a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        if (mVersion != null) {
905a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            serializer.attribute(null, "version", mVersion);
915a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        }
925a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        serializer.endTag(null, "usb-accessory");
935a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    }
945a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
955a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public boolean matches(UsbAccessory acc) {
965a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        if (mManufacturer != null && !acc.getManufacturer().equals(mManufacturer)) return false;
975a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        if (mModel != null && !acc.getModel().equals(mModel)) return false;
985a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        return !(mVersion != null && !acc.getVersion().equals(mVersion));
995a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    }
1005a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
1015a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    /**
1025a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann     * Is the accessories described {@code accessory} covered by this filter?
1035a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann     *
1045a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann     * @param accessory A filter describing the accessory
1055a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann     *
1065a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann     * @return {@code true} iff this the filter covers the accessory
1075a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann     */
1085a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public boolean contains(AccessoryFilter accessory) {
1095a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        if (mManufacturer != null && !Objects.equals(accessory.mManufacturer, mManufacturer)) {
1105a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            return false;
1115a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        }
1125a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        if (mModel != null && !Objects.equals(accessory.mModel, mModel)) return false;
1135a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        return !(mVersion != null && !Objects.equals(accessory.mVersion, mVersion));
1145a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    }
1155a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
1165a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    @Override
1175a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public boolean equals(Object obj) {
1185a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        // can't compare if we have wildcard strings
1195a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        if (mManufacturer == null || mModel == null || mVersion == null) {
1205a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            return false;
1215a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        }
1225a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        if (obj instanceof AccessoryFilter) {
1235a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            AccessoryFilter filter = (AccessoryFilter)obj;
1245a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            return (mManufacturer.equals(filter.mManufacturer) &&
1255a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                    mModel.equals(filter.mModel) &&
1265a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                    mVersion.equals(filter.mVersion));
1275a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        }
1285a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        if (obj instanceof UsbAccessory) {
1295a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            UsbAccessory accessory = (UsbAccessory)obj;
1305a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann            return (mManufacturer.equals(accessory.getManufacturer()) &&
1315a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                    mModel.equals(accessory.getModel()) &&
1325a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                    mVersion.equals(accessory.getVersion()));
1335a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        }
1345a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        return false;
1355a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    }
1365a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
1375a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    @Override
1385a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public int hashCode() {
1395a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^
1405a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                (mModel == null ? 0 : mModel.hashCode()) ^
1415a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                (mVersion == null ? 0 : mVersion.hashCode()));
1425a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    }
1435a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann
1445a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    @Override
1455a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    public String toString() {
1465a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann        return "AccessoryFilter[mManufacturer=\"" + mManufacturer +
1475a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                "\", mModel=\"" + mModel +
1485a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann                "\", mVersion=\"" + mVersion + "\"]";
1495a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann    }
150371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann
151371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann    /**
152371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann     * Write a description of the filter to a dump stream.
153371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann     */
154371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann    public void dump(@NonNull DualDumpOutputStream dump, String idName, long id) {
155371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann        long token = dump.start(idName, id);
156371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann
157371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann        dump.write("manufacturer", UsbAccessoryFilterProto.MANUFACTURER, mManufacturer);
158371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann        dump.write("model", UsbAccessoryFilterProto.MODEL, mModel);
159371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann        dump.write("version", UsbAccessoryFilterProto.VERSION, mVersion);
160371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann
161371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann        dump.end(token);
162371a3b879ba82bbe5a4d914328a20659131d0220Philip P. Moltmann    }
1635a633c6e4d9661c574ac34e891da22ec62b5e383Philip P. Moltmann}
164