e2ad4fa3d273ba58b6ba290a5fa9a784e1238825
[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.File;
21 import java.io.IOException;
22 import java.net.URLEncoder;
23 import java.util.Vector;
24
25 import android.accounts.Account;
26 import android.accounts.AuthenticatorException;
27 import android.accounts.OperationCanceledException;
28 import android.content.Context;
29 import android.content.res.Resources;
30 import android.graphics.Bitmap;
31 import android.graphics.Bitmap.CompressFormat;
32 import android.graphics.BitmapFactory;
33 import android.media.ThumbnailUtils;
34 import android.os.AsyncTask;
35 import android.util.TypedValue;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.widget.BaseAdapter;
40 import android.widget.ImageView;
41 import android.widget.ListAdapter;
42 import android.widget.ListView;
43 import android.widget.TextView;
44
45 import com.owncloud.android.R;
46 import com.owncloud.android.authentication.AccountUtils;
47 import com.owncloud.android.datamodel.FileDataStorageManager;
48 import com.owncloud.android.datamodel.OCFile;
49 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
50 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
51 import com.owncloud.android.lib.common.OwnCloudAccount;
52 import com.owncloud.android.lib.common.OwnCloudClient;
53 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
54 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
55 import com.owncloud.android.ui.activity.ComponentsGetter;
56 import com.owncloud.android.utils.DisplayUtils;
57
58 import org.apache.http.HttpEntity;
59 import org.apache.http.HttpResponse;
60 import org.apache.http.auth.AuthScope;
61 import org.apache.http.auth.UsernamePasswordCredentials;
62 import org.apache.http.client.methods.HttpGet;
63 import org.apache.http.impl.client.DefaultHttpClient;
64 import org.apache.http.util.EntityUtils;
65
66
67 /**
68 * This Adapter populates a ListView with all files and folders in an ownCloud
69 * instance.
70 *
71 * @author Bartek Przybylski
72 *
73 */
74 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
75 private final static String PERMISSION_SHARED_WITH_ME = "S";
76
77 private Context mContext;
78 private OCFile mFile = null;
79 private Vector<OCFile> mFiles = null;
80
81 private FileDataStorageManager mStorageManager;
82 private Account mAccount;
83 private ComponentsGetter mTransferServiceGetter;
84 private final Object mDiskCacheLock = new Object();
85 private DiskLruImageCache mDiskLruCache;
86 private boolean mDiskCacheStarting = true;
87 private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
88 private CompressFormat mCompressFormat = CompressFormat.JPEG;
89 private int mCompressQuality = 70;
90 private OwnCloudClient mClient;
91
92 public FileListListAdapter(Context context, ComponentsGetter transferServiceGetter) {
93 mContext = context;
94 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
95 mTransferServiceGetter = transferServiceGetter;
96
97 // Initialise disk cache on background thread
98 new InitDiskCacheTask().execute();
99 }
100
101 class InitDiskCacheTask extends AsyncTask<File, Void, Void> {
102 @Override
103 protected Void doInBackground(File... params) {
104 synchronized (mDiskCacheLock) {
105 mDiskLruCache = new DiskLruImageCache(mContext, "thumbnailCache", DISK_CACHE_SIZE, mCompressFormat,mCompressQuality);
106
107 try {
108 OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
109 mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
110 getClientFor(ocAccount, mContext);
111 } catch (AccountNotFoundException e) {
112 // TODO Auto-generated catch block
113 e.printStackTrace();
114 } catch (AuthenticatorException e) {
115 // TODO Auto-generated catch block
116 e.printStackTrace();
117 } catch (OperationCanceledException e) {
118 // TODO Auto-generated catch block
119 e.printStackTrace();
120 } catch (IOException e) {
121 // TODO Auto-generated catch block
122 e.printStackTrace();
123 }
124
125 mDiskCacheStarting = false; // Finished initialization
126 mDiskCacheLock.notifyAll(); // Wake any waiting threads
127 }
128 return null;
129 }
130 }
131
132 class BitmapWorkerTask extends AsyncTask<OCFile, Void, Bitmap> {
133 private final ImageView fileIcon;
134
135 public BitmapWorkerTask(ImageView fileIcon) {
136 this.fileIcon = fileIcon;
137 }
138
139 // Decode image in background.
140 @Override
141 protected Bitmap doInBackground(OCFile... params) {
142 OCFile file = params[0];
143 final String imageKey = String.valueOf(file.getRemoteId());
144
145 // Check disk cache in background thread
146 Bitmap thumbnail = getBitmapFromDiskCache(imageKey);
147
148 // Not found in disk cache
149 if (thumbnail == null) {
150 // Converts dp to pixel
151 Resources r = mContext.getResources();
152 int px = (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()));
153
154 if (file.isDown()){
155 Bitmap bitmap = BitmapFactory.decodeFile(file.getStoragePath());
156 thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
157
158 // Add thumbnail to cache
159 addBitmapToCache(imageKey, thumbnail);
160
161 } else {
162 // Download thumbnail from server
163 DefaultHttpClient httpclient = new DefaultHttpClient();
164 try {
165 httpclient.getCredentialsProvider().setCredentials(
166 new AuthScope(mClient.getBaseUri().toString().replace("https://", ""), 443),
167 new UsernamePasswordCredentials(mClient.getCredentials().getUsername(), mClient.getCredentials().getAuthToken()));
168
169
170 // TODO change to user preview.png
171 //HttpGet httpget = new HttpGet(mClient.getBaseUri() + "/index.php/core/preview.png?file="+URLEncoder.encode(file.getRemotePath(), "UTF-8")+"&x=36&y=36&forceIcon=1");
172 HttpGet httpget = new HttpGet(mClient.getBaseUri() + "/ocs/v1.php/thumbnail?x=50&y=50&path=" + URLEncoder.encode(file.getRemotePath(), "UTF-8"));
173 HttpResponse response = httpclient.execute(httpget);
174 HttpEntity entity = response.getEntity();
175
176 if (entity != null) {
177 byte[] bytes = EntityUtils.toByteArray(entity);
178 Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
179 thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
180
181 // Add thumbnail to cache
182 if (thumbnail != null){
183 addBitmapToCache(imageKey, thumbnail);
184 }
185 }
186 } catch(Exception e){
187 e.printStackTrace();
188 }finally {
189 httpclient.getConnectionManager().shutdown();
190 }
191 }
192 }
193 return thumbnail;
194 }
195
196 protected void onPostExecute(Bitmap bitmap){
197 if (bitmap != null){
198 fileIcon.setImageBitmap(bitmap);
199 }
200 }
201 }
202
203 public void addBitmapToCache(String key, Bitmap bitmap) {
204 synchronized (mDiskCacheLock) {
205 if (mDiskLruCache != null && mDiskLruCache.getBitmap(key) == null) {
206 mDiskLruCache.put(key, bitmap);
207 }
208 }
209 }
210
211 public Bitmap getBitmapFromDiskCache(String key) {
212 synchronized (mDiskCacheLock) {
213 // Wait while disk cache is started from background thread
214 while (mDiskCacheStarting) {
215 try {
216 mDiskCacheLock.wait();
217 } catch (InterruptedException e) {}
218 }
219 if (mDiskLruCache != null) {
220 return (Bitmap) mDiskLruCache.getBitmap(key);
221 }
222 }
223 return null;
224 }
225
226 @Override
227 public boolean areAllItemsEnabled() {
228 return true;
229 }
230
231 @Override
232 public boolean isEnabled(int position) {
233 return true;
234 }
235
236 @Override
237 public int getCount() {
238 return mFiles != null ? mFiles.size() : 0;
239 }
240
241 @Override
242 public Object getItem(int position) {
243 if (mFiles == null || mFiles.size() <= position)
244 return null;
245 return mFiles.get(position);
246 }
247
248 @Override
249 public long getItemId(int position) {
250 if (mFiles == null || mFiles.size() <= position)
251 return 0;
252 return mFiles.get(position).getFileId();
253 }
254
255 @Override
256 public int getItemViewType(int position) {
257 return 0;
258 }
259
260 @Override
261 public View getView(int position, View convertView, ViewGroup parent) {
262 View view = convertView;
263 if (view == null) {
264 LayoutInflater inflator = (LayoutInflater) mContext
265 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
266 view = inflator.inflate(R.layout.list_item, null);
267 }
268
269 if (mFiles != null && mFiles.size() > position) {
270 OCFile file = mFiles.get(position);
271 TextView fileName = (TextView) view.findViewById(R.id.Filename);
272 String name = file.getFileName();
273
274 fileName.setText(name);
275 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
276 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
277 ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
278 sharedWithMeIconV.setVisibility(View.GONE);
279
280 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
281 localStateView.bringToFront();
282 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
283 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
284 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
285 localStateView.setImageResource(R.drawable.downloading_file_indicator);
286 localStateView.setVisibility(View.VISIBLE);
287 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
288 localStateView.setImageResource(R.drawable.uploading_file_indicator);
289 localStateView.setVisibility(View.VISIBLE);
290 } else if (file.isDown()) {
291 localStateView.setImageResource(R.drawable.local_file_indicator);
292 localStateView.setVisibility(View.VISIBLE);
293 } else {
294 localStateView.setVisibility(View.INVISIBLE);
295 }
296
297 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
298 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
299 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
300
301 if (!file.isFolder()) {
302 fileSizeV.setVisibility(View.VISIBLE);
303 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
304 lastModV.setVisibility(View.VISIBLE);
305 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
306 // this if-else is needed even thoe fav icon is visible by default
307 // because android reuses views in listview
308 if (!file.keepInSync()) {
309 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
310 } else {
311 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
312 }
313
314 ListView parentList = (ListView)parent;
315 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
316 checkBoxV.setVisibility(View.GONE);
317 } else {
318 if (parentList.isItemChecked(position)) {
319 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
320 } else {
321 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
322 }
323 checkBoxV.setVisibility(View.VISIBLE);
324 }
325
326 // first set thumbnail according to Mimetype, prevents empty thumbnails
327 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
328
329 // get Thumbnail if file is image
330 if (file.isImage()){
331 // Thumbnail in Cache?
332 Bitmap thumbnail = getBitmapFromDiskCache(String.valueOf(file.getRemoteId()));
333 if (thumbnail != null){
334 fileIcon.setImageBitmap(thumbnail);
335 } else {
336 // generate new Thumbnail
337 new BitmapWorkerTask(fileIcon).execute(file);
338 }
339 }
340
341 if (checkIfFileIsSharedWithMe(file)) {
342 sharedWithMeIconV.setVisibility(View.VISIBLE);
343 }
344 }
345 else {
346 fileSizeV.setVisibility(View.INVISIBLE);
347 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
348 lastModV.setVisibility(View.VISIBLE);
349 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
350 checkBoxV.setVisibility(View.GONE);
351 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
352
353 if (checkIfFileIsSharedWithMe(file)) {
354 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
355 sharedWithMeIconV.setVisibility(View.VISIBLE);
356 } else {
357 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
358 }
359
360 // If folder is sharedByLink, icon folder must be changed to
361 // folder-public one
362 if (file.isShareByLink()) {
363 fileIcon.setImageResource(R.drawable.folder_public);
364 }
365 }
366
367 if (file.isShareByLink()) {
368 sharedIconV.setVisibility(View.VISIBLE);
369 } else {
370 sharedIconV.setVisibility(View.GONE);
371 }
372 }
373
374 return view;
375 }
376
377 @Override
378 public int getViewTypeCount() {
379 return 1;
380 }
381
382 @Override
383 public boolean hasStableIds() {
384 return true;
385 }
386
387 @Override
388 public boolean isEmpty() {
389 return (mFiles == null || mFiles.isEmpty());
390 }
391
392 /**
393 * Change the adapted directory for a new one
394 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
395 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
396 */
397 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
398 mFile = directory;
399 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
400 mStorageManager = updatedStorageManager;
401 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
402 }
403 if (mStorageManager != null) {
404 mFiles = mStorageManager.getFolderContent(mFile);
405 } else {
406 mFiles = null;
407 }
408 notifyDataSetChanged();
409 }
410
411 /**
412 * Check if parent folder does not include 'S' permission and if file/folder
413 * is shared with me
414 *
415 * @param file: OCFile
416 * @return boolean: True if it is shared with me and false if it is not
417 */
418 private boolean checkIfFileIsSharedWithMe(OCFile file) {
419 return (mFile.getPermissions() != null && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
420 && file.getPermissions() != null && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
421 }
422 }