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
.authenticator
.AuthUtils
;
42 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
43 import eu
.alefzero
.owncloud
.ui
.fragment
.FileList
;
44 import eu
.alefzero
.webdav
.WebdavClient
;
47 * Displays, what files the user has available in his ownCloud.
48 * @author Bartek Przybylski
52 public class FileDisplayActivity
extends android
.support
.v4
.app
.FragmentActivity
implements OnNavigationListener
{
53 private ArrayAdapter
<String
> mDirectories
;
55 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
57 public void pushPath(String path
) {
58 mDirectories
.insert(path
, 0);
61 public boolean popPath() {
62 mDirectories
.remove(mDirectories
.getItem(0));
63 return !mDirectories
.isEmpty();
67 protected Dialog
onCreateDialog(int id
, Bundle args
) {
68 final AlertDialog
.Builder builder
= new Builder(this);
69 final EditText dirName
= new EditText(getBaseContext());
70 final Account a
= AuthUtils
.getCurrentOwnCloudAccount(this);
71 builder
.setView(dirName
);
72 builder
.setTitle(R
.string
.uploader_info_dirname
);
74 builder
.setPositiveButton(R
.string
.common_ok
, new OnClickListener() {
75 public void onClick(DialogInterface dialog
, int which
) {
76 String s
= dirName
.getText().toString();
77 if (s
.trim().isEmpty()) {
83 for (int i
= mDirectories
.getCount()-2; i
>= 0; --i
) {
84 path
+= "/" + mDirectories
.getItem(i
);
86 OCFile parent
= new OCFile(getContentResolver(), a
, path
+"/");
87 path
+= "/" + s
+ "/";
88 Thread thread
= new Thread(new DirectoryCreator(path
, a
));
90 OCFile
.createNewFile(getContentResolver(), a
, path
, 0, 0, 0, "DIR", parent
.getFileId()).save();
95 builder
.setNegativeButton(R
.string
.common_cancel
, new OnClickListener() {
96 public void onClick(DialogInterface dialog
, int which
) {
100 return builder
.create();
104 public void onCreate(Bundle savedInstanceState
) {
105 super.onCreate(savedInstanceState
);
106 mDirectories
= new ArrayAdapter
<String
>(this, android
.R
.layout
.simple_spinner_dropdown_item
);
107 mDirectories
.add("/");
108 setContentView(R
.layout
.files
);
109 ActionBar action_bar
= getSupportActionBar();
110 action_bar
.setNavigationMode(android
.support
.v4
.app
.ActionBar
.NAVIGATION_MODE_LIST
);
111 action_bar
.setDisplayShowTitleEnabled(false
);
112 action_bar
.setListNavigationCallbacks(mDirectories
, this);
116 public boolean onOptionsItemSelected(MenuItem item
) {
117 switch (item
.getItemId()) {
118 case R
.id
.settingsItem
:
120 Intent i
= new Intent(this, Preferences
.class);
124 case R
.id
.createDirectoryItem
:
134 protected Dialog
onCreateDialog(int id
) {
136 case DIALOG_CHOOSE_ACCOUNT
:
137 return createChooseAccountDialog();
139 throw new IllegalArgumentException("Unknown dialog id: " + id
);
144 public boolean onCreateOptionsMenu(Menu menu
) {
145 MenuInflater inflater
= getMenuInflater();
146 inflater
.inflate(R
.menu
.menu
, menu
);
150 private Dialog
createChooseAccountDialog() {
151 final AccountManager accMan
= AccountManager
.get(this);
152 CharSequence
[] items
= new CharSequence
[accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
154 for (Account a
: accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
158 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
159 builder
.setTitle(R
.string
.common_choose_account
);
160 builder
.setCancelable(true
);
161 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
162 public void onClick(DialogInterface dialog
, int item
) {
163 //mAccount = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
167 builder
.setOnCancelListener(new OnCancelListener() {
168 public void onCancel(DialogInterface dialog
) {
169 FileDisplayActivity
.this.finish();
172 AlertDialog alert
= builder
.create();
177 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
178 int i
= itemPosition
;
186 public void onBackPressed() {
188 if (mDirectories
.getCount() == 0)
190 super.onBackPressed();
193 ((FileList
)getSupportFragmentManager().findFragmentById(R
.id
.fileList
)).onBackPressed();
196 private class DirectoryCreator
implements Runnable
{
197 private String mTargetPath
;
198 private Account mAccount
;
199 private AccountManager mAm
;
201 public DirectoryCreator(String targetPath
, Account account
) {
202 mTargetPath
= targetPath
;
204 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
209 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(mAccount
,
210 AccountAuthenticator
.KEY_OC_URL
)));
212 String username
= mAccount
.name
.substring(0, mAccount
.name
.lastIndexOf('@'));
213 String password
= mAm
.getPassword(mAccount
);
215 wdc
.setCredentials(username
, password
);
216 wdc
.allowUnsignedCertificates();
217 wdc
.createDirectory(mTargetPath
);