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() + "/index.php/core/preview.png?file="+URLEncoder.encode(file.getRemotePath(), "UTF-8")+"&x=36&y=36&forceIcon=1");
172 HttpGet httpget
= new HttpGet(mClient
.getBaseUri() + "/ocs/v1.php/thumbnail?x=50&y=50&path=" + URLEncoder
.encode(file
.getRemotePath(), "UTF-8"));
173 HttpResponse response
= httpclient
.execute(httpget
);
174 HttpEntity entity
= response
.getEntity();
176 if (entity
!= null
) {
177 byte[] bytes
= EntityUtils
.toByteArray(entity
);
178 Bitmap bitmap
= BitmapFactory
.decodeByteArray(bytes
, 0, bytes
.length
);
179 thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
);
181 // Add thumbnail to cache
182 if (thumbnail
!= null
){
183 addBitmapToCache(imageKey
, thumbnail
);
186 } catch(Exception e
){
189 httpclient
.getConnectionManager().shutdown();
196 protected void onPostExecute(Bitmap bitmap
){
198 fileIcon
.setImageBitmap(bitmap
);
203 public void addBitmapToCache(String key
, Bitmap bitmap
) {
204 synchronized (mDiskCacheLock
) {
205 if (mDiskLruCache
!= null
&& mDiskLruCache
.getBitmap(key
) == null
) {
206 mDiskLruCache
.put(key
, bitmap
);
211 public Bitmap
getBitmapFromDiskCache(String key
) {
212 synchronized (mDiskCacheLock
) {
213 // Wait while disk cache is started from background thread
214 while (mDiskCacheStarting
) {
216 mDiskCacheLock
.wait();
217 } catch (InterruptedException e
) {}
219 if (mDiskLruCache
!= null
) {
220 return (Bitmap
) mDiskLruCache
.getBitmap(key
);
227 public boolean areAllItemsEnabled() {
232 public boolean isEnabled(int position
) {
237 public int getCount() {
238 return mFiles
!= null ? mFiles
.size() : 0;
242 public Object
getItem(int position
) {
243 if (mFiles
== null
|| mFiles
.size() <= position
)
245 return mFiles
.get(position
);
249 public long getItemId(int position
) {
250 if (mFiles
== null
|| mFiles
.size() <= position
)
252 return mFiles
.get(position
).getFileId();
256 public int getItemViewType(int position
) {
261 public View
getView(int position
, View convertView
, ViewGroup parent
) {
262 View view
= convertView
;
264 LayoutInflater inflator
= (LayoutInflater
) mContext
265 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
266 view
= inflator
.inflate(R
.layout
.list_item
, null
);
269 if (mFiles
!= null
&& mFiles
.size() > position
) {
270 OCFile file
= mFiles
.get(position
);
271 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
272 String name
= file
.getFileName();
274 fileName
.setText(name
);
275 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
276 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
277 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
278 sharedWithMeIconV
.setVisibility(View
.GONE
);
280 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
281 localStateView
.bringToFront();
282 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
283 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
284 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
285 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
286 localStateView
.setVisibility(View
.VISIBLE
);
287 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
288 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
289 localStateView
.setVisibility(View
.VISIBLE
);
290 } else if (file
.isDown()) {
291 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
292 localStateView
.setVisibility(View
.VISIBLE
);
294 localStateView
.setVisibility(View
.INVISIBLE
);
297 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
298 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
299 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
301 if (!file
.isFolder()) {
302 fileSizeV
.setVisibility(View
.VISIBLE
);
303 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
304 lastModV
.setVisibility(View
.VISIBLE
);
305 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
306 // this if-else is needed even thoe fav icon is visible by default
307 // because android reuses views in listview
308 if (!file
.keepInSync()) {
309 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
311 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
314 ListView parentList
= (ListView
)parent
;
315 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
316 checkBoxV
.setVisibility(View
.GONE
);
318 if (parentList
.isItemChecked(position
)) {
319 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
321 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
323 checkBoxV
.setVisibility(View
.VISIBLE
);
326 // first set thumbnail according to Mimetype, prevents empty thumbnails
327 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
329 // get Thumbnail if file is image
331 // Thumbnail in Cache?
332 Bitmap thumbnail
= getBitmapFromDiskCache(String
.valueOf(file
.getRemoteId()));
333 if (thumbnail
!= null
){
334 fileIcon
.setImageBitmap(thumbnail
);
336 // generate new Thumbnail
337 new BitmapWorkerTask(fileIcon
).execute(file
);
341 if (checkIfFileIsSharedWithMe(file
)) {
342 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
346 fileSizeV
.setVisibility(View
.INVISIBLE
);
347 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
348 lastModV
.setVisibility(View
.VISIBLE
);
349 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
350 checkBoxV
.setVisibility(View
.GONE
);
351 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
353 if (checkIfFileIsSharedWithMe(file
)) {
354 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
355 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
357 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
360 // If folder is sharedByLink, icon folder must be changed to
362 if (file
.isShareByLink()) {
363 fileIcon
.setImageResource(R
.drawable
.folder_public
);
367 if (file
.isShareByLink()) {
368 sharedIconV
.setVisibility(View
.VISIBLE
);
370 sharedIconV
.setVisibility(View
.GONE
);
378 public int getViewTypeCount() {
383 public boolean hasStableIds() {
388 public boolean isEmpty() {
389 return (mFiles
== null
|| mFiles
.isEmpty());
393 * Change the adapted directory for a new one
394 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
395 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
397 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
399 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
400 mStorageManager
= updatedStorageManager
;
401 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
403 if (mStorageManager
!= null
) {
404 mFiles
= mStorageManager
.getFolderContent(mFile
);
408 notifyDataSetChanged();
412 * Check if parent folder does not include 'S' permission and if file/folder
415 * @param file: OCFile
416 * @return boolean: True if it is shared with me and false if it is not
418 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
419 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
420 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));