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