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