Home | History | Annotate | Download | only in ingest
      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 
     17 package com.android.gallery3d.ingest;
     18 
     19 import android.content.Context;
     20 import android.mtp.MtpDevice;
     21 import android.mtp.MtpObjectInfo;
     22 import android.os.Environment;
     23 import android.os.PowerManager;
     24 
     25 import com.android.gallery3d.util.GalleryUtils;
     26 
     27 import java.io.File;
     28 import java.util.Collection;
     29 import java.util.LinkedList;
     30 import java.util.List;
     31 
     32 public 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             mListener.onImportProgress(visited, total, null);
     69             File dest = new File(Environment.getExternalStorageDirectory(), mDestAlbumName);
     70             dest.mkdirs();
     71             for (MtpObjectInfo object : mObjectsToImport) {
     72                 visited++;
     73                 String importedPath = null;
     74                 if (GalleryUtils.hasSpaceForSize(object.getCompressedSize())) {
     75                     importedPath = new File(dest, object.getName()).getAbsolutePath();
     76                     if (!mDevice.importFile(object.getObjectHandle(), importedPath)) {
     77                         importedPath = null;
     78                     }
     79                 }
     80                 if (importedPath == null) {
     81                     objectsNotImported.add(object);
     82                 }
     83                 if (mListener != null) {
     84                     mListener.onImportProgress(visited, total, importedPath);
     85                 }
     86             }
     87             if (mListener != null) {
     88                 mListener.onImportFinish(objectsNotImported, visited);
     89             }
     90         } finally {
     91             mListener = null;
     92             mWakeLock.release();
     93         }
     94     }
     95 }
     96