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