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
;
90 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
92 public void pushPath(String path
) {
93 mDirectories
.insert(path
, 0);
96 public boolean popPath() {
97 mDirectories
.remove(mDirectories
.getItem(0));
98 Log
.d("TAG", ""+getActionBar().getCustomView());
99 return !mDirectories
.isEmpty();
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);
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());
119 /*getSupportFragmentManager().beginTransaction().add(arg0, arg1);
120 FileList fl = new FileList();
121 ft.add(R.id.fileList, fl);
126 if (savedInstanceState != null) {
127 mParents = (Stack<String>)savedInstanceState.getSerializable("parentsStack");
128 mIsDisplayingFile = savedInstanceState.getBoolean("isDisplayingFile");
129 mPath = (LinkedList<String>)savedInstanceState.getSerializable("path");
131 mParents = new Stack<String>();
132 mPath = new LinkedList<String>();
133 mIsDisplayingFile = false;
136 mDBHandler = new DbHandler(getBaseContext());
137 requestWindowFeature(Window.FEATURE_NO_TITLE);
138 setContentView(R.layout.main);
140 AccountManager accMan = AccountManager.get(this);
141 Account[] accounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
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);
153 mAccount = accounts[0];
159 public boolean onOptionsItemSelected(MenuItem item
) {
160 switch (item
.getItemId()) {
161 case R
.id
.settingsItem
:
162 Intent i
= new Intent(this, Preferences
.class);
170 protected Dialog
onCreateDialog(int id
) {
172 case DIALOG_CHOOSE_ACCOUNT
:
173 return createChooseAccountDialog();
175 throw new IllegalArgumentException("Unknown dialog id: " + id
);
180 public boolean onCreateOptionsMenu(Menu menu
) {
181 MenuInflater inflater
= getMenuInflater();
182 inflater
.inflate(R
.menu
.menu
, menu
);
186 private Dialog
createChooseAccountDialog() {
187 final AccountManager accMan
= AccountManager
.get(this);
188 CharSequence
[] items
= new CharSequence
[accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
190 for (Account a
: accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
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
];
203 builder
.setOnCancelListener(new OnCancelListener() {
204 public void onCancel(DialogInterface dialog
) {
205 FileDisplayActivity
.this.finish();
208 AlertDialog alert
= builder
.create();
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);
220 if (mParents.empty()) {
221 uri = ProviderTableMeta.CONTENT_URI;
223 uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek());
225 mCursor = managedQuery(uri,
227 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
228 new String[]{mAccount.name}, null);
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));
237 getListView().setAdapter(new FileListListAdapter(mCursor, this));
238 getListView().invalidate();
241 if (mParents.size()==0) {
242 super.onBackPressed();
244 } else if (mParents.size() == 1) {
248 mCursor = managedQuery(ProviderTableMeta.CONTENT_URI,
250 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
251 new String[]{mAccount.name},
257 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
259 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
260 new String[]{mAccount.name},
264 setListAdapter(new FileListListAdapter(mCursor, this));
265 getListView().invalidate();*/
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");
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));
282 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
284 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
285 new String[]{mAccount.name}, null);
286 setListAdapter(new FileListListAdapter(mCursor, this));
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_),
296 mCursor.moveToFirst();
298 TextView tv = (TextView) findViewById(R.id.textView1);
299 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)));
301 tv = (TextView) findViewById(R.id.textView2);
302 tv.setText(DisplayUtils.convertMIMEtoPrettyPrint(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))));
304 tv = (TextView) findViewById(R.id.textView3);
305 tv.setText(DisplayUtils.bitsToHumanReadable(mCursor.getLong(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))));
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();
315 if (bmp.getWidth() > bmp.getHeight()) {
316 scale = (float) (200./bmp.getWidth());
318 scale = (float) (200./bmp.getHeight());
320 m.postScale(scale, scale);
321 Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
322 iv.setImageBitmap(newBmp);
324 setListAdapter(new FileListActionListAdapter(mCursor, this, mAccount));
326 getListView().invalidate();
328 Intent i = (Intent) getListAdapter().getItem(position);
329 if (i.hasExtra("toDownload")) {
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);
336 if (i.getBooleanExtra("toDownload", false)) {
337 startActivityForResult(i, 200);
347 // setListAdapter(new FileListListAdapter(mCursor, this));
348 // getListView().invalidate();
351 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
352 int i
= itemPosition
;
355 //mFileList.onBackPressed();
361 public void onBackPressed() {
363 if (mDirectories
.getCount() == 0)
365 super.onBackPressed();
368 ((FileList
)getSupportFragmentManager().findFragmentById(id
.file_list_container
)).onBackPressed();
372 /*protected void onActivityResult(int requestCode, int resultCode, Intent data) {
373 super.onActivityResult(requestCode, resultCode, data);
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);