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