da4f81596135149ff6bf39fa892f160084b79bb5
[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.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.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 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 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 return 0;
95 }
96
97 @Override
98 public View getView(int position, View convertView, ViewGroup parent) {
99 View view = convertView;
100 if (view == null) {
101 LayoutInflater inflator = (LayoutInflater) mContext
102 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
103 view = inflator.inflate(R.layout.list_layout, null);
104 }
105 if (mFiles.size() > position) {
106 OCFile file = mFiles.get(position);
107 TextView fileName = (TextView) view.findViewById(R.id.Filename);
108 String name = file.getFileName();
109
110 fileName.setText(name);
111 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
112 if (file.getMimetype() == null || !file.getMimetype().equals("DIR")) {
113 fileIcon.setImageResource(R.drawable.file);
114 } else {
115 fileIcon.setImageResource(R.drawable.ic_menu_archive);
116 }
117 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
118 if (FileDownloader.isDownloading(mAccount, file.getRemotePath())) {
119 localStateView.setImageResource(R.drawable.downloading_file_indicator);
120 localStateView.setVisibility(View.VISIBLE);
121 } else if (FileUploader.isUploading(mAccount, file.getRemotePath())) {
122 localStateView.setImageResource(R.drawable.uploading_file_indicator);
123 localStateView.setVisibility(View.VISIBLE);
124 } else if (file.isDown()) {
125 localStateView.setImageResource(R.drawable.local_file_indicator);
126 localStateView.setVisibility(View.VISIBLE);
127 } else {
128 localStateView.setVisibility(View.INVISIBLE);
129 }
130
131
132 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
133 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
134 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
135
136 if (!file.isDirectory()) {
137 fileSizeV.setVisibility(View.VISIBLE);
138 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
139 lastModV.setVisibility(View.VISIBLE);
140 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
141 // this if-else is needed even thoe fav icon is visible by default
142 // because android reuses views in listview
143 if (!file.keepInSync()) {
144 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
145 } else {
146 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
147 }
148
149 ListView parentList = (ListView)parent;
150 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
151 checkBoxV.setVisibility(View.GONE);
152 } else {
153 if (parentList.isItemChecked(position)) {
154 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
155 } else {
156 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
157 }
158 checkBoxV.setVisibility(View.VISIBLE);
159 }
160
161 } else {
162 fileSizeV.setVisibility(View.GONE);
163 lastModV.setVisibility(View.GONE);
164 checkBoxV.setVisibility(View.GONE);
165 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
166 }
167 }
168
169 return view;
170 }
171
172 @Override
173 public int getViewTypeCount() {
174 return 4;
175 }
176
177 @Override
178 public boolean hasStableIds() {
179 return true;
180 }
181
182 @Override
183 public boolean isEmpty() {
184 return mFiles != null ? mFiles.isEmpty() : false;
185 }
186
187 @Override
188 public void registerDataSetObserver(DataSetObserver observer) {
189 }
190
191 @Override
192 public void unregisterDataSetObserver(DataSetObserver observer) {
193 }
194 }