9c7960384c0ebfcc45bfb76d738dffb03edc11d5
[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.DisplayUtils;
23 import eu.alefzero.owncloud.R;
24 import eu.alefzero.owncloud.datamodel.DataStorageManager;
25 import eu.alefzero.owncloud.datamodel.OCFile;
26
27 import android.content.Context;
28 import android.database.DataSetObserver;
29 import android.util.Log;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.ImageView;
34 import android.widget.ListAdapter;
35 import android.widget.TextView;
36
37 /**
38 * This Adapter populates a ListView with all files and folders in an ownCloud
39 * instance.
40 *
41 * @author Bartek Przybylski
42 *
43 */
44 public class FileListListAdapter implements ListAdapter {
45 private Context mContext;
46 private OCFile mFile;
47 private Vector<OCFile> mFiles;
48 private DataStorageManager mStorageManager;
49
50 public FileListListAdapter(OCFile file, DataStorageManager storage_man,
51 Context context) {
52 mFile = file;
53 mStorageManager = storage_man;
54 mFiles = mStorageManager.getDirectoryContent(mFile);
55 mContext = context;
56 }
57
58 @Override
59 public boolean areAllItemsEnabled() {
60 return true;
61 }
62
63 @Override
64 public boolean isEnabled(int position) {
65 // TODO Auto-generated method stub
66 return true;
67 }
68
69 @Override
70 public int getCount() {
71 return mFiles != null ? mFiles.size() : 0;
72 }
73
74 @Override
75 public Object getItem(int position) {
76 if (mFiles.size() <= position)
77 return null;
78 return mFiles.get(position);
79 }
80
81 @Override
82 public long getItemId(int position) {
83 return mFiles != null ? mFiles.get(position).getFileId() : 0;
84 }
85
86 @Override
87 public int getItemViewType(int position) {
88 // TODO Auto-generated method stub
89 return 0;
90 }
91
92 @Override
93 public View getView(int position, View convertView, ViewGroup parent) {
94 View view = convertView;
95 if (view == null) {
96 LayoutInflater inflator = (LayoutInflater) mContext
97 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
98 view = inflator.inflate(R.layout.list_layout, null);
99 }
100 if (mFiles.size() > position) {
101 OCFile file = mFiles.get(position);
102 TextView fileName = (TextView) view.findViewById(R.id.Filename);
103 String name = file.getFileName();
104
105 fileName.setText(name);
106 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
107 if (file.getMimetype() == null || !file.getMimetype().equals("DIR")) {
108 fileIcon.setImageResource(R.drawable.file);
109 } else {
110 fileIcon.setImageResource(R.drawable.ic_menu_archive);
111 }
112 ImageView downloaded = (ImageView) view.findViewById(R.id.imageView2);
113 ImageView downloading = (ImageView) view.findViewById(R.id.imageView4);
114 if (file.isDownloading()) {
115 downloaded.setVisibility(View.INVISIBLE);
116 downloading.setVisibility(View.VISIBLE);
117 } else if (file.isDown()) {
118 downloaded.setVisibility(View.VISIBLE);
119 downloading.setVisibility(View.INVISIBLE);
120 } else {
121 downloaded.setVisibility(View.INVISIBLE);
122 downloading.setVisibility(View.INVISIBLE);
123 }
124
125 if (!file.isDirectory()) {
126 view.findViewById(R.id.file_size).setVisibility(View.VISIBLE);
127 view.findViewById(R.id.last_mod).setVisibility(View.VISIBLE);
128 ((TextView)view.findViewById(R.id.file_size)).setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
129 ((TextView)view.findViewById(R.id.last_mod)).setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
130 // this if-else is needed even thoe fav icon is visible by default
131 // because android reuses views in listview
132 if (!file.keepInSync()) {
133 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
134 } else {
135 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
136 }
137 } else {
138 view.findViewById(R.id.file_size).setVisibility(View.GONE);
139 view.findViewById(R.id.last_mod).setVisibility(View.GONE);
140 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
141 }
142 }
143
144 return view;
145 }
146
147 @Override
148 public int getViewTypeCount() {
149 return 4;
150 }
151
152 @Override
153 public boolean hasStableIds() {
154 return true;
155 }
156
157 @Override
158 public boolean isEmpty() {
159 return mFiles != null ? mFiles.isEmpty() : false;
160 }
161
162 @Override
163 public void registerDataSetObserver(DataSetObserver observer) {
164 // TODO Auto-generated method stub
165
166 }
167
168 @Override
169 public void unregisterDataSetObserver(DataSetObserver observer) {
170 // TODO Auto-generated method stub
171
172 }
173 }