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
.files
.services
.FileDownloader
.FileDownloaderBinder
;
52 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
53 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
54 import com
.owncloud
.android
.utils
.DisplayUtils
;
55 import com
.owncloud
.android
.utils
.FileStorageUtils
;
59 * This Adapter populates a ListView with all files and folders in an ownCloud
62 * @author Bartek Przybylski
63 * @author Tobias Kaminsky
64 * @author David A. Velasco
66 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
67 private final static String PERMISSION_SHARED_WITH_ME
= "S";
69 private Context mContext
;
70 private OCFile mFile
= null
;
71 private Vector
<OCFile
> mFiles
= null
;
72 private Vector
<OCFile
> mFilesOrig
= new Vector
<OCFile
>();
73 private boolean mJustFolders
;
75 private FileDataStorageManager mStorageManager
;
76 private Account mAccount
;
77 private ComponentsGetter mTransferServiceGetter
;
78 private enum ViewType
{LIST_ITEM
, GRID_IMAGE
, GRID_ITEM
};
79 private Integer mSortOrder
;
80 public static final Integer SORT_NAME
= 0;
81 public static final Integer SORT_DATE
= 1;
82 public static final Integer SORT_SIZE
= 2;
83 private Boolean mSortAscending
;
84 private SharedPreferences mAppPreferences
;
86 public FileListListAdapter(
89 ComponentsGetter transferServiceGetter
92 mJustFolders
= justFolders
;
94 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
95 mTransferServiceGetter
= transferServiceGetter
;
97 mAppPreferences
= PreferenceManager
98 .getDefaultSharedPreferences(mContext
);
100 // Read sorting order, default to sort by name ascending
101 FileStorageUtils
.mSortOrder
= mAppPreferences
.getInt("sortOrder", 0);
102 FileStorageUtils
.mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
105 // initialise thumbnails cache on background thread
106 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
110 public boolean areAllItemsEnabled() {
115 public boolean isEnabled(int position
) {
120 public int getCount() {
121 return mFiles
!= null ? mFiles
.size() : 0;
125 public Object
getItem(int position
) {
126 if (mFiles
== null
|| mFiles
.size() <= position
)
128 return mFiles
.get(position
);
132 public long getItemId(int position
) {
133 if (mFiles
== null
|| mFiles
.size() <= position
)
135 return mFiles
.get(position
).getFileId();
139 public int getItemViewType(int position
) {
144 public View
getView(int position
, View convertView
, ViewGroup parent
) {
146 boolean fileView
= DisplayUtils
.decideViewLayout(mFiles
);
148 View view
= convertView
;
150 LayoutInflater inflator
= (LayoutInflater
) mContext
151 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
153 if (mFiles
!= null
&& mFiles
.size() > position
) {
154 file
= mFiles
.get(position
);
157 // Find out which layout should be displayed
160 viewType
= ViewType
.LIST_ITEM
;
161 } else if (file
.isImage()){
162 viewType
= ViewType
.GRID_IMAGE
;
164 viewType
= ViewType
.GRID_ITEM
;
170 view
= inflator
.inflate(R
.layout
.grid_image
, null
);
173 view
= inflator
.inflate(R
.layout
.grid_item
, null
);
176 view
= inflator
.inflate(R
.layout
.list_item
, null
);
184 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.thumbnail
);
190 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
191 name
= file
.getFileName();
192 fileName
.setText(name
);
194 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
195 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
196 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
198 lastModV
.setVisibility(View
.VISIBLE
);
199 lastModV
.setText(showRelativeTimestamp(file
));
201 checkBoxV
.setVisibility(View
.GONE
);
203 fileSizeV
.setVisibility(View
.VISIBLE
);
204 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
206 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
209 if (file
.isShareByLink()) {
210 sharedIconV
.setVisibility(View
.VISIBLE
);
212 sharedIconV
.setVisibility(View
.GONE
);
215 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.localFileIndicator
);
217 if (!file
.isFolder()) {
218 GridView parentList
= (GridView
)parent
;
219 if (parentList
.getChoiceMode() == GridView
.CHOICE_MODE_NONE
) {
220 checkBoxV
.setVisibility(View
.GONE
);
222 if (parentList
.isItemChecked(position
)) {
223 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
225 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
227 checkBoxV
.setVisibility(View
.VISIBLE
);
230 localStateView
.bringToFront();
231 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
232 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
233 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
234 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
235 localStateView
.setVisibility(View
.VISIBLE
);
236 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
237 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
238 localStateView
.setVisibility(View
.VISIBLE
);
239 } else if (file
.isDown()) {
240 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
241 localStateView
.setVisibility(View
.VISIBLE
);
243 localStateView
.setVisibility(View
.INVISIBLE
);
246 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
247 if (checkIfFileIsSharedWithMe(file
)) {
248 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
250 sharedWithMeIconV
.setVisibility(View
.GONE
);
253 localStateView
.setVisibility(View
.INVISIBLE
);
257 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
258 name
= file
.getFileName();
259 fileName
.setText(name
);
267 // this if-else is needed even though favorite icon is visible by default
268 // because android reuses views in listview
269 if (!file
.keepInSync()) {
270 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.GONE
);
272 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.VISIBLE
);
276 if (!file
.isFolder()) {
277 if (file
.isImage() && file
.getRemoteId() != null
){
278 // Thumbnail in Cache?
279 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
280 String
.valueOf(file
.getRemoteId())
282 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
283 fileIcon
.setImageBitmap(thumbnail
);
285 // generate new Thumbnail
286 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
287 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
288 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
289 fileIcon
, mStorageManager
, mAccount
291 if (thumbnail
== null
) {
292 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
294 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
295 new ThumbnailsCacheManager
.AsyncDrawable(
296 mContext
.getResources(),
300 fileIcon
.setImageDrawable(asyncDrawable
);
305 fileIcon
.setImageResource(DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName()));
309 if (checkIfFileIsSharedWithMe(file
)) {
310 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
311 } else if (file
.isShareByLink()) {
312 // If folder is sharedByLink, icon folder must be changed to
314 fileIcon
.setImageResource(R
.drawable
.folder_public
);
316 fileIcon
.setImageResource(
317 DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName())
327 * Local Folder size in human readable format
331 * @return Size in human readable format
333 private String
getFolderSizeHuman(String path
) {
335 File dir
= new File(path
);
338 long bytes
= FileStorageUtils
.getFolderSize(dir
);
339 return DisplayUtils
.bytesToHumanReadable(bytes
);
348 * @return Size in bytes
350 private long getFolderSize(File dir
) {
353 File
[] fileList
= dir
.listFiles();
354 for(int i
= 0; i
< fileList
.length
; i
++) {
355 if(fileList
[i
].isDirectory()) {
356 result
+= getFolderSize(fileList
[i
]);
358 result
+= fileList
[i
].length();
367 public int getViewTypeCount() {
372 public boolean hasStableIds() {
377 public boolean isEmpty() {
378 return (mFiles
== null
|| mFiles
.isEmpty());
382 * Change the adapted directory for a new one
383 * @param directory New file to adapt. Can be NULL, meaning
384 * "no content to adapt".
385 * @param updatedStorageManager Optional updated storage manager; used to replace
386 * mStorageManager if is different (and not NULL)
388 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
390 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
391 mStorageManager
= updatedStorageManager
;
392 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
394 if (mStorageManager
!= null
) {
395 mFiles
= mStorageManager
.getFolderContent(mFile
);
397 mFilesOrig
.addAll(mFiles
);
400 mFiles
= getFolders(mFiles
);
406 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
407 notifyDataSetChanged();
412 * Filter for getting only the folders
414 * @return Vector<OCFile>
416 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
417 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
418 OCFile current
= null
;
419 for (int i
=0; i
<files
.size(); i
++) {
420 current
= files
.get(i
);
421 if (current
.isFolder()) {
430 * Check if parent folder does not include 'S' permission and if file/folder
433 * @param file: OCFile
434 * @return boolean: True if it is shared with me and false if it is not
436 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
437 return (mFile
.getPermissions() != null
438 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
439 && file
.getPermissions() != null
440 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
445 * @param sortAscending true: ascending, false: descending
447 private void sortByDate(boolean sortAscending
){
455 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
456 public int compare(OCFile o1
, OCFile o2
) {
457 if (o1
.isFolder() && o2
.isFolder()) {
458 Long obj1
= o1
.getModificationTimestamp();
459 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
461 else if (o1
.isFolder()) {
463 } else if (o2
.isFolder()) {
465 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
468 Long obj1
= o1
.getModificationTimestamp();
469 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
477 * @param sortAscending true: ascending, false: descending
479 private void sortBySize(boolean sortAscending
){
487 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
488 public int compare(OCFile o1
, OCFile o2
) {
489 if (o1
.isFolder() && o2
.isFolder()) {
490 Long obj1
= getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o1
)));
491 return val
* obj1
.compareTo(getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o2
))));
493 else if (o1
.isFolder()) {
495 } else if (o2
.isFolder()) {
497 } else if (o1
.getFileLength() == 0 || o2
.getFileLength() == 0){
500 Long obj1
= o1
.getFileLength();
501 return val
* obj1
.compareTo(o2
.getFileLength());
509 * @param sortAscending true: ascending, false: descending
511 private void sortByName(boolean sortAscending
){
519 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
520 public int compare(OCFile o1
, OCFile o2
) {
521 if (o1
.isFolder() && o2
.isFolder()) {
522 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
523 } else if (o1
.isFolder()) {
525 } else if (o2
.isFolder()) {
528 return val
* new AlphanumComparator().compare(o1
, o2
);
533 public void setSortOrder(Integer order
, boolean ascending
) {
534 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
535 editor
.putInt("sortOrder", order
);
536 editor
.putBoolean("sortAscending", ascending
);
539 FileStorageUtils
.mSortOrder
= order
;
540 FileStorageUtils
.mSortAscending
= ascending
;
543 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
544 notifyDataSetChanged();
548 private CharSequence
showRelativeTimestamp(OCFile file
){
549 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
550 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);