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
.Collections
;
23 import java
.util
.Comparator
;
24 import java
.util
.Vector
;
26 import third_parties
.daveKoeller
.AlphanumComparator
;
27 import android
.accounts
.Account
;
28 import android
.content
.Context
;
29 import android
.content
.SharedPreferences
;
30 import android
.graphics
.Bitmap
;
31 import android
.preference
.PreferenceManager
;
32 import android
.view
.LayoutInflater
;
33 import android
.view
.View
;
34 import android
.view
.ViewGroup
;
35 import android
.widget
.BaseAdapter
;
36 import android
.widget
.ImageView
;
37 import android
.widget
.ListAdapter
;
38 import android
.widget
.ListView
;
39 import android
.widget
.TextView
;
41 import com
.owncloud
.android
.R
;
42 import com
.owncloud
.android
.authentication
.AccountUtils
;
43 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
44 import com
.owncloud
.android
.datamodel
.OCFile
;
45 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
46 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
.AsyncDrawable
;
47 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
48 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
49 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
50 import com
.owncloud
.android
.utils
.DisplayUtils
;
51 import com
.owncloud
.android
.utils
.FileStorageUtils
;
55 * This Adapter populates a ListView with all files and folders in an ownCloud
58 * @author Bartek Przybylski
59 * @author Tobias Kaminsky
60 * @author David A. Velasco
62 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
63 private final static String PERMISSION_SHARED_WITH_ME
= "S";
65 private Context mContext
;
66 private OCFile mFile
= null
;
67 private Vector
<OCFile
> mFiles
= null
;
68 private boolean mJustFolders
;
70 private FileDataStorageManager mStorageManager
;
71 private Account mAccount
;
72 private ComponentsGetter mTransferServiceGetter
;
74 private SharedPreferences mAppPreferences
;
76 public FileListListAdapter(
79 ComponentsGetter transferServiceGetter
82 mJustFolders
= justFolders
;
84 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
85 mTransferServiceGetter
= transferServiceGetter
;
87 mAppPreferences
= PreferenceManager
88 .getDefaultSharedPreferences(mContext
);
90 // Read sorting order, default to sort by name ascending
91 FileStorageUtils
.mSortOrder
= mAppPreferences
92 .getInt("sortOrder", 0);
93 FileStorageUtils
.mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
95 // initialise thumbnails cache on background thread
96 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
100 public boolean areAllItemsEnabled() {
105 public boolean isEnabled(int position
) {
110 public int getCount() {
111 return mFiles
!= null ? mFiles
.size() : 0;
115 public Object
getItem(int position
) {
116 if (mFiles
== null
|| mFiles
.size() <= position
)
118 return mFiles
.get(position
);
122 public long getItemId(int position
) {
123 if (mFiles
== null
|| mFiles
.size() <= position
)
125 return mFiles
.get(position
).getFileId();
129 public int getItemViewType(int position
) {
134 public View
getView(int position
, View convertView
, ViewGroup parent
) {
135 View view
= convertView
;
137 LayoutInflater inflator
= (LayoutInflater
) mContext
138 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
139 view
= inflator
.inflate(R
.layout
.list_item
, null
);
142 if (mFiles
!= null
&& mFiles
.size() > position
) {
143 OCFile file
= mFiles
.get(position
);
144 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
145 String name
= file
.getFileName();
147 fileName
.setText(name
);
148 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
149 fileIcon
.setTag(file
.getFileId());
150 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
151 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
152 sharedWithMeIconV
.setVisibility(View
.GONE
);
154 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
155 localStateView
.bringToFront();
156 FileDownloaderBinder downloaderBinder
=
157 mTransferServiceGetter
.getFileDownloaderBinder();
158 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
159 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
160 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
161 localStateView
.setVisibility(View
.VISIBLE
);
162 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
163 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
164 localStateView
.setVisibility(View
.VISIBLE
);
165 } else if (file
.isDown()) {
166 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
167 localStateView
.setVisibility(View
.VISIBLE
);
169 localStateView
.setVisibility(View
.INVISIBLE
);
172 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
173 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
174 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
176 if (!file
.isFolder()) {
177 fileSizeV
.setVisibility(View
.VISIBLE
);
178 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
179 lastModV
.setVisibility(View
.VISIBLE
);
181 DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp())
183 // this if-else is needed even thoe fav icon is visible by default
184 // because android reuses views in listview
185 if (!file
.keepInSync()) {
186 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
188 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
191 ListView parentList
= (ListView
)parent
;
192 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
193 checkBoxV
.setVisibility(View
.GONE
);
195 if (parentList
.isItemChecked(position
)) {
196 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
198 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
200 checkBoxV
.setVisibility(View
.VISIBLE
);
203 // get Thumbnail if file is image
204 if (file
.isImage() && file
.getRemoteId() != null
){
205 // Thumbnail in Cache?
206 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
207 String
.valueOf(file
.getRemoteId())
209 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
210 fileIcon
.setImageBitmap(thumbnail
);
212 // generate new Thumbnail
213 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
214 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
215 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
216 fileIcon
, mStorageManager
218 if (thumbnail
== null
) {
219 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
221 final AsyncDrawable asyncDrawable
= new AsyncDrawable(
222 mContext
.getResources(),
226 fileIcon
.setImageDrawable(asyncDrawable
);
231 fileIcon
.setImageResource(
232 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
236 if (checkIfFileIsSharedWithMe(file
)) {
237 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
241 // TODO Re-enable when server supports folder-size calculation
242 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
243 // fileSizeV.setVisibility(View.VISIBLE);
244 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
246 fileSizeV
.setVisibility(View
.INVISIBLE
);
249 lastModV
.setVisibility(View
.VISIBLE
);
251 DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp())
253 checkBoxV
.setVisibility(View
.GONE
);
254 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
256 if (checkIfFileIsSharedWithMe(file
)) {
257 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
258 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
260 fileIcon
.setImageResource(
261 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
265 // If folder is sharedByLink, icon folder must be changed to
267 if (file
.isShareByLink()) {
268 fileIcon
.setImageResource(R
.drawable
.folder_public
);
272 if (file
.isShareByLink()) {
273 sharedIconV
.setVisibility(View
.VISIBLE
);
275 sharedIconV
.setVisibility(View
.GONE
);
283 * Local Folder size in human readable format
287 * @return Size in human readable format
289 private String
getFolderSizeHuman(String path
) {
291 File dir
= new File(path
);
294 long bytes
= FileStorageUtils
.getFolderSize(dir
);
295 return DisplayUtils
.bytesToHumanReadable(bytes
);
304 public int getViewTypeCount() {
309 public boolean hasStableIds() {
314 public boolean isEmpty() {
315 return (mFiles
== null
|| mFiles
.isEmpty());
319 * Change the adapted directory for a new one
320 * @param directory New file to adapt. Can be NULL, meaning
321 * "no content to adapt".
322 * @param updatedStorageManager Optional updated storage manager; used to replace
323 * mStorageManager if is different (and not NULL)
325 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
327 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
328 mStorageManager
= updatedStorageManager
;
329 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
331 if (mStorageManager
!= null
) {
332 mFiles
= mStorageManager
.getFolderContent(mFile
);
334 mFiles
= getFolders(mFiles
);
340 mFiles
= FileStorageUtils
.sortDirectory(mFiles
);
341 notifyDataSetChanged();
346 * Filter for getting only the folders
348 * @return Vector<OCFile>
350 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
351 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
352 OCFile current
= null
;
353 for (int i
=0; i
<files
.size(); i
++) {
354 current
= files
.get(i
);
355 if (current
.isFolder()) {
364 * Check if parent folder does not include 'S' permission and if file/folder
367 * @param file: OCFile
368 * @return boolean: True if it is shared with me and false if it is not
370 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
371 return (mFile
.getPermissions() != null
372 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
373 && file
.getPermissions() != null
374 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
377 public void setSortOrder(Integer order
, boolean ascending
) {
378 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
379 editor
.putInt("sortOrder", order
);
380 editor
.putBoolean("sortAscending", ascending
);
383 FileStorageUtils
.mSortOrder
= order
;
384 FileStorageUtils
.mSortAscending
= ascending
;
386 mFiles
= FileStorageUtils
.sortDirectory(mFiles
);
387 notifyDataSetChanged();