9a2a0d3af31cd9e5b75f3531886f15f392585d5e
[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.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.BaseAdapter;
36 import android.widget.ImageView;
37 import android.widget.ListAdapter;
38 import android.widget.ListView;
39 import android.widget.TextView;
40
41 import com.owncloud.android.R;
42 import com.owncloud.android.authentication.AccountUtils;
43 import com.owncloud.android.datamodel.FileDataStorageManager;
44 import com.owncloud.android.datamodel.OCFile;
45 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
46 import com.owncloud.android.datamodel.ThumbnailsCacheManager.AsyncDrawable;
47 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
48 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
49 import com.owncloud.android.ui.activity.ComponentsGetter;
50 import com.owncloud.android.utils.DisplayUtils;
51 import com.owncloud.android.utils.FileStorageUtils;
52
53
54 /**
55 * This Adapter populates a ListView with all files and folders in an ownCloud
56 * instance.
57 *
58 * @author Bartek Przybylski
59 * @author Tobias Kaminsky
60 * @author David A. Velasco
61 */
62 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
63 private final static String PERMISSION_SHARED_WITH_ME = "S";
64
65 private Context mContext;
66 private OCFile mFile = null;
67 private Vector<OCFile> mFiles = null;
68 private boolean mJustFolders;
69
70 private FileDataStorageManager mStorageManager;
71 private Account mAccount;
72 private ComponentsGetter mTransferServiceGetter;
73 private Integer mSortOrder;
74 public static final Integer SORT_NAME = 0;
75 public static final Integer SORT_DATE = 1;
76 public static final Integer SORT_SIZE = 2;
77 private Boolean mSortAscending;
78 private SharedPreferences mAppPreferences;
79
80 public FileListListAdapter(
81 boolean justFolders,
82 Context context,
83 ComponentsGetter transferServiceGetter
84 ) {
85
86 mJustFolders = justFolders;
87 mContext = context;
88 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
89 mTransferServiceGetter = transferServiceGetter;
90
91 mAppPreferences = PreferenceManager
92 .getDefaultSharedPreferences(mContext);
93
94 // Read sorting order, default to sort by name ascending
95 mSortOrder = mAppPreferences
96 .getInt("sortOrder", 0);
97 mSortAscending = mAppPreferences.getBoolean("sortAscending", true);
98
99 // initialise thumbnails cache on background thread
100 new ThumbnailsCacheManager.InitDiskCacheTask().execute();
101 }
102
103 @Override
104 public boolean areAllItemsEnabled() {
105 return true;
106 }
107
108 @Override
109 public boolean isEnabled(int position) {
110 return true;
111 }
112
113 @Override
114 public int getCount() {
115 return mFiles != null ? mFiles.size() : 0;
116 }
117
118 @Override
119 public Object getItem(int position) {
120 if (mFiles == null || mFiles.size() <= position)
121 return null;
122 return mFiles.get(position);
123 }
124
125 @Override
126 public long getItemId(int position) {
127 if (mFiles == null || mFiles.size() <= position)
128 return 0;
129 return mFiles.get(position).getFileId();
130 }
131
132 @Override
133 public int getItemViewType(int position) {
134 return 0;
135 }
136
137 @Override
138 public View getView(int position, View convertView, ViewGroup parent) {
139 View view = convertView;
140 if (view == null) {
141 LayoutInflater inflator = (LayoutInflater) mContext
142 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
143 view = inflator.inflate(R.layout.list_item, null);
144 }
145
146 if (mFiles != null && mFiles.size() > position) {
147 OCFile file = mFiles.get(position);
148 TextView fileName = (TextView) view.findViewById(R.id.Filename);
149 String name = file.getFileName();
150
151 fileName.setText(name);
152 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
153 fileIcon.setTag(file.getFileId());
154 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
155 ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
156 sharedWithMeIconV.setVisibility(View.GONE);
157
158 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
159 localStateView.bringToFront();
160 FileDownloaderBinder downloaderBinder =
161 mTransferServiceGetter.getFileDownloaderBinder();
162 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
163 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
164 localStateView.setImageResource(R.drawable.downloading_file_indicator);
165 localStateView.setVisibility(View.VISIBLE);
166 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
167 localStateView.setImageResource(R.drawable.uploading_file_indicator);
168 localStateView.setVisibility(View.VISIBLE);
169 } else if (file.isDown()) {
170 localStateView.setImageResource(R.drawable.local_file_indicator);
171 localStateView.setVisibility(View.VISIBLE);
172 } else {
173 localStateView.setVisibility(View.INVISIBLE);
174 }
175
176 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
177 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
178 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
179
180 if (!file.isFolder()) {
181 fileSizeV.setVisibility(View.VISIBLE);
182 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
183 lastModV.setVisibility(View.VISIBLE);
184 lastModV.setText(
185 DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp())
186 );
187 // this if-else is needed even thoe fav icon is visible by default
188 // because android reuses views in listview
189 if (!file.keepInSync()) {
190 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
191 } else {
192 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
193 }
194
195 ListView parentList = (ListView)parent;
196 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
197 checkBoxV.setVisibility(View.GONE);
198 } else {
199 if (parentList.isItemChecked(position)) {
200 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
201 } else {
202 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
203 }
204 checkBoxV.setVisibility(View.VISIBLE);
205 }
206
207 // get Thumbnail if file is image
208 if (file.isImage() && file.getRemoteId() != null){
209 // Thumbnail in Cache?
210 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
211 String.valueOf(file.getRemoteId())
212 );
213 if (thumbnail != null && !file.needsUpdateThumbnail()){
214 fileIcon.setImageBitmap(thumbnail);
215 } else {
216 // generate new Thumbnail
217 if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
218 final ThumbnailsCacheManager.ThumbnailGenerationTask task =
219 new ThumbnailsCacheManager.ThumbnailGenerationTask(
220 fileIcon, mStorageManager
221 );
222 if (thumbnail == null) {
223 thumbnail = ThumbnailsCacheManager.mDefaultImg;
224 }
225 final AsyncDrawable asyncDrawable = new AsyncDrawable(
226 mContext.getResources(),
227 thumbnail,
228 task
229 );
230 fileIcon.setImageDrawable(asyncDrawable);
231 task.execute(file);
232 }
233 }
234 } else {
235 fileIcon.setImageResource(
236 DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())
237 );
238 }
239
240 if (checkIfFileIsSharedWithMe(file)) {
241 sharedWithMeIconV.setVisibility(View.VISIBLE);
242 }
243 }
244 else {
245 // TODO Re-enable when server supports folder-size calculation
246 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
247 // fileSizeV.setVisibility(View.VISIBLE);
248 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
249 // } else {
250 fileSizeV.setVisibility(View.INVISIBLE);
251 // }
252
253 lastModV.setVisibility(View.VISIBLE);
254 lastModV.setText(
255 DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp())
256 );
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 }