f0bc4f2c6692428c5c06dac07042c58c73c94017
[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.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;
37
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;
44
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;
51
52 /**
53 * Displays, what files the user has available in his ownCloud.
54 *
55 * @author Bartek Przybylski
56 *
57 */
58
59 public class FileDisplayActivity extends SherlockFragmentActivity implements
60 OnNavigationListener {
61 private ArrayAdapter<String> mDirectories;
62
63 private static final int DIALOG_CHOOSE_ACCOUNT = 0;
64
65 public void pushPath(String path) {
66 mDirectories.insert(path, 0);
67 }
68
69 public boolean popPath() {
70 mDirectories.remove(mDirectories.getItem(0));
71 return !mDirectories.isEmpty();
72 }
73
74 @Override
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);
81 dirName.setTextColor(R.color.setup_text_typed);
82
83 builder.setPositiveButton(R.string.common_ok, new OnClickListener() {
84 public void onClick(DialogInterface dialog, int which) {
85 String s = dirName.getText().toString();
86 if (s.trim().isEmpty()) {
87 dialog.cancel();
88 return;
89 }
90
91 String path = "";
92 for (int i = mDirectories.getCount() - 2; i >= 0; --i) {
93 path += "/" + mDirectories.getItem(i);
94 }
95 OCFile parent = new OCFile(getContentResolver(), a, path + "/");
96 path += "/" + s + "/";
97 Thread thread = new Thread(new DirectoryCreator(path, a));
98 thread.start();
99 OCFile.createNewFile(getContentResolver(), a, path, 0, 0, 0,
100 "DIR", parent.getFileId()).save();
101
102 dialog.dismiss();
103 }
104 });
105 builder.setNegativeButton(R.string.common_cancel,
106 new OnClickListener() {
107 public void onClick(DialogInterface dialog, int which) {
108 dialog.cancel();
109 }
110 });
111 return builder.create();
112 }
113
114 @Override
115 public void onCreate(Bundle savedInstanceState) {
116 super.onCreate(savedInstanceState);
117 mDirectories = new CustomArrayAdapter<String>(this,
118 R.layout.sherlock_spinner_dropdown_item);
119 mDirectories.add("/");
120 setContentView(R.layout.files);
121 ActionBar action_bar = getSupportActionBar();
122 action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
123 action_bar.setDisplayShowTitleEnabled(false);
124 action_bar.setListNavigationCallbacks(mDirectories, this);
125 action_bar.setDisplayHomeAsUpEnabled(true);
126 }
127
128 @Override
129 public boolean onOptionsItemSelected(MenuItem item) {
130 switch (item.getItemId()) {
131 case R.id.settingsItem: {
132 Intent i = new Intent(this, Preferences.class);
133 startActivity(i);
134 break;
135 }
136 case R.id.createDirectoryItem: {
137 showDialog(0);
138 break;
139 }
140 }
141 return true;
142 }
143
144 @Override
145 protected Dialog onCreateDialog(int id) {
146 switch (id) {
147 case DIALOG_CHOOSE_ACCOUNT:
148 return createChooseAccountDialog();
149 default:
150 throw new IllegalArgumentException("Unknown dialog id: " + id);
151 }
152 }
153
154 @Override
155 public boolean onCreateOptionsMenu(Menu menu) {
156 MenuInflater inflater = getSherlock().getMenuInflater();
157 inflater.inflate(R.menu.menu, menu);
158 return true;
159 }
160
161 private Dialog createChooseAccountDialog() {
162 final AccountManager accMan = AccountManager.get(this);
163 CharSequence[] items = new CharSequence[accMan
164 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
165 int i = 0;
166 for (Account a : accMan
167 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
168 items[i++] = a.name;
169 }
170
171 AlertDialog.Builder builder = new AlertDialog.Builder(this);
172 builder.setTitle(R.string.common_choose_account);
173 builder.setCancelable(true);
174 builder.setItems(items, new DialogInterface.OnClickListener() {
175 public void onClick(DialogInterface dialog, int item) {
176 // mAccount =
177 // accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
178 dialog.dismiss();
179 }
180 });
181 builder.setOnCancelListener(new OnCancelListener() {
182 public void onCancel(DialogInterface dialog) {
183 FileDisplayActivity.this.finish();
184 }
185 });
186 AlertDialog alert = builder.create();
187 return alert;
188 }
189
190 @Override
191 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
192 int i = itemPosition;
193 while (i-- != 0) {
194 onBackPressed();
195 }
196 return true;
197 }
198
199 @Override
200 public void onBackPressed() {
201 popPath();
202 if (mDirectories.getCount() == 0) {
203 super.onBackPressed();
204 return;
205 }
206 ((FileList) getSupportFragmentManager().findFragmentById(R.id.fileList))
207 .onBackPressed();
208 }
209
210 private class DirectoryCreator implements Runnable {
211 private String mTargetPath;
212 private Account mAccount;
213 private AccountManager mAm;
214
215 public DirectoryCreator(String targetPath, Account account) {
216 mTargetPath = targetPath;
217 mAccount = account;
218 mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);
219 }
220
221 @Override
222 public void run() {
223 WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(
224 mAccount, AccountAuthenticator.KEY_OC_URL)));
225
226 String username = mAccount.name.substring(0,
227 mAccount.name.lastIndexOf('@'));
228 String password = mAm.getPassword(mAccount);
229
230 wdc.setCredentials(username, password);
231 wdc.allowUnsignedCertificates();
232 wdc.createDirectory(mTargetPath);
233 }
234
235 }
236
237 // Custom array adapter to override text colors
238 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
239
240 public CustomArrayAdapter(FileDisplayActivity ctx,
241 int view) {
242 super(ctx, view);
243 }
244
245 public View getView(int position, View convertView,
246 ViewGroup parent) {
247 View v = super.getView(position, convertView, parent);
248
249 ((TextView) v).setTextColor(
250 getResources()
251 .getColorStateList(android.R.color.white));
252 return v;
253 }
254
255 public View getDropDownView(int position, View convertView,
256 ViewGroup parent) {
257 View v = super.getDropDownView(position, convertView,
258 parent);
259
260 ((TextView) v).setTextColor(getResources().getColorStateList(
261 android.R.color.white));
262
263 return v;
264 }
265
266
267
268 }
269 }