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