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