33f16c834f67f3d3e682b4a6c770a22b6b731588
[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
21 import java.io.File;
22 import java.util.Vector;
23
24 import android.accounts.Account;
25 import android.content.Context;
26 import android.content.SharedPreferences;
27 import android.graphics.Bitmap;
28 import android.preference.PreferenceManager;
29 import android.text.format.DateUtils;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.BaseAdapter;
34 import android.widget.ImageView;
35 import android.widget.ListAdapter;
36 import android.widget.ListView;
37 import android.widget.TextView;
38
39 import com.owncloud.android.R;
40 import com.owncloud.android.authentication.AccountUtils;
41 import com.owncloud.android.datamodel.FileDataStorageManager;
42 import com.owncloud.android.datamodel.OCFile;
43 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
44 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
45 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
46 import com.owncloud.android.ui.activity.ComponentsGetter;
47 import com.owncloud.android.utils.DisplayUtils;
48 import com.owncloud.android.utils.FileStorageUtils;
49
50
51 /**
52 * This Adapter populates a ListView with all files and folders in an ownCloud
53 * instance.
54 *
55 * @author Bartek Przybylski
56 * @author Tobias Kaminsky
57 * @author David A. Velasco
58 */
59 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
60 private final static String PERMISSION_SHARED_WITH_ME = "S";
61
62 private Context mContext;
63 private OCFile mFile = null;
64 private Vector<OCFile> mFiles = null;
65 private boolean mJustFolders;
66
67 private FileDataStorageManager mStorageManager;
68 private Account mAccount;
69 private ComponentsGetter mTransferServiceGetter;
70
71 private SharedPreferences mAppPreferences;
72
73 public FileListListAdapter(
74 boolean justFolders,
75 Context context,
76 ComponentsGetter transferServiceGetter
77 ) {
78
79 mJustFolders = justFolders;
80 mContext = context;
81 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
82
83 mTransferServiceGetter = transferServiceGetter;
84
85 mAppPreferences = PreferenceManager
86 .getDefaultSharedPreferences(mContext);
87
88 // Read sorting order, default to sort by name ascending
89 FileStorageUtils.mSortOrder = mAppPreferences.getInt("sortOrder", 0);
90 FileStorageUtils.mSortAscending = mAppPreferences.getBoolean("sortAscending", true);
91
92
93 // initialise thumbnails cache on background thread
94 new ThumbnailsCacheManager.InitDiskCacheTask().execute();
95
96 }
97
98 @Override
99 public boolean areAllItemsEnabled() {
100 return true;
101 }
102
103 @Override
104 public boolean isEnabled(int position) {
105 return true;
106 }
107
108 @Override
109 public int getCount() {
110 return mFiles != null ? mFiles.size() : 0;
111 }
112
113 @Override
114 public Object getItem(int position) {
115 if (mFiles == null || mFiles.size() <= position)
116 return null;
117 return mFiles.get(position);
118 }
119
120 @Override
121 public long getItemId(int position) {
122 if (mFiles == null || mFiles.size() <= position)
123 return 0;
124 return mFiles.get(position).getFileId();
125 }
126
127 @Override
128 public int getItemViewType(int position) {
129 return 0;
130 }
131
132 @Override
133 public View getView(int position, View convertView, ViewGroup parent) {
134 View view = convertView;
135 if (view == null) {
136 LayoutInflater inflator = (LayoutInflater) mContext
137 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
138 view = inflator.inflate(R.layout.list_item, null);
139 }
140
141 if (mFiles != null && mFiles.size() > position) {
142 OCFile file = mFiles.get(position);
143 TextView fileName = (TextView) view.findViewById(R.id.Filename);
144 String name = file.getFileName();
145
146 fileName.setText(name);
147 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
148 fileIcon.setTag(file.getFileId());
149 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
150 ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
151 sharedWithMeIconV.setVisibility(View.GONE);
152
153 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
154 localStateView.bringToFront();
155 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
156 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
157 //if (file.isSynchronizing() || file.isDownloading()) {
158 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
159 localStateView.setImageResource(R.drawable.downloading_file_indicator);
160 localStateView.setVisibility(View.VISIBLE);
161 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
162 localStateView.setImageResource(R.drawable.uploading_file_indicator);
163 localStateView.setVisibility(View.VISIBLE);
164 } else if (file.isDown()) {
165 localStateView.setImageResource(R.drawable.local_file_indicator);
166 localStateView.setVisibility(View.VISIBLE);
167 } else {
168 localStateView.setVisibility(View.INVISIBLE);
169 }
170
171 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
172 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
173 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
174
175 if (!file.isFolder()) {
176 fileSizeV.setVisibility(View.VISIBLE);
177 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
178 lastModV.setVisibility(View.VISIBLE);
179 lastModV.setText(showRelativeTimestamp(file));
180 // this if-else is needed even thoe fav icon is visible by default
181 // because android reuses views in listview
182 if (!file.keepInSync()) {
183 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
184 } else {
185 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
186 }
187
188 ListView parentList = (ListView)parent;
189 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
190 checkBoxV.setVisibility(View.GONE);
191 } else {
192 if (parentList.isItemChecked(position)) {
193 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
194 } else {
195 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
196 }
197 checkBoxV.setVisibility(View.VISIBLE);
198 }
199
200 // get Thumbnail if file is image
201 if (file.isImage() && file.getRemoteId() != null){
202 // Thumbnail in Cache?
203 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
204 String.valueOf(file.getRemoteId())
205 );
206 if (thumbnail != null && !file.needsUpdateThumbnail()){
207 fileIcon.setImageBitmap(thumbnail);
208 } else {
209
210 // generate new Thumbnail
211 if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
212 final ThumbnailsCacheManager.ThumbnailGenerationTask task =
213 new ThumbnailsCacheManager.ThumbnailGenerationTask(
214 fileIcon, mStorageManager, mAccount
215 );
216 if (thumbnail == null) {
217 thumbnail = ThumbnailsCacheManager.mDefaultImg;
218 }
219 final ThumbnailsCacheManager.AsyncDrawable asyncDrawable =
220 new ThumbnailsCacheManager.AsyncDrawable(
221 mContext.getResources(),
222 thumbnail,
223 task
224 );
225 fileIcon.setImageDrawable(asyncDrawable);
226 task.execute(file);
227 }
228 }
229 } else {
230 fileIcon.setImageResource(DisplayUtils.getFileTypeIconId(file.getMimetype(), file.getFileName()));
231 }
232
233 if (checkIfFileIsSharedWithMe(file)) {
234 sharedWithMeIconV.setVisibility(View.VISIBLE);
235 }
236 }
237 else {
238 // TODO Re-enable when server supports folder-size calculation
239 // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
240 // fileSizeV.setVisibility(View.VISIBLE);
241 // fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
242 // } else {
243 fileSizeV.setVisibility(View.INVISIBLE);
244 // }
245
246 lastModV.setVisibility(View.VISIBLE);
247 lastModV.setText(showRelativeTimestamp(file));
248 checkBoxV.setVisibility(View.GONE);
249 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
250
251 if (checkIfFileIsSharedWithMe(file)) {
252 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
253 sharedWithMeIconV.setVisibility(View.VISIBLE);
254 } else {
255 fileIcon.setImageResource(
256 DisplayUtils.getFileTypeIconId(file.getMimetype(), file.getFileName())
257 );
258 }
259
260 // If folder is sharedByLink, icon folder must be changed to
261 // folder-public one
262 if (file.isShareByLink()) {
263 fileIcon.setImageResource(R.drawable.folder_public);
264 }
265 }
266
267 if (file.isShareByLink()) {
268 sharedIconV.setVisibility(View.VISIBLE);
269 } else {
270 sharedIconV.setVisibility(View.GONE);
271 }
272 }
273
274 return view;
275 }
276
277 /**
278 * Local Folder size in human readable format
279 *
280 * @param path
281 * String
282 * @return Size in human readable format
283 */
284 private String getFolderSizeHuman(String path) {
285
286 File dir = new File(path);
287
288 if (dir.exists()) {
289 long bytes = FileStorageUtils.getFolderSize(dir);
290 return DisplayUtils.bytesToHumanReadable(bytes);
291 }
292
293 return "0 B";
294 }
295
296 /**
297 * Local Folder size
298 * @param dir File
299 * @return Size in bytes
300 */
301 private long getFolderSize(File dir) {
302 if (dir.exists()) {
303 long result = 0;
304 File[] fileList = dir.listFiles();
305 for(int i = 0; i < fileList.length; i++) {
306 if(fileList[i].isDirectory()) {
307 result += getFolderSize(fileList[i]);
308 } else {
309 result += fileList[i].length();
310 }
311 }
312 return result;
313 }
314 return 0;
315 }
316
317 @Override
318 public int getViewTypeCount() {
319 return 1;
320 }
321
322 @Override
323 public boolean hasStableIds() {
324 return true;
325 }
326
327 @Override
328 public boolean isEmpty() {
329 return (mFiles == null || mFiles.isEmpty());
330 }
331
332 /**
333 * Change the adapted directory for a new one
334 * @param directory New file to adapt. Can be NULL, meaning
335 * "no content to adapt".
336 * @param updatedStorageManager Optional updated storage manager; used to replace
337 * mStorageManager if is different (and not NULL)
338 */
339 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
340 mFile = directory;
341 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
342 mStorageManager = updatedStorageManager;
343 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
344 }
345 if (mStorageManager != null) {
346 mFiles = mStorageManager.getFolderContent(mFile);
347 if (mJustFolders) {
348 mFiles = getFolders(mFiles);
349 }
350 } else {
351 mFiles = null;
352 }
353
354 mFiles = FileStorageUtils.sortFolder(mFiles);
355 notifyDataSetChanged();
356 }
357
358
359 /**
360 * Filter for getting only the folders
361 * @param files
362 * @return Vector<OCFile>
363 */
364 public Vector<OCFile> getFolders(Vector<OCFile> files) {
365 Vector<OCFile> ret = new Vector<OCFile>();
366 OCFile current = null;
367 for (int i=0; i<files.size(); i++) {
368 current = files.get(i);
369 if (current.isFolder()) {
370 ret.add(current);
371 }
372 }
373 return ret;
374 }
375
376
377 /**
378 * Check if parent folder does not include 'S' permission and if file/folder
379 * is shared with me
380 *
381 * @param file: OCFile
382 * @return boolean: True if it is shared with me and false if it is not
383 */
384 private boolean checkIfFileIsSharedWithMe(OCFile file) {
385 return (mFile.getPermissions() != null
386 && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
387 && file.getPermissions() != null
388 && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
389 }
390
391 public void setSortOrder(Integer order, boolean ascending) {
392 SharedPreferences.Editor editor = mAppPreferences.edit();
393 editor.putInt("sortOrder", order);
394 editor.putBoolean("sortAscending", ascending);
395 editor.commit();
396
397 FileStorageUtils.mSortOrder = order;
398 FileStorageUtils.mSortAscending = ascending;
399
400
401 mFiles = FileStorageUtils.sortFolder(mFiles);
402 notifyDataSetChanged();
403
404 }
405
406 private CharSequence showRelativeTimestamp(OCFile file){
407 return DisplayUtils.getRelativeDateTimeString(mContext, file.getModificationTimestamp(),
408 DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
409 }
410 }