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