7ca5eb8cf08adfbb862a8218207392e417bc0f2e
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / OwnCloudMainScreen.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.util.Stack;
22
23 import android.accounts.Account;
24 import android.accounts.AccountManager;
25 import android.app.AlertDialog;
26 import android.app.Dialog;
27 import android.app.ListActivity;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.content.DialogInterface.OnCancelListener;
31 import android.content.res.Configuration;
32 import android.database.Cursor;
33 import android.graphics.Bitmap;
34 import android.graphics.BitmapFactory;
35 import android.graphics.Matrix;
36 import android.net.Uri;
37 import android.os.Bundle;
38 import android.text.TextUtils;
39 import android.view.Menu;
40 import android.view.MenuInflater;
41 import android.view.MenuItem;
42 import android.view.View;
43 import android.view.Window;
44 import android.widget.ImageView;
45 import android.widget.ListView;
46 import android.widget.TextView;
47 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
48 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
49
50 public class OwnCloudMainScreen extends ListActivity {
51 private DbHandler mDBHandler;
52 private Stack<String> mParents;
53 private Account mAccount;
54 private Cursor mCursor;
55 private boolean mIsDisplayingFile;
56
57 private static final int DIALOG_CHOOSE_ACCOUNT = 0;
58
59 @Override
60 public void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62
63 mParents = new Stack<String>();
64 mIsDisplayingFile = false;
65 mDBHandler = new DbHandler(getBaseContext());
66 requestWindowFeature(Window.FEATURE_NO_TITLE);
67 setContentView(R.layout.main);
68
69 AccountManager accMan = AccountManager.get(this);
70 Account[] accounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
71
72 if (accounts.length == 0) {
73 // using string value since in API7 this constatn is not defined
74 // in API7 < this constatant is defined in Settings.ADD_ACCOUNT_SETTINGS
75 // and Settings.EXTRA_AUTHORITIES
76 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
77 intent.putExtra("authorities", new String[] {AccountAuthenticator.AUTH_TOKEN_TYPE});
78 startActivity(intent);
79 } else if (accounts.length > 1) {
80 showDialog(DIALOG_CHOOSE_ACCOUNT);
81 } else {
82 mAccount = accounts[0];
83 populateFileList();
84 }
85 }
86
87 @Override
88 public boolean onOptionsItemSelected(MenuItem item) {
89 switch (item.getItemId()) {
90 case R.id.settingsItem :
91 Intent i = new Intent(this, Preferences.class);
92 startActivity(i);
93 break;
94 }
95 return true;
96 }
97
98 @Override
99 protected Dialog onCreateDialog(int id) {
100 switch (id) {
101 case DIALOG_CHOOSE_ACCOUNT:
102 return createChooseAccountDialog();
103 default:
104 throw new IllegalArgumentException("Unknown dialog id: " + id);
105 }
106 }
107
108 @Override
109 public boolean onCreateOptionsMenu(Menu menu) {
110 MenuInflater inflater = getMenuInflater();
111 inflater.inflate(R.menu.menu, menu);
112 return true;
113 }
114
115 @Override
116 protected void onDestroy() {
117 mDBHandler.close();
118 super.onDestroy();
119 }
120
121 private Dialog createChooseAccountDialog() {
122 final AccountManager accMan = AccountManager.get(this);
123 CharSequence[] items = new CharSequence[accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
124 int i = 0;
125 for (Account a : accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
126 items[i++] = a.name;
127 }
128
129 AlertDialog.Builder builder = new AlertDialog.Builder(this);
130 builder.setTitle(R.string.common_choose_account);
131 builder.setCancelable(true);
132 builder.setItems(items, new DialogInterface.OnClickListener() {
133 public void onClick(DialogInterface dialog, int item) {
134 mAccount = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
135 dialog.dismiss();
136 populateFileList();
137 }
138 });
139 builder.setOnCancelListener(new OnCancelListener() {
140 public void onCancel(DialogInterface dialog) {
141 OwnCloudMainScreen.this.finish();
142 }
143 });
144 AlertDialog alert = builder.create();
145 return alert;
146 }
147
148 @Override
149 public void onBackPressed() {
150 PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
151 if (mIsDisplayingFile) {
152 mIsDisplayingFile = false;
153 setContentView(R.layout.main);
154 pl = (PathLayout) findViewById(R.id.pathLayout1);
155 Uri uri;
156 if (mParents.empty()) {
157 uri = ProviderTableMeta.CONTENT_URI;
158 } else {
159 uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek());
160 }
161 mCursor = managedQuery(uri,
162 null,
163 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
164 new String[]{mAccount.name}, null);
165
166 if (mCursor.moveToFirst()) {
167 String s = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH));
168 for (String str : s.split("/")) {
169 if (!TextUtils.isEmpty(str))
170 pl.push(DisplayUtils.HtmlDecode(str));
171 }
172 }
173 getListView().setAdapter(new FileListListAdapter(mCursor, this));
174 getListView().invalidate();
175 return;
176 }
177 if (mParents.size()==0) {
178 super.onBackPressed();
179 return;
180 } else if (mParents.size() == 1) {
181 mParents.pop();
182 pl.pop();
183 mCursor = managedQuery(ProviderTableMeta.CONTENT_URI,
184 null,
185 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
186 new String[]{mAccount.name},
187 null);
188 } else {
189 mParents.pop();
190 pl.pop();
191 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
192 null,
193 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
194 new String[]{mAccount.name},
195 null);
196 }
197
198 setListAdapter(new FileListListAdapter(mCursor, this));
199 getListView().invalidate();
200 }
201
202 @Override
203 protected void onListItemClick(ListView l, View v, int position, long id) {
204 super.onListItemClick(l, v, position, id);
205 PathLayout pl = (PathLayout) findViewById(R.id.pathLayout1);
206 if (!mCursor.moveToPosition(position)) {
207 throw new IndexOutOfBoundsException("Incorrect item selected");
208 }
209 if (!mIsDisplayingFile) {
210 if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
211 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
212 String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
213 pl.push(DisplayUtils.HtmlDecode(dirname));
214 mParents.push(id_);
215 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
216 null,
217 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
218 new String[]{mAccount.name}, null);
219 setListAdapter(new FileListListAdapter(mCursor, this));
220 } else {
221 mIsDisplayingFile = true;
222 setContentView(R.layout.file_display);
223 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
224 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, id_),
225 null,
226 null,
227 null,
228 null);
229 mCursor.moveToFirst();
230 // filename
231 TextView tv = (TextView) findViewById(R.id.textView1);
232 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)));
233 // filetype
234 tv = (TextView) findViewById(R.id.textView2);
235 tv.setText(DisplayUtils.convertMIMEtoPrettyPrint(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))));
236 // size
237 tv = (TextView) findViewById(R.id.textView3);
238 tv.setText(DisplayUtils.bitsToHumanReadable(mCursor.getLong(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))));
239 // modified
240 tv = (TextView) findViewById(R.id.textView4);
241 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
242 if (!TextUtils.isEmpty(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)))) {
243 ImageView iv = (ImageView) findViewById(R.id.imageView1);
244 Bitmap bmp = BitmapFactory.decodeFile(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
245 Matrix m = new Matrix();
246 float scale;
247 if (bmp.getWidth() > bmp.getHeight()) {
248 scale = (float) (200./bmp.getWidth());
249 } else {
250 scale = (float) (200./bmp.getHeight());
251 }
252 m.postScale(scale, scale);
253 Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
254 iv.setImageBitmap(newBmp);
255 }
256 setListAdapter(new FileListActionListAdapter(mCursor, this, mAccount));
257 }
258 getListView().invalidate();
259 } else {
260 try {
261 Intent i = (Intent) getListAdapter().getItem(position);
262 startActivity(i);
263 } catch (ClassCastException e) {}
264 }
265 }
266
267 private void populateFileList() {
268 mCursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
269 null,
270 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
271 new String[]{mAccount.name},
272 null);
273 setListAdapter(new FileListListAdapter(mCursor, this));
274 getListView().invalidate();
275 }
276
277 @Override
278 public void onConfigurationChanged(Configuration newConfig) {
279 // TODO Auto-generated method stub
280 //super.onConfigurationChanged(newConfig);
281 }
282
283 }