2913c9e6f01227c5c19cffe73cd5ffdac9c8cd1e
[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 public FileListListAdapter(Context context, TransferServiceGetter transferServiceGetter) {
61 super(context, null, 0);
62 mContext = context;
63 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
64 mTransferServiceGetter = transferServiceGetter;
65 }
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 int getViewTypeCount() {
104 return 1;
105 }
106
107 @Override
108 public boolean hasStableIds() {
109 return true;
110 }
111
112 @Override
113 public boolean isEmpty() {
114 return (mFiles == null || mFiles.isEmpty());
115 }
116 */
117
118 /**
119 * Change the adapted directory for a new one
120 * @param folder New file to adapt. Can be NULL, meaning "no content to adapt".
121 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
122 */
123 public void swapDirectory(OCFile folder, FileDataStorageManager updatedStorageManager) {
124 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
125 mStorageManager = updatedStorageManager;
126 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
127 }
128 Cursor newCursor = null;
129 if (mStorageManager != null) {
130 //mFiles = mStorageManager.getFolderContent(mFile);
131 newCursor = mStorageManager.getContent(folder.getFileId());
132 }
133 Cursor oldCursor = swapCursor(newCursor);
134 if (oldCursor != null){
135 oldCursor.close();
136 }
137 notifyDataSetChanged();
138 }
139
140 @Override
141 public void bindView(View view, Context context, Cursor cursor) {
142 Log_OC.d(TAG, "bindView start");
143
144 OCFile file = mStorageManager.createFileInstance(cursor);
145
146 TextView fileName = (TextView) view.findViewById(R.id.Filename);
147 String name = file.getFileName();
148
149 fileName.setText(name);
150 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
151 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype()));
152 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
153 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
154 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
155 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
156 localStateView.setImageResource(R.drawable.downloading_file_indicator);
157 localStateView.setVisibility(View.VISIBLE);
158 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
159 localStateView.setImageResource(R.drawable.uploading_file_indicator);
160 localStateView.setVisibility(View.VISIBLE);
161 } else if (file.isDown()) {
162 localStateView.setImageResource(R.drawable.local_file_indicator);
163 localStateView.setVisibility(View.VISIBLE);
164 } else {
165 localStateView.setVisibility(View.INVISIBLE);
166 }
167
168 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
169 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
170 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
171
172 if (!file.isFolder()) {
173 fileSizeV.setVisibility(View.VISIBLE);
174 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
175 lastModV.setVisibility(View.VISIBLE);
176 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
177 // this if-else is needed even thoe fav icon is visible by default
178 // because android reuses views in listview
179 if (!file.keepInSync()) {
180 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
181 } else {
182 view.findViewById(R.id.imageView3).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 Log_OC.d(TAG, "bindView end");
204 }
205
206 @Override
207 public View newView(Context context, Cursor cursor, ViewGroup parent) {
208 Log_OC.d(TAG, "newView start");
209 LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
210 View view = inflator.inflate(R.layout.list_item, null);
211
212 // TODO check activity to upload
213 ListView parentList = (ListView) parent;
214 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
215 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
216 checkBoxV.setVisibility(View.GONE);
217 } else {
218 /*if (parentList.isItemChecked(position)) {
219 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
220 } else {
221 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
222 }*/
223 checkBoxV.setVisibility(View.VISIBLE);
224 }
225 Log_OC.d(TAG, "newView end");
226 return view;
227
228 }
229
230 }