1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
;
21 import java
.util
.Stack
;
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
;
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
;
58 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
61 public void onCreate(Bundle savedInstanceState
) {
62 super.onCreate(savedInstanceState
);
64 mParents
= new Stack
<String
>();
65 mIsDisplayingFile
= false
;
66 mDBHandler
= new DbHandler(getBaseContext());
67 requestWindowFeature(Window
.FEATURE_NO_TITLE
);
68 setContentView(R
.layout
.main
);
70 AccountManager accMan
= AccountManager
.get(this);
71 Account
[] accounts
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
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
);
83 mAccount
= accounts
[0];
89 public boolean onOptionsItemSelected(MenuItem item
) {
90 switch (item
.getItemId()) {
91 case R
.id
.settingsItem
:
92 Intent i
= new Intent(this, Preferences
.class);
100 protected Dialog
onCreateDialog(int id
) {
102 case DIALOG_CHOOSE_ACCOUNT
:
103 return createChooseAccountDialog();
105 throw new IllegalArgumentException("Unknown dialog id: " + id
);
110 public boolean onCreateOptionsMenu(Menu menu
) {
111 MenuInflater inflater
= getMenuInflater();
112 inflater
.inflate(R
.menu
.menu
, menu
);
117 protected void onDestroy() {
122 private Dialog
createChooseAccountDialog() {
123 final AccountManager accMan
= AccountManager
.get(this);
124 CharSequence
[] items
= new CharSequence
[accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
126 for (Account a
: accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
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
];
140 builder
.setOnCancelListener(new OnCancelListener() {
141 public void onCancel(DialogInterface dialog
) {
142 OwnCloudMainScreen
.this.finish();
145 AlertDialog alert
= builder
.create();
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
);
157 if (mParents
.empty()) {
158 uri
= ProviderTableMeta
.CONTENT_URI
;
160 uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek());
162 mCursor
= managedQuery(uri
,
164 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
165 new String
[]{mAccount
.name
}, null
);
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", " "));
174 getListView().setAdapter(new FileListListAdapter(mCursor
, this));
175 getListView().invalidate();
178 if (mParents
.size()==0) {
179 super.onBackPressed();
181 } else if (mParents
.size() == 1) {
184 mCursor
= managedQuery(ProviderTableMeta
.CONTENT_URI
,
186 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
187 new String
[]{mAccount
.name
},
192 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek()),
194 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
195 new String
[]{mAccount
.name
},
199 setListAdapter(new FileListListAdapter(mCursor
, this));
200 getListView().invalidate();
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");
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
));
216 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, id_
),
218 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
219 new String
[]{mAccount
.name
}, null
);
220 setListAdapter(new FileListListAdapter(mCursor
, this));
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_
),
230 mCursor
.moveToFirst();
232 TextView tv
= (TextView
) findViewById(R
.id
.textView1
);
233 tv
.setText(mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_NAME
)));
235 tv
= (TextView
) findViewById(R
.id
.textView2
);
236 tv
.setText(DisplayUtils
.convertMIMEtoPrettyPrint(mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
))));
238 tv
= (TextView
) findViewById(R
.id
.textView3
);
239 tv
.setText(DisplayUtils
.bitsToHumanReadable(mCursor
.getLong(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
))));
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();
248 if (bmp
.getWidth() > bmp
.getHeight()) {
249 scale
= (float) (200./bmp
.getWidth());
251 scale
= (float) (200./bmp
.getHeight());
253 m
.postScale(scale
, scale
);
254 Bitmap newBmp
= Bitmap
.createBitmap(bmp
, 0, 0, bmp
.getWidth(), bmp
.getHeight(), m
, true
);
255 iv
.setImageBitmap(newBmp
);
257 setListAdapter(new FileListActionListAdapter(mCursor
, this, mAccount
));
259 getListView().invalidate();
262 Intent i
= (Intent
) getListAdapter().getItem(position
);
264 } catch (ClassCastException e
) {}
268 private void populateFileList() {
269 mCursor
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
271 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
272 new String
[]{mAccount
.name
},
274 setListAdapter(new FileListListAdapter(mCursor
, this));
275 getListView().invalidate();