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