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