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