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
;
21 import java
.util
.Collections
;
22 import java
.util
.Comparator
;
23 import java
.util
.Vector
;
25 import third_parties
.daveKoeller
.AlphanumComparator
;
26 import android
.accounts
.Account
;
27 import android
.content
.Context
;
28 import android
.content
.SharedPreferences
;
29 import android
.preference
.PreferenceManager
;
30 import android
.view
.LayoutInflater
;
31 import android
.view
.View
;
32 import android
.view
.ViewGroup
;
33 import android
.widget
.BaseAdapter
;
34 import android
.widget
.ImageView
;
35 import android
.widget
.ListAdapter
;
36 import android
.widget
.ListView
;
37 import android
.widget
.TextView
;
39 import com
.owncloud
.android
.R
;
40 import com
.owncloud
.android
.authentication
.AccountUtils
;
41 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
42 import com
.owncloud
.android
.datamodel
.OCFile
;
43 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
44 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
45 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
46 import com
.owncloud
.android
.utils
.DisplayUtils
;
47 import com
.owncloud
.android
.utils
.FileStorageUtils
;
50 * This Adapter populates a ListView with all files and folders in an ownCloud
53 * @author Bartek Przybylski
56 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
57 private final static String PERMISSION_SHARED_WITH_ME
= "S";
59 private final Context mContext
;
60 private OCFile mFile
= null
;
61 private Vector
<OCFile
> mFiles
= null
;
62 private final boolean mJustFolders
;
64 private FileDataStorageManager mStorageManager
;
65 private Account mAccount
;
66 private final ComponentsGetter mTransferServiceGetter
;
67 private Integer mSortOrder
;
68 public static final Integer mSortName
= 0;
69 public static final Integer mSortDate
= 1;
70 public static final Integer mSortSize
= 2;
71 private Boolean mSortAscending
;
72 private final SharedPreferences mAppPreferences
;
74 public FileListListAdapter(boolean justFolders
, Context context
, ComponentsGetter transferServiceGetter
) {
75 mJustFolders
= justFolders
;
77 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
78 mTransferServiceGetter
= transferServiceGetter
;
80 mAppPreferences
= PreferenceManager
.getDefaultSharedPreferences(mContext
);
82 // Read sorting order, default to sort by name ascending
83 mSortOrder
= mAppPreferences
.getInt("sortOrder", mSortName
);
84 mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
89 public boolean areAllItemsEnabled() {
94 public boolean isEnabled(int position
) {
99 public int getCount() {
100 return mFiles
!= null ? mFiles
.size() : 0;
104 public Object
getItem(int position
) {
105 if (mFiles
== null
|| mFiles
.size() <= position
)
107 return mFiles
.get(position
);
111 public long getItemId(int position
) {
112 if (mFiles
== null
|| mFiles
.size() <= position
)
114 return mFiles
.get(position
).getFileId();
118 public int getItemViewType(int position
) {
123 public View
getView(int position
, View convertView
, ViewGroup parent
) {
124 View view
= convertView
;
126 LayoutInflater inflator
= (LayoutInflater
) mContext
.getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
127 view
= inflator
.inflate(R
.layout
.list_item
, null
);
130 if (mFiles
!= null
&& mFiles
.size() > position
) {
131 OCFile file
= mFiles
.get(position
);
132 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
133 String name
= file
.getFileName();
135 fileName
.setText(name
);
136 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
137 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
138 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
139 sharedWithMeIconV
.setVisibility(View
.GONE
);
141 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
142 localStateView
.bringToFront();
143 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
144 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
145 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
146 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
147 localStateView
.setVisibility(View
.VISIBLE
);
148 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
149 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
150 localStateView
.setVisibility(View
.VISIBLE
);
151 } else if (file
.isDown()) {
152 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
153 localStateView
.setVisibility(View
.VISIBLE
);
155 localStateView
.setVisibility(View
.INVISIBLE
);
158 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
159 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
160 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
162 if (!file
.isFolder()) {
163 fileSizeV
.setVisibility(View
.VISIBLE
);
164 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
165 lastModV
.setVisibility(View
.VISIBLE
);
166 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
167 // this if-else is needed even thoe fav icon is visible by
169 // because android reuses views in listview
170 if (!file
.keepInSync()) {
171 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
173 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
176 ListView parentList
= (ListView
) parent
;
177 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
178 checkBoxV
.setVisibility(View
.GONE
);
180 if (parentList
.isItemChecked(position
)) {
181 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
183 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
185 checkBoxV
.setVisibility(View
.VISIBLE
);
188 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
190 if (checkIfFileIsSharedWithMe(file
)) {
191 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
194 // TODO Re-enable when server supports folder-size calculation
195 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name,
197 // fileSizeV.setVisibility(View.VISIBLE);
199 // .setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name,
202 fileSizeV
.setVisibility(View
.INVISIBLE
);
205 lastModV
.setVisibility(View
.VISIBLE
);
206 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
207 checkBoxV
.setVisibility(View
.GONE
);
208 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
210 if (checkIfFileIsSharedWithMe(file
)) {
211 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
212 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
214 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
217 // If folder is sharedByLink, icon folder must be changed to
219 if (file
.isShareByLink()) {
220 fileIcon
.setImageResource(R
.drawable
.folder_public
);
224 if (file
.isShareByLink()) {
225 sharedIconV
.setVisibility(View
.VISIBLE
);
227 sharedIconV
.setVisibility(View
.GONE
);
235 * Local Folder size in human readable format
239 * @return Size in human readable format
241 private String
getFolderSizeHuman(String path
) {
243 File dir
= new File(path
);
246 long bytes
= getFolderSize(dir
);
247 return DisplayUtils
.bytesToHumanReadable(bytes
);
258 * @return Size in bytes
260 private long getFolderSize(File dir
) {
263 File
[] fileList
= dir
.listFiles();
264 for (int i
= 0; i
< fileList
.length
; i
++) {
265 if (fileList
[i
].isDirectory()) {
266 result
+= getFolderSize(fileList
[i
]);
268 result
+= fileList
[i
].length();
277 public int getViewTypeCount() {
282 public boolean hasStableIds() {
287 public boolean isEmpty() {
288 return (mFiles
== null
|| mFiles
.isEmpty());
292 * Change the adapted directory for a new one
295 * New file to adapt. Can be NULL, meaning "no content to adapt".
296 * @param updatedStorageManager
297 * Optional updated storage manager; used to replace
298 * mStorageManager if is different (and not NULL)
300 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
302 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
303 mStorageManager
= updatedStorageManager
;
304 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
306 if (mStorageManager
!= null
) {
307 mFiles
= mStorageManager
.getFolderContent(mFile
);
309 mFiles
= getFolders(mFiles
);
319 * Sorts all filenames, regarding last user decision
321 private void sortDirectory() {
322 switch (mSortOrder
) {
324 sortByName(mSortAscending
);
327 sortByDate(mSortAscending
);
330 sortBySize(mSortAscending
);
334 notifyDataSetChanged();
338 * Filter for getting only the folders
341 * @return Vector<OCFile>
343 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
344 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
345 OCFile current
= null
;
346 for (int i
= 0; i
< files
.size(); i
++) {
347 current
= files
.get(i
);
348 if (current
.isFolder()) {
356 * Check if parent folder does not include 'S' permission and if file/folder
361 * @return boolean: True if it is shared with me and false if it is not
363 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
364 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
365 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
371 * @param sortAscending
372 * true: ascending, false: descending
374 private void sortByDate(boolean sortAscending
) {
382 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
384 public int compare(OCFile o1
, OCFile o2
) {
385 if (o1
.isFolder() && o2
.isFolder()) {
386 return val
* Long
.compare(o1
.getModificationTimestamp(), o2
.getModificationTimestamp());
387 } else if (o1
.isFolder()) {
389 } else if (o2
.isFolder()) {
391 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0) {
394 return val
* Long
.compare(o1
.getModificationTimestamp(), o2
.getModificationTimestamp());
403 * @param sortAscending
404 * true: ascending, false: descending
406 private void sortBySize(boolean sortAscending
) {
414 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
416 public int compare(OCFile o1
, OCFile o2
) {
417 if (o1
.isFolder() && o2
.isFolder()) {
420 getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o1
))),
421 getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o2
))));
422 } else if (o1
.isFolder()) {
424 } else if (o2
.isFolder()) {
426 } else if (o1
.getFileLength() == 0 || o2
.getFileLength() == 0) {
429 return val
* Long
.compare(o1
.getFileLength(), o2
.getFileLength());
438 * @param sortAscending
439 * true: ascending, false: descending
441 private void sortByName(boolean sortAscending
) {
449 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
451 public int compare(OCFile o1
, OCFile o2
) {
452 if (o1
.isFolder() && o2
.isFolder()) {
453 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
454 } else if (o1
.isFolder()) {
456 } else if (o2
.isFolder()) {
459 return val
* new AlphanumComparator().compare(o1
, o2
);
464 public void setSortOrder(Integer order
, boolean ascending
) {
465 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
466 editor
.putInt("sortOrder", order
);
467 editor
.putBoolean("sortAscending", ascending
);
471 mSortAscending
= ascending
;