From b206b6d351e38c96eb44d801a45c872844c1a0fd Mon Sep 17 00:00:00 2001 From: mar-v-in Date: Fri, 1 Aug 2014 12:41:30 +0200 Subject: Cache input file thumbnail. --- .../keychain/ui/EncryptFileFragment.java | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java index 1125dce77..7538d2a56 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java @@ -38,9 +38,13 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.FileHelper; import org.sufficientlysecure.keychain.helper.OtherHelper; import org.sufficientlysecure.keychain.provider.TemporaryStorageProvider; +import org.sufficientlysecure.keychain.util.Log; import java.io.File; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; public class EncryptFileFragment extends Fragment implements EncryptActivityInterface.UpdateListener { public static final String ARG_URIS = "uris"; @@ -55,6 +59,7 @@ public class EncryptFileFragment extends Fragment implements EncryptActivityInte private View mShareFile; private View mEncryptFile; private SelectedFilesAdapter mAdapter = new SelectedFilesAdapter(); + private final Map thumbnailCache = new HashMap(); @Override public void onAttach(Activity activity) { @@ -237,6 +242,14 @@ public class EncryptFileFragment extends Fragment implements EncryptActivityInte @Override public void onNotifyUpdate() { + // Clear cache if needed + for (Uri uri : new HashSet(thumbnailCache.keySet())) { + if (!mEncryptInterface.getInputUris().contains(uri)) { + Log.d(Constants.TAG, "Removed thumbnail for uri: "+uri); + thumbnailCache.remove(uri); + } + } + mAdapter.notifyDataSetChanged(); } @@ -258,14 +271,15 @@ public class EncryptFileFragment extends Fragment implements EncryptActivityInte @Override public View getView(final int position, View convertView, ViewGroup parent) { + Uri inputUri = mEncryptInterface.getInputUris().get(position); View view; if (convertView == null) { view = getActivity().getLayoutInflater().inflate(R.layout.file_list_entry, null); } else { view = convertView; } - ((TextView) view.findViewById(R.id.filename)).setText(FileHelper.getFilename(getActivity(), mEncryptInterface.getInputUris().get(position))); - long size = FileHelper.getFileSize(getActivity(), mEncryptInterface.getInputUris().get(position)); + ((TextView) view.findViewById(R.id.filename)).setText(FileHelper.getFilename(getActivity(), inputUri)); + long size = FileHelper.getFileSize(getActivity(), inputUri); if (size == -1) { ((TextView) view.findViewById(R.id.filesize)).setText(""); } else { @@ -278,7 +292,10 @@ public class EncryptFileFragment extends Fragment implements EncryptActivityInte } }); int px = OtherHelper.dpToPx(getActivity(), 48); - Bitmap bitmap = FileHelper.getThumbnail(getActivity(), mEncryptInterface.getInputUris().get(position), new Point(px, px)); + if (!thumbnailCache.containsKey(inputUri)) { + thumbnailCache.put(inputUri, FileHelper.getThumbnail(getActivity(), inputUri, new Point(px, px))); + } + Bitmap bitmap = thumbnailCache.get(inputUri); if (bitmap != null) { ((ImageView) view.findViewById(R.id.thumbnail)).setImageBitmap(bitmap); } else { -- cgit v1.2.3