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
17package android.mtp;
18
19/**
20 * This class encapsulates information about a MTP event.
21 * This corresponds to the events described in appendix G of the MTP specification.
22 */
23public class MtpEvent {
24    /** Event code for UNDEFINED event */
25    public static final int EVENT_UNDEFINED = 0x4000;
26    /** Event code for CANCEL_TRANSACTION event */
27    public static final int EVENT_CANCEL_TRANSACTION = 0x4001;
28    /** Event code for OBJECT_ADDED event */
29    public static final int EVENT_OBJECT_ADDED = 0x4002;
30    /** Event code for OBJECT_REMOVED event */
31    public static final int EVENT_OBJECT_REMOVED = 0x4003;
32    /** Event code for STORE_ADDED event */
33    public static final int EVENT_STORE_ADDED = 0x4004;
34    /** Event code for STORE_REMOVED event */
35    public static final int EVENT_STORE_REMOVED = 0x4005;
36    /** Event code for DEVICE_PROP_CHANGED event */
37    public static final int EVENT_DEVICE_PROP_CHANGED = 0x4006;
38    /** Event code for OBJECT_INFO_CHANGED event */
39    public static final int EVENT_OBJECT_INFO_CHANGED = 0x4007;
40    /** Event code for DEVICE_INFO_CHANGED event */
41    public static final int EVENT_DEVICE_INFO_CHANGED = 0x4008;
42    /** Event code for REQUEST_OBJECT_TRANSFER event */
43    public static final int EVENT_REQUEST_OBJECT_TRANSFER = 0x4009;
44    /** Event code for STORE_FULL event */
45    public static final int EVENT_STORE_FULL = 0x400A;
46    /** Event code for DEVICE_RESET event */
47    public static final int EVENT_DEVICE_RESET = 0x400B;
48    /** Event code for STORAGE_INFO_CHANGED event */
49    public static final int EVENT_STORAGE_INFO_CHANGED = 0x400C;
50    /** Event code for CAPTURE_COMPLETE event */
51    public static final int EVENT_CAPTURE_COMPLETE = 0x400D;
52    /** Event code for UNREPORTED_STATUS event */
53    public static final int EVENT_UNREPORTED_STATUS = 0x400E;
54    /** Event code for OBJECT_PROP_CHANGED event */
55    public static final int EVENT_OBJECT_PROP_CHANGED = 0xC801;
56    /** Event code for OBJECT_PROP_DESC_CHANGED event */
57    public static final int EVENT_OBJECT_PROP_DESC_CHANGED = 0xC802;
58    /** Event code for OBJECT_REFERENCES_CHANGED event */
59    public static final int EVENT_OBJECT_REFERENCES_CHANGED = 0xC803;
60
61    private int mEventCode = EVENT_UNDEFINED;
62
63    // Parameters for event. The interpretation of event parameters depends upon mEventCode.
64    private int mParameter1;
65    private int mParameter2;
66    private int mParameter3;
67
68    /**
69     * MtpEvent is instantiated by JNI.
70     */
71    private MtpEvent() {}
72
73    /**
74     * Returns event code of MTP event.
75     * See the USB-IF MTP specification for the details of event constants.
76     * @return event code
77     */
78    public int getEventCode() { return mEventCode; }
79
80    /**
81     * Obtains the first event parameter.
82     */
83    public int getParameter1() { return mParameter1; }
84
85    /**
86     * Obtains the second event parameter.
87     */
88    public int getParameter2() { return mParameter2; }
89
90    /**
91     * Obtains the third event parameter.
92     */
93    public int getParameter3() { return mParameter3; }
94
95    /**
96     * Obtains objectHandle event parameter.
97     *
98     * @see #EVENT_OBJECT_ADDED
99     * @see #EVENT_OBJECT_REMOVED
100     * @see #EVENT_OBJECT_INFO_CHANGED
101     * @see #EVENT_REQUEST_OBJECT_TRANSFER
102     * @see #EVENT_OBJECT_PROP_CHANGED
103     * @see #EVENT_OBJECT_REFERENCES_CHANGED
104     */
105    public int getObjectHandle() {
106        switch (mEventCode) {
107            case EVENT_OBJECT_ADDED:
108                return mParameter1;
109            case EVENT_OBJECT_REMOVED:
110                return mParameter1;
111            case EVENT_OBJECT_INFO_CHANGED:
112                return mParameter1;
113            case EVENT_REQUEST_OBJECT_TRANSFER:
114                return mParameter1;
115            case EVENT_OBJECT_PROP_CHANGED:
116                return mParameter1;
117            case EVENT_OBJECT_REFERENCES_CHANGED:
118                return mParameter1;
119            default:
120                throw new IllegalParameterAccess("objectHandle", mEventCode);
121        }
122    }
123
124    /**
125     * Obtains storageID event parameter.
126     *
127     * @see #EVENT_STORE_ADDED
128     * @see #EVENT_STORE_REMOVED
129     * @see #EVENT_STORE_FULL
130     * @see #EVENT_STORAGE_INFO_CHANGED
131     */
132    public int getStorageId() {
133        switch (mEventCode) {
134            case EVENT_STORE_ADDED:
135                return mParameter1;
136            case EVENT_STORE_REMOVED:
137                return mParameter1;
138            case EVENT_STORE_FULL:
139                return mParameter1;
140            case EVENT_STORAGE_INFO_CHANGED:
141                return mParameter1;
142            default:
143                throw new IllegalParameterAccess("storageID", mEventCode);
144        }
145    }
146
147    /**
148     * Obtains devicePropCode event parameter.
149     *
150     * @see #EVENT_DEVICE_PROP_CHANGED
151     */
152    public int getDevicePropCode() {
153        switch (mEventCode) {
154            case EVENT_DEVICE_PROP_CHANGED:
155                return mParameter1;
156            default:
157                throw new IllegalParameterAccess("devicePropCode", mEventCode);
158        }
159    }
160
161    /**
162     * Obtains transactionID event parameter.
163     *
164     * @see #EVENT_CAPTURE_COMPLETE
165     */
166    public int getTransactionId() {
167        switch (mEventCode) {
168            case EVENT_CAPTURE_COMPLETE:
169                return mParameter1;
170            default:
171                throw new IllegalParameterAccess("transactionID", mEventCode);
172        }
173    }
174
175    /**
176     * Obtains objectPropCode event parameter.
177     *
178     * @see #EVENT_OBJECT_PROP_CHANGED
179     * @see #EVENT_OBJECT_PROP_DESC_CHANGED
180     */
181    public int getObjectPropCode() {
182        switch (mEventCode) {
183            case EVENT_OBJECT_PROP_CHANGED:
184                return mParameter2;
185            case EVENT_OBJECT_PROP_DESC_CHANGED:
186                return mParameter1;
187            default:
188                throw new IllegalParameterAccess("objectPropCode", mEventCode);
189        }
190    }
191
192    /**
193     * Obtains objectFormatCode event parameter.
194     *
195     * @see #EVENT_OBJECT_PROP_DESC_CHANGED
196     */
197    public int getObjectFormatCode() {
198        switch (mEventCode) {
199            case EVENT_OBJECT_PROP_DESC_CHANGED:
200                return mParameter2;
201            default:
202                throw new IllegalParameterAccess("objectFormatCode", mEventCode);
203        }
204    }
205
206    private static class IllegalParameterAccess extends UnsupportedOperationException {
207        public IllegalParameterAccess(String propertyName, int eventCode) {
208            super("Cannot obtain " + propertyName + " for the event: " + eventCode + ".");
209        }
210    }
211}
212