0a8dab1068651a1dd46cfda9a4a584102bfc8e34
[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.util.Vector;
21
22 import android.accounts.Account;
23 import android.content.Context;
24 import android.graphics.Bitmap;
25 import android.graphics.BitmapFactory;
26 import android.media.ThumbnailUtils;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.BaseAdapter;
31 import android.widget.GridView;
32 import android.widget.ImageView;
33 import android.widget.ListAdapter;
34 import android.widget.ListView;
35 import android.widget.TextView;
36
37 import com.owncloud.android.R;
38 import com.owncloud.android.authentication.AccountUtils;
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.Log_OC;
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 *
54 */
55 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
56 private final static String PERMISSION_SHARED_WITH_ME = "S";
57
58 private Context mContext;
59 private OCFile mFile = null;
60 private Vector<OCFile> mFiles = null;
61 private boolean mJustFolders;
62
63 private FileDataStorageManager mStorageManager;
64 private Account mAccount;
65 private ComponentsGetter mTransferServiceGetter;
66 private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM };
67
68 public FileListListAdapter(
69 boolean justFolders,
70 Context context,
71 ComponentsGetter transferServiceGetter
72 ) {
73 mJustFolders = justFolders;
74 mContext = context;
75 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
76 mTransferServiceGetter = transferServiceGetter;
77 }
78
79 @Override
80 public boolean areAllItemsEnabled() {
81 return true;
82 }
83
84 @Override
85 public boolean isEnabled(int position) {
86 return true;
87 }
88
89 @Override
90 public int getCount() {
91 return mFiles != null ? mFiles.size() : 0;
92 }
93
94 @Override
95 public Object getItem(int position) {
96 if (mFiles == null || mFiles.size() <= position)
97 return null;
98 return mFiles.get(position);
99 }
100
101 @Override
102 public long getItemId(int position) {
103 if (mFiles == null || mFiles.size() <= position)
104 return 0;
105 return mFiles.get(position).getFileId();
106 }
107
108 @Override
109 public int getItemViewType(int position) {
110 return 0;
111 }
112
113 @Override
114 public View getView(int position, View convertView, ViewGroup parent) {
115 // decide image vs. file view
116 double count = 0;
117
118
119 for (OCFile file : mFiles){
120 if (file.isImage()){
121 count++;
122 }
123 }
124
125 // > 50% Images --> image view
126 boolean fileView = true;
127 if ((count / mFiles.size()) >= 0.8){
128 fileView = false;
129 } else {
130 fileView = true;
131 }
132
133 View view = convertView;
134 OCFile file = null;
135 LayoutInflater inflator = (LayoutInflater) mContext
136 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
137
138 if (mFiles != null && mFiles.size() > position) {
139 file = mFiles.get(position);
140 }
141
142 // Find out which layout should be displayed
143 ViewType viewType;
144 if (fileView){
145 viewType = ViewType.LIST_ITEM;
146 } else if (file.isImage()){
147 viewType = ViewType.GRID_IMAGE;
148 } else {
149 viewType = ViewType.GRID_ITEM;
150 }
151
152 // Create View
153 switch (viewType){
154 case GRID_IMAGE:
155 view = inflator.inflate(R.layout.grid_image, null);
156 break;
157 case GRID_ITEM:
158 view = inflator.inflate(R.layout.grid_item, null);
159 break;
160 case LIST_ITEM:
161 view = inflator.inflate(R.layout.list_item, null);
162 break;
163 }
164
165 view.invalidate();
166
167 if (file != null){
168
169 ImageView fileIcon = (ImageView) view.findViewById(R.id.thumbnail);
170 TextView fileName;
171 String name;
172
173 switch (viewType){
174 case LIST_ITEM:
175 fileName = (TextView) view.findViewById(R.id.Filename);
176 name = file.getFileName();
177 fileName.setText(name);
178
179 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
180 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
181 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
182
183 lastModV.setVisibility(View.VISIBLE);
184 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
185
186 checkBoxV.setVisibility(View.GONE);
187
188 fileSizeV.setVisibility(View.VISIBLE);
189 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
190
191 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
192
193
194 if (file.isShareByLink()) {
195 sharedIconV.setVisibility(View.VISIBLE);
196 } else {
197 sharedIconV.setVisibility(View.GONE);
198 }
199
200 ImageView localStateView = (ImageView) view.findViewById(R.id.localFileIndicator);
201
202 if (!file.isFolder()) {
203 GridView parentList = (GridView)parent;
204 if (parentList.getChoiceMode() == GridView.CHOICE_MODE_NONE) {
205 checkBoxV.setVisibility(View.GONE);
206 } else {
207 if (parentList.isItemChecked(position)) {
208 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
209 } else {
210 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
211 }
212 checkBoxV.setVisibility(View.VISIBLE);
213 }
214
215 localStateView.bringToFront();
216 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
217 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
218 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
219 localStateView.setImageResource(R.drawable.downloading_file_indicator);
220 localStateView.setVisibility(View.VISIBLE);
221 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
222 localStateView.setImageResource(R.drawable.uploading_file_indicator);
223 localStateView.setVisibility(View.VISIBLE);
224 } else if (file.isDown()) {
225 localStateView.setImageResource(R.drawable.local_file_indicator);
226 localStateView.setVisibility(View.VISIBLE);
227 } else {
228 localStateView.setVisibility(View.INVISIBLE);
229 }
230
231 ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
232 if (checkIfFileIsSharedWithMe(file)) {
233 sharedWithMeIconV.setVisibility(View.VISIBLE);
234 } else {
235 sharedWithMeIconV.setVisibility(View.GONE);
236 }
237 } else {
238 localStateView.setVisibility(View.INVISIBLE);
239 }
240 break;
241 case GRID_ITEM:
242 fileName = (TextView) view.findViewById(R.id.Filename);
243 name = file.getFileName();
244 fileName.setText(name);
245 break;
246 case GRID_IMAGE:
247 break;
248 }
249
250 // For all Views
251
252 // this if-else is needed even though favorite icon is visible by default
253 // because android reuses views in listview
254 if (!file.keepInSync()) {
255 view.findViewById(R.id.favoriteIcon).setVisibility(View.GONE);
256 } else {
257 view.findViewById(R.id.favoriteIcon).setVisibility(View.VISIBLE);
258 }
259
260 // No Folder
261 if (!file.isFolder()) {
262 if (file.isImage() && file.isDown()){
263 Bitmap bitmap = BitmapFactory.decodeFile(file.getStoragePath());
264 fileIcon.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap, 200, 200));
265 } else {
266 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
267 }
268 } else {
269 // Folder
270 if (checkIfFileIsSharedWithMe(file)) {
271 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
272 } else if (file.isShareByLink()) {
273 // If folder is sharedByLink, icon folder must be changed to
274 // folder-public one
275 fileIcon.setImageResource(R.drawable.folder_public);
276 } else {
277 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
278 }
279 }
280 }
281
282 return view;
283 }
284
285 @Override
286 public int getViewTypeCount() {
287 return 1;
288 }
289
290 @Override
291 public boolean hasStableIds() {
292 return true;
293 }
294
295 @Override
296 public boolean isEmpty() {
297 return (mFiles == null || mFiles.isEmpty());
298 }
299
300 /**
301 * Change the adapted directory for a new one
302 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
303 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
304 */
305 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
306 mFile = directory;
307 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
308 mStorageManager = updatedStorageManager;
309 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
310 }
311 if (mStorageManager != null) {
312 mFiles = mStorageManager.getFolderContent(mFile);
313 if (mJustFolders) {
314 mFiles = getFolders(mFiles);
315 }
316 } else {
317 mFiles = null;
318 }
319 notifyDataSetChanged();
320 }
321
322
323 /**
324 * Filter for getting only the folders
325 * @param files
326 * @return Vector<OCFile>
327 */
328 public Vector<OCFile> getFolders(Vector<OCFile> files) {
329 Vector<OCFile> ret = new Vector<OCFile>();
330 OCFile current = null;
331 for (int i=0; i<files.size(); i++) {
332 current = files.get(i);
333 if (current.isFolder()) {
334 ret.add(current);
335 }
336 }
337 return ret;
338 }
339
340
341 /**
342 * Check if parent folder does not include 'S' permission and if file/folder
343 * is shared with me
344 *
345 * @param file: OCFile
346 * @return boolean: True if it is shared with me and false if it is not
347 */
348 private boolean checkIfFileIsSharedWithMe(OCFile file) {
349 return (mFile.getPermissions() != null && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
350 && file.getPermissions() != null && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
351 }
352 }