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.R; 20import com.android.gallery3d.app.GalleryApp; 21import com.android.gallery3d.util.MediaSetUtils; 22 23import android.mtp.MtpDeviceInfo; 24import android.net.Uri; 25import android.util.Log; 26 27import java.util.ArrayList; 28import java.util.Collections; 29import java.util.List; 30 31// MtpDeviceSet -- MtpDevice -- MtpImage 32public class MtpDeviceSet extends MediaSet { 33 private static final String TAG = "MtpDeviceSet"; 34 35 private GalleryApp mApplication; 36 private final ArrayList<MediaSet> mDeviceSet = new ArrayList<MediaSet>(); 37 private final ChangeNotifier mNotifier; 38 private final MtpContext mMtpContext; 39 private final String mName; 40 41 public MtpDeviceSet(Path path, GalleryApp application, MtpContext mtpContext) { 42 super(path, nextVersionNumber()); 43 mApplication = application; 44 mNotifier = new ChangeNotifier(this, Uri.parse("mtp://"), application); 45 mMtpContext = mtpContext; 46 mName = application.getResources().getString(R.string.set_label_mtp_devices); 47 } 48 49 private void loadDevices() { 50 DataManager dataManager = mApplication.getDataManager(); 51 // Enumerate all devices 52 mDeviceSet.clear(); 53 List<android.mtp.MtpDevice> devices = mMtpContext.getMtpClient().getDeviceList(); 54 Log.v(TAG, "loadDevices: " + devices + ", size=" + devices.size()); 55 for (android.mtp.MtpDevice mtpDevice : devices) { 56 int deviceId = mtpDevice.getDeviceId(); 57 Path childPath = mPath.getChild(deviceId); 58 MtpDevice device = (MtpDevice) dataManager.peekMediaObject(childPath); 59 if (device == null) { 60 device = new MtpDevice(childPath, mApplication, deviceId, mMtpContext); 61 } 62 Log.d(TAG, "add device " + device); 63 mDeviceSet.add(device); 64 } 65 66 Collections.sort(mDeviceSet, MediaSetUtils.NAME_COMPARATOR); 67 for (int i = 0, n = mDeviceSet.size(); i < n; i++) { 68 mDeviceSet.get(i).reload(); 69 } 70 } 71 72 public static String getDeviceName(MtpContext mtpContext, int deviceId) { 73 android.mtp.MtpDevice device = mtpContext.getMtpClient().getDevice(deviceId); 74 if (device == null) { 75 return ""; 76 } 77 MtpDeviceInfo info = device.getDeviceInfo(); 78 if (info == null) { 79 return ""; 80 } 81 String manufacturer = info.getManufacturer().trim(); 82 String model = info.getModel().trim(); 83 return manufacturer + " " + model; 84 } 85 86 @Override 87 public MediaSet getSubMediaSet(int index) { 88 return index < mDeviceSet.size() ? mDeviceSet.get(index) : null; 89 } 90 91 @Override 92 public int getSubMediaSetCount() { 93 return mDeviceSet.size(); 94 } 95 96 @Override 97 public String getName() { 98 return mName; 99 } 100 101 @Override 102 public long reload() { 103 if (mNotifier.isDirty()) { 104 mDataVersion = nextVersionNumber(); 105 loadDevices(); 106 } 107 return mDataVersion; 108 } 109} 110