2 * ownCloud Android client application
4 * @author Bartek Przybylski
5 * @author Tobias Kaminsky
6 * @author David A. Velasco
8 * Copyright (C) 2011 Bartek Przybylski
9 * Copyright (C) 2015 ownCloud Inc.
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 package com
.owncloud
.android
.ui
.adapter
;
28 import java
.util
.Vector
;
30 import android
.accounts
.Account
;
31 import android
.content
.Context
;
32 import android
.content
.SharedPreferences
;
33 import android
.graphics
.Bitmap
;
34 import android
.os
.Build
;
35 import android
.preference
.PreferenceManager
;
36 import android
.text
.format
.DateUtils
;
37 import android
.view
.LayoutInflater
;
38 import android
.view
.View
;
39 import android
.view
.ViewGroup
;
40 import android
.widget
.AbsListView
;
41 import android
.widget
.BaseAdapter
;
42 import android
.widget
.ImageView
;
43 import android
.widget
.LinearLayout
;
44 import android
.widget
.ListAdapter
;
45 import android
.widget
.TextView
;
47 import com
.owncloud
.android
.R
;
48 import com
.owncloud
.android
.authentication
.AccountUtils
;
49 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
50 import com
.owncloud
.android
.datamodel
.OCFile
;
51 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
52 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
53 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
54 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
55 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
56 import com
.owncloud
.android
.utils
.DisplayUtils
;
57 import com
.owncloud
.android
.utils
.FileStorageUtils
;
58 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
62 * This Adapter populates a ListView with all files and folders in an ownCloud
65 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
67 private Context mContext
;
68 private OCFile mFile
= null
;
69 private Vector
<OCFile
> mFiles
= null
;
70 private Vector
<OCFile
> mFilesOrig
= new Vector
<OCFile
>();
71 private boolean mJustFolders
;
73 private FileDataStorageManager mStorageManager
;
74 private Account mAccount
;
75 private ComponentsGetter mTransferServiceGetter
;
76 private boolean mGridMode
;
78 private enum ViewType
{LIST_ITEM
, GRID_IMAGE
, GRID_ITEM
};
80 private SharedPreferences mAppPreferences
;
82 public FileListListAdapter(
85 ComponentsGetter transferServiceGetter
88 mJustFolders
= justFolders
;
90 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
91 mTransferServiceGetter
= transferServiceGetter
;
93 mAppPreferences
= PreferenceManager
94 .getDefaultSharedPreferences(mContext
);
96 // Read sorting order, default to sort by name ascending
97 FileStorageUtils
.mSortOrder
= mAppPreferences
.getInt("sortOrder", 0);
98 FileStorageUtils
.mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
100 // initialise thumbnails cache on background thread
101 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
107 public boolean areAllItemsEnabled() {
112 public boolean isEnabled(int position
) {
117 public int getCount() {
118 return mFiles
!= null ? mFiles
.size() : 0;
122 public Object
getItem(int position
) {
123 if (mFiles
== null
|| mFiles
.size() <= position
)
125 return mFiles
.get(position
);
129 public long getItemId(int position
) {
130 if (mFiles
== null
|| mFiles
.size() <= position
)
132 return mFiles
.get(position
).getFileId();
136 public int getItemViewType(int position
) {
141 public View
getView(int position
, View convertView
, ViewGroup parent
) {
143 View view
= convertView
;
145 LayoutInflater inflator
= (LayoutInflater
) mContext
146 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
148 if (mFiles
!= null
&& mFiles
.size() > position
) {
149 file
= mFiles
.get(position
);
152 // Find out which layout should be displayed
155 viewType
= ViewType
.LIST_ITEM
;
156 } else if (file
.isImage()){
157 viewType
= ViewType
.GRID_IMAGE
;
159 viewType
= ViewType
.GRID_ITEM
;
162 // create view only if differs, otherwise reuse
163 if (convertView
== null
|| (convertView
!= null
&& convertView
.getTag() != viewType
)) {
166 view
= inflator
.inflate(R
.layout
.grid_image
, null
);
167 view
.setTag(ViewType
.GRID_IMAGE
);
170 view
= inflator
.inflate(R
.layout
.grid_item
, null
);
171 view
.setTag(ViewType
.GRID_ITEM
);
174 view
= inflator
.inflate(R
.layout
.list_item
, null
);
175 view
.setTag(ViewType
.LIST_ITEM
);
184 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.thumbnail
);
186 fileIcon
.setTag(file
.getFileId());
188 String name
= file
.getFileName();
190 LinearLayout linearLayout
= (LinearLayout
) view
.findViewById(R
.id
.ListItemLayout
);
191 linearLayout
.setContentDescription("LinearLayout-" + name
);
195 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
196 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
197 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
199 lastModV
.setVisibility(View
.VISIBLE
);
200 lastModV
.setText(showRelativeTimestamp(file
));
202 checkBoxV
.setVisibility(View
.GONE
);
204 fileSizeV
.setVisibility(View
.VISIBLE
);
205 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
207 if (!file
.isFolder()) {
208 AbsListView parentList
= (AbsListView
)parent
;
209 if (android
.os
.Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.HONEYCOMB
) {
210 if (parentList
.getChoiceMode() == AbsListView
.CHOICE_MODE_NONE
) {
211 checkBoxV
.setVisibility(View
.GONE
);
213 if (parentList
.isItemChecked(position
)) {
214 checkBoxV
.setImageResource(
215 android
.R
.drawable
.checkbox_on_background
);
217 checkBoxV
.setImageResource(
218 android
.R
.drawable
.checkbox_off_background
);
220 checkBoxV
.setVisibility(View
.VISIBLE
);
225 fileSizeV
.setVisibility(View
.INVISIBLE
);
230 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
231 name
= file
.getFileName();
232 fileName
.setText(name
);
236 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
237 if (file
.isSharedViaLink()) {
238 sharedIconV
.setImageResource(R
.drawable
.shared_via_link
);
239 sharedIconV
.setVisibility(View
.VISIBLE
);
240 sharedIconV
.bringToFront();
241 } else if (file
.isSharedWithSharee() || file
.isSharedWithMe() ) {
242 sharedIconV
.setImageResource(R
.drawable
.shared_via_users
);
243 sharedIconV
.setVisibility(View
.VISIBLE
);
244 sharedIconV
.bringToFront();
246 sharedIconV
.setVisibility(View
.GONE
);
249 /*ImageView sharedWithMeIcon = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
250 sharedWithMeIcon.bringToFront();*/
253 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.localFileIndicator
);
254 localStateView
.bringToFront();
255 FileDownloaderBinder downloaderBinder
=
256 mTransferServiceGetter
.getFileDownloaderBinder();
257 FileUploaderBinder uploaderBinder
=
258 mTransferServiceGetter
.getFileUploaderBinder();
259 OperationsServiceBinder opsBinder
=
260 mTransferServiceGetter
.getOperationsServiceBinder();
262 localStateView
.setVisibility(View
.INVISIBLE
); // default first
266 opsBinder
.isSynchronizing(mAccount
, file
.getRemotePath())
268 localStateView
.setImageResource(R
.drawable
.synchronizing_file_indicator
);
269 localStateView
.setVisibility(View
.VISIBLE
);
271 } else if ( // downloading
272 downloaderBinder
!= null
&&
273 downloaderBinder
.isDownloading(mAccount
, file
)
275 localStateView
.setImageResource(
277 R
.drawable
.synchronizing_file_indicator
:
278 R
.drawable
.downloading_file_indicator
280 localStateView
.setVisibility(View
.VISIBLE
);
282 } else if ( //uploading
283 uploaderBinder
!= null
&&
284 uploaderBinder
.isUploading(mAccount
, file
)
286 localStateView
.setImageResource(
288 R
.drawable
.synchronizing_file_indicator
:
289 R
.drawable
.uploading_file_indicator
291 localStateView
.setVisibility(View
.VISIBLE
);
293 } else if (file
.getEtagInConflict() != null
) { // conflict
294 localStateView
.setImageResource(R
.drawable
.conflict_file_indicator
);
295 localStateView
.setVisibility(View
.VISIBLE
);
297 } else if (file
.isDown()) {
298 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
299 localStateView
.setVisibility(View
.VISIBLE
);
307 // this if-else is needed even though favorite icon is visible by default
308 // because android reuses views in listview
309 if (!file
.isFavorite()) {
310 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.GONE
);
312 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.VISIBLE
);
316 if (!file
.isFolder()) {
317 if (file
.isImage() && file
.getRemoteId() != null
){
318 // Thumbnail in Cache?
319 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
320 String
.valueOf(file
.getRemoteId())
322 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
323 fileIcon
.setImageBitmap(thumbnail
);
325 // generate new Thumbnail
326 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
327 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
328 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
329 fileIcon
, mStorageManager
, mAccount
331 if (thumbnail
== null
) {
332 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
334 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
335 new ThumbnailsCacheManager
.AsyncDrawable(
336 mContext
.getResources(),
340 fileIcon
.setImageDrawable(asyncDrawable
);
345 if (file
.getMimetype().equalsIgnoreCase("image/png")) {
346 fileIcon
.setBackgroundColor(mContext
.getResources()
347 .getColor(R
.color
.background_color
));
352 fileIcon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(file
.getMimetype(),
353 file
.getFileName()));
358 fileIcon
.setImageResource(
359 MimetypeIconUtil
.getFolderTypeIconId(
360 file
.isSharedWithMe() || file
.isSharedWithSharee(),
361 file
.isSharedViaLink()
371 public int getViewTypeCount() {
376 public boolean hasStableIds() {
381 public boolean isEmpty() {
382 return (mFiles
== null
|| mFiles
.isEmpty());
386 * Change the adapted directory for a new one
387 * @param directory New file to adapt. Can be NULL, meaning
388 * "no content to adapt".
389 * @param updatedStorageManager Optional updated storage manager; used to replace
390 * mStorageManager if is different (and not NULL)
392 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
393 /*, boolean onlyOnDevice*/) {
395 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
396 mStorageManager
= updatedStorageManager
;
397 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
399 if (mStorageManager
!= null
) {
400 // TODO Enable when "On Device" is recovered ?
401 mFiles
= mStorageManager
.getFolderContent(mFile
/*, onlyOnDevice*/);
403 mFilesOrig
.addAll(mFiles
);
406 mFiles
= getFolders(mFiles
);
412 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
413 notifyDataSetChanged();
418 * Filter for getting only the folders
420 * @return Vector<OCFile>
422 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
423 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
424 OCFile current
= null
;
425 for (int i
=0; i
<files
.size(); i
++) {
426 current
= files
.get(i
);
427 if (current
.isFolder()) {
435 public void setSortOrder(Integer order
, boolean ascending
) {
436 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
437 editor
.putInt("sortOrder", order
);
438 editor
.putBoolean("sortAscending", ascending
);
441 FileStorageUtils
.mSortOrder
= order
;
442 FileStorageUtils
.mSortAscending
= ascending
;
445 mFiles
= FileStorageUtils
.sortFolder(mFiles
);
446 notifyDataSetChanged();
450 private CharSequence
showRelativeTimestamp(OCFile file
){
451 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
452 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);
455 public void setGridMode(boolean gridMode
) {
456 mGridMode
= gridMode
;