ImageSimpleAdapter.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2016 Tobias Kaminsky
  6. * Copyright (C) 2016 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.ui.activity;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import android.accounts.Account;
  26. import android.content.Context;
  27. import android.graphics.Bitmap;
  28. import android.os.storage.StorageManager;
  29. import android.view.LayoutInflater;
  30. import android.view.View;
  31. import android.view.ViewGroup;
  32. import android.widget.ImageView;
  33. import android.widget.SimpleAdapter;
  34. import android.widget.TextView;
  35. import com.owncloud.android.R;
  36. import com.owncloud.android.authentication.AccountUtils;
  37. import com.owncloud.android.datamodel.FileDataStorageManager;
  38. import com.owncloud.android.datamodel.OCFile;
  39. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  40. import com.owncloud.android.datamodel.ThumbnailsCacheManager.AsyncDrawable;
  41. import com.owncloud.android.lib.common.OwnCloudAccount;
  42. import com.owncloud.android.utils.DisplayUtils;
  43. import com.owncloud.android.utils.MimetypeIconUtil;
  44. public class ImageSimpleAdapter extends SimpleAdapter {
  45. private Context mContext;
  46. private Account mAccount;
  47. private FileDataStorageManager mStorageManager;
  48. private int mResource;
  49. private LayoutInflater inflater;
  50. public ImageSimpleAdapter(Context context,
  51. List<? extends Map<String, ?>> data, int resource, String[] from,
  52. int[] to, FileDataStorageManager storageManager, Account account) {
  53. super(context, data, resource, from, to);
  54. mAccount = account;
  55. mStorageManager = storageManager;
  56. mContext = context;
  57. mResource = resource;
  58. inflater = (LayoutInflater) mContext
  59. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  60. }
  61. @Override
  62. public View getView(int position, View convertView, ViewGroup parent) {
  63. View vi = convertView;
  64. if (convertView == null)
  65. vi = inflater.inflate(mResource, null);
  66. HashMap<String, OCFile> data = (HashMap<String, OCFile>) getItem(position);
  67. OCFile file = data.get("dirname");
  68. TextView filename = (TextView) vi.findViewById(R.id.filename);
  69. filename.setText((CharSequence) file.getFileName());
  70. ImageView fileIcon = (ImageView) vi.findViewById(R.id.thumbnail);
  71. fileIcon.setTag(file.getFileId());
  72. // get Thumbnail if file is image
  73. if (file.isImage() && file.getRemoteId() != null){
  74. // Thumbnail in Cache?
  75. Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
  76. String.valueOf(file.getRemoteId())
  77. );
  78. if (thumbnail != null && !file.needsUpdateThumbnail()){
  79. fileIcon.setImageBitmap(thumbnail);
  80. } else {
  81. // generate new Thumbnail
  82. if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
  83. final ThumbnailsCacheManager.ThumbnailGenerationTask task =
  84. new ThumbnailsCacheManager.ThumbnailGenerationTask(fileIcon, mStorageManager,
  85. mAccount);
  86. if (thumbnail == null) {
  87. thumbnail = ThumbnailsCacheManager.mDefaultImg;
  88. }
  89. final AsyncDrawable asyncDrawable = new AsyncDrawable(
  90. mContext.getResources(),
  91. thumbnail,
  92. task
  93. );
  94. fileIcon.setImageDrawable(asyncDrawable);
  95. task.execute(file);
  96. }
  97. }
  98. } else {
  99. fileIcon.setImageResource(
  100. MimetypeIconUtil.getFileTypeIconId(file.getMimetype(), file.getFileName())
  101. );
  102. }
  103. return vi;
  104. }
  105. }