handle dir creation from filelist
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / FileDisplayActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.ui.activity;
20
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;
44
45 /**
46 * Displays, what files the user has available in his ownCloud.
47 * @author Bartek Przybylski
48 *
49 */
50
51 public class FileDisplayActivity extends android.support.v4.app.FragmentActivity implements OnNavigationListener {
52 private ArrayAdapter<String> mDirectories;
53
54 private static final int DIALOG_CHOOSE_ACCOUNT = 0;
55
56 public void pushPath(String path) {
57 mDirectories.insert(path, 0);
58 }
59
60 public boolean popPath() {
61 mDirectories.remove(mDirectories.getItem(0));
62 return !mDirectories.isEmpty();
63 }
64
65 @Override
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);
71
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()) {
76 dialog.cancel();
77 return;
78 }
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];
83
84 String path = "";
85 for (int i = mDirectories.getCount()-2; i >= 0; --i) {
86 path += "/" + mDirectories.getItem(i);
87 }
88 OCFile parent = new OCFile(getContentResolver(), a, path+"/");
89 path += "/" + s + "/";
90 Thread thread = new Thread(new DirectoryCreator(path, a, am));
91 thread.start();
92 OCFile.createNewFile(getContentResolver(), a, path, 0, 0, 0, "DIR", parent.getFileId()).save();
93
94 dialog.dismiss();
95 }
96 });
97 builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
98 public void onClick(DialogInterface dialog, int which) {
99 dialog.cancel();
100 }
101 });
102 return builder.create();
103 }
104
105 @Override
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);
115 }
116
117 @Override
118 public boolean onOptionsItemSelected(MenuItem item) {
119 switch (item.getItemId()) {
120 case R.id.settingsItem :
121 {
122 Intent i = new Intent(this, Preferences.class);
123 startActivity(i);
124 break;
125 }
126 case R.id.createDirectoryItem:
127 {
128 showDialog(0);
129 break;
130 }
131 }
132 return true;
133 }
134
135 @Override
136 protected Dialog onCreateDialog(int id) {
137 switch (id) {
138 case DIALOG_CHOOSE_ACCOUNT:
139 return createChooseAccountDialog();
140 default:
141 throw new IllegalArgumentException("Unknown dialog id: " + id);
142 }
143 }
144
145 @Override
146 public boolean onCreateOptionsMenu(Menu menu) {
147 MenuInflater inflater = getMenuInflater();
148 inflater.inflate(R.menu.menu, menu);
149 return true;
150 }
151
152 private Dialog createChooseAccountDialog() {
153 final AccountManager accMan = AccountManager.get(this);
154 CharSequence[] items = new CharSequence[accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
155 int i = 0;
156 for (Account a : accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
157 items[i++] = a.name;
158 }
159
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];
166 dialog.dismiss();
167 }
168 });
169 builder.setOnCancelListener(new OnCancelListener() {
170 public void onCancel(DialogInterface dialog) {
171 FileDisplayActivity.this.finish();
172 }
173 });
174 AlertDialog alert = builder.create();
175 return alert;
176 }
177
178 @Override
179 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
180 int i = itemPosition;
181 while (i-- != 0) {
182 onBackPressed();
183 }
184 return true;
185 }
186
187 @Override
188 public void onBackPressed() {
189 popPath();
190 if (mDirectories.getCount() == 0)
191 {
192 super.onBackPressed();
193 return;
194 }
195 ((FileList)getSupportFragmentManager().findFragmentById(R.id.fileList)).onBackPressed();
196 }
197
198 private class DirectoryCreator implements Runnable {
199 private String mTargetPath;
200 private Account mAccount;
201 private AccountManager mAm;
202
203 public DirectoryCreator(String targetPath, Account account, AccountManager am) {
204 mTargetPath = targetPath;
205 mAccount = account;
206 mAm = am;
207 }
208
209 @Override
210 public void run() {
211 WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(mAccount,
212 AccountAuthenticator.KEY_OC_URL)));
213
214 String username = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));
215 String password = mAm.getPassword(mAccount);
216
217 wdc.setCredentials(username, password);
218 wdc.allowUnsignedCertificates();
219 wdc.createDirectory(mTargetPath);
220 }
221
222 }
223 }