1 package org.openintents.filemanager;
4 import java.lang.reflect.Method;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.Comparator;
10 import org.openintents.filemanager.util.FileUtils;
11 import org.openintents.filemanager.util.ImageUtils;
12 import org.openintents.filemanager.util.MimeTypes;
14 import android.content.Context;
15 import android.content.Intent;
16 import android.content.pm.ApplicationInfo;
17 import android.content.pm.PackageInfo;
18 import android.content.pm.PackageManager;
19 import android.content.pm.PackageManager.NameNotFoundException;
20 import android.content.pm.ResolveInfo;
21 import android.content.res.Resources.NotFoundException;
22 import android.graphics.drawable.Drawable;
23 import android.net.Uri;
24 import android.os.Handler;
25 import android.os.Message;
26 import android.os.SystemClock;
27 import android.support.v2.os.Build;
28 import android.util.Log;
30 public class DirectoryScanner extends Thread {
32 private static final String TAG = "OIFM_DirScanner";
34 private File currentDirectory;
37 private String mSdCardPath;
38 private Context context;
39 private MimeTypes mMimeTypes;
40 private Handler handler;
41 private long operationStartTime;
42 private String mFilterFiletype;
43 private String mFilterMimetype;
45 private boolean mWriteableOnly;
47 private boolean mDirectoriesOnly;
49 // Update progress bar every n files
50 static final private int PROGRESS_STEPS = 50;
53 private static final String MIME_APK = "application/vnd.android.package-archive";
55 // Cupcake-specific methods
56 static Method formatter_formatFileSize;
59 initializeCupcakeInterface();
64 DirectoryScanner(File directory, Context context, Handler handler, MimeTypes mimeTypes, String filterFiletype, String filterMimetype, String sdCardPath, boolean writeableOnly, boolean directoriesOnly) {
65 super("Directory Scanner");
66 currentDirectory = directory;
67 this.context = context;
68 this.handler = handler;
69 this.mMimeTypes = mimeTypes;
70 this.mFilterFiletype = filterFiletype;
71 this.mFilterMimetype = filterMimetype;
72 this.mSdCardPath = sdCardPath;
73 this.mWriteableOnly = writeableOnly;
74 this.mDirectoriesOnly = directoriesOnly;
77 private void clearData() {
78 // Remove all references so we don't delay the garbage collection.
85 Log.v(TAG, "Scanning directory " + currentDirectory);
87 File[] files = currentDirectory.listFiles();
95 Log.v(TAG, "Scan aborted");
101 Log.v(TAG, "Returned null - inaccessible directory?");
104 totalCount = files.length;
107 operationStartTime = SystemClock.uptimeMillis();
109 Log.v(TAG, "Counting files... (total count=" + totalCount + ")");
113 /** Dir separate for return after sorting*/
114 List<IconifiedText> listDir = new ArrayList<IconifiedText>(totalCount);
115 /** Dir separate for sorting */
116 List<File> listDirFile = new ArrayList<File>(totalCount);
118 /** Files separate for return after sorting*/
119 List<IconifiedText> listFile = new ArrayList<IconifiedText>(totalCount);
120 /** Files separate for sorting */
121 List<File> listFileFile = new ArrayList<File>(totalCount);
123 /** SD card separate for sorting - actually not sorted, so we don't need an ArrayList<File>*/
124 List<IconifiedText> listSdCard = new ArrayList<IconifiedText>(3);
126 boolean noMedia = false;
128 // Cache some commonly used icons.
129 Drawable sdIcon = context.getResources().getDrawable(R.drawable.ic_launcher_sdcard);
130 Drawable folderIcon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
131 Drawable genericFileIcon = context.getResources().getDrawable(R.drawable.icon_file);
133 Drawable currentIcon = null;
135 boolean displayHiddenFiles = PreferenceActivity.getDisplayHiddenFiles(context);
138 for (File currentFile : files){
141 Log.v(TAG, "Scan aborted while checking files");
147 updateProgress(progress, totalCount);
149 //If the user doesn't want to display hidden files and the file is hidden,
150 //skip displaying the file
151 if (!displayHiddenFiles && currentFile.isHidden()){
156 if (currentFile.isDirectory()) {
157 if (currentFile.getAbsolutePath().equals(mSdCardPath)) {
158 currentIcon = sdIcon;
160 listSdCard.add(new IconifiedText(
161 currentFile.getName(), "", currentIcon));
163 if (!mWriteableOnly || currentFile.canWrite()){
164 listDirFile.add(currentFile);
168 String fileName = currentFile.getName();
170 // Is this the ".nomedia" file?
172 if (fileName.equalsIgnoreCase(".nomedia")) {
178 String mimetype = mMimeTypes.getMimeType(fileName);
180 String filetype = FileUtils.getExtension(fileName);
181 boolean ext_allow = filetype.equalsIgnoreCase(mFilterFiletype) || mFilterFiletype == "";
182 boolean mime_allow = mFilterMimetype != null &&
183 (mimetype.contentEquals(mFilterMimetype) || mFilterMimetype.contentEquals("*/*") ||
184 mFilterFiletype == null);
185 if (!mDirectoriesOnly && (ext_allow || mime_allow)) {
186 listFileFile.add(currentFile);
192 Log.v(TAG, "Sorting results...");
194 //Collections.sort(mListSdCard);
195 int sortBy = PreferenceActivity.getSortBy(context);
196 boolean ascending = PreferenceActivity.getAscending(context);
199 Collections.sort(listDirFile, Comparators.getForDirectory(sortBy, ascending));
200 Collections.sort(listFileFile, Comparators.getForFile(sortBy, ascending));
202 for(File f : listDirFile){
203 listDir.add(new IconifiedText(
204 f.getName(), FileUtils.formatDate(context, f.lastModified()), folderIcon));
207 for(File currentFile : listFileFile){
208 String mimetype = mMimeTypes.getMimeType(currentFile.getName());
209 currentIcon = getDrawableForMimetype(currentFile, mimetype);
210 if (currentIcon == null) {
211 currentIcon = genericFileIcon;
213 int width = genericFileIcon.getIntrinsicWidth();
214 int height = genericFileIcon.getIntrinsicHeight();
216 currentIcon = ImageUtils.resizeDrawable(currentIcon, width, height);
223 size = (String) formatter_formatFileSize.invoke(null, context, currentFile.length());
224 } catch (Exception e) {
225 // The file size method is probably null (this is most
226 // likely not a Cupcake phone), or something else went wrong.
227 // Let's fall back to something primitive, like just the number
229 size = Long.toString(currentFile.length() / 1024);
232 // Technically "KB" should come from a string resource,
233 // but this is just a Cupcake 1.1 callback, and KB is universal
237 listFile.add(new IconifiedText(
238 currentFile.getName(), size + " , " + FileUtils.formatDate(
239 context, currentFile.lastModified()), currentIcon));
243 Log.v(TAG, "Sending data back to main thread");
245 DirectoryContents contents = new DirectoryContents();
247 contents.listDir = listDir;
248 contents.listFile = listFile;
249 contents.listSdCard = listSdCard;
250 contents.noMedia = noMedia;
252 Message msg = handler.obtainMessage(FileManagerActivity.MESSAGE_SHOW_DIRECTORY_CONTENTS);
260 private void updateProgress(int progress, int maxProgress) {
261 // Only update the progress bar every n steps...
262 if ((progress % PROGRESS_STEPS) == 0) {
263 // Also don't update for the first second.
264 long curTime = SystemClock.uptimeMillis();
266 if (curTime - operationStartTime < 1000L) {
270 // Okay, send an update.
271 Message msg = handler.obtainMessage(FileManagerActivity.MESSAGE_SET_PROGRESS);
273 msg.arg2 = maxProgress;
279 * Return the Drawable that is associated with a specific mime type
280 * for the VIEW action.
285 Drawable getDrawableForMimetype(File file, String mimetype) {
286 if (mimetype == null) {
290 PackageManager pm = context.getPackageManager();
292 // Returns the icon packaged in files with the .apk MIME type.
293 if(mimetype.equals(MIME_APK)){
294 String path = file.getPath();
295 PackageInfo pInfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
297 ApplicationInfo aInfo = pInfo.applicationInfo;
299 // Bug in SDK versions >= 8. See here: http://code.google.com/p/android/issues/detail?id=9151
300 if(Build.VERSION.SDK_INT >= 8){
301 aInfo.sourceDir = path;
302 aInfo.publicSourceDir = path;
305 return aInfo.loadIcon(pm);
309 int iconResource = mMimeTypes.getIcon(mimetype);
311 if(iconResource > 0){
313 ret = pm.getResourcesForApplication(context.getPackageName()).getDrawable(iconResource);
314 }catch(NotFoundException e){}
315 catch(NameNotFoundException e){}
322 Uri data = FileUtils.getUri(file);
324 Intent intent = new Intent(Intent.ACTION_VIEW);
325 //intent.setType(mimetype);
327 // Let's probe the intent exactly in the same way as the VIEW action
328 // is performed in FileManagerActivity.openFile(..)
329 intent.setDataAndType(data, mimetype);
331 final List<ResolveInfo> lri = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
333 if (lri != null && lri.size() > 0) {
334 //Log.i(TAG, "lri.size()" + lri.size());
336 // return first element
339 // Actually first element should be "best match",
340 // but it seems that more recently installed applications
341 // could be even better match.
342 index = lri.size()-1;
344 final ResolveInfo ri = lri.get(index);
345 return ri.loadIcon(pm);
351 private static void initializeCupcakeInterface() {
353 formatter_formatFileSize = Class.forName("android.text.format.Formatter").getMethod("formatFileSize", Context.class, long.class);
354 } catch (Exception ex) {
355 // This is not cupcake.
362 * The container class for all comparators.
365 public static final int NAME = 1;
366 public static final int SIZE = 2;
367 public static final int LAST_MODIFIED = 3;
370 public static Comparator<File> getForFile(int comparator, boolean ascending){
372 case NAME: return new NameComparator(ascending);
373 case SIZE: return new SizeComparator(ascending);
374 case LAST_MODIFIED: return new LastModifiedComparator(ascending);
375 default: return null;
378 public static Comparator<File> getForDirectory(int comparator, boolean ascending){
380 case NAME: return new NameComparator(ascending);
381 case SIZE: return new NameComparator(ascending); //Not a bug! Getting directory's size is verry slow
382 case LAST_MODIFIED: return new LastModifiedComparator(ascending);
383 default: return null;
389 abstract class FileComparator implements Comparator<File>{
390 protected boolean ascending = true;
392 public FileComparator(boolean asc){
396 public FileComparator(){
400 public int compare(File f1, File f2){
401 return comp((ascending ? f1 : f2), (ascending ? f2 : f1));
404 protected abstract int comp(File f1, File f2);
407 class NameComparator extends FileComparator{
408 public NameComparator(boolean asc){
412 protected int comp(File f1, File f2) {
413 return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase());
417 class SizeComparator extends FileComparator{
418 public SizeComparator(boolean asc){
422 protected int comp(File f1, File f2) {
423 return ((Long)f1.length()).compareTo(f2.length());
427 private long getFileSize(File f){
431 for(File file : f.listFiles())
432 ret += getFileSize(file);
439 class LastModifiedComparator extends FileComparator{
440 public LastModifiedComparator(boolean asc){
444 protected int comp(File f1, File f2) {
445 return ((Long)f1.lastModified()).compareTo(f2.lastModified());