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
;
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
.authenticator
.AccountAuthenticator
;
57 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
59 public class MainScreen
extends FragmentActivity
{
60 private DbHandler mDBHandler
;
61 private Stack
<String
> mParents
;
62 private LinkedList
<String
> mPath
;
63 private Account mAccount
;
64 private Cursor mCursor
;
65 private boolean mIsDisplayingFile
;
67 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
70 public void onCreate(Bundle savedInstanceState
) {
71 super.onCreate(savedInstanceState
);
72 getWindow().requestFeature(Window
.FEATURE_NO_TITLE
);
73 setContentView(R
.layout
.main
);
75 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
76 ft
.add(R
.id
.fileList
, new FileList());
77 if (getResources().getConfiguration().orientation
== Configuration
.ORIENTATION_LANDSCAPE
) {
78 ft
.add(R
.id
.fileDetail
, new FileDetail());
82 /*getSupportFragmentManager().beginTransaction().add(arg0, arg1);
83 FileList fl = new FileList();
84 ft.add(R.id.fileList, fl);
89 if (savedInstanceState != null) {
90 mParents = (Stack<String>)savedInstanceState.getSerializable("parentsStack");
91 mIsDisplayingFile = savedInstanceState.getBoolean("isDisplayingFile");
92 mPath = (LinkedList<String>)savedInstanceState.getSerializable("path");
94 mParents = new Stack<String>();
95 mPath = new LinkedList<String>();
96 mIsDisplayingFile = false;
99 mDBHandler = new DbHandler(getBaseContext());
100 requestWindowFeature(Window.FEATURE_NO_TITLE);
101 setContentView(R.layout.main);
103 AccountManager accMan = AccountManager.get(this);
104 Account[] accounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
106 if (accounts.length == 0) {
107 // using string value since in API7 this constatn is not defined
108 // in API7 < this constatant is defined in Settings.ADD_ACCOUNT_SETTINGS
109 // and Settings.EXTRA_AUTHORITIES
110 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
111 intent.putExtra("authorities", new String[] {AccountAuthenticator.AUTH_TOKEN_TYPE});
112 startActivity(intent);
113 } else if (accounts.length > 1) {
114 showDialog(DIALOG_CHOOSE_ACCOUNT);
116 mAccount = accounts[0];
122 public boolean onOptionsItemSelected(MenuItem item
) {
123 switch (item
.getItemId()) {
124 case R
.id
.settingsItem
:
125 Intent i
= new Intent(this, Preferences
.class);
133 protected Dialog
onCreateDialog(int id
) {
135 case DIALOG_CHOOSE_ACCOUNT
:
136 return createChooseAccountDialog();
138 throw new IllegalArgumentException("Unknown dialog id: " + id
);
143 public boolean onCreateOptionsMenu(Menu menu
) {
144 MenuInflater inflater
= getMenuInflater();
145 inflater
.inflate(R
.menu
.menu
, menu
);
149 private Dialog
createChooseAccountDialog() {
150 final AccountManager accMan
= AccountManager
.get(this);
151 CharSequence
[] items
= new CharSequence
[accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
153 for (Account a
: accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
157 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
158 builder
.setTitle(R
.string
.common_choose_account
);
159 builder
.setCancelable(true
);
160 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
161 public void onClick(DialogInterface dialog
, int item
) {
162 mAccount
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[item
];
167 builder
.setOnCancelListener(new OnCancelListener() {
168 public void onCancel(DialogInterface dialog
) {
169 MainScreen
.this.finish();
172 AlertDialog alert
= builder
.create();
177 //public void onBackPressed() {
178 /*PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
179 if (mIsDisplayingFile) {
180 mIsDisplayingFile = false;
181 setContentView(R.layout.main);
182 pl = (PathLayout) findViewById(R.id.pathLayout1);
184 if (mParents.empty()) {
185 uri = ProviderTableMeta.CONTENT_URI;
187 uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek());
189 mCursor = managedQuery(uri,
191 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
192 new String[]{mAccount.name}, null);
194 if (mCursor.moveToFirst()) {
195 String s = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH));
196 for (String str : s.split("/")) {
197 if (!TextUtils.isEmpty(str))
198 pl.push(DisplayUtils.HtmlDecode(str));
201 getListView().setAdapter(new FileListListAdapter(mCursor, this));
202 getListView().invalidate();
205 if (mParents.size()==0) {
206 super.onBackPressed();
208 } else if (mParents.size() == 1) {
212 mCursor = managedQuery(ProviderTableMeta.CONTENT_URI,
214 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
215 new String[]{mAccount.name},
221 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
223 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
224 new String[]{mAccount.name},
228 setListAdapter(new FileListListAdapter(mCursor, this));
229 getListView().invalidate();*/
233 /* protected void onListItemClick(ListView l, View v, int position, long id) {
234 super.onListItemClick(l, v, position, id);
235 /*PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
236 if (!mCursor.moveToPosition(position)) {
237 throw new IndexOutOfBoundsException("Incorrect item selected");
239 if (!mIsDisplayingFile) {
240 if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
241 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
242 String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
243 pl.push(DisplayUtils.HtmlDecode(dirname));
244 mPath.addLast(DisplayUtils.HtmlDecode(dirname));
246 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
248 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
249 new String[]{mAccount.name}, null);
250 setListAdapter(new FileListListAdapter(mCursor, this));
252 mIsDisplayingFile = true;
253 setContentView(R.layout.file_display);
254 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
255 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, id_),
260 mCursor.moveToFirst();
262 TextView tv = (TextView) findViewById(R.id.textView1);
263 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)));
265 tv = (TextView) findViewById(R.id.textView2);
266 tv.setText(DisplayUtils.convertMIMEtoPrettyPrint(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))));
268 tv = (TextView) findViewById(R.id.textView3);
269 tv.setText(DisplayUtils.bitsToHumanReadable(mCursor.getLong(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))));
271 tv = (TextView) findViewById(R.id.textView4);
272 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
273 if (!TextUtils.isEmpty(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH))) &&
274 mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).matches("image/*")) {
275 ImageView iv = (ImageView) findViewById(R.id.imageView1);
276 Bitmap bmp = BitmapFactory.decodeFile(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
277 Matrix m = new Matrix();
279 if (bmp.getWidth() > bmp.getHeight()) {
280 scale = (float) (200./bmp.getWidth());
282 scale = (float) (200./bmp.getHeight());
284 m.postScale(scale, scale);
285 Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
286 iv.setImageBitmap(newBmp);
288 setListAdapter(new FileListActionListAdapter(mCursor, this, mAccount));
290 getListView().invalidate();
292 Intent i = (Intent) getListAdapter().getItem(position);
293 if (i.hasExtra("toDownload")) {
295 Intent intent = new Intent(this, FileDownloader.class);
296 intent.putExtra(FileDownloader.EXTRA_FILE_PATH, "/"+((TextView)findViewById(R.id.textView1)).getText().toString());
297 intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
298 startService(intent);
300 if (i.getBooleanExtra("toDownload", false)) {
301 startActivityForResult(i, 200);
310 private void populateFileList() {
311 if (mParents
.empty()) {
312 mCursor
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
314 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
315 new String
[]{mAccount
.name
},
318 mCursor
= getContentResolver().query(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek()),
320 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
321 new String
[]{mAccount
.name
}, null
);
322 if (!mIsDisplayingFile
) {
323 //PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
324 //for (String s : mPath) {
329 // setListAdapter(new FileListListAdapter(mCursor, this));
330 // getListView().invalidate();
334 /*protected void onActivityResult(int requestCode, int resultCode, Intent data) {
335 super.onActivityResult(requestCode, resultCode, data);
339 protected void onSaveInstanceState(Bundle outState) {
340 super.onSaveInstanceState(outState);
341 outState.putSerializable("parentsStack", mParents);
342 outState.putSerializable("path", mPath);
343 outState.putBoolean("isDisplayingFile", mIsDisplayingFile);