2 * ownCloud Android client application
4 * @author Bartek Przybylski
5 * @author Tobias Kaminsky
6 * @author David A. Velasco
7 * Copyright (C) 2011 Bartek Przybylski
8 * Copyright (C) 2015 ownCloud Inc.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package com
.owncloud
.android
.ui
.adapter
;
27 import java
.util
.Vector
;
29 import android
.accounts
.Account
;
30 import android
.content
.Context
;
31 import android
.content
.SharedPreferences
;
32 import android
.graphics
.Bitmap
;
33 import android
.os
.Build
;
34 import android
.preference
.PreferenceManager
;
35 import android
.text
.format
.DateUtils
;
36 import android
.view
.LayoutInflater
;
37 import android
.view
.View
;
38 import android
.view
.ViewGroup
;
39 import android
.widget
.AbsListView
;
40 import android
.widget
.BaseAdapter
;
41 import android
.widget
.ImageView
;
42 import android
.widget
.LinearLayout
;
43 import android
.widget
.ListAdapter
;
44 import android
.widget
.TextView
;
46 import com
.owncloud
.android
.R
;
47 import com
.owncloud
.android
.authentication
.AccountUtils
;
48 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
49 import com
.owncloud
.android
.datamodel
.OCFile
;
50 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
51 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
52 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
53 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
54 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
55 import com
.owncloud
.android
.utils
.DisplayUtils
;
56 import com
.owncloud
.android
.utils
.FileStorageUtils
;
60 * This Adapter populates a ListView with all files and folders in an ownCloud
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 Vector
<OCFile
> mFilesOrig
= new Vector
<OCFile
>();
70 private boolean mJustFolders
;
72 private FileDataStorageManager mStorageManager
;
73 private Account mAccount
;
74 private ComponentsGetter mTransferServiceGetter
;
75 private boolean mGridMode
;
77 private enum ViewType
{LIST_ITEM
, GRID_IMAGE
, GRID_ITEM
};
79 private SharedPreferences mAppPreferences
;
81 public FileListListAdapter(
84 ComponentsGetter transferServiceGetter
87 mJustFolders
= justFolders
;
89 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
90 mTransferServiceGetter
= transferServiceGetter
;
92 mAppPreferences
= PreferenceManager
93 .getDefaultSharedPreferences(mContext
);
95 // Read sorting order, default to sort by name ascending
96 FileStorageUtils
.mSortOrder
= mAppPreferences
.getInt("sortOrder", 0);
97 FileStorageUtils
.mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
99 // initialise thumbnails cache on background thread
100 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
106 public boolean areAllItemsEnabled() {
111 public boolean isEnabled(int position
) {
116 public int getCount() {
117 return mFiles
!= null ? mFiles
.size() : 0;
121 public Object
getItem(int position
) {
122 if (mFiles
== null
|| mFiles
.size() <= position
)
124 return mFiles
.get(position
);
128 public long getItemId(int position
) {
129 if (mFiles
== null
|| mFiles
.size() <= position
)
131 return mFiles
.get(position
).getFileId();
135 public int getItemViewType(int position
) {
140 public View
getView(int position
, View convertView
, ViewGroup parent
) {
142 View view
= convertView
;
144 LayoutInflater inflator
= (LayoutInflater
) mContext
145 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
147 if (mFiles
!= null
&& mFiles
.size() > position
) {
148 file
= mFiles
.get(position
);
151 // Find out which layout should be displayed
154 viewType
= ViewType
.LIST_ITEM
;
155 } else if (file
.isImage()){
156 viewType
= ViewType
.GRID_IMAGE
;
158 viewType
= ViewType
.GRID_ITEM
;
161 // create view only if differs, otherwise reuse
162 if (convertView
== null
|| (convertView
!= null
&& convertView
.getTag() != viewType
)) {
165 view
= inflator
.inflate(R
.layout
.grid_image
, null
);
166 view
.setTag(ViewType
.GRID_IMAGE
);
169 view
= inflator
.inflate(R
.layout
.grid_item
, null
);
170 view
.setTag(ViewType
.GRID_ITEM
);
173 view
= inflator
.inflate(R
.layout
.list_item
, null
);
174 view
.setTag(ViewType
.LIST_ITEM
);
183 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.thumbnail
);
185 fileIcon
.setTag(file
.getFileId());
187 String name
= file
.getFileName();
189 LinearLayout linearLayout
= (LinearLayout
) view
.findViewById(R
.id
.ListItemLayout
);
190 linearLayout
.setContentDescription("LinearLayout-" + name
);
194 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
195 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
196 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
198 lastModV
.setVisibility(View
.VISIBLE
);
199 lastModV
.setText(showRelativeTimestamp(file
));
201 checkBoxV
.setVisibility(View
.GONE
);
203 fileSizeV
.setVisibility(View
.VISIBLE
);
204 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
206 if (!file
.isFolder()) {
207 AbsListView parentList
= (AbsListView
)parent
;
208 if (android
.os
.Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.HONEYCOMB
) {
209 if (parentList
.getChoiceMode() == AbsListView
.CHOICE_MODE_NONE
) {
210 checkBoxV
.setVisibility(View
.GONE
);
212 if (parentList
.isItemChecked(position
)) {
213 checkBoxV
.setImageResource(
214 R
.drawable
.ic_checkbox_marked
);
216 checkBoxV
.setImageResource(
217 R
.drawable
.ic_checkbox_blank_outline
);
219 checkBoxV
.setVisibility(View
.VISIBLE
);
224 fileSizeV
.setVisibility(View
.INVISIBLE
);
229 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
230 name
= file
.getFileName();
231 fileName
.setText(name
);
235 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
236 if (file
.isShareByLink()) {
237 sharedIconV
.setVisibility(View
.VISIBLE
);
238 sharedIconV
.bringToFront();
240 sharedIconV
.setVisibility(View
.GONE
);
244 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.localFileIndicator
);
245 localStateView
.bringToFront();
246 FileDownloaderBinder downloaderBinder
=
247 mTransferServiceGetter
.getFileDownloaderBinder();
248 FileUploaderBinder uploaderBinder
=
249 mTransferServiceGetter
.getFileUploaderBinder();
250 boolean downloading
= (downloaderBinder
!= null
&&
251 downloaderBinder
.isDownloading(mAccount
, file
));
252 OperationsServiceBinder opsBinder
=
253 mTransferServiceGetter
.getOperationsServiceBinder();
254 downloading
|= (opsBinder
!= null
&&
255 opsBinder
.isSynchronizing(mAccount
, file
.getRemotePath()));
257 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
258 localStateView
.setVisibility(View
.VISIBLE
);
259 } else if (uploaderBinder
!= null
&&
260 uploaderBinder
.isUploading(mAccount
, file
)) {
261 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
262 localStateView
.setVisibility(View
.VISIBLE
);
263 } else if (file
.isDown()) {
264 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
265 localStateView
.setVisibility(View
.VISIBLE
);
267 localStateView
.setVisibility(View
.INVISIBLE
);
270 // share with me icon
271 ImageView sharedWithMeIconV
= (ImageView
)
272 view
.findViewById(R
.id
.sharedWithMeIcon
);
273 sharedWithMeIconV
.bringToFront();
274 if (checkIfFileIsSharedWithMe(file
) &&
275 (!file
.isFolder() || !mGridMode
)) {
276 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
278 sharedWithMeIconV
.setVisibility(View
.GONE
);
286 // this if-else is needed even though favorite icon is visible by default
287 // because android reuses views in listview
288 if (!file
.isFavorite()) {
289 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.GONE
);
291 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.VISIBLE
);
295 if (!file
.isFolder()) {
296 if (file
.isImage() && file
.getRemoteId() != null
){
297 // Thumbnail in Cache?
298 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
299 String
.valueOf(file
.getRemoteId())
301 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
302 fileIcon
.setImageBitmap(thumbnail
);
304 // generate new Thumbnail
305 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
306 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
307 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
308 fileIcon
, mStorageManager
, mAccount
310 if (thumbnail
== null
) {
311 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
313 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
314 new ThumbnailsCacheManager
.AsyncDrawable(
315 mContext
.getResources(),
319 fileIcon
.setImageDrawable(asyncDrawable
);
324 fileIcon
.setImageResource(DisplayUtils
.getFileTypeIconId(file
.getMimetype(),
325 file
.getFileName()));
330 if (checkIfFileIsSharedWithMe(file
)) {
331 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
332 } else if (file
.isShareByLink()) {
333 // If folder is sharedByLink, icon folder must be changed to
335 fileIcon
.setImageResource(R
.drawable
.folder_public
);
337 fileIcon
.setImageResource(
338 DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName())
348 * Local Folder size in human readable format
352 * @return Size in human readable format
354 private String
getFolderSizeHuman(String path
) {
356 File dir
= new File(path
);
359 long bytes
= FileStorageUtils
.getFolderSize(dir
);
360 return DisplayUtils
.bytesToHumanReadable(bytes
);
369 * @return Size in bytes
371 private long getFolderSize(File dir
) {
374 File
[] fileList
= dir
.listFiles();
375 for(int i
= 0; i
< fileList
.length
; i
++) {
376 if(fileList
[i
].isDirectory()) {
377 result
+= getFolderSize(fileList
[i
]);
379 result
+= fileList
[i
].length();
388 public int getViewTypeCount() {
393 public boolean hasStableIds() {
398 public boolean isEmpty() {
399 return (mFiles
== null
|| mFiles
.isEmpty());
403 * Change the adapted directory for a new one
404 * @param directory New file to adapt. Can be NULL, meaning
405 * "no content to adapt".
406 * @param updatedStorageManager Optional updated storage manager; used to replace
407 * mStorageManager if is different (and not NULL)
409 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
410 /*, boolean onlyOnDevice*/) {
412 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
413 mStorageManager
= updatedStorageManager
;
414 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
416 if (mStorageManager
!= null
) {
417 // TODO Enable when "On Device" is recovered ?
418 mFiles
= mStorageManager
.getFolderContent(mFile
/*, onlyOnDevice*/);
420 mFilesOrig
.addAll(mFiles
);
423 mFiles
= getFolders(mFiles
);
429 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
430 notifyDataSetChanged();
435 * Filter for getting only the folders
437 * @return Vector<OCFile>
439 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
440 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
441 OCFile current
= null
;
442 for (int i
=0; i
<files
.size(); i
++) {
443 current
= files
.get(i
);
444 if (current
.isFolder()) {
453 * Check if parent folder does not include 'S' permission and if file/folder
456 * @param file: OCFile
457 * @return boolean: True if it is shared with me and false if it is not
459 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
460 return (mFile
.getPermissions() != null
461 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
462 && file
.getPermissions() != null
463 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
466 public void setSortOrder(Integer order
, boolean ascending
) {
467 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
468 editor
.putInt("sortOrder", order
);
469 editor
.putBoolean("sortAscending", ascending
);
472 FileStorageUtils
.mSortOrder
= order
;
473 FileStorageUtils
.mSortAscending
= ascending
;
476 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
477 notifyDataSetChanged();
481 private CharSequence
showRelativeTimestamp(OCFile file
){
482 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
483 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);
486 public void setGridMode(boolean gridMode
) {
487 mGridMode
= gridMode
;