2f29c91c54bc9fc49e7b0397a7617ea52cd89a74
[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 private final static String FILE_CONTENTTYPE_FOLDER = "DIR";
53
54 private Context mContext;
55 private OCFile mFile = null;
56 private Vector<OCFile> mFiles = null;
57
58 private FileDataStorageManager mStorageManager;
59 private Account mAccount;
60 private ComponentsGetter mTransferServiceGetter;
61
62 public FileListListAdapter(Context context, ComponentsGetter transferServiceGetter) {
63 mContext = context;
64 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
65 mTransferServiceGetter = transferServiceGetter;
66 }
67
68 @Override
69 public boolean areAllItemsEnabled() {
70 return true;
71 }
72
73 @Override
74 public boolean isEnabled(int position) {
75 return true;
76 }
77
78 @Override
79 public int getCount() {
80 return mFiles != null ? mFiles.size() : 0;
81 }
82
83 @Override
84 public Object getItem(int position) {
85 if (mFiles == null || mFiles.size() <= position)
86 return null;
87 return mFiles.get(position);
88 }
89
90 @Override
91 public long getItemId(int position) {
92 if (mFiles == null || mFiles.size() <= position)
93 return 0;
94 return mFiles.get(position).getFileId();
95 }
96
97 @Override
98 public int getItemViewType(int position) {
99 return 0;
100 }
101
102 @Override
103 public View getView(int position, View convertView, ViewGroup parent) {
104 View view = convertView;
105 if (view == null) {
106 LayoutInflater inflator = (LayoutInflater) mContext
107 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
108 view = inflator.inflate(R.layout.list_item, null);
109 }
110
111 if (mFiles != null && mFiles.size() > position) {
112 OCFile file = mFiles.get(position);
113 TextView fileName = (TextView) view.findViewById(R.id.Filename);
114 String name = file.getFileName();
115
116 fileName.setText(name);
117 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
118 ImageView shareIconV = (ImageView) view.findViewById(R.id.shareIcon);
119 ImageView shareWithMeIconV = (ImageView) view.findViewById(R.id.shareWithMeIcon);
120
121 if (file.isShareByLink()) {
122 shareIconV.setVisibility(View.VISIBLE);
123 } else {
124 shareIconV.setVisibility(View.GONE);
125 }
126
127 // Checks if parent folder does not include 'S' permission
128 if (mFile.getPermissions() != null && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)) {
129
130 // Checks if file/folder is shared with me
131 if (file.getPermissions() != null && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME)) {
132
133 // For folders, also update left icon
134 if (file.getMimetype().equals(FILE_CONTENTTYPE_FOLDER)) {
135 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
136 } else {
137 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
138 }
139 shareWithMeIconV.setVisibility(View.VISIBLE);
140 } else {
141 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
142 }
143 }
144
145 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
146 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
147 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
148 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
149 localStateView.setImageResource(R.drawable.downloading_file_indicator);
150 localStateView.setVisibility(View.VISIBLE);
151 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
152 localStateView.setImageResource(R.drawable.uploading_file_indicator);
153 localStateView.setVisibility(View.VISIBLE);
154 } else if (file.isDown()) {
155 localStateView.setImageResource(R.drawable.local_file_indicator);
156 localStateView.setVisibility(View.VISIBLE);
157 } else {
158 localStateView.setVisibility(View.INVISIBLE);
159 }
160
161 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
162 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
163 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
164
165 if (!file.isFolder()) {
166 fileSizeV.setVisibility(View.VISIBLE);
167 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
168 lastModV.setVisibility(View.VISIBLE);
169 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
170 // this if-else is needed even thoe fav icon is visible by default
171 // because android reuses views in listview
172 if (!file.keepInSync()) {
173 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
174 } else {
175 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
176 }
177
178 ListView parentList = (ListView)parent;
179 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
180 checkBoxV.setVisibility(View.GONE);
181 } else {
182 if (parentList.isItemChecked(position)) {
183 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
184 } else {
185 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
186 }
187 checkBoxV.setVisibility(View.VISIBLE);
188 }
189
190 }
191 else {
192
193 fileSizeV.setVisibility(View.INVISIBLE);
194 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
195 lastModV.setVisibility(View.VISIBLE);
196 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
197 checkBoxV.setVisibility(View.GONE);
198 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
199 }
200 }
201
202 return view;
203 }
204
205 @Override
206 public int getViewTypeCount() {
207 return 1;
208 }
209
210 @Override
211 public boolean hasStableIds() {
212 return true;
213 }
214
215 @Override
216 public boolean isEmpty() {
217 return (mFiles == null || mFiles.isEmpty());
218 }
219
220 /**
221 * Change the adapted directory for a new one
222 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
223 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
224 */
225 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
226 mFile = directory;
227 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
228 mStorageManager = updatedStorageManager;
229 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
230 }
231 if (mStorageManager != null) {
232 mFiles = mStorageManager.getFolderContent(mFile);
233 } else {
234 mFiles = null;
235 }
236 notifyDataSetChanged();
237 }
238
239 }