4050113353a4bdea44c2aa9c58e3ea6e022b67d0
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / FileListListAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.ui.adapter;
19
20 import java.util.Vector;
21
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;
33
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;
44
45
46 /**
47 * This Adapter populates a ListView with all files and folders in an ownCloud
48 * instance.
49 *
50 * @author Bartek Przybylski
51 * @author Tobias Kaminsky
52 * @author David A. Velasco
53 */
54 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
55 private final static String PERMISSION_SHARED_WITH_ME = "S";
56
57 private Context mContext;
58 private OCFile mFile = null;
59 private Vector<OCFile> mFiles = null;
60 private boolean mJustFolders;
61
62 private FileDataStorageManager mStorageManager;
63 private Account mAccount;
64 private ComponentsGetter mTransferServiceGetter;
65
66 public FileListListAdapter(
67 boolean justFolders,
68 Context context,
69 ComponentsGetter transferServiceGetter
70 ) {
71
72 mJustFolders = justFolders;
73 mContext = context;
74 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
75 mTransferServiceGetter = transferServiceGetter;
76
77 // initialise thumbnails cache on background thread
78 new ThumbnailsCacheManager.InitDiskCacheTask().execute();
79 }
80
81 @Override
82 public boolean areAllItemsEnabled() {
83 return true;
84 }
85
86 @Override
87 public boolean isEnabled(int position) {
88 return true;
89 }
90
91 @Override
92 public int getCount() {
93 return mFiles != null ? mFiles.size() : 0;
94 }
95
96 @Override
97 public Object getItem(int position) {
98 if (mFiles == null || mFiles.size() <= position)
99 return null;
100 return mFiles.get(position);
101 }
102
103 @Override
104 public long getItemId(int position) {
105 if (mFiles == null || mFiles.size() <= position)
106 return 0;
107 return mFiles.get(position).getFileId();
108 }
109
110 @Override
111 public int getItemViewType(int position) {
112 return 0;
113 }
114
115 @Override
116 public View getView(int position, View convertView, ViewGroup parent) {
117 View view = convertView;
118 if (view == null) {
119 LayoutInflater inflator = (LayoutInflater) mContext
120 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
121 view = inflator.inflate(R.layout.list_item, null);
122 }
123
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();
128
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);
135
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);
150 } else {
151 localStateView.setVisibility(View.INVISIBLE);
152 }
153
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);
157
158 if (!file.isFolder()) {
159 fileSizeV.setVisibility(View.VISIBLE);
160 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
161 lastModV.setVisibility(View.VISIBLE);
162 lastModV.setText(
163 DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp())
164 );
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);
169 } else {
170 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
171 }
172
173 ListView parentList = (ListView)parent;
174 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
175 checkBoxV.setVisibility(View.GONE);
176 } else {
177 if (parentList.isItemChecked(position)) {
178 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
179 } else {
180 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
181 }
182 checkBoxV.setVisibility(View.VISIBLE);
183 }
184
185 // get Thumbnail if file is image
186 if (file.isImage()){
187 // Thumbnail in Cache?
188 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
189 String.valueOf(file.getRemoteId())
190 );
191 if (thumbnail != null && !file.needsUpdateThumbnail()){
192 fileIcon.setImageBitmap(thumbnail);
193 } else {
194 // generate new Thumbnail
195 if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
196 final ThumbnailsCacheManager.ThumbnailGenerationTask task =
197 new ThumbnailsCacheManager.ThumbnailGenerationTask(
198 fileIcon, mStorageManager
199 );
200 final AsyncDrawable asyncDrawable = new AsyncDrawable(
201 mContext.getResources(),
202 ThumbnailsCacheManager.mDefaultImg,
203 task
204 );
205 fileIcon.setImageDrawable(asyncDrawable);
206 task.execute(file);
207 }
208 }
209 } else {
210 fileIcon.setImageResource(
211 DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())
212 );
213 }
214
215 if (checkIfFileIsSharedWithMe(file)) {
216 sharedWithMeIconV.setVisibility(View.VISIBLE);
217 }
218 }
219 else {
220 fileSizeV.setVisibility(View.INVISIBLE);
221 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
222 lastModV.setVisibility(View.VISIBLE);
223 lastModV.setText(
224 DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp())
225 );
226 checkBoxV.setVisibility(View.GONE);
227 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
228
229 if (checkIfFileIsSharedWithMe(file)) {
230 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
231 sharedWithMeIconV.setVisibility(View.VISIBLE);
232 } else {
233 fileIcon.setImageResource(
234 DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())
235 );
236 }
237
238 // If folder is sharedByLink, icon folder must be changed to
239 // folder-public one
240 if (file.isShareByLink()) {
241 fileIcon.setImageResource(R.drawable.folder_public);
242 }
243 }
244
245 if (file.isShareByLink()) {
246 sharedIconV.setVisibility(View.VISIBLE);
247 } else {
248 sharedIconV.setVisibility(View.GONE);
249 }
250 }
251
252 return view;
253 }
254
255 @Override
256 public int getViewTypeCount() {
257 return 1;
258 }
259
260 @Override
261 public boolean hasStableIds() {
262 return true;
263 }
264
265 @Override
266 public boolean isEmpty() {
267 return (mFiles == null || mFiles.isEmpty());
268 }
269
270 /**
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)
276 */
277 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
278 mFile = directory;
279 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
280 mStorageManager = updatedStorageManager;
281 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
282 }
283 if (mStorageManager != null) {
284 mFiles = mStorageManager.getFolderContent(mFile);
285 if (mJustFolders) {
286 mFiles = getFolders(mFiles);
287 }
288 } else {
289 mFiles = null;
290 }
291 notifyDataSetChanged();
292 }
293
294
295 /**
296 * Filter for getting only the folders
297 * @param files
298 * @return Vector<OCFile>
299 */
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()) {
306 ret.add(current);
307 }
308 }
309 return ret;
310 }
311
312
313 /**
314 * Check if parent folder does not include 'S' permission and if file/folder
315 * is shared with me
316 *
317 * @param file: OCFile
318 * @return boolean: True if it is shared with me and false if it is not
319 */
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));
325 }
326 }