500a244c2b6542ca4cb4bdeea56d76df6cf627d5
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / FileDisplayActivity.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.activity;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.util.LinkedList;
26 import java.util.Stack;
27
28 import android.accounts.Account;
29 import android.accounts.AccountManager;
30 import android.app.AlertDialog;
31 import android.app.Dialog;
32 import android.app.ListActivity;
33 import android.content.DialogInterface;
34 import android.content.Intent;
35 import android.content.DialogInterface.OnCancelListener;
36 import android.content.res.Configuration;
37 import android.database.Cursor;
38 import android.graphics.Bitmap;
39 import android.graphics.BitmapFactory;
40 import android.graphics.Matrix;
41 import android.net.Uri;
42 import android.os.Bundle;
43 import android.os.Environment;
44 import android.support.v4.app.FragmentActivity;
45 import android.support.v4.app.FragmentTransaction;
46 import android.support.v4.view.Menu;
47 import android.support.v4.view.MenuItem;
48 import android.text.TextUtils;
49 import android.util.Log;
50 import android.view.MenuInflater;
51 import android.view.View;
52 import android.view.Window;
53 import android.widget.ImageView;
54 import android.widget.ListView;
55 import android.widget.TextView;
56 import eu.alefzero.owncloud.R;
57 import eu.alefzero.owncloud.R.id;
58 import eu.alefzero.owncloud.R.layout;
59 import eu.alefzero.owncloud.R.menu;
60 import eu.alefzero.owncloud.R.string;
61 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
62 import eu.alefzero.owncloud.db.DbHandler;
63 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
64 import eu.alefzero.owncloud.ui.fragment.FileDetail;
65 import eu.alefzero.owncloud.ui.fragment.FileList;
66 import eu.alefzero.owncloud.ui.fragment.ActionBar;
67
68 /**
69 * Displays, what files the user has available in his ownCloud.
70 * @author Bartek Przybylski
71 *
72 */
73 public class FileDisplayActivity extends FragmentActivity {
74 private DbHandler mDBHandler;
75 private Stack<String> mParents;
76 private LinkedList<String> mPath;
77 private Account mAccount;
78 private Cursor mCursor;
79 private boolean mIsDisplayingFile;
80
81 private static final int DIALOG_CHOOSE_ACCOUNT = 0;
82
83 @Override
84 public void onCreate(Bundle savedInstanceState) {
85 super.onCreate(savedInstanceState);
86 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
87 setContentView(R.layout.files);
88
89 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
90 //ft.add(R.id.actionBar, new ActionBar());
91 ft.add(R.id.fileList, new FileList());
92 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
93 ft.add(R.id.fileDetail, new FileDetail());
94 }
95 ft.commit();
96
97 /*getSupportFragmentManager().beginTransaction().add(arg0, arg1);
98 FileList fl = new FileList();
99 ft.add(R.id.fileList, fl);
100 ft.commit();
101 /*
102
103
104 if (savedInstanceState != null) {
105 mParents = (Stack<String>)savedInstanceState.getSerializable("parentsStack");
106 mIsDisplayingFile = savedInstanceState.getBoolean("isDisplayingFile");
107 mPath = (LinkedList<String>)savedInstanceState.getSerializable("path");
108 } else {
109 mParents = new Stack<String>();
110 mPath = new LinkedList<String>();
111 mIsDisplayingFile = false;
112 }
113
114 mDBHandler = new DbHandler(getBaseContext());
115 requestWindowFeature(Window.FEATURE_NO_TITLE);
116 setContentView(R.layout.main);
117
118 AccountManager accMan = AccountManager.get(this);
119 Account[] accounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
120
121 if (accounts.length == 0) {
122 // using string value since in API7 this constatn is not defined
123 // in API7 < this constatant is defined in Settings.ADD_ACCOUNT_SETTINGS
124 // and Settings.EXTRA_AUTHORITIES
125 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
126 intent.putExtra("authorities", new String[] {AccountAuthenticator.AUTH_TOKEN_TYPE});
127 startActivity(intent);
128 } else if (accounts.length > 1) {
129 showDialog(DIALOG_CHOOSE_ACCOUNT);
130 } else {
131 mAccount = accounts[0];
132 populateFileList();
133 }*/
134 }
135
136 @Override
137 public boolean onOptionsItemSelected(MenuItem item) {
138 switch (item.getItemId()) {
139 case R.id.settingsItem :
140 Intent i = new Intent(this, Preferences.class);
141 startActivity(i);
142 break;
143 }
144 return true;
145 }
146
147 @Override
148 protected Dialog onCreateDialog(int id) {
149 switch (id) {
150 case DIALOG_CHOOSE_ACCOUNT:
151 return createChooseAccountDialog();
152 default:
153 throw new IllegalArgumentException("Unknown dialog id: " + id);
154 }
155 }
156
157 @Override
158 public boolean onCreateOptionsMenu(Menu menu) {
159 MenuInflater inflater = getMenuInflater();
160 inflater.inflate(R.menu.menu, menu);
161 return true;
162 }
163
164 private Dialog createChooseAccountDialog() {
165 final AccountManager accMan = AccountManager.get(this);
166 CharSequence[] items = new CharSequence[accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
167 int i = 0;
168 for (Account a : accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
169 items[i++] = a.name;
170 }
171
172 AlertDialog.Builder builder = new AlertDialog.Builder(this);
173 builder.setTitle(R.string.common_choose_account);
174 builder.setCancelable(true);
175 builder.setItems(items, new DialogInterface.OnClickListener() {
176 public void onClick(DialogInterface dialog, int item) {
177 mAccount = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
178 dialog.dismiss();
179 populateFileList();
180 }
181 });
182 builder.setOnCancelListener(new OnCancelListener() {
183 public void onCancel(DialogInterface dialog) {
184 FileDisplayActivity.this.finish();
185 }
186 });
187 AlertDialog alert = builder.create();
188 return alert;
189 }
190
191 //@Override
192 //public void onBackPressed() {
193 /*PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
194 if (mIsDisplayingFile) {
195 mIsDisplayingFile = false;
196 setContentView(R.layout.main);
197 pl = (PathLayout) findViewById(R.id.pathLayout1);
198 Uri uri;
199 if (mParents.empty()) {
200 uri = ProviderTableMeta.CONTENT_URI;
201 } else {
202 uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek());
203 }
204 mCursor = managedQuery(uri,
205 null,
206 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
207 new String[]{mAccount.name}, null);
208
209 if (mCursor.moveToFirst()) {
210 String s = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH));
211 for (String str : s.split("/")) {
212 if (!TextUtils.isEmpty(str))
213 pl.push(DisplayUtils.HtmlDecode(str));
214 }
215 }
216 getListView().setAdapter(new FileListListAdapter(mCursor, this));
217 getListView().invalidate();
218 return;
219 }
220 if (mParents.size()==0) {
221 super.onBackPressed();
222 return;
223 } else if (mParents.size() == 1) {
224 mParents.pop();
225 mPath.removeLast();
226 pl.pop();
227 mCursor = managedQuery(ProviderTableMeta.CONTENT_URI,
228 null,
229 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
230 new String[]{mAccount.name},
231 null);
232 } else {
233 mParents.pop();
234 mPath.removeLast();
235 pl.pop();
236 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
237 null,
238 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
239 new String[]{mAccount.name},
240 null);
241 }
242
243 setListAdapter(new FileListListAdapter(mCursor, this));
244 getListView().invalidate();*/
245 //}
246
247 //@Override
248 /* protected void onListItemClick(ListView l, View v, int position, long id) {
249 super.onListItemClick(l, v, position, id);
250 /*PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
251 if (!mCursor.moveToPosition(position)) {
252 throw new IndexOutOfBoundsException("Incorrect item selected");
253 }
254 if (!mIsDisplayingFile) {
255 if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
256 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
257 String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
258 pl.push(DisplayUtils.HtmlDecode(dirname));
259 mPath.addLast(DisplayUtils.HtmlDecode(dirname));
260 mParents.push(id_);
261 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
262 null,
263 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
264 new String[]{mAccount.name}, null);
265 setListAdapter(new FileListListAdapter(mCursor, this));
266 } else {
267 mIsDisplayingFile = true;
268 setContentView(R.layout.file_display);
269 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
270 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, id_),
271 null,
272 null,
273 null,
274 null);
275 mCursor.moveToFirst();
276 // filename
277 TextView tv = (TextView) findViewById(R.id.textView1);
278 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)));
279 // filetype
280 tv = (TextView) findViewById(R.id.textView2);
281 tv.setText(DisplayUtils.convertMIMEtoPrettyPrint(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))));
282 // size
283 tv = (TextView) findViewById(R.id.textView3);
284 tv.setText(DisplayUtils.bitsToHumanReadable(mCursor.getLong(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))));
285 // modified
286 tv = (TextView) findViewById(R.id.textView4);
287 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
288 if (!TextUtils.isEmpty(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH))) &&
289 mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).matches("image/*")) {
290 ImageView iv = (ImageView) findViewById(R.id.imageView1);
291 Bitmap bmp = BitmapFactory.decodeFile(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
292 Matrix m = new Matrix();
293 float scale;
294 if (bmp.getWidth() > bmp.getHeight()) {
295 scale = (float) (200./bmp.getWidth());
296 } else {
297 scale = (float) (200./bmp.getHeight());
298 }
299 m.postScale(scale, scale);
300 Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
301 iv.setImageBitmap(newBmp);
302 }
303 setListAdapter(new FileListActionListAdapter(mCursor, this, mAccount));
304 }
305 getListView().invalidate();
306 } else {
307 Intent i = (Intent) getListAdapter().getItem(position);
308 if (i.hasExtra("toDownload")) {
309
310 Intent intent = new Intent(this, FileDownloader.class);
311 intent.putExtra(FileDownloader.EXTRA_FILE_PATH, "/"+((TextView)findViewById(R.id.textView1)).getText().toString());
312 intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
313 startService(intent);
314 /*
315 if (i.getBooleanExtra("toDownload", false)) {
316 startActivityForResult(i, 200);
317 } else {
318 startActivity(i);
319 }*/
320 // }
321
322 //}
323 // }
324
325 private void populateFileList() {
326 if (mParents.empty()) {
327 mCursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
328 null,
329 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
330 new String[]{mAccount.name},
331 null);
332 } else {
333 mCursor = getContentResolver().query(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
334 null,
335 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
336 new String[]{mAccount.name}, null);
337 if (!mIsDisplayingFile) {
338 //PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
339 //for (String s : mPath) {
340 // pl.push(s);
341 // }
342 }
343 }
344 // setListAdapter(new FileListListAdapter(mCursor, this));
345 // getListView().invalidate();
346 }
347
348 //@Override
349 /*protected void onActivityResult(int requestCode, int resultCode, Intent data) {
350 super.onActivityResult(requestCode, resultCode, data);
351 }
352
353 @Override
354 protected void onSaveInstanceState(Bundle outState) {
355 super.onSaveInstanceState(outState);
356 outState.putSerializable("parentsStack", mParents);
357 outState.putSerializable("path", mPath);
358 outState.putBoolean("isDisplayingFile", mIsDisplayingFile);
359 }*/
360
361 }