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