Changed navigation behavior again after some feedback ;-)
[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.FileListFragment;
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 case android.R.id.home: {
141 onBackPressed();
142 break;
143 }
144
145 }
146 return true;
147 }
148
149 @Override
150 public void onBackPressed(){
151 popPath();
152 if(mDirectories.getCount() == 0) {
153 Intent intent = new Intent(this, LandingActivity.class);
154 startActivity(intent);
155 return;
156 }
157 ((FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList))
158 .onNavigateUp();
159 }
160
161 @Override
162 protected Dialog onCreateDialog(int id) {
163 switch (id) {
164 case DIALOG_CHOOSE_ACCOUNT:
165 return createChooseAccountDialog();
166 default:
167 throw new IllegalArgumentException("Unknown dialog id: " + id);
168 }
169 }
170
171 @Override
172 public boolean onCreateOptionsMenu(Menu menu) {
173 MenuInflater inflater = getSherlock().getMenuInflater();
174 inflater.inflate(R.menu.menu, menu);
175 return true;
176 }
177
178 private Dialog createChooseAccountDialog() {
179 final AccountManager accMan = AccountManager.get(this);
180 CharSequence[] items = new CharSequence[accMan
181 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
182 int i = 0;
183 for (Account a : accMan
184 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
185 items[i++] = a.name;
186 }
187
188 AlertDialog.Builder builder = new AlertDialog.Builder(this);
189 builder.setTitle(R.string.common_choose_account);
190 builder.setCancelable(true);
191 builder.setItems(items, new DialogInterface.OnClickListener() {
192 public void onClick(DialogInterface dialog, int item) {
193 // mAccount =
194 // accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
195 dialog.dismiss();
196 }
197 });
198 builder.setOnCancelListener(new OnCancelListener() {
199 public void onCancel(DialogInterface dialog) {
200 FileDisplayActivity.this.finish();
201 }
202 });
203 AlertDialog alert = builder.create();
204 return alert;
205 }
206
207 @Override
208 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
209 int i = itemPosition;
210 while (i-- != 0) {
211 onBackPressed();
212 }
213 return true;
214 }
215
216 private class DirectoryCreator implements Runnable {
217 private String mTargetPath;
218 private Account mAccount;
219 private AccountManager mAm;
220
221 public DirectoryCreator(String targetPath, Account account) {
222 mTargetPath = targetPath;
223 mAccount = account;
224 mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);
225 }
226
227 @Override
228 public void run() {
229 WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(
230 mAccount, AccountAuthenticator.KEY_OC_URL)));
231
232 String username = mAccount.name.substring(0,
233 mAccount.name.lastIndexOf('@'));
234 String password = mAm.getPassword(mAccount);
235
236 wdc.setCredentials(username, password);
237 wdc.allowUnsignedCertificates();
238 wdc.createDirectory(mTargetPath);
239 }
240
241 }
242
243 // Custom array adapter to override text colors
244 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
245
246 public CustomArrayAdapter(FileDisplayActivity ctx,
247 int view) {
248 super(ctx, view);
249 }
250
251 public View getView(int position, View convertView,
252 ViewGroup parent) {
253 View v = super.getView(position, convertView, parent);
254
255 ((TextView) v).setTextColor(
256 getResources()
257 .getColorStateList(android.R.color.white));
258 return v;
259 }
260
261 public View getDropDownView(int position, View convertView,
262 ViewGroup parent) {
263 View v = super.getDropDownView(position, convertView,
264 parent);
265
266 ((TextView) v).setTextColor(getResources().getColorStateList(
267 android.R.color.white));
268
269 return v;
270 }
271
272
273
274 }
275 }