734623e55cd49a22e9eb122b7aad370ee8401cb5
[pub/Android/ownCloud.git] / src / de / mobilcom / debitel / cloud / 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 de.mobilcom.debitel.cloud.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 de.mobilcom.debitel.cloud.android.DisplayUtils;
32 import de.mobilcom.debitel.cloud.android.R;
33 import de.mobilcom.debitel.cloud.android.authentication.AccountUtils;
34 import de.mobilcom.debitel.cloud.android.datamodel.DataStorageManager;
35 import de.mobilcom.debitel.cloud.android.datamodel.OCFile;
36 import de.mobilcom.debitel.cloud.android.files.services.FileDownloader.FileDownloaderBinder;
37 import de.mobilcom.debitel.cloud.android.files.services.FileUploader.FileUploaderBinder;
38 import de.mobilcom.debitel.cloud.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 @Override
102 public View getView(int position, View convertView, ViewGroup parent) {
103 View view = convertView;
104 if (view == null) {
105 LayoutInflater inflator = (LayoutInflater) mContext
106 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
107 view = inflator.inflate(R.layout.list_item, null);
108 }
109
110 if (mFiles != null && mFiles.size() > position) {
111 OCFile file = mFiles.get(position);
112 TextView fileName = (TextView) view.findViewById(R.id.Filename);
113 String name = file.getFileName();
114
115 fileName.setText(name);
116 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
117 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype()));
118 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
119 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
120 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
121 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
122 localStateView.setImageResource(R.drawable.downloading_file_indicator);
123 localStateView.setVisibility(View.VISIBLE);
124 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
125 localStateView.setImageResource(R.drawable.uploading_file_indicator);
126 localStateView.setVisibility(View.VISIBLE);
127 } else if (file.isDown()) {
128 localStateView.setImageResource(R.drawable.local_file_indicator);
129 localStateView.setVisibility(View.VISIBLE);
130 } else {
131 localStateView.setVisibility(View.INVISIBLE);
132 }
133
134 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
135 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
136 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
137
138 if (!file.isDirectory()) {
139 fileSizeV.setVisibility(View.VISIBLE);
140 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
141 lastModV.setVisibility(View.VISIBLE);
142 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
143 // this if-else is needed even thoe fav icon is visible by default
144 // because android reuses views in listview
145 if (!file.keepInSync()) {
146 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
147 } else {
148 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
149 }
150
151 ListView parentList = (ListView)parent;
152 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
153 checkBoxV.setVisibility(View.GONE);
154 } else {
155 if (parentList.isItemChecked(position)) {
156 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
157 } else {
158 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
159 }
160 checkBoxV.setVisibility(View.VISIBLE);
161 }
162
163 }
164 else {
165
166 fileSizeV.setVisibility(View.VISIBLE);
167 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
168 lastModV.setVisibility(View.VISIBLE);
169 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
170 checkBoxV.setVisibility(View.GONE);
171 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
172 }
173 }
174
175 return view;
176 }
177
178 @Override
179 public int getViewTypeCount() {
180 return 1;
181 }
182
183 @Override
184 public boolean hasStableIds() {
185 return true;
186 }
187
188 @Override
189 public boolean isEmpty() {
190 return (mFiles == null || mFiles.isEmpty());
191 }
192
193 /**
194 * Change the adapted directory for a new one
195 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
196 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
197 */
198 public void swapDirectory(OCFile directory, DataStorageManager updatedStorageManager) {
199 mFile = directory;
200 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
201 mStorageManager = updatedStorageManager;
202 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
203 }
204 if (mStorageManager != null) {
205 mFiles = mStorageManager.getDirectoryContent(mFile);
206 } else {
207 mFiles = null;
208 }
209 notifyDataSetChanged();
210 }
211
212 }