39c470c486ae17163d523b5f1b56f4d86567ca89
[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.CheckedTextView;
39 import android.widget.ImageView;
40 import android.widget.ListAdapter;
41 import android.widget.ListView;
42 import android.widget.TextView;
43
44 /**
45 * This Adapter populates a ListView with all files and folders in an ownCloud
46 * instance.
47 *
48 * @author Bartek Przybylski
49 *
50 */
51 public class FileListListAdapter implements ListAdapter {
52 private Context mContext;
53 private OCFile mFile;
54 private Vector<OCFile> mFiles;
55 private DataStorageManager mStorageManager;
56 private Account mAccount;
57
58 public FileListListAdapter(OCFile file, DataStorageManager storage_man,
59 Context context) {
60 mFile = file;
61 mStorageManager = storage_man;
62 mFiles = mStorageManager.getDirectoryContent(mFile);
63 mContext = context;
64 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
65 }
66
67 @Override
68 public boolean areAllItemsEnabled() {
69 return true;
70 }
71
72 @Override
73 public boolean isEnabled(int position) {
74 // TODO Auto-generated method stub
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.size() <= position)
86 return null;
87 return mFiles.get(position);
88 }
89
90 @Override
91 public long getItemId(int position) {
92 return mFiles != null ? mFiles.get(position).getFileId() : 0;
93 }
94
95 @Override
96 public int getItemViewType(int position) {
97 // TODO Auto-generated method stub
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_layout, null);
108 }
109 if (mFiles.size() > position) {
110 OCFile file = mFiles.get(position);
111 TextView fileName = (TextView) view.findViewById(R.id.Filename);
112 String name = file.getFileName();
113
114 fileName.setText(name);
115 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
116 if (file.getMimetype() == null || !file.getMimetype().equals("DIR")) {
117 fileIcon.setImageResource(R.drawable.file);
118 } else {
119 fileIcon.setImageResource(R.drawable.ic_menu_archive);
120 }
121 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
122 if (FileDownloader.isDownloading(mAccount, file.getRemotePath())) {
123 localStateView.setImageResource(R.drawable.downloading_file_indicator);
124 localStateView.setVisibility(View.VISIBLE);
125 } else if (FileUploader.isUploading(mAccount, file.getRemotePath())) {
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
136 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
137 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
138 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
139
140 if (!file.isDirectory()) {
141 fileSizeV.setVisibility(View.VISIBLE);
142 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
143 lastModV.setVisibility(View.VISIBLE);
144 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
145 // this if-else is needed even thoe fav icon is visible by default
146 // because android reuses views in listview
147 if (!file.keepInSync()) {
148 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
149 } else {
150 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
151 }
152
153 ListView parentList = (ListView)parent;
154 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
155 checkBoxV.setVisibility(View.GONE);
156 } else {
157 if (parentList.isItemChecked(position)) {
158 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
159 } else {
160 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
161 }
162 checkBoxV.setVisibility(View.VISIBLE);
163 }
164
165 } else {
166 fileSizeV.setVisibility(View.GONE);
167 lastModV.setVisibility(View.GONE);
168 checkBoxV.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 }