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
.preference
.PreferenceManager
;
32 import android
.text
.format
.DateUtils
;
33 import android
.view
.LayoutInflater
;
34 import android
.view
.View
;
35 import android
.view
.ViewGroup
;
36 import android
.widget
.BaseAdapter
;
37 import android
.widget
.ImageView
;
38 import android
.widget
.ListAdapter
;
39 import android
.widget
.ListView
;
40 import android
.widget
.TextView
;
42 import com
.owncloud
.android
.R
;
43 import com
.owncloud
.android
.authentication
.AccountUtils
;
44 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
45 import com
.owncloud
.android
.datamodel
.OCFile
;
46 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
47 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
.AsyncDrawable
;
48 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
49 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
50 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
51 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
52 import com
.owncloud
.android
.utils
.DisplayUtils
;
53 import com
.owncloud
.android
.utils
.FileStorageUtils
;
57 * This Adapter populates a ListView with all files and folders in an ownCloud
60 * @author Bartek Przybylski
61 * @author Tobias Kaminsky
62 * @author David A. Velasco
64 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
65 private final static String PERMISSION_SHARED_WITH_ME
= "S";
67 private Context mContext
;
68 private OCFile mFile
= null
;
69 private Vector
<OCFile
> mFiles
= null
;
70 private boolean mJustFolders
;
72 private FileDataStorageManager mStorageManager
;
73 private Account mAccount
;
74 private ComponentsGetter mTransferServiceGetter
;
75 private Integer mSortOrder
;
76 public static final Integer SORT_NAME
= 0;
77 public static final Integer SORT_DATE
= 1;
78 public static final Integer SORT_SIZE
= 2;
79 private Boolean mSortAscending
;
80 private SharedPreferences mAppPreferences
;
82 public FileListListAdapter(
85 ComponentsGetter transferServiceGetter
88 mJustFolders
= justFolders
;
90 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
92 mTransferServiceGetter
= transferServiceGetter
;
94 mAppPreferences
= PreferenceManager
95 .getDefaultSharedPreferences(mContext
);
97 // Read sorting order, default to sort by name ascending
98 mSortOrder
= mAppPreferences
99 .getInt("sortOrder", 0);
100 mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
102 // initialise thumbnails cache on background thread
103 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
108 public boolean areAllItemsEnabled() {
113 public boolean isEnabled(int position
) {
118 public int getCount() {
119 return mFiles
!= null ? mFiles
.size() : 0;
123 public Object
getItem(int position
) {
124 if (mFiles
== null
|| mFiles
.size() <= position
)
126 return mFiles
.get(position
);
130 public long getItemId(int position
) {
131 if (mFiles
== null
|| mFiles
.size() <= position
)
133 return mFiles
.get(position
).getFileId();
137 public int getItemViewType(int position
) {
142 public View
getView(int position
, View convertView
, ViewGroup parent
) {
143 View view
= convertView
;
145 LayoutInflater inflator
= (LayoutInflater
) mContext
146 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
147 view
= inflator
.inflate(R
.layout
.list_item
, null
);
150 if (mFiles
!= null
&& mFiles
.size() > position
) {
151 OCFile file
= mFiles
.get(position
);
152 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
153 String name
= file
.getFileName();
155 fileName
.setText(name
);
156 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
157 fileIcon
.setTag(file
.getFileId());
158 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
159 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
160 sharedWithMeIconV
.setVisibility(View
.GONE
);
162 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
163 localStateView
.bringToFront();
164 FileDownloaderBinder downloaderBinder
=
165 mTransferServiceGetter
.getFileDownloaderBinder();
166 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
167 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
168 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
169 localStateView
.setVisibility(View
.VISIBLE
);
170 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
171 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
172 localStateView
.setVisibility(View
.VISIBLE
);
173 } else if (file
.isDown()) {
174 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
175 localStateView
.setVisibility(View
.VISIBLE
);
177 localStateView
.setVisibility(View
.INVISIBLE
);
180 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
181 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
182 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
184 if (!file
.isFolder()) {
185 fileSizeV
.setVisibility(View
.VISIBLE
);
186 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
187 lastModV
.setVisibility(View
.VISIBLE
);
188 lastModV
.setText(showRelativeTimestamp(file
));
189 // this if-else is needed even thoe fav icon is visible by default
190 // because android reuses views in listview
191 if (!file
.keepInSync()) {
192 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
194 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
197 ListView parentList
= (ListView
)parent
;
198 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
199 checkBoxV
.setVisibility(View
.GONE
);
201 if (parentList
.isItemChecked(position
)) {
202 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
204 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
206 checkBoxV
.setVisibility(View
.VISIBLE
);
209 // get Thumbnail if file is image
210 if (file
.isImage() && file
.getRemoteId() != null
){
211 // Thumbnail in Cache?
212 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
213 String
.valueOf(file
.getRemoteId())
215 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
216 fileIcon
.setImageBitmap(thumbnail
);
218 // generate new Thumbnail
219 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
220 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
221 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
222 fileIcon
, mStorageManager
, mAccount
224 if (thumbnail
== null
) {
225 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
227 final AsyncDrawable asyncDrawable
= new AsyncDrawable(
228 mContext
.getResources(),
232 fileIcon
.setImageDrawable(asyncDrawable
);
237 fileIcon
.setImageResource(
238 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
242 if (checkIfFileIsSharedWithMe(file
)) {
243 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
247 // TODO Re-enable when server supports folder-size calculation
248 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
249 // fileSizeV.setVisibility(View.VISIBLE);
250 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
252 fileSizeV
.setVisibility(View
.INVISIBLE
);
255 lastModV
.setVisibility(View
.VISIBLE
);
256 lastModV
.setText(showRelativeTimestamp(file
));
257 checkBoxV
.setVisibility(View
.GONE
);
258 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
260 if (checkIfFileIsSharedWithMe(file
)) {
261 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
262 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
264 fileIcon
.setImageResource(
265 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
269 // If folder is sharedByLink, icon folder must be changed to
271 if (file
.isShareByLink()) {
272 fileIcon
.setImageResource(R
.drawable
.folder_public
);
276 if (file
.isShareByLink()) {
277 sharedIconV
.setVisibility(View
.VISIBLE
);
279 sharedIconV
.setVisibility(View
.GONE
);
287 * Local Folder size in human readable format
291 * @return Size in human readable format
293 private String
getFolderSizeHuman(String path
) {
295 File dir
= new File(path
);
298 long bytes
= getFolderSize(dir
);
299 return DisplayUtils
.bytesToHumanReadable(bytes
);
308 * @return Size in bytes
310 private long getFolderSize(File dir
) {
313 File
[] fileList
= dir
.listFiles();
314 for(int i
= 0; i
< fileList
.length
; i
++) {
315 if(fileList
[i
].isDirectory()) {
316 result
+= getFolderSize(fileList
[i
]);
318 result
+= fileList
[i
].length();
327 public int getViewTypeCount() {
332 public boolean hasStableIds() {
337 public boolean isEmpty() {
338 return (mFiles
== null
|| mFiles
.isEmpty());
342 * Change the adapted directory for a new one
343 * @param directory New file to adapt. Can be NULL, meaning
344 * "no content to adapt".
345 * @param updatedStorageManager Optional updated storage manager; used to replace
346 * mStorageManager if is different (and not NULL)
348 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
350 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
351 mStorageManager
= updatedStorageManager
;
352 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
354 if (mStorageManager
!= null
) {
355 mFiles
= mStorageManager
.getFolderContent(mFile
);
357 mFiles
= getFolders(mFiles
);
367 * Sorts all filenames, regarding last user decision
369 private void sortDirectory(){
372 sortByName(mSortAscending
);
375 sortByDate(mSortAscending
);
378 sortBySize(mSortAscending
);
382 notifyDataSetChanged();
387 * Filter for getting only the folders
389 * @return Vector<OCFile>
391 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
392 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
393 OCFile current
= null
;
394 for (int i
=0; i
<files
.size(); i
++) {
395 current
= files
.get(i
);
396 if (current
.isFolder()) {
405 * Check if parent folder does not include 'S' permission and if file/folder
408 * @param file: OCFile
409 * @return boolean: True if it is shared with me and false if it is not
411 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
412 return (mFile
.getPermissions() != null
413 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
414 && file
.getPermissions() != null
415 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
420 * @param sortAscending true: ascending, false: descending
422 private void sortByDate(boolean sortAscending
){
430 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
431 public int compare(OCFile o1
, OCFile o2
) {
432 if (o1
.isFolder() && o2
.isFolder()) {
433 Long obj1
= o1
.getModificationTimestamp();
434 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
436 else if (o1
.isFolder()) {
438 } else if (o2
.isFolder()) {
440 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
443 Long obj1
= o1
.getModificationTimestamp();
444 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
452 * @param sortAscending true: ascending, false: descending
454 private void sortBySize(boolean sortAscending
){
462 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
463 public int compare(OCFile o1
, OCFile o2
) {
464 if (o1
.isFolder() && o2
.isFolder()) {
465 Long obj1
= getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o1
)));
466 return val
* obj1
.compareTo(getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o2
))));
468 else if (o1
.isFolder()) {
470 } else if (o2
.isFolder()) {
472 } else if (o1
.getFileLength() == 0 || o2
.getFileLength() == 0){
475 Long obj1
= o1
.getFileLength();
476 return val
* obj1
.compareTo(o2
.getFileLength());
484 * @param sortAscending true: ascending, false: descending
486 private void sortByName(boolean sortAscending
){
494 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
495 public int compare(OCFile o1
, OCFile o2
) {
496 if (o1
.isFolder() && o2
.isFolder()) {
497 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
498 } else if (o1
.isFolder()) {
500 } else if (o2
.isFolder()) {
503 return val
* new AlphanumComparator().compare(o1
, o2
);
508 public void setSortOrder(Integer order
, boolean ascending
) {
509 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
510 editor
.putInt("sortOrder", order
);
511 editor
.putBoolean("sortAscending", ascending
);
515 mSortAscending
= ascending
;
520 private CharSequence
showRelativeTimestamp(OCFile file
){
521 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
522 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);