1556e0d8b761b39849ec42f18115c05a618e7c25
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / adapter / FileListActionListAdapter.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
19 package eu.alefzero.owncloud.ui.adapter;
20
21 import java.io.File;
22
23 import eu.alefzero.owncloud.R;
24 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
25 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
26 import eu.alefzero.webdav.WebdavUtils;
27 import android.accounts.Account;
28 import android.accounts.AccountManager;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.database.Cursor;
32 import android.database.DataSetObserver;
33 import android.net.Uri;
34 import android.text.TextUtils;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.ImageView;
39 import android.widget.ListAdapter;
40 import android.widget.TextView;
41
42 public class FileListActionListAdapter implements ListAdapter {
43
44 private Context mContext;
45 private Account mAccount;
46 private String mFilename, mFileType, mFilePath, mFileStoragePath;
47
48 private final int ITEM_DOWNLOAD = 0;
49
50 // private final int ITEM_SHARE = 1;
51
52 public FileListActionListAdapter(Cursor c, Context co, Account account) {
53 mContext = co;
54 mFilename = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_NAME));
55 mFileType = c.getString(c
56 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE));
57 mFilePath = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
58 mFileStoragePath = c.getString(c
59 .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
60 // mItemId = c.getString(c.getColumnIndex(ProviderTableMeta._ID));
61 mAccount = account;
62 }
63
64 public boolean areAllItemsEnabled() {
65 // TODO Auto-generated method stub
66 return true;
67 }
68
69 public boolean isEnabled(int position) {
70 // TODO Auto-generated method stub
71 return true;
72 }
73
74 public int getCount() {
75 // TODO Auto-generated method stub
76 return 1;
77 }
78
79 public Object getItem(int position) {
80 if (position == 0) {
81 Intent intent = new Intent(Intent.ACTION_VIEW);
82 if (TextUtils.isEmpty(mFileStoragePath)) {
83 intent.putExtra("toDownload", true);
84 AccountManager accm = (AccountManager) mContext
85 .getSystemService(Context.ACCOUNT_SERVICE);
86 String ocurl = accm.getUserData(mAccount,
87 AccountAuthenticator.KEY_OC_URL);
88 ocurl += WebdavUtils.encodePath(mFilePath + mFilename);
89 intent.setData(Uri.parse(ocurl));
90 } else {
91 intent.putExtra("toDownload", false);
92 intent.setDataAndType(Uri.fromFile(new File(mFileStoragePath)),
93 mFileType);
94 }
95 return intent;
96 }
97 return null;
98 }
99
100 public long getItemId(int position) {
101 // TODO Auto-generated method stub
102 return 0;
103 }
104
105 public int getItemViewType(int position) {
106 // TODO Auto-generated method stub
107 return 0;
108 }
109
110 public View getView(int position, View convertView, ViewGroup parent) {
111 View v = convertView;
112 if (v == null) {
113 LayoutInflater vi = (LayoutInflater) mContext
114 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
115 v = vi.inflate(R.layout.file_display_action_list_element, null);
116 }
117
118 TextView tv;
119 ImageView iv;
120 switch (position) {
121 case ITEM_DOWNLOAD:
122 tv = (TextView) v.findViewById(R.id.textView1);
123 if (mFileStoragePath == null) {
124 tv.setText("Download");
125 } else {
126 setActionName(tv);
127 }
128 iv = (ImageView) v.findViewById(R.id.imageView1);
129 iv.setImageResource(R.drawable.download);
130 break;
131 }
132
133 return v;
134 }
135
136 public int getViewTypeCount() {
137 // TODO Auto-generated method stub
138 return 2;
139 }
140
141 public boolean hasStableIds() {
142 // TODO Auto-generated method stub
143 return false;
144 }
145
146 public boolean isEmpty() {
147 // TODO Auto-generated method stub
148 return false;
149 }
150
151 public void registerDataSetObserver(DataSetObserver observer) {
152 // TODO Auto-generated method stub
153
154 }
155
156 public void unregisterDataSetObserver(DataSetObserver observer) {
157 // TODO Auto-generated method stub
158
159 }
160
161 private void setActionName(TextView tv) {
162 if (mFileType.matches("image/.*")) {
163 tv.setText("View");
164 } else if (mFileType.matches("audio/.*")
165 || mFileType.matches("video/.*")) {
166 tv.setText("Play");
167 } else {
168 tv.setText("Open");
169 }
170 }
171
172 }