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
.io
.IOException
;
22 import java
.net
.URLEncoder
;
23 import java
.util
.Vector
;
25 import android
.accounts
.Account
;
26 import android
.accounts
.AuthenticatorException
;
27 import android
.accounts
.OperationCanceledException
;
28 import android
.content
.Context
;
29 import android
.content
.res
.Resources
;
30 import android
.graphics
.Bitmap
;
31 import android
.graphics
.Bitmap
.CompressFormat
;
32 import android
.graphics
.BitmapFactory
;
33 import android
.media
.ThumbnailUtils
;
34 import android
.os
.AsyncTask
;
35 import android
.util
.TypedValue
;
36 import android
.view
.LayoutInflater
;
37 import android
.view
.View
;
38 import android
.view
.ViewGroup
;
39 import android
.widget
.BaseAdapter
;
40 import android
.widget
.ImageView
;
41 import android
.widget
.ListAdapter
;
42 import android
.widget
.ListView
;
43 import android
.widget
.TextView
;
45 import com
.owncloud
.android
.R
;
46 import com
.owncloud
.android
.authentication
.AccountUtils
;
47 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
48 import com
.owncloud
.android
.datamodel
.OCFile
;
49 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
50 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
51 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
52 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
53 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
54 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
;
55 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
56 import com
.owncloud
.android
.utils
.DisplayUtils
;
58 import org
.apache
.http
.HttpEntity
;
59 import org
.apache
.http
.HttpResponse
;
60 import org
.apache
.http
.auth
.AuthScope
;
61 import org
.apache
.http
.auth
.UsernamePasswordCredentials
;
62 import org
.apache
.http
.client
.methods
.HttpGet
;
63 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
64 import org
.apache
.http
.util
.EntityUtils
;
68 * This Adapter populates a ListView with all files and folders in an ownCloud
71 * @author Bartek Przybylski
74 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
75 private final static String PERMISSION_SHARED_WITH_ME
= "S";
77 private Context mContext
;
78 private OCFile mFile
= null
;
79 private Vector
<OCFile
> mFiles
= null
;
81 private FileDataStorageManager mStorageManager
;
82 private Account mAccount
;
83 private ComponentsGetter mTransferServiceGetter
;
84 private final Object mDiskCacheLock
= new Object();
85 private DiskLruImageCache mDiskLruCache
;
86 private boolean mDiskCacheStarting
= true
;
87 private static final int DISK_CACHE_SIZE
= 1024 * 1024 * 10; // 10MB
88 private CompressFormat mCompressFormat
= CompressFormat
.JPEG
;
89 private int mCompressQuality
= 70;
90 private OwnCloudClient mClient
;
92 public FileListListAdapter(Context context
, ComponentsGetter transferServiceGetter
) {
94 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
95 mTransferServiceGetter
= transferServiceGetter
;
97 // Initialise disk cache on background thread
98 new InitDiskCacheTask().execute();
101 class InitDiskCacheTask
extends AsyncTask
<File
, Void
, Void
> {
103 protected Void
doInBackground(File
... params
) {
104 synchronized (mDiskCacheLock
) {
105 mDiskLruCache
= new DiskLruImageCache(mContext
, "thumbnailCache", DISK_CACHE_SIZE
, mCompressFormat
,mCompressQuality
);
108 OwnCloudAccount ocAccount
= new OwnCloudAccount(mAccount
, mContext
);
109 mClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
110 getClientFor(ocAccount
, mContext
);
111 } catch (AccountNotFoundException e
) {
112 // TODO Auto-generated catch block
114 } catch (AuthenticatorException e
) {
115 // TODO Auto-generated catch block
117 } catch (OperationCanceledException e
) {
118 // TODO Auto-generated catch block
120 } catch (IOException e
) {
121 // TODO Auto-generated catch block
125 mDiskCacheStarting
= false
; // Finished initialization
126 mDiskCacheLock
.notifyAll(); // Wake any waiting threads
132 class BitmapWorkerTask
extends AsyncTask
<OCFile
, Void
, Bitmap
> {
133 private final ImageView fileIcon
;
135 public BitmapWorkerTask(ImageView fileIcon
) {
136 this.fileIcon
= fileIcon
;
139 // Decode image in background.
141 protected Bitmap
doInBackground(OCFile
... params
) {
142 OCFile file
= params
[0];
143 final String imageKey
= String
.valueOf(file
.getRemoteId());
145 // Check disk cache in background thread
146 Bitmap thumbnail
= getBitmapFromDiskCache(imageKey
);
148 // Not found in disk cache
149 if (thumbnail
== null
) {
150 // Converts dp to pixel
151 Resources r
= mContext
.getResources();
152 int px
= (int) Math
.round(TypedValue
.applyDimension(TypedValue
.COMPLEX_UNIT_DIP
, 150, r
.getDisplayMetrics()));
155 Bitmap bitmap
= BitmapFactory
.decodeFile(file
.getStoragePath());
156 thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
);
158 // Add thumbnail to cache
159 addBitmapToCache(imageKey
, thumbnail
);
162 // Download thumbnail from server
163 DefaultHttpClient httpclient
= new DefaultHttpClient();
165 httpclient
.getCredentialsProvider().setCredentials(
166 new AuthScope(mClient
.getBaseUri().toString().replace("https://", ""), 443),
167 new UsernamePasswordCredentials(mClient
.getCredentials().getUsername(), mClient
.getCredentials().getAuthToken()));
170 // TODO change to user preview.png
171 HttpGet httpget
= new HttpGet(mClient
.getBaseUri() + "/ocs/v1.php/thumbnail?path=" + URLEncoder
.encode(file
.getRemotePath(), "UTF-8"));
172 HttpResponse response
= httpclient
.execute(httpget
);
173 HttpEntity entity
= response
.getEntity();
175 if (entity
!= null
) {
176 byte[] bytes
= EntityUtils
.toByteArray(entity
);
177 Bitmap bitmap
= BitmapFactory
.decodeByteArray(bytes
, 0, bytes
.length
);
178 thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
);
180 // Add thumbnail to cache
181 if (thumbnail
!= null
){
182 addBitmapToCache(imageKey
, thumbnail
);
186 } catch(Exception e
){
189 httpclient
.getConnectionManager().shutdown();
196 protected void onPostExecute(Bitmap bitmap
){
197 fileIcon
.setImageBitmap(bitmap
);
201 public void addBitmapToCache(String key
, Bitmap bitmap
) {
202 synchronized (mDiskCacheLock
) {
203 if (mDiskLruCache
!= null
&& mDiskLruCache
.getBitmap(key
) == null
) {
204 mDiskLruCache
.put(key
, bitmap
);
209 public Bitmap
getBitmapFromDiskCache(String key
) {
210 synchronized (mDiskCacheLock
) {
211 // Wait while disk cache is started from background thread
212 while (mDiskCacheStarting
) {
214 mDiskCacheLock
.wait();
215 } catch (InterruptedException e
) {}
217 if (mDiskLruCache
!= null
) {
218 return (Bitmap
) mDiskLruCache
.getBitmap(key
);
225 public boolean areAllItemsEnabled() {
230 public boolean isEnabled(int position
) {
235 public int getCount() {
236 return mFiles
!= null ? mFiles
.size() : 0;
240 public Object
getItem(int position
) {
241 if (mFiles
== null
|| mFiles
.size() <= position
)
243 return mFiles
.get(position
);
247 public long getItemId(int position
) {
248 if (mFiles
== null
|| mFiles
.size() <= position
)
250 return mFiles
.get(position
).getFileId();
254 public int getItemViewType(int position
) {
259 public View
getView(int position
, View convertView
, ViewGroup parent
) {
260 View view
= convertView
;
262 LayoutInflater inflator
= (LayoutInflater
) mContext
263 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
264 view
= inflator
.inflate(R
.layout
.list_item
, null
);
267 if (mFiles
!= null
&& mFiles
.size() > position
) {
268 OCFile file
= mFiles
.get(position
);
269 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
270 String name
= file
.getFileName();
272 fileName
.setText(name
);
273 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
274 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
275 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
276 sharedWithMeIconV
.setVisibility(View
.GONE
);
278 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
279 localStateView
.bringToFront();
280 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
281 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
282 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
283 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
284 localStateView
.setVisibility(View
.VISIBLE
);
285 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
286 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
287 localStateView
.setVisibility(View
.VISIBLE
);
288 } else if (file
.isDown()) {
289 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
290 localStateView
.setVisibility(View
.VISIBLE
);
292 localStateView
.setVisibility(View
.INVISIBLE
);
295 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
296 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
297 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
299 if (!file
.isFolder()) {
300 fileSizeV
.setVisibility(View
.VISIBLE
);
301 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
302 lastModV
.setVisibility(View
.VISIBLE
);
303 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
304 // this if-else is needed even thoe fav icon is visible by default
305 // because android reuses views in listview
306 if (!file
.keepInSync()) {
307 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
309 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
312 ListView parentList
= (ListView
)parent
;
313 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
314 checkBoxV
.setVisibility(View
.GONE
);
316 if (parentList
.isItemChecked(position
)) {
317 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
319 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
321 checkBoxV
.setVisibility(View
.VISIBLE
);
324 // first set thumbnail according to Mimetype, prevents empty thumbnails
325 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
327 // get Thumbnail if file is image
329 // Thumbnail in Cache?
330 Bitmap thumbnail
= getBitmapFromDiskCache(String
.valueOf(file
.getRemoteId()));
331 if (thumbnail
!= null
){
332 fileIcon
.setImageBitmap(thumbnail
);
334 // generate new Thumbnail
335 new BitmapWorkerTask(fileIcon
).execute(file
);
339 if (checkIfFileIsSharedWithMe(file
)) {
340 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
344 fileSizeV
.setVisibility(View
.INVISIBLE
);
345 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
346 lastModV
.setVisibility(View
.VISIBLE
);
347 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
348 checkBoxV
.setVisibility(View
.GONE
);
349 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
351 if (checkIfFileIsSharedWithMe(file
)) {
352 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
353 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
355 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
358 // If folder is sharedByLink, icon folder must be changed to
360 if (file
.isShareByLink()) {
361 fileIcon
.setImageResource(R
.drawable
.folder_public
);
365 if (file
.isShareByLink()) {
366 sharedIconV
.setVisibility(View
.VISIBLE
);
368 sharedIconV
.setVisibility(View
.GONE
);
376 public int getViewTypeCount() {
381 public boolean hasStableIds() {
386 public boolean isEmpty() {
387 return (mFiles
== null
|| mFiles
.isEmpty());
391 * Change the adapted directory for a new one
392 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
393 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
395 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
397 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
398 mStorageManager
= updatedStorageManager
;
399 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
401 if (mStorageManager
!= null
) {
402 mFiles
= mStorageManager
.getFolderContent(mFile
);
406 notifyDataSetChanged();
410 * Check if parent folder does not include 'S' permission and if file/folder
413 * @param file: OCFile
414 * @return boolean: True if it is shared with me and false if it is not
416 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
417 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
418 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));