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