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
.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
;
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
;
57 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
60 public void onCreate(Bundle savedInstanceState
) {
61 super.onCreate(savedInstanceState
);
63 mParents
= new Stack
<String
>();
64 mIsDisplayingFile
= false
;
65 mDBHandler
= new DbHandler(getBaseContext());
66 requestWindowFeature(Window
.FEATURE_NO_TITLE
);
67 setContentView(R
.layout
.main
);
69 AccountManager accMan
= AccountManager
.get(this);
70 Account
[] accounts
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
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
);
82 mAccount
= accounts
[0];
88 public boolean onOptionsItemSelected(MenuItem item
) {
89 switch (item
.getItemId()) {
90 case R
.id
.settingsItem
:
91 Intent i
= new Intent(this, Preferences
.class);
99 protected Dialog
onCreateDialog(int id
) {
101 case DIALOG_CHOOSE_ACCOUNT
:
102 return createChooseAccountDialog();
104 throw new IllegalArgumentException("Unknown dialog id: " + id
);
109 public boolean onCreateOptionsMenu(Menu menu
) {
110 MenuInflater inflater
= getMenuInflater();
111 inflater
.inflate(R
.menu
.menu
, menu
);
116 protected void onDestroy() {
121 private Dialog
createChooseAccountDialog() {
122 final AccountManager accMan
= AccountManager
.get(this);
123 CharSequence
[] items
= new CharSequence
[accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
125 for (Account a
: accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
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
];
139 builder
.setOnCancelListener(new OnCancelListener() {
140 public void onCancel(DialogInterface dialog
) {
141 OwnCloudMainScreen
.this.finish();
144 AlertDialog alert
= builder
.create();
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
);
156 if (mParents
.empty()) {
157 uri
= ProviderTableMeta
.CONTENT_URI
;
159 uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek());
161 mCursor
= managedQuery(uri
,
163 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
164 new String
[]{mAccount
.name
}, null
);
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
));
173 getListView().setAdapter(new FileListListAdapter(mCursor
, this));
174 getListView().invalidate();
177 if (mParents
.size()==0) {
178 super.onBackPressed();
180 } else if (mParents
.size() == 1) {
183 mCursor
= managedQuery(ProviderTableMeta
.CONTENT_URI
,
185 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
186 new String
[]{mAccount
.name
},
191 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek()),
193 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
194 new String
[]{mAccount
.name
},
198 setListAdapter(new FileListListAdapter(mCursor
, this));
199 getListView().invalidate();
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");
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
));
215 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, id_
),
217 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
218 new String
[]{mAccount
.name
}, null
);
219 setListAdapter(new FileListListAdapter(mCursor
, this));
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_
),
229 mCursor
.moveToFirst();
231 TextView tv
= (TextView
) findViewById(R
.id
.textView1
);
232 tv
.setText(mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_NAME
)));
234 tv
= (TextView
) findViewById(R
.id
.textView2
);
235 tv
.setText(DisplayUtils
.convertMIMEtoPrettyPrint(mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
))));
237 tv
= (TextView
) findViewById(R
.id
.textView3
);
238 tv
.setText(DisplayUtils
.bitsToHumanReadable(mCursor
.getLong(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
))));
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();
247 if (bmp
.getWidth() > bmp
.getHeight()) {
248 scale
= (float) (200./bmp
.getWidth());
250 scale
= (float) (200./bmp
.getHeight());
252 m
.postScale(scale
, scale
);
253 Bitmap newBmp
= Bitmap
.createBitmap(bmp
, 0, 0, bmp
.getWidth(), bmp
.getHeight(), m
, true
);
254 iv
.setImageBitmap(newBmp
);
256 setListAdapter(new FileListActionListAdapter(mCursor
, this, mAccount
));
258 getListView().invalidate();
261 Intent i
= (Intent
) getListAdapter().getItem(position
);
263 } catch (ClassCastException e
) {}
267 private void populateFileList() {
268 mCursor
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
270 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
271 new String
[]{mAccount
.name
},
273 setListAdapter(new FileListListAdapter(mCursor
, this));
274 getListView().invalidate();
278 public void onConfigurationChanged(Configuration newConfig
) {
279 // TODO Auto-generated method stub
280 //super.onConfigurationChanged(newConfig);