packages/apps/FileManager
Révision | 2d8f1f6a97383b1431d180da74dc3e049ad959c8 (tree) |
---|---|
l'heure | 2011-06-29 14:37:58 |
Auteur | Joe Berria <nexesdevelopment@gmai...> |
Commiter | Joe Berria |
fixed problem with slow loading thumbnails.
@@ -81,6 +81,7 @@ public class DirContentActivity extends Fragment implements OnItemClickListener, | ||
81 | 81 | private FileManager mFileMang; |
82 | 82 | private EventHandler mHandler; |
83 | 83 | private MultiSelectHandler mMultiSelect; |
84 | + private ThumbnailCreator mThumbnail; | |
84 | 85 | private static OnBookMarkAddListener mBookmarkList; |
85 | 86 | |
86 | 87 | private LinearLayout mPathView, mMultiSelectView; |
@@ -467,6 +468,9 @@ public class DirContentActivity extends Fragment implements OnItemClickListener, | ||
467 | 468 | } |
468 | 469 | |
469 | 470 | if(file.isDirectory() && !mActionModeSelected ) { |
471 | + if (mThumbnail != null) | |
472 | + mThumbnail = null; | |
473 | + | |
470 | 474 | addBackButton(name, true); |
471 | 475 | |
472 | 476 | } else if (!file.isDirectory() && !mActionModeSelected ) { |
@@ -616,6 +620,9 @@ public class DirContentActivity extends Fragment implements OnItemClickListener, | ||
616 | 620 | if(mActionModeSelected || mMultiSelectOn) |
617 | 621 | return; |
618 | 622 | |
623 | + if (mThumbnail != null) | |
624 | + mThumbnail = null; | |
625 | + | |
619 | 626 | mData = mFileMang.setHomeDir(name); |
620 | 627 | mDelegate.notifyDataSetChanged(); |
621 | 628 |
@@ -870,20 +877,20 @@ public class DirContentActivity extends Fragment implements OnItemClickListener, | ||
870 | 877 | private final int GB = MG * KB; |
871 | 878 | |
872 | 879 | private DataViewHolder mHolder; |
873 | - private ThumbnailCreator mThumbnail; | |
874 | 880 | private String mName; |
875 | 881 | |
876 | 882 | public DataAdapter(Context context, int layout, ArrayList<String> data) { |
877 | 883 | super(context, layout, data); |
878 | - mThumbnail = new ThumbnailCreator(72, 72); | |
879 | - | |
880 | 884 | } |
881 | 885 | |
882 | 886 | @Override |
883 | 887 | public View getView(int position, View view, ViewGroup parent) { |
884 | 888 | String ext; |
885 | 889 | File file = null; |
886 | - String current = mFileMang.getCurrentDir(); | |
890 | + String current = mFileMang.getCurrentDir(); | |
891 | + | |
892 | + if (mThumbnail == null) | |
893 | + mThumbnail = new ThumbnailCreator(72, 72); | |
887 | 894 | |
888 | 895 | mName = mData.get(position); |
889 | 896 | file = new File(current + "/" + mName); |
@@ -973,15 +980,16 @@ public class DirContentActivity extends Fragment implements OnItemClickListener, | ||
973 | 980 | if (thumb == null) { |
974 | 981 | final Handler handle = new Handler(new Handler.Callback() { |
975 | 982 | public boolean handleMessage(Message msg) { |
976 | - mHolder.mIcon.setImageDrawable((BitmapDrawable)msg.obj); | |
977 | - mHolder.mIcon.setScaleType(ScaleType.CENTER); | |
978 | 983 | notifyDataSetChanged(); |
979 | 984 | |
980 | 985 | return true; |
981 | 986 | } |
982 | 987 | }); |
988 | + | |
989 | + mThumbnail.createNewThumbnail(mData, mFileMang.getCurrentDir(), handle); | |
983 | 990 | |
984 | - mThumbnail.createNewThumbnail(file.getPath(), handle); | |
991 | + if (!mThumbnail.isAlive()) | |
992 | + mThumbnail.start(); | |
985 | 993 | |
986 | 994 | } else { |
987 | 995 | mHolder.mIcon.setImageDrawable(thumb); |
@@ -24,18 +24,22 @@ import android.graphics.drawable.BitmapDrawable; | ||
24 | 24 | import android.os.Handler; |
25 | 25 | import android.os.Message; |
26 | 26 | import android.view.Gravity; |
27 | -import android.util.Log; | |
28 | 27 | |
29 | 28 | import java.lang.ref.SoftReference; |
30 | 29 | import java.util.HashMap; |
30 | +import java.util.ArrayList; | |
31 | 31 | import java.io.File; |
32 | 32 | |
33 | -public class ThumbnailCreator { | |
34 | - | |
33 | +public class ThumbnailCreator extends Thread { | |
35 | 34 | private int mWidth; |
36 | 35 | private int mHeight; |
37 | 36 | private SoftReference<Bitmap> mThumb; |
38 | 37 | private static HashMap<String, BitmapDrawable> mCacheMap = null; |
38 | + | |
39 | + //test vars | |
40 | + private ArrayList<String> files; | |
41 | + private String dir; | |
42 | + private Handler handler; | |
39 | 43 | |
40 | 44 | public ThumbnailCreator(int width, int height) { |
41 | 45 | mHeight = height; |
@@ -49,51 +53,72 @@ public class ThumbnailCreator { | ||
49 | 53 | return mCacheMap.get(name); |
50 | 54 | } |
51 | 55 | |
52 | - /* | |
53 | - * This needs to be done properly. | |
54 | - */ | |
55 | - public void createNewThumbnail(final String imageName, final Handler handler) { | |
56 | - Thread thread = new Thread() { | |
56 | + @Override | |
57 | + public void run() { | |
58 | + int len = files.size(); | |
59 | + | |
60 | + for (int i = 0; i < len; i++) { | |
61 | + final File file = new File(dir + "/" + files.get(i)); | |
57 | 62 | |
58 | - public void run() { | |
59 | - File file = new File(imageName); | |
63 | + if (isImageFile(file.getName())) { | |
60 | 64 | long len_kb = file.length() / 1024; |
61 | - | |
65 | + | |
62 | 66 | BitmapFactory.Options options = new BitmapFactory.Options(); |
63 | 67 | options.outWidth = mWidth; |
64 | 68 | options.outHeight = mHeight; |
65 | 69 | |
66 | - if (len_kb > 400 && len_kb < 1600) { | |
70 | + if (len_kb > 500 && len_kb < 2000) { | |
67 | 71 | options.inSampleSize = 16; |
68 | - mThumb = new SoftReference<Bitmap>(BitmapFactory.decodeFile(imageName, options)); | |
72 | + options.inPurgeable = true; | |
73 | + options.inPreferQualityOverSpeed = false; | |
74 | + mThumb = new SoftReference<Bitmap>(BitmapFactory.decodeFile(file.getPath(), options)); | |
69 | 75 | |
70 | - } else if (len_kb >= 1600) { | |
76 | + } else if (len_kb >= 2000) { | |
71 | 77 | options.inSampleSize = 32; |
72 | - mThumb = new SoftReference<Bitmap>(BitmapFactory.decodeFile(imageName, options)); | |
78 | + options.inPurgeable = true; | |
79 | + options.inPreferQualityOverSpeed = false; | |
80 | + mThumb = new SoftReference<Bitmap>(BitmapFactory.decodeFile(file.getPath(), options)); | |
73 | 81 | |
74 | - } else if (len_kb <= 400) { | |
82 | + } else if (len_kb <= 500) { | |
83 | + options.inPurgeable = true; | |
75 | 84 | mThumb = new SoftReference<Bitmap>(Bitmap.createScaledBitmap( |
76 | - BitmapFactory.decodeFile(imageName), | |
85 | + BitmapFactory.decodeFile(file.getPath()), | |
77 | 86 | mWidth, |
78 | 87 | mHeight, |
79 | 88 | false)); |
80 | 89 | } |
81 | 90 | |
82 | - BitmapDrawable d = new BitmapDrawable(mThumb.get()); | |
91 | + final BitmapDrawable d = new BitmapDrawable(mThumb.get()); | |
92 | + | |
83 | 93 | d.setGravity(Gravity.CENTER); |
84 | - mCacheMap.put(imageName, d); | |
94 | + mCacheMap.put(file.getPath(), d); | |
85 | 95 | |
86 | 96 | handler.post(new Runnable() { |
87 | 97 | @Override |
88 | 98 | public void run() { |
89 | 99 | Message msg = handler.obtainMessage(); |
90 | - msg.obj = (BitmapDrawable)mCacheMap.get((imageName)); | |
100 | + msg.obj = (BitmapDrawable)d; | |
91 | 101 | msg.sendToTarget(); |
92 | 102 | } |
93 | 103 | }); |
94 | 104 | } |
95 | - }; | |
96 | - thread.start(); | |
97 | - | |
105 | + } | |
106 | + } | |
107 | + | |
108 | + public void createNewThumbnail(ArrayList<String> files, String dir, Handler handler) { | |
109 | + this.files = files; | |
110 | + this.dir = dir; | |
111 | + this.handler = handler; | |
112 | + } | |
113 | + | |
114 | + private boolean isImageFile(String file) { | |
115 | + String ext = file.substring(file.lastIndexOf(".") + 1); | |
116 | + | |
117 | + if (ext.equalsIgnoreCase("png") || ext.equalsIgnoreCase("jpg") || | |
118 | + ext.equalsIgnoreCase("jpeg")|| ext.equalsIgnoreCase("gif") || | |
119 | + ext.equalsIgnoreCase("tiff")|| ext.equalsIgnoreCase("tif")) | |
120 | + return true; | |
121 | + | |
122 | + return false; | |
98 | 123 | } |
99 | 124 | } |