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
.ui
.activity
;
21 import android
.accounts
.Account
;
22 import android
.accounts
.AccountManager
;
23 import android
.app
.AlertDialog
;
24 import android
.app
.AlertDialog
.Builder
;
25 import android
.app
.Dialog
;
26 import android
.content
.DialogInterface
;
27 import android
.content
.DialogInterface
.OnCancelListener
;
28 import android
.content
.DialogInterface
.OnClickListener
;
29 import android
.content
.Intent
;
30 import android
.net
.Uri
;
31 import android
.os
.Bundle
;
32 import android
.view
.View
;
33 import android
.view
.ViewGroup
;
34 import android
.widget
.ArrayAdapter
;
35 import android
.widget
.EditText
;
36 import android
.widget
.TextView
;
38 import com
.actionbarsherlock
.app
.ActionBar
;
39 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
40 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
41 import com
.actionbarsherlock
.view
.Menu
;
42 import com
.actionbarsherlock
.view
.MenuInflater
;
43 import com
.actionbarsherlock
.view
.MenuItem
;
45 import eu
.alefzero
.owncloud
.AccountUtils
;
46 import eu
.alefzero
.owncloud
.R
;
47 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
48 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
49 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
50 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
51 import eu
.alefzero
.owncloud
.ui
.fragment
.FileListFragment
;
52 import eu
.alefzero
.webdav
.WebdavClient
;
55 * Displays, what files the user has available in his ownCloud.
57 * @author Bartek Przybylski
61 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
62 OnNavigationListener
, OnClickListener
{
63 private ArrayAdapter
<String
> mDirectories
;
64 private DataStorageManager mStorageManager
;
66 private static final int DIALOG_SETUP_ACCOUNT
= 0;
67 private static final int DIALOG_CREATE_DIR
= 1;
69 public void pushPath(String path
) {
70 mDirectories
.insert(path
, 0);
73 public boolean popPath() {
74 mDirectories
.remove(mDirectories
.getItem(0));
75 return !mDirectories
.isEmpty();
79 protected Dialog
onCreateDialog(int id
) {
81 AlertDialog
.Builder builder
;
83 case DIALOG_SETUP_ACCOUNT
:
84 builder
= new AlertDialog
.Builder(this);
85 builder
.setTitle(R
.string
.main_tit_accsetup
);
86 builder
.setMessage(R
.string
.main_wrn_accsetup
);
87 builder
.setCancelable(false
);
88 builder
.setPositiveButton(R
.string
.common_ok
, this);
89 builder
.setNegativeButton(R
.string
.common_cancel
, this);
90 dialog
= builder
.create();
92 case DIALOG_CREATE_DIR
:
94 builder
= new Builder(this);
95 final EditText dirName
= new EditText(getBaseContext());
96 final Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
97 builder
.setView(dirName
);
98 builder
.setTitle(R
.string
.uploader_info_dirname
);
99 dirName
.setTextColor(R
.color
.setup_text_typed
);
101 builder
.setPositiveButton(R
.string
.common_ok
, new OnClickListener() {
102 public void onClick(DialogInterface dialog
, int which
) {
103 String s
= dirName
.getText().toString();
104 if (s
.trim().isEmpty()) {
110 for (int i
= mDirectories
.getCount() - 2; i
>= 0; --i
) {
111 path
+= "/" + mDirectories
.getItem(i
);
113 OCFile parent
= mStorageManager
.getFileByPath(path
+ "/");
115 Thread thread
= new Thread(new DirectoryCreator(path
, a
));
118 OCFile new_file
= new OCFile(path
);
119 new_file
.setMimetype("DIR");
120 new_file
.setParentId(parent
.getParentId());
121 mStorageManager
.saveFile(new_file
);
126 builder
.setNegativeButton(R
.string
.common_cancel
,
127 new OnClickListener() {
128 public void onClick(DialogInterface dialog
, int which
) {
141 public void onCreate(Bundle savedInstanceState
) {
142 super.onCreate(savedInstanceState
);
143 if(!accountsAreSetup()){
144 showDialog(DIALOG_SETUP_ACCOUNT
);
148 mDirectories
= new CustomArrayAdapter
<String
>(this,
149 R
.layout
.sherlock_spinner_dropdown_item
);
150 mDirectories
.add("/");
151 setContentView(R
.layout
.files
);
152 mStorageManager
= new FileDataStorageManager(AccountUtils
.getCurrentOwnCloudAccount(this), getContentResolver());
153 ActionBar action_bar
= getSupportActionBar();
154 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
155 action_bar
.setDisplayShowTitleEnabled(false
);
156 action_bar
.setListNavigationCallbacks(mDirectories
, this);
157 action_bar
.setDisplayHomeAsUpEnabled(true
);
161 public boolean onOptionsItemSelected(MenuItem item
) {
162 switch (item
.getItemId()) {
163 case R
.id
.settingsItem
: {
164 Intent i
= new Intent(this, Preferences
.class);
168 case R
.id
.createDirectoryItem
: {
169 showDialog(DIALOG_CREATE_DIR
);
172 case android
.R
.id
.home
: {
182 public void onBackPressed(){
183 if(mDirectories
.getCount() == 1) {
188 ((FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
))
193 public boolean onCreateOptionsMenu(Menu menu
) {
194 MenuInflater inflater
= getSherlock().getMenuInflater();
195 inflater
.inflate(R
.menu
.menu
, menu
);
200 protected void onRestoreInstanceState(Bundle savedInstanceState
) {
201 super.onRestoreInstanceState(savedInstanceState
);
202 // Check, if there are ownCloud accounts
203 if(!accountsAreSetup()){
204 showDialog(DIALOG_SETUP_ACCOUNT
);
209 protected void onStart() {
211 // Check, if there are ownCloud accounts
212 if(!accountsAreSetup()){
213 showDialog(DIALOG_SETUP_ACCOUNT
);
218 protected void onResume() {
220 if(!accountsAreSetup()){
221 showDialog(DIALOG_SETUP_ACCOUNT
);
226 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
227 int i
= itemPosition
;
234 private class DirectoryCreator
implements Runnable
{
235 private String mTargetPath
;
236 private Account mAccount
;
237 private AccountManager mAm
;
239 public DirectoryCreator(String targetPath
, Account account
) {
240 mTargetPath
= targetPath
;
242 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
247 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(
248 mAccount
, AccountAuthenticator
.KEY_OC_URL
)));
250 String username
= mAccount
.name
.substring(0,
251 mAccount
.name
.lastIndexOf('@'));
252 String password
= mAm
.getPassword(mAccount
);
254 wdc
.setCredentials(username
, password
);
255 wdc
.allowUnsignedCertificates();
256 wdc
.createDirectory(mTargetPath
);
261 // Custom array adapter to override text colors
262 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
264 public CustomArrayAdapter(FileDisplayActivity ctx
,
269 public View
getView(int position
, View convertView
,
271 View v
= super.getView(position
, convertView
, parent
);
273 ((TextView
) v
).setTextColor(
275 .getColorStateList(android
.R
.color
.white
));
279 public View
getDropDownView(int position
, View convertView
,
281 View v
= super.getDropDownView(position
, convertView
,
284 ((TextView
) v
).setTextColor(getResources().getColorStateList(
285 android
.R
.color
.white
));
294 public void onClick(DialogInterface dialog
, int which
) {
295 // In any case - we won't need it anymore
298 case DialogInterface
.BUTTON_POSITIVE
:
299 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
300 intent
.putExtra("authorities",
301 new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
302 startActivity(intent
);
304 case DialogInterface
.BUTTON_NEGATIVE
:
311 * Checks, whether or not there are any ownCloud accounts
314 * @return true, if there is at least one account.
316 private boolean accountsAreSetup() {
317 AccountManager accMan
= AccountManager
.get(this);
318 Account
[] accounts
= accMan
319 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
320 return accounts
.length
> 0;