1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2015 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
.Vector
;
24 import android
.accounts
.Account
;
25 import android
.content
.Context
;
26 import android
.content
.SharedPreferences
;
27 import android
.graphics
.Bitmap
;
28 import android
.preference
.PreferenceManager
;
29 import android
.text
.format
.DateUtils
;
30 import android
.view
.LayoutInflater
;
31 import android
.view
.View
;
32 import android
.view
.ViewGroup
;
33 import android
.widget
.BaseAdapter
;
34 import android
.widget
.GridView
;
35 import android
.widget
.ImageView
;
36 import android
.widget
.ListAdapter
;
37 import android
.widget
.TextView
;
39 import com
.owncloud
.android
.R
;
40 import com
.owncloud
.android
.authentication
.AccountUtils
;
41 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
42 import com
.owncloud
.android
.datamodel
.OCFile
;
43 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
44 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
45 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
46 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
47 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
48 import com
.owncloud
.android
.utils
.DisplayUtils
;
49 import com
.owncloud
.android
.utils
.FileStorageUtils
;
53 * This Adapter populates a ListView with all files and folders in an ownCloud
56 * @author Bartek Przybylski
57 * @author Tobias Kaminsky
58 * @author David A. Velasco
60 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
61 private final static String PERMISSION_SHARED_WITH_ME
= "S";
63 private Context mContext
;
64 private OCFile mFile
= null
;
65 private Vector
<OCFile
> mFiles
= null
;
66 private Vector
<OCFile
> mFilesOrig
= new Vector
<OCFile
>();
67 private boolean mJustFolders
;
69 private FileDataStorageManager mStorageManager
;
70 private Account mAccount
;
71 private ComponentsGetter mTransferServiceGetter
;
73 private enum ViewType
{LIST_ITEM
, GRID_IMAGE
, GRID_ITEM
};
75 private SharedPreferences mAppPreferences
;
77 public FileListListAdapter(
80 ComponentsGetter transferServiceGetter
83 mJustFolders
= justFolders
;
85 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
86 mTransferServiceGetter
= transferServiceGetter
;
88 mAppPreferences
= PreferenceManager
89 .getDefaultSharedPreferences(mContext
);
91 // Read sorting order, default to sort by name ascending
92 FileStorageUtils
.mSortOrder
= mAppPreferences
.getInt("sortOrder", 0);
93 FileStorageUtils
.mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
96 // initialise thumbnails cache on background thread
97 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
101 public boolean areAllItemsEnabled() {
106 public boolean isEnabled(int position
) {
111 public int getCount() {
112 return mFiles
!= null ? mFiles
.size() : 0;
116 public Object
getItem(int position
) {
117 if (mFiles
== null
|| mFiles
.size() <= position
)
119 return mFiles
.get(position
);
123 public long getItemId(int position
) {
124 if (mFiles
== null
|| mFiles
.size() <= position
)
126 return mFiles
.get(position
).getFileId();
130 public int getItemViewType(int position
) {
135 public View
getView(int position
, View convertView
, ViewGroup parent
) {
137 boolean fileView
= DisplayUtils
.decideViewLayout(mFiles
);
139 View view
= convertView
;
141 LayoutInflater inflator
= (LayoutInflater
) mContext
142 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
144 if (mFiles
!= null
&& mFiles
.size() > position
) {
145 file
= mFiles
.get(position
);
148 // Find out which layout should be displayed
151 viewType
= ViewType
.LIST_ITEM
;
152 } else if (file
.isImage()){
153 viewType
= ViewType
.GRID_IMAGE
;
155 viewType
= ViewType
.GRID_ITEM
;
161 view
= inflator
.inflate(R
.layout
.grid_image
, null
);
164 view
= inflator
.inflate(R
.layout
.grid_item
, null
);
167 view
= inflator
.inflate(R
.layout
.list_item
, null
);
175 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.thumbnail
);
176 fileIcon
.setTag(file
.getFileId());
182 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
183 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
184 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
186 lastModV
.setVisibility(View
.VISIBLE
);
187 lastModV
.setText(showRelativeTimestamp(file
));
189 checkBoxV
.setVisibility(View
.GONE
);
191 fileSizeV
.setVisibility(View
.VISIBLE
);
192 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
194 if (!file
.isFolder()) {
195 GridView parentList
= (GridView
)parent
;
196 if (parentList
.getChoiceMode() == GridView
.CHOICE_MODE_NONE
) {
197 checkBoxV
.setVisibility(View
.GONE
);
199 if (parentList
.isItemChecked(position
)) {
200 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
202 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
204 checkBoxV
.setVisibility(View
.VISIBLE
);
208 fileSizeV
.setVisibility(View
.INVISIBLE
);
213 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
214 name
= file
.getFileName();
215 fileName
.setText(name
);
219 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
220 if (file
.isShareByLink()) {
221 sharedIconV
.setVisibility(View
.VISIBLE
);
222 sharedIconV
.bringToFront();
224 sharedIconV
.setVisibility(View
.GONE
);
228 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.localFileIndicator
);
229 localStateView
.bringToFront();
230 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
231 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
232 boolean downloading
= (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
));
233 OperationsServiceBinder opsBinder
= mTransferServiceGetter
.getOperationsServiceBinder();
234 downloading
|= (opsBinder
!= null
&& opsBinder
.isSynchronizing(mAccount
, file
.getRemotePath()));
236 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
237 localStateView
.setVisibility(View
.VISIBLE
);
238 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
239 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
240 localStateView
.setVisibility(View
.VISIBLE
);
241 } else if (file
.isDown()) {
242 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
243 localStateView
.setVisibility(View
.VISIBLE
);
245 localStateView
.setVisibility(View
.INVISIBLE
);
248 // share with me icon
249 if (!file
.isFolder()) {
250 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
251 sharedWithMeIconV
.bringToFront();
252 if (checkIfFileIsSharedWithMe(file
)) {
253 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
255 sharedWithMeIconV
.setVisibility(View
.GONE
);
264 // this if-else is needed even though favorite icon is visible by default
265 // because android reuses views in listview
266 if (!file
.keepInSync()) {
267 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.GONE
);
269 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.VISIBLE
);
273 if (!file
.isFolder()) {
274 if (file
.isImage() && file
.getRemoteId() != null
){
275 // Thumbnail in Cache?
276 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
277 String
.valueOf(file
.getRemoteId())
279 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
280 fileIcon
.setImageBitmap(thumbnail
);
282 // generate new Thumbnail
283 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
284 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
285 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
286 fileIcon
, mStorageManager
, mAccount
288 if (thumbnail
== null
) {
289 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
291 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
292 new ThumbnailsCacheManager
.AsyncDrawable(
293 mContext
.getResources(),
297 fileIcon
.setImageDrawable(asyncDrawable
);
302 fileIcon
.setImageResource(DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName()));
306 if (checkIfFileIsSharedWithMe(file
)) {
307 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
308 } else if (file
.isShareByLink()) {
309 // If folder is sharedByLink, icon folder must be changed to
311 fileIcon
.setImageResource(R
.drawable
.folder_public
);
313 fileIcon
.setImageResource(
314 DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName())
324 * Local Folder size in human readable format
328 * @return Size in human readable format
330 private String
getFolderSizeHuman(String path
) {
332 File dir
= new File(path
);
335 long bytes
= FileStorageUtils
.getFolderSize(dir
);
336 return DisplayUtils
.bytesToHumanReadable(bytes
);
345 * @return Size in bytes
347 private long getFolderSize(File dir
) {
350 File
[] fileList
= dir
.listFiles();
351 for(int i
= 0; i
< fileList
.length
; i
++) {
352 if(fileList
[i
].isDirectory()) {
353 result
+= getFolderSize(fileList
[i
]);
355 result
+= fileList
[i
].length();
364 public int getViewTypeCount() {
369 public boolean hasStableIds() {
374 public boolean isEmpty() {
375 return (mFiles
== null
|| mFiles
.isEmpty());
379 * Change the adapted directory for a new one
380 * @param directory New file to adapt. Can be NULL, meaning
381 * "no content to adapt".
382 * @param updatedStorageManager Optional updated storage manager; used to replace
383 * mStorageManager if is different (and not NULL)
385 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
387 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
388 mStorageManager
= updatedStorageManager
;
389 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
391 if (mStorageManager
!= null
) {
392 mFiles
= mStorageManager
.getFolderContent(mFile
);
394 mFilesOrig
.addAll(mFiles
);
397 mFiles
= getFolders(mFiles
);
403 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
404 notifyDataSetChanged();
409 * Filter for getting only the folders
411 * @return Vector<OCFile>
413 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
414 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
415 OCFile current
= null
;
416 for (int i
=0; i
<files
.size(); i
++) {
417 current
= files
.get(i
);
418 if (current
.isFolder()) {
427 * Check if parent folder does not include 'S' permission and if file/folder
430 * @param file: OCFile
431 * @return boolean: True if it is shared with me and false if it is not
433 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
434 return (mFile
.getPermissions() != null
435 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
436 && file
.getPermissions() != null
437 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
440 public void setSortOrder(Integer order
, boolean ascending
) {
441 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
442 editor
.putInt("sortOrder", order
);
443 editor
.putBoolean("sortAscending", ascending
);
446 FileStorageUtils
.mSortOrder
= order
;
447 FileStorageUtils
.mSortAscending
= ascending
;
450 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
451 notifyDataSetChanged();
455 private CharSequence
showRelativeTimestamp(OCFile file
){
456 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
457 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);