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