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
;
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
.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
.text
.TextUtils
;
47 import android
.util
.Log
;
48 import android
.view
.Menu
;
49 import android
.view
.MenuInflater
;
50 import android
.view
.MenuItem
;
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
.DbHandler
;
57 import eu
.alefzero
.owncloud
.FileDetail
;
58 import eu
.alefzero
.owncloud
.R
;
59 import eu
.alefzero
.owncloud
.R
.id
;
60 import eu
.alefzero
.owncloud
.R
.layout
;
61 import eu
.alefzero
.owncloud
.R
.menu
;
62 import eu
.alefzero
.owncloud
.R
.string
;
63 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
64 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
65 import eu
.alefzero
.owncloud
.ui
.fragment
.FileList
;
67 public class FileDisplayActivity
extends FragmentActivity
{
68 private DbHandler mDBHandler
;
69 private Stack
<String
> mParents
;
70 private LinkedList
<String
> mPath
;
71 private Account mAccount
;
72 private Cursor mCursor
;
73 private boolean mIsDisplayingFile
;
75 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
78 public void onCreate(Bundle savedInstanceState
) {
79 super.onCreate(savedInstanceState
);
80 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
81 setContentView(R
.layout
.main
);
83 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
84 ft
.add(R
.id
.fileList
, new FileList());
85 if (getResources().getConfiguration().orientation
== Configuration
.ORIENTATION_LANDSCAPE
) {
86 ft
.add(R
.id
.fileDetail
, new FileDetail());
90 /*getSupportFragmentManager().beginTransaction().add(arg0, arg1);
91 FileList fl = new FileList();
92 ft.add(R.id.fileList, fl);
97 if (savedInstanceState != null) {
98 mParents = (Stack<String>)savedInstanceState.getSerializable("parentsStack");
99 mIsDisplayingFile = savedInstanceState.getBoolean("isDisplayingFile");
100 mPath = (LinkedList<String>)savedInstanceState.getSerializable("path");
102 mParents = new Stack<String>();
103 mPath = new LinkedList<String>();
104 mIsDisplayingFile = false;
107 mDBHandler = new DbHandler(getBaseContext());
108 requestWindowFeature(Window.FEATURE_NO_TITLE);
109 setContentView(R.layout.main);
111 AccountManager accMan = AccountManager.get(this);
112 Account[] accounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
114 if (accounts.length == 0) {
115 // using string value since in API7 this constatn is not defined
116 // in API7 < this constatant is defined in Settings.ADD_ACCOUNT_SETTINGS
117 // and Settings.EXTRA_AUTHORITIES
118 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
119 intent.putExtra("authorities", new String[] {AccountAuthenticator.AUTH_TOKEN_TYPE});
120 startActivity(intent);
121 } else if (accounts.length > 1) {
122 showDialog(DIALOG_CHOOSE_ACCOUNT);
124 mAccount = accounts[0];
130 public boolean onOptionsItemSelected(MenuItem item
) {
131 switch (item
.getItemId()) {
132 case R
.id
.settingsItem
:
133 Intent i
= new Intent(this, Preferences
.class);
141 protected Dialog
onCreateDialog(int id
) {
143 case DIALOG_CHOOSE_ACCOUNT
:
144 return createChooseAccountDialog();
146 throw new IllegalArgumentException("Unknown dialog id: " + id
);
151 public boolean onCreateOptionsMenu(Menu menu
) {
152 MenuInflater inflater
= getMenuInflater();
153 inflater
.inflate(R
.menu
.menu
, menu
);
157 private Dialog
createChooseAccountDialog() {
158 final AccountManager accMan
= AccountManager
.get(this);
159 CharSequence
[] items
= new CharSequence
[accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
161 for (Account a
: accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
165 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
166 builder
.setTitle(R
.string
.common_choose_account
);
167 builder
.setCancelable(true
);
168 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
169 public void onClick(DialogInterface dialog
, int item
) {
170 mAccount
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[item
];
175 builder
.setOnCancelListener(new OnCancelListener() {
176 public void onCancel(DialogInterface dialog
) {
177 FileDisplayActivity
.this.finish();
180 AlertDialog alert
= builder
.create();
185 //public void onBackPressed() {
186 /*PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
187 if (mIsDisplayingFile) {
188 mIsDisplayingFile = false;
189 setContentView(R.layout.main);
190 pl = (PathLayout) findViewById(R.id.pathLayout1);
192 if (mParents.empty()) {
193 uri = ProviderTableMeta.CONTENT_URI;
195 uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek());
197 mCursor = managedQuery(uri,
199 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
200 new String[]{mAccount.name}, null);
202 if (mCursor.moveToFirst()) {
203 String s = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH));
204 for (String str : s.split("/")) {
205 if (!TextUtils.isEmpty(str))
206 pl.push(DisplayUtils.HtmlDecode(str));
209 getListView().setAdapter(new FileListListAdapter(mCursor, this));
210 getListView().invalidate();
213 if (mParents.size()==0) {
214 super.onBackPressed();
216 } else if (mParents.size() == 1) {
220 mCursor = managedQuery(ProviderTableMeta.CONTENT_URI,
222 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
223 new String[]{mAccount.name},
229 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
231 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
232 new String[]{mAccount.name},
236 setListAdapter(new FileListListAdapter(mCursor, this));
237 getListView().invalidate();*/
241 /* protected void onListItemClick(ListView l, View v, int position, long id) {
242 super.onListItemClick(l, v, position, id);
243 /*PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
244 if (!mCursor.moveToPosition(position)) {
245 throw new IndexOutOfBoundsException("Incorrect item selected");
247 if (!mIsDisplayingFile) {
248 if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
249 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
250 String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
251 pl.push(DisplayUtils.HtmlDecode(dirname));
252 mPath.addLast(DisplayUtils.HtmlDecode(dirname));
254 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
256 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
257 new String[]{mAccount.name}, null);
258 setListAdapter(new FileListListAdapter(mCursor, this));
260 mIsDisplayingFile = true;
261 setContentView(R.layout.file_display);
262 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
263 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, id_),
268 mCursor.moveToFirst();
270 TextView tv = (TextView) findViewById(R.id.textView1);
271 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)));
273 tv = (TextView) findViewById(R.id.textView2);
274 tv.setText(DisplayUtils.convertMIMEtoPrettyPrint(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))));
276 tv = (TextView) findViewById(R.id.textView3);
277 tv.setText(DisplayUtils.bitsToHumanReadable(mCursor.getLong(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))));
279 tv = (TextView) findViewById(R.id.textView4);
280 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
281 if (!TextUtils.isEmpty(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH))) &&
282 mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).matches("image/*")) {
283 ImageView iv = (ImageView) findViewById(R.id.imageView1);
284 Bitmap bmp = BitmapFactory.decodeFile(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
285 Matrix m = new Matrix();
287 if (bmp.getWidth() > bmp.getHeight()) {
288 scale = (float) (200./bmp.getWidth());
290 scale = (float) (200./bmp.getHeight());
292 m.postScale(scale, scale);
293 Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
294 iv.setImageBitmap(newBmp);
296 setListAdapter(new FileListActionListAdapter(mCursor, this, mAccount));
298 getListView().invalidate();
300 Intent i = (Intent) getListAdapter().getItem(position);
301 if (i.hasExtra("toDownload")) {
303 Intent intent = new Intent(this, FileDownloader.class);
304 intent.putExtra(FileDownloader.EXTRA_FILE_PATH, "/"+((TextView)findViewById(R.id.textView1)).getText().toString());
305 intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
306 startService(intent);
308 if (i.getBooleanExtra("toDownload", false)) {
309 startActivityForResult(i, 200);
318 private void populateFileList() {
319 if (mParents
.empty()) {
320 mCursor
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
322 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
323 new String
[]{mAccount
.name
},
326 mCursor
= getContentResolver().query(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek()),
328 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
329 new String
[]{mAccount
.name
}, null
);
330 if (!mIsDisplayingFile
) {
331 //PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
332 //for (String s : mPath) {
337 // setListAdapter(new FileListListAdapter(mCursor, this));
338 // getListView().invalidate();
342 /*protected void onActivityResult(int requestCode, int resultCode, Intent data) {
343 super.onActivityResult(requestCode, resultCode, data);
347 protected void onSaveInstanceState(Bundle outState) {
348 super.onSaveInstanceState(outState);
349 outState.putSerializable("parentsStack", mParents);
350 outState.putSerializable("path", mPath);
351 outState.putBoolean("isDisplayingFile", mIsDisplayingFile);