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