57efb4e782e9891fd171f53f2ba053c5909d72c9
[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.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
90 mTransferServiceGetter = transferServiceGetter;
91
92 mAppPreferences = PreferenceManager
93 .getDefaultSharedPreferences(mContext);
94
95 // Read sorting order, default to sort by name ascending
96 mSortOrder = mAppPreferences
97 .getInt("sortOrder", 0);
98 mSortAscending = mAppPreferences.getBoolean("sortAscending", true);
99
100 // initialise thumbnails cache on background thread
101 new ThumbnailsCacheManager.InitDiskCacheTask().execute();
102
103 }
104
105 @Override
106 public boolean areAllItemsEnabled() {
107 return true;
108 }
109
110 @Override
111 public boolean isEnabled(int position) {
112 return true;
113 }
114
115 @Override
116 public int getCount() {
117 return mFiles != null ? mFiles.size() : 0;
118 }
119
120 @Override
121 public Object getItem(int position) {
122 if (mFiles == null || mFiles.size() <= position)
123 return null;
124 return mFiles.get(position);
125 }
126
127 @Override
128 public long getItemId(int position) {
129 if (mFiles == null || mFiles.size() <= position)
130 return 0;
131 return mFiles.get(position).getFileId();
132 }
133
134 @Override
135 public int getItemViewType(int position) {
136 return 0;
137 }
138
139 @Override
140 public View getView(int position, View convertView, ViewGroup parent) {
141 View view = convertView;
142 if (view == null) {
143 LayoutInflater inflator = (LayoutInflater) mContext
144 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
145 view = inflator.inflate(R.layout.list_item, null);
146 }
147
148 if (mFiles != null && mFiles.size() > position) {
149 OCFile file = mFiles.get(position);
150 TextView fileName = (TextView) view.findViewById(R.id.Filename);
151 String name = file.getFileName();
152
153 fileName.setText(name);
154 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
155 fileIcon.setTag(file.getFileId());
156 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
157 ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
158 sharedWithMeIconV.setVisibility(View.GONE);
159
160 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
161 localStateView.bringToFront();
162 FileDownloaderBinder downloaderBinder =
163 mTransferServiceGetter.getFileDownloaderBinder();
164 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
165 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
166 localStateView.setImageResource(R.drawable.downloading_file_indicator);
167 localStateView.setVisibility(View.VISIBLE);
168 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
169 localStateView.setImageResource(R.drawable.uploading_file_indicator);
170 localStateView.setVisibility(View.VISIBLE);
171 } else if (file.isDown()) {
172 localStateView.setImageResource(R.drawable.local_file_indicator);
173 localStateView.setVisibility(View.VISIBLE);
174 } else {
175 localStateView.setVisibility(View.INVISIBLE);
176 }
177
178 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
179 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
180 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
181
182 if (!file.isFolder()) {
183 fileSizeV.setVisibility(View.VISIBLE);
184 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
185 lastModV.setVisibility(View.VISIBLE);
186 lastModV.setText(showRelativeTimestamp(file));
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
217 ThumbnailsCacheManager.AsyncTaskFile asyncTaskFile = new ThumbnailsCacheManager.AsyncTaskOCFile(file);
218 // generate new Thumbnail
219 if (ThumbnailsCacheManager.cancelPotentialGlobalWork(asyncTaskFile, fileIcon)) {
220 final ThumbnailsCacheManager.ThumbnailGenerationGlobalTask task =
221 new ThumbnailsCacheManager.ThumbnailGenerationGlobalTask(
222 fileIcon, mStorageManager, mAccount
223 );
224 if (thumbnail == null) {
225 thumbnail = ThumbnailsCacheManager.mDefaultImg;
226 }
227 final ThumbnailsCacheManager.AsyncGlobalDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncGlobalDrawable(
228 mContext.getResources(),
229 thumbnail,
230 task
231 );
232 fileIcon.setImageDrawable(asyncDrawable);
233 task.execute(asyncTaskFile);
234 }
235 }
236 } else {
237 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
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(showRelativeTimestamp(file));
255 checkBoxV.setVisibility(View.GONE);
256 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
257
258 if (checkIfFileIsSharedWithMe(file)) {
259 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
260 sharedWithMeIconV.setVisibility(View.VISIBLE);
261 } else {
262 fileIcon.setImageResource(
263 DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())
264 );
265 }
266
267 // If folder is sharedByLink, icon folder must be changed to
268 // folder-public one
269 if (file.isShareByLink()) {
270 fileIcon.setImageResource(R.drawable.folder_public);
271 }
272 }
273
274 if (file.isShareByLink()) {
275 sharedIconV.setVisibility(View.VISIBLE);
276 } else {
277 sharedIconV.setVisibility(View.GONE);
278 }
279 }
280
281 return view;
282 }
283
284 /**
285 * Local Folder size in human readable format
286 *
287 * @param path
288 * String
289 * @return Size in human readable format
290 */
291 private String getFolderSizeHuman(String path) {
292
293 File dir = new File(path);
294
295 if (dir.exists()) {
296 long bytes = getFolderSize(dir);
297 return DisplayUtils.bytesToHumanReadable(bytes);
298 }
299
300 return "0 B";
301 }
302
303 /**
304 * Local Folder size
305 * @param dir File
306 * @return Size in bytes
307 */
308 private long getFolderSize(File dir) {
309 if (dir.exists()) {
310 long result = 0;
311 File[] fileList = dir.listFiles();
312 for(int i = 0; i < fileList.length; i++) {
313 if(fileList[i].isDirectory()) {
314 result += getFolderSize(fileList[i]);
315 } else {
316 result += fileList[i].length();
317 }
318 }
319 return result;
320 }
321 return 0;
322 }
323
324 @Override
325 public int getViewTypeCount() {
326 return 1;
327 }
328
329 @Override
330 public boolean hasStableIds() {
331 return true;
332 }
333
334 @Override
335 public boolean isEmpty() {
336 return (mFiles == null || mFiles.isEmpty());
337 }
338
339 /**
340 * Change the adapted directory for a new one
341 * @param directory New file to adapt. Can be NULL, meaning
342 * "no content to adapt".
343 * @param updatedStorageManager Optional updated storage manager; used to replace
344 * mStorageManager if is different (and not NULL)
345 */
346 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
347 mFile = directory;
348 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
349 mStorageManager = updatedStorageManager;
350 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
351 }
352 if (mStorageManager != null) {
353 mFiles = mStorageManager.getFolderContent(mFile);
354 if (mJustFolders) {
355 mFiles = getFolders(mFiles);
356 }
357 } else {
358 mFiles = null;
359 }
360
361 sortDirectory();
362 }
363
364 /**
365 * Sorts all filenames, regarding last user decision
366 */
367 private void sortDirectory(){
368 switch (mSortOrder){
369 case 0:
370 sortByName(mSortAscending);
371 break;
372 case 1:
373 sortByDate(mSortAscending);
374 break;
375 case 2:
376 sortBySize(mSortAscending);
377 break;
378 }
379
380 notifyDataSetChanged();
381 }
382
383
384 /**
385 * Filter for getting only the folders
386 * @param files
387 * @return Vector<OCFile>
388 */
389 public Vector<OCFile> getFolders(Vector<OCFile> files) {
390 Vector<OCFile> ret = new Vector<OCFile>();
391 OCFile current = null;
392 for (int i=0; i<files.size(); i++) {
393 current = files.get(i);
394 if (current.isFolder()) {
395 ret.add(current);
396 }
397 }
398 return ret;
399 }
400
401
402 /**
403 * Check if parent folder does not include 'S' permission and if file/folder
404 * is shared with me
405 *
406 * @param file: OCFile
407 * @return boolean: True if it is shared with me and false if it is not
408 */
409 private boolean checkIfFileIsSharedWithMe(OCFile file) {
410 return (mFile.getPermissions() != null
411 && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
412 && file.getPermissions() != null
413 && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
414 }
415
416 /**
417 * Sorts list by Date
418 * @param sortAscending true: ascending, false: descending
419 */
420 private void sortByDate(boolean sortAscending){
421 final Integer val;
422 if (sortAscending){
423 val = 1;
424 } else {
425 val = -1;
426 }
427
428 Collections.sort(mFiles, new Comparator<OCFile>() {
429 public int compare(OCFile o1, OCFile o2) {
430 if (o1.isFolder() && o2.isFolder()) {
431 Long obj1 = o1.getModificationTimestamp();
432 return val * obj1.compareTo(o2.getModificationTimestamp());
433 }
434 else if (o1.isFolder()) {
435 return -1;
436 } else if (o2.isFolder()) {
437 return 1;
438 } else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
439 return 0;
440 } else {
441 Long obj1 = o1.getModificationTimestamp();
442 return val * obj1.compareTo(o2.getModificationTimestamp());
443 }
444 }
445 });
446 }
447
448 /**
449 * Sorts list by Size
450 * @param sortAscending true: ascending, false: descending
451 */
452 private void sortBySize(boolean sortAscending){
453 final Integer val;
454 if (sortAscending){
455 val = 1;
456 } else {
457 val = -1;
458 }
459
460 Collections.sort(mFiles, new Comparator<OCFile>() {
461 public int compare(OCFile o1, OCFile o2) {
462 if (o1.isFolder() && o2.isFolder()) {
463 Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
464 return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
465 }
466 else if (o1.isFolder()) {
467 return -1;
468 } else if (o2.isFolder()) {
469 return 1;
470 } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
471 return 0;
472 } else {
473 Long obj1 = o1.getFileLength();
474 return val * obj1.compareTo(o2.getFileLength());
475 }
476 }
477 });
478 }
479
480 /**
481 * Sorts list by Name
482 * @param sortAscending true: ascending, false: descending
483 */
484 private void sortByName(boolean sortAscending){
485 final Integer val;
486 if (sortAscending){
487 val = 1;
488 } else {
489 val = -1;
490 }
491
492 Collections.sort(mFiles, new Comparator<OCFile>() {
493 public int compare(OCFile o1, OCFile o2) {
494 if (o1.isFolder() && o2.isFolder()) {
495 return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
496 } else if (o1.isFolder()) {
497 return -1;
498 } else if (o2.isFolder()) {
499 return 1;
500 }
501 return val * new AlphanumComparator().compare(o1, o2);
502 }
503 });
504 }
505
506 public void setSortOrder(Integer order, boolean ascending) {
507 SharedPreferences.Editor editor = mAppPreferences.edit();
508 editor.putInt("sortOrder", order);
509 editor.putBoolean("sortAscending", ascending);
510 editor.commit();
511
512 mSortOrder = order;
513 mSortAscending = ascending;
514
515 sortDirectory();
516 }
517
518 private CharSequence showRelativeTimestamp(OCFile file){
519 return DisplayUtils.getRelativeDateTimeString(mContext, file.getModificationTimestamp(),
520 DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
521 }
522 }