6b28f0985fd9345440d1a7dbbbb0b21509ec5c47
[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.FileDownloaderBinder;
27 import com.owncloud.android.files.services.FileUploader;
28 import com.owncloud.android.ui.activity.TransferServiceGetter;
29
30 import com.owncloud.android.R;
31
32 import android.accounts.Account;
33 import android.content.Context;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.BaseAdapter;
38 import android.widget.ImageView;
39 import android.widget.ListAdapter;
40 import android.widget.ListView;
41 import android.widget.TextView;
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
58 public FileListListAdapter(OCFile file, DataStorageManager storage_man,
59 Context context, TransferServiceGetter transferServiceGetter) {
60 mStorageManager = storage_man;
61 mContext = context;
62 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
63 mTransferServiceGetter = transferServiceGetter;
64 swapDirectory(file);
65 /*mFile = file;
66 mFiles = mStorageManager.getDirectoryContent(mFile);*/
67 }
68
69 @Override
70 public boolean areAllItemsEnabled() {
71 return true;
72 }
73
74 @Override
75 public boolean isEnabled(int position) {
76 return true;
77 }
78
79 @Override
80 public int getCount() {
81 return mFiles != null ? mFiles.size() : 0;
82 }
83
84 @Override
85 public Object getItem(int position) {
86 if (mFiles == null || mFiles.size() <= position)
87 return null;
88 return mFiles.get(position);
89 }
90
91 @Override
92 public long getItemId(int position) {
93 if (mFiles == null || mFiles.size() <= position)
94 return 0;
95 return mFiles.get(position).getFileId();
96 }
97
98 @Override
99 public int getItemViewType(int position) {
100 return 0;
101 }
102
103 @Override
104 public View getView(int position, View convertView, ViewGroup parent) {
105 View view = convertView;
106 if (view == null) {
107 LayoutInflater inflator = (LayoutInflater) mContext
108 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
109 view = inflator.inflate(R.layout.list_layout, null);
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 if (file.getMimetype() == null || !file.getMimetype().equals("DIR")) {
119 fileIcon.setImageResource(R.drawable.file);
120 } else {
121 fileIcon.setImageResource(R.drawable.ic_menu_archive);
122 }
123 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
124 //if (FileDownloader.isDownloading(mAccount, file.getRemotePath())) {
125 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
126 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file.getRemotePath())) {
127 localStateView.setImageResource(R.drawable.downloading_file_indicator);
128 localStateView.setVisibility(View.VISIBLE);
129 } else if (FileUploader.isUploading(mAccount, file.getRemotePath())) {
130 localStateView.setImageResource(R.drawable.uploading_file_indicator);
131 localStateView.setVisibility(View.VISIBLE);
132 } else if (file.isDown()) {
133 localStateView.setImageResource(R.drawable.local_file_indicator);
134 localStateView.setVisibility(View.VISIBLE);
135 } else {
136 localStateView.setVisibility(View.INVISIBLE);
137 }
138
139
140 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
141 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
142 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
143
144 if (!file.isDirectory()) {
145 fileSizeV.setVisibility(View.VISIBLE);
146 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
147 lastModV.setVisibility(View.VISIBLE);
148 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
149 // this if-else is needed even thoe fav icon is visible by default
150 // because android reuses views in listview
151 if (!file.keepInSync()) {
152 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
153 } else {
154 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
155 }
156
157 ListView parentList = (ListView)parent;
158 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
159 checkBoxV.setVisibility(View.GONE);
160 } else {
161 if (parentList.isItemChecked(position)) {
162 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
163 } else {
164 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
165 }
166 checkBoxV.setVisibility(View.VISIBLE);
167 }
168
169 } else {
170 fileSizeV.setVisibility(View.GONE);
171 lastModV.setVisibility(View.GONE);
172 checkBoxV.setVisibility(View.GONE);
173 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
174 }
175 }
176
177 return view;
178 }
179
180 @Override
181 public int getViewTypeCount() {
182 return 1;
183 }
184
185 @Override
186 public boolean hasStableIds() {
187 return true;
188 }
189
190 @Override
191 public boolean isEmpty() {
192 return (mFiles == null || mFiles.isEmpty());
193 }
194
195 /**
196 * Change the adapted directory for a new one
197 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
198 */
199 public void swapDirectory(OCFile directory) {
200 mFile = directory;
201 if (mStorageManager != null) {
202 mFiles = mStorageManager.getDirectoryContent(mFile);
203 } else {
204 mFiles = null;
205 }
206 notifyDataSetChanged();
207 }
208
209 }