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