103563209cbfc47b59d9de09ff58ce726b0f81aa
[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.lib.common.utils.Log_OC;
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 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
168 localStateView.setImageResource(R.drawable.downloading_file_indicator);
169 localStateView.setVisibility(View.VISIBLE);
170 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
171 localStateView.setImageResource(R.drawable.uploading_file_indicator);
172 localStateView.setVisibility(View.VISIBLE);
173 } else if (file.isDown()) {
174 localStateView.setImageResource(R.drawable.local_file_indicator);
175 localStateView.setVisibility(View.VISIBLE);
176 } else {
177 localStateView.setVisibility(View.INVISIBLE);
178 }
179
180 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
181 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
182 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
183
184 if (!file.isFolder()) {
185 fileSizeV.setVisibility(View.VISIBLE);
186 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
187 lastModV.setVisibility(View.VISIBLE);
188 lastModV.setText(showRelativeTimestamp(file));
189 // this if-else is needed even thoe fav icon is visible by default
190 // because android reuses views in listview
191 if (!file.keepInSync()) {
192 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
193 } else {
194 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
195 }
196
197 ListView parentList = (ListView)parent;
198 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
199 checkBoxV.setVisibility(View.GONE);
200 } else {
201 if (parentList.isItemChecked(position)) {
202 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
203 } else {
204 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
205 }
206 checkBoxV.setVisibility(View.VISIBLE);
207 }
208
209 // get Thumbnail if file is image
210 if (file.isImage() && file.getRemoteId() != null){
211 // Thumbnail in Cache?
212 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
213 String.valueOf(file.getRemoteId())
214 );
215 if (thumbnail != null && !file.needsUpdateThumbnail()){
216 fileIcon.setImageBitmap(thumbnail);
217 } else {
218 // generate new Thumbnail
219 if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
220 final ThumbnailsCacheManager.ThumbnailGenerationTask task =
221 new ThumbnailsCacheManager.ThumbnailGenerationTask(
222 fileIcon, mStorageManager, mAccount
223 );
224 if (thumbnail == null) {
225 thumbnail = ThumbnailsCacheManager.mDefaultImg;
226 }
227 final AsyncDrawable asyncDrawable = new AsyncDrawable(
228 mContext.getResources(),
229 thumbnail,
230 task
231 );
232 fileIcon.setImageDrawable(asyncDrawable);
233 task.execute(file);
234 }
235 }
236 } else {
237 fileIcon.setImageResource(
238 DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())
239 );
240 }
241
242 if (checkIfFileIsSharedWithMe(file)) {
243 sharedWithMeIconV.setVisibility(View.VISIBLE);
244 }
245 }
246 else {
247 // TODO Re-enable when server supports folder-size calculation
248 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
249 // fileSizeV.setVisibility(View.VISIBLE);
250 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
251 // } else {
252 fileSizeV.setVisibility(View.INVISIBLE);
253 // }
254
255 lastModV.setVisibility(View.VISIBLE);
256 lastModV.setText(showRelativeTimestamp(file));
257 checkBoxV.setVisibility(View.GONE);
258 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
259
260 if (checkIfFileIsSharedWithMe(file)) {
261 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
262 sharedWithMeIconV.setVisibility(View.VISIBLE);
263 } else {
264 fileIcon.setImageResource(
265 DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())
266 );
267 }
268
269 // If folder is sharedByLink, icon folder must be changed to
270 // folder-public one
271 if (file.isShareByLink()) {
272 fileIcon.setImageResource(R.drawable.folder_public);
273 }
274 }
275
276 if (file.isShareByLink()) {
277 sharedIconV.setVisibility(View.VISIBLE);
278 } else {
279 sharedIconV.setVisibility(View.GONE);
280 }
281 }
282
283 return view;
284 }
285
286 /**
287 * Local Folder size in human readable format
288 *
289 * @param path
290 * String
291 * @return Size in human readable format
292 */
293 private String getFolderSizeHuman(String path) {
294
295 File dir = new File(path);
296
297 if (dir.exists()) {
298 long bytes = getFolderSize(dir);
299 return DisplayUtils.bytesToHumanReadable(bytes);
300 }
301
302 return "0 B";
303 }
304
305 /**
306 * Local Folder size
307 * @param dir File
308 * @return Size in bytes
309 */
310 private long getFolderSize(File dir) {
311 if (dir.exists()) {
312 long result = 0;
313 File[] fileList = dir.listFiles();
314 for(int i = 0; i < fileList.length; i++) {
315 if(fileList[i].isDirectory()) {
316 result += getFolderSize(fileList[i]);
317 } else {
318 result += fileList[i].length();
319 }
320 }
321 return result;
322 }
323 return 0;
324 }
325
326 @Override
327 public int getViewTypeCount() {
328 return 1;
329 }
330
331 @Override
332 public boolean hasStableIds() {
333 return true;
334 }
335
336 @Override
337 public boolean isEmpty() {
338 return (mFiles == null || mFiles.isEmpty());
339 }
340
341 /**
342 * Change the adapted directory for a new one
343 * @param directory New file to adapt. Can be NULL, meaning
344 * "no content to adapt".
345 * @param updatedStorageManager Optional updated storage manager; used to replace
346 * mStorageManager if is different (and not NULL)
347 */
348 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
349 mFile = directory;
350 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
351 mStorageManager = updatedStorageManager;
352 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
353 }
354 if (mStorageManager != null) {
355 mFiles = mStorageManager.getFolderContent(mFile);
356 if (mJustFolders) {
357 mFiles = getFolders(mFiles);
358 }
359 } else {
360 mFiles = null;
361 }
362
363 sortDirectory();
364 }
365
366 /**
367 * Sorts all filenames, regarding last user decision
368 */
369 private void sortDirectory(){
370 switch (mSortOrder){
371 case 0:
372 sortByName(mSortAscending);
373 break;
374 case 1:
375 sortByDate(mSortAscending);
376 break;
377 case 2:
378 sortBySize(mSortAscending);
379 break;
380 }
381
382 notifyDataSetChanged();
383 }
384
385
386 /**
387 * Filter for getting only the folders
388 * @param files
389 * @return Vector<OCFile>
390 */
391 public Vector<OCFile> getFolders(Vector<OCFile> files) {
392 Vector<OCFile> ret = new Vector<OCFile>();
393 OCFile current = null;
394 for (int i=0; i<files.size(); i++) {
395 current = files.get(i);
396 if (current.isFolder()) {
397 ret.add(current);
398 }
399 }
400 return ret;
401 }
402
403
404 /**
405 * Check if parent folder does not include 'S' permission and if file/folder
406 * is shared with me
407 *
408 * @param file: OCFile
409 * @return boolean: True if it is shared with me and false if it is not
410 */
411 private boolean checkIfFileIsSharedWithMe(OCFile file) {
412 return (mFile.getPermissions() != null
413 && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
414 && file.getPermissions() != null
415 && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
416 }
417
418 /**
419 * Sorts list by Date
420 * @param sortAscending true: ascending, false: descending
421 */
422 private void sortByDate(boolean sortAscending){
423 final Integer val;
424 if (sortAscending){
425 val = 1;
426 } else {
427 val = -1;
428 }
429
430 Collections.sort(mFiles, new Comparator<OCFile>() {
431 public int compare(OCFile o1, OCFile o2) {
432 if (o1.isFolder() && o2.isFolder()) {
433 Long obj1 = o1.getModificationTimestamp();
434 return val * obj1.compareTo(o2.getModificationTimestamp());
435 }
436 else if (o1.isFolder()) {
437 return -1;
438 } else if (o2.isFolder()) {
439 return 1;
440 } else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
441 return 0;
442 } else {
443 Long obj1 = o1.getModificationTimestamp();
444 return val * obj1.compareTo(o2.getModificationTimestamp());
445 }
446 }
447 });
448 }
449
450 /**
451 * Sorts list by Size
452 * @param sortAscending true: ascending, false: descending
453 */
454 private void sortBySize(boolean sortAscending){
455 final Integer val;
456 if (sortAscending){
457 val = 1;
458 } else {
459 val = -1;
460 }
461
462 Collections.sort(mFiles, new Comparator<OCFile>() {
463 public int compare(OCFile o1, OCFile o2) {
464 if (o1.isFolder() && o2.isFolder()) {
465 Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
466 return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
467 }
468 else if (o1.isFolder()) {
469 return -1;
470 } else if (o2.isFolder()) {
471 return 1;
472 } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
473 return 0;
474 } else {
475 Long obj1 = o1.getFileLength();
476 return val * obj1.compareTo(o2.getFileLength());
477 }
478 }
479 });
480 }
481
482 /**
483 * Sorts list by Name
484 * @param sortAscending true: ascending, false: descending
485 */
486 private void sortByName(boolean sortAscending){
487 final Integer val;
488 if (sortAscending){
489 val = 1;
490 } else {
491 val = -1;
492 }
493
494 Collections.sort(mFiles, new Comparator<OCFile>() {
495 public int compare(OCFile o1, OCFile o2) {
496 if (o1.isFolder() && o2.isFolder()) {
497 return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
498 } else if (o1.isFolder()) {
499 return -1;
500 } else if (o2.isFolder()) {
501 return 1;
502 }
503 return val * new AlphanumComparator().compare(o1, o2);
504 }
505 });
506 }
507
508 public void setSortOrder(Integer order, boolean ascending) {
509 SharedPreferences.Editor editor = mAppPreferences.edit();
510 editor.putInt("sortOrder", order);
511 editor.putBoolean("sortAscending", ascending);
512 editor.commit();
513
514 mSortOrder = order;
515 mSortAscending = ascending;
516
517 sortDirectory();
518 }
519
520 private CharSequence showRelativeTimestamp(OCFile file){
521 return DisplayUtils.getRelativeDateTimeString(mContext, file.getModificationTimestamp(),
522 DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
523 }
524 }