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
;
20 import java
.io
.ByteArrayInputStream
;
22 import java
.io
.IOException
;
23 import java
.io
.InputStream
;
24 import java
.lang
.ref
.WeakReference
;
25 import java
.net
.URLEncoder
;
26 //import java.net.URLEncoder;
27 import java
.util
.Vector
;
29 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
30 import org
.apache
.http
.HttpStatus
;
32 import android
.accounts
.Account
;
33 import android
.accounts
.AuthenticatorException
;
34 import android
.accounts
.OperationCanceledException
;
35 import android
.content
.Context
;
36 import android
.content
.res
.Resources
;
37 import android
.graphics
.Bitmap
;
38 import android
.graphics
.Bitmap
.CompressFormat
;
39 import android
.graphics
.BitmapFactory
;
40 import android
.graphics
.drawable
.BitmapDrawable
;
41 import android
.graphics
.drawable
.Drawable
;
42 import android
.media
.ThumbnailUtils
;
43 import android
.os
.AsyncTask
;
44 import android
.util
.TypedValue
;
45 import android
.view
.LayoutInflater
;
46 import android
.view
.View
;
47 import android
.view
.ViewGroup
;
48 import android
.widget
.BaseAdapter
;
49 import android
.widget
.ImageView
;
50 import android
.widget
.ListAdapter
;
51 import android
.widget
.ListView
;
52 import android
.widget
.TextView
;
54 import com
.owncloud
.android
.R
;
55 import com
.owncloud
.android
.authentication
.AccountUtils
;
56 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
57 import com
.owncloud
.android
.datamodel
.OCFile
;
58 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
59 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
60 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
61 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
62 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
63 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
;
64 import com
.owncloud
.android
.lib
.resources
.shares
.ShareUtils
;
65 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
66 import com
.owncloud
.android
.utils
.DisplayUtils
;
67 import com
.owncloud
.android
.utils
.Log_OC
;
70 import org.apache.http.HttpEntity;
71 import org.apache.http.HttpResponse;
72 import org.apache.http.auth.AuthScope;
73 import org.apache.http.auth.UsernamePasswordCredentials;
74 import org.apache.http.client.methods.HttpGet;
75 import org.apache.http.impl.client.DefaultHttpClient;
76 import org.apache.http.util.EntityUtils;
81 * This Adapter populates a ListView with all files and folders in an ownCloud
84 * @author Bartek Przybylski
85 * @Author Tobias Kaminsky
88 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
89 private final static String PERMISSION_SHARED_WITH_ME
= "S";
91 private Context mContext
;
92 private OCFile mFile
= null
;
93 private Vector
<OCFile
> mFiles
= null
;
94 private boolean mJustFolders
;
96 private FileDataStorageManager mStorageManager
;
97 private Account mAccount
;
98 private ComponentsGetter mTransferServiceGetter
;
100 private final Object thumbnailDiskCacheLock
= new Object();
101 private DiskLruImageCache mThumbnailCache
;
102 private boolean mThumbnailCacheStarting
= true
;
103 private static final int DISK_CACHE_SIZE
= 1024 * 1024 * 10; // 10MB
104 private static final CompressFormat mCompressFormat
= CompressFormat
.JPEG
;
105 private static final int mCompressQuality
= 70;
106 private Bitmap defaultImg
;
107 private OwnCloudClient client
;
109 public FileListListAdapter(
112 ComponentsGetter transferServiceGetter
115 mJustFolders
= justFolders
;
117 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
118 mTransferServiceGetter
= transferServiceGetter
;
119 defaultImg
= BitmapFactory
.decodeResource(mContext
.getResources(),
120 DisplayUtils
.getResourceId("image/png", "default.png"));
122 // Initialise disk cache on background thread
123 new InitDiskCacheTask().execute();
129 class InitDiskCacheTask
extends AsyncTask
<File
, Void
, Void
> {
131 protected Void
doInBackground(File
... params
) {
132 synchronized (thumbnailDiskCacheLock
) {
133 OwnCloudAccount ocAccount
;
135 ocAccount
= new OwnCloudAccount(mAccount
, mContext
);
136 client
= OwnCloudClientManagerFactory
.getDefaultSingleton().
137 getClientFor(ocAccount
, mContext
);
138 } catch (AccountNotFoundException e
) {
139 // TODO Auto-generated catch block
141 } catch (AuthenticatorException e
) {
142 // TODO Auto-generated catch block
144 } catch (OperationCanceledException e
) {
145 // TODO Auto-generated catch block
147 } catch (IOException e
) {
148 // TODO Auto-generated catch block
153 mThumbnailCache
= new DiskLruImageCache(mContext
, "thumbnailCache",
154 DISK_CACHE_SIZE
, mCompressFormat
, mCompressQuality
);
156 mThumbnailCacheStarting
= false
; // Finished initialization
157 thumbnailDiskCacheLock
.notifyAll(); // Wake any waiting threads
163 static class AsyncDrawable
extends BitmapDrawable
{
164 private final WeakReference
<ThumbnailGenerationTask
> bitmapWorkerTaskReference
;
166 public AsyncDrawable(Resources res
, Bitmap bitmap
,
167 ThumbnailGenerationTask bitmapWorkerTask
) {
169 bitmapWorkerTaskReference
=
170 new WeakReference
<ThumbnailGenerationTask
>(bitmapWorkerTask
);
173 public ThumbnailGenerationTask
getBitmapWorkerTask() {
174 return bitmapWorkerTaskReference
.get();
178 class ThumbnailGenerationTask
extends AsyncTask
<OCFile
, Void
, Bitmap
> {
179 private final WeakReference
<ImageView
> imageViewReference
;
183 public ThumbnailGenerationTask(ImageView imageView
) {
184 // Use a WeakReference to ensure the ImageView can be garbage collected
185 imageViewReference
= new WeakReference
<ImageView
>(imageView
);
188 // Decode image in background.
190 protected Bitmap
doInBackground(OCFile
... params
) {
192 final String imageKey
= String
.valueOf(file
.getRemoteId().hashCode());
193 Log_OC
.d("Thumbnail", imageKey
);
195 // Check disk cache in background thread
196 Bitmap thumbnail
= getBitmapFromDiskCache(imageKey
);
198 // Not found in disk cache
199 if (thumbnail
== null
) {
200 // Converts dp to pixel
201 Resources r
= mContext
.getResources();
202 int px
= (int) Math
.round(TypedValue
.applyDimension(
203 TypedValue
.COMPLEX_UNIT_DIP
, 150, r
.getDisplayMetrics()
207 Bitmap bitmap
= BitmapFactory
.decodeFile(file
.getStoragePath());
209 if (bitmap
!= null
) {
210 thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
);
212 // Add thumbnail to cache
213 addBitmapToCache(imageKey
, thumbnail
);
217 // Download thumbnail from server
218 // Commented out as maybe changes to client library are needed
222 String uri
= client
.getBaseUri() + "/index.php/apps/files/api/v1/thumbnail/100/100/"+URLEncoder
.encode(file
.getRemotePath(), "UTF-8").replaceAll("%2F", "/");
223 Log_OC
.d("Thumbnail", "URI: " + uri
);
224 GetMethod get
= new GetMethod(uri
);
225 status
= client
.executeMethod(get
);
226 if(isSuccess(status
)) {
227 byte[] bytes
= get
.getResponseBody();
228 Bitmap bitmap
= BitmapFactory
.decodeByteArray(bytes
, 0, bytes
.length
);
229 thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
);
231 // Add thumbnail to cache
232 if (thumbnail
!= null
){
233 addBitmapToCache(imageKey
, thumbnail
);
236 } catch(Exception e
){
244 private boolean isSuccess(int status
) {
245 return (status
== HttpStatus
.SC_OK
);
249 protected void onPostExecute(Bitmap bitmap
){
254 if (imageViewReference
!= null
&& bitmap
!= null
) {
255 final ImageView imageView
= imageViewReference
.get();
256 final ThumbnailGenerationTask bitmapWorkerTask
=
257 getBitmapWorkerTask(imageView
);
258 if (this == bitmapWorkerTask
&& imageView
!= null
) {
259 imageView
.setImageBitmap(bitmap
);
265 public void addBitmapToCache(String key
, Bitmap bitmap
) {
266 synchronized (thumbnailDiskCacheLock
) {
267 if (mThumbnailCache
!= null
&& mThumbnailCache
.getBitmap(key
) == null
) {
268 mThumbnailCache
.put(key
, bitmap
);
273 public Bitmap
getBitmapFromDiskCache(String key
) {
274 synchronized (thumbnailDiskCacheLock
) {
275 // Wait while disk cache is started from background thread
276 while (mThumbnailCacheStarting
) {
278 thumbnailDiskCacheLock
.wait();
279 } catch (InterruptedException e
) {}
281 if (mThumbnailCache
!= null
) {
282 return (Bitmap
) mThumbnailCache
.getBitmap(key
);
289 public boolean areAllItemsEnabled() {
294 public boolean isEnabled(int position
) {
299 public int getCount() {
300 return mFiles
!= null ? mFiles
.size() : 0;
304 public Object
getItem(int position
) {
305 if (mFiles
== null
|| mFiles
.size() <= position
)
307 return mFiles
.get(position
);
311 public long getItemId(int position
) {
312 if (mFiles
== null
|| mFiles
.size() <= position
)
314 return mFiles
.get(position
).getFileId();
318 public int getItemViewType(int position
) {
323 public View
getView(int position
, View convertView
, ViewGroup parent
) {
324 View view
= convertView
;
326 LayoutInflater inflator
= (LayoutInflater
) mContext
327 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
328 view
= inflator
.inflate(R
.layout
.list_item
, null
);
331 if (mFiles
!= null
&& mFiles
.size() > position
) {
332 OCFile file
= mFiles
.get(position
);
333 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
334 String name
= file
.getFileName();
336 fileName
.setText(name
);
337 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
338 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
339 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
340 sharedWithMeIconV
.setVisibility(View
.GONE
);
342 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
343 localStateView
.bringToFront();
344 FileDownloaderBinder downloaderBinder
=
345 mTransferServiceGetter
.getFileDownloaderBinder();
346 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
347 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
348 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
349 localStateView
.setVisibility(View
.VISIBLE
);
350 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
351 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
352 localStateView
.setVisibility(View
.VISIBLE
);
353 } else if (file
.isDown()) {
354 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
355 localStateView
.setVisibility(View
.VISIBLE
);
357 localStateView
.setVisibility(View
.INVISIBLE
);
360 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
361 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
362 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
364 if (!file
.isFolder()) {
365 fileSizeV
.setVisibility(View
.VISIBLE
);
366 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
367 lastModV
.setVisibility(View
.VISIBLE
);
369 DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp())
371 // this if-else is needed even thoe fav icon is visible by default
372 // because android reuses views in listview
373 if (!file
.keepInSync()) {
374 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
376 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
379 ListView parentList
= (ListView
)parent
;
380 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
381 checkBoxV
.setVisibility(View
.GONE
);
383 if (parentList
.isItemChecked(position
)) {
384 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
386 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
388 checkBoxV
.setVisibility(View
.VISIBLE
);
391 // get Thumbnail if file is image
393 // Thumbnail in Cache?
394 Bitmap thumbnail
= getBitmapFromDiskCache(String
.valueOf(file
.getRemoteId().hashCode()));
395 if (thumbnail
!= null
){
396 fileIcon
.setImageBitmap(thumbnail
);
398 // generate new Thumbnail
399 if (cancelPotentialWork(file
, fileIcon
)) {
400 final ThumbnailGenerationTask task
=
401 new ThumbnailGenerationTask(fileIcon
);
402 final AsyncDrawable asyncDrawable
=
403 new AsyncDrawable(mContext
.getResources(), defaultImg
, task
);
404 fileIcon
.setImageDrawable(asyncDrawable
);
409 fileIcon
.setImageResource(
410 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
414 if (checkIfFileIsSharedWithMe(file
)) {
415 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
419 fileSizeV
.setVisibility(View
.INVISIBLE
);
420 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
421 lastModV
.setVisibility(View
.VISIBLE
);
423 DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp())
425 checkBoxV
.setVisibility(View
.GONE
);
426 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
428 if (checkIfFileIsSharedWithMe(file
)) {
429 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
430 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
432 fileIcon
.setImageResource(
433 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
437 // If folder is sharedByLink, icon folder must be changed to
439 if (file
.isShareByLink()) {
440 fileIcon
.setImageResource(R
.drawable
.folder_public
);
444 if (file
.isShareByLink()) {
445 sharedIconV
.setVisibility(View
.VISIBLE
);
447 sharedIconV
.setVisibility(View
.GONE
);
454 public static boolean cancelPotentialWork(OCFile file
, ImageView imageView
) {
455 final ThumbnailGenerationTask bitmapWorkerTask
= getBitmapWorkerTask(imageView
);
457 if (bitmapWorkerTask
!= null
) {
458 final OCFile bitmapData
= bitmapWorkerTask
.file
;
459 // If bitmapData is not yet set or it differs from the new data
460 if (bitmapData
== null
|| bitmapData
!= file
) {
461 // Cancel previous task
462 bitmapWorkerTask
.cancel(true
);
464 // The same work is already in progress
468 // No task associated with the ImageView, or an existing task was cancelled
472 private static ThumbnailGenerationTask
getBitmapWorkerTask(ImageView imageView
) {
473 if (imageView
!= null
) {
474 final Drawable drawable
= imageView
.getDrawable();
475 if (drawable
instanceof AsyncDrawable
) {
476 final AsyncDrawable asyncDrawable
= (AsyncDrawable
) drawable
;
477 return asyncDrawable
.getBitmapWorkerTask();
484 public int getViewTypeCount() {
489 public boolean hasStableIds() {
494 public boolean isEmpty() {
495 return (mFiles
== null
|| mFiles
.isEmpty());
499 * Change the adapted directory for a new one
500 * @param directory New file to adapt. Can be NULL, meaning
501 * "no content to adapt".
502 * @param updatedStorageManager Optional updated storage manager; used to replace
503 * mStorageManager if is different (and not NULL)
505 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
507 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
508 mStorageManager
= updatedStorageManager
;
509 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
511 if (mStorageManager
!= null
) {
512 mFiles
= mStorageManager
.getFolderContent(mFile
);
514 mFiles
= getFolders(mFiles
);
519 notifyDataSetChanged();
524 * Filter for getting only the folders
526 * @return Vector<OCFile>
528 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
529 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
530 OCFile current
= null
;
531 for (int i
=0; i
<files
.size(); i
++) {
532 current
= files
.get(i
);
533 if (current
.isFolder()) {
542 * Check if parent folder does not include 'S' permission and if file/folder
545 * @param file: OCFile
546 * @return boolean: True if it is shared with me and false if it is not
548 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
549 return (mFile
.getPermissions() != null
550 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
551 && file
.getPermissions() != null
552 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));