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