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
.graphics
.BitmapFactory
;
26 import android
.media
.ThumbnailUtils
;
27 import android
.view
.LayoutInflater
;
28 import android
.view
.View
;
29 import android
.view
.ViewGroup
;
30 import android
.widget
.BaseAdapter
;
31 import android
.widget
.GridView
;
32 import android
.widget
.ImageView
;
33 import android
.widget
.ListAdapter
;
34 import android
.widget
.ListView
;
35 import android
.widget
.TextView
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.authentication
.AccountUtils
;
39 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
40 import com
.owncloud
.android
.datamodel
.OCFile
;
41 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
42 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
43 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
44 import com
.owncloud
.android
.utils
.DisplayUtils
;
45 import com
.owncloud
.android
.utils
.Log_OC
;
49 * This Adapter populates a ListView with all files and folders in an ownCloud
52 * @author Bartek Przybylski
55 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
56 private final static String PERMISSION_SHARED_WITH_ME
= "S";
58 private Context mContext
;
59 private OCFile mFile
= null
;
60 private Vector
<OCFile
> mFiles
= null
;
61 private boolean mJustFolders
;
63 private FileDataStorageManager mStorageManager
;
64 private Account mAccount
;
65 private ComponentsGetter mTransferServiceGetter
;
67 public FileListListAdapter(
70 ComponentsGetter transferServiceGetter
72 mJustFolders
= justFolders
;
74 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
75 mTransferServiceGetter
= transferServiceGetter
;
79 public boolean areAllItemsEnabled() {
84 public boolean isEnabled(int position
) {
89 public int getCount() {
90 return mFiles
!= null ? mFiles
.size() : 0;
94 public Object
getItem(int position
) {
95 if (mFiles
== null
|| mFiles
.size() <= position
)
97 return mFiles
.get(position
);
101 public long getItemId(int position
) {
102 if (mFiles
== null
|| mFiles
.size() <= position
)
104 return mFiles
.get(position
).getFileId();
108 public int getItemViewType(int position
) {
113 public View
getView(int position
, View convertView
, ViewGroup parent
) {
114 // decide image vs. file view
118 for (OCFile file
: mFiles
){
124 // > 50% Images --> image view
125 boolean fileView
= true
;
126 if ((count
/ mFiles
.size()) >= 0.8){
132 View view
= convertView
;
133 LayoutInflater inflator
= (LayoutInflater
) mContext
134 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
136 view
= inflator
.inflate(R
.layout
.list_item
, null
);
138 view
= inflator
.inflate(R
.layout
.image_item
, null
);
139 View frame
= view
.findViewById(R
.id
.imageItemFrame
);
140 frame
.setVisibility(View
.GONE
);
145 if (mFiles
!= null
&& mFiles
.size() > position
) {
146 OCFile file
= mFiles
.get(position
);
147 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
148 if (!fileView
){fileName
.setVisibility(View
.GONE
);}
149 String name
= file
.getFileName();
151 fileName
.setText(name
);
152 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
153 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
154 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
155 sharedWithMeIconV
.setVisibility(View
.GONE
);
157 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
158 localStateView
.bringToFront();
159 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
160 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
161 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
162 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
163 localStateView
.setVisibility(View
.VISIBLE
);
164 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
165 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
166 localStateView
.setVisibility(View
.VISIBLE
);
167 } else if (file
.isDown()) {
168 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
169 localStateView
.setVisibility(View
.VISIBLE
);
171 localStateView
.setVisibility(View
.INVISIBLE
);
174 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
175 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
176 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
178 if (!file
.isFolder()) {
179 fileSizeV
.setVisibility(View
.VISIBLE
);
180 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
181 lastModV
.setVisibility(View
.VISIBLE
);
182 lastModV
.setText(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 GridView parentList
= (GridView
)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 if (file
.isImage() && file
.isDown()){
204 Bitmap bitmap
= BitmapFactory
.decodeFile(file
.getStoragePath());
205 fileIcon
.setImageBitmap(ThumbnailUtils
.extractThumbnail(bitmap
, 100, 100));
207 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
210 if (checkIfFileIsSharedWithMe(file
)) {
211 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
216 fileSizeV
.setVisibility(View
.INVISIBLE
);
217 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
218 lastModV
.setVisibility(View
.VISIBLE
);
219 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
220 checkBoxV
.setVisibility(View
.GONE
);
221 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
223 if (checkIfFileIsSharedWithMe(file
)) {
224 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
225 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
227 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
230 // If folder is sharedByLink, icon folder must be changed to
232 if (file
.isShareByLink()) {
233 fileIcon
.setImageResource(R
.drawable
.folder_public
);
237 if (file
.isShareByLink()) {
238 sharedIconV
.setVisibility(View
.VISIBLE
);
240 sharedIconV
.setVisibility(View
.GONE
);
248 public int getViewTypeCount() {
253 public boolean hasStableIds() {
258 public boolean isEmpty() {
259 return (mFiles
== null
|| mFiles
.isEmpty());
263 * Change the adapted directory for a new one
264 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
265 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
267 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
269 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
270 mStorageManager
= updatedStorageManager
;
271 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
273 if (mStorageManager
!= null
) {
274 mFiles
= mStorageManager
.getFolderContent(mFile
);
276 mFiles
= getFolders(mFiles
);
281 notifyDataSetChanged();
286 * Filter for getting only the folders
288 * @return Vector<OCFile>
290 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
291 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
292 OCFile current
= null
;
293 for (int i
=0; i
<files
.size(); i
++) {
294 current
= files
.get(i
);
295 if (current
.isFolder()) {
304 * Check if parent folder does not include 'S' permission and if file/folder
307 * @param file: OCFile
308 * @return boolean: True if it is shared with me and false if it is not
310 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
311 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
312 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));