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