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
.R
;
46 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
47 import eu
.alefzero
.owncloud
.authenticator
.AuthUtils
;
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
{
63 private ArrayAdapter
<String
> mDirectories
;
64 private DataStorageManager mStorageManager
;
66 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
68 public void pushPath(String path
) {
69 mDirectories
.insert(path
, 0);
72 public boolean popPath() {
73 mDirectories
.remove(mDirectories
.getItem(0));
74 return !mDirectories
.isEmpty();
78 protected Dialog
onCreateDialog(int id
, Bundle args
) {
79 final AlertDialog
.Builder builder
= new Builder(this);
80 final EditText dirName
= new EditText(getBaseContext());
81 final Account a
= AuthUtils
.getCurrentOwnCloudAccount(this);
82 builder
.setView(dirName
);
83 builder
.setTitle(R
.string
.uploader_info_dirname
);
84 dirName
.setTextColor(R
.color
.setup_text_typed
);
86 builder
.setPositiveButton(R
.string
.common_ok
, new OnClickListener() {
87 public void onClick(DialogInterface dialog
, int which
) {
88 String s
= dirName
.getText().toString();
89 if (s
.trim().isEmpty()) {
95 for (int i
= mDirectories
.getCount() - 2; i
>= 0; --i
) {
96 path
+= "/" + mDirectories
.getItem(i
);
98 OCFile parent
= mStorageManager
.getFileByPath(path
+ "/");
100 Thread thread
= new Thread(new DirectoryCreator(path
, a
));
103 OCFile new_file
= new OCFile(path
);
104 new_file
.setMimetype("DIR");
105 new_file
.setParentId(parent
.getParentId());
106 mStorageManager
.saveFile(new_file
);
111 builder
.setNegativeButton(R
.string
.common_cancel
,
112 new OnClickListener() {
113 public void onClick(DialogInterface dialog
, int which
) {
117 return builder
.create();
121 public void onCreate(Bundle savedInstanceState
) {
122 super.onCreate(savedInstanceState
);
123 mDirectories
= new CustomArrayAdapter
<String
>(this,
124 R
.layout
.sherlock_spinner_dropdown_item
);
125 mDirectories
.add("/");
126 setContentView(R
.layout
.files
);
127 mStorageManager
= new FileDataStorageManager(AuthUtils
.getCurrentOwnCloudAccount(this), getContentResolver());
128 ActionBar action_bar
= getSupportActionBar();
129 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
130 action_bar
.setDisplayShowTitleEnabled(false
);
131 action_bar
.setListNavigationCallbacks(mDirectories
, this);
132 action_bar
.setDisplayHomeAsUpEnabled(true
);
136 public boolean onOptionsItemSelected(MenuItem item
) {
137 switch (item
.getItemId()) {
138 case R
.id
.settingsItem
: {
139 Intent i
= new Intent(this, Preferences
.class);
143 case R
.id
.createDirectoryItem
: {
147 case android
.R
.id
.home
: {
157 public void onBackPressed(){
159 if(mDirectories
.getCount() == 0) {
160 Intent intent
= new Intent(this, LandingActivity
.class);
161 startActivity(intent
);
164 ((FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
))
169 protected Dialog
onCreateDialog(int id
) {
171 case DIALOG_CHOOSE_ACCOUNT
:
172 return createChooseAccountDialog();
174 throw new IllegalArgumentException("Unknown dialog id: " + id
);
179 public boolean onCreateOptionsMenu(Menu menu
) {
180 MenuInflater inflater
= getSherlock().getMenuInflater();
181 inflater
.inflate(R
.menu
.menu
, menu
);
185 private Dialog
createChooseAccountDialog() {
186 final AccountManager accMan
= AccountManager
.get(this);
187 CharSequence
[] items
= new CharSequence
[accMan
188 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
190 for (Account a
: accMan
191 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
195 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
196 builder
.setTitle(R
.string
.common_choose_account
);
197 builder
.setCancelable(true
);
198 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
199 public void onClick(DialogInterface dialog
, int item
) {
201 // accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
205 builder
.setOnCancelListener(new OnCancelListener() {
206 public void onCancel(DialogInterface dialog
) {
207 FileDisplayActivity
.this.finish();
210 AlertDialog alert
= builder
.create();
215 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
216 int i
= itemPosition
;
223 private class DirectoryCreator
implements Runnable
{
224 private String mTargetPath
;
225 private Account mAccount
;
226 private AccountManager mAm
;
228 public DirectoryCreator(String targetPath
, Account account
) {
229 mTargetPath
= targetPath
;
231 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
236 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(
237 mAccount
, AccountAuthenticator
.KEY_OC_URL
)));
239 String username
= mAccount
.name
.substring(0,
240 mAccount
.name
.lastIndexOf('@'));
241 String password
= mAm
.getPassword(mAccount
);
243 wdc
.setCredentials(username
, password
);
244 wdc
.allowUnsignedCertificates();
245 wdc
.createDirectory(mTargetPath
);
250 // Custom array adapter to override text colors
251 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
253 public CustomArrayAdapter(FileDisplayActivity ctx
,
258 public View
getView(int position
, View convertView
,
260 View v
= super.getView(position
, convertView
, parent
);
262 ((TextView
) v
).setTextColor(
264 .getColorStateList(android
.R
.color
.white
));
268 public View
getDropDownView(int position
, View convertView
,
270 View v
= super.getDropDownView(position
, convertView
,
273 ((TextView
) v
).setTextColor(getResources().getColorStateList(
274 android
.R
.color
.white
));