836ecccca2eed1f6804025d4ad022554f4419cfb
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / FileListListAdapter.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author Tobias Kaminsky
6 * @author David A. Velasco
7 * @author masensio
8 * Copyright (C) 2011 Bartek Przybylski
9 * Copyright (C) 2015 ownCloud Inc.
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24 package com.owncloud.android.ui.adapter;
25
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Vector;
32
33 import android.accounts.Account;
34 import android.content.Context;
35 import android.content.SharedPreferences;
36 import android.graphics.Bitmap;
37 import android.graphics.Color;
38 import android.graphics.BitmapFactory;
39 import android.graphics.Canvas;
40 import android.graphics.Paint;
41 import android.os.Build;
42 import android.preference.PreferenceManager;
43 import android.text.format.DateUtils;
44 import android.view.LayoutInflater;
45 import android.view.View;
46 import android.view.ViewGroup;
47 import android.widget.AbsListView;
48 import android.widget.BaseAdapter;
49 import android.widget.ImageView;
50 import android.widget.LinearLayout;
51 import android.widget.ListAdapter;
52 import android.widget.TextView;
53
54 import com.owncloud.android.R;
55 import com.owncloud.android.authentication.AccountUtils;
56 import com.owncloud.android.datamodel.FileDataStorageManager;
57 import com.owncloud.android.datamodel.OCFile;
58 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
59 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
60 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
61 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
62 import com.owncloud.android.ui.activity.ComponentsGetter;
63 import com.owncloud.android.utils.DisplayUtils;
64 import com.owncloud.android.utils.FileStorageUtils;
65 import com.owncloud.android.utils.MimetypeIconUtil;
66
67
68 /**
69 * This Adapter populates a ListView with all files and folders in an ownCloud
70 * instance.
71 */
72 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
73 private final static String PERMISSION_SHARED_WITH_ME = "S";
74
75 private Context mContext;
76 private OCFile mFile = null;
77 private Vector<OCFile> mFiles = null;
78 private Vector<OCFile> mFilesOrig = new Vector<OCFile>();
79 private boolean mJustFolders;
80
81 private FileDataStorageManager mStorageManager;
82 private Account mAccount;
83 private ComponentsGetter mTransferServiceGetter;
84 private boolean mGridMode;
85
86 private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM };
87
88 private SharedPreferences mAppPreferences;
89
90 private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
91
92 public FileListListAdapter(
93 boolean justFolders,
94 Context context,
95 ComponentsGetter transferServiceGetter
96 ) {
97
98 mJustFolders = justFolders;
99 mContext = context;
100 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
101 mTransferServiceGetter = transferServiceGetter;
102
103 mAppPreferences = PreferenceManager
104 .getDefaultSharedPreferences(mContext);
105
106 // Read sorting order, default to sort by name ascending
107 FileStorageUtils.mSortOrder = mAppPreferences.getInt("sortOrder", 0);
108 FileStorageUtils.mSortAscending = mAppPreferences.getBoolean("sortAscending", true);
109
110 // initialise thumbnails cache on background thread
111 new ThumbnailsCacheManager.InitDiskCacheTask().execute();
112
113 mGridMode = false;
114 }
115
116 @Override
117 public boolean areAllItemsEnabled() {
118 return true;
119 }
120
121 @Override
122 public boolean isEnabled(int position) {
123 return true;
124 }
125
126 @Override
127 public int getCount() {
128 return mFiles != null ? mFiles.size() : 0;
129 }
130
131 @Override
132 public Object getItem(int position) {
133 if (mFiles == null || mFiles.size() <= position)
134 return null;
135 return mFiles.get(position);
136 }
137
138 @Override
139 public long getItemId(int position) {
140 if (mFiles == null || mFiles.size() <= position)
141 return 0;
142 return mFiles.get(position).getFileId();
143 }
144
145 @Override
146 public int getItemViewType(int position) {
147 return 0;
148 }
149
150 @Override
151 public View getView(int position, View convertView, ViewGroup parent) {
152
153 View view = convertView;
154 OCFile file = null;
155 LayoutInflater inflator = (LayoutInflater) mContext
156 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
157
158 if (mFiles != null && mFiles.size() > position) {
159 file = mFiles.get(position);
160 }
161
162 // Find out which layout should be displayed
163 ViewType viewType;
164 if (!mGridMode){
165 viewType = ViewType.LIST_ITEM;
166 } else if (file.isImage() || file.isVideo()){
167 viewType = ViewType.GRID_IMAGE;
168 } else {
169 viewType = ViewType.GRID_ITEM;
170 }
171
172 // create view only if differs, otherwise reuse
173 if (convertView == null || (convertView != null && convertView.getTag() != viewType)) {
174 switch (viewType) {
175 case GRID_IMAGE:
176 view = inflator.inflate(R.layout.grid_image, null);
177 view.setTag(ViewType.GRID_IMAGE);
178 break;
179 case GRID_ITEM:
180 view = inflator.inflate(R.layout.grid_item, null);
181 view.setTag(ViewType.GRID_ITEM);
182 break;
183 case LIST_ITEM:
184 view = inflator.inflate(R.layout.list_item, null);
185 view.setTag(ViewType.LIST_ITEM);
186 break;
187 }
188 }
189
190 view.invalidate();
191
192 if (file != null){
193
194 ImageView fileIcon = (ImageView) view.findViewById(R.id.thumbnail);
195
196 fileIcon.setTag(file.getFileId());
197 TextView fileName;
198 String name = file.getFileName();
199
200 LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ListItemLayout);
201 linearLayout.setContentDescription("LinearLayout-" + name);
202
203 switch (viewType){
204 case LIST_ITEM:
205 TextView fileSizeV = (TextView) view.findViewById(R.id.last_size);
206 TextView fileSizeSeparatorV = (TextView) view.findViewById(R.id.file_separator);
207 TextView lastModV = (TextView) view.findViewById(R.id.file_mod);
208
209
210 lastModV.setVisibility(View.VISIBLE);
211 lastModV.setText(showRelativeTimestamp(file));
212
213
214 fileSizeSeparatorV.setVisibility(View.VISIBLE);
215 fileSizeV.setVisibility(View.VISIBLE);
216 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
217
218 // if (!file.isFolder()) {
219 // AbsListView parentList = (AbsListView)parent;
220 // if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
221 // if (parentList.getChoiceMode() == AbsListView.CHOICE_MODE_NONE) {
222 // checkBoxV.setVisibility(View.GONE);
223 // } else {
224 // if (parentList.isItemChecked(position)) {
225 // checkBoxV.setImageResource(
226 // R.drawable.ic_checkbox_marked);
227 // } else {
228 // checkBoxV.setImageResource(
229 // R.drawable.ic_checkbox_blank_outline);
230 // }
231 // checkBoxV.setVisibility(View.VISIBLE);
232 // }
233 // }
234
235 if (file.isFolder()) {
236 fileSizeSeparatorV.setVisibility(View.INVISIBLE);
237 fileSizeV.setVisibility(View.INVISIBLE);
238 }
239
240 case GRID_ITEM:
241 // filename
242 fileName = (TextView) view.findViewById(R.id.Filename);
243 name = file.getFileName();
244 fileName.setText(name);
245
246 case GRID_IMAGE:
247 // sharedIcon
248 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
249 if (file.isShareByLink()) {
250 sharedIconV.setVisibility(View.VISIBLE);
251 sharedIconV.bringToFront();
252 } else {
253 sharedIconV.setVisibility(View.GONE);
254 }
255
256 // local state
257 ImageView localStateView = (ImageView) view.findViewById(R.id.localFileIndicator);
258 localStateView.bringToFront();
259 FileDownloaderBinder downloaderBinder =
260 mTransferServiceGetter.getFileDownloaderBinder();
261 FileUploaderBinder uploaderBinder =
262 mTransferServiceGetter.getFileUploaderBinder();
263 OperationsServiceBinder opsBinder =
264 mTransferServiceGetter.getOperationsServiceBinder();
265
266 localStateView.setVisibility(View.INVISIBLE); // default first
267
268 if ( //synchronizing
269 opsBinder != null &&
270 opsBinder.isSynchronizing(mAccount, file.getRemotePath())
271 ) {
272 localStateView.setImageResource(R.drawable.synchronizing_file_indicator);
273 localStateView.setVisibility(View.VISIBLE);
274
275 } else if ( // downloading
276 downloaderBinder != null &&
277 downloaderBinder.isDownloading(mAccount, file)
278 ) {
279 localStateView.setImageResource(
280 file.isFolder() ?
281 R.drawable.synchronizing_file_indicator :
282 R.drawable.downloading_file_indicator
283 );
284 localStateView.setVisibility(View.VISIBLE);
285
286 } else if ( //uploading
287 uploaderBinder != null &&
288 uploaderBinder.isUploading(mAccount, file)
289 ) {
290 localStateView.setImageResource(
291 file.isFolder() ?
292 R.drawable.synchronizing_file_indicator :
293 R.drawable.uploading_file_indicator
294 );
295 localStateView.setVisibility(View.VISIBLE);
296
297 } else if (file.getEtagInConflict() != null) { // conflict
298 localStateView.setImageResource(R.drawable.conflict_file_indicator);
299 localStateView.setVisibility(View.VISIBLE);
300
301 } else if (file.isDown()) {
302 localStateView.setImageResource(R.drawable.local_file_indicator);
303 localStateView.setVisibility(View.VISIBLE);
304 }
305
306 // share with me icon
307 ImageView sharedWithMeIconV = (ImageView)
308 view.findViewById(R.id.sharedWithMeIcon);
309 sharedWithMeIconV.bringToFront();
310 if (checkIfFileIsSharedWithMe(file) &&
311 (!file.isFolder() || !mGridMode)) {
312 sharedWithMeIconV.setVisibility(View.VISIBLE);
313 } else {
314 sharedWithMeIconV.setVisibility(View.GONE);
315 }
316
317 break;
318 }
319
320 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
321 checkBoxV.setVisibility(View.GONE);
322
323 AbsListView parentList = (AbsListView)parent;
324 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
325 if (parentList.getChoiceMode() == AbsListView.CHOICE_MODE_NONE) {
326 checkBoxV.setVisibility(View.GONE);
327 } else if (parentList.getCheckedItemCount() > 0){
328 if (parentList.isItemChecked(position)) {
329 checkBoxV.setImageResource(
330 android.R.drawable.checkbox_on_background);
331 } else {
332 checkBoxV.setImageResource(
333 android.R.drawable.checkbox_off_background);
334 }
335 checkBoxV.setVisibility(View.VISIBLE);
336 }
337 }
338
339 // For all Views
340
341 // this if-else is needed even though favorite icon is visible by default
342 // because android reuses views in listview
343 if (!file.isFavorite()) {
344 view.findViewById(R.id.favoriteIcon).setVisibility(View.GONE);
345 } else {
346 view.findViewById(R.id.favoriteIcon).setVisibility(View.VISIBLE);
347 }
348
349 // No Folder
350 if (!file.isFolder()) {
351 if ((file.isImage() || file.isVideo()) && file.getRemoteId() != null){
352 // Thumbnail in Cache?
353 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
354 "t" + String.valueOf(file.getRemoteId()));
355 if (thumbnail != null && !file.needsUpdateThumbnail()){
356
357 if (file.isVideo()) {
358 Bitmap withOverlay = ThumbnailsCacheManager.addVideoOverlay(thumbnail);
359 fileIcon.setImageBitmap(withOverlay);
360 } else {
361 fileIcon.setImageBitmap(thumbnail);
362 }
363 } else {
364 // generate new Thumbnail
365 if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
366 final ThumbnailsCacheManager.ThumbnailGenerationTask task =
367 new ThumbnailsCacheManager.ThumbnailGenerationTask(
368 fileIcon, mStorageManager, mAccount
369 );
370 if (thumbnail == null) {
371 thumbnail = ThumbnailsCacheManager.mDefaultImg;
372 }
373 final ThumbnailsCacheManager.AsyncDrawable asyncDrawable =
374 new ThumbnailsCacheManager.AsyncDrawable(
375 mContext.getResources(),
376 thumbnail,
377 task
378 );
379 fileIcon.setImageDrawable(asyncDrawable);
380 task.execute(file, true);
381 }
382 }
383
384 if (file.getMimetype().equalsIgnoreCase("image/png")) {
385 fileIcon.setBackgroundColor(mContext.getResources()
386 .getColor(R.color.background_color));
387 }
388
389
390 } else {
391 fileIcon.setImageResource(MimetypeIconUtil.getFileTypeIconId(file.getMimetype(),
392 file.getFileName()));
393 }
394
395 } else {
396 // Folder
397 fileIcon.setImageResource(
398 MimetypeIconUtil.getFolderTypeIconId(
399 checkIfFileIsSharedWithMe(file), file.isShareByLink()));
400 }
401 }
402
403 if (mSelection.get(position) != null) {
404 view.setBackgroundColor(Color.rgb(248, 248, 248));
405 } else {
406 view.setBackgroundColor(Color.WHITE);
407 }
408
409 return view;
410 }
411
412 @Override
413 public int getViewTypeCount() {
414 return 1;
415 }
416
417 @Override
418 public boolean hasStableIds() {
419 return true;
420 }
421
422 @Override
423 public boolean isEmpty() {
424 return (mFiles == null || mFiles.isEmpty());
425 }
426
427 /**
428 * Change the adapted directory for a new one
429 * @param directory New file to adapt. Can be NULL, meaning
430 * "no content to adapt".
431 * @param updatedStorageManager Optional updated storage manager; used to replace
432 * mStorageManager if is different (and not NULL)
433 */
434 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager
435 , boolean onlyOnDevice) {
436 mFile = directory;
437 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
438 mStorageManager = updatedStorageManager;
439 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
440 }
441 if (mStorageManager != null) {
442 mFiles = mStorageManager.getFolderContent(mFile, onlyOnDevice);
443 mFilesOrig.clear();
444 mFilesOrig.addAll(mFiles);
445
446 if (mJustFolders) {
447 mFiles = getFolders(mFiles);
448 }
449 } else {
450 mFiles = null;
451 }
452
453 mFiles = FileStorageUtils.sortOcFolder(mFiles);
454 notifyDataSetChanged();
455 }
456
457
458 /**
459 * Filter for getting only the folders
460 * @param files
461 * @return Vector<OCFile>
462 */
463 public Vector<OCFile> getFolders(Vector<OCFile> files) {
464 Vector<OCFile> ret = new Vector<OCFile>();
465 OCFile current = null;
466 for (int i=0; i<files.size(); i++) {
467 current = files.get(i);
468 if (current.isFolder()) {
469 ret.add(current);
470 }
471 }
472 return ret;
473 }
474
475
476 /**
477 * Check if parent folder does not include 'S' permission and if file/folder
478 * is shared with me
479 *
480 * @param file: OCFile
481 * @return boolean: True if it is shared with me and false if it is not
482 */
483 private boolean checkIfFileIsSharedWithMe(OCFile file) {
484 return (mFile.getPermissions() != null
485 && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
486 && file.getPermissions() != null
487 && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
488 }
489
490 public void setSortOrder(Integer order, boolean ascending) {
491 SharedPreferences.Editor editor = mAppPreferences.edit();
492 editor.putInt("sortOrder", order);
493 editor.putBoolean("sortAscending", ascending);
494 editor.commit();
495
496 FileStorageUtils.mSortOrder = order;
497 FileStorageUtils.mSortAscending = ascending;
498
499
500 mFiles = FileStorageUtils.sortOcFolder(mFiles);
501 notifyDataSetChanged();
502
503 }
504
505 private CharSequence showRelativeTimestamp(OCFile file){
506 return DisplayUtils.getRelativeDateTimeString(mContext, file.getModificationTimestamp(),
507 DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
508 }
509
510 public void setGridMode(boolean gridMode) {
511 mGridMode = gridMode;
512 }
513
514 public boolean isGridMode() {
515 return mGridMode;
516 }
517
518 public void setNewSelection(int position, boolean checked) {
519 mSelection.put(position, checked);
520 notifyDataSetChanged();
521 }
522
523 public void removeSelection(int position) {
524 mSelection.remove(position);
525 notifyDataSetChanged();
526 }
527
528 public void removeSelection(){
529 mSelection.clear();
530 notifyDataSetChanged();
531 }
532
533 public ArrayList<Integer> getCheckedItemPositions() {
534 ArrayList<Integer> ids = new ArrayList<Integer>();
535
536 for (Map.Entry<Integer, Boolean> entry : mSelection.entrySet()){
537 if (entry.getValue()){
538 ids.add(entry.getKey());
539 }
540 }
541 return ids;
542 }
543
544 public ArrayList<OCFile> getCheckedItems() {
545 ArrayList<OCFile> files = new ArrayList<OCFile>();
546
547 for (Map.Entry<Integer, Boolean> entry : mSelection.entrySet()){
548 if (entry.getValue()){
549 files.add((OCFile) getItem(entry.getKey()));
550 }
551 }
552 return files;
553 }
554 }