1/*
2 * Copyright (C) 2010 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 com.android.gallery3d.data;
18
19import com.android.gallery3d.app.GalleryApp;
20
21class MtpSource extends MediaSource {
22    private static final String TAG = "MtpSource";
23
24    private static final int MTP_DEVICESET = 0;
25    private static final int MTP_DEVICE = 1;
26    private static final int MTP_ITEM = 2;
27
28    GalleryApp mApplication;
29    PathMatcher mMatcher;
30    MtpContext mMtpContext;
31
32    public MtpSource(GalleryApp application) {
33        super("mtp");
34        mApplication = application;
35        mMatcher = new PathMatcher();
36        mMatcher.add("/mtp", MTP_DEVICESET);
37        mMatcher.add("/mtp/*", MTP_DEVICE);
38        mMatcher.add("/mtp/item/*/*", MTP_ITEM);
39        mMtpContext = new MtpContext(mApplication.getAndroidContext());
40    }
41
42    @Override
43    public MediaObject createMediaObject(Path path) {
44        switch (mMatcher.match(path)) {
45            case MTP_DEVICESET: {
46                return new MtpDeviceSet(path, mApplication, mMtpContext);
47            }
48            case MTP_DEVICE: {
49                int deviceId = mMatcher.getIntVar(0);
50                return new MtpDevice(path, mApplication, deviceId, mMtpContext);
51            }
52            case MTP_ITEM: {
53                int deviceId = mMatcher.getIntVar(0);
54                int objectId = mMatcher.getIntVar(1);
55                return new MtpImage(path, mApplication, deviceId, objectId, mMtpContext);
56            }
57            default:
58                throw new RuntimeException("bad path: " + path);
59        }
60    }
61
62    @Override
63    public void pause() {
64        mMtpContext.pause();
65    }
66
67    @Override
68    public void resume() {
69        mMtpContext.resume();
70    }
71}
72