49c66347c0d79f7123661029a4ba485a9bdfe2d3
[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 import java.io.ByteArrayInputStream;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.lang.ref.WeakReference;
25 import java.net.URLEncoder;
26 //import java.net.URLEncoder;
27 import java.util.Vector;
28
29 import org.apache.commons.httpclient.methods.GetMethod;
30 import org.apache.http.HttpStatus;
31
32 import android.accounts.Account;
33 import android.accounts.AuthenticatorException;
34 import android.accounts.OperationCanceledException;
35 import android.content.Context;
36 import android.content.res.Resources;
37 import android.graphics.Bitmap;
38 import android.graphics.Bitmap.CompressFormat;
39 import android.graphics.BitmapFactory;
40 import android.graphics.drawable.BitmapDrawable;
41 import android.graphics.drawable.Drawable;
42 import android.media.ThumbnailUtils;
43 import android.os.AsyncTask;
44 import android.util.TypedValue;
45 import android.view.LayoutInflater;
46 import android.view.View;
47 import android.view.ViewGroup;
48 import android.widget.BaseAdapter;
49 import android.widget.ImageView;
50 import android.widget.ListAdapter;
51 import android.widget.ListView;
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.files.services.FileDownloader.FileDownloaderBinder;
59 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
60 import com.owncloud.android.lib.common.OwnCloudAccount;
61 import com.owncloud.android.lib.common.OwnCloudClient;
62 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
63 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
64 import com.owncloud.android.lib.resources.shares.ShareUtils;
65 import com.owncloud.android.ui.activity.ComponentsGetter;
66 import com.owncloud.android.utils.DisplayUtils;
67 import com.owncloud.android.utils.Log_OC;
68
69 /*
70 import org.apache.http.HttpEntity;
71 import org.apache.http.HttpResponse;
72 import org.apache.http.auth.AuthScope;
73 import org.apache.http.auth.UsernamePasswordCredentials;
74 import org.apache.http.client.methods.HttpGet;
75 import org.apache.http.impl.client.DefaultHttpClient;
76 import org.apache.http.util.EntityUtils;
77 */
78
79
80 /**
81 * This Adapter populates a ListView with all files and folders in an ownCloud
82 * instance.
83 *
84 * @author Bartek Przybylski
85 * @Author Tobias Kaminsky
86 *
87 */
88 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
89 private final static String PERMISSION_SHARED_WITH_ME = "S";
90
91 private Context mContext;
92 private OCFile mFile = null;
93 private Vector<OCFile> mFiles = null;
94 private boolean mJustFolders;
95
96 private FileDataStorageManager mStorageManager;
97 private Account mAccount;
98 private ComponentsGetter mTransferServiceGetter;
99
100 private final Object thumbnailDiskCacheLock = new Object();
101 private DiskLruImageCache mThumbnailCache;
102 private boolean mThumbnailCacheStarting = true;
103 private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
104 private static final CompressFormat mCompressFormat = CompressFormat.JPEG;
105 private static final int mCompressQuality = 70;
106 private Bitmap defaultImg;
107 private OwnCloudClient client;
108
109 public FileListListAdapter(
110 boolean justFolders,
111 Context context,
112 ComponentsGetter transferServiceGetter
113 ) {
114
115 mJustFolders = justFolders;
116 mContext = context;
117 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
118 mTransferServiceGetter = transferServiceGetter;
119 defaultImg = BitmapFactory.decodeResource(mContext.getResources(),
120 DisplayUtils.getResourceId("image/png", "default.png"));
121
122 // Initialise disk cache on background thread
123 new InitDiskCacheTask().execute();
124
125
126
127 }
128
129 class InitDiskCacheTask extends AsyncTask<File, Void, Void> {
130 @Override
131 protected Void doInBackground(File... params) {
132 synchronized (thumbnailDiskCacheLock) {
133 OwnCloudAccount ocAccount;
134 try {
135 ocAccount = new OwnCloudAccount(mAccount, mContext);
136 client = OwnCloudClientManagerFactory.getDefaultSingleton().
137 getClientFor(ocAccount, mContext);
138 } catch (AccountNotFoundException e) {
139 // TODO Auto-generated catch block
140 e.printStackTrace();
141 } catch (AuthenticatorException e) {
142 // TODO Auto-generated catch block
143 e.printStackTrace();
144 } catch (OperationCanceledException e) {
145 // TODO Auto-generated catch block
146 e.printStackTrace();
147 } catch (IOException e) {
148 // TODO Auto-generated catch block
149 e.printStackTrace();
150 }
151
152
153 mThumbnailCache = new DiskLruImageCache(mContext, "thumbnailCache",
154 DISK_CACHE_SIZE, mCompressFormat, mCompressQuality);
155
156 mThumbnailCacheStarting = false; // Finished initialization
157 thumbnailDiskCacheLock.notifyAll(); // Wake any waiting threads
158 }
159 return null;
160 }
161 }
162
163 static class AsyncDrawable extends BitmapDrawable {
164 private final WeakReference<ThumbnailGenerationTask> bitmapWorkerTaskReference;
165
166 public AsyncDrawable(Resources res, Bitmap bitmap,
167 ThumbnailGenerationTask bitmapWorkerTask) {
168 super(res, bitmap);
169 bitmapWorkerTaskReference =
170 new WeakReference<ThumbnailGenerationTask>(bitmapWorkerTask);
171 }
172
173 public ThumbnailGenerationTask getBitmapWorkerTask() {
174 return bitmapWorkerTaskReference.get();
175 }
176 }
177
178 class ThumbnailGenerationTask extends AsyncTask<OCFile, Void, Bitmap> {
179 private final WeakReference<ImageView> imageViewReference;
180 private OCFile file;
181
182
183 public ThumbnailGenerationTask(ImageView imageView) {
184 // Use a WeakReference to ensure the ImageView can be garbage collected
185 imageViewReference = new WeakReference<ImageView>(imageView);
186 }
187
188 // Decode image in background.
189 @Override
190 protected Bitmap doInBackground(OCFile... params) {
191 file = params[0];
192 final String imageKey = String.valueOf(file.getRemoteId().hashCode());
193 Log_OC.d("Thumbnail", imageKey);
194
195 // Check disk cache in background thread
196 Bitmap thumbnail = getBitmapFromDiskCache(imageKey);
197
198 // Not found in disk cache
199 if (thumbnail == null) {
200 // Converts dp to pixel
201 Resources r = mContext.getResources();
202 int px = (int) Math.round(TypedValue.applyDimension(
203 TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()
204 ));
205
206 if (file.isDown()){
207 Bitmap bitmap = BitmapFactory.decodeFile(file.getStoragePath());
208
209 if (bitmap != null) {
210 thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
211
212 // Add thumbnail to cache
213 addBitmapToCache(imageKey, thumbnail);
214 }
215
216 } else {
217 // Download thumbnail from server
218 // Commented out as maybe changes to client library are needed
219 try {
220 int status = -1;
221
222 String uri = client.getBaseUri() + "/index.php/apps/files/api/v1/thumbnail/100/100/"+URLEncoder.encode(file.getRemotePath(), "UTF-8").replaceAll("%2F", "/");
223 Log_OC.d("Thumbnail", "URI: " + uri);
224 GetMethod get = new GetMethod(uri);
225 status = client.executeMethod(get);
226 if(isSuccess(status)) {
227 byte[] bytes = get.getResponseBody();
228 Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
229 thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
230
231 // Add thumbnail to cache
232 if (thumbnail != null){
233 addBitmapToCache(imageKey, thumbnail);
234 }
235 }
236 } catch(Exception e){
237 e.printStackTrace();
238 }
239 }
240 }
241 return thumbnail;
242 }
243
244 private boolean isSuccess(int status) {
245 return (status == HttpStatus.SC_OK);
246 }
247
248
249 protected void onPostExecute(Bitmap bitmap){
250 if (isCancelled()) {
251 bitmap = null;
252 }
253
254 if (imageViewReference != null && bitmap != null) {
255 final ImageView imageView = imageViewReference.get();
256 final ThumbnailGenerationTask bitmapWorkerTask =
257 getBitmapWorkerTask(imageView);
258 if (this == bitmapWorkerTask && imageView != null) {
259 imageView.setImageBitmap(bitmap);
260 }
261 }
262 }
263 }
264
265 public void addBitmapToCache(String key, Bitmap bitmap) {
266 synchronized (thumbnailDiskCacheLock) {
267 if (mThumbnailCache != null && mThumbnailCache.getBitmap(key) == null) {
268 mThumbnailCache.put(key, bitmap);
269 }
270 }
271 }
272
273 public Bitmap getBitmapFromDiskCache(String key) {
274 synchronized (thumbnailDiskCacheLock) {
275 // Wait while disk cache is started from background thread
276 while (mThumbnailCacheStarting) {
277 try {
278 thumbnailDiskCacheLock.wait();
279 } catch (InterruptedException e) {}
280 }
281 if (mThumbnailCache != null) {
282 return (Bitmap) mThumbnailCache.getBitmap(key);
283 }
284 }
285 return null;
286 }
287
288 @Override
289 public boolean areAllItemsEnabled() {
290 return true;
291 }
292
293 @Override
294 public boolean isEnabled(int position) {
295 return true;
296 }
297
298 @Override
299 public int getCount() {
300 return mFiles != null ? mFiles.size() : 0;
301 }
302
303 @Override
304 public Object getItem(int position) {
305 if (mFiles == null || mFiles.size() <= position)
306 return null;
307 return mFiles.get(position);
308 }
309
310 @Override
311 public long getItemId(int position) {
312 if (mFiles == null || mFiles.size() <= position)
313 return 0;
314 return mFiles.get(position).getFileId();
315 }
316
317 @Override
318 public int getItemViewType(int position) {
319 return 0;
320 }
321
322 @Override
323 public View getView(int position, View convertView, ViewGroup parent) {
324 View view = convertView;
325 if (view == null) {
326 LayoutInflater inflator = (LayoutInflater) mContext
327 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
328 view = inflator.inflate(R.layout.list_item, null);
329 }
330
331 if (mFiles != null && mFiles.size() > position) {
332 OCFile file = mFiles.get(position);
333 TextView fileName = (TextView) view.findViewById(R.id.Filename);
334 String name = file.getFileName();
335
336 fileName.setText(name);
337 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
338 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
339 ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
340 sharedWithMeIconV.setVisibility(View.GONE);
341
342 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
343 localStateView.bringToFront();
344 FileDownloaderBinder downloaderBinder =
345 mTransferServiceGetter.getFileDownloaderBinder();
346 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
347 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
348 localStateView.setImageResource(R.drawable.downloading_file_indicator);
349 localStateView.setVisibility(View.VISIBLE);
350 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
351 localStateView.setImageResource(R.drawable.uploading_file_indicator);
352 localStateView.setVisibility(View.VISIBLE);
353 } else if (file.isDown()) {
354 localStateView.setImageResource(R.drawable.local_file_indicator);
355 localStateView.setVisibility(View.VISIBLE);
356 } else {
357 localStateView.setVisibility(View.INVISIBLE);
358 }
359
360 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
361 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
362 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
363
364 if (!file.isFolder()) {
365 fileSizeV.setVisibility(View.VISIBLE);
366 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
367 lastModV.setVisibility(View.VISIBLE);
368 lastModV.setText(
369 DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp())
370 );
371 // this if-else is needed even thoe fav icon is visible by default
372 // because android reuses views in listview
373 if (!file.keepInSync()) {
374 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
375 } else {
376 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
377 }
378
379 ListView parentList = (ListView)parent;
380 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
381 checkBoxV.setVisibility(View.GONE);
382 } else {
383 if (parentList.isItemChecked(position)) {
384 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
385 } else {
386 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
387 }
388 checkBoxV.setVisibility(View.VISIBLE);
389 }
390
391 // get Thumbnail if file is image
392 if (file.isImage()){
393 // Thumbnail in Cache?
394 Bitmap thumbnail = getBitmapFromDiskCache(String.valueOf(file.getRemoteId().hashCode()));
395 if (thumbnail != null){
396 fileIcon.setImageBitmap(thumbnail);
397 } else {
398 // generate new Thumbnail
399 if (cancelPotentialWork(file, fileIcon)) {
400 final ThumbnailGenerationTask task =
401 new ThumbnailGenerationTask(fileIcon);
402 final AsyncDrawable asyncDrawable =
403 new AsyncDrawable(mContext.getResources(), defaultImg, task);
404 fileIcon.setImageDrawable(asyncDrawable);
405 task.execute(file);
406 }
407 }
408 } else {
409 fileIcon.setImageResource(
410 DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())
411 );
412 }
413
414 if (checkIfFileIsSharedWithMe(file)) {
415 sharedWithMeIconV.setVisibility(View.VISIBLE);
416 }
417 }
418 else {
419 fileSizeV.setVisibility(View.INVISIBLE);
420 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
421 lastModV.setVisibility(View.VISIBLE);
422 lastModV.setText(
423 DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp())
424 );
425 checkBoxV.setVisibility(View.GONE);
426 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
427
428 if (checkIfFileIsSharedWithMe(file)) {
429 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
430 sharedWithMeIconV.setVisibility(View.VISIBLE);
431 } else {
432 fileIcon.setImageResource(
433 DisplayUtils.getResourceId(file.getMimetype(), file.getFileName())
434 );
435 }
436
437 // If folder is sharedByLink, icon folder must be changed to
438 // folder-public one
439 if (file.isShareByLink()) {
440 fileIcon.setImageResource(R.drawable.folder_public);
441 }
442 }
443
444 if (file.isShareByLink()) {
445 sharedIconV.setVisibility(View.VISIBLE);
446 } else {
447 sharedIconV.setVisibility(View.GONE);
448 }
449 }
450
451 return view;
452 }
453
454 public static boolean cancelPotentialWork(OCFile file, ImageView imageView) {
455 final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
456
457 if (bitmapWorkerTask != null) {
458 final OCFile bitmapData = bitmapWorkerTask.file;
459 // If bitmapData is not yet set or it differs from the new data
460 if (bitmapData == null || bitmapData != file) {
461 // Cancel previous task
462 bitmapWorkerTask.cancel(true);
463 } else {
464 // The same work is already in progress
465 return false;
466 }
467 }
468 // No task associated with the ImageView, or an existing task was cancelled
469 return true;
470 }
471
472 private static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
473 if (imageView != null) {
474 final Drawable drawable = imageView.getDrawable();
475 if (drawable instanceof AsyncDrawable) {
476 final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
477 return asyncDrawable.getBitmapWorkerTask();
478 }
479 }
480 return null;
481 }
482
483 @Override
484 public int getViewTypeCount() {
485 return 1;
486 }
487
488 @Override
489 public boolean hasStableIds() {
490 return true;
491 }
492
493 @Override
494 public boolean isEmpty() {
495 return (mFiles == null || mFiles.isEmpty());
496 }
497
498 /**
499 * Change the adapted directory for a new one
500 * @param directory New file to adapt. Can be NULL, meaning
501 * "no content to adapt".
502 * @param updatedStorageManager Optional updated storage manager; used to replace
503 * mStorageManager if is different (and not NULL)
504 */
505 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
506 mFile = directory;
507 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
508 mStorageManager = updatedStorageManager;
509 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
510 }
511 if (mStorageManager != null) {
512 mFiles = mStorageManager.getFolderContent(mFile);
513 if (mJustFolders) {
514 mFiles = getFolders(mFiles);
515 }
516 } else {
517 mFiles = null;
518 }
519 notifyDataSetChanged();
520 }
521
522
523 /**
524 * Filter for getting only the folders
525 * @param files
526 * @return Vector<OCFile>
527 */
528 public Vector<OCFile> getFolders(Vector<OCFile> files) {
529 Vector<OCFile> ret = new Vector<OCFile>();
530 OCFile current = null;
531 for (int i=0; i<files.size(); i++) {
532 current = files.get(i);
533 if (current.isFolder()) {
534 ret.add(current);
535 }
536 }
537 return ret;
538 }
539
540
541 /**
542 * Check if parent folder does not include 'S' permission and if file/folder
543 * is shared with me
544 *
545 * @param file: OCFile
546 * @return boolean: True if it is shared with me and false if it is not
547 */
548 private boolean checkIfFileIsSharedWithMe(OCFile file) {
549 return (mFile.getPermissions() != null
550 && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
551 && file.getPermissions() != null
552 && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
553 }
554 }