d99898511f00c6703bad4cd3c2af9c9c4fe40bfb
[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.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.BaseAdapter;
37 import android.widget.ImageView;
38 import android.widget.ListAdapter;
39 import android.widget.ListView;
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 extends BaseAdapter implements ListAdapter {
50 private Context mContext;
51 private OCFile mFile = null;
52 private Vector<OCFile> mFiles = null;
53 private DataStorageManager mStorageManager;
54 private Account mAccount;
55
56 public FileListListAdapter(OCFile file, DataStorageManager storage_man,
57 Context context) {
58 mStorageManager = storage_man;
59 mContext = context;
60 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
61 swapDirectory(file);
62 /*mFile = file;
63 mFiles = mStorageManager.getDirectoryContent(mFile);*/
64 }
65
66 @Override
67 public boolean areAllItemsEnabled() {
68 return true;
69 }
70
71 @Override
72 public boolean isEnabled(int position) {
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 == null || mFiles.size() <= position)
84 return null;
85 return mFiles.get(position);
86 }
87
88 @Override
89 public long getItemId(int position) {
90 if (mFiles == null || mFiles.size() <= position)
91 return 0;
92 return mFiles.get(position).getFileId();
93 }
94
95 @Override
96 public int getItemViewType(int position) {
97 return 0;
98 }
99
100 @Override
101 public View getView(int position, View convertView, ViewGroup parent) {
102 View view = convertView;
103 if (view == null) {
104 LayoutInflater inflator = (LayoutInflater) mContext
105 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
106 view = inflator.inflate(R.layout.list_layout, null);
107 }
108 if (mFiles != null && mFiles.size() > position) {
109 OCFile file = mFiles.get(position);
110 TextView fileName = (TextView) view.findViewById(R.id.Filename);
111 String name = file.getFileName();
112
113 fileName.setText(name);
114 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
115 if (file.getMimetype() == null || !file.getMimetype().equals("DIR")) {
116 fileIcon.setImageResource(R.drawable.file);
117 } else {
118 fileIcon.setImageResource(R.drawable.ic_menu_archive);
119 }
120 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
121 if (FileDownloader.isDownloading(mAccount, file.getRemotePath())) {
122 localStateView.setImageResource(R.drawable.downloading_file_indicator);
123 localStateView.setVisibility(View.VISIBLE);
124 } else if (FileUploader.isUploading(mAccount, file.getRemotePath())) {
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
135 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
136 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
137 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
138
139 if (!file.isDirectory()) {
140 fileSizeV.setVisibility(View.VISIBLE);
141 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
142 lastModV.setVisibility(View.VISIBLE);
143 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
144 // this if-else is needed even thoe fav icon is visible by default
145 // because android reuses views in listview
146 if (!file.keepInSync()) {
147 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
148 } else {
149 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
150 }
151
152 ListView parentList = (ListView)parent;
153 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
154 checkBoxV.setVisibility(View.GONE);
155 } else {
156 if (parentList.isItemChecked(position)) {
157 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
158 } else {
159 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
160 }
161 checkBoxV.setVisibility(View.VISIBLE);
162 }
163
164 } else {
165 fileSizeV.setVisibility(View.GONE);
166 lastModV.setVisibility(View.GONE);
167 checkBoxV.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 1;
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());
188 }
189
190 /**
191 * Change the adapted directory for a new one
192 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
193 */
194 public void swapDirectory(OCFile directory) {
195 mFile = directory;
196 if (mStorageManager != null) {
197 mFiles = mStorageManager.getDirectoryContent(mFile);
198 } else {
199 mFiles = null;
200 }
201 notifyDataSetChanged();
202 }
203
204 }