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