c89ef3bf593349cd41413830a81838651ca0d2eb
[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
21 import java.io.File;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.Vector;
25
26 import third_parties.daveKoeller.AlphanumComparator;
27 import android.accounts.Account;
28 import android.content.Context;
29 import android.content.SharedPreferences;
30 import android.graphics.Bitmap;
31 import android.preference.PreferenceManager;
32 import android.text.format.DateUtils;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.BaseAdapter;
37 import android.widget.ImageView;
38 import android.widget.ListAdapter;
39 import android.widget.ListView;
40 import android.widget.TextView;
41
42 import com.owncloud.android.R;
43 import com.owncloud.android.authentication.AccountUtils;
44 import com.owncloud.android.datamodel.FileDataStorageManager;
45 import com.owncloud.android.datamodel.OCFile;
46 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
47 import com.owncloud.android.datamodel.ThumbnailsCacheManager.AsyncDrawable;
48 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
49 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
50 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
51 import com.owncloud.android.ui.activity.ComponentsGetter;
52 import com.owncloud.android.utils.DisplayUtils;
53 import com.owncloud.android.utils.FileStorageUtils;
54
55
56 /**
57 * This Adapter populates a ListView with all files and folders in an ownCloud
58 * instance.
59 *
60 * @author Bartek Przybylski
61 * @author Tobias Kaminsky
62 * @author David A. Velasco
63 */
64 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
65 private final static String PERMISSION_SHARED_WITH_ME = "S";
66
67 private Context mContext;
68 private OCFile mFile = null;
69 private Vector<OCFile> mFiles = null;
70 private boolean mJustFolders;
71
72 private FileDataStorageManager mStorageManager;
73 private Account mAccount;
74 private ComponentsGetter mTransferServiceGetter;
75 private Integer mSortOrder;
76 public static final Integer SORT_NAME = 0;
77 public static final Integer SORT_DATE = 1;
78 public static final Integer SORT_SIZE = 2;
79 private Boolean mSortAscending;
80 private SharedPreferences mAppPreferences;
81
82 public FileListListAdapter(
83 boolean justFolders,
84 Context context,
85 ComponentsGetter transferServiceGetter
86 ) {
87
88 mJustFolders = justFolders;
89 mContext = context;
90 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
91
92 mTransferServiceGetter = transferServiceGetter;
93
94 mAppPreferences = PreferenceManager
95 .getDefaultSharedPreferences(mContext);
96
97 // Read sorting order, default to sort by name ascending
98 mSortOrder = mAppPreferences
99 .getInt("sortOrder", 0);
100 mSortAscending = mAppPreferences.getBoolean("sortAscending", true);
101
102 // initialise thumbnails cache on background thread
103 new ThumbnailsCacheManager.InitDiskCacheTask().execute();
104
105 }
106
107 @Override
108 public boolean areAllItemsEnabled() {
109 return true;
110 }
111
112 @Override
113 public boolean isEnabled(int position) {
114 return true;
115 }
116
117 @Override
118 public int getCount() {
119 return mFiles != null ? mFiles.size() : 0;
120 }
121
122 @Override
123 public Object getItem(int position) {
124 if (mFiles == null || mFiles.size() <= position)
125 return null;
126 return mFiles.get(position);
127 }
128
129 @Override
130 public long getItemId(int position) {
131 if (mFiles == null || mFiles.size() <= position)
132 return 0;
133 return mFiles.get(position).getFileId();
134 }
135
136 @Override
137 public int getItemViewType(int position) {
138 return 0;
139 }
140
141 @Override
142 public View getView(int position, View convertView, ViewGroup parent) {
143 View view = convertView;
144 if (view == null) {
145 LayoutInflater inflator = (LayoutInflater) mContext
146 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
147 view = inflator.inflate(R.layout.list_item, null);
148 }
149
150 if (mFiles != null && mFiles.size() > position) {
151 OCFile file = mFiles.get(position);
152 TextView fileName = (TextView) view.findViewById(R.id.Filename);
153 String name = file.getFileName();
154
155 fileName.setText(name);
156 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
157 fileIcon.setTag(file.getFileId());
158 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
159 ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
160 sharedWithMeIconV.setVisibility(View.GONE);
161
162 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
163 localStateView.bringToFront();
164 FileDownloaderBinder downloaderBinder =
165 mTransferServiceGetter.getFileDownloaderBinder();
166 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
167 OperationsServiceBinder opsBinder = mTransferServiceGetter.getOperationsServiceBinder();
168 if ((downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
169 (opsBinder != null && opsBinder.isSynchronizing(mAccount, file.getRemotePath()))) {
170 localStateView.setImageResource(R.drawable.downloading_file_indicator);
171 localStateView.setVisibility(View.VISIBLE);
172 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
173 localStateView.setImageResource(R.drawable.uploading_file_indicator);
174 localStateView.setVisibility(View.VISIBLE);
175 } else if (file.isDown()) {
176 localStateView.setImageResource(R.drawable.local_file_indicator);
177 localStateView.setVisibility(View.VISIBLE);
178 } else {
179 localStateView.setVisibility(View.INVISIBLE);
180 }
181
182 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
183 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
184 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
185
186 if (!file.isFolder()) {
187 fileSizeV.setVisibility(View.VISIBLE);
188 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
189 lastModV.setVisibility(View.VISIBLE);
190 lastModV.setText(showRelativeTimestamp(file));
191 // this if-else is needed even thoe fav icon is visible by default
192 // because android reuses views in listview
193 if (!file.keepInSync()) {
194 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
195 } else {
196 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
197 }
198
199 ListView parentList = (ListView)parent;
200 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
201 checkBoxV.setVisibility(View.GONE);
202 } else {
203 if (parentList.isItemChecked(position)) {
204 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
205 } else {
206 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
207 }
208 checkBoxV.setVisibility(View.VISIBLE);
209 }
210
211 // get Thumbnail if file is image
212 if (file.isImage() && file.getRemoteId() != null){
213 // Thumbnail in Cache?
214 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
215 String.valueOf(file.getRemoteId())
216 );
217 if (thumbnail != null && !file.needsUpdateThumbnail()){
218 fileIcon.setImageBitmap(thumbnail);
219 } else {
220 // generate new Thumbnail
221 if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
222 final ThumbnailsCacheManager.ThumbnailGenerationTask task =
223 new ThumbnailsCacheManager.ThumbnailGenerationTask(
224 fileIcon, mStorageManager, mAccount
225 );
226 if (thumbnail == null) {
227 thumbnail = ThumbnailsCacheManager.mDefaultImg;
228 }
229 final AsyncDrawable asyncDrawable = new AsyncDrawable(
230 mContext.getResources(),
231 thumbnail,
232 task
233 );
234 fileIcon.setImageDrawable(asyncDrawable);
235 task.execute(file);
236 }
237 }
238 } else {
239 fileIcon.setImageResource(
240 DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())
241 );
242 }
243
244 if (checkIfFileIsSharedWithMe(file)) {
245 sharedWithMeIconV.setVisibility(View.VISIBLE);
246 }
247 }
248 else {
249 // TODO Re-enable when server supports folder-size calculation
250 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
251 // fileSizeV.setVisibility(View.VISIBLE);
252 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
253 // } else {
254 fileSizeV.setVisibility(View.INVISIBLE);
255 // }
256
257 lastModV.setVisibility(View.VISIBLE);
258 lastModV.setText(showRelativeTimestamp(file));
259 checkBoxV.setVisibility(View.GONE);
260 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
261
262 if (checkIfFileIsSharedWithMe(file)) {
263 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
264 sharedWithMeIconV.setVisibility(View.VISIBLE);
265 } else {
266 fileIcon.setImageResource(
267 DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())
268 );
269 }
270
271 // If folder is sharedByLink, icon folder must be changed to
272 // folder-public one
273 if (file.isShareByLink()) {
274 fileIcon.setImageResource(R.drawable.folder_public);
275 }
276 }
277
278 if (file.isShareByLink()) {
279 sharedIconV.setVisibility(View.VISIBLE);
280 } else {
281 sharedIconV.setVisibility(View.GONE);
282 }
283 }
284
285 return view;
286 }
287
288 /**
289 * Local Folder size in human readable format
290 *
291 * @param path
292 * String
293 * @return Size in human readable format
294 */
295 private String getFolderSizeHuman(String path) {
296
297 File dir = new File(path);
298
299 if (dir.exists()) {
300 long bytes = getFolderSize(dir);
301 return DisplayUtils.bytesToHumanReadable(bytes);
302 }
303
304 return "0 B";
305 }
306
307 /**
308 * Local Folder size
309 * @param dir File
310 * @return Size in bytes
311 */
312 private long getFolderSize(File dir) {
313 if (dir.exists()) {
314 long result = 0;
315 File[] fileList = dir.listFiles();
316 for(int i = 0; i < fileList.length; i++) {
317 if(fileList[i].isDirectory()) {
318 result += getFolderSize(fileList[i]);
319 } else {
320 result += fileList[i].length();
321 }
322 }
323 return result;
324 }
325 return 0;
326 }
327
328 @Override
329 public int getViewTypeCount() {
330 return 1;
331 }
332
333 @Override
334 public boolean hasStableIds() {
335 return true;
336 }
337
338 @Override
339 public boolean isEmpty() {
340 return (mFiles == null || mFiles.isEmpty());
341 }
342
343 /**
344 * Change the adapted directory for a new one
345 * @param directory New file to adapt. Can be NULL, meaning
346 * "no content to adapt".
347 * @param updatedStorageManager Optional updated storage manager; used to replace
348 * mStorageManager if is different (and not NULL)
349 */
350 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
351 mFile = directory;
352 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
353 mStorageManager = updatedStorageManager;
354 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
355 }
356 if (mStorageManager != null) {
357 mFiles = mStorageManager.getFolderContent(mFile);
358 if (mJustFolders) {
359 mFiles = getFolders(mFiles);
360 }
361 } else {
362 mFiles = null;
363 }
364
365 sortDirectory();
366 }
367
368 /**
369 * Sorts all filenames, regarding last user decision
370 */
371 private void sortDirectory(){
372 switch (mSortOrder){
373 case 0:
374 sortByName(mSortAscending);
375 break;
376 case 1:
377 sortByDate(mSortAscending);
378 break;
379 case 2:
380 sortBySize(mSortAscending);
381 break;
382 }
383
384 notifyDataSetChanged();
385 }
386
387
388 /**
389 * Filter for getting only the folders
390 * @param files
391 * @return Vector<OCFile>
392 */
393 public Vector<OCFile> getFolders(Vector<OCFile> files) {
394 Vector<OCFile> ret = new Vector<OCFile>();
395 OCFile current = null;
396 for (int i=0; i<files.size(); i++) {
397 current = files.get(i);
398 if (current.isFolder()) {
399 ret.add(current);
400 }
401 }
402 return ret;
403 }
404
405
406 /**
407 * Check if parent folder does not include 'S' permission and if file/folder
408 * is shared with me
409 *
410 * @param file: OCFile
411 * @return boolean: True if it is shared with me and false if it is not
412 */
413 private boolean checkIfFileIsSharedWithMe(OCFile file) {
414 return (mFile.getPermissions() != null
415 && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
416 && file.getPermissions() != null
417 && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
418 }
419
420 /**
421 * Sorts list by Date
422 * @param sortAscending true: ascending, false: descending
423 */
424 private void sortByDate(boolean sortAscending){
425 final Integer val;
426 if (sortAscending){
427 val = 1;
428 } else {
429 val = -1;
430 }
431
432 Collections.sort(mFiles, new Comparator<OCFile>() {
433 public int compare(OCFile o1, OCFile o2) {
434 if (o1.isFolder() && o2.isFolder()) {
435 Long obj1 = o1.getModificationTimestamp();
436 return val * obj1.compareTo(o2.getModificationTimestamp());
437 }
438 else if (o1.isFolder()) {
439 return -1;
440 } else if (o2.isFolder()) {
441 return 1;
442 } else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
443 return 0;
444 } else {
445 Long obj1 = o1.getModificationTimestamp();
446 return val * obj1.compareTo(o2.getModificationTimestamp());
447 }
448 }
449 });
450 }
451
452 /**
453 * Sorts list by Size
454 * @param sortAscending true: ascending, false: descending
455 */
456 private void sortBySize(boolean sortAscending){
457 final Integer val;
458 if (sortAscending){
459 val = 1;
460 } else {
461 val = -1;
462 }
463
464 Collections.sort(mFiles, new Comparator<OCFile>() {
465 public int compare(OCFile o1, OCFile o2) {
466 if (o1.isFolder() && o2.isFolder()) {
467 Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
468 return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
469 }
470 else if (o1.isFolder()) {
471 return -1;
472 } else if (o2.isFolder()) {
473 return 1;
474 } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
475 return 0;
476 } else {
477 Long obj1 = o1.getFileLength();
478 return val * obj1.compareTo(o2.getFileLength());
479 }
480 }
481 });
482 }
483
484 /**
485 * Sorts list by Name
486 * @param sortAscending true: ascending, false: descending
487 */
488 private void sortByName(boolean sortAscending){
489 final Integer val;
490 if (sortAscending){
491 val = 1;
492 } else {
493 val = -1;
494 }
495
496 Collections.sort(mFiles, new Comparator<OCFile>() {
497 public int compare(OCFile o1, OCFile o2) {
498 if (o1.isFolder() && o2.isFolder()) {
499 return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
500 } else if (o1.isFolder()) {
501 return -1;
502 } else if (o2.isFolder()) {
503 return 1;
504 }
505 return val * new AlphanumComparator().compare(o1, o2);
506 }
507 });
508 }
509
510 public void setSortOrder(Integer order, boolean ascending) {
511 SharedPreferences.Editor editor = mAppPreferences.edit();
512 editor.putInt("sortOrder", order);
513 editor.putBoolean("sortAscending", ascending);
514 editor.commit();
515
516 mSortOrder = order;
517 mSortAscending = ascending;
518
519 sortDirectory();
520 }
521
522 private CharSequence showRelativeTimestamp(OCFile file){
523 return DisplayUtils.getRelativeDateTimeString(mContext, file.getModificationTimestamp(),
524 DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
525 }
526 }