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
.Dialog
;
25 import android
.app
.AlertDialog
.Builder
;
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
.support
.v4
.app
.ActionBar
;
33 import android
.support
.v4
.app
.ActionBar
.OnNavigationListener
;
34 import android
.support
.v4
.view
.Menu
;
35 import android
.support
.v4
.view
.MenuItem
;
36 import android
.view
.MenuInflater
;
37 import android
.widget
.ArrayAdapter
;
38 import android
.widget
.EditText
;
39 import eu
.alefzero
.owncloud
.R
;
40 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
41 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
42 import eu
.alefzero
.owncloud
.ui
.fragment
.FileList
;
43 import eu
.alefzero
.webdav
.WebdavClient
;
46 * Displays, what files the user has available in his ownCloud.
47 * @author Bartek Przybylski
51 public class FileDisplayActivity
extends android
.support
.v4
.app
.FragmentActivity
implements OnNavigationListener
{
52 private ArrayAdapter
<String
> mDirectories
;
54 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
56 public void pushPath(String path
) {
57 mDirectories
.insert(path
, 0);
60 public boolean popPath() {
61 mDirectories
.remove(mDirectories
.getItem(0));
62 return !mDirectories
.isEmpty();
66 protected Dialog
onCreateDialog(int id
, Bundle args
) {
67 final AlertDialog
.Builder builder
= new Builder(this);
68 final EditText dirName
= new EditText(getBaseContext());
69 builder
.setView(dirName
);
70 builder
.setTitle(R
.string
.uploader_info_dirname
);
72 builder
.setPositiveButton(R
.string
.common_ok
, new OnClickListener() {
73 public void onClick(DialogInterface dialog
, int which
) {
74 String s
= dirName
.getText().toString();
75 if (s
.trim().isEmpty()) {
79 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
80 // following account choosing is incorrect and needs to be replaced
81 // with some sort of session mechanism
82 Account a
= am
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[0];
85 for (int i
= mDirectories
.getCount()-2; i
>= 0; --i
) {
86 path
+= "/" + mDirectories
.getItem(i
);
88 OCFile parent
= new OCFile(getContentResolver(), a
, path
+"/");
89 path
+= "/" + s
+ "/";
90 Thread thread
= new Thread(new DirectoryCreator(path
, a
, am
));
92 OCFile
.createNewFile(getContentResolver(), a
, path
, 0, 0, 0, "DIR", parent
.getFileId()).save();
97 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
98 public void onClick(DialogInterface dialog
, int which
) {
102 return builder
.create();
106 public void onCreate(Bundle savedInstanceState
) {
107 super.onCreate(savedInstanceState
);
108 mDirectories
= new ArrayAdapter
<String
>(this, android
.R
.layout
.simple_spinner_dropdown_item
);
109 mDirectories
.add("/");
110 setContentView(R
.layout
.files
);
111 ActionBar action_bar
= getSupportActionBar();
112 action_bar
.setNavigationMode(android
.support
.v4
.app
.ActionBar
.NAVIGATION_MODE_LIST
);
113 action_bar
.setDisplayShowTitleEnabled(false
);
114 action_bar
.setListNavigationCallbacks(mDirectories
, this);
118 public boolean onOptionsItemSelected(MenuItem item
) {
119 switch (item
.getItemId()) {
120 case R
.id
.settingsItem
:
122 Intent i
= new Intent(this, Preferences
.class);
126 case R
.id
.createDirectoryItem
:
136 protected Dialog
onCreateDialog(int id
) {
138 case DIALOG_CHOOSE_ACCOUNT
:
139 return createChooseAccountDialog();
141 throw new IllegalArgumentException("Unknown dialog id: " + id
);
146 public boolean onCreateOptionsMenu(Menu menu
) {
147 MenuInflater inflater
= getMenuInflater();
148 inflater
.inflate(R
.menu
.menu
, menu
);
152 private Dialog
createChooseAccountDialog() {
153 final AccountManager accMan
= AccountManager
.get(this);
154 CharSequence
[] items
= new CharSequence
[accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
156 for (Account a
: accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
160 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
161 builder
.setTitle(R
.string
.common_choose_account
);
162 builder
.setCancelable(true
);
163 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
164 public void onClick(DialogInterface dialog
, int item
) {
165 //mAccount = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
169 builder
.setOnCancelListener(new OnCancelListener() {
170 public void onCancel(DialogInterface dialog
) {
171 FileDisplayActivity
.this.finish();
174 AlertDialog alert
= builder
.create();
179 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
180 int i
= itemPosition
;
188 public void onBackPressed() {
190 if (mDirectories
.getCount() == 0)
192 super.onBackPressed();
195 ((FileList
)getSupportFragmentManager().findFragmentById(R
.id
.fileList
)).onBackPressed();
198 private class DirectoryCreator
implements Runnable
{
199 private String mTargetPath
;
200 private Account mAccount
;
201 private AccountManager mAm
;
203 public DirectoryCreator(String targetPath
, Account account
, AccountManager am
) {
204 mTargetPath
= targetPath
;
211 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(mAccount
,
212 AccountAuthenticator
.KEY_OC_URL
)));
214 String username
= mAccount
.name
.substring(0, mAccount
.name
.lastIndexOf('@'));
215 String password
= mAm
.getPassword(mAccount
);
217 wdc
.setCredentials(username
, password
);
218 wdc
.allowUnsignedCertificates();
219 wdc
.createDirectory(mTargetPath
);