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