032981bc1a98ab04d06132cb4bde415f16743ea9
[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-2013 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.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.BaseAdapter;
26 import android.widget.ImageView;
27 import android.widget.ListAdapter;
28 import android.widget.ListView;
29 import android.widget.TextView;
30
31 import com.owncloud.android.DisplayUtils;
32 import com.owncloud.android.R;
33 import com.owncloud.android.authentication.AccountUtils;
34 import com.owncloud.android.datamodel.DataStorageManager;
35 import com.owncloud.android.datamodel.OCFile;
36 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
37 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
38 import com.owncloud.android.ui.activity.TransferServiceGetter;
39
40 import java.util.Vector;
41
42
43 /**
44 * This Adapter populates a ListView with all files and folders in an ownCloud
45 * instance.
46 *
47 * @author Bartek Przybylski
48 *
49 */
50 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
51 private Context mContext;
52 private OCFile mFile = null;
53 private Vector<OCFile> mFiles = null;
54 private DataStorageManager mStorageManager;
55 private Account mAccount;
56 private TransferServiceGetter mTransferServiceGetter;
57 //total size of a directory (recursive)
58 private Long totalSizeOfDirectoriesRecursive = null;
59 private Long lastModifiedOfAllSubdirectories = null;
60
61 public FileListListAdapter(Context context, TransferServiceGetter transferServiceGetter) {
62 mContext = context;
63 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
64 mTransferServiceGetter = transferServiceGetter;
65 }
66
67 @Override
68 public boolean areAllItemsEnabled() {
69 return true;
70 }
71
72 @Override
73 public boolean isEnabled(int position) {
74 return true;
75 }
76
77 @Override
78 public int getCount() {
79 return mFiles != null ? mFiles.size() : 0;
80 }
81
82 @Override
83 public Object getItem(int position) {
84 if (mFiles == null || mFiles.size() <= position)
85 return null;
86 return mFiles.get(position);
87 }
88
89 @Override
90 public long getItemId(int position) {
91 if (mFiles == null || mFiles.size() <= position)
92 return 0;
93 return mFiles.get(position).getFileId();
94 }
95
96 @Override
97 public int getItemViewType(int position) {
98 return 0;
99 }
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.isDirectory()) {
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.VISIBLE);
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
176 return view;
177 }
178
179 @Override
180 public int getViewTypeCount() {
181 return 1;
182 }
183
184 @Override
185 public boolean hasStableIds() {
186 return true;
187 }
188
189 @Override
190 public boolean isEmpty() {
191 return (mFiles == null || mFiles.isEmpty());
192 }
193
194 /**
195 * Change the adapted directory for a new one
196 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
197 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
198 */
199 public void swapDirectory(OCFile directory, DataStorageManager updatedStorageManager) {
200 mFile = directory;
201 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
202 mStorageManager = updatedStorageManager;
203 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
204 }
205 if (mStorageManager != null) {
206 mFiles = mStorageManager.getDirectoryContent(mFile);
207 } else {
208 mFiles = null;
209 }
210 notifyDataSetChanged();
211 }
212
213 }