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
.view
.LayoutInflater
;
28 import android
.view
.View
;
29 import android
.view
.ViewGroup
;
30 import android
.widget
.BaseAdapter
;
31 import android
.widget
.ImageView
;
32 import android
.widget
.ListAdapter
;
33 import android
.widget
.ListView
;
34 import android
.widget
.TextView
;
36 import com
.owncloud
.android
.R
;
37 import com
.owncloud
.android
.authentication
.AccountUtils
;
38 import com
.owncloud
.android
.datamodel
.AlphanumComparator
;
39 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
40 import com
.owncloud
.android
.datamodel
.OCFile
;
41 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
42 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
43 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
44 import com
.owncloud
.android
.utils
.DisplayUtils
;
45 import com
.owncloud
.android
.utils
.FileStorageUtils
;
46 import com
.owncloud
.android
.utils
.Log_OC
;
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 Context mContext
;
60 private OCFile mFile
= null
;
61 private Vector
<OCFile
> mFiles
= null
;
62 private boolean mJustFolders
;
64 private FileDataStorageManager mStorageManager
;
65 private Account mAccount
;
66 private ComponentsGetter mTransferServiceGetter
;
67 public enum sortOrders
{ NAME
, DATE
, SIZE
}
68 private sortOrders sort
= sortOrders
.NAME
;
69 private boolean sortAscending
= true
;
71 public FileListListAdapter(
74 ComponentsGetter transferServiceGetter
76 mJustFolders
= justFolders
;
78 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
79 mTransferServiceGetter
= transferServiceGetter
;
83 public boolean areAllItemsEnabled() {
88 public boolean isEnabled(int position
) {
93 public int getCount() {
94 return mFiles
!= null ? mFiles
.size() : 0;
98 public Object
getItem(int position
) {
99 if (mFiles
== null
|| mFiles
.size() <= position
)
101 return mFiles
.get(position
);
105 public long getItemId(int position
) {
106 if (mFiles
== null
|| mFiles
.size() <= position
)
108 return mFiles
.get(position
).getFileId();
112 public int getItemViewType(int position
) {
117 public View
getView(int position
, View convertView
, ViewGroup parent
) {
118 View view
= convertView
;
120 LayoutInflater inflator
= (LayoutInflater
) mContext
121 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
122 view
= inflator
.inflate(R
.layout
.list_item
, null
);
125 if (mFiles
!= null
&& mFiles
.size() > position
) {
126 OCFile file
= mFiles
.get(position
);
127 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
128 String name
= file
.getFileName();
130 fileName
.setText(name
);
131 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
132 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
133 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
134 sharedWithMeIconV
.setVisibility(View
.GONE
);
136 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
137 localStateView
.bringToFront();
138 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
139 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
140 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
141 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
142 localStateView
.setVisibility(View
.VISIBLE
);
143 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
144 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
145 localStateView
.setVisibility(View
.VISIBLE
);
146 } else if (file
.isDown()) {
147 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
148 localStateView
.setVisibility(View
.VISIBLE
);
150 localStateView
.setVisibility(View
.INVISIBLE
);
153 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
154 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
155 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
157 if (!file
.isFolder()) {
158 fileSizeV
.setVisibility(View
.VISIBLE
);
159 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
160 lastModV
.setVisibility(View
.VISIBLE
);
161 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
162 // this if-else is needed even thoe fav icon is visible by default
163 // because android reuses views in listview
164 if (!file
.keepInSync()) {
165 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
167 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
170 ListView parentList
= (ListView
)parent
;
171 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
172 checkBoxV
.setVisibility(View
.GONE
);
174 if (parentList
.isItemChecked(position
)) {
175 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
177 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
179 checkBoxV
.setVisibility(View
.VISIBLE
);
182 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
184 if (checkIfFileIsSharedWithMe(file
)) {
185 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
189 if (FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
) != null
){
190 fileSizeV
.setVisibility(View
.VISIBLE
);
191 fileSizeV
.setText(getFolderSizeHuman(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
)));
193 fileSizeV
.setVisibility(View
.INVISIBLE
);
196 lastModV
.setVisibility(View
.VISIBLE
);
197 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
198 checkBoxV
.setVisibility(View
.GONE
);
199 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
201 if (checkIfFileIsSharedWithMe(file
)) {
202 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
203 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
205 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
208 // If folder is sharedByLink, icon folder must be changed to
210 if (file
.isShareByLink()) {
211 fileIcon
.setImageResource(R
.drawable
.folder_public
);
215 if (file
.isShareByLink()) {
216 sharedIconV
.setVisibility(View
.VISIBLE
);
218 sharedIconV
.setVisibility(View
.GONE
);
226 * Local Folder size in human readable format
228 * @return Size in human readable format
230 private String
getFolderSizeHuman(String path
) {
232 File dir
= new File(path
);
235 long bytes
= getFolderSize(dir
);
236 if (bytes
< 1024) return bytes
+ " B";
237 int exp
= (int) (Math
.log(bytes
) / Math
.log(1024));
238 String pre
= ("KMGTPE").charAt(exp
-1) + "";
240 return String
.format("%.1f %sB", bytes
/ Math
.pow(1024, exp
), pre
);
249 * @return Size in bytes
251 private long getFolderSize(File dir
) {
254 File
[] fileList
= dir
.listFiles();
255 for(int i
= 0; i
< fileList
.length
; i
++) {
256 if(fileList
[i
].isDirectory()) {
257 result
+= getFolderSize(fileList
[i
]);
259 result
+= fileList
[i
].length();
268 public int getViewTypeCount() {
273 public boolean hasStableIds() {
278 public boolean isEmpty() {
279 return (mFiles
== null
|| mFiles
.isEmpty());
283 * Change the adapted directory for a new one
284 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
285 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
287 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
289 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
290 mStorageManager
= updatedStorageManager
;
291 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
293 if (mStorageManager
!= null
) {
294 mFiles
= mStorageManager
.getFolderContent(mFile
);
296 mFiles
= getFolders(mFiles
);
306 * Sorts all filenames, regarding last user decision
308 private void sortDirectory(){
311 sortByName(sortAscending
);
315 sortBySize(sortAscending
);
319 sortByDate(sortAscending
);
323 notifyDataSetChanged();
328 * Filter for getting only the folders
330 * @return Vector<OCFile>
332 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
333 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
334 OCFile current
= null
;
335 for (int i
=0; i
<files
.size(); i
++) {
336 current
= files
.get(i
);
337 if (current
.isFolder()) {
346 * Check if parent folder does not include 'S' permission and if file/folder
349 * @param file: OCFile
350 * @return boolean: True if it is shared with me and false if it is not
352 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
353 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
354 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
359 * @param sortAscending true: ascending, false: descending
361 private void sortByDate(boolean sortAscending
){
369 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
370 public int compare(OCFile o1
, OCFile o2
) {
371 if (o1
.isFolder() && o2
.isFolder()) {
372 return val
* Long
.compare(o1
.getModificationTimestamp(), o2
.getModificationTimestamp());
374 else if (o1
.isFolder()) {
376 } else if (o2
.isFolder()) {
378 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
381 return val
* Long
.compare(o1
.getModificationTimestamp(), o2
.getModificationTimestamp());
389 * @param sortAscending true: ascending, false: descending
391 private void sortBySize(boolean sortAscending
){
399 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
400 public int compare(OCFile o1
, OCFile o2
) {
401 if (o1
.isFolder() && o2
.isFolder()) {
402 return val
* Long
.compare(getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o1
))),
403 getFolderSize(new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, o2
))));
405 else if (o1
.isFolder()) {
407 } else if (o2
.isFolder()) {
409 } else if (o1
.getFileLength() == 0 || o2
.getFileLength() == 0){
412 return val
* Long
.compare(o1
.getFileLength(), o2
.getFileLength());
420 * @param sortAscending true: ascending, false: descending
422 private void sortByName(boolean sortAscending
){
430 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
431 public int compare(OCFile o1
, OCFile o2
) {
432 if (o1
.isFolder() && o2
.isFolder()) {
433 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
434 } else if (o1
.isFolder()) {
436 } else if (o2
.isFolder()) {
439 return val
* new AlphanumComparator().compare(o1
, o2
);
444 public void setSortOrder(sortOrders order
, boolean descending
) {
446 sortAscending
= descending
;