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