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 
{ 
  66     private final static String PERMISSION_SHARED_WITH_ME 
= "S"; 
  68     private Context mContext
; 
  69     private OCFile mFile 
= null
; 
  70     private Vector
<OCFile
> mFiles 
= null
; 
  71     private Vector
<OCFile
> mFilesOrig 
= new Vector
<OCFile
>(); 
  72     private boolean mJustFolders
; 
  74     private FileDataStorageManager mStorageManager
; 
  75     private Account mAccount
; 
  76     private ComponentsGetter mTransferServiceGetter
; 
  77     private boolean mGridMode
; 
  79     private enum ViewType 
{LIST_ITEM
, GRID_IMAGE
, GRID_ITEM 
}; 
  81     private SharedPreferences mAppPreferences
; 
  83     public FileListListAdapter( 
  86             ComponentsGetter transferServiceGetter
 
  89         mJustFolders 
= justFolders
; 
  91         mAccount 
= AccountUtils
.getCurrentOwnCloudAccount(mContext
); 
  92         mTransferServiceGetter 
= transferServiceGetter
; 
  94         mAppPreferences 
= PreferenceManager
 
  95                 .getDefaultSharedPreferences(mContext
); 
  97         // Read sorting order, default to sort by name ascending 
  98         FileStorageUtils
.mSortOrder 
= mAppPreferences
.getInt("sortOrder", 0); 
  99         FileStorageUtils
.mSortAscending 
= mAppPreferences
.getBoolean("sortAscending", true
); 
 101         // initialise thumbnails cache on background thread 
 102         new ThumbnailsCacheManager
.InitDiskCacheTask().execute(); 
 108     public boolean areAllItemsEnabled() { 
 113     public boolean isEnabled(int position
) { 
 118     public int getCount() { 
 119         return mFiles 
!= null ? mFiles
.size() : 0; 
 123     public Object 
getItem(int position
) { 
 124         if (mFiles 
== null 
|| mFiles
.size() <= position
) 
 126         return mFiles
.get(position
); 
 130     public long getItemId(int position
) { 
 131         if (mFiles 
== null 
|| mFiles
.size() <= position
) 
 133         return mFiles
.get(position
).getFileId(); 
 137     public int getItemViewType(int position
) { 
 142     public View 
getView(int position
, View convertView
, ViewGroup parent
) { 
 144         View view 
= convertView
; 
 146         LayoutInflater inflator 
= (LayoutInflater
) mContext
 
 147                 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
); 
 149         if (mFiles 
!= null 
&& mFiles
.size() > position
) { 
 150             file 
= mFiles
.get(position
); 
 153         // Find out which layout should be displayed 
 156             viewType 
= ViewType
.LIST_ITEM
; 
 157         } else if (file
.isImage()){ 
 158             viewType 
= ViewType
.GRID_IMAGE
; 
 160             viewType 
= ViewType
.GRID_ITEM
; 
 163         // create view only if differs, otherwise reuse 
 164         if (convertView 
== null 
|| (convertView 
!= null 
&& convertView
.getTag() != viewType
)) { 
 167                     view 
= inflator
.inflate(R
.layout
.grid_image
, null
); 
 168                     view
.setTag(ViewType
.GRID_IMAGE
); 
 171                     view 
= inflator
.inflate(R
.layout
.grid_item
, null
); 
 172                     view
.setTag(ViewType
.GRID_ITEM
); 
 175                     view 
= inflator
.inflate(R
.layout
.list_item
, null
); 
 176                     view
.setTag(ViewType
.LIST_ITEM
); 
 185             ImageView fileIcon 
= (ImageView
) view
.findViewById(R
.id
.thumbnail
); 
 187             fileIcon
.setTag(file
.getFileId()); 
 189             String name 
= file
.getFileName(); 
 191             LinearLayout linearLayout 
= (LinearLayout
) view
.findViewById(R
.id
.ListItemLayout
); 
 192             linearLayout
.setContentDescription("LinearLayout-" + name
); 
 196                     TextView fileSizeV 
= (TextView
) view
.findViewById(R
.id
.file_size
); 
 197                     TextView fileSizeSeparatorV 
= (TextView
) view
.findViewById(R
.id
.file_separator
); 
 198                     TextView lastModV 
= (TextView
) view
.findViewById(R
.id
.last_mod
); 
 199                     ImageView checkBoxV 
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
); 
 201                     lastModV
.setVisibility(View
.VISIBLE
); 
 202                     lastModV
.setText(showRelativeTimestamp(file
)); 
 204                     checkBoxV
.setVisibility(View
.GONE
); 
 206                     fileSizeSeparatorV
.setVisibility(View
.VISIBLE
); 
 207                     fileSizeV
.setVisibility(View
.VISIBLE
); 
 208                     fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength())); 
 210                     if (!file
.isFolder()) { 
 211                         AbsListView parentList 
= (AbsListView
)parent
; 
 212                         if (android
.os
.Build
.VERSION
.SDK_INT 
>= Build
.VERSION_CODES
.HONEYCOMB
) { 
 213                             if (parentList
.getChoiceMode() == AbsListView
.CHOICE_MODE_NONE
) { 
 214                                 checkBoxV
.setVisibility(View
.GONE
); 
 216                                 if (parentList
.isItemChecked(position
)) { 
 217                                     checkBoxV
.setImageResource( 
 218                                             R
.drawable
.ic_checkbox_marked
); 
 220                                     checkBoxV
.setImageResource( 
 221                                             R
.drawable
.ic_checkbox_blank_outline
); 
 223                                 checkBoxV
.setVisibility(View
.VISIBLE
); 
 228                         fileSizeSeparatorV
.setVisibility(View
.INVISIBLE
); 
 229                         fileSizeV
.setVisibility(View
.INVISIBLE
); 
 234                     fileName 
= (TextView
) view
.findViewById(R
.id
.Filename
); 
 235                     name 
= file
.getFileName(); 
 236                     fileName
.setText(name
); 
 240                     ImageView sharedIconV 
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
); 
 241                     if (file
.isShareByLink()) { 
 242                         sharedIconV
.setVisibility(View
.VISIBLE
); 
 243                         sharedIconV
.bringToFront(); 
 245                         sharedIconV
.setVisibility(View
.GONE
); 
 249                     ImageView localStateView 
= (ImageView
) view
.findViewById(R
.id
.localFileIndicator
); 
 250                     localStateView
.bringToFront(); 
 251                     FileDownloaderBinder downloaderBinder 
= 
 252                             mTransferServiceGetter
.getFileDownloaderBinder(); 
 253                     FileUploaderBinder uploaderBinder 
= 
 254                             mTransferServiceGetter
.getFileUploaderBinder(); 
 255                     OperationsServiceBinder opsBinder 
= 
 256                             mTransferServiceGetter
.getOperationsServiceBinder(); 
 258                     localStateView
.setVisibility(View
.INVISIBLE
);   // default first 
 262                                 opsBinder
.isSynchronizing(mAccount
, file
.getRemotePath()) 
 264                         localStateView
.setImageResource(R
.drawable
.synchronizing_file_indicator
); 
 265                         localStateView
.setVisibility(View
.VISIBLE
); 
 267                     } else if ( // downloading 
 268                                 downloaderBinder 
!= null 
&& 
 269                                 downloaderBinder
.isDownloading(mAccount
, file
) 
 271                         localStateView
.setImageResource( 
 273                                         R
.drawable
.synchronizing_file_indicator 
: 
 274                                         R
.drawable
.downloading_file_indicator
 
 276                         localStateView
.setVisibility(View
.VISIBLE
); 
 278                     } else if ( //uploading 
 279                                 uploaderBinder 
!= null 
&& 
 280                                 uploaderBinder
.isUploading(mAccount
, file
) 
 282                         localStateView
.setImageResource( 
 284                                         R
.drawable
.synchronizing_file_indicator 
: 
 285                                         R
.drawable
.uploading_file_indicator
 
 287                         localStateView
.setVisibility(View
.VISIBLE
); 
 289                     } else if (file
.getEtagInConflict() != null
) {   // conflict 
 290                         localStateView
.setImageResource(R
.drawable
.conflict_file_indicator
); 
 291                         localStateView
.setVisibility(View
.VISIBLE
); 
 293                     } else if (file
.isDown()) { 
 294                         localStateView
.setImageResource(R
.drawable
.local_file_indicator
); 
 295                         localStateView
.setVisibility(View
.VISIBLE
); 
 298                     // share with me icon 
 299                     ImageView sharedWithMeIconV 
= (ImageView
) 
 300                             view
.findViewById(R
.id
.sharedWithMeIcon
); 
 301                     sharedWithMeIconV
.bringToFront(); 
 302                     if (checkIfFileIsSharedWithMe(file
) && 
 303                             (!file
.isFolder() || !mGridMode
)) { 
 304                         sharedWithMeIconV
.setVisibility(View
.VISIBLE
); 
 306                         sharedWithMeIconV
.setVisibility(View
.GONE
); 
 314             // this if-else is needed even though favorite icon is visible by default 
 315             // because android reuses views in listview 
 316             if (!file
.isFavorite()) { 
 317                 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.GONE
); 
 319                 view
.findViewById(R
.id
.favoriteIcon
).setVisibility(View
.VISIBLE
); 
 323             if (!file
.isFolder()) { 
 324                 if (file
.isImage() && file
.getRemoteId() != null
){ 
 325                     // Thumbnail in Cache? 
 326                     Bitmap thumbnail 
= ThumbnailsCacheManager
.getBitmapFromDiskCache( 
 327                             String
.valueOf(file
.getRemoteId()) 
 329                     if (thumbnail 
!= null 
&& !file
.needsUpdateThumbnail()){ 
 330                         fileIcon
.setImageBitmap(thumbnail
); 
 332                         // generate new Thumbnail 
 333                         if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) { 
 334                             final ThumbnailsCacheManager
.ThumbnailGenerationTask task 
= 
 335                                     new ThumbnailsCacheManager
.ThumbnailGenerationTask( 
 336                                             fileIcon
, mStorageManager
, mAccount
 
 338                             if (thumbnail 
== null
) { 
 339                                 thumbnail 
= ThumbnailsCacheManager
.mDefaultImg
; 
 341                             final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable 
= 
 342                                     new ThumbnailsCacheManager
.AsyncDrawable( 
 343                                     mContext
.getResources(),  
 347                             fileIcon
.setImageDrawable(asyncDrawable
); 
 352                     if (file
.getMimetype().equalsIgnoreCase("image/png")) { 
 353                         fileIcon
.setBackgroundColor(mContext
.getResources() 
 354                                 .getColor(R
.color
.background_color
)); 
 359                     fileIcon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(file
.getMimetype(), 
 360                             file
.getFileName())); 
 365                 fileIcon
.setImageResource( 
 366                         MimetypeIconUtil
.getFolderTypeIconId( 
 367                                 checkIfFileIsSharedWithMe(file
), file
.isShareByLink())); 
 375     public int getViewTypeCount() { 
 380     public boolean hasStableIds() { 
 385     public boolean isEmpty() { 
 386         return (mFiles 
== null 
|| mFiles
.isEmpty()); 
 390      * Change the adapted directory for a new one 
 391      * @param directory                 New file to adapt. Can be NULL, meaning  
 392      *                                  "no content to adapt". 
 393      * @param updatedStorageManager     Optional updated storage manager; used to replace  
 394      *                                  mStorageManager if is different (and not NULL) 
 396     public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
 
 397             /*, boolean onlyOnDevice*/) { 
 399         if (updatedStorageManager 
!= null 
&& updatedStorageManager 
!= mStorageManager
) { 
 400             mStorageManager 
= updatedStorageManager
; 
 401             mAccount 
= AccountUtils
.getCurrentOwnCloudAccount(mContext
); 
 403         if (mStorageManager 
!= null
) { 
 404             // TODO Enable when "On Device" is recovered ? 
 405             mFiles 
= mStorageManager
.getFolderContent(mFile
/*, onlyOnDevice*/); 
 407             mFilesOrig
.addAll(mFiles
); 
 410                 mFiles 
= getFolders(mFiles
); 
 416         mFiles 
= FileStorageUtils
.sortFolder(mFiles
); 
 417         notifyDataSetChanged(); 
 422      * Filter for getting only the folders 
 424      * @return Vector<OCFile> 
 426     public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) { 
 427         Vector
<OCFile
> ret 
= new Vector
<OCFile
>();  
 428         OCFile current 
= null
;  
 429         for (int i
=0; i
<files
.size(); i
++) { 
 430             current 
= files
.get(i
); 
 431             if (current
.isFolder()) { 
 440      * Check if parent folder does not include 'S' permission and if file/folder 
 443      * @param file: OCFile 
 444      * @return boolean: True if it is shared with me and false if it is not 
 446     private boolean checkIfFileIsSharedWithMe(OCFile file
) { 
 447         return (mFile
.getPermissions() != null 
 
 448                 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
) 
 449                 && file
.getPermissions() != null 
 
 450                 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)); 
 453     public void setSortOrder(Integer order
, boolean ascending
) { 
 454         SharedPreferences
.Editor editor 
= mAppPreferences
.edit(); 
 455         editor
.putInt("sortOrder", order
); 
 456         editor
.putBoolean("sortAscending", ascending
); 
 459         FileStorageUtils
.mSortOrder 
= order
; 
 460         FileStorageUtils
.mSortAscending 
= ascending
; 
 463         mFiles 
= FileStorageUtils
.sortFolder(mFiles
); 
 464         notifyDataSetChanged(); 
 468     private CharSequence 
showRelativeTimestamp(OCFile file
){ 
 469         return DisplayUtils
.getRelativeDateTimeString(mContext
, file
.getModificationTimestamp(), 
 470                 DateUtils
.SECOND_IN_MILLIS
, DateUtils
.WEEK_IN_MILLIS
, 0); 
 473     public void setGridMode(boolean gridMode
) { 
 474         mGridMode 
= gridMode
;