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