1 package com.android.gallery3d.data; 2 3 import com.android.gallery3d.R; 4 import com.android.gallery3d.util.GalleryUtils; 5 6 import android.content.Context; 7 import android.hardware.usb.UsbDevice; 8 import android.media.MediaScannerConnection; 9 import android.media.MediaScannerConnection.MediaScannerConnectionClient; 10 import android.mtp.MtpObjectInfo; 11 import android.net.Uri; 12 import android.os.Environment; 13 import android.util.Log; 14 import android.widget.Toast; 15 16 import java.io.File; 17 import java.util.ArrayList; 18 import java.util.List; 19 20 public class MtpContext implements MtpClient.Listener { 21 private static final String TAG = "MtpContext"; 22 23 public static final String NAME_IMPORTED_FOLDER = "Imported"; 24 25 private ScannerClient mScannerClient; 26 private Context mContext; 27 private MtpClient mClient; 28 29 private static final class ScannerClient implements MediaScannerConnectionClient { 30 ArrayList<String> mPaths = new ArrayList<String>(); 31 MediaScannerConnection mScannerConnection; 32 boolean mConnected; 33 Object mLock = new Object(); 34 35 public ScannerClient(Context context) { 36 mScannerConnection = new MediaScannerConnection(context, this); 37 } 38 39 public void scanPath(String path) { 40 synchronized (mLock) { 41 if (mConnected) { 42 mScannerConnection.scanFile(path, null); 43 } else { 44 mPaths.add(path); 45 mScannerConnection.connect(); 46 } 47 } 48 } 49 50 @Override 51 public void onMediaScannerConnected() { 52 synchronized (mLock) { 53 mConnected = true; 54 if (!mPaths.isEmpty()) { 55 for (String path : mPaths) { 56 mScannerConnection.scanFile(path, null); 57 } 58 mPaths.clear(); 59 } 60 } 61 } 62 63 @Override 64 public void onScanCompleted(String path, Uri uri) { 65 } 66 } 67 68 public MtpContext(Context context) { 69 mContext = context; 70 mScannerClient = new ScannerClient(context); 71 mClient = new MtpClient(mContext); 72 } 73 74 public void pause() { 75 mClient.removeListener(this); 76 } 77 78 public void resume() { 79 mClient.addListener(this); 80 notifyDirty(); 81 } 82 83 public void deviceAdded(android.mtp.MtpDevice device) { 84 notifyDirty(); 85 showToast(R.string.camera_connected); 86 } 87 88 public void deviceRemoved(android.mtp.MtpDevice device) { 89 notifyDirty(); 90 showToast(R.string.camera_disconnected); 91 } 92 93 private void notifyDirty() { 94 mContext.getContentResolver().notifyChange(Uri.parse("mtp://"), null); 95 } 96 97 private void showToast(final int msg) { 98 Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show(); 99 } 100 101 public MtpClient getMtpClient() { 102 return mClient; 103 } 104 105 public boolean copyFile(String deviceName, MtpObjectInfo objInfo) { 106 if (GalleryUtils.hasSpaceForSize(objInfo.getCompressedSize())) { 107 File dest = Environment.getExternalStorageDirectory(); 108 dest = new File(dest, NAME_IMPORTED_FOLDER); 109 dest.mkdirs(); 110 String destPath = new File(dest, objInfo.getName()).getAbsolutePath(); 111 int objectId = objInfo.getObjectHandle(); 112 if (mClient.importFile(deviceName, objectId, destPath)) { 113 mScannerClient.scanPath(destPath); 114 return true; 115 } 116 } else { 117 Log.w(TAG, "No space to import " + objInfo.getName() + 118 " whose size = " + objInfo.getCompressedSize()); 119 } 120 return false; 121 } 122 123 public boolean copyAlbum(String deviceName, String albumName, 124 List<MtpObjectInfo> children) { 125 File dest = Environment.getExternalStorageDirectory(); 126 dest = new File(dest, albumName); 127 dest.mkdirs(); 128 int success = 0; 129 for (MtpObjectInfo child : children) { 130 if (!GalleryUtils.hasSpaceForSize(child.getCompressedSize())) continue; 131 132 File importedFile = new File(dest, child.getName()); 133 String path = importedFile.getAbsolutePath(); 134 if (mClient.importFile(deviceName, child.getObjectHandle(), path)) { 135 mScannerClient.scanPath(path); 136 success++; 137 } 138 } 139 return success == children.size(); 140 } 141 } 142