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
;
22 import java
.util
.Collections
;
23 import java
.util
.Comparator
;
24 import java
.util
.Vector
;
26 import third_parties
.daveKoeller
.AlphanumComparator
;
27 import android
.accounts
.Account
;
28 import android
.content
.Context
;
29 import android
.content
.SharedPreferences
;
30 import android
.graphics
.Bitmap
;
31 import android
.preference
.PreferenceManager
;
32 import android
.text
.format
.DateUtils
;
33 import android
.view
.LayoutInflater
;
34 import android
.view
.View
;
35 import android
.view
.ViewGroup
;
36 import android
.widget
.BaseAdapter
;
37 import android
.widget
.ImageView
;
38 import android
.widget
.ListAdapter
;
39 import android
.widget
.ListView
;
40 import android
.widget
.TextView
;
42 import com
.owncloud
.android
.R
;
43 import com
.owncloud
.android
.authentication
.AccountUtils
;
44 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
45 import com
.owncloud
.android
.datamodel
.OCFile
;
46 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
47 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
48 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
49 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
50 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
51 import com
.owncloud
.android
.utils
.DisplayUtils
;
52 import com
.owncloud
.android
.utils
.FileStorageUtils
;
56 * This Adapter populates a ListView with all files and folders in an ownCloud
59 * @author Bartek Przybylski
60 * @author Tobias Kaminsky
61 * @author David A. Velasco
63 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
64 private final static String PERMISSION_SHARED_WITH_ME
= "S";
66 private Context mContext
;
67 private OCFile mFile
= null
;
68 private Vector
<OCFile
> mFiles
= null
;
69 private boolean mJustFolders
;
71 private FileDataStorageManager mStorageManager
;
72 private Account mAccount
;
73 private ComponentsGetter mTransferServiceGetter
;
75 private SharedPreferences mAppPreferences
;
77 public FileListListAdapter(
80 ComponentsGetter transferServiceGetter
83 mJustFolders
= justFolders
;
85 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
87 mTransferServiceGetter
= transferServiceGetter
;
89 mAppPreferences
= PreferenceManager
90 .getDefaultSharedPreferences(mContext
);
92 // Read sorting order, default to sort by name ascending
93 FileStorageUtils
.mSortOrder
= mAppPreferences
.getInt("sortOrder", 0);
94 FileStorageUtils
.mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
97 // initialise thumbnails cache on background thread
98 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
103 public boolean areAllItemsEnabled() {
108 public boolean isEnabled(int position
) {
113 public int getCount() {
114 return mFiles
!= null ? mFiles
.size() : 0;
118 public Object
getItem(int position
) {
119 if (mFiles
== null
|| mFiles
.size() <= position
)
121 return mFiles
.get(position
);
125 public long getItemId(int position
) {
126 if (mFiles
== null
|| mFiles
.size() <= position
)
128 return mFiles
.get(position
).getFileId();
132 public int getItemViewType(int position
) {
137 public View
getView(int position
, View convertView
, ViewGroup parent
) {
138 View view
= convertView
;
140 LayoutInflater inflator
= (LayoutInflater
) mContext
141 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
142 view
= inflator
.inflate(R
.layout
.list_item
, null
);
145 if (mFiles
!= null
&& mFiles
.size() > position
) {
146 OCFile file
= mFiles
.get(position
);
147 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
148 String name
= file
.getFileName();
150 fileName
.setText(name
);
151 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
152 fileIcon
.setTag(file
.getFileId());
153 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
154 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
155 sharedWithMeIconV
.setVisibility(View
.GONE
);
157 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
158 localStateView
.bringToFront();
159 FileDownloaderBinder downloaderBinder
=
160 mTransferServiceGetter
.getFileDownloaderBinder();
161 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
162 OperationsServiceBinder opsBinder
= mTransferServiceGetter
.getOperationsServiceBinder();
163 if ((downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) ||
164 (file
.isFolder() && opsBinder
!= null
&& opsBinder
.isSynchronizing(mAccount
, file
.getRemotePath()))) {
165 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
166 localStateView
.setVisibility(View
.VISIBLE
);
167 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
168 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
169 localStateView
.setVisibility(View
.VISIBLE
);
170 } else if (file
.isDown()) {
171 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
172 localStateView
.setVisibility(View
.VISIBLE
);
174 localStateView
.setVisibility(View
.INVISIBLE
);
177 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
178 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
179 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
181 if (!file
.isFolder()) {
182 fileSizeV
.setVisibility(View
.VISIBLE
);
183 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
184 lastModV
.setVisibility(View
.VISIBLE
);
185 lastModV
.setText(showRelativeTimestamp(file
));
186 // this if-else is needed even thoe fav icon is visible by default
187 // because android reuses views in listview
188 if (!file
.keepInSync()) {
189 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
191 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
194 ListView parentList
= (ListView
)parent
;
195 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
196 checkBoxV
.setVisibility(View
.GONE
);
198 if (parentList
.isItemChecked(position
)) {
199 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
201 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
203 checkBoxV
.setVisibility(View
.VISIBLE
);
206 // get Thumbnail if file is image
207 if (file
.isImage() && file
.getRemoteId() != null
){
208 // Thumbnail in Cache?
209 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
210 String
.valueOf(file
.getRemoteId())
212 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
213 fileIcon
.setImageBitmap(thumbnail
);
216 // generate new Thumbnail
217 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
218 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
219 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
220 fileIcon
, mStorageManager
, mAccount
222 if (thumbnail
== null
) {
223 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
225 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
226 new ThumbnailsCacheManager
.AsyncDrawable(
227 mContext
.getResources(),
231 fileIcon
.setImageDrawable(asyncDrawable
);
236 fileIcon
.setImageResource(DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName()));
239 if (checkIfFileIsSharedWithMe(file
)) {
240 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
244 // TODO Re-enable when server supports folder-size calculation
245 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
246 // fileSizeV.setVisibility(View.VISIBLE);
247 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
249 fileSizeV
.setVisibility(View
.INVISIBLE
);
252 lastModV
.setVisibility(View
.VISIBLE
);
253 lastModV
.setText(showRelativeTimestamp(file
));
254 checkBoxV
.setVisibility(View
.GONE
);
255 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
257 if (checkIfFileIsSharedWithMe(file
)) {
258 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
259 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
261 fileIcon
.setImageResource(
262 DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName())
266 // If folder is sharedByLink, icon folder must be changed to
268 if (file
.isShareByLink()) {
269 fileIcon
.setImageResource(R
.drawable
.folder_public
);
273 if (file
.isShareByLink()) {
274 sharedIconV
.setVisibility(View
.VISIBLE
);
276 sharedIconV
.setVisibility(View
.GONE
);
284 * Local Folder size in human readable format
288 * @return Size in human readable format
290 private String
getFolderSizeHuman(String path
) {
292 File dir
= new File(path
);
295 long bytes
= FileStorageUtils
.getFolderSize(dir
);
296 return DisplayUtils
.bytesToHumanReadable(bytes
);
305 * @return Size in bytes
307 private long getFolderSize(File dir
) {
310 File
[] fileList
= dir
.listFiles();
311 for(int i
= 0; i
< fileList
.length
; i
++) {
312 if(fileList
[i
].isDirectory()) {
313 result
+= getFolderSize(fileList
[i
]);
315 result
+= fileList
[i
].length();
324 public int getViewTypeCount() {
329 public boolean hasStableIds() {
334 public boolean isEmpty() {
335 return (mFiles
== null
|| mFiles
.isEmpty());
339 * Change the adapted directory for a new one
340 * @param directory New file to adapt. Can be NULL, meaning
341 * "no content to adapt".
342 * @param updatedStorageManager Optional updated storage manager; used to replace
343 * mStorageManager if is different (and not NULL)
345 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
347 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
348 mStorageManager
= updatedStorageManager
;
349 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
351 if (mStorageManager
!= null
) {
352 mFiles
= mStorageManager
.getFolderContent(mFile
);
354 mFiles
= getFolders(mFiles
);
360 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
361 notifyDataSetChanged();
366 * Filter for getting only the folders
368 * @return Vector<OCFile>
370 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
371 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
372 OCFile current
= null
;
373 for (int i
=0; i
<files
.size(); i
++) {
374 current
= files
.get(i
);
375 if (current
.isFolder()) {
384 * Check if parent folder does not include 'S' permission and if file/folder
387 * @param file: OCFile
388 * @return boolean: True if it is shared with me and false if it is not
390 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
391 return (mFile
.getPermissions() != null
392 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
393 && file
.getPermissions() != null
394 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
397 public void setSortOrder(Integer order
, boolean ascending
) {
398 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
399 editor
.putInt("sortOrder", order
);
400 editor
.putBoolean("sortAscending", ascending
);
403 FileStorageUtils
.mSortOrder
= order
;
404 FileStorageUtils
.mSortAscending
= ascending
;
407 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
408 notifyDataSetChanged();
412 private CharSequence
showRelativeTimestamp(OCFile file
){
413 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
414 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);