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
.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
;
69 * Displays, what files the user has available in his ownCloud.
70 * @author Bartek Przybylski
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
;
81 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
84 public void onCreate(Bundle savedInstanceState
) {
85 super.onCreate(savedInstanceState
);
86 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
87 setContentView(R
.layout
.files
);
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());
97 /*getSupportFragmentManager().beginTransaction().add(arg0, arg1);
98 FileList fl = new FileList();
99 ft.add(R.id.fileList, fl);
104 if (savedInstanceState != null) {
105 mParents = (Stack<String>)savedInstanceState.getSerializable("parentsStack");
106 mIsDisplayingFile = savedInstanceState.getBoolean("isDisplayingFile");
107 mPath = (LinkedList<String>)savedInstanceState.getSerializable("path");
109 mParents = new Stack<String>();
110 mPath = new LinkedList<String>();
111 mIsDisplayingFile = false;
114 mDBHandler = new DbHandler(getBaseContext());
115 requestWindowFeature(Window.FEATURE_NO_TITLE);
116 setContentView(R.layout.main);
118 AccountManager accMan = AccountManager.get(this);
119 Account[] accounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
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);
131 mAccount = accounts[0];
137 public boolean onOptionsItemSelected(MenuItem item
) {
138 switch (item
.getItemId()) {
139 case R
.id
.settingsItem
:
140 Intent i
= new Intent(this, Preferences
.class);
148 protected Dialog
onCreateDialog(int id
) {
150 case DIALOG_CHOOSE_ACCOUNT
:
151 return createChooseAccountDialog();
153 throw new IllegalArgumentException("Unknown dialog id: " + id
);
158 public boolean onCreateOptionsMenu(Menu menu
) {
159 MenuInflater inflater
= getMenuInflater();
160 inflater
.inflate(R
.menu
.menu
, menu
);
164 private Dialog
createChooseAccountDialog() {
165 final AccountManager accMan
= AccountManager
.get(this);
166 CharSequence
[] items
= new CharSequence
[accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
168 for (Account a
: accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
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
];
182 builder
.setOnCancelListener(new OnCancelListener() {
183 public void onCancel(DialogInterface dialog
) {
184 FileDisplayActivity
.this.finish();
187 AlertDialog alert
= builder
.create();
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);
199 if (mParents.empty()) {
200 uri = ProviderTableMeta.CONTENT_URI;
202 uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek());
204 mCursor = managedQuery(uri,
206 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
207 new String[]{mAccount.name}, null);
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));
216 getListView().setAdapter(new FileListListAdapter(mCursor, this));
217 getListView().invalidate();
220 if (mParents.size()==0) {
221 super.onBackPressed();
223 } else if (mParents.size() == 1) {
227 mCursor = managedQuery(ProviderTableMeta.CONTENT_URI,
229 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
230 new String[]{mAccount.name},
236 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
238 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
239 new String[]{mAccount.name},
243 setListAdapter(new FileListListAdapter(mCursor, this));
244 getListView().invalidate();*/
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");
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));
261 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
263 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
264 new String[]{mAccount.name}, null);
265 setListAdapter(new FileListListAdapter(mCursor, this));
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_),
275 mCursor.moveToFirst();
277 TextView tv = (TextView) findViewById(R.id.textView1);
278 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)));
280 tv = (TextView) findViewById(R.id.textView2);
281 tv.setText(DisplayUtils.convertMIMEtoPrettyPrint(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))));
283 tv = (TextView) findViewById(R.id.textView3);
284 tv.setText(DisplayUtils.bitsToHumanReadable(mCursor.getLong(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))));
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();
294 if (bmp.getWidth() > bmp.getHeight()) {
295 scale = (float) (200./bmp.getWidth());
297 scale = (float) (200./bmp.getHeight());
299 m.postScale(scale, scale);
300 Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
301 iv.setImageBitmap(newBmp);
303 setListAdapter(new FileListActionListAdapter(mCursor, this, mAccount));
305 getListView().invalidate();
307 Intent i = (Intent) getListAdapter().getItem(position);
308 if (i.hasExtra("toDownload")) {
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);
315 if (i.getBooleanExtra("toDownload", false)) {
316 startActivityForResult(i, 200);
325 private void populateFileList() {
326 if (mParents
.empty()) {
327 mCursor
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
329 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
330 new String
[]{mAccount
.name
},
333 mCursor
= getContentResolver().query(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek()),
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) {
344 // setListAdapter(new FileListListAdapter(mCursor, this));
345 // getListView().invalidate();
349 /*protected void onActivityResult(int requestCode, int resultCode, Intent data) {
350 super.onActivityResult(requestCode, resultCode, data);
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);