Adjusted resolution according to googles specs
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / MainScreen.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;
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.authenticator.AccountAuthenticator;
57 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
58
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;
66
67 private static final int DIALOG_CHOOSE_ACCOUNT = 0;
68
69 @Override
70 public void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
72 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
73 setContentView(R.layout.main);
74
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());
79 }
80 ft.commit();
81
82 /*getSupportFragmentManager().beginTransaction().add(arg0, arg1);
83 FileList fl = new FileList();
84 ft.add(R.id.fileList, fl);
85 ft.commit();
86 /*
87
88
89 if (savedInstanceState != null) {
90 mParents = (Stack<String>)savedInstanceState.getSerializable("parentsStack");
91 mIsDisplayingFile = savedInstanceState.getBoolean("isDisplayingFile");
92 mPath = (LinkedList<String>)savedInstanceState.getSerializable("path");
93 } else {
94 mParents = new Stack<String>();
95 mPath = new LinkedList<String>();
96 mIsDisplayingFile = false;
97 }
98
99 mDBHandler = new DbHandler(getBaseContext());
100 requestWindowFeature(Window.FEATURE_NO_TITLE);
101 setContentView(R.layout.main);
102
103 AccountManager accMan = AccountManager.get(this);
104 Account[] accounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
105
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);
115 } else {
116 mAccount = accounts[0];
117 populateFileList();
118 }*/
119 }
120
121 @Override
122 public boolean onOptionsItemSelected(MenuItem item) {
123 switch (item.getItemId()) {
124 case R.id.settingsItem :
125 Intent i = new Intent(this, Preferences.class);
126 startActivity(i);
127 break;
128 }
129 return true;
130 }
131
132 @Override
133 protected Dialog onCreateDialog(int id) {
134 switch (id) {
135 case DIALOG_CHOOSE_ACCOUNT:
136 return createChooseAccountDialog();
137 default:
138 throw new IllegalArgumentException("Unknown dialog id: " + id);
139 }
140 }
141
142 @Override
143 public boolean onCreateOptionsMenu(Menu menu) {
144 MenuInflater inflater = getMenuInflater();
145 inflater.inflate(R.menu.menu, menu);
146 return true;
147 }
148
149 private Dialog createChooseAccountDialog() {
150 final AccountManager accMan = AccountManager.get(this);
151 CharSequence[] items = new CharSequence[accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
152 int i = 0;
153 for (Account a : accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
154 items[i++] = a.name;
155 }
156
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];
163 dialog.dismiss();
164 populateFileList();
165 }
166 });
167 builder.setOnCancelListener(new OnCancelListener() {
168 public void onCancel(DialogInterface dialog) {
169 MainScreen.this.finish();
170 }
171 });
172 AlertDialog alert = builder.create();
173 return alert;
174 }
175
176 //@Override
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);
183 Uri uri;
184 if (mParents.empty()) {
185 uri = ProviderTableMeta.CONTENT_URI;
186 } else {
187 uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek());
188 }
189 mCursor = managedQuery(uri,
190 null,
191 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
192 new String[]{mAccount.name}, null);
193
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));
199 }
200 }
201 getListView().setAdapter(new FileListListAdapter(mCursor, this));
202 getListView().invalidate();
203 return;
204 }
205 if (mParents.size()==0) {
206 super.onBackPressed();
207 return;
208 } else if (mParents.size() == 1) {
209 mParents.pop();
210 mPath.removeLast();
211 pl.pop();
212 mCursor = managedQuery(ProviderTableMeta.CONTENT_URI,
213 null,
214 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
215 new String[]{mAccount.name},
216 null);
217 } else {
218 mParents.pop();
219 mPath.removeLast();
220 pl.pop();
221 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
222 null,
223 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
224 new String[]{mAccount.name},
225 null);
226 }
227
228 setListAdapter(new FileListListAdapter(mCursor, this));
229 getListView().invalidate();*/
230 //}
231
232 //@Override
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");
238 }
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));
245 mParents.push(id_);
246 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
247 null,
248 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
249 new String[]{mAccount.name}, null);
250 setListAdapter(new FileListListAdapter(mCursor, this));
251 } else {
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_),
256 null,
257 null,
258 null,
259 null);
260 mCursor.moveToFirst();
261 // filename
262 TextView tv = (TextView) findViewById(R.id.textView1);
263 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)));
264 // filetype
265 tv = (TextView) findViewById(R.id.textView2);
266 tv.setText(DisplayUtils.convertMIMEtoPrettyPrint(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))));
267 // size
268 tv = (TextView) findViewById(R.id.textView3);
269 tv.setText(DisplayUtils.bitsToHumanReadable(mCursor.getLong(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))));
270 // modified
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();
278 float scale;
279 if (bmp.getWidth() > bmp.getHeight()) {
280 scale = (float) (200./bmp.getWidth());
281 } else {
282 scale = (float) (200./bmp.getHeight());
283 }
284 m.postScale(scale, scale);
285 Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
286 iv.setImageBitmap(newBmp);
287 }
288 setListAdapter(new FileListActionListAdapter(mCursor, this, mAccount));
289 }
290 getListView().invalidate();
291 } else {
292 Intent i = (Intent) getListAdapter().getItem(position);
293 if (i.hasExtra("toDownload")) {
294
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);
299 /*
300 if (i.getBooleanExtra("toDownload", false)) {
301 startActivityForResult(i, 200);
302 } else {
303 startActivity(i);
304 }*/
305 // }
306
307 //}
308 // }
309
310 private void populateFileList() {
311 if (mParents.empty()) {
312 mCursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
313 null,
314 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
315 new String[]{mAccount.name},
316 null);
317 } else {
318 mCursor = getContentResolver().query(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
319 null,
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) {
325 // pl.push(s);
326 // }
327 }
328 }
329 // setListAdapter(new FileListListAdapter(mCursor, this));
330 // getListView().invalidate();
331 }
332
333 //@Override
334 /*protected void onActivityResult(int requestCode, int resultCode, Intent data) {
335 super.onActivityResult(requestCode, resultCode, data);
336 }
337
338 @Override
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);
344 }*/
345
346 }