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
;
20 import java
.util
.Collections
;
21 import java
.util
.Comparator
;
22 import java
.util
.Vector
;
24 import android
.accounts
.Account
;
25 import android
.content
.Context
;
26 import android
.view
.LayoutInflater
;
27 import android
.view
.View
;
28 import android
.view
.ViewGroup
;
29 import android
.widget
.BaseAdapter
;
30 import android
.widget
.ImageView
;
31 import android
.widget
.ListAdapter
;
32 import android
.widget
.ListView
;
33 import android
.widget
.TextView
;
35 import com
.owncloud
.android
.R
;
36 import com
.owncloud
.android
.authentication
.AccountUtils
;
37 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
38 import com
.owncloud
.android
.datamodel
.OCFile
;
39 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
40 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
41 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
42 import com
.owncloud
.android
.utils
.DisplayUtils
;
46 * This Adapter populates a ListView with all files and folders in an ownCloud
49 * @author Bartek Przybylski
52 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
53 private final static String PERMISSION_SHARED_WITH_ME
= "S";
55 private Context mContext
;
56 private OCFile mFile
= null
;
57 private Vector
<OCFile
> mFiles
= null
;
58 private boolean mJustFolders
;
60 private FileDataStorageManager mStorageManager
;
61 private Account mAccount
;
62 private ComponentsGetter mTransferServiceGetter
;
63 public enum sortOrders
{ NAME
, DATE
, SIZE
}
64 private sortOrders sort
= sortOrders
.NAME
;
65 private boolean sortAscending
= true
;
67 public FileListListAdapter(
70 ComponentsGetter transferServiceGetter
72 mJustFolders
= justFolders
;
74 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
75 mTransferServiceGetter
= transferServiceGetter
;
79 public boolean areAllItemsEnabled() {
84 public boolean isEnabled(int position
) {
89 public int getCount() {
90 return mFiles
!= null ? mFiles
.size() : 0;
94 public Object
getItem(int position
) {
95 if (mFiles
== null
|| mFiles
.size() <= position
)
97 return mFiles
.get(position
);
101 public long getItemId(int position
) {
102 if (mFiles
== null
|| mFiles
.size() <= position
)
104 return mFiles
.get(position
).getFileId();
108 public int getItemViewType(int position
) {
113 public View
getView(int position
, View convertView
, ViewGroup parent
) {
114 View view
= convertView
;
116 LayoutInflater inflator
= (LayoutInflater
) mContext
117 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
118 view
= inflator
.inflate(R
.layout
.list_item
, null
);
121 if (mFiles
!= null
&& mFiles
.size() > position
) {
122 OCFile file
= mFiles
.get(position
);
123 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
124 String name
= file
.getFileName();
126 fileName
.setText(name
);
127 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
128 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
129 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
130 sharedWithMeIconV
.setVisibility(View
.GONE
);
132 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
133 localStateView
.bringToFront();
134 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
135 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
136 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
137 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
138 localStateView
.setVisibility(View
.VISIBLE
);
139 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
140 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
141 localStateView
.setVisibility(View
.VISIBLE
);
142 } else if (file
.isDown()) {
143 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
144 localStateView
.setVisibility(View
.VISIBLE
);
146 localStateView
.setVisibility(View
.INVISIBLE
);
149 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
150 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
151 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
153 if (!file
.isFolder()) {
154 fileSizeV
.setVisibility(View
.VISIBLE
);
155 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
156 lastModV
.setVisibility(View
.VISIBLE
);
157 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
158 // this if-else is needed even thoe fav icon is visible by default
159 // because android reuses views in listview
160 if (!file
.keepInSync()) {
161 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
163 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
166 ListView parentList
= (ListView
)parent
;
167 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
168 checkBoxV
.setVisibility(View
.GONE
);
170 if (parentList
.isItemChecked(position
)) {
171 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
173 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
175 checkBoxV
.setVisibility(View
.VISIBLE
);
178 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
180 if (checkIfFileIsSharedWithMe(file
)) {
181 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
186 fileSizeV
.setVisibility(View
.INVISIBLE
);
187 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
188 lastModV
.setVisibility(View
.VISIBLE
);
189 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
190 checkBoxV
.setVisibility(View
.GONE
);
191 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
193 if (checkIfFileIsSharedWithMe(file
)) {
194 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
195 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
197 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
200 // If folder is sharedByLink, icon folder must be changed to
202 if (file
.isShareByLink()) {
203 fileIcon
.setImageResource(R
.drawable
.folder_public
);
207 if (file
.isShareByLink()) {
208 sharedIconV
.setVisibility(View
.VISIBLE
);
210 sharedIconV
.setVisibility(View
.GONE
);
218 public int getViewTypeCount() {
223 public boolean hasStableIds() {
228 public boolean isEmpty() {
229 return (mFiles
== null
|| mFiles
.isEmpty());
233 * Change the adapted directory for a new one
234 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
235 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
237 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
239 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
240 mStorageManager
= updatedStorageManager
;
241 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
243 if (mStorageManager
!= null
) {
244 mFiles
= mStorageManager
.getFolderContent(mFile
);
246 mFiles
= getFolders(mFiles
);
256 * Sorts all filenames, regarding last user decision
258 private void sortDirectory(){
274 Collections
.reverse(mFiles
);
277 notifyDataSetChanged();
282 * Filter for getting only the folders
284 * @return Vector<OCFile>
286 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
287 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
288 OCFile current
= null
;
289 for (int i
=0; i
<files
.size(); i
++) {
290 current
= files
.get(i
);
291 if (current
.isFolder()) {
300 * Check if parent folder does not include 'S' permission and if file/folder
303 * @param file: OCFile
304 * @return boolean: True if it is shared with me and false if it is not
306 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
307 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
308 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
311 private void sortByDate(){
312 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
313 public int compare(OCFile o1
, OCFile o2
) {
314 if (o1
.isFolder() && o2
.isFolder()) {
315 return Long
.compare(o1
.getModificationTimestamp(), o2
.getModificationTimestamp());
317 else if (o1
.isFolder()) {
319 } else if (o2
.isFolder()) {
321 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
324 return Long
.compare(o1
.getModificationTimestamp(), o2
.getModificationTimestamp());
330 private void sortBySize(){
331 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
332 public int compare(OCFile o1
, OCFile o2
) {
333 if (o1
.isFolder() && o2
.isFolder()) {
334 return o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
336 else if (o1
.isFolder()) {
338 } else if (o2
.isFolder()) {
340 } else if (o1
.getFileLength() == 0 || o2
.getFileLength() == 0){
343 return Long
.compare(o1
.getFileLength(), o2
.getFileLength());
349 private void sortByName(){
350 Collections
.sort(mFiles
);
353 public void setSortOrder(sortOrders order
, boolean descending
) {
355 sortAscending
= descending
;