7af483282acc3e4338e76111eff94243b3078eaa
[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.CursorAdapter;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ImageView;
28 import android.widget.ListAdapter;
29 import android.widget.ListView;
30 import android.widget.TextView;
31
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.TransferServiceGetter;
40 import com.owncloud.android.utils.DisplayUtils;
41 import com.owncloud.android.utils.Log_OC;
42
43
44 /**
45 * This Adapter populates a ListView with all files and folders in an ownCloud
46 * instance.
47 *
48 * @author Bartek Przybylski
49 *
50 */
51 public class FileListListAdapter extends CursorAdapter implements ListAdapter {
52
53 private static final String TAG = FileListListAdapter.class.getSimpleName();
54
55 private Context mContext;
56 private FileDataStorageManager mStorageManager;
57 private Account mAccount;
58 private TransferServiceGetter mTransferServiceGetter;
59
60
61 public FileListListAdapter(Context context, TransferServiceGetter transferServiceGetter) {
62 super(context, null, FLAG_AUTO_REQUERY);
63 mContext = context;
64 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
65 mTransferServiceGetter = transferServiceGetter;
66 }
67
68 public void setStorageManager(FileDataStorageManager storageManager) {
69 mStorageManager = storageManager;
70 }
71
72 @Override
73 protected void onContentChanged() {
74 Log_OC.d(TAG, "onContentChanged() start");
75 super.onContentChanged();
76
77 notifyDataSetChanged();
78 Log_OC.d(TAG, "onContentChanged() end");
79 }
80 /*
81 @Override
82 public boolean areAllItemsEnabled() {
83 return true;
84 }
85
86 @Override
87 public boolean isEnabled(int position) {
88 return true;
89 }
90
91 @Override
92 public int getCount() {
93 return mFiles != null ? mFiles.size() : 0;
94 }
95
96 @Override
97 public Object getItem(int position) {
98 if (mFiles == null || mFiles.size() <= position)
99 return null;
100 return mFiles.get(position);
101 }
102
103 @Override
104 public long getItemId(int position) {
105 if (mFiles == null || mFiles.size() <= position)
106 return 0;
107 return mFiles.get(position).getFileId();
108 }
109
110 @Override
111 public int getItemViewType(int position) {
112 return 0;
113 }
114
115 @Override
116 public int getViewTypeCount() {
117 return 1;
118 }
119
120 @Override
121 public boolean hasStableIds() {
122 return true;
123 }
124
125 @Override
126 public boolean isEmpty() {
127 return (mFiles == null || mFiles.isEmpty());
128 }
129 */
130
131 // /**
132 // * Change the adapted directory for a new one
133 // * @param folder New file to adapt. Can be NULL, meaning "no content to adapt".
134 // * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
135 // */
136 // public void swapDirectory(OCFile folder, FileDataStorageManager updatedStorageManager) {
137 // if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
138 // mStorageManager = updatedStorageManager;
139 // mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
140 // }
141 // Cursor newCursor = null;
142 // if (mStorageManager != null) {
143 // //mFiles = mStorageManager.getFolderContent(mFile);
144 // newCursor = mStorageManager.getContent(folder.getFileId());
145 // Uri uri = Uri.withAppendedPath(
146 // ProviderTableMeta.CONTENT_URI_DIR,
147 // String.valueOf(folder.getFileId()));
148 // Log_OC.d(TAG, "swapDirectory Uri " + uri);
149 // newCursor.setNotificationUri(mContext.getContentResolver(), uri);
150 //
151 // }
152 // Cursor oldCursor = swapCursor(newCursor);
153 // if (oldCursor != null){
154 // oldCursor.close();
155 // }
156 // notifyDataSetChanged();
157 // }
158
159 @Override
160 public void bindView(View view, Context context, Cursor cursor) {
161 //Log_OC.d(TAG, "bindView start");
162
163 OCFile file = mStorageManager.createFileInstance(cursor);
164
165 TextView fileName = (TextView) view.findViewById(R.id.Filename);
166 String name = file.getFileName();
167
168 fileName.setText(name);
169 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
170 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype()));
171 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
172 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
173 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
174 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
175 localStateView.setImageResource(R.drawable.downloading_file_indicator);
176 localStateView.setVisibility(View.VISIBLE);
177 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
178 localStateView.setImageResource(R.drawable.uploading_file_indicator);
179 localStateView.setVisibility(View.VISIBLE);
180 } else if (file.isDown()) {
181 localStateView.setImageResource(R.drawable.local_file_indicator);
182 localStateView.setVisibility(View.VISIBLE);
183 } else {
184 localStateView.setVisibility(View.INVISIBLE);
185 }
186
187 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
188 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
189 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
190
191 if (!file.isFolder()) {
192 fileSizeV.setVisibility(View.VISIBLE);
193 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
194 lastModV.setVisibility(View.VISIBLE);
195 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
196 // this if-else is needed even thoe fav icon is visible by default
197 // because android reuses views in listview
198 if (!file.keepInSync()) {
199 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
200 } else {
201 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
202 }
203
204 }
205 else {
206
207 fileSizeV.setVisibility(View.INVISIBLE);
208 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
209 lastModV.setVisibility(View.VISIBLE);
210 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
211 checkBoxV.setVisibility(View.GONE);
212 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
213 }
214
215 ImageView shareIconV = (ImageView) view.findViewById(R.id.shareIcon);
216 if (file.isShareByLink()) {
217 shareIconV.setVisibility(View.VISIBLE);
218 } else {
219 shareIconV.setVisibility(View.INVISIBLE);
220 }
221 //}
222 //Log_OC.d(TAG, "bindView end");
223 }
224
225 @Override
226 public View newView(Context context, Cursor cursor, ViewGroup parent) {
227 //Log_OC.d(TAG, "newView start");
228 LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
229 View view = inflator.inflate(R.layout.list_item, null);
230
231 // TODO check activity to upload
232 ListView parentList = (ListView) parent;
233 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
234 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
235 checkBoxV.setVisibility(View.GONE);
236 } else {
237 /*if (parentList.isItemChecked(position)) {
238 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
239 } else {
240 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
241 }*/
242 checkBoxV.setVisibility(View.VISIBLE);
243 }
244 //Log_OC.d(TAG, "newView end");
245 return view;
246
247 }
248
249 }