dd9239376fbfefdc291866fc1274e0d13a58745d
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / FileDisplayActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.ui;
20
21 import java.io.File;
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;
27
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;
66
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;
74
75 private static final int DIALOG_CHOOSE_ACCOUNT = 0;
76
77 @Override
78 public void onCreate(Bundle savedInstanceState) {
79 super.onCreate(savedInstanceState);
80 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
81 setContentView(R.layout.main);
82
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());
87 }
88 ft.commit();
89
90 /*getSupportFragmentManager().beginTransaction().add(arg0, arg1);
91 FileList fl = new FileList();
92 ft.add(R.id.fileList, fl);
93 ft.commit();
94 /*
95
96
97 if (savedInstanceState != null) {
98 mParents = (Stack<String>)savedInstanceState.getSerializable("parentsStack");
99 mIsDisplayingFile = savedInstanceState.getBoolean("isDisplayingFile");
100 mPath = (LinkedList<String>)savedInstanceState.getSerializable("path");
101 } else {
102 mParents = new Stack<String>();
103 mPath = new LinkedList<String>();
104 mIsDisplayingFile = false;
105 }
106
107 mDBHandler = new DbHandler(getBaseContext());
108 requestWindowFeature(Window.FEATURE_NO_TITLE);
109 setContentView(R.layout.main);
110
111 AccountManager accMan = AccountManager.get(this);
112 Account[] accounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
113
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);
123 } else {
124 mAccount = accounts[0];
125 populateFileList();
126 }*/
127 }
128
129 @Override
130 public boolean onOptionsItemSelected(MenuItem item) {
131 switch (item.getItemId()) {
132 case R.id.settingsItem :
133 Intent i = new Intent(this, Preferences.class);
134 startActivity(i);
135 break;
136 }
137 return true;
138 }
139
140 @Override
141 protected Dialog onCreateDialog(int id) {
142 switch (id) {
143 case DIALOG_CHOOSE_ACCOUNT:
144 return createChooseAccountDialog();
145 default:
146 throw new IllegalArgumentException("Unknown dialog id: " + id);
147 }
148 }
149
150 @Override
151 public boolean onCreateOptionsMenu(Menu menu) {
152 MenuInflater inflater = getMenuInflater();
153 inflater.inflate(R.menu.menu, menu);
154 return true;
155 }
156
157 private Dialog createChooseAccountDialog() {
158 final AccountManager accMan = AccountManager.get(this);
159 CharSequence[] items = new CharSequence[accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
160 int i = 0;
161 for (Account a : accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
162 items[i++] = a.name;
163 }
164
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];
171 dialog.dismiss();
172 populateFileList();
173 }
174 });
175 builder.setOnCancelListener(new OnCancelListener() {
176 public void onCancel(DialogInterface dialog) {
177 FileDisplayActivity.this.finish();
178 }
179 });
180 AlertDialog alert = builder.create();
181 return alert;
182 }
183
184 //@Override
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);
191 Uri uri;
192 if (mParents.empty()) {
193 uri = ProviderTableMeta.CONTENT_URI;
194 } else {
195 uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek());
196 }
197 mCursor = managedQuery(uri,
198 null,
199 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
200 new String[]{mAccount.name}, null);
201
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));
207 }
208 }
209 getListView().setAdapter(new FileListListAdapter(mCursor, this));
210 getListView().invalidate();
211 return;
212 }
213 if (mParents.size()==0) {
214 super.onBackPressed();
215 return;
216 } else if (mParents.size() == 1) {
217 mParents.pop();
218 mPath.removeLast();
219 pl.pop();
220 mCursor = managedQuery(ProviderTableMeta.CONTENT_URI,
221 null,
222 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
223 new String[]{mAccount.name},
224 null);
225 } else {
226 mParents.pop();
227 mPath.removeLast();
228 pl.pop();
229 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
230 null,
231 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
232 new String[]{mAccount.name},
233 null);
234 }
235
236 setListAdapter(new FileListListAdapter(mCursor, this));
237 getListView().invalidate();*/
238 //}
239
240 //@Override
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");
246 }
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));
253 mParents.push(id_);
254 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
255 null,
256 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
257 new String[]{mAccount.name}, null);
258 setListAdapter(new FileListListAdapter(mCursor, this));
259 } else {
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_),
264 null,
265 null,
266 null,
267 null);
268 mCursor.moveToFirst();
269 // filename
270 TextView tv = (TextView) findViewById(R.id.textView1);
271 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)));
272 // filetype
273 tv = (TextView) findViewById(R.id.textView2);
274 tv.setText(DisplayUtils.convertMIMEtoPrettyPrint(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))));
275 // size
276 tv = (TextView) findViewById(R.id.textView3);
277 tv.setText(DisplayUtils.bitsToHumanReadable(mCursor.getLong(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))));
278 // modified
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();
286 float scale;
287 if (bmp.getWidth() > bmp.getHeight()) {
288 scale = (float) (200./bmp.getWidth());
289 } else {
290 scale = (float) (200./bmp.getHeight());
291 }
292 m.postScale(scale, scale);
293 Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
294 iv.setImageBitmap(newBmp);
295 }
296 setListAdapter(new FileListActionListAdapter(mCursor, this, mAccount));
297 }
298 getListView().invalidate();
299 } else {
300 Intent i = (Intent) getListAdapter().getItem(position);
301 if (i.hasExtra("toDownload")) {
302
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);
307 /*
308 if (i.getBooleanExtra("toDownload", false)) {
309 startActivityForResult(i, 200);
310 } else {
311 startActivity(i);
312 }*/
313 // }
314
315 //}
316 // }
317
318 private void populateFileList() {
319 if (mParents.empty()) {
320 mCursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
321 null,
322 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
323 new String[]{mAccount.name},
324 null);
325 } else {
326 mCursor = getContentResolver().query(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
327 null,
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) {
333 // pl.push(s);
334 // }
335 }
336 }
337 // setListAdapter(new FileListListAdapter(mCursor, this));
338 // getListView().invalidate();
339 }
340
341 //@Override
342 /*protected void onActivityResult(int requestCode, int resultCode, Intent data) {
343 super.onActivityResult(requestCode, resultCode, data);
344 }
345
346 @Override
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);
352 }*/
353
354 }