65ed6f05ce80c292a77b1c39c642eec1af4027a6
[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.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, mItemId;
47
48 private final int ITEM_DOWNLOAD = 0;
49 private final int ITEM_SHARE = 1;
50
51 public FileListActionListAdapter(Cursor c, Context co, Account account) {
52 mContext = co;
53 mFilename = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_NAME));
54 mFileType = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE));
55 mFilePath = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
56 mFileStoragePath = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
57 mItemId = c.getString(c.getColumnIndex(ProviderTableMeta._ID));
58 mAccount = account;
59 }
60
61 public boolean areAllItemsEnabled() {
62 // TODO Auto-generated method stub
63 return true;
64 }
65
66 public boolean isEnabled(int position) {
67 // TODO Auto-generated method stub
68 return true;
69 }
70
71 public int getCount() {
72 // TODO Auto-generated method stub
73 return 1;
74 }
75
76 public Object getItem(int position) {
77 if (position == 0) {
78 AccountManager accm = (AccountManager) mContext.getSystemService(Context.ACCOUNT_SERVICE);
79 String ocurl = accm.getUserData(mAccount, AccountAuthenticator.KEY_OC_URL);
80 ocurl += mFilePath + mFilename;
81 Intent intent = new Intent(Intent.ACTION_VIEW);
82 intent.setDataAndType(Uri.fromFile(new File(mFileStoragePath)), mFileType);
83 return intent;
84 }
85 return null;
86 }
87
88 public long getItemId(int position) {
89 // TODO Auto-generated method stub
90 return 0;
91 }
92
93 public int getItemViewType(int position) {
94 // TODO Auto-generated method stub
95 return 0;
96 }
97
98 public View getView(int position, View convertView, ViewGroup parent) {
99 View v = convertView;
100 if (v == null) {
101 LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
102 v = vi.inflate(R.layout.file_display_action_list_element, null);
103 }
104
105 TextView tv;
106 ImageView iv;
107 switch (position) {
108 case ITEM_DOWNLOAD :
109 tv = (TextView) v.findViewById(R.id.textView1);
110 if (mFileStoragePath == null) {
111 tv.setText("Download");
112 } else {
113 setActionName(tv);
114 }
115 iv = (ImageView) v.findViewById(R.id.imageView1);
116 iv.setImageResource(R.drawable.download);
117 break;
118 }
119
120 return v;
121 }
122
123 public int getViewTypeCount() {
124 // TODO Auto-generated method stub
125 return 2;
126 }
127
128 public boolean hasStableIds() {
129 // TODO Auto-generated method stub
130 return false;
131 }
132
133 public boolean isEmpty() {
134 // TODO Auto-generated method stub
135 return false;
136 }
137
138 public void registerDataSetObserver(DataSetObserver observer) {
139 // TODO Auto-generated method stub
140
141 }
142
143 public void unregisterDataSetObserver(DataSetObserver observer) {
144 // TODO Auto-generated method stub
145
146 }
147
148 private void setActionName(TextView tv) {
149 if (mFileType.matches("image/.*")) {
150 tv.setText("View");
151 } else if (mFileType.matches("audio/.*") || mFileType.matches("video/.*")) {
152 tv.setText("Play");
153 } else {
154 tv.setText("Open");
155 }
156 }
157
158 }