1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.adapter
;
21 import java
.lang
.ref
.WeakReference
;
22 import java
.util
.Vector
;
24 import android
.accounts
.Account
;
25 import android
.content
.Context
;
26 import android
.content
.res
.Resources
;
27 import android
.graphics
.Bitmap
;
28 import android
.graphics
.Bitmap
.CompressFormat
;
29 import android
.graphics
.BitmapFactory
;
30 import android
.graphics
.drawable
.BitmapDrawable
;
31 import android
.graphics
.drawable
.Drawable
;
32 import android
.media
.ThumbnailUtils
;
33 import android
.os
.AsyncTask
;
34 import android
.util
.TypedValue
;
35 import android
.view
.LayoutInflater
;
36 import android
.view
.View
;
37 import android
.view
.ViewGroup
;
38 import android
.widget
.BaseAdapter
;
39 import android
.widget
.ImageView
;
40 import android
.widget
.ListAdapter
;
41 import android
.widget
.ListView
;
42 import android
.widget
.TextView
;
44 import com
.owncloud
.android
.R
;
45 import com
.owncloud
.android
.authentication
.AccountUtils
;
46 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
47 import com
.owncloud
.android
.datamodel
.OCFile
;
48 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
49 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
50 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
51 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
52 import com
.owncloud
.android
.utils
.BitmapUtils
;
53 import com
.owncloud
.android
.utils
.DisplayUtils
;
57 * This Adapter populates a ListView with all files and folders in an ownCloud
60 * @author Bartek Przybylski
61 * @author Tobias Kaminsky
62 * @author David A. Velasco
64 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
65 private final static String PERMISSION_SHARED_WITH_ME
= "S";
67 private static final String TAG
= FileListListAdapter
.class.getSimpleName();
69 private Context mContext
;
70 private OCFile mFile
= null
;
71 private Vector
<OCFile
> mFiles
= null
;
72 private boolean mJustFolders
;
74 private FileDataStorageManager mStorageManager
;
75 private Account mAccount
;
76 private ComponentsGetter mTransferServiceGetter
;
78 private final Object thumbnailDiskCacheLock
= new Object();
79 private DiskLruImageCache mThumbnailCache
;
80 private boolean mThumbnailCacheStarting
= true
;
81 private static final int DISK_CACHE_SIZE
= 1024 * 1024 * 10; // 10MB
82 private static final CompressFormat mCompressFormat
= CompressFormat
.JPEG
;
83 private static final int mCompressQuality
= 70;
84 private Bitmap defaultImg
;
86 public FileListListAdapter(
89 ComponentsGetter transferServiceGetter
92 mJustFolders
= justFolders
;
94 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
95 mTransferServiceGetter
= transferServiceGetter
;
96 defaultImg
= BitmapFactory
.decodeResource(mContext
.getResources(),
97 DisplayUtils
.getResourceId("image/png", "default.png"));
99 // Initialise disk cache on background thread
100 new InitDiskCacheTask().execute();
103 class InitDiskCacheTask
extends AsyncTask
<File
, Void
, Void
> {
105 protected Void
doInBackground(File
... params
) {
106 synchronized (thumbnailDiskCacheLock
) {
108 mThumbnailCache
= new DiskLruImageCache(mContext
, "thumbnailCache",
109 DISK_CACHE_SIZE
, mCompressFormat
, mCompressQuality
);
110 } catch (Exception e
) {
111 Log_OC
.d(TAG
, "Thumbnail cache could not be opened ", e
);
112 mThumbnailCache
= null
;
114 mThumbnailCacheStarting
= false
; // Finished initialization
115 thumbnailDiskCacheLock
.notifyAll(); // Wake any waiting threads
121 static class AsyncDrawable
extends BitmapDrawable
{
122 private final WeakReference
<ThumbnailGenerationTask
> bitmapWorkerTaskReference
;
124 public AsyncDrawable(Resources res
, Bitmap bitmap
,
125 ThumbnailGenerationTask bitmapWorkerTask
) {
127 bitmapWorkerTaskReference
=
128 new WeakReference
<ThumbnailGenerationTask
>(bitmapWorkerTask
);
131 public ThumbnailGenerationTask
getBitmapWorkerTask() {
132 return bitmapWorkerTaskReference
.get();
136 class ThumbnailGenerationTask
extends AsyncTask
<OCFile
, Void
, Bitmap
> {
137 private final WeakReference
<ImageView
> imageViewReference
;
141 public ThumbnailGenerationTask(ImageView imageView
) {
142 // Use a WeakReference to ensure the ImageView can be garbage collected
143 imageViewReference
= new WeakReference
<ImageView
>(imageView
);
146 // Decode image in background.
148 protected Bitmap
doInBackground(OCFile
... params
) {
149 Bitmap thumbnail
= null
;
153 final String imageKey
= String
.valueOf(file
.getRemoteId());
155 // Check disk cache in background thread
156 thumbnail
= getBitmapFromDiskCache(imageKey
);
158 // Not found in disk cache
159 if (thumbnail
== null
|| file
.needsUpdateThumbnail()) {
160 // Converts dp to pixel
161 Resources r
= mContext
.getResources();
162 int px
= (int) Math
.round(TypedValue
.applyDimension(
163 TypedValue
.COMPLEX_UNIT_DIP
, 150, r
.getDisplayMetrics()
167 Bitmap bitmap
= BitmapUtils
.decodeSampledBitmapFromFile(
168 file
.getStoragePath(), px
, px
);
170 if (bitmap
!= null
) {
171 thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
);
173 // Add thumbnail to cache
174 addBitmapToCache(imageKey
, thumbnail
);
176 file
.setNeedsUpdateThumbnail(false
);
177 mStorageManager
.saveFile(file
);
183 } catch (Throwable t
) {
184 // the app should never break due to a problem with thumbnails
185 Log_OC
.e(TAG
, "Generation of thumbnail for " + file
+ " failed", t
);
186 if (t
instanceof OutOfMemoryError
) {
194 protected void onPostExecute(Bitmap bitmap
){
199 if (imageViewReference
!= null
&& bitmap
!= null
) {
200 final ImageView imageView
= imageViewReference
.get();
201 final ThumbnailGenerationTask bitmapWorkerTask
=
202 getBitmapWorkerTask(imageView
);
203 if (this == bitmapWorkerTask
&& imageView
!= null
) {
204 if (imageView
.getTag().equals(file
.getFileId())) {
205 imageView
.setImageBitmap(bitmap
);
212 public void addBitmapToCache(String key
, Bitmap bitmap
) {
213 synchronized (thumbnailDiskCacheLock
) {
214 if (mThumbnailCache
!= null
) {
215 mThumbnailCache
.put(key
, bitmap
);
220 public Bitmap
getBitmapFromDiskCache(String key
) {
221 synchronized (thumbnailDiskCacheLock
) {
222 // Wait while disk cache is started from background thread
223 while (mThumbnailCacheStarting
) {
225 thumbnailDiskCacheLock
.wait();
226 } catch (InterruptedException e
) {}
228 if (mThumbnailCache
!= null
) {
229 return (Bitmap
) mThumbnailCache
.getBitmap(key
);
236 public boolean areAllItemsEnabled() {
241 public boolean isEnabled(int position
) {
246 public int getCount() {
247 return mFiles
!= null ? mFiles
.size() : 0;
251 public Object
getItem(int position
) {
252 if (mFiles
== null
|| mFiles
.size() <= position
)
254 return mFiles
.get(position
);
258 public long getItemId(int position
) {
259 if (mFiles
== null
|| mFiles
.size() <= position
)
261 return mFiles
.get(position
).getFileId();
265 public int getItemViewType(int position
) {
270 public View
getView(int position
, View convertView
, ViewGroup parent
) {
271 View view
= convertView
;
273 LayoutInflater inflator
= (LayoutInflater
) mContext
274 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
275 view
= inflator
.inflate(R
.layout
.list_item
, null
);
278 if (mFiles
!= null
&& mFiles
.size() > position
) {
279 OCFile file
= mFiles
.get(position
);
280 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
281 String name
= file
.getFileName();
283 fileName
.setText(name
);
284 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
285 fileIcon
.setTag(file
.getFileId());
286 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
287 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
288 sharedWithMeIconV
.setVisibility(View
.GONE
);
290 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
291 localStateView
.bringToFront();
292 FileDownloaderBinder downloaderBinder
=
293 mTransferServiceGetter
.getFileDownloaderBinder();
294 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
295 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
296 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
297 localStateView
.setVisibility(View
.VISIBLE
);
298 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
299 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
300 localStateView
.setVisibility(View
.VISIBLE
);
301 } else if (file
.isDown()) {
302 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
303 localStateView
.setVisibility(View
.VISIBLE
);
305 localStateView
.setVisibility(View
.INVISIBLE
);
308 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
309 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
310 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
312 if (!file
.isFolder()) {
313 fileSizeV
.setVisibility(View
.VISIBLE
);
314 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
315 lastModV
.setVisibility(View
.VISIBLE
);
317 DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp())
319 // this if-else is needed even thoe fav icon is visible by default
320 // because android reuses views in listview
321 if (!file
.keepInSync()) {
322 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
324 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
327 ListView parentList
= (ListView
)parent
;
328 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
329 checkBoxV
.setVisibility(View
.GONE
);
331 if (parentList
.isItemChecked(position
)) {
332 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
334 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
336 checkBoxV
.setVisibility(View
.VISIBLE
);
339 // get Thumbnail if file is image
341 // Thumbnail in Cache?
342 Bitmap thumbnail
= getBitmapFromDiskCache(String
.valueOf(file
.getRemoteId()));
343 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
344 fileIcon
.setImageBitmap(thumbnail
);
346 // generate new Thumbnail
347 if (cancelPotentialWork(file
, fileIcon
)) {
348 final ThumbnailGenerationTask task
=
349 new ThumbnailGenerationTask(fileIcon
);
350 final AsyncDrawable asyncDrawable
=
351 new AsyncDrawable(mContext
.getResources(), defaultImg
, task
);
352 fileIcon
.setImageDrawable(asyncDrawable
);
357 fileIcon
.setImageResource(
358 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
362 if (checkIfFileIsSharedWithMe(file
)) {
363 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
367 fileSizeV
.setVisibility(View
.INVISIBLE
);
368 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
369 lastModV
.setVisibility(View
.VISIBLE
);
371 DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp())
373 checkBoxV
.setVisibility(View
.GONE
);
374 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
376 if (checkIfFileIsSharedWithMe(file
)) {
377 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
378 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
380 fileIcon
.setImageResource(
381 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
385 // If folder is sharedByLink, icon folder must be changed to
387 if (file
.isShareByLink()) {
388 fileIcon
.setImageResource(R
.drawable
.folder_public
);
392 if (file
.isShareByLink()) {
393 sharedIconV
.setVisibility(View
.VISIBLE
);
395 sharedIconV
.setVisibility(View
.GONE
);
402 public static boolean cancelPotentialWork(OCFile file
, ImageView imageView
) {
403 final ThumbnailGenerationTask bitmapWorkerTask
= getBitmapWorkerTask(imageView
);
405 if (bitmapWorkerTask
!= null
) {
406 final OCFile bitmapData
= bitmapWorkerTask
.file
;
407 // If bitmapData is not yet set or it differs from the new data
408 if (bitmapData
== null
|| bitmapData
!= file
) {
409 // Cancel previous task
410 bitmapWorkerTask
.cancel(true
);
412 // The same work is already in progress
416 // No task associated with the ImageView, or an existing task was cancelled
420 private static ThumbnailGenerationTask
getBitmapWorkerTask(ImageView imageView
) {
421 if (imageView
!= null
) {
422 final Drawable drawable
= imageView
.getDrawable();
423 if (drawable
instanceof AsyncDrawable
) {
424 final AsyncDrawable asyncDrawable
= (AsyncDrawable
) drawable
;
425 return asyncDrawable
.getBitmapWorkerTask();
432 public int getViewTypeCount() {
437 public boolean hasStableIds() {
442 public boolean isEmpty() {
443 return (mFiles
== null
|| mFiles
.isEmpty());
447 * Change the adapted directory for a new one
448 * @param directory New file to adapt. Can be NULL, meaning
449 * "no content to adapt".
450 * @param updatedStorageManager Optional updated storage manager; used to replace
451 * mStorageManager if is different (and not NULL)
453 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
455 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
456 mStorageManager
= updatedStorageManager
;
457 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
459 if (mStorageManager
!= null
) {
460 mFiles
= mStorageManager
.getFolderContent(mFile
);
462 mFiles
= getFolders(mFiles
);
467 notifyDataSetChanged();
472 * Filter for getting only the folders
474 * @return Vector<OCFile>
476 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
477 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
478 OCFile current
= null
;
479 for (int i
=0; i
<files
.size(); i
++) {
480 current
= files
.get(i
);
481 if (current
.isFolder()) {
490 * Check if parent folder does not include 'S' permission and if file/folder
493 * @param file: OCFile
494 * @return boolean: True if it is shared with me and false if it is not
496 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
497 return (mFile
.getPermissions() != null
498 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
499 && file
.getPermissions() != null
500 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));