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
;
22 import java
.util
.Collections
;
23 import java
.util
.Comparator
;
24 import java
.util
.Vector
;
26 import third_parties
.daveKoeller
.AlphanumComparator
;
27 import android
.accounts
.Account
;
28 import android
.content
.Context
;
30 import android
.graphics
.Bitmap
;
31 import android
.graphics
.BitmapFactory
;
32 import android
.media
.ThumbnailUtils
;
34 import android
.content
.SharedPreferences
;
35 import android
.graphics
.Bitmap
;
36 import android
.preference
.PreferenceManager
;
37 >>>>>>> upstream
/develop
38 import android
.view
.LayoutInflater
;
39 import android
.view
.View
;
40 import android
.view
.ViewGroup
;
41 import android
.widget
.BaseAdapter
;
42 import android
.widget
.GridView
;
43 import android
.widget
.ImageView
;
44 import android
.widget
.ListAdapter
;
45 import android
.widget
.ListView
;
46 import android
.widget
.TextView
;
48 import com
.owncloud
.android
.R
;
49 import com
.owncloud
.android
.authentication
.AccountUtils
;
50 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
51 import com
.owncloud
.android
.datamodel
.OCFile
;
52 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
53 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
.AsyncDrawable
;
54 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
55 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
56 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
57 import com
.owncloud
.android
.utils
.DisplayUtils
;
59 import com
.owncloud
.android
.utils
.Log_OC
;
62 import com
.owncloud
.android
.utils
.FileStorageUtils
;
64 >>>>>>> upstream
/develop
67 * This Adapter populates a ListView with all files and folders in an ownCloud
70 * @author Bartek Przybylski
71 * @author Tobias Kaminsky
72 * @author David A. Velasco
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
;
80 private boolean mJustFolders
;
82 private FileDataStorageManager mStorageManager
;
83 private Account mAccount
;
84 private ComponentsGetter mTransferServiceGetter
;
86 private enum ViewType
{LIST_ITEM
, GRID_IMAGE
, GRID_ITEM
};
88 private Integer mSortOrder
;
89 public static final Integer SORT_NAME
= 0;
90 public static final Integer SORT_DATE
= 1;
91 public static final Integer SORT_SIZE
= 2;
92 private Boolean mSortAscending
;
93 private SharedPreferences mAppPreferences
;
94 >>>>>>> upstream
/develop
96 public FileListListAdapter(
99 ComponentsGetter transferServiceGetter
102 mJustFolders
= justFolders
;
104 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
106 mTransferServiceGetter
= transferServiceGetter
;
108 mAppPreferences
= PreferenceManager
109 .getDefaultSharedPreferences(mContext
);
111 // Read sorting order, default to sort by name ascending
112 mSortOrder
= mAppPreferences
113 .getInt("sortOrder", 0);
114 mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
116 // initialise thumbnails cache on background thread
117 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
122 public boolean areAllItemsEnabled() {
127 public boolean isEnabled(int position
) {
132 public int getCount() {
133 return mFiles
!= null ? mFiles
.size() : 0;
137 public Object
getItem(int position
) {
138 if (mFiles
== null
|| mFiles
.size() <= position
)
140 return mFiles
.get(position
);
144 public long getItemId(int position
) {
145 if (mFiles
== null
|| mFiles
.size() <= position
)
147 return mFiles
.get(position
).getFileId();
151 public int getItemViewType(int position
) {
156 public View
getView(int position
, View convertView
, ViewGroup parent
) {
157 // decide image vs. file view
161 for (OCFile file
: mFiles
){
167 // TODO threshold as constant in Preferences
168 // > 50% Images --> image view
169 boolean fileView
= true
;
170 if ((count
/ mFiles
.size()) >= 0.5){
176 View view
= convertView
;
178 LayoutInflater inflator
= (LayoutInflater
) mContext
179 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
181 if (mFiles
!= null
&& mFiles
.size() > position
) {
182 file
= mFiles
.get(position
);
185 // Find out which layout should be displayed
188 viewType
= ViewType
.LIST_ITEM
;
189 } else if (file
.isImage()){
190 viewType
= ViewType
.GRID_IMAGE
;
192 viewType
= ViewType
.GRID_ITEM
;
198 view
= inflator
.inflate(R
.layout
.grid_image
, null
);
201 view
= inflator
.inflate(R
.layout
.grid_item
, null
);
204 view
= inflator
.inflate(R
.layout
.list_item
, null
);
213 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.thumbnail
);
218 if (mFiles
!= null
&& mFiles
.size() > position
) {
219 OCFile file
= mFiles
.get(position
);
220 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
221 String name
= file
.getFileName();
223 fileName
.setText(name
);
224 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
225 fileIcon
.setTag(file
.getFileId());
226 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
227 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
228 sharedWithMeIconV
.setVisibility(View
.GONE
);
230 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
231 localStateView
.bringToFront();
232 FileDownloaderBinder downloaderBinder
=
233 mTransferServiceGetter
.getFileDownloaderBinder();
234 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
235 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
236 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
237 localStateView
.setVisibility(View
.VISIBLE
);
238 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
239 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
240 localStateView
.setVisibility(View
.VISIBLE
);
241 } else if (file
.isDown()) {
242 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
243 localStateView
.setVisibility(View
.VISIBLE
);
245 localStateView
.setVisibility(View
.INVISIBLE
);
248 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
249 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
250 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
251 >>>>>>> upstream
/develop
255 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
256 name
= file
.getFileName();
257 fileName
.setText(name
);
259 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
260 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
261 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
263 lastModV
.setVisibility(View
.VISIBLE
);
265 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
267 checkBoxV
.setVisibility(View
.GONE
);
269 fileSizeV
.setVisibility(View
.VISIBLE
);
270 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
272 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
275 if (file
.isShareByLink()) {
276 sharedIconV
.setVisibility(View
.VISIBLE
);
279 DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp())
281 // this if-else is needed even thoe fav icon is visible by default
282 // because android reuses views in listview
283 if (!file
.keepInSync()) {
284 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
285 >>>>>>> upstream
/develop
287 sharedIconV
.setVisibility(View
.GONE
);
290 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.localFileIndicator
);
292 if (!file
.isFolder()) {
293 GridView parentList
= (GridView
)parent
;
294 if (parentList
.getChoiceMode() == GridView
.CHOICE_MODE_NONE
) {
295 checkBoxV
.setVisibility(View
.GONE
);
297 if (parentList
.isItemChecked(position
)) {
298 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
300 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
302 checkBoxV
.setVisibility(View
.VISIBLE
);
306 localStateView
.bringToFront();
307 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
308 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
309 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
310 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
311 localStateView
.setVisibility(View
.VISIBLE
);
312 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
313 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
314 localStateView
.setVisibility(View
.VISIBLE
);
315 } else if (file
.isDown()) {
316 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
317 localStateView
.setVisibility(View
.VISIBLE
);
319 localStateView
.setVisibility(View
.INVISIBLE
);
322 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
323 if (checkIfFileIsSharedWithMe(file
)) {
324 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
326 sharedWithMeIconV
.setVisibility(View
.GONE
);
329 localStateView
.setVisibility(View
.INVISIBLE
);
333 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
334 name
= file
.getFileName();
335 fileName
.setText(name
);
343 // this if-else is needed even though favorite icon is visible by default
344 // because android reuses views in listview
345 if (!file
.keepInSync()) {
346 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.GONE
);
348 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.VISIBLE
);
352 if (!file
.isFolder()) {
353 if (file
.isImage() && file
.isDown()){
354 Bitmap bitmap
= BitmapFactory
.decodeFile(file
.getStoragePath());
355 fileIcon
.setImageBitmap(ThumbnailUtils
.extractThumbnail(bitmap
, 200, 200));
357 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
362 checkBoxV
.setVisibility(View
.VISIBLE
);
365 // get Thumbnail if file is image
366 if (file
.isImage() && file
.getRemoteId() != null
){
367 // Thumbnail in Cache?
368 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
369 String
.valueOf(file
.getRemoteId())
371 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
372 fileIcon
.setImageBitmap(thumbnail
);
374 // generate new Thumbnail
375 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
376 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
377 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
378 fileIcon
, mStorageManager
, mAccount
380 if (thumbnail
== null
) {
381 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
383 final AsyncDrawable asyncDrawable
= new AsyncDrawable(
384 mContext
.getResources(),
388 fileIcon
.setImageDrawable(asyncDrawable
);
393 fileIcon
.setImageResource(
394 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
398 if (checkIfFileIsSharedWithMe(file
)) {
399 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
403 // TODO Re-enable when server supports folder-size calculation
404 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
405 // fileSizeV.setVisibility(View.VISIBLE);
406 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
408 fileSizeV
.setVisibility(View
.INVISIBLE
);
411 lastModV
.setVisibility(View
.VISIBLE
);
413 DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp())
415 checkBoxV
.setVisibility(View
.GONE
);
416 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
418 >>>>>>> upstream
/develop
419 if (checkIfFileIsSharedWithMe(file
)) {
420 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
421 } else if (file
.isShareByLink()) {
422 // If folder is sharedByLink, icon folder must be changed to
424 fileIcon
.setImageResource(R
.drawable
.folder_public
);
426 fileIcon
.setImageResource(
427 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
437 * Local Folder size in human readable format
441 * @return Size in human readable format
443 private String
getFolderSizeHuman(String path
) {
445 File dir
= new File(path
);
448 long bytes
= getFolderSize(dir
);
449 return DisplayUtils
.bytesToHumanReadable(bytes
);
458 * @return Size in bytes
460 private long getFolderSize(File dir
) {
463 File
[] fileList
= dir
.listFiles();
464 for(int i
= 0; i
< fileList
.length
; i
++) {
465 if(fileList
[i
].isDirectory()) {
466 result
+= getFolderSize(fileList
[i
]);
468 result
+= fileList
[i
].length();
477 public int getViewTypeCount() {
482 public boolean hasStableIds() {
487 public boolean isEmpty() {
488 return (mFiles
== null
|| mFiles
.isEmpty());
492 * Change the adapted directory for a new one
493 * @param directory New file to adapt. Can be NULL, meaning
494 * "no content to adapt".
495 * @param updatedStorageManager Optional updated storage manager; used to replace
496 * mStorageManager if is different (and not NULL)
498 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
500 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
501 mStorageManager
= updatedStorageManager
;
502 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
504 if (mStorageManager
!= null
) {
505 mFiles
= mStorageManager
.getFolderContent(mFile
);
507 mFiles
= getFolders(mFiles
);
517 * Sorts all filenames, regarding last user decision
519 private void sortDirectory(){
522 sortByName(mSortAscending
);
525 sortByDate(mSortAscending
);
528 sortBySize(mSortAscending
);
532 notifyDataSetChanged();
537 * Filter for getting only the folders
539 * @return Vector<OCFile>
541 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
542 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
543 OCFile current
= null
;
544 for (int i
=0; i
<files
.size(); i
++) {
545 current
= files
.get(i
);
546 if (current
.isFolder()) {
555 * Check if parent folder does not include 'S' permission and if file/folder
558 * @param file: OCFile
559 * @return boolean: True if it is shared with me and false if it is not
561 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
562 return (mFile
.getPermissions() != null
563 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
564 && file
.getPermissions() != null
565 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
570 * @param sortAscending true: ascending, false: descending
572 private void sortByDate(boolean sortAscending
){
580 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
581 public int compare(OCFile o1
, OCFile o2
) {
582 if (o1
.isFolder() && o2
.isFolder()) {
583 Long obj1
= o1
.getModificationTimestamp();
584 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
586 else if (o1
.isFolder()) {
588 } else if (o2
.isFolder()) {
590 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
593 Long obj1
= o1
.getModificationTimestamp();
594 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
602 * @param sortAscending true: ascending, false: descending
604 private void sortBySize(boolean sortAscending
){
612 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
613 public int compare(OCFile o1
, OCFile o2
) {
614 if (o1
.isFolder() && o2
.isFolder()) {
615 Long obj1
= getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o1
)));
616 return val
* obj1
.compareTo(getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o2
))));
618 else if (o1
.isFolder()) {
620 } else if (o2
.isFolder()) {
622 } else if (o1
.getFileLength() == 0 || o2
.getFileLength() == 0){
625 Long obj1
= o1
.getFileLength();
626 return val
* obj1
.compareTo(o2
.getFileLength());
634 * @param sortAscending true: ascending, false: descending
636 private void sortByName(boolean sortAscending
){
644 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
645 public int compare(OCFile o1
, OCFile o2
) {
646 if (o1
.isFolder() && o2
.isFolder()) {
647 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
648 } else if (o1
.isFolder()) {
650 } else if (o2
.isFolder()) {
653 return val
* new AlphanumComparator().compare(o1
, o2
);
658 public void setSortOrder(Integer order
, boolean ascending
) {
659 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
660 editor
.putInt("sortOrder", order
);
661 editor
.putBoolean("sortAscending", ascending
);
665 mSortAscending
= ascending
;