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
.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
.ImageView
;
35 import android
.widget
.ListAdapter
;
36 import android
.widget
.ListView
;
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
.ui
.activity
.ComponentsGetter
;
47 import com
.owncloud
.android
.utils
.DisplayUtils
;
48 import com
.owncloud
.android
.utils
.FileStorageUtils
;
52 * This Adapter populates a ListView with all files and folders in an ownCloud
55 * @author Bartek Przybylski
56 * @author Tobias Kaminsky
57 * @author David A. Velasco
59 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
60 private final static String PERMISSION_SHARED_WITH_ME
= "S";
62 private Context mContext
;
63 private OCFile mFile
= null
;
64 private Vector
<OCFile
> mFiles
= null
;
65 private boolean mJustFolders
;
67 private FileDataStorageManager mStorageManager
;
68 private Account mAccount
;
69 private ComponentsGetter mTransferServiceGetter
;
71 private SharedPreferences mAppPreferences
;
73 public FileListListAdapter(
76 ComponentsGetter transferServiceGetter
79 mJustFolders
= justFolders
;
81 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
83 mTransferServiceGetter
= transferServiceGetter
;
85 mAppPreferences
= PreferenceManager
86 .getDefaultSharedPreferences(mContext
);
88 // Read sorting order, default to sort by name ascending
89 FileStorageUtils
.mSortOrder
= mAppPreferences
.getInt("sortOrder", 0);
90 FileStorageUtils
.mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
93 // initialise thumbnails cache on background thread
94 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
99 public boolean areAllItemsEnabled() {
104 public boolean isEnabled(int position
) {
109 public int getCount() {
110 return mFiles
!= null ? mFiles
.size() : 0;
114 public Object
getItem(int position
) {
115 if (mFiles
== null
|| mFiles
.size() <= position
)
117 return mFiles
.get(position
);
121 public long getItemId(int position
) {
122 if (mFiles
== null
|| mFiles
.size() <= position
)
124 return mFiles
.get(position
).getFileId();
128 public int getItemViewType(int position
) {
133 public View
getView(int position
, View convertView
, ViewGroup parent
) {
134 View view
= convertView
;
136 LayoutInflater inflator
= (LayoutInflater
) mContext
137 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
138 view
= inflator
.inflate(R
.layout
.list_item
, null
);
141 if (mFiles
!= null
&& mFiles
.size() > position
) {
142 OCFile file
= mFiles
.get(position
);
143 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
144 String name
= file
.getFileName();
146 fileName
.setText(name
);
147 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
148 fileIcon
.setTag(file
.getFileId());
149 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
150 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
151 sharedWithMeIconV
.setVisibility(View
.GONE
);
153 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
154 localStateView
.bringToFront();
155 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
156 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
157 //if (file.isSynchronizing() || file.isDownloading()) {
158 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
159 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
160 localStateView
.setVisibility(View
.VISIBLE
);
161 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
162 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
163 localStateView
.setVisibility(View
.VISIBLE
);
164 } else if (file
.isDown()) {
165 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
166 localStateView
.setVisibility(View
.VISIBLE
);
168 localStateView
.setVisibility(View
.INVISIBLE
);
171 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
172 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
173 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
175 if (!file
.isFolder()) {
176 fileSizeV
.setVisibility(View
.VISIBLE
);
177 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
178 lastModV
.setVisibility(View
.VISIBLE
);
179 lastModV
.setText(showRelativeTimestamp(file
));
180 // this if-else is needed even thoe fav icon is visible by default
181 // because android reuses views in listview
182 if (!file
.keepInSync()) {
183 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
185 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
188 ListView parentList
= (ListView
)parent
;
189 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
190 checkBoxV
.setVisibility(View
.GONE
);
192 if (parentList
.isItemChecked(position
)) {
193 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
195 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
197 checkBoxV
.setVisibility(View
.VISIBLE
);
200 // get Thumbnail if file is image
201 if (file
.isImage() && file
.getRemoteId() != null
){
202 // Thumbnail in Cache?
203 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
204 String
.valueOf(file
.getRemoteId())
206 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
207 fileIcon
.setImageBitmap(thumbnail
);
210 // generate new Thumbnail
211 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
212 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
213 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
214 fileIcon
, mStorageManager
, mAccount
216 if (thumbnail
== null
) {
217 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
219 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
220 new ThumbnailsCacheManager
.AsyncDrawable(
221 mContext
.getResources(),
225 fileIcon
.setImageDrawable(asyncDrawable
);
230 fileIcon
.setImageResource(DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName()));
233 if (checkIfFileIsSharedWithMe(file
)) {
234 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
238 // TODO Re-enable when server supports folder-size calculation
239 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
240 // fileSizeV.setVisibility(View.VISIBLE);
241 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
243 fileSizeV
.setVisibility(View
.INVISIBLE
);
246 lastModV
.setVisibility(View
.VISIBLE
);
247 lastModV
.setText(showRelativeTimestamp(file
));
248 checkBoxV
.setVisibility(View
.GONE
);
249 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
251 if (checkIfFileIsSharedWithMe(file
)) {
252 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
253 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
255 fileIcon
.setImageResource(
256 DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName())
260 // If folder is sharedByLink, icon folder must be changed to
262 if (file
.isShareByLink()) {
263 fileIcon
.setImageResource(R
.drawable
.folder_public
);
267 if (file
.isShareByLink()) {
268 sharedIconV
.setVisibility(View
.VISIBLE
);
270 sharedIconV
.setVisibility(View
.GONE
);
278 * Local Folder size in human readable format
282 * @return Size in human readable format
284 private String
getFolderSizeHuman(String path
) {
286 File dir
= new File(path
);
289 long bytes
= FileStorageUtils
.getFolderSize(dir
);
290 return DisplayUtils
.bytesToHumanReadable(bytes
);
299 * @return Size in bytes
301 private long getFolderSize(File dir
) {
304 File
[] fileList
= dir
.listFiles();
305 for(int i
= 0; i
< fileList
.length
; i
++) {
306 if(fileList
[i
].isDirectory()) {
307 result
+= getFolderSize(fileList
[i
]);
309 result
+= fileList
[i
].length();
318 public int getViewTypeCount() {
323 public boolean hasStableIds() {
328 public boolean isEmpty() {
329 return (mFiles
== null
|| mFiles
.isEmpty());
333 * Change the adapted directory for a new one
334 * @param directory New file to adapt. Can be NULL, meaning
335 * "no content to adapt".
336 * @param updatedStorageManager Optional updated storage manager; used to replace
337 * mStorageManager if is different (and not NULL)
339 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
341 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
342 mStorageManager
= updatedStorageManager
;
343 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
345 if (mStorageManager
!= null
) {
346 mFiles
= mStorageManager
.getFolderContent(mFile
);
348 mFiles
= getFolders(mFiles
);
354 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
355 notifyDataSetChanged();
360 * Filter for getting only the folders
362 * @return Vector<OCFile>
364 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
365 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
366 OCFile current
= null
;
367 for (int i
=0; i
<files
.size(); i
++) {
368 current
= files
.get(i
);
369 if (current
.isFolder()) {
378 * Check if parent folder does not include 'S' permission and if file/folder
381 * @param file: OCFile
382 * @return boolean: True if it is shared with me and false if it is not
384 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
385 return (mFile
.getPermissions() != null
386 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
387 && file
.getPermissions() != null
388 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
391 public void setSortOrder(Integer order
, boolean ascending
) {
392 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
393 editor
.putInt("sortOrder", order
);
394 editor
.putBoolean("sortAscending", ascending
);
397 FileStorageUtils
.mSortOrder
= order
;
398 FileStorageUtils
.mSortAscending
= ascending
;
401 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
402 notifyDataSetChanged();
406 private CharSequence
showRelativeTimestamp(OCFile file
){
407 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
408 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);