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