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
.ArrayList
;
29 import java
.util
.HashMap
;
31 import java
.util
.Vector
;
33 import android
.accounts
.Account
;
34 import android
.content
.Context
;
35 import android
.content
.SharedPreferences
;
36 import android
.graphics
.Bitmap
;
37 import android
.graphics
.Color
;
38 import android
.graphics
.BitmapFactory
;
39 import android
.graphics
.Canvas
;
40 import android
.graphics
.Paint
;
41 import android
.os
.Build
;
42 import android
.preference
.PreferenceManager
;
43 import android
.text
.format
.DateUtils
;
44 import android
.view
.LayoutInflater
;
45 import android
.view
.View
;
46 import android
.view
.ViewGroup
;
47 import android
.widget
.AbsListView
;
48 import android
.widget
.BaseAdapter
;
49 import android
.widget
.ImageView
;
50 import android
.widget
.LinearLayout
;
51 import android
.widget
.ListAdapter
;
52 import android
.widget
.TextView
;
54 import com
.owncloud
.android
.R
;
55 import com
.owncloud
.android
.authentication
.AccountUtils
;
56 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
57 import com
.owncloud
.android
.datamodel
.OCFile
;
58 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
59 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
60 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
61 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
62 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
63 import com
.owncloud
.android
.utils
.DisplayUtils
;
64 import com
.owncloud
.android
.utils
.FileStorageUtils
;
65 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
69 * This Adapter populates a ListView with all files and folders in an ownCloud
72 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
73 private final static String PERMISSION_SHARED_WITH_ME
= "S";
75 private Context mContext
;
76 private OCFile mFile
= null
;
77 private Vector
<OCFile
> mFiles
= null
;
78 private Vector
<OCFile
> mFilesOrig
= new Vector
<OCFile
>();
79 private boolean mJustFolders
;
81 private FileDataStorageManager mStorageManager
;
82 private Account mAccount
;
83 private ComponentsGetter mTransferServiceGetter
;
84 private boolean mGridMode
;
86 private enum ViewType
{LIST_ITEM
, GRID_IMAGE
, GRID_ITEM
};
88 private SharedPreferences mAppPreferences
;
90 private HashMap
<Integer
, Boolean
> mSelection
= new HashMap
<Integer
, Boolean
>();
92 public FileListListAdapter(
95 ComponentsGetter transferServiceGetter
98 mJustFolders
= justFolders
;
100 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
101 mTransferServiceGetter
= transferServiceGetter
;
103 mAppPreferences
= PreferenceManager
104 .getDefaultSharedPreferences(mContext
);
106 // Read sorting order, default to sort by name ascending
107 FileStorageUtils
.mSortOrder
= mAppPreferences
.getInt("sortOrder", 0);
108 FileStorageUtils
.mSortAscending
= mAppPreferences
.getBoolean("sortAscending", true
);
110 // initialise thumbnails cache on background thread
111 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
117 public boolean areAllItemsEnabled() {
122 public boolean isEnabled(int position
) {
127 public int getCount() {
128 return mFiles
!= null ? mFiles
.size() : 0;
132 public Object
getItem(int position
) {
133 if (mFiles
== null
|| mFiles
.size() <= position
)
135 return mFiles
.get(position
);
139 public long getItemId(int position
) {
140 if (mFiles
== null
|| mFiles
.size() <= position
)
142 return mFiles
.get(position
).getFileId();
146 public int getItemViewType(int position
) {
151 public View
getView(int position
, View convertView
, ViewGroup parent
) {
153 View view
= convertView
;
155 LayoutInflater inflator
= (LayoutInflater
) mContext
156 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
158 if (mFiles
!= null
&& mFiles
.size() > position
) {
159 file
= mFiles
.get(position
);
162 // Find out which layout should be displayed
165 viewType
= ViewType
.LIST_ITEM
;
166 } else if (file
.isImage() || file
.isVideo()){
167 viewType
= ViewType
.GRID_IMAGE
;
169 viewType
= ViewType
.GRID_ITEM
;
172 // create view only if differs, otherwise reuse
173 if (convertView
== null
|| (convertView
!= null
&& convertView
.getTag() != viewType
)) {
176 view
= inflator
.inflate(R
.layout
.grid_image
, null
);
177 view
.setTag(ViewType
.GRID_IMAGE
);
180 view
= inflator
.inflate(R
.layout
.grid_item
, null
);
181 view
.setTag(ViewType
.GRID_ITEM
);
184 view
= inflator
.inflate(R
.layout
.list_item
, null
);
185 view
.setTag(ViewType
.LIST_ITEM
);
194 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.thumbnail
);
196 fileIcon
.setTag(file
.getFileId());
198 String name
= file
.getFileName();
200 LinearLayout linearLayout
= (LinearLayout
) view
.findViewById(R
.id
.ListItemLayout
);
201 linearLayout
.setContentDescription("LinearLayout-" + name
);
205 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.last_size
);
206 TextView fileSizeSeparatorV
= (TextView
) view
.findViewById(R
.id
.file_separator
);
207 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.file_mod
);
210 lastModV
.setVisibility(View
.VISIBLE
);
211 lastModV
.setText(showRelativeTimestamp(file
));
214 fileSizeSeparatorV
.setVisibility(View
.VISIBLE
);
215 fileSizeV
.setVisibility(View
.VISIBLE
);
216 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
218 // if (!file.isFolder()) {
219 // AbsListView parentList = (AbsListView)parent;
220 // if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
221 // if (parentList.getChoiceMode() == AbsListView.CHOICE_MODE_NONE) {
222 // checkBoxV.setVisibility(View.GONE);
224 // if (parentList.isItemChecked(position)) {
225 // checkBoxV.setImageResource(
226 // R.drawable.ic_checkbox_marked);
228 // checkBoxV.setImageResource(
229 // R.drawable.ic_checkbox_blank_outline);
231 // checkBoxV.setVisibility(View.VISIBLE);
235 if (file
.isFolder()) {
236 fileSizeSeparatorV
.setVisibility(View
.INVISIBLE
);
237 fileSizeV
.setVisibility(View
.INVISIBLE
);
242 fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
243 name
= file
.getFileName();
244 fileName
.setText(name
);
248 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
249 if (file
.isShareByLink()) {
250 sharedIconV
.setVisibility(View
.VISIBLE
);
251 sharedIconV
.bringToFront();
253 sharedIconV
.setVisibility(View
.GONE
);
257 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.localFileIndicator
);
258 localStateView
.bringToFront();
259 FileDownloaderBinder downloaderBinder
=
260 mTransferServiceGetter
.getFileDownloaderBinder();
261 FileUploaderBinder uploaderBinder
=
262 mTransferServiceGetter
.getFileUploaderBinder();
263 OperationsServiceBinder opsBinder
=
264 mTransferServiceGetter
.getOperationsServiceBinder();
266 localStateView
.setVisibility(View
.INVISIBLE
); // default first
270 opsBinder
.isSynchronizing(mAccount
, file
.getRemotePath())
272 localStateView
.setImageResource(R
.drawable
.synchronizing_file_indicator
);
273 localStateView
.setVisibility(View
.VISIBLE
);
275 } else if ( // downloading
276 downloaderBinder
!= null
&&
277 downloaderBinder
.isDownloading(mAccount
, file
)
279 localStateView
.setImageResource(
281 R
.drawable
.synchronizing_file_indicator
:
282 R
.drawable
.downloading_file_indicator
284 localStateView
.setVisibility(View
.VISIBLE
);
286 } else if ( //uploading
287 uploaderBinder
!= null
&&
288 uploaderBinder
.isUploading(mAccount
, file
)
290 localStateView
.setImageResource(
292 R
.drawable
.synchronizing_file_indicator
:
293 R
.drawable
.uploading_file_indicator
295 localStateView
.setVisibility(View
.VISIBLE
);
297 } else if (file
.getEtagInConflict() != null
) { // conflict
298 localStateView
.setImageResource(R
.drawable
.conflict_file_indicator
);
299 localStateView
.setVisibility(View
.VISIBLE
);
301 } else if (file
.isDown()) {
302 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
303 localStateView
.setVisibility(View
.VISIBLE
);
306 // share with me icon
307 ImageView sharedWithMeIconV
= (ImageView
)
308 view
.findViewById(R
.id
.sharedWithMeIcon
);
309 sharedWithMeIconV
.bringToFront();
310 if (checkIfFileIsSharedWithMe(file
) &&
311 (!file
.isFolder() || !mGridMode
)) {
312 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
314 sharedWithMeIconV
.setVisibility(View
.GONE
);
320 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
321 checkBoxV
.setVisibility(View
.GONE
);
323 AbsListView parentList
= (AbsListView
)parent
;
324 if (android
.os
.Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.HONEYCOMB
) {
325 if (parentList
.getChoiceMode() == AbsListView
.CHOICE_MODE_NONE
) {
326 checkBoxV
.setVisibility(View
.GONE
);
327 } else if (parentList
.getCheckedItemCount() > 0){
328 if (parentList
.isItemChecked(position
)) {
329 checkBoxV
.setImageResource(
330 android
.R
.drawable
.checkbox_on_background
);
332 checkBoxV
.setImageResource(
333 android
.R
.drawable
.checkbox_off_background
);
335 checkBoxV
.setVisibility(View
.VISIBLE
);
341 // this if-else is needed even though favorite icon is visible by default
342 // because android reuses views in listview
343 if (!file
.isFavorite()) {
344 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.GONE
);
346 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.VISIBLE
);
350 if (!file
.isFolder()) {
351 if ((file
.isImage() || file
.isVideo()) && file
.getRemoteId() != null
){
352 // Thumbnail in Cache?
353 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
354 "t" + String
.valueOf(file
.getRemoteId()));
355 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
357 if (file
.isVideo()) {
358 Bitmap withOverlay
= ThumbnailsCacheManager
.addVideoOverlay(thumbnail
);
359 fileIcon
.setImageBitmap(withOverlay
);
361 fileIcon
.setImageBitmap(thumbnail
);
364 // generate new Thumbnail
365 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
366 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
367 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
368 fileIcon
, mStorageManager
, mAccount
370 if (thumbnail
== null
) {
371 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
373 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
374 new ThumbnailsCacheManager
.AsyncDrawable(
375 mContext
.getResources(),
379 fileIcon
.setImageDrawable(asyncDrawable
);
380 task
.execute(file
, true
);
384 if (file
.getMimetype().equalsIgnoreCase("image/png")) {
385 fileIcon
.setBackgroundColor(mContext
.getResources()
386 .getColor(R
.color
.background_color
));
391 fileIcon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(file
.getMimetype(),
392 file
.getFileName()));
397 fileIcon
.setImageResource(
398 MimetypeIconUtil
.getFolderTypeIconId(
399 checkIfFileIsSharedWithMe(file
), file
.isShareByLink()));
403 if (mSelection
.get(position
) != null
) {
404 view
.setBackgroundColor(Color
.rgb(248, 248, 248));
406 view
.setBackgroundColor(Color
.WHITE
);
413 public int getViewTypeCount() {
418 public boolean hasStableIds() {
423 public boolean isEmpty() {
424 return (mFiles
== null
|| mFiles
.isEmpty());
428 * Change the adapted directory for a new one
429 * @param directory New file to adapt. Can be NULL, meaning
430 * "no content to adapt".
431 * @param updatedStorageManager Optional updated storage manager; used to replace
432 * mStorageManager if is different (and not NULL)
434 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
435 , boolean onlyOnDevice
) {
437 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
438 mStorageManager
= updatedStorageManager
;
439 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
441 if (mStorageManager
!= null
) {
442 mFiles
= mStorageManager
.getFolderContent(mFile
, onlyOnDevice
);
444 mFilesOrig
.addAll(mFiles
);
447 mFiles
= getFolders(mFiles
);
453 mFiles
= FileStorageUtils
.sortOcFolder(mFiles
);
454 notifyDataSetChanged();
459 * Filter for getting only the folders
461 * @return Vector<OCFile>
463 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
464 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
465 OCFile current
= null
;
466 for (int i
=0; i
<files
.size(); i
++) {
467 current
= files
.get(i
);
468 if (current
.isFolder()) {
477 * Check if parent folder does not include 'S' permission and if file/folder
480 * @param file: OCFile
481 * @return boolean: True if it is shared with me and false if it is not
483 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
484 return (mFile
.getPermissions() != null
485 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
486 && file
.getPermissions() != null
487 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));
490 public void setSortOrder(Integer order
, boolean ascending
) {
491 SharedPreferences
.Editor editor
= mAppPreferences
.edit();
492 editor
.putInt("sortOrder", order
);
493 editor
.putBoolean("sortAscending", ascending
);
496 FileStorageUtils
.mSortOrder
= order
;
497 FileStorageUtils
.mSortAscending
= ascending
;
500 mFiles
= FileStorageUtils
.sortOcFolder(mFiles
);
501 notifyDataSetChanged();
505 private CharSequence
showRelativeTimestamp(OCFile file
){
506 return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(),
507 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0);
510 public void setGridMode(boolean gridMode
) {
511 mGridMode
= gridMode
;
514 public boolean isGridMode() {
518 public void setNewSelection(int position
, boolean checked
) {
519 mSelection
.put(position
, checked
);
520 notifyDataSetChanged();
523 public void removeSelection(int position
) {
524 mSelection
.remove(position
);
525 notifyDataSetChanged();
528 public void removeSelection(){
530 notifyDataSetChanged();
533 public ArrayList
<Integer
> getCheckedItemPositions() {
534 ArrayList
<Integer
> ids
= new ArrayList
<Integer
>();
536 for (Map
.Entry
<Integer
, Boolean
> entry
: mSelection
.entrySet()){
537 if (entry
.getValue()){
538 ids
.add(entry
.getKey());
544 public ArrayList
<OCFile
> getCheckedItems() {
545 ArrayList
<OCFile
> files
= new ArrayList
<OCFile
>();
547 for (Map
.Entry
<Integer
, Boolean
> entry
: mSelection
.entrySet()){
548 if (entry
.getValue()){
549 files
.add((OCFile
) getItem(entry
.getKey()));