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