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