273f936045fb8da786e58b1074eac58e5e64987b
[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.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.files.services.FileDownloader.FileDownloaderBinder;
44 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
45 import com.owncloud.android.ui.activity.ComponentsGetter;
46 import com.owncloud.android.utils.DisplayUtils;
47 import com.owncloud.android.utils.Log_OC;
48
49
50 /**
51 * This Adapter populates a ListView with all files and folders in an ownCloud
52 * instance.
53 *
54 * @author Bartek Przybylski
55 *
56 */
57 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
58 private final static String PERMISSION_SHARED_WITH_ME = "S";
59
60 private Context mContext;
61 private OCFile mFile = null;
62 private Vector<OCFile> mFiles = null;
63
64 private FileDataStorageManager mStorageManager;
65 private Account mAccount;
66 private ComponentsGetter mTransferServiceGetter;
67
68 public FileListListAdapter(Context context, ComponentsGetter transferServiceGetter) {
69 mContext = context;
70 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
71 mTransferServiceGetter = transferServiceGetter;
72 }
73
74 @Override
75 public boolean areAllItemsEnabled() {
76 return true;
77 }
78
79 @Override
80 public boolean isEnabled(int position) {
81 return true;
82 }
83
84 @Override
85 public int getCount() {
86 return mFiles != null ? mFiles.size() : 0;
87 }
88
89 @Override
90 public Object getItem(int position) {
91 if (mFiles == null || mFiles.size() <= position)
92 return null;
93 return mFiles.get(position);
94 }
95
96 @Override
97 public long getItemId(int position) {
98 if (mFiles == null || mFiles.size() <= position)
99 return 0;
100 return mFiles.get(position).getFileId();
101 }
102
103 @Override
104 public int getItemViewType(int position) {
105 return 0;
106 }
107
108 @Override
109 public View getView(int position, View convertView, ViewGroup parent) {
110
111
112 View view = convertView;
113 if (view == null) {
114 LayoutInflater inflator = (LayoutInflater) mContext
115 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
116 view = inflator.inflate(R.layout.list_item, null);
117
118 }
119
120 if (mFiles != null && mFiles.size() > position) {
121 OCFile file = mFiles.get(position);
122 TextView fileName = (TextView) view.findViewById(R.id.Filename);
123 fileName.setVisibility(View.GONE);
124 String name = file.getFileName();
125
126 fileName.setText(name);
127 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
128 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
129 ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
130 sharedWithMeIconV.setVisibility(View.GONE);
131
132 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
133 localStateView.bringToFront();
134 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
135 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
136 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
137 localStateView.setImageResource(R.drawable.downloading_file_indicator);
138 localStateView.setVisibility(View.VISIBLE);
139 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
140 localStateView.setImageResource(R.drawable.uploading_file_indicator);
141 localStateView.setVisibility(View.VISIBLE);
142 } else if (file.isDown()) {
143 localStateView.setImageResource(R.drawable.local_file_indicator);
144 localStateView.setVisibility(View.VISIBLE);
145 } else {
146 localStateView.setVisibility(View.INVISIBLE);
147 }
148
149 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
150 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
151 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
152
153 if (!file.isFolder()) {
154 fileSizeV.setVisibility(View.VISIBLE);
155 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
156 lastModV.setVisibility(View.VISIBLE);
157 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
158 // this if-else is needed even thoe fav icon is visible by default
159 // because android reuses views in listview
160 if (!file.keepInSync()) {
161 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
162 } else {
163 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
164 }
165
166 ListView parentList = (ListView)parent;
167 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
168 checkBoxV.setVisibility(View.GONE);
169 } else {
170 if (parentList.isItemChecked(position)) {
171 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
172 } else {
173 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
174 }
175 checkBoxV.setVisibility(View.VISIBLE);
176 }
177
178 // generate Thumbnail if file is available local and image
179 if (file.isDown() && file.isImage()){
180 // Converts dp to pixel
181 Resources r = mContext.getResources();
182 int px = (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()));
183 Bitmap bitmap = BitmapFactory.decodeFile(file.getStoragePath());
184 fileIcon.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap, px, px));
185 } else {
186 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
187 }
188
189 if (checkIfFileIsSharedWithMe(file)) {
190 sharedWithMeIconV.setVisibility(View.VISIBLE);
191 }
192 }
193 else {
194 fileSizeV.setVisibility(View.INVISIBLE);
195 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
196 lastModV.setVisibility(View.VISIBLE);
197 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
198 checkBoxV.setVisibility(View.GONE);
199 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
200
201 if (checkIfFileIsSharedWithMe(file)) {
202 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
203 sharedWithMeIconV.setVisibility(View.VISIBLE);
204 } else {
205 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
206 }
207
208 // If folder is sharedByLink, icon folder must be changed to
209 // folder-public one
210 if (file.isShareByLink()) {
211 fileIcon.setImageResource(R.drawable.folder_public);
212 }
213 }
214
215 if (file.isShareByLink()) {
216 sharedIconV.setVisibility(View.VISIBLE);
217 } else {
218 sharedIconV.setVisibility(View.GONE);
219 }
220 }
221
222 return view;
223 }
224
225 @Override
226 public int getViewTypeCount() {
227 return 1;
228 }
229
230 @Override
231 public boolean hasStableIds() {
232 return true;
233 }
234
235 @Override
236 public boolean isEmpty() {
237 return (mFiles == null || mFiles.isEmpty());
238 }
239
240 /**
241 * Change the adapted directory for a new one
242 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
243 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
244 */
245 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
246 mFile = directory;
247 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
248 mStorageManager = updatedStorageManager;
249 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
250 }
251 if (mStorageManager != null) {
252 mFiles = mStorageManager.getFolderContent(mFile);
253 } else {
254 mFiles = null;
255 }
256 notifyDataSetChanged();
257 }
258
259 /**
260 * Check if parent folder does not include 'S' permission and if file/folder
261 * is shared with me
262 *
263 * @param file: OCFile
264 * @return boolean: True if it is shared with me and false if it is not
265 */
266 private boolean checkIfFileIsSharedWithMe(OCFile file) {
267 return (mFile.getPermissions() != null && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
268 && file.getPermissions() != null && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
269 }
270 }