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
.OCFile
;
49 import eu
.alefzero
.owncloud
.ui
.fragment
.FileList
;
50 import eu
.alefzero
.webdav
.WebdavClient
;
53 * Displays, what files the user has available in his ownCloud.
55 * @author Bartek Przybylski
59 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
60 OnNavigationListener
{
61 private ArrayAdapter
<String
> mDirectories
;
63 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
65 public void pushPath(String path
) {
66 mDirectories
.insert(path
, 0);
69 public boolean popPath() {
70 mDirectories
.remove(mDirectories
.getItem(0));
71 return !mDirectories
.isEmpty();
75 protected Dialog
onCreateDialog(int id
, Bundle args
) {
76 final AlertDialog
.Builder builder
= new Builder(this);
77 final EditText dirName
= new EditText(getBaseContext());
78 final Account a
= AuthUtils
.getCurrentOwnCloudAccount(this);
79 builder
.setView(dirName
);
80 builder
.setTitle(R
.string
.uploader_info_dirname
);
82 builder
.setPositiveButton(R
.string
.common_ok
, new OnClickListener() {
83 public void onClick(DialogInterface dialog
, int which
) {
84 String s
= dirName
.getText().toString();
85 if (s
.trim().isEmpty()) {
91 for (int i
= mDirectories
.getCount() - 2; i
>= 0; --i
) {
92 path
+= "/" + mDirectories
.getItem(i
);
94 OCFile parent
= new OCFile(getContentResolver(), a
, path
+ "/");
95 path
+= "/" + s
+ "/";
96 Thread thread
= new Thread(new DirectoryCreator(path
, a
));
98 OCFile
.createNewFile(getContentResolver(), a
, path
, 0, 0, 0,
99 "DIR", parent
.getFileId()).save();
104 builder
.setNegativeButton(R
.string
.common_cancel
,
105 new OnClickListener() {
106 public void onClick(DialogInterface dialog
, int which
) {
110 return builder
.create();
114 public void onCreate(Bundle savedInstanceState
) {
115 super.onCreate(savedInstanceState
);
116 mDirectories
= new CustomArrayAdapter
<String
>(this,
117 R
.layout
.sherlock_spinner_dropdown_item
);
118 mDirectories
.add("/");
119 setContentView(R
.layout
.files
);
120 ActionBar action_bar
= getSupportActionBar();
121 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
122 action_bar
.setDisplayShowTitleEnabled(false
);
123 action_bar
.setListNavigationCallbacks(mDirectories
, this);
124 action_bar
.setDisplayHomeAsUpEnabled(true
);
128 public boolean onOptionsItemSelected(MenuItem item
) {
129 switch (item
.getItemId()) {
130 case R
.id
.settingsItem
: {
131 Intent i
= new Intent(this, Preferences
.class);
135 case R
.id
.createDirectoryItem
: {
144 protected Dialog
onCreateDialog(int id
) {
146 case DIALOG_CHOOSE_ACCOUNT
:
147 return createChooseAccountDialog();
149 throw new IllegalArgumentException("Unknown dialog id: " + id
);
154 public boolean onCreateOptionsMenu(Menu menu
) {
155 MenuInflater inflater
= getSherlock().getMenuInflater();
156 inflater
.inflate(R
.menu
.menu
, menu
);
160 private Dialog
createChooseAccountDialog() {
161 final AccountManager accMan
= AccountManager
.get(this);
162 CharSequence
[] items
= new CharSequence
[accMan
163 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
165 for (Account a
: accMan
166 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
170 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
171 builder
.setTitle(R
.string
.common_choose_account
);
172 builder
.setCancelable(true
);
173 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
174 public void onClick(DialogInterface dialog
, int item
) {
176 // accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
180 builder
.setOnCancelListener(new OnCancelListener() {
181 public void onCancel(DialogInterface dialog
) {
182 FileDisplayActivity
.this.finish();
185 AlertDialog alert
= builder
.create();
190 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
191 int i
= itemPosition
;
199 public void onBackPressed() {
201 if (mDirectories
.getCount() == 0) {
202 super.onBackPressed();
205 ((FileList
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
))
209 private class DirectoryCreator
implements Runnable
{
210 private String mTargetPath
;
211 private Account mAccount
;
212 private AccountManager mAm
;
214 public DirectoryCreator(String targetPath
, Account account
) {
215 mTargetPath
= targetPath
;
217 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
222 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(
223 mAccount
, AccountAuthenticator
.KEY_OC_URL
)));
225 String username
= mAccount
.name
.substring(0,
226 mAccount
.name
.lastIndexOf('@'));
227 String password
= mAm
.getPassword(mAccount
);
229 wdc
.setCredentials(username
, password
);
230 wdc
.allowUnsignedCertificates();
231 wdc
.createDirectory(mTargetPath
);
236 // Custom array adapter to override text colors
237 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
239 public CustomArrayAdapter(FileDisplayActivity ctx
,
244 public View
getView(int position
, View convertView
,
246 View v
= super.getView(position
, convertView
, parent
);
248 ((TextView
) v
).setTextColor(
250 .getColorStateList(android
.R
.color
.white
));
254 public View
getDropDownView(int position
, View convertView
,
256 View v
= super.getDropDownView(position
, convertView
,
259 ((TextView
) v
).setTextColor(getResources().getColorStateList(
260 android
.R
.color
.white
));