ac7aa90f96c276584cae1625df570354517438df
[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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.ui.adapter;
21
22 import java.io.File;
23
24 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
25
26 import com.owncloud.android.AccountUtils;
27 import com.owncloud.android.R;
28 import eu.alefzero.webdav.WebdavUtils;
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;
49
50 private final int ITEM_DOWNLOAD = 0;
51
52 // private final int ITEM_SHARE = 1;
53
54 public FileListActionListAdapter(Cursor c, Context co, Account account) {
55 mContext = co;
56 mFilename = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_NAME));
57 mFileType = c.getString(c
58 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE));
59 mFilePath = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
60 mFileStoragePath = c.getString(c
61 .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
62 // mItemId = c.getString(c.getColumnIndex(ProviderTableMeta._ID));
63 mAccount = account;
64 }
65
66 public boolean areAllItemsEnabled() {
67 return true;
68 }
69
70 public boolean isEnabled(int position) {
71 return true;
72 }
73
74 public int getCount() {
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 AccountUtils.constructFullURLForAccount(mContext, mAccount));
87 ocurl += WebdavUtils.encodePath(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 return 0;
101 }
102
103 public int getItemViewType(int position) {
104 return 0;
105 }
106
107 public View getView(int position, View convertView, ViewGroup parent) {
108 View v = convertView;
109 if (v == null) {
110 LayoutInflater vi = (LayoutInflater) mContext
111 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
112 v = vi.inflate(R.layout.file_display_action_list_element, null);
113 }
114
115 TextView tv;
116 ImageView iv;
117 switch (position) {
118 case ITEM_DOWNLOAD:
119 tv = (TextView) v.findViewById(R.id.textView1);
120 if (mFileStoragePath == null) {
121 tv.setText("Download");
122 } else {
123 setActionName(tv);
124 }
125 iv = (ImageView) v.findViewById(R.id.imageView1);
126 iv.setImageResource(R.drawable.download);
127 break;
128 }
129
130 return v;
131 }
132
133 public int getViewTypeCount() {
134 return 2;
135 }
136
137 public boolean hasStableIds() {
138 return false;
139 }
140
141 public boolean isEmpty() {
142 return false;
143 }
144
145 public void registerDataSetObserver(DataSetObserver observer) { }
146
147 public void unregisterDataSetObserver(DataSetObserver observer) { }
148
149 private void setActionName(TextView tv) {
150 if (mFileType.matches("image/.*")) {
151 tv.setText("View");
152 } else if (mFileType.matches("audio/.*")
153 || mFileType.matches("video/.*")) {
154 tv.setText("Play");
155 } else {
156 tv.setText("Open");
157 }
158 }
159
160 }