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
.AlphanumComparator
;
38 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
39 import com
.owncloud
.android
.datamodel
.OCFile
;
40 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
41 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
42 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
43 import com
.owncloud
.android
.utils
.DisplayUtils
;
47 * This Adapter populates a ListView with all files and folders in an ownCloud
50 * @author Bartek Przybylski
53 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
54 private final static String PERMISSION_SHARED_WITH_ME
= "S";
56 private Context mContext
;
57 private OCFile mFile
= null
;
58 private Vector
<OCFile
> mFiles
= null
;
59 private boolean mJustFolders
;
61 private FileDataStorageManager mStorageManager
;
62 private Account mAccount
;
63 private ComponentsGetter mTransferServiceGetter
;
64 public enum sortOrders
{ NAME
, DATE
, SIZE
}
65 private sortOrders sort
= sortOrders
.NAME
;
66 private boolean sortAscending
= true
;
68 public FileListListAdapter(
71 ComponentsGetter transferServiceGetter
73 mJustFolders
= justFolders
;
75 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
76 mTransferServiceGetter
= transferServiceGetter
;
80 public boolean areAllItemsEnabled() {
85 public boolean isEnabled(int position
) {
90 public int getCount() {
91 return mFiles
!= null ? mFiles
.size() : 0;
95 public Object
getItem(int position
) {
96 if (mFiles
== null
|| mFiles
.size() <= position
)
98 return mFiles
.get(position
);
102 public long getItemId(int position
) {
103 if (mFiles
== null
|| mFiles
.size() <= position
)
105 return mFiles
.get(position
).getFileId();
109 public int getItemViewType(int position
) {
114 public View
getView(int position
, View convertView
, ViewGroup parent
) {
115 View view
= convertView
;
117 LayoutInflater inflator
= (LayoutInflater
) mContext
118 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
119 view
= inflator
.inflate(R
.layout
.list_item
, null
);
122 if (mFiles
!= null
&& mFiles
.size() > position
) {
123 OCFile file
= mFiles
.get(position
);
124 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
125 String name
= file
.getFileName();
127 fileName
.setText(name
);
128 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
129 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
130 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
131 sharedWithMeIconV
.setVisibility(View
.GONE
);
133 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
134 localStateView
.bringToFront();
135 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
136 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
137 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
138 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
139 localStateView
.setVisibility(View
.VISIBLE
);
140 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
141 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
142 localStateView
.setVisibility(View
.VISIBLE
);
143 } else if (file
.isDown()) {
144 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
145 localStateView
.setVisibility(View
.VISIBLE
);
147 localStateView
.setVisibility(View
.INVISIBLE
);
150 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
151 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
152 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
154 if (!file
.isFolder()) {
155 fileSizeV
.setVisibility(View
.VISIBLE
);
156 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
157 lastModV
.setVisibility(View
.VISIBLE
);
158 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
159 // this if-else is needed even thoe fav icon is visible by default
160 // because android reuses views in listview
161 if (!file
.keepInSync()) {
162 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
164 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
167 ListView parentList
= (ListView
)parent
;
168 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
169 checkBoxV
.setVisibility(View
.GONE
);
171 if (parentList
.isItemChecked(position
)) {
172 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
174 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
176 checkBoxV
.setVisibility(View
.VISIBLE
);
179 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
181 if (checkIfFileIsSharedWithMe(file
)) {
182 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
187 fileSizeV
.setVisibility(View
.INVISIBLE
);
188 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
189 lastModV
.setVisibility(View
.VISIBLE
);
190 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
191 checkBoxV
.setVisibility(View
.GONE
);
192 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
194 if (checkIfFileIsSharedWithMe(file
)) {
195 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
196 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
198 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
201 // If folder is sharedByLink, icon folder must be changed to
203 if (file
.isShareByLink()) {
204 fileIcon
.setImageResource(R
.drawable
.folder_public
);
208 if (file
.isShareByLink()) {
209 sharedIconV
.setVisibility(View
.VISIBLE
);
211 sharedIconV
.setVisibility(View
.GONE
);
219 public int getViewTypeCount() {
224 public boolean hasStableIds() {
229 public boolean isEmpty() {
230 return (mFiles
== null
|| mFiles
.isEmpty());
234 * Change the adapted directory for a new one
235 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
236 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
238 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
240 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
241 mStorageManager
= updatedStorageManager
;
242 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
244 if (mStorageManager
!= null
) {
245 mFiles
= mStorageManager
.getFolderContent(mFile
);
247 mFiles
= getFolders(mFiles
);
257 * Sorts all filenames, regarding last user decision
259 private void sortDirectory(){
262 sortByName(sortAscending
);
266 sortBySize(sortAscending
);
270 sortByDate(sortAscending
);
274 notifyDataSetChanged();
279 * Filter for getting only the folders
281 * @return Vector<OCFile>
283 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
284 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
285 OCFile current
= null
;
286 for (int i
=0; i
<files
.size(); i
++) {
287 current
= files
.get(i
);
288 if (current
.isFolder()) {
297 * Check if parent folder does not include 'S' permission and if file/folder
300 * @param file: OCFile
301 * @return boolean: True if it is shared with me and false if it is not
303 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
304 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
305 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
308 private void sortByDate(boolean sortAscending
){
316 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
317 public int compare(OCFile o1
, OCFile o2
) {
318 if (o1
.isFolder() && o2
.isFolder()) {
319 return val
* Long
.compare(o1
.getModificationTimestamp(), o2
.getModificationTimestamp());
321 else if (o1
.isFolder()) {
323 } else if (o2
.isFolder()) {
325 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
328 return val
* Long
.compare(o1
.getModificationTimestamp(), o2
.getModificationTimestamp());
334 private void sortBySize(boolean sortAscending
){
342 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
343 public int compare(OCFile o1
, OCFile o2
) {
344 if (o1
.isFolder() && o2
.isFolder()) {
345 return o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
347 else if (o1
.isFolder()) {
349 } else if (o2
.isFolder()) {
351 } else if (o1
.getFileLength() == 0 || o2
.getFileLength() == 0){
354 return val
* Long
.compare(o1
.getFileLength(), o2
.getFileLength());
360 private void sortByName(boolean sortAscending
){
368 Collections
.sort(mFiles
, new Comparator
<OCFile
>() {
369 public int compare(OCFile o1
, OCFile o2
) {
370 if (o1
.isFolder() && o2
.isFolder()) {
371 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
372 } else if (o1
.isFolder()) {
374 } else if (o2
.isFolder()) {
377 return val
* new AlphanumComparator().compare(o1
, o2
);
382 public void setSortOrder(sortOrders order
, boolean descending
) {
384 sortAscending
= descending
;