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
.io
.ObjectInputStream
.GetField
;
21 import java
.util
.Vector
;
23 import android
.accounts
.Account
;
24 import android
.content
.Context
;
25 import android
.content
.res
.Resources
;
26 import android
.graphics
.Bitmap
;
27 import android
.graphics
.BitmapFactory
;
28 import android
.media
.ThumbnailUtils
;
29 import android
.util
.TypedValue
;
30 import android
.view
.LayoutInflater
;
31 import android
.view
.View
;
32 import android
.view
.ViewGroup
;
33 import android
.widget
.BaseAdapter
;
34 import android
.widget
.GridView
;
35 import android
.widget
.ImageView
;
36 import android
.widget
.ListAdapter
;
37 import android
.widget
.ListView
;
38 import android
.widget
.TextView
;
40 import com
.owncloud
.android
.R
;
41 import com
.owncloud
.android
.authentication
.AccountUtils
;
42 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
43 import com
.owncloud
.android
.datamodel
.OCFile
;
44 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
45 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
46 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
47 import com
.owncloud
.android
.utils
.DisplayUtils
;
48 import com
.owncloud
.android
.utils
.Log_OC
;
52 * This Adapter populates a ListView with all files and folders in an ownCloud
55 * @author Bartek Przybylski
58 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
59 private final static String PERMISSION_SHARED_WITH_ME
= "S";
61 private Context mContext
;
62 private OCFile mFile
= null
;
63 private Vector
<OCFile
> mFiles
= null
;
65 private FileDataStorageManager mStorageManager
;
66 private Account mAccount
;
67 private ComponentsGetter mTransferServiceGetter
;
69 public FileListListAdapter(Context context
, ComponentsGetter transferServiceGetter
) {
71 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
72 mTransferServiceGetter
= transferServiceGetter
;
76 public boolean areAllItemsEnabled() {
81 public boolean isEnabled(int position
) {
86 public int getCount() {
87 return mFiles
!= null ? mFiles
.size() : 0;
91 public Object
getItem(int position
) {
92 if (mFiles
== null
|| mFiles
.size() <= position
)
94 return mFiles
.get(position
);
98 public long getItemId(int position
) {
99 if (mFiles
== null
|| mFiles
.size() <= position
)
101 return mFiles
.get(position
).getFileId();
105 public int getItemViewType(int position
) {
110 public View
getView(int position
, View convertView
, ViewGroup parent
) {
111 // decide image vs. file view
115 for (OCFile file
: mFiles
){
121 // > 50% Images --> image view
122 boolean fileView
= true
;
123 if ((count
/ mFiles
.size()) >= 0.5){
124 Log_OC
.i("FileListListAdapter", "Image View");
127 Log_OC
.i("FileListListAdapter", "File View");
131 View view
= convertView
;
132 // if (view == null) {
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
.thumbnail
);
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() == GridView
.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 // generate Thumbnail if file is available local and image
204 if (file
.isDown() && file
.isImage()){
205 // Converts dp to pixel
206 Resources r
= mContext
.getResources();
207 int px
= (int) Math
.round(TypedValue
.applyDimension(TypedValue
.COMPLEX_UNIT_DIP
, 150, r
.getDisplayMetrics()));
208 Bitmap bitmap
= BitmapFactory
.decodeFile(file
.getStoragePath());
209 fileIcon
.setImageBitmap(ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
));
211 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
214 if (checkIfFileIsSharedWithMe(file
)) {
215 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
219 fileSizeV
.setVisibility(View
.INVISIBLE
);
220 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
221 lastModV
.setVisibility(View
.VISIBLE
);
222 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
223 checkBoxV
.setVisibility(View
.GONE
);
224 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
226 if (checkIfFileIsSharedWithMe(file
)) {
227 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
228 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
230 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
233 // If folder is sharedByLink, icon folder must be changed to
235 if (file
.isShareByLink()) {
236 fileIcon
.setImageResource(R
.drawable
.folder_public
);
240 if (file
.isShareByLink()) {
241 sharedIconV
.setVisibility(View
.VISIBLE
);
243 sharedIconV
.setVisibility(View
.GONE
);
251 public int getViewTypeCount() {
256 public boolean hasStableIds() {
261 public boolean isEmpty() {
262 return (mFiles
== null
|| mFiles
.isEmpty());
266 * Change the adapted directory for a new one
267 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
268 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
270 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
272 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
273 mStorageManager
= updatedStorageManager
;
274 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
276 if (mStorageManager
!= null
) {
277 mFiles
= mStorageManager
.getFolderContent(mFile
);
281 notifyDataSetChanged();
285 * Check if parent folder does not include 'S' permission and if file/folder
288 * @param file: OCFile
289 * @return boolean: True if it is shared with me and false if it is not
291 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
292 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
293 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));