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