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 android
.accounts
.Account
;
26 import android
.content
.Context
;
27 import android
.content
.SharedPreferences
;
28 import android
.preference
.PreferenceManager
;
29 import android
.view
.LayoutInflater
;
30 import android
.view
.View
;
31 import android
.view
.ViewGroup
;
32 import android
.widget
.BaseAdapter
;
33 import android
.widget
.ImageView
;
34 import android
.widget
.ListAdapter
;
35 import android
.widget
.ListView
;
36 import android
.widget
.TextView
;
38 import com
.owncloud
.android
.R
;
39 import com
.owncloud
.android
.authentication
.AccountUtils
;
40 import com
.owncloud
.android
.datamodel
.AlphanumComparator
;
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
;
48 import com
.owncloud
.android
.utils
.Log_OC
;
52 * This Adapter populates a ListView with all files and folders in an ownCloud
55 * @author Bartek Przybylski
58 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
59 private final static String PERMISSION_SHARED_WITH_ME
= "S";
61 private Context mContext
;
62 private OCFile mFile
= null
;
63 private Vector
<OCFile
> mFiles
= null
;
64 private boolean mJustFolders
;
66 private FileDataStorageManager mStorageManager
;
67 private Account mAccount
;
68 private ComponentsGetter mTransferServiceGetter
;
69 private String sortOrder
;
70 private Boolean sortAscending
;
71 private SharedPreferences appPreferences
;
73 public FileListListAdapter(
76 ComponentsGetter transferServiceGetter
78 mJustFolders
= justFolders
;
80 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
81 mTransferServiceGetter
= transferServiceGetter
;
83 appPreferences
= PreferenceManager
84 .getDefaultSharedPreferences(mContext
);
86 // Read sorting order, default to sort by name ascending
87 sortOrder
= appPreferences
88 .getString("sortOrder", "name");
89 sortAscending
= appPreferences
.getBoolean("sortAscending", true
);
94 public boolean areAllItemsEnabled() {
99 public boolean isEnabled(int position
) {
104 public int getCount() {
105 return mFiles
!= null ? mFiles
.size() : 0;
109 public Object
getItem(int position
) {
110 if (mFiles
== null
|| mFiles
.size() <= position
)
112 return mFiles
.get(position
);
116 public long getItemId(int position
) {
117 if (mFiles
== null
|| mFiles
.size() <= position
)
119 return mFiles
.get(position
).getFileId();
123 public int getItemViewType(int position
) {
128 public View
getView(int position
, View convertView
, ViewGroup parent
) {
129 View view
= convertView
;
131 LayoutInflater inflator
= (LayoutInflater
) mContext
132 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
133 view
= inflator
.inflate(R
.layout
.list_item
, null
);
136 if (mFiles
!= null
&& mFiles
.size() > position
) {
137 OCFile file
= mFiles
.get(position
);
138 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
139 String name
= file
.getFileName();
141 fileName
.setText(name
);
142 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
143 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
144 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
145 sharedWithMeIconV
.setVisibility(View
.GONE
);
147 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
148 localStateView
.bringToFront();
149 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
150 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
151 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
152 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
153 localStateView
.setVisibility(View
.VISIBLE
);
154 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
155 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
156 localStateView
.setVisibility(View
.VISIBLE
);
157 } else if (file
.isDown()) {
158 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
159 localStateView
.setVisibility(View
.VISIBLE
);
161 localStateView
.setVisibility(View
.INVISIBLE
);
164 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
165 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
166 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
168 if (!file
.isFolder()) {
169 fileSizeV
.setVisibility(View
.VISIBLE
);
170 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
171 lastModV
.setVisibility(View
.VISIBLE
);
172 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
173 // this if-else is needed even thoe fav icon is visible by default
174 // because android reuses views in listview
175 if (!file
.keepInSync()) {
176 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
178 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
181 ListView parentList
= (ListView
)parent
;
182 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
183 checkBoxV
.setVisibility(View
.GONE
);
185 if (parentList
.isItemChecked(position
)) {
186 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
188 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
190 checkBoxV
.setVisibility(View
.VISIBLE
);
193 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
195 if (checkIfFileIsSharedWithMe(file
)) {
196 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
200 if (FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
) != null
){
201 fileSizeV
.setVisibility(View
.VISIBLE
);
202 fileSizeV
.setText(getFolderSizeHuman(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
)));
204 fileSizeV
.setVisibility(View
.INVISIBLE
);
207 lastModV
.setVisibility(View
.VISIBLE
);
208 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
209 checkBoxV
.setVisibility(View
.GONE
);
210 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
212 if (checkIfFileIsSharedWithMe(file
)) {
213 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
214 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
216 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
219 // If folder is sharedByLink, icon folder must be changed to
221 if (file
.isShareByLink()) {
222 fileIcon
.setImageResource(R
.drawable
.folder_public
);
226 if (file
.isShareByLink()) {
227 sharedIconV
.setVisibility(View
.VISIBLE
);
229 sharedIconV
.setVisibility(View
.GONE
);
237 * 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 if (bytes
< 1024) return bytes
+ " B";
248 int exp
= (int) (Math
.log(bytes
) / Math
.log(1024));
249 String pre
= ("KMGTPE").charAt(exp
-1) + "";
251 return String
.format("%.1f %sB", bytes
/ Math
.pow(1024, exp
), pre
);
260 * @return Size in bytes
262 private long getFolderSize(File dir
) {
265 File
[] fileList
= dir
.listFiles();
266 for(int i
= 0; i
< fileList
.length
; i
++) {
267 if(fileList
[i
].isDirectory()) {
268 result
+= getFolderSize(fileList
[i
]);
270 result
+= fileList
[i
].length();
279 public int getViewTypeCount() {
284 public boolean hasStableIds() {
289 public boolean isEmpty() {
290 return (mFiles
== null
|| mFiles
.isEmpty());
294 * Change the adapted directory for a new one
295 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
296 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
298 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
300 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
301 mStorageManager
= updatedStorageManager
;
302 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
304 if (mStorageManager
!= null
) {
305 mFiles
= mStorageManager
.getFolderContent(mFile
);
307 mFiles
= getFolders(mFiles
);
317 * Sorts all filenames, regarding last user decision
319 private void sortDirectory(){
320 if (sortOrder
.equals("name")){
321 sortByName(sortAscending
);
322 } else if (sortOrder
.equals("size")){
323 sortBySize(sortAscending
);
324 } else if (sortOrder
.equals("date")){
325 sortByDate(sortAscending
);
328 notifyDataSetChanged();
333 * Filter for getting only the folders
335 * @return Vector<OCFile>
337 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
338 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
339 OCFile current
= null
;
340 for (int i
=0; i
<files
.size(); i
++) {
341 current
= files
.get(i
);
342 if (current
.isFolder()) {
351 * Check if parent folder does not include 'S' permission and if file/folder
354 * @param file: OCFile
355 * @return boolean: True if it is shared with me and false if it is not
357 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
358 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
359 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
364 * @param sortAscending true: ascending, false: descending
366 private void sortByDate(boolean sortAscending
){
374 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
375 public int compare(OCFile o1
, OCFile o2
) {
376 if (o1
.isFolder() && o2
.isFolder()) {
377 return val
* Long
.compare(o1
.getModificationTimestamp(), o2
.getModificationTimestamp());
379 else if (o1
.isFolder()) {
381 } else if (o2
.isFolder()) {
383 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
386 return val
* Long
.compare(o1
.getModificationTimestamp(), o2
.getModificationTimestamp());
394 * @param sortAscending true: ascending, false: descending
396 private void sortBySize(boolean sortAscending
){
404 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
405 public int compare(OCFile o1
, OCFile o2
) {
406 if (o1
.isFolder() && o2
.isFolder()) {
407 return val
* Long
.compare(getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o1
))),
408 getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o2
))));
410 else if (o1
.isFolder()) {
412 } else if (o2
.isFolder()) {
414 } else if (o1
.getFileLength() == 0 || o2
.getFileLength() == 0){
417 return val
* Long
.compare(o1
.getFileLength(), o2
.getFileLength());
425 * @param sortAscending true: ascending, false: descending
427 private void sortByName(boolean sortAscending
){
435 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
436 public int compare(OCFile o1
, OCFile o2
) {
437 if (o1
.isFolder() && o2
.isFolder()) {
438 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
439 } else if (o1
.isFolder()) {
441 } else if (o2
.isFolder()) {
444 return val
* new AlphanumComparator().compare(o1
, o2
);
449 public void setSortOrder(String order
, boolean ascending
) {
450 SharedPreferences
.Editor editor
= appPreferences
.edit();
451 editor
.putString("sortOrder", order
);
452 editor
.putBoolean("sortAscending", ascending
);
456 sortAscending
= ascending
;