1/*
2 * Copyright (C) 2013 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.ingest;
18
19import android.content.Context;
20import android.mtp.MtpDevice;
21import android.mtp.MtpObjectInfo;
22import android.os.Environment;
23import android.os.PowerManager;
24
25import com.android.gallery3d.util.GalleryUtils;
26
27import java.io.File;
28import java.util.Collection;
29import java.util.LinkedList;
30import java.util.List;
31
32public class ImportTask implements Runnable {
33
34    public interface Listener {
35        void onImportProgress(int visitedCount, int totalCount, String pathIfSuccessful);
36
37        void onImportFinish(Collection<MtpObjectInfo> objectsNotImported, int visitedCount);
38    }
39
40    static private final String WAKELOCK_LABEL = "MTP Import Task";
41
42    private Listener mListener;
43    private String mDestAlbumName;
44    private Collection<MtpObjectInfo> mObjectsToImport;
45    private MtpDevice mDevice;
46    private PowerManager.WakeLock mWakeLock;
47
48    public ImportTask(MtpDevice device, Collection<MtpObjectInfo> objectsToImport,
49            String destAlbumName, Context context) {
50        mDestAlbumName = destAlbumName;
51        mObjectsToImport = objectsToImport;
52        mDevice = device;
53        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
54        mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, WAKELOCK_LABEL);
55    }
56
57    public void setListener(Listener listener) {
58        mListener = listener;
59    }
60
61    @Override
62    public void run() {
63        mWakeLock.acquire();
64        try {
65            List<MtpObjectInfo> objectsNotImported = new LinkedList<MtpObjectInfo>();
66            int visited = 0;
67            int total = mObjectsToImport.size();
68            File dest = new File(Environment.getExternalStorageDirectory(), mDestAlbumName);
69            dest.mkdirs();
70            for (MtpObjectInfo object : mObjectsToImport) {
71                visited++;
72                String importedPath = null;
73                if (GalleryUtils.hasSpaceForSize(object.getCompressedSize())) {
74                    importedPath = new File(dest, object.getName()).getAbsolutePath();
75                    if (!mDevice.importFile(object.getObjectHandle(), importedPath)) {
76                        importedPath = null;
77                    }
78                }
79                if (importedPath == null) {
80                    objectsNotImported.add(object);
81                }
82                if (mListener != null) {
83                    mListener.onImportProgress(visited, total, importedPath);
84                }
85            }
86            if (mListener != null) {
87                mListener.onImportFinish(objectsNotImported, visited);
88            }
89        } finally {
90            mListener = null;
91            mWakeLock.release();
92        }
93    }
94}
95