94094ee29b474024893d5648d72cc9edc9b7098d
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / FileListActionListAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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 com.owncloud.android.ui.adapter;
20
21 import java.io.File;
22
23 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
24
25 import com.owncloud.android.AccountUtils;
26 import com.owncloud.android.R;
27 import eu.alefzero.webdav.WebdavUtils;
28 import android.accounts.Account;
29 import android.accounts.AccountManager;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.database.Cursor;
33 import android.database.DataSetObserver;
34 import android.net.Uri;
35 import android.text.TextUtils;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.widget.ImageView;
40 import android.widget.ListAdapter;
41 import android.widget.TextView;
42
43 public class FileListActionListAdapter implements ListAdapter {
44
45 private Context mContext;
46 private Account mAccount;
47 private String mFilename, mFileType, mFilePath, mFileStoragePath;
48
49 private final int ITEM_DOWNLOAD = 0;
50
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
57 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE));
58 mFilePath = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
59 mFileStoragePath = c.getString(c
60 .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
61 // mItemId = c.getString(c.getColumnIndex(ProviderTableMeta._ID));
62 mAccount = account;
63 }
64
65 public boolean areAllItemsEnabled() {
66 return true;
67 }
68
69 public boolean isEnabled(int position) {
70 return true;
71 }
72
73 public int getCount() {
74 return 1;
75 }
76
77 public Object getItem(int position) {
78 if (position == 0) {
79 Intent intent = new Intent(Intent.ACTION_VIEW);
80 if (TextUtils.isEmpty(mFileStoragePath)) {
81 intent.putExtra("toDownload", true);
82 AccountManager accm = (AccountManager) mContext
83 .getSystemService(Context.ACCOUNT_SERVICE);
84 String ocurl = accm.getUserData(mAccount,
85 AccountUtils.constructFullURLForAccount(mContext, mAccount));
86 ocurl += WebdavUtils.encodePath(mFilePath + mFilename);
87 intent.setData(Uri.parse(ocurl));
88 } else {
89 intent.putExtra("toDownload", false);
90 intent.setDataAndType(Uri.fromFile(new File(mFileStoragePath)),
91 mFileType);
92 }
93 return intent;
94 }
95 return null;
96 }
97
98 public long getItemId(int position) {
99 return 0;
100 }
101
102 public int getItemViewType(int position) {
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
110 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
111 v = vi.inflate(R.layout.file_display_action_list_element, null);
112 }
113
114 TextView tv;
115 ImageView iv;
116 switch (position) {
117 case ITEM_DOWNLOAD:
118 tv = (TextView) v.findViewById(R.id.textView1);
119 if (mFileStoragePath == null) {
120 tv.setText("Download");
121 } else {
122 setActionName(tv);
123 }
124 iv = (ImageView) v.findViewById(R.id.imageView1);
125 iv.setImageResource(R.drawable.download);
126 break;
127 }
128
129 return v;
130 }
131
132 public int getViewTypeCount() {
133 return 2;
134 }
135
136 public boolean hasStableIds() {
137 return false;
138 }
139
140 public boolean isEmpty() {
141 return false;
142 }
143
144 public void registerDataSetObserver(DataSetObserver observer) { }
145
146 public void unregisterDataSetObserver(DataSetObserver observer) { }
147
148 private void setActionName(TextView tv) {
149 if (mFileType.matches("image/.*")) {
150 tv.setText("View");
151 } else if (mFileType.matches("audio/.*")
152 || mFileType.matches("video/.*")) {
153 tv.setText("Play");
154 } else {
155 tv.setText("Open");
156 }
157 }
158
159 }