1bab20b843dd37309aec9ddc9756bd9bebf3604d
[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.ObjectInputStream.GetField;
21 import java.util.Vector;
22
23 import android.accounts.Account;
24 import android.content.Context;
25 import android.content.res.Resources;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapFactory;
28 import android.media.ThumbnailUtils;
29 import android.util.TypedValue;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.BaseAdapter;
34 import android.widget.GridView;
35 import android.widget.ImageView;
36 import android.widget.ListAdapter;
37 import android.widget.ListView;
38 import android.widget.TextView;
39
40 import com.owncloud.android.R;
41 import com.owncloud.android.authentication.AccountUtils;
42 import com.owncloud.android.datamodel.FileDataStorageManager;
43 import com.owncloud.android.datamodel.OCFile;
44 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
45 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
46 import com.owncloud.android.ui.activity.ComponentsGetter;
47 import com.owncloud.android.utils.DisplayUtils;
48 import com.owncloud.android.utils.Log_OC;
49
50
51 /**
52 * This Adapter populates a ListView with all files and folders in an ownCloud
53 * instance.
54 *
55 * @author Bartek Przybylski
56 *
57 */
58 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
59 private final static String PERMISSION_SHARED_WITH_ME = "S";
60
61 private Context mContext;
62 private OCFile mFile = null;
63 private Vector<OCFile> mFiles = null;
64
65 private FileDataStorageManager mStorageManager;
66 private Account mAccount;
67 private ComponentsGetter mTransferServiceGetter;
68
69 public FileListListAdapter(Context context, ComponentsGetter transferServiceGetter) {
70 mContext = context;
71 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
72 mTransferServiceGetter = transferServiceGetter;
73 }
74
75 @Override
76 public boolean areAllItemsEnabled() {
77 return true;
78 }
79
80 @Override
81 public boolean isEnabled(int position) {
82 return true;
83 }
84
85 @Override
86 public int getCount() {
87 return mFiles != null ? mFiles.size() : 0;
88 }
89
90 @Override
91 public Object getItem(int position) {
92 if (mFiles == null || mFiles.size() <= position)
93 return null;
94 return mFiles.get(position);
95 }
96
97 @Override
98 public long getItemId(int position) {
99 if (mFiles == null || mFiles.size() <= position)
100 return 0;
101 return mFiles.get(position).getFileId();
102 }
103
104 @Override
105 public int getItemViewType(int position) {
106 return 0;
107 }
108
109 @Override
110 public View getView(int position, View convertView, ViewGroup parent) {
111 // decide image vs. file view
112 double count = 0;
113
114
115 for (OCFile file : mFiles){
116 if (file.isImage()){
117 count++;
118 }
119 }
120
121 // > 50% Images --> image view
122 boolean fileView = true;
123 if ((count / mFiles.size()) >= 0.5){
124 Log_OC.i("FileListListAdapter", "Image View");
125 fileView = false;
126 } else {
127 Log_OC.i("FileListListAdapter", "File View");
128 fileView = true;
129 }
130
131 View view = convertView;
132 // if (view == null) {
133 LayoutInflater inflator = (LayoutInflater) mContext
134 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
135 if (fileView){
136 view = inflator.inflate(R.layout.list_item, null);
137 } else {
138 view = inflator.inflate(R.layout.image_item, null);
139 View frame = view.findViewById(R.id.imageItemFrame);
140 frame.setVisibility(View.GONE);
141 }
142 // }
143 view.invalidate();
144
145 if (mFiles != null && mFiles.size() > position) {
146 OCFile file = mFiles.get(position);
147 TextView fileName = (TextView) view.findViewById(R.id.Filename);
148 if (!fileView){fileName.setVisibility(View.GONE);}
149 String name = file.getFileName();
150
151 fileName.setText(name);
152 ImageView fileIcon = (ImageView) view.findViewById(R.id.thumbnail);
153 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
154 ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
155 sharedWithMeIconV.setVisibility(View.GONE);
156
157 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
158 localStateView.bringToFront();
159 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
160 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
161 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
162 localStateView.setImageResource(R.drawable.downloading_file_indicator);
163 localStateView.setVisibility(View.VISIBLE);
164 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
165 localStateView.setImageResource(R.drawable.uploading_file_indicator);
166 localStateView.setVisibility(View.VISIBLE);
167 } else if (file.isDown()) {
168 localStateView.setImageResource(R.drawable.local_file_indicator);
169 localStateView.setVisibility(View.VISIBLE);
170 } else {
171 localStateView.setVisibility(View.INVISIBLE);
172 }
173
174 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
175 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
176 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
177
178 if (!file.isFolder()) {
179 fileSizeV.setVisibility(View.VISIBLE);
180 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
181 lastModV.setVisibility(View.VISIBLE);
182 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
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 GridView parentList = (GridView)parent;
192 if (parentList.getChoiceMode() == GridView.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 // generate Thumbnail if file is available local and image
204 if (file.isDown() && file.isImage()){
205 // Converts dp to pixel
206 Resources r = mContext.getResources();
207 int px = (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()));
208 Bitmap bitmap = BitmapFactory.decodeFile(file.getStoragePath());
209 fileIcon.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap, px, px));
210 } else {
211 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
212 }
213
214 if (checkIfFileIsSharedWithMe(file)) {
215 sharedWithMeIconV.setVisibility(View.VISIBLE);
216 }
217 }
218 else {
219 fileSizeV.setVisibility(View.INVISIBLE);
220 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
221 lastModV.setVisibility(View.VISIBLE);
222 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
223 checkBoxV.setVisibility(View.GONE);
224 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
225
226 if (checkIfFileIsSharedWithMe(file)) {
227 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
228 sharedWithMeIconV.setVisibility(View.VISIBLE);
229 } else {
230 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
231 }
232
233 // If folder is sharedByLink, icon folder must be changed to
234 // folder-public one
235 if (file.isShareByLink()) {
236 fileIcon.setImageResource(R.drawable.folder_public);
237 }
238 }
239
240 if (file.isShareByLink()) {
241 sharedIconV.setVisibility(View.VISIBLE);
242 } else {
243 sharedIconV.setVisibility(View.GONE);
244 }
245 }
246
247 return view;
248 }
249
250 @Override
251 public int getViewTypeCount() {
252 return 1;
253 }
254
255 @Override
256 public boolean hasStableIds() {
257 return true;
258 }
259
260 @Override
261 public boolean isEmpty() {
262 return (mFiles == null || mFiles.isEmpty());
263 }
264
265 /**
266 * Change the adapted directory for a new one
267 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
268 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
269 */
270 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
271 mFile = directory;
272 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
273 mStorageManager = updatedStorageManager;
274 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
275 }
276 if (mStorageManager != null) {
277 mFiles = mStorageManager.getFolderContent(mFile);
278 } else {
279 mFiles = null;
280 }
281 notifyDataSetChanged();
282 }
283
284 /**
285 * Check if parent folder does not include 'S' permission and if file/folder
286 * is shared with me
287 *
288 * @param file: OCFile
289 * @return boolean: True if it is shared with me and false if it is not
290 */
291 private boolean checkIfFileIsSharedWithMe(OCFile file) {
292 return (mFile.getPermissions() != null && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
293 && file.getPermissions() != null && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
294 }
295 }