1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
.ui
.activity
;
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
;
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
;
76 * Displays, what files the user has available in his ownCloud.
77 * @author Bartek Przybylski
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 private FileList mFileList
;
91 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
93 public void pushPath(String path
) {
94 mDirectories
.insert(path
, 0);
97 public boolean popPath() {
98 mDirectories
.remove(mDirectories
.getItem(0));
99 Log
.d("TAG", ""+getActionBar().getCustomView());
100 return !mDirectories
.isEmpty();
104 public void onCreate(Bundle savedInstanceState
) {
105 super.onCreate(savedInstanceState
);
106 mDirectories
= new ArrayAdapter
<String
>(this, android
.R
.layout
.simple_spinner_dropdown_item
);
107 mDirectories
.add("/");
108 mFileList
= new FileList();
109 setContentView(R
.layout
.files
);
110 getActionBar().setNavigationMode(android
.support
.v4
.app
.ActionBar
.NAVIGATION_MODE_LIST
);
111 getActionBar().setDisplayShowTitleEnabled(false
);
112 getActionBar().setListNavigationCallbacks(mDirectories
, this);
114 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
115 ft
.add(R
.id
.file_list_container
, mFileList
);
116 if (getResources().getConfiguration().orientation
== Configuration
.ORIENTATION_LANDSCAPE
) {
117 ft
.add(R
.id
.fileDetail
, new FileDetail());
121 /*getSupportFragmentManager().beginTransaction().add(arg0, arg1);
122 FileList fl = new FileList();
123 ft.add(R.id.fileList, fl);
128 if (savedInstanceState != null) {
129 mParents = (Stack<String>)savedInstanceState.getSerializable("parentsStack");
130 mIsDisplayingFile = savedInstanceState.getBoolean("isDisplayingFile");
131 mPath = (LinkedList<String>)savedInstanceState.getSerializable("path");
133 mParents = new Stack<String>();
134 mPath = new LinkedList<String>();
135 mIsDisplayingFile = false;
138 mDBHandler = new DbHandler(getBaseContext());
139 requestWindowFeature(Window.FEATURE_NO_TITLE);
140 setContentView(R.layout.main);
142 AccountManager accMan = AccountManager.get(this);
143 Account[] accounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
145 if (accounts.length == 0) {
146 // using string value since in API7 this constatn is not defined
147 // in API7 < this constatant is defined in Settings.ADD_ACCOUNT_SETTINGS
148 // and Settings.EXTRA_AUTHORITIES
149 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
150 intent.putExtra("authorities", new String[] {AccountAuthenticator.AUTH_TOKEN_TYPE});
151 startActivity(intent);
152 } else if (accounts.length > 1) {
153 showDialog(DIALOG_CHOOSE_ACCOUNT);
155 mAccount = accounts[0];
161 public boolean onOptionsItemSelected(MenuItem item
) {
162 switch (item
.getItemId()) {
163 case R
.id
.settingsItem
:
164 Intent i
= new Intent(this, Preferences
.class);
172 protected Dialog
onCreateDialog(int id
) {
174 case DIALOG_CHOOSE_ACCOUNT
:
175 return createChooseAccountDialog();
177 throw new IllegalArgumentException("Unknown dialog id: " + id
);
182 public boolean onCreateOptionsMenu(Menu menu
) {
183 MenuInflater inflater
= getMenuInflater();
184 inflater
.inflate(R
.menu
.menu
, menu
);
188 private Dialog
createChooseAccountDialog() {
189 final AccountManager accMan
= AccountManager
.get(this);
190 CharSequence
[] items
= new CharSequence
[accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
192 for (Account a
: accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
196 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
197 builder
.setTitle(R
.string
.common_choose_account
);
198 builder
.setCancelable(true
);
199 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
200 public void onClick(DialogInterface dialog
, int item
) {
201 mAccount
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[item
];
205 builder
.setOnCancelListener(new OnCancelListener() {
206 public void onCancel(DialogInterface dialog
) {
207 FileDisplayActivity
.this.finish();
210 AlertDialog alert
= builder
.create();
215 //public void onBackPressed() {
216 /*PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
217 if (mIsDisplayingFile) {
218 mIsDisplayingFile = false;
219 setContentView(R.layout.main);
220 pl = (PathLayout) findViewById(R.id.pathLayout1);
222 if (mParents.empty()) {
223 uri = ProviderTableMeta.CONTENT_URI;
225 uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek());
227 mCursor = managedQuery(uri,
229 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
230 new String[]{mAccount.name}, null);
232 if (mCursor.moveToFirst()) {
233 String s = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH));
234 for (String str : s.split("/")) {
235 if (!TextUtils.isEmpty(str))
236 pl.push(DisplayUtils.HtmlDecode(str));
239 getListView().setAdapter(new FileListListAdapter(mCursor, this));
240 getListView().invalidate();
243 if (mParents.size()==0) {
244 super.onBackPressed();
246 } else if (mParents.size() == 1) {
250 mCursor = managedQuery(ProviderTableMeta.CONTENT_URI,
252 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
253 new String[]{mAccount.name},
259 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
261 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
262 new String[]{mAccount.name},
266 setListAdapter(new FileListListAdapter(mCursor, this));
267 getListView().invalidate();*/
271 /* protected void onListItemClick(ListView l, View v, int position, long id) {
272 super.onListItemClick(l, v, position, id);
273 /*PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
274 if (!mCursor.moveToPosition(position)) {
275 throw new IndexOutOfBoundsException("Incorrect item selected");
277 if (!mIsDisplayingFile) {
278 if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
279 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
280 String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
281 pl.push(DisplayUtils.HtmlDecode(dirname));
282 mPath.addLast(DisplayUtils.HtmlDecode(dirname));
284 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
286 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
287 new String[]{mAccount.name}, null);
288 setListAdapter(new FileListListAdapter(mCursor, this));
290 mIsDisplayingFile = true;
291 setContentView(R.layout.file_display);
292 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
293 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, id_),
298 mCursor.moveToFirst();
300 TextView tv = (TextView) findViewById(R.id.textView1);
301 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)));
303 tv = (TextView) findViewById(R.id.textView2);
304 tv.setText(DisplayUtils.convertMIMEtoPrettyPrint(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))));
306 tv = (TextView) findViewById(R.id.textView3);
307 tv.setText(DisplayUtils.bitsToHumanReadable(mCursor.getLong(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))));
309 tv = (TextView) findViewById(R.id.textView4);
310 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
311 if (!TextUtils.isEmpty(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH))) &&
312 mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).matches("image/*")) {
313 ImageView iv = (ImageView) findViewById(R.id.imageView1);
314 Bitmap bmp = BitmapFactory.decodeFile(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
315 Matrix m = new Matrix();
317 if (bmp.getWidth() > bmp.getHeight()) {
318 scale = (float) (200./bmp.getWidth());
320 scale = (float) (200./bmp.getHeight());
322 m.postScale(scale, scale);
323 Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
324 iv.setImageBitmap(newBmp);
326 setListAdapter(new FileListActionListAdapter(mCursor, this, mAccount));
328 getListView().invalidate();
330 Intent i = (Intent) getListAdapter().getItem(position);
331 if (i.hasExtra("toDownload")) {
333 Intent intent = new Intent(this, FileDownloader.class);
334 intent.putExtra(FileDownloader.EXTRA_FILE_PATH, "/"+((TextView)findViewById(R.id.textView1)).getText().toString());
335 intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
336 startService(intent);
338 if (i.getBooleanExtra("toDownload", false)) {
339 startActivityForResult(i, 200);
349 // setListAdapter(new FileListListAdapter(mCursor, this));
350 // getListView().invalidate();
353 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
354 int i
= itemPosition
;
357 //mFileList.onBackPressed();
363 public void onBackPressed() {
365 //getSupportFragmentManager().popBackStack();
366 //super.onBackPressed();
367 getSupportFragmentManager().popBackStackImmediate();
371 /*protected void onActivityResult(int requestCode, int resultCode, Intent data) {
372 super.onActivityResult(requestCode, resultCode, data);
376 protected void onSaveInstanceState(Bundle outState) {
377 super.onSaveInstanceState(outState);
378 outState.putSerializable("parentsStack", mParents);
379 outState.putSerializable("path", mPath);
380 outState.putBoolean("isDisplayingFile", mIsDisplayingFile);