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