21cfe0b41c60b5fb51f7612344ddca313e7e7c5c
[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.support.v4.widget.SimpleCursorAdapter;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 //import android.widget.BaseAdapter;
27 import android.widget.ImageView;
28 import android.widget.ListAdapter;
29 import android.widget.ListView;
30 import android.widget.TextView;
31
32
33 import java.util.Vector;
34
35 import com.owncloud.android.R;
36 import com.owncloud.android.authentication.AccountUtils;
37 import com.owncloud.android.datamodel.FileDataStorageManager;
38 import com.owncloud.android.datamodel.OCFile;
39 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
40 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
41 import com.owncloud.android.ui.activity.ComponentsGetter;
42 import com.owncloud.android.utils.DisplayUtils;
43
44
45 /**
46 * This Adapter populates a ListView with all files and folders in an ownCloud
47 * instance.
48 *
49 * @author Bartek Przybylski
50 *
51 */
52 public class FileListListAdapter extends SimpleCursorAdapter implements ListAdapter {
53
54 private Context mContext;
55 private static OCFile mFile = null;
56 private Vector<OCFile> mFiles = null;
57 private static FileDataStorageManager mStorageManager;
58 private Account mAccount;
59 private ComponentsGetter mTransferServiceGetter;
60
61
62 public FileListListAdapter(Context context, ComponentsGetter componentsGetter) {
63 super(context, 0, null, null, null, 0);
64 mContext = context;
65 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
66 mTransferServiceGetter = componentsGetter; }
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 View getView(int position, View convertView, ViewGroup parent) {
104 View view = convertView;
105 if (view == null) {
106 LayoutInflater inflator = (LayoutInflater) mContext
107 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
108 view = inflator.inflate(R.layout.list_item, null);
109 }
110
111 if (mFiles != null && mFiles.size() > position) {
112 OCFile file = mFiles.get(position);
113 TextView fileName = (TextView) view.findViewById(R.id.Filename);
114 String name = file.getFileName();
115
116 fileName.setText(name);
117 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
118 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype()));
119 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
120 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
121 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
122 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
123 localStateView.setImageResource(R.drawable.downloading_file_indicator);
124 localStateView.setVisibility(View.VISIBLE);
125 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
126 localStateView.setImageResource(R.drawable.uploading_file_indicator);
127 localStateView.setVisibility(View.VISIBLE);
128 } else if (file.isDown()) {
129 localStateView.setImageResource(R.drawable.local_file_indicator);
130 localStateView.setVisibility(View.VISIBLE);
131 } else {
132 localStateView.setVisibility(View.INVISIBLE);
133 }
134
135 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
136 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
137 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
138
139 if (!file.isFolder()) {
140 fileSizeV.setVisibility(View.VISIBLE);
141 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
142 lastModV.setVisibility(View.VISIBLE);
143 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
144 // this if-else is needed even thoe fav icon is visible by default
145 // because android reuses views in listview
146 if (!file.keepInSync()) {
147 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
148 } else {
149 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
150 }
151
152 ListView parentList = (ListView)parent;
153 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
154 checkBoxV.setVisibility(View.GONE);
155 } else {
156 if (parentList.isItemChecked(position)) {
157 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
158 } else {
159 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
160 }
161 checkBoxV.setVisibility(View.VISIBLE);
162 }
163
164 }
165 else {
166
167 fileSizeV.setVisibility(View.INVISIBLE);
168 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
169 lastModV.setVisibility(View.VISIBLE);
170 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
171 checkBoxV.setVisibility(View.GONE);
172 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
173 }
174
175 ImageView shareIconV = (ImageView) view.findViewById(R.id.shareIcon);
176 if (file.isShareByLink()) {
177 shareIconV.setVisibility(View.VISIBLE);
178 } else {
179 shareIconV.setVisibility(View.INVISIBLE);
180 }
181 }
182
183 return view;
184 }
185
186 @Override
187 public int getViewTypeCount() {
188 return 1;
189 }
190
191 @Override
192 public boolean hasStableIds() {
193 return true;
194 }
195
196 @Override
197 public boolean isEmpty() {
198 return (mFiles == null || mFiles.isEmpty());
199 }
200
201 /**
202 * Change the adapted directory for a new one
203 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
204 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
205 */
206 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
207 mFile = directory;
208 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
209 mStorageManager = updatedStorageManager;
210 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
211 }
212 if (mStorageManager != null) {
213 mFiles = mStorageManager.getFolderContent(mFile);
214 mCursor = mStorageManager.getContent(mFile.getParentId());
215 } else {
216 mFiles = null;
217 mCursor = null;
218 }
219 notifyDataSetChanged();
220 }
221
222 }