bcdce7937fdc627351f8085c3012477694053611
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / 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 eu.alefzero.owncloud.ui.adapter;
19
20 import java.util.Vector;
21
22 import eu.alefzero.owncloud.AccountUtils;
23 import eu.alefzero.owncloud.DisplayUtils;
24 import eu.alefzero.owncloud.R;
25 import eu.alefzero.owncloud.datamodel.DataStorageManager;
26 import eu.alefzero.owncloud.datamodel.OCFile;
27 import eu.alefzero.owncloud.files.services.FileDownloader;
28
29 import android.accounts.Account;
30 import android.content.Context;
31 import android.database.DataSetObserver;
32 import android.util.Log;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.ImageView;
37 import android.widget.ListAdapter;
38 import android.widget.TextView;
39
40 /**
41 * This Adapter populates a ListView with all files and folders in an ownCloud
42 * instance.
43 *
44 * @author Bartek Przybylski
45 *
46 */
47 public class FileListListAdapter implements ListAdapter {
48 private Context mContext;
49 private OCFile mFile;
50 private Vector<OCFile> mFiles;
51 private DataStorageManager mStorageManager;
52 private Account mAccount;
53
54 public FileListListAdapter(OCFile file, DataStorageManager storage_man,
55 Context context) {
56 mFile = file;
57 mStorageManager = storage_man;
58 mFiles = mStorageManager.getDirectoryContent(mFile);
59 mContext = context;
60 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
61 }
62
63 @Override
64 public boolean areAllItemsEnabled() {
65 return true;
66 }
67
68 @Override
69 public boolean isEnabled(int position) {
70 // TODO Auto-generated method stub
71 return true;
72 }
73
74 @Override
75 public int getCount() {
76 return mFiles != null ? mFiles.size() : 0;
77 }
78
79 @Override
80 public Object getItem(int position) {
81 if (mFiles.size() <= position)
82 return null;
83 return mFiles.get(position);
84 }
85
86 @Override
87 public long getItemId(int position) {
88 return mFiles != null ? mFiles.get(position).getFileId() : 0;
89 }
90
91 @Override
92 public int getItemViewType(int position) {
93 // TODO Auto-generated method stub
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 downloaded = (ImageView) view.findViewById(R.id.imageView2);
118 ImageView downloading = (ImageView) view.findViewById(R.id.imageView4);
119 if (FileDownloader.isDownloading(mAccount, file.getRemotePath())) {
120 downloaded.setVisibility(View.INVISIBLE);
121 downloading.setVisibility(View.VISIBLE);
122 } else if (file.isDown()) {
123 downloaded.setVisibility(View.VISIBLE);
124 downloading.setVisibility(View.INVISIBLE);
125 } else {
126 downloaded.setVisibility(View.INVISIBLE);
127 downloading.setVisibility(View.INVISIBLE);
128 }
129
130 if (!file.isDirectory()) {
131 view.findViewById(R.id.file_size).setVisibility(View.VISIBLE);
132 view.findViewById(R.id.last_mod).setVisibility(View.VISIBLE);
133 ((TextView)view.findViewById(R.id.file_size)).setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
134 ((TextView)view.findViewById(R.id.last_mod)).setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
135 // this if-else is needed even thoe fav icon is visible by default
136 // because android reuses views in listview
137 if (!file.keepInSync()) {
138 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
139 } else {
140 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
141 }
142 } else {
143 view.findViewById(R.id.file_size).setVisibility(View.GONE);
144 view.findViewById(R.id.last_mod).setVisibility(View.GONE);
145 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
146 }
147 }
148
149 return view;
150 }
151
152 @Override
153 public int getViewTypeCount() {
154 return 4;
155 }
156
157 @Override
158 public boolean hasStableIds() {
159 return true;
160 }
161
162 @Override
163 public boolean isEmpty() {
164 return mFiles != null ? mFiles.isEmpty() : false;
165 }
166
167 @Override
168 public void registerDataSetObserver(DataSetObserver observer) {
169 // TODO Auto-generated method stub
170
171 }
172
173 @Override
174 public void unregisterDataSetObserver(DataSetObserver observer) {
175 // TODO Auto-generated method stub
176
177 }
178 }