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