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
;
164 view
= inflator
.inflate(R
.layout
.grid_image
, null
);
167 view
= inflator
.inflate(R
.layout
.grid_item
, null
);
170 view
= inflator
.inflate(R
.layout
.list_item
, null
);
178 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.thumbnail
);
180 fileIcon
.setTag(file
.getFileId());
182 String name
= file
.getFileName();
184 LinearLayout linearLayout
= (LinearLayout
) view
.findViewById(R
.id
.ListItemLayout
);
185 linearLayout
.setContentDescription("LinearLayout-" + name
);
189 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
190 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
191 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
193 lastModV
.setVisibility(View
.VISIBLE
);
194 lastModV
.setText(showRelativeTimestamp(file
));
196 checkBoxV
.setVisibility(View
.GONE
);
198 fileSizeV
.setVisibility(View
.VISIBLE
);
199 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
201 if (!file
.isFolder()) {
202 AbsListView parentList
= (AbsListView
)parent
;
203 if (android
.os
.Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.HONEYCOMB
) {
204 if (parentList
.getChoiceMode() == AbsListView
.CHOICE_MODE_NONE
) {
205 checkBoxV
.setVisibility(View
.GONE
);
207 if (parentList
.isItemChecked(position
)) {
208 checkBoxV
.setImageResource(
209 android
.R
.drawable
.checkbox_on_background
);
211 checkBoxV
.setImageResource(
212 android
.R
.drawable
.checkbox_off_background
);
214 checkBoxV
.setVisibility(View
.VISIBLE
);
219 fileSizeV
.setVisibility(View
.INVISIBLE
);
224 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
225 name
= file
.getFileName();
226 fileName
.setText(name
);
230 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
231 if (file
.isShareByLink()) {
232 sharedIconV
.setVisibility(View
.VISIBLE
);
233 sharedIconV
.bringToFront();
235 sharedIconV
.setVisibility(View
.GONE
);
239 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.localFileIndicator
);
240 localStateView
.bringToFront();
241 FileDownloaderBinder downloaderBinder
=
242 mTransferServiceGetter
.getFileDownloaderBinder();
243 FileUploaderBinder uploaderBinder
=
244 mTransferServiceGetter
.getFileUploaderBinder();
245 boolean downloading
= (downloaderBinder
!= null
&&
246 downloaderBinder
.isDownloading(mAccount
, file
));
247 OperationsServiceBinder opsBinder
=
248 mTransferServiceGetter
.getOperationsServiceBinder();
249 downloading
|= (opsBinder
!= null
&&
250 opsBinder
.isSynchronizing(mAccount
, file
.getRemotePath()));
252 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
253 localStateView
.setVisibility(View
.VISIBLE
);
254 } else if (uploaderBinder
!= null
&&
255 uploaderBinder
.isUploading(mAccount
, file
)) {
256 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
257 localStateView
.setVisibility(View
.VISIBLE
);
258 } else if (file
.isDown()) {
259 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
260 localStateView
.setVisibility(View
.VISIBLE
);
262 localStateView
.setVisibility(View
.INVISIBLE
);
265 // share with me icon
266 if (!file
.isFolder()) {
267 ImageView sharedWithMeIconV
= (ImageView
)
268 view
.findViewById(R
.id
.sharedWithMeIcon
);
269 sharedWithMeIconV
.bringToFront();
270 if (checkIfFileIsSharedWithMe(file
)) {
271 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
273 sharedWithMeIconV
.setVisibility(View
.GONE
);
282 // this if-else is needed even though favorite icon is visible by default
283 // because android reuses views in listview
284 if (!file
.keepInSync()) {
285 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.GONE
);
287 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.VISIBLE
);
291 if (!file
.isFolder()) {
292 if (file
.isImage() && file
.getRemoteId() != null
){
293 // Thumbnail in Cache?
294 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
295 String
.valueOf(file
.getRemoteId())
297 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
298 fileIcon
.setImageBitmap(thumbnail
);
300 // generate new Thumbnail
301 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
302 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
303 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
304 fileIcon
, mStorageManager
, mAccount
306 if (thumbnail
== null
) {
307 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
309 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
310 new ThumbnailsCacheManager
.AsyncDrawable(
311 mContext
.getResources(),
315 fileIcon
.setImageDrawable(asyncDrawable
);
320 fileIcon
.setImageResource(DisplayUtils
.getFileTypeIconId(file
.getMimetype(),
321 file
.getFileName()));
326 if (checkIfFileIsSharedWithMe(file
)) {
327 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
328 } else if (file
.isShareByLink()) {
329 // If folder is sharedByLink, icon folder must be changed to
331 fileIcon
.setImageResource(R
.drawable
.folder_public
);
333 fileIcon
.setImageResource(
334 DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName())
344 * Local Folder size in human readable format
348 * @return Size in human readable format
350 private String
getFolderSizeHuman(String path
) {
352 File dir
= new File(path
);
355 long bytes
= FileStorageUtils
.getFolderSize(dir
);
356 return DisplayUtils
.bytesToHumanReadable(bytes
);
365 * @return Size in bytes
367 private long getFolderSize(File dir
) {
370 File
[] fileList
= dir
.listFiles();
371 for(int i
= 0; i
< fileList
.length
; i
++) {
372 if(fileList
[i
].isDirectory()) {
373 result
+= getFolderSize(fileList
[i
]);
375 result
+= fileList
[i
].length();
384 public int getViewTypeCount() {
389 public boolean hasStableIds() {
394 public boolean isEmpty() {
395 return (mFiles
== null
|| mFiles
.isEmpty());
399 * Change the adapted directory for a new one
400 * @param directory New file to adapt. Can be NULL, meaning
401 * "no content to adapt".
402 * @param updatedStorageManager Optional updated storage manager; used to replace
403 * mStorageManager if is different (and not NULL)
405 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
407 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
408 mStorageManager
= updatedStorageManager
;
409 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
411 if (mStorageManager
!= null
) {
412 mFiles
= mStorageManager
.getFolderContent(mFile
);
414 mFilesOrig
.addAll(mFiles
);
417 mFiles
= getFolders(mFiles
);
423 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
424 notifyDataSetChanged();
429 * Filter for getting only the folders
431 * @return Vector<OCFile>
433 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
434 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
435 OCFile current
= null
;
436 for (int i
=0; i
<files
.size(); i
++) {
437 current
= files
.get(i
);
438 if (current
.isFolder()) {
447 * Check if parent folder does not include 'S' permission and if file/folder
450 * @param file: OCFile
451 * @return boolean: True if it is shared with me and false if it is not
453 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
454 return (mFile
.getPermissions() != null
455 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
456 && file
.getPermissions() != null
457 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
460 public void setSortOrder(Integer order
, boolean ascending
) {
461 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
462 editor
.putInt("sortOrder", order
);
463 editor
.putBoolean("sortAscending", ascending
);
466 FileStorageUtils
.mSortOrder
= order
;
467 FileStorageUtils
.mSortAscending
= ascending
;
470 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
471 notifyDataSetChanged();
475 private CharSequence
showRelativeTimestamp(OCFile file
){
476 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
477 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);
480 public void setGridMode(boolean gridMode
) {
481 mGridMode
= gridMode
;