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
;
29 import android
.content
.SharedPreferences
;
30 import android
.graphics
.Bitmap
;
31 import android
.graphics
.BitmapFactory
;
32 import android
.media
.ThumbnailUtils
;
33 import android
.preference
.PreferenceManager
;
34 import android
.text
.format
.DateUtils
;
35 import android
.view
.LayoutInflater
;
36 import android
.view
.View
;
37 import android
.view
.ViewGroup
;
38 import android
.widget
.BaseAdapter
;
39 import android
.widget
.Filter
;
40 import android
.widget
.Filterable
;
41 import android
.widget
.GridView
;
42 import android
.widget
.ImageView
;
43 import android
.widget
.ListAdapter
;
44 import android
.widget
.TextView
;
46 import com
.owncloud
.android
.R
;
47 import com
.owncloud
.android
.authentication
.AccountUtils
;
48 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
49 import com
.owncloud
.android
.datamodel
.OCFile
;
50 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
51 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
.AsyncDrawable
;
52 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
53 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
54 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
55 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
56 import com
.owncloud
.android
.utils
.DisplayUtils
;
57 import com
.owncloud
.android
.utils
.FileStorageUtils
;
61 * This Adapter populates a ListView with all files and folders in an ownCloud
64 * @author Bartek Przybylski
65 * @author Tobias Kaminsky
66 * @author David A. Velasco
68 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
69 private final static String PERMISSION_SHARED_WITH_ME
= "S";
71 private Context mContext
;
72 private OCFile mFile
= null
;
73 private Vector
<OCFile
> mFiles
= null
;
74 private Vector
<OCFile
> mFilesOrig
= new Vector
<OCFile
>();
75 private boolean mJustFolders
;
77 private FileDataStorageManager mStorageManager
;
78 private Account mAccount
;
79 private ComponentsGetter mTransferServiceGetter
;
80 private enum ViewType
{LIST_ITEM
, GRID_IMAGE
, GRID_ITEM
};
81 private Integer mSortOrder
;
82 public static final Integer SORT_NAME
= 0;
83 public static final Integer SORT_DATE
= 1;
84 public static final Integer SORT_SIZE
= 2;
85 private Boolean mSortAscending
;
86 private SharedPreferences mAppPreferences
;
88 public FileListListAdapter(
91 ComponentsGetter transferServiceGetter
94 mJustFolders
= justFolders
;
96 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
97 mTransferServiceGetter
= transferServiceGetter
;
99 mAppPreferences
= PreferenceManager
100 .getDefaultSharedPreferences(mContext
);
102 // Read sorting order, default to sort by name ascending
103 mSortOrder
= mAppPreferences
104 .getInt("sortOrder", 0);
105 mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
107 // initialise thumbnails cache on background thread
108 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
112 public boolean areAllItemsEnabled() {
117 public boolean isEnabled(int position
) {
122 public int getCount() {
123 return mFiles
!= null ? mFiles
.size() : 0;
127 public Object
getItem(int position
) {
128 if (mFiles
== null
|| mFiles
.size() <= position
)
130 return mFiles
.get(position
);
134 public long getItemId(int position
) {
135 if (mFiles
== null
|| mFiles
.size() <= position
)
137 return mFiles
.get(position
).getFileId();
141 public int getItemViewType(int position
) {
146 public View
getView(int position
, View convertView
, ViewGroup parent
) {
147 // decide image vs. file view
148 double countImages
= 0;
149 double countFiles
= 0;
151 for (OCFile file
: mFiles
){
152 if (!file
.isFolder()){
161 // TODO threshold as constant in Preferences
162 // > 50% Images --> image view
163 boolean fileView
= true
;
164 if ((countImages
/ countFiles
) >= 0.5){
170 View view
= convertView
;
172 LayoutInflater inflator
= (LayoutInflater
) mContext
173 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
175 if (mFiles
!= null
&& mFiles
.size() > position
) {
176 file
= mFiles
.get(position
);
179 // Find out which layout should be displayed
182 viewType
= ViewType
.LIST_ITEM
;
183 } else if (file
.isImage()){
184 viewType
= ViewType
.GRID_IMAGE
;
186 viewType
= ViewType
.GRID_ITEM
;
192 view
= inflator
.inflate(R
.layout
.grid_image
, null
);
195 view
= inflator
.inflate(R
.layout
.grid_item
, null
);
198 view
= inflator
.inflate(R
.layout
.list_item
, null
);
206 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.thumbnail
);
212 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
213 name
= file
.getFileName();
214 fileName
.setText(name
);
216 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
217 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
218 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
220 lastModV
.setVisibility(View
.VISIBLE
);
221 lastModV
.setText(showRelativeTimestamp(file
));
223 checkBoxV
.setVisibility(View
.GONE
);
225 fileSizeV
.setVisibility(View
.VISIBLE
);
226 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
228 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
231 if (file
.isShareByLink()) {
232 sharedIconV
.setVisibility(View
.VISIBLE
);
234 sharedIconV
.setVisibility(View
.GONE
);
237 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.localFileIndicator
);
239 if (!file
.isFolder()) {
240 GridView parentList
= (GridView
)parent
;
241 if (parentList
.getChoiceMode() == GridView
.CHOICE_MODE_NONE
) {
242 checkBoxV
.setVisibility(View
.GONE
);
244 if (parentList
.isItemChecked(position
)) {
245 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
247 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
249 checkBoxV
.setVisibility(View
.VISIBLE
);
252 localStateView
.bringToFront();
253 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
254 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
255 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
256 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
257 localStateView
.setVisibility(View
.VISIBLE
);
258 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
259 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
260 localStateView
.setVisibility(View
.VISIBLE
);
261 } else if (file
.isDown()) {
262 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
263 localStateView
.setVisibility(View
.VISIBLE
);
265 localStateView
.setVisibility(View
.INVISIBLE
);
268 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
269 if (checkIfFileIsSharedWithMe(file
)) {
270 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
272 sharedWithMeIconV
.setVisibility(View
.GONE
);
275 localStateView
.setVisibility(View
.INVISIBLE
);
279 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
280 name
= file
.getFileName();
281 fileName
.setText(name
);
289 // this if-else is needed even though favorite icon is visible by default
290 // because android reuses views in listview
291 if (!file
.keepInSync()) {
292 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.GONE
);
294 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.VISIBLE
);
298 if (!file
.isFolder()) {
299 if (file
.isImage() && file
.getRemoteId() != null
){
300 // Thumbnail in Cache?
301 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
302 String
.valueOf(file
.getRemoteId())
304 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
305 fileIcon
.setImageBitmap(thumbnail
);
307 // generate new Thumbnail
308 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
309 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
310 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
311 fileIcon
, mStorageManager
, mAccount
313 if (thumbnail
== null
) {
314 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
316 final AsyncDrawable asyncDrawable
= new AsyncDrawable(
317 mContext
.getResources(),
321 fileIcon
.setImageDrawable(asyncDrawable
);
328 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
332 if (checkIfFileIsSharedWithMe(file
)) {
333 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
334 } else if (file
.isShareByLink()) {
335 // If folder is sharedByLink, icon folder must be changed to
337 fileIcon
.setImageResource(R
.drawable
.folder_public
);
339 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
348 * Local Folder size in human readable format
352 * @return Size in human readable format
354 private String
getFolderSizeHuman(String path
) {
356 File dir
= new File(path
);
359 long bytes
= getFolderSize(dir
);
360 return DisplayUtils
.bytesToHumanReadable(bytes
);
369 * @return Size in bytes
371 private long getFolderSize(File dir
) {
374 File
[] fileList
= dir
.listFiles();
375 for(int i
= 0; i
< fileList
.length
; i
++) {
376 if(fileList
[i
].isDirectory()) {
377 result
+= getFolderSize(fileList
[i
]);
379 result
+= fileList
[i
].length();
388 public int getViewTypeCount() {
393 public boolean hasStableIds() {
398 public boolean isEmpty() {
399 return (mFiles
== null
|| mFiles
.isEmpty());
403 * Change the adapted directory for a new one
404 * @param directory New file to adapt. Can be NULL, meaning
405 * "no content to adapt".
406 * @param updatedStorageManager Optional updated storage manager; used to replace
407 * mStorageManager if is different (and not NULL)
409 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
411 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
412 mStorageManager
= updatedStorageManager
;
413 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
415 if (mStorageManager
!= null
) {
416 mFiles
= mStorageManager
.getFolderContent(mFile
);
418 mFilesOrig
.addAll(mFiles
);
421 mFiles
= getFolders(mFiles
);
431 * Sorts all filenames, regarding last user decision
433 private void sortDirectory(){
436 sortByName(mSortAscending
);
439 sortByDate(mSortAscending
);
442 sortBySize(mSortAscending
);
446 notifyDataSetChanged();
451 * Filter for getting only the folders
453 * @return Vector<OCFile>
455 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
456 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
457 OCFile current
= null
;
458 for (int i
=0; i
<files
.size(); i
++) {
459 current
= files
.get(i
);
460 if (current
.isFolder()) {
469 * Check if parent folder does not include 'S' permission and if file/folder
472 * @param file: OCFile
473 * @return boolean: True if it is shared with me and false if it is not
475 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
476 return (mFile
.getPermissions() != null
477 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
478 && file
.getPermissions() != null
479 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
484 * @param sortAscending true: ascending, false: descending
486 private void sortByDate(boolean sortAscending
){
494 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
495 public int compare(OCFile o1
, OCFile o2
) {
496 if (o1
.isFolder() && o2
.isFolder()) {
497 Long obj1
= o1
.getModificationTimestamp();
498 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
500 else if (o1
.isFolder()) {
502 } else if (o2
.isFolder()) {
504 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
507 Long obj1
= o1
.getModificationTimestamp();
508 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
516 * @param sortAscending true: ascending, false: descending
518 private void sortBySize(boolean sortAscending
){
526 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
527 public int compare(OCFile o1
, OCFile o2
) {
528 if (o1
.isFolder() && o2
.isFolder()) {
529 Long obj1
= getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o1
)));
530 return val
* obj1
.compareTo(getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o2
))));
532 else if (o1
.isFolder()) {
534 } else if (o2
.isFolder()) {
536 } else if (o1
.getFileLength() == 0 || o2
.getFileLength() == 0){
539 Long obj1
= o1
.getFileLength();
540 return val
* obj1
.compareTo(o2
.getFileLength());
548 * @param sortAscending true: ascending, false: descending
550 private void sortByName(boolean sortAscending
){
558 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
559 public int compare(OCFile o1
, OCFile o2
) {
560 if (o1
.isFolder() && o2
.isFolder()) {
561 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
562 } else if (o1
.isFolder()) {
564 } else if (o2
.isFolder()) {
567 return val
* new AlphanumComparator().compare(o1
, o2
);
572 public void setSortOrder(Integer order
, boolean ascending
) {
573 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
574 editor
.putInt("sortOrder", order
);
575 editor
.putBoolean("sortAscending", ascending
);
579 mSortAscending
= ascending
;
584 private CharSequence
showRelativeTimestamp(OCFile file
){
585 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
586 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);