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
;
20 import java
.util
.Vector
;
22 import android
.accounts
.Account
;
23 import android
.content
.Context
;
24 import android
.graphics
.Bitmap
;
25 import android
.view
.LayoutInflater
;
26 import android
.view
.View
;
27 import android
.view
.ViewGroup
;
28 import android
.widget
.BaseAdapter
;
29 import android
.widget
.ImageView
;
30 import android
.widget
.ListAdapter
;
31 import android
.widget
.ListView
;
32 import android
.widget
.TextView
;
34 import com
.owncloud
.android
.R
;
35 import com
.owncloud
.android
.authentication
.AccountUtils
;
36 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
37 import com
.owncloud
.android
.datamodel
.OCFile
;
38 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
39 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
.AsyncDrawable
;
40 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
41 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
42 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
43 import com
.owncloud
.android
.utils
.DisplayUtils
;
47 * This Adapter populates a ListView with all files and folders in an ownCloud
50 * @author Bartek Przybylski
51 * @author Tobias Kaminsky
52 * @author David A. Velasco
54 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
55 private final static String PERMISSION_SHARED_WITH_ME
= "S";
57 private Context mContext
;
58 private OCFile mFile
= null
;
59 private Vector
<OCFile
> mFiles
= null
;
60 private boolean mJustFolders
;
62 private FileDataStorageManager mStorageManager
;
63 private Account mAccount
;
64 private ComponentsGetter mTransferServiceGetter
;
66 public FileListListAdapter(
69 ComponentsGetter transferServiceGetter
72 mJustFolders
= justFolders
;
74 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
75 mTransferServiceGetter
= transferServiceGetter
;
77 // initialise thumbnails cache on background thread
78 new ThumbnailsCacheManager
.InitDiskCacheTask().execute();
82 public boolean areAllItemsEnabled() {
87 public boolean isEnabled(int position
) {
92 public int getCount() {
93 return mFiles
!= null ? mFiles
.size() : 0;
97 public Object
getItem(int position
) {
98 if (mFiles
== null
|| mFiles
.size() <= position
)
100 return mFiles
.get(position
);
104 public long getItemId(int position
) {
105 if (mFiles
== null
|| mFiles
.size() <= position
)
107 return mFiles
.get(position
).getFileId();
111 public int getItemViewType(int position
) {
116 public View
getView(int position
, View convertView
, ViewGroup parent
) {
117 View view
= convertView
;
119 LayoutInflater inflator
= (LayoutInflater
) mContext
120 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
121 view
= inflator
.inflate(R
.layout
.list_item
, null
);
124 if (mFiles
!= null
&& mFiles
.size() > position
) {
125 OCFile file
= mFiles
.get(position
);
126 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
127 String name
= file
.getFileName();
129 fileName
.setText(name
);
130 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
131 fileIcon
.setTag(file
.getFileId());
132 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
133 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
134 sharedWithMeIconV
.setVisibility(View
.GONE
);
136 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
137 localStateView
.bringToFront();
138 FileDownloaderBinder downloaderBinder
=
139 mTransferServiceGetter
.getFileDownloaderBinder();
140 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
141 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
142 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
143 localStateView
.setVisibility(View
.VISIBLE
);
144 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
145 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
146 localStateView
.setVisibility(View
.VISIBLE
);
147 } else if (file
.isDown()) {
148 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
149 localStateView
.setVisibility(View
.VISIBLE
);
151 localStateView
.setVisibility(View
.INVISIBLE
);
154 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
155 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
156 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
158 if (!file
.isFolder()) {
159 fileSizeV
.setVisibility(View
.VISIBLE
);
160 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
161 lastModV
.setVisibility(View
.VISIBLE
);
163 DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp())
165 // this if-else is needed even thoe fav icon is visible by default
166 // because android reuses views in listview
167 if (!file
.keepInSync()) {
168 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
170 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
173 ListView parentList
= (ListView
)parent
;
174 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
175 checkBoxV
.setVisibility(View
.GONE
);
177 if (parentList
.isItemChecked(position
)) {
178 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
180 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
182 checkBoxV
.setVisibility(View
.VISIBLE
);
185 // get Thumbnail if file is image
187 // Thumbnail in Cache?
188 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
189 String
.valueOf(file
.getRemoteId())
191 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()){
192 fileIcon
.setImageBitmap(thumbnail
);
194 // generate new Thumbnail
195 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, fileIcon
)) {
196 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
197 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
198 fileIcon
, mStorageManager
200 final AsyncDrawable asyncDrawable
= new AsyncDrawable(
201 mContext
.getResources(),
202 ThumbnailsCacheManager
.mDefaultImg
,
205 fileIcon
.setImageDrawable(asyncDrawable
);
210 fileIcon
.setImageResource(
211 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
215 if (checkIfFileIsSharedWithMe(file
)) {
216 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
220 fileSizeV
.setVisibility(View
.INVISIBLE
);
221 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
222 lastModV
.setVisibility(View
.VISIBLE
);
224 DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp())
226 checkBoxV
.setVisibility(View
.GONE
);
227 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
229 if (checkIfFileIsSharedWithMe(file
)) {
230 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
231 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
233 fileIcon
.setImageResource(
234 DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName())
238 // If folder is sharedByLink, icon folder must be changed to
240 if (file
.isShareByLink()) {
241 fileIcon
.setImageResource(R
.drawable
.folder_public
);
245 if (file
.isShareByLink()) {
246 sharedIconV
.setVisibility(View
.VISIBLE
);
248 sharedIconV
.setVisibility(View
.GONE
);
256 public int getViewTypeCount() {
261 public boolean hasStableIds() {
266 public boolean isEmpty() {
267 return (mFiles
== null
|| mFiles
.isEmpty());
271 * Change the adapted directory for a new one
272 * @param directory New file to adapt. Can be NULL, meaning
273 * "no content to adapt".
274 * @param updatedStorageManager Optional updated storage manager; used to replace
275 * mStorageManager if is different (and not NULL)
277 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
279 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
280 mStorageManager
= updatedStorageManager
;
281 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
283 if (mStorageManager
!= null
) {
284 mFiles
= mStorageManager
.getFolderContent(mFile
);
286 mFiles
= getFolders(mFiles
);
291 notifyDataSetChanged();
296 * Filter for getting only the folders
298 * @return Vector<OCFile>
300 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
301 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
302 OCFile current
= null
;
303 for (int i
=0; i
<files
.size(); i
++) {
304 current
= files
.get(i
);
305 if (current
.isFolder()) {
314 * Check if parent folder does not include 'S' permission and if file/folder
317 * @param file: OCFile
318 * @return boolean: True if it is shared with me and false if it is not
320 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
321 return (mFile
.getPermissions() != null
322 && !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
323 && file
.getPermissions() != null
324 && file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));