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
.ImageView
;
35 import android
.widget
.ListAdapter
;
36 import android
.widget
.ListView
;
37 import android
.widget
.TextView
;
39 import com
.owncloud
.android
.R
;
40 import com
.owncloud
.android
.authentication
.AccountUtils
;
41 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
42 import com
.owncloud
.android
.datamodel
.OCFile
;
43 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
44 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
45 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
46 import com
.owncloud
.android
.utils
.DisplayUtils
;
47 import com
.owncloud
.android
.utils
.Log_OC
;
51 * This Adapter populates a ListView with all files and folders in an ownCloud
54 * @author Bartek Przybylski
57 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
58 private final static String PERMISSION_SHARED_WITH_ME
= "S";
60 private Context mContext
;
61 private OCFile mFile
= null
;
62 private Vector
<OCFile
> mFiles
= null
;
64 private FileDataStorageManager mStorageManager
;
65 private Account mAccount
;
66 private ComponentsGetter mTransferServiceGetter
;
68 public FileListListAdapter(Context context
, ComponentsGetter transferServiceGetter
) {
70 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
71 mTransferServiceGetter
= transferServiceGetter
;
75 public boolean areAllItemsEnabled() {
80 public boolean isEnabled(int position
) {
85 public int getCount() {
86 return mFiles
!= null ? mFiles
.size() : 0;
90 public Object
getItem(int position
) {
91 if (mFiles
== null
|| mFiles
.size() <= position
)
93 return mFiles
.get(position
);
97 public long getItemId(int position
) {
98 if (mFiles
== null
|| mFiles
.size() <= position
)
100 return mFiles
.get(position
).getFileId();
104 public int getItemViewType(int position
) {
109 public View
getView(int position
, View convertView
, ViewGroup parent
) {
112 View view
= convertView
;
114 LayoutInflater inflator
= (LayoutInflater
) mContext
115 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
116 view
= inflator
.inflate(R
.layout
.list_item
, null
);
120 if (mFiles
!= null
&& mFiles
.size() > position
) {
121 OCFile file
= mFiles
.get(position
);
122 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
123 fileName
.setVisibility(View
.GONE
);
124 String name
= file
.getFileName();
126 fileName
.setText(name
);
127 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
128 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
129 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
130 sharedWithMeIconV
.setVisibility(View
.GONE
);
132 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
133 localStateView
.bringToFront();
134 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
135 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
136 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
137 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
138 localStateView
.setVisibility(View
.VISIBLE
);
139 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
140 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
141 localStateView
.setVisibility(View
.VISIBLE
);
142 } else if (file
.isDown()) {
143 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
144 localStateView
.setVisibility(View
.VISIBLE
);
146 localStateView
.setVisibility(View
.INVISIBLE
);
149 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
150 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
151 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
153 if (!file
.isFolder()) {
154 fileSizeV
.setVisibility(View
.VISIBLE
);
155 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
156 lastModV
.setVisibility(View
.VISIBLE
);
157 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
158 // this if-else is needed even thoe fav icon is visible by default
159 // because android reuses views in listview
160 if (!file
.keepInSync()) {
161 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
163 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
166 ListView parentList
= (ListView
)parent
;
167 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
168 checkBoxV
.setVisibility(View
.GONE
);
170 if (parentList
.isItemChecked(position
)) {
171 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
173 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
175 checkBoxV
.setVisibility(View
.VISIBLE
);
178 // generate Thumbnail if file is available local and image
179 if (file
.isDown() && file
.isImage()){
180 // Converts dp to pixel
181 Resources r
= mContext
.getResources();
182 int px
= (int) Math
.round(TypedValue
.applyDimension(TypedValue
.COMPLEX_UNIT_DIP
, 150, r
.getDisplayMetrics()));
183 Bitmap bitmap
= BitmapFactory
.decodeFile(file
.getStoragePath());
184 fileIcon
.setImageBitmap(ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
));
186 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
189 if (checkIfFileIsSharedWithMe(file
)) {
190 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
194 fileSizeV
.setVisibility(View
.INVISIBLE
);
195 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
196 lastModV
.setVisibility(View
.VISIBLE
);
197 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
198 checkBoxV
.setVisibility(View
.GONE
);
199 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
201 if (checkIfFileIsSharedWithMe(file
)) {
202 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
203 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
205 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
208 // If folder is sharedByLink, icon folder must be changed to
210 if (file
.isShareByLink()) {
211 fileIcon
.setImageResource(R
.drawable
.folder_public
);
215 if (file
.isShareByLink()) {
216 sharedIconV
.setVisibility(View
.VISIBLE
);
218 sharedIconV
.setVisibility(View
.GONE
);
226 public int getViewTypeCount() {
231 public boolean hasStableIds() {
236 public boolean isEmpty() {
237 return (mFiles
== null
|| mFiles
.isEmpty());
241 * Change the adapted directory for a new one
242 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
243 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
245 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
247 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
248 mStorageManager
= updatedStorageManager
;
249 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
251 if (mStorageManager
!= null
) {
252 mFiles
= mStorageManager
.getFolderContent(mFile
);
256 notifyDataSetChanged();
260 * Check if parent folder does not include 'S' permission and if file/folder
263 * @param file: OCFile
264 * @return boolean: True if it is shared with me and false if it is not
266 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
267 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
268 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));