initial commit
[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.TextUtils;
38 import android.view.Menu;
39 import android.view.MenuInflater;
40 import android.view.MenuItem;
41 import android.view.View;
42 import android.view.Window;
43 import android.widget.ImageView;
44 import android.widget.ListView;
45 import android.widget.TextView;
46 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
47 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
48
49 public class OwnCloudMainScreen extends ListActivity {
50 private DbHandler mDBHandler;
51 private Stack<String> mParents;
52 private Account mAccount;
53 private Cursor mCursor;
54 private boolean mIsDisplayingFile;
55
56 private static final int DIALOG_CHOOSE_ACCOUNT = 0;
57
58 @Override
59 public void onCreate(Bundle savedInstanceState) {
60 super.onCreate(savedInstanceState);
61
62 mParents = new Stack<String>();
63 mIsDisplayingFile = false;
64 mDBHandler = new DbHandler(getBaseContext());
65 requestWindowFeature(Window.FEATURE_NO_TITLE);
66 setContentView(R.layout.main);
67
68 AccountManager accMan = AccountManager.get(this);
69 Account[] accounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
70
71 if (accounts.length == 0) {
72 // using string value since in API7 this constatn is not defined
73 // in API7 < this constatant is defined in Settings.ADD_ACCOUNT_SETTINGS
74 // and Settings.EXTRA_AUTHORITIES
75 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
76 intent.putExtra("authorities", new String[] {AccountAuthenticator.AUTH_TOKEN_TYPE});
77 startActivity(intent);
78 } else if (accounts.length > 1) {
79 showDialog(DIALOG_CHOOSE_ACCOUNT);
80 } else {
81 mAccount = accounts[0];
82 populateFileList();
83 }
84 }
85
86 @Override
87 public boolean onOptionsItemSelected(MenuItem item) {
88 switch (item.getItemId()) {
89 case R.id.settingsItem :
90 Intent i = new Intent(this, Preferences.class);
91 startActivity(i);
92 break;
93 }
94 return true;
95 }
96
97 @Override
98 protected Dialog onCreateDialog(int id) {
99 switch (id) {
100 case DIALOG_CHOOSE_ACCOUNT:
101 return createChooseAccountDialog();
102 default:
103 throw new IllegalArgumentException("Unknown dialog id: " + id);
104 }
105 }
106
107 @Override
108 public boolean onCreateOptionsMenu(Menu menu) {
109 MenuInflater inflater = getMenuInflater();
110 inflater.inflate(R.menu.menu, menu);
111 return true;
112 }
113
114 @Override
115 protected void onDestroy() {
116 mDBHandler.close();
117 super.onDestroy();
118 }
119
120 private Dialog createChooseAccountDialog() {
121 final AccountManager accMan = AccountManager.get(this);
122 CharSequence[] items = new CharSequence[accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
123 int i = 0;
124 for (Account a : accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
125 items[i++] = a.name;
126 }
127
128 AlertDialog.Builder builder = new AlertDialog.Builder(this);
129 builder.setTitle(R.string.common_choose_account);
130 builder.setCancelable(true);
131 builder.setItems(items, new DialogInterface.OnClickListener() {
132 public void onClick(DialogInterface dialog, int item) {
133 mAccount = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
134 dialog.dismiss();
135 populateFileList();
136 }
137 });
138 builder.setOnCancelListener(new OnCancelListener() {
139 public void onCancel(DialogInterface dialog) {
140 OwnCloudMainScreen.this.finish();
141 }
142 });
143 AlertDialog alert = builder.create();
144 return alert;
145 }
146
147 @Override
148 public void onBackPressed() {
149 if (mIsDisplayingFile) {
150 mIsDisplayingFile = false;
151 setContentView(R.layout.main);
152 Uri uri;
153 if (mParents.empty()) {
154 uri = ProviderTableMeta.CONTENT_URI;
155 } else {
156 uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek());
157 }
158 mCursor = managedQuery(uri,
159 null,
160 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
161 new String[]{mAccount.name}, null);
162
163 if (mCursor.moveToFirst()) {
164 TextView tv = (TextView) findViewById(R.id.directory_name);
165 tv.setText(mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH)));
166 }
167 getListView().setAdapter(new FileListListAdapter(mCursor, this));
168 getListView().invalidate();
169 return;
170 }
171 if (mParents.size()==0) {
172 super.onBackPressed();
173 return;
174 } else if (mParents.size() == 1) {
175 mParents.pop();
176 mCursor = managedQuery(ProviderTableMeta.CONTENT_URI,
177 null,
178 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
179 new String[]{mAccount.name},
180 null);
181 } else {
182 mParents.pop();
183 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParents.peek()),
184 null,
185 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
186 new String[]{mAccount.name},
187 null);
188 }
189
190 setListAdapter(new FileListListAdapter(mCursor, this));
191 getListView().invalidate();
192
193 TextView tv = (TextView) findViewById(R.id.directory_name);
194 String s = tv.getText().toString();
195 if (s.endsWith("/")) {
196 s = s.substring(0, s.length() - 1);
197 }
198 s = s.substring(0, s.lastIndexOf('/') + 1);
199 tv.setText(s);
200 }
201
202 @Override
203 protected void onListItemClick(ListView l, View v, int position, long id) {
204 super.onListItemClick(l, v, position, id);
205 if (!mCursor.moveToPosition(position)) {
206 throw new IndexOutOfBoundsException("Incorrect item selected");
207 }
208 if (!mIsDisplayingFile) {
209 if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
210 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
211 String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
212 TextView tv = (TextView) findViewById(R.id.directory_name);
213 tv.setText(tv.getText().toString()+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 TextView tv = (TextView) findViewById(R.id.directory_name);
269 tv.setText("/");
270 mCursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
271 null,
272 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
273 new String[]{mAccount.name},
274 null);
275 setListAdapter(new FileListListAdapter(mCursor, this));
276 getListView().invalidate();
277 }
278
279
280
281 }