2672926f0d473dc55c9a9043da6072c9dbfca096
[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.Vector;
29
30 import android.accounts.Account;
31 import android.content.Context;
32 import android.content.SharedPreferences;
33 import android.graphics.Bitmap;
34 import android.os.Build;
35 import android.preference.PreferenceManager;
36 import android.text.format.DateUtils;
37 import android.view.LayoutInflater;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.widget.AbsListView;
41 import android.widget.BaseAdapter;
42 import android.widget.ImageView;
43 import android.widget.LinearLayout;
44 import android.widget.ListAdapter;
45 import android.widget.TextView;
46
47 import com.owncloud.android.R;
48 import com.owncloud.android.authentication.AccountUtils;
49 import com.owncloud.android.datamodel.FileDataStorageManager;
50 import com.owncloud.android.datamodel.OCFile;
51 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
52 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
53 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
54 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
55 import com.owncloud.android.ui.activity.ComponentsGetter;
56 import com.owncloud.android.utils.DisplayUtils;
57 import com.owncloud.android.utils.FileStorageUtils;
58 import com.owncloud.android.utils.MimetypeIconUtil;
59
60
61 /**
62 * This Adapter populates a ListView with all files and folders in an ownCloud
63 * instance.
64 */
65 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
66 private final static String PERMISSION_SHARED_WITH_ME = "S";
67
68 private Context mContext;
69 private OCFile mFile = null;
70 private Vector<OCFile> mFiles = null;
71 private Vector<OCFile> mFilesOrig = new Vector<OCFile>();
72 private boolean mJustFolders;
73
74 private FileDataStorageManager mStorageManager;
75 private Account mAccount;
76 private ComponentsGetter mTransferServiceGetter;
77 private boolean mGridMode;
78
79 private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM };
80
81 private SharedPreferences mAppPreferences;
82
83 public FileListListAdapter(
84 boolean justFolders,
85 Context context,
86 ComponentsGetter transferServiceGetter
87 ) {
88
89 mJustFolders = justFolders;
90 mContext = context;
91 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
92 mTransferServiceGetter = transferServiceGetter;
93
94 mAppPreferences = PreferenceManager
95 .getDefaultSharedPreferences(mContext);
96
97 // Read sorting order, default to sort by name ascending
98 FileStorageUtils.mSortOrder = mAppPreferences.getInt("sortOrder", 0);
99 FileStorageUtils.mSortAscending = mAppPreferences.getBoolean("sortAscending", true);
100
101 // initialise thumbnails cache on background thread
102 new ThumbnailsCacheManager.InitDiskCacheTask().execute();
103
104 mGridMode = false;
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
144 View view = convertView;
145 OCFile file = null;
146 LayoutInflater inflator = (LayoutInflater) mContext
147 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
148
149 if (mFiles != null && mFiles.size() > position) {
150 file = mFiles.get(position);
151 }
152
153 // Find out which layout should be displayed
154 ViewType viewType;
155 if (!mGridMode){
156 viewType = ViewType.LIST_ITEM;
157 } else if (file.isImage()){
158 viewType = ViewType.GRID_IMAGE;
159 } else {
160 viewType = ViewType.GRID_ITEM;
161 }
162
163 // create view only if differs, otherwise reuse
164 if (convertView == null || (convertView != null && convertView.getTag() != viewType)) {
165 switch (viewType) {
166 case GRID_IMAGE:
167 view = inflator.inflate(R.layout.grid_image, null);
168 view.setTag(ViewType.GRID_IMAGE);
169 break;
170 case GRID_ITEM:
171 view = inflator.inflate(R.layout.grid_item, null);
172 view.setTag(ViewType.GRID_ITEM);
173 break;
174 case LIST_ITEM:
175 view = inflator.inflate(R.layout.list_item, null);
176 view.setTag(ViewType.LIST_ITEM);
177 break;
178 }
179 }
180
181 view.invalidate();
182
183 if (file != null){
184
185 ImageView fileIcon = (ImageView) view.findViewById(R.id.thumbnail);
186
187 fileIcon.setTag(file.getFileId());
188 TextView fileName;
189 String name = file.getFileName();
190
191 LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ListItemLayout);
192 linearLayout.setContentDescription("LinearLayout-" + name);
193
194 switch (viewType){
195 case LIST_ITEM:
196 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
197 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
198 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
199
200 lastModV.setVisibility(View.VISIBLE);
201 lastModV.setText(showRelativeTimestamp(file));
202
203 checkBoxV.setVisibility(View.GONE);
204
205 fileSizeV.setVisibility(View.VISIBLE);
206 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
207
208 if (!file.isFolder()) {
209 AbsListView parentList = (AbsListView)parent;
210 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
211 if (parentList.getChoiceMode() == AbsListView.CHOICE_MODE_NONE) {
212 checkBoxV.setVisibility(View.GONE);
213 } else {
214 if (parentList.isItemChecked(position)) {
215 checkBoxV.setImageResource(
216 android.R.drawable.checkbox_on_background);
217 } else {
218 checkBoxV.setImageResource(
219 android.R.drawable.checkbox_off_background);
220 }
221 checkBoxV.setVisibility(View.VISIBLE);
222 }
223 }
224
225 } else { //Folder
226 fileSizeV.setVisibility(View.INVISIBLE);
227 }
228
229 case GRID_ITEM:
230 // filename
231 fileName = (TextView) view.findViewById(R.id.Filename);
232 name = file.getFileName();
233 fileName.setText(name);
234
235 case GRID_IMAGE:
236 // sharedIcon
237 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
238 if (file.isShareByLink()) {
239 sharedIconV.setVisibility(View.VISIBLE);
240 sharedIconV.bringToFront();
241 } else {
242 sharedIconV.setVisibility(View.GONE);
243 }
244
245 // local state
246 ImageView localStateView = (ImageView) view.findViewById(R.id.localFileIndicator);
247 localStateView.bringToFront();
248 FileDownloaderBinder downloaderBinder =
249 mTransferServiceGetter.getFileDownloaderBinder();
250 FileUploaderBinder uploaderBinder =
251 mTransferServiceGetter.getFileUploaderBinder();
252 OperationsServiceBinder opsBinder =
253 mTransferServiceGetter.getOperationsServiceBinder();
254
255 localStateView.setVisibility(View.INVISIBLE); // default first
256
257 if (file.isFolder()) {
258 if ( //synchronizing
259 (opsBinder != null &&
260 opsBinder.isSynchronizing(mAccount, file.getRemotePath())) ||
261 // downloading
262 (downloaderBinder != null &&
263 downloaderBinder.isDownloading(mAccount, file)) ||
264 // uploading
265 (uploaderBinder != null &&
266 uploaderBinder.isUploading(mAccount, file))
267 ) {
268
269 localStateView.setImageResource(R.drawable.synchronizing_file_indicator);
270 localStateView.setVisibility(View.VISIBLE);
271 }
272
273 } else if ( //synchronizing
274 opsBinder != null &&
275 opsBinder.isSynchronizing(mAccount, file.getRemotePath())
276 ) {
277 localStateView.setImageResource(R.drawable.synchronizing_file_indicator);
278 localStateView.setVisibility(View.VISIBLE);
279
280 } else if ( // downloading
281 downloaderBinder != null &&
282 downloaderBinder.isDownloading(mAccount, file)
283 ) {
284 localStateView.setImageResource(R.drawable.downloading_file_indicator);
285 localStateView.setVisibility(View.VISIBLE);
286
287 } else if (//uploading
288 uploaderBinder != null &&
289 uploaderBinder.isUploading(mAccount, file)
290 ) {
291 localStateView.setImageResource(R.drawable.uploading_file_indicator);
292 localStateView.setVisibility(View.VISIBLE);
293
294 } else if (file.isInConflict()) { // conflict
295 localStateView.setImageResource(R.drawable.conflict_file_indicator);
296 localStateView.setVisibility(View.VISIBLE);
297
298 } else if (file.isDown()) {
299 localStateView.setImageResource(R.drawable.local_file_indicator);
300 localStateView.setVisibility(View.VISIBLE);
301 }
302
303 // share with me icon
304 ImageView sharedWithMeIconV = (ImageView)
305 view.findViewById(R.id.sharedWithMeIcon);
306 sharedWithMeIconV.bringToFront();
307 if (checkIfFileIsSharedWithMe(file) &&
308 (!file.isFolder() || !mGridMode)) {
309 sharedWithMeIconV.setVisibility(View.VISIBLE);
310 } else {
311 sharedWithMeIconV.setVisibility(View.GONE);
312 }
313
314 break;
315 }
316
317 // For all Views
318
319 // this if-else is needed even though favorite icon is visible by default
320 // because android reuses views in listview
321 if (!file.isFavorite()) {
322 view.findViewById(R.id.favoriteIcon).setVisibility(View.GONE);
323 } else {
324 view.findViewById(R.id.favoriteIcon).setVisibility(View.VISIBLE);
325 }
326
327 // No Folder
328 if (!file.isFolder()) {
329 if (file.isImage() && file.getRemoteId() != null){
330 // Thumbnail in Cache?
331 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
332 String.valueOf(file.getRemoteId())
333 );
334 if (thumbnail != null && !file.needsUpdateThumbnail()){
335 fileIcon.setImageBitmap(thumbnail);
336 } else {
337 // generate new Thumbnail
338 if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
339 final ThumbnailsCacheManager.ThumbnailGenerationTask task =
340 new ThumbnailsCacheManager.ThumbnailGenerationTask(
341 fileIcon, mStorageManager, mAccount
342 );
343 if (thumbnail == null) {
344 thumbnail = ThumbnailsCacheManager.mDefaultImg;
345 }
346 final ThumbnailsCacheManager.AsyncDrawable asyncDrawable =
347 new ThumbnailsCacheManager.AsyncDrawable(
348 mContext.getResources(),
349 thumbnail,
350 task
351 );
352 fileIcon.setImageDrawable(asyncDrawable);
353 task.execute(file);
354 }
355 }
356
357 if (file.getMimetype().equalsIgnoreCase("image/png")) {
358 fileIcon.setBackgroundColor(mContext.getResources()
359 .getColor(R.color.background_color));
360 }
361
362
363 } else {
364 fileIcon.setImageResource(MimetypeIconUtil.getFileTypeIconId(file.getMimetype(),
365 file.getFileName()));
366 }
367
368 } else {
369 // Folder
370 fileIcon.setImageResource(
371 MimetypeIconUtil.getFolderTypeIconId(
372 checkIfFileIsSharedWithMe(file), file.isShareByLink()));
373 }
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 }