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
.utils
.DisplayUtils
;
45 import com
.owncloud
.android
.utils
.FileStorageUtils
;
49 * This Adapter populates a ListView with all files and folders in an ownCloud
52 * @author Bartek Przybylski
53 * @author Tobias Kaminsky
54 * @author David A. Velasco
56 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
57 private final static String PERMISSION_SHARED_WITH_ME
= "S";
59 private Context mContext
;
60 private OCFile mFile
= null
;
61 private Vector
<OCFile
> mFiles
= null
;
62 private boolean mJustFolders
;
64 private FileDataStorageManager mStorageManager
;
65 private Account mAccount
;
67 private SharedPreferences mAppPreferences
;
69 public FileListListAdapter(
74 mJustFolders
= justFolders
;
76 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
78 mAppPreferences
= PreferenceManager
79 .getDefaultSharedPreferences(mContext
);
81 // Read sorting order, default to sort by name ascending
82 FileStorageUtils
.mSortOrder
= mAppPreferences
.getInt("sortOrder", 0);
83 FileStorageUtils
.mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
86 // initialise thumbnails cache on background thread
87 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
92 public boolean areAllItemsEnabled() {
97 public boolean isEnabled(int position
) {
102 public int getCount() {
103 return mFiles
!= null ? mFiles
.size() : 0;
107 public Object
getItem(int position
) {
108 if (mFiles
== null
|| mFiles
.size() <= position
)
110 return mFiles
.get(position
);
114 public long getItemId(int position
) {
115 if (mFiles
== null
|| mFiles
.size() <= position
)
117 return mFiles
.get(position
).getFileId();
121 public int getItemViewType(int position
) {
126 public View
getView(int position
, View convertView
, ViewGroup parent
) {
127 View view
= convertView
;
129 LayoutInflater inflator
= (LayoutInflater
) mContext
130 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
131 view
= inflator
.inflate(R
.layout
.list_item
, null
);
134 if (mFiles
!= null
&& mFiles
.size() > position
) {
135 OCFile file
= mFiles
.get(position
);
136 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
137 String name
= file
.getFileName();
139 fileName
.setText(name
);
140 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
141 fileIcon
.setTag(file
.getFileId());
142 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
143 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
144 sharedWithMeIconV
.setVisibility(View
.GONE
);
146 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
147 localStateView
.bringToFront();
148 if (file
.isSynchronizing() || file
.isDownloading()) {
149 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
150 localStateView
.setVisibility(View
.VISIBLE
);
151 } else if (file
.isUploading()) {
152 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
153 localStateView
.setVisibility(View
.VISIBLE
);
154 } else if (file
.isDown()) {
155 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
156 localStateView
.setVisibility(View
.VISIBLE
);
158 localStateView
.setVisibility(View
.INVISIBLE
);
161 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
162 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
163 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
165 if (!file
.isFolder()) {
166 fileSizeV
.setVisibility(View
.VISIBLE
);
167 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
168 lastModV
.setVisibility(View
.VISIBLE
);
169 lastModV
.setText(showRelativeTimestamp(file
));
170 // this if-else is needed even thoe fav icon is visible by default
171 // because android reuses views in listview
172 if (!file
.keepInSync()) {
173 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
175 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
178 ListView parentList
= (ListView
)parent
;
179 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
180 checkBoxV
.setVisibility(View
.GONE
);
182 if (parentList
.isItemChecked(position
)) {
183 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
185 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
187 checkBoxV
.setVisibility(View
.VISIBLE
);
190 // get Thumbnail if file is image
191 if (file
.isImage() && file
.getRemoteId() != null
){
192 // Thumbnail in Cache?
193 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
194 String
.valueOf(file
.getRemoteId())
196 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
197 fileIcon
.setImageBitmap(thumbnail
);
200 // generate new Thumbnail
201 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
202 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
203 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
204 fileIcon
, mStorageManager
, mAccount
206 if (thumbnail
== null
) {
207 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
209 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
210 new ThumbnailsCacheManager
.AsyncDrawable(
211 mContext
.getResources(),
215 fileIcon
.setImageDrawable(asyncDrawable
);
220 fileIcon
.setImageResource(DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName()));
223 if (checkIfFileIsSharedWithMe(file
)) {
224 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
228 // TODO Re-enable when server supports folder-size calculation
229 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
230 // fileSizeV.setVisibility(View.VISIBLE);
231 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
233 fileSizeV
.setVisibility(View
.INVISIBLE
);
236 lastModV
.setVisibility(View
.VISIBLE
);
237 lastModV
.setText(showRelativeTimestamp(file
));
238 checkBoxV
.setVisibility(View
.GONE
);
239 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
241 if (checkIfFileIsSharedWithMe(file
)) {
242 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
243 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
245 fileIcon
.setImageResource(
246 DisplayUtils
.getFileTypeIconId(file
.getMimetype(), file
.getFileName())
250 // If folder is sharedByLink, icon folder must be changed to
252 if (file
.isShareByLink()) {
253 fileIcon
.setImageResource(R
.drawable
.folder_public
);
257 if (file
.isShareByLink()) {
258 sharedIconV
.setVisibility(View
.VISIBLE
);
260 sharedIconV
.setVisibility(View
.GONE
);
268 * Local Folder size in human readable format
272 * @return Size in human readable format
274 private String
getFolderSizeHuman(String path
) {
276 File dir
= new File(path
);
279 long bytes
= FileStorageUtils
.getFolderSize(dir
);
280 return DisplayUtils
.bytesToHumanReadable(bytes
);
289 * @return Size in bytes
291 private long getFolderSize(File dir
) {
294 File
[] fileList
= dir
.listFiles();
295 for(int i
= 0; i
< fileList
.length
; i
++) {
296 if(fileList
[i
].isDirectory()) {
297 result
+= getFolderSize(fileList
[i
]);
299 result
+= fileList
[i
].length();
308 public int getViewTypeCount() {
313 public boolean hasStableIds() {
318 public boolean isEmpty() {
319 return (mFiles
== null
|| mFiles
.isEmpty());
323 * Change the adapted directory for a new one
324 * @param directory New file to adapt. Can be NULL, meaning
325 * "no content to adapt".
326 * @param updatedStorageManager Optional updated storage manager; used to replace
327 * mStorageManager if is different (and not NULL)
329 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
331 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
332 mStorageManager
= updatedStorageManager
;
333 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
335 if (mStorageManager
!= null
) {
336 mFiles
= mStorageManager
.getFolderContent(mFile
);
338 mFiles
= getFolders(mFiles
);
344 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
345 notifyDataSetChanged();
350 * Filter for getting only the folders
352 * @return Vector<OCFile>
354 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
355 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
356 OCFile current
= null
;
357 for (int i
=0; i
<files
.size(); i
++) {
358 current
= files
.get(i
);
359 if (current
.isFolder()) {
368 * Check if parent folder does not include 'S' permission and if file/folder
371 * @param file: OCFile
372 * @return boolean: True if it is shared with me and false if it is not
374 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
375 return (mFile
.getPermissions() != null
376 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
377 && file
.getPermissions() != null
378 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
381 public void setSortOrder(Integer order
, boolean ascending
) {
382 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
383 editor
.putInt("sortOrder", order
);
384 editor
.putBoolean("sortAscending", ascending
);
387 FileStorageUtils
.mSortOrder
= order
;
388 FileStorageUtils
.mSortAscending
= ascending
;
391 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
392 notifyDataSetChanged();
396 private CharSequence
showRelativeTimestamp(OCFile file
){
397 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
398 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);