9ab68db46b99fa08dca4b8bc425fd3ed63fd26a9
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / adapter / FileListListAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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 eu.alefzero.owncloud.ui.adapter;
19
20 import java.util.Vector;
21
22 import eu.alefzero.owncloud.AccountUtils;
23 import eu.alefzero.owncloud.DisplayUtils;
24 import eu.alefzero.owncloud.R;
25 import eu.alefzero.owncloud.datamodel.DataStorageManager;
26 import eu.alefzero.owncloud.datamodel.OCFile;
27 import eu.alefzero.owncloud.files.services.FileDownloader;
28 import eu.alefzero.owncloud.files.services.FileUploader;
29
30 import android.accounts.Account;
31 import android.content.Context;
32 import android.database.DataSetObserver;
33 import android.util.Log;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.ImageView;
38 import android.widget.ListAdapter;
39 import android.widget.TextView;
40
41 /**
42 * This Adapter populates a ListView with all files and folders in an ownCloud
43 * instance.
44 *
45 * @author Bartek Przybylski
46 *
47 */
48 public class FileListListAdapter implements ListAdapter {
49 private Context mContext;
50 private OCFile mFile;
51 private Vector<OCFile> mFiles;
52 private DataStorageManager mStorageManager;
53 private Account mAccount;
54
55 public FileListListAdapter(OCFile file, DataStorageManager storage_man,
56 Context context) {
57 mFile = file;
58 mStorageManager = storage_man;
59 mFiles = mStorageManager.getDirectoryContent(mFile);
60 mContext = context;
61 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
62 }
63
64 @Override
65 public boolean areAllItemsEnabled() {
66 return true;
67 }
68
69 @Override
70 public boolean isEnabled(int position) {
71 // TODO Auto-generated method stub
72 return true;
73 }
74
75 @Override
76 public int getCount() {
77 return mFiles != null ? mFiles.size() : 0;
78 }
79
80 @Override
81 public Object getItem(int position) {
82 if (mFiles.size() <= position)
83 return null;
84 return mFiles.get(position);
85 }
86
87 @Override
88 public long getItemId(int position) {
89 return mFiles != null ? mFiles.get(position).getFileId() : 0;
90 }
91
92 @Override
93 public int getItemViewType(int position) {
94 // TODO Auto-generated method stub
95 return 0;
96 }
97
98 @Override
99 public View getView(int position, View convertView, ViewGroup parent) {
100 View view = convertView;
101 if (view == null) {
102 LayoutInflater inflator = (LayoutInflater) mContext
103 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
104 view = inflator.inflate(R.layout.list_layout, null);
105 }
106 if (mFiles.size() > position) {
107 OCFile file = mFiles.get(position);
108 TextView fileName = (TextView) view.findViewById(R.id.Filename);
109 String name = file.getFileName();
110
111 fileName.setText(name);
112 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
113 if (file.getMimetype() == null || !file.getMimetype().equals("DIR")) {
114 fileIcon.setImageResource(R.drawable.file);
115 } else {
116 fileIcon.setImageResource(R.drawable.ic_menu_archive);
117 }
118 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
119 if (FileDownloader.isDownloading(mAccount, file.getRemotePath())) {
120 localStateView.setImageResource(R.drawable.downloading_file_indicator);
121 localStateView.setVisibility(View.VISIBLE);
122 } else if (FileUploader.isUploading(mAccount, file.getRemotePath())) {
123 localStateView.setImageResource(R.drawable.uploading_file_indicator);
124 localStateView.setVisibility(View.VISIBLE);
125 } else if (file.isDown()) {
126 localStateView.setImageResource(R.drawable.local_file_indicator);
127 localStateView.setVisibility(View.VISIBLE);
128 } else {
129 localStateView.setVisibility(View.INVISIBLE);
130 }
131 /*
132 ImageView down = (ImageView) view.findViewById(R.id.imageView2);
133 ImageView downloading = (ImageView) view.findViewById(R.id.imageView4);
134 ImageView uploading = (ImageView) view.findViewById(R.id.imageView5);
135 if (FileDownloader.isDownloading(mAccount, file.getRemotePath())) {
136 down.setVisibility(View.INVISIBLE);
137 downloading.setVisibility(View.VISIBLE);
138 uploading.setVisibility(View.INVISIBLE);
139 } else if (FileUploader.isUploading(mAccount, file.getRemotePath())) {
140 down.setVisibility(View.INVISIBLE);
141 downloading.setVisibility(View.INVISIBLE);
142 uploading.setVisibility(View.VISIBLE);
143 } else if (file.isDown()) {
144 down.setVisibility(View.VISIBLE);
145 downloading.setVisibility(View.INVISIBLE);
146 uploading.setVisibility(View.INVISIBLE);
147 } else {
148 down.setVisibility(View.INVISIBLE);
149 downloading.setVisibility(View.INVISIBLE);
150 uploading.setVisibility(View.INVISIBLE);
151 }*/
152
153 if (!file.isDirectory()) {
154 view.findViewById(R.id.file_size).setVisibility(View.VISIBLE);
155 view.findViewById(R.id.last_mod).setVisibility(View.VISIBLE);
156 ((TextView)view.findViewById(R.id.file_size)).setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
157 ((TextView)view.findViewById(R.id.last_mod)).setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
158 // this if-else is needed even thoe fav icon is visible by default
159 // because android reuses views in listview
160 if (!file.keepInSync()) {
161 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
162 } else {
163 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
164 }
165 } else {
166 view.findViewById(R.id.file_size).setVisibility(View.GONE);
167 view.findViewById(R.id.last_mod).setVisibility(View.GONE);
168 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
169 }
170 }
171
172 return view;
173 }
174
175 @Override
176 public int getViewTypeCount() {
177 return 4;
178 }
179
180 @Override
181 public boolean hasStableIds() {
182 return true;
183 }
184
185 @Override
186 public boolean isEmpty() {
187 return mFiles != null ? mFiles.isEmpty() : false;
188 }
189
190 @Override
191 public void registerDataSetObserver(DataSetObserver observer) {
192 // TODO Auto-generated method stub
193
194 }
195
196 @Override
197 public void unregisterDataSetObserver(DataSetObserver observer) {
198 // TODO Auto-generated method stub
199
200 }
201 }