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
.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
;
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
;
56 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
59 public void onCreate(Bundle savedInstanceState
) {
60 super.onCreate(savedInstanceState
);
62 mParents
= new Stack
<String
>();
63 mIsDisplayingFile
= false
;
64 mDBHandler
= new DbHandler(getBaseContext());
65 requestWindowFeature(Window
.FEATURE_NO_TITLE
);
66 setContentView(R
.layout
.main
);
68 AccountManager accMan
= AccountManager
.get(this);
69 Account
[] accounts
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
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
);
81 mAccount
= accounts
[0];
87 public boolean onOptionsItemSelected(MenuItem item
) {
88 switch (item
.getItemId()) {
89 case R
.id
.settingsItem
:
90 Intent i
= new Intent(this, Preferences
.class);
98 protected Dialog
onCreateDialog(int id
) {
100 case DIALOG_CHOOSE_ACCOUNT
:
101 return createChooseAccountDialog();
103 throw new IllegalArgumentException("Unknown dialog id: " + id
);
108 public boolean onCreateOptionsMenu(Menu menu
) {
109 MenuInflater inflater
= getMenuInflater();
110 inflater
.inflate(R
.menu
.menu
, menu
);
115 protected void onDestroy() {
120 private Dialog
createChooseAccountDialog() {
121 final AccountManager accMan
= AccountManager
.get(this);
122 CharSequence
[] items
= new CharSequence
[accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
124 for (Account a
: accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
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
];
138 builder
.setOnCancelListener(new OnCancelListener() {
139 public void onCancel(DialogInterface dialog
) {
140 OwnCloudMainScreen
.this.finish();
143 AlertDialog alert
= builder
.create();
148 public void onBackPressed() {
149 if (mIsDisplayingFile
) {
150 mIsDisplayingFile
= false
;
151 setContentView(R
.layout
.main
);
153 if (mParents
.empty()) {
154 uri
= ProviderTableMeta
.CONTENT_URI
;
156 uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek());
158 mCursor
= managedQuery(uri
,
160 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
161 new String
[]{mAccount
.name
}, null
);
163 if (mCursor
.moveToFirst()) {
164 TextView tv
= (TextView
) findViewById(R
.id
.directory_name
);
165 tv
.setText(mCursor
.getString(mCursor
.getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
167 getListView().setAdapter(new FileListListAdapter(mCursor
, this));
168 getListView().invalidate();
171 if (mParents
.size()==0) {
172 super.onBackPressed();
174 } else if (mParents
.size() == 1) {
176 mCursor
= managedQuery(ProviderTableMeta
.CONTENT_URI
,
178 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
179 new String
[]{mAccount
.name
},
183 mCursor
= managedQuery(Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, mParents
.peek()),
185 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
186 new String
[]{mAccount
.name
},
190 setListAdapter(new FileListListAdapter(mCursor
, this));
191 getListView().invalidate();
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);
198 s
= s
.substring(0, s
.lastIndexOf('/') + 1);
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");
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
+"/");
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 TextView tv
= (TextView
) findViewById(R
.id
.directory_name
);
270 mCursor
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
272 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
273 new String
[]{mAccount
.name
},
275 setListAdapter(new FileListListAdapter(mCursor
, this));
276 getListView().invalidate();