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
.ui
.activity
.ComponentsGetter
;
51 import com
.owncloud
.android
.utils
.DisplayUtils
;
52 import com
.owncloud
.android
.utils
.FileStorageUtils
;
56 * This Adapter populates a ListView with all files and folders in an ownCloud
59 * @author Bartek Przybylski
60 * @author Tobias Kaminsky
61 * @author David A. Velasco
63 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
64 private final static String PERMISSION_SHARED_WITH_ME
= "S";
66 private Context mContext
;
67 private OCFile mFile
= null
;
68 private Vector
<OCFile
> mFiles
= null
;
69 private boolean mJustFolders
;
71 private FileDataStorageManager mStorageManager
;
72 private Account mAccount
;
73 private ComponentsGetter mTransferServiceGetter
;
74 private Integer mSortOrder
;
75 public static final Integer SORT_NAME
= 0;
76 public static final Integer SORT_DATE
= 1;
77 public static final Integer SORT_SIZE
= 2;
78 private Boolean mSortAscending
;
79 private SharedPreferences mAppPreferences
;
81 public FileListListAdapter(
84 ComponentsGetter transferServiceGetter
87 mJustFolders
= justFolders
;
89 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
91 mTransferServiceGetter
= transferServiceGetter
;
93 mAppPreferences
= PreferenceManager
94 .getDefaultSharedPreferences(mContext
);
96 // Read sorting order, default to sort by name ascending
97 mSortOrder
= mAppPreferences
98 .getInt("sortOrder", 0);
99 mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
101 // initialise thumbnails cache on background thread
102 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
107 public boolean areAllItemsEnabled() {
112 public boolean isEnabled(int position
) {
117 public int getCount() {
118 return mFiles
!= null ? mFiles
.size() : 0;
122 public Object
getItem(int position
) {
123 if (mFiles
== null
|| mFiles
.size() <= position
)
125 return mFiles
.get(position
);
129 public long getItemId(int position
) {
130 if (mFiles
== null
|| mFiles
.size() <= position
)
132 return mFiles
.get(position
).getFileId();
136 public int getItemViewType(int position
) {
141 public View
getView(int position
, View convertView
, ViewGroup parent
) {
142 View view
= convertView
;
144 LayoutInflater inflator
= (LayoutInflater
) mContext
145 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
146 view
= inflator
.inflate(R
.layout
.list_item
, null
);
149 if (mFiles
!= null
&& mFiles
.size() > position
) {
150 OCFile file
= mFiles
.get(position
);
151 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
152 String name
= file
.getFileName();
154 fileName
.setText(name
);
155 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
156 fileIcon
.setTag(file
.getFileId());
157 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
158 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
159 sharedWithMeIconV
.setVisibility(View
.GONE
);
161 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
162 localStateView
.bringToFront();
163 FileDownloaderBinder downloaderBinder
=
164 mTransferServiceGetter
.getFileDownloaderBinder();
165 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
166 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
167 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
168 localStateView
.setVisibility(View
.VISIBLE
);
169 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
170 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
171 localStateView
.setVisibility(View
.VISIBLE
);
172 } else if (file
.isDown()) {
173 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
174 localStateView
.setVisibility(View
.VISIBLE
);
176 localStateView
.setVisibility(View
.INVISIBLE
);
179 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
180 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
181 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
183 if (!file
.isFolder()) {
184 fileSizeV
.setVisibility(View
.VISIBLE
);
185 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
186 lastModV
.setVisibility(View
.VISIBLE
);
187 lastModV
.setText(showRelativeTimestamp(file
));
188 // this if-else is needed even thoe fav icon is visible by default
189 // because android reuses views in listview
190 if (!file
.keepInSync()) {
191 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
193 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
196 ListView parentList
= (ListView
)parent
;
197 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
198 checkBoxV
.setVisibility(View
.GONE
);
200 if (parentList
.isItemChecked(position
)) {
201 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
203 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
205 checkBoxV
.setVisibility(View
.VISIBLE
);
208 // get Thumbnail if file is image
209 if (file
.isImage() && file
.getRemoteId() != null
){
210 // Thumbnail in Cache?
211 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
212 String
.valueOf(file
.getRemoteId())
214 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
215 fileIcon
.setImageBitmap(thumbnail
);
217 // generate new Thumbnail
218 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
219 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
220 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
221 fileIcon
, mStorageManager
, mAccount
223 if (thumbnail
== null
) {
224 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
226 final AsyncDrawable asyncDrawable
= new AsyncDrawable(
227 mContext
.getResources(),
231 fileIcon
.setImageDrawable(asyncDrawable
);
236 fileIcon
.setImageResource(
237 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
241 if (checkIfFileIsSharedWithMe(file
)) {
242 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
246 // TODO Re-enable when server supports folder-size calculation
247 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
248 // fileSizeV.setVisibility(View.VISIBLE);
249 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
251 fileSizeV
.setVisibility(View
.INVISIBLE
);
254 lastModV
.setVisibility(View
.VISIBLE
);
255 lastModV
.setText(showRelativeTimestamp(file
));
256 checkBoxV
.setVisibility(View
.GONE
);
257 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
259 if (checkIfFileIsSharedWithMe(file
)) {
260 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
261 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
263 fileIcon
.setImageResource(
264 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
268 // If folder is sharedByLink, icon folder must be changed to
270 if (file
.isShareByLink()) {
271 fileIcon
.setImageResource(R
.drawable
.folder_public
);
275 if (file
.isShareByLink()) {
276 sharedIconV
.setVisibility(View
.VISIBLE
);
278 sharedIconV
.setVisibility(View
.GONE
);
286 * Local Folder size in human readable format
290 * @return Size in human readable format
292 private String
getFolderSizeHuman(String path
) {
294 File dir
= new File(path
);
297 long bytes
= getFolderSize(dir
);
298 return DisplayUtils
.bytesToHumanReadable(bytes
);
307 * @return Size in bytes
309 private long getFolderSize(File dir
) {
312 File
[] fileList
= dir
.listFiles();
313 for(int i
= 0; i
< fileList
.length
; i
++) {
314 if(fileList
[i
].isDirectory()) {
315 result
+= getFolderSize(fileList
[i
]);
317 result
+= fileList
[i
].length();
326 public int getViewTypeCount() {
331 public boolean hasStableIds() {
336 public boolean isEmpty() {
337 return (mFiles
== null
|| mFiles
.isEmpty());
341 * Change the adapted directory for a new one
342 * @param directory New file to adapt. Can be NULL, meaning
343 * "no content to adapt".
344 * @param updatedStorageManager Optional updated storage manager; used to replace
345 * mStorageManager if is different (and not NULL)
347 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
349 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
350 mStorageManager
= updatedStorageManager
;
351 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
353 if (mStorageManager
!= null
) {
354 mFiles
= mStorageManager
.getFolderContent(mFile
);
356 mFiles
= getFolders(mFiles
);
366 * Sorts all filenames, regarding last user decision
368 private void sortDirectory(){
371 sortByName(mSortAscending
);
374 sortByDate(mSortAscending
);
377 sortBySize(mSortAscending
);
381 notifyDataSetChanged();
386 * Filter for getting only the folders
388 * @return Vector<OCFile>
390 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
391 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
392 OCFile current
= null
;
393 for (int i
=0; i
<files
.size(); i
++) {
394 current
= files
.get(i
);
395 if (current
.isFolder()) {
404 * Check if parent folder does not include 'S' permission and if file/folder
407 * @param file: OCFile
408 * @return boolean: True if it is shared with me and false if it is not
410 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
411 return (mFile
.getPermissions() != null
412 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
413 && file
.getPermissions() != null
414 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
419 * @param sortAscending true: ascending, false: descending
421 private void sortByDate(boolean sortAscending
){
429 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
430 public int compare(OCFile o1
, OCFile o2
) {
431 if (o1
.isFolder() && o2
.isFolder()) {
432 Long obj1
= o1
.getModificationTimestamp();
433 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
435 else if (o1
.isFolder()) {
437 } else if (o2
.isFolder()) {
439 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
442 Long obj1
= o1
.getModificationTimestamp();
443 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
451 * @param sortAscending true: ascending, false: descending
453 private void sortBySize(boolean sortAscending
){
461 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
462 public int compare(OCFile o1
, OCFile o2
) {
463 if (o1
.isFolder() && o2
.isFolder()) {
464 Long obj1
= getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o1
)));
465 return val
* obj1
.compareTo(getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o2
))));
467 else if (o1
.isFolder()) {
469 } else if (o2
.isFolder()) {
471 } else if (o1
.getFileLength() == 0 || o2
.getFileLength() == 0){
474 Long obj1
= o1
.getFileLength();
475 return val
* obj1
.compareTo(o2
.getFileLength());
483 * @param sortAscending true: ascending, false: descending
485 private void sortByName(boolean sortAscending
){
493 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
494 public int compare(OCFile o1
, OCFile o2
) {
495 if (o1
.isFolder() && o2
.isFolder()) {
496 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
497 } else if (o1
.isFolder()) {
499 } else if (o2
.isFolder()) {
502 return val
* new AlphanumComparator().compare(o1
, o2
);
507 public void setSortOrder(Integer order
, boolean ascending
) {
508 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
509 editor
.putInt("sortOrder", order
);
510 editor
.putBoolean("sortAscending", ascending
);
514 mSortAscending
= ascending
;
519 private CharSequence
showRelativeTimestamp(OCFile file
){
520 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
521 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);