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