cac6fea55767ddade3490c86bff565e4a7278515
[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.AccountUtils;
46 import eu.alefzero.owncloud.R;
47 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
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, OnClickListener {
63 private ArrayAdapter<String> mDirectories;
64 private DataStorageManager mStorageManager;
65
66 private static final int DIALOG_SETUP_ACCOUNT = 0;
67 private static final int DIALOG_CREATE_DIR = 1;
68
69 public void pushPath(String path) {
70 mDirectories.insert(path, 0);
71 }
72
73 public boolean popPath() {
74 mDirectories.remove(mDirectories.getItem(0));
75 return !mDirectories.isEmpty();
76 }
77
78 @Override
79 protected Dialog onCreateDialog(int id) {
80 Dialog dialog;
81 AlertDialog.Builder builder;
82 switch(id){
83 case DIALOG_SETUP_ACCOUNT:
84 builder = new AlertDialog.Builder(this);
85 builder.setTitle(R.string.main_tit_accsetup);
86 builder.setMessage(R.string.main_wrn_accsetup);
87 builder.setCancelable(false);
88 builder.setPositiveButton(R.string.common_ok, this);
89 builder.setNegativeButton(R.string.common_cancel, this);
90 dialog = builder.create();
91 break;
92 case DIALOG_CREATE_DIR:
93 {
94 builder = new Builder(this);
95 final EditText dirName = new EditText(getBaseContext());
96 final Account a = AccountUtils.getCurrentOwnCloudAccount(this);
97 builder.setView(dirName);
98 builder.setTitle(R.string.uploader_info_dirname);
99 dirName.setTextColor(R.color.setup_text_typed);
100
101 builder.setPositiveButton(R.string.common_ok, new OnClickListener() {
102 public void onClick(DialogInterface dialog, int which) {
103 String s = dirName.getText().toString();
104 if (s.trim().isEmpty()) {
105 dialog.cancel();
106 return;
107 }
108
109 String path = "";
110 for (int i = mDirectories.getCount() - 2; i >= 0; --i) {
111 path += "/" + mDirectories.getItem(i);
112 }
113 OCFile parent = mStorageManager.getFileByPath(path + "/");
114 path += s + "/";
115 Thread thread = new Thread(new DirectoryCreator(path, a));
116 thread.start();
117
118 OCFile new_file = new OCFile(path);
119 new_file.setMimetype("DIR");
120 new_file.setParentId(parent.getParentId());
121 mStorageManager.saveFile(new_file);
122
123 dialog.dismiss();
124 }
125 });
126 builder.setNegativeButton(R.string.common_cancel,
127 new OnClickListener() {
128 public void onClick(DialogInterface dialog, int which) {
129 dialog.cancel();
130 }
131 });
132 }
133 default:
134 dialog = null;
135 }
136
137 return dialog;
138 }
139
140 @Override
141 public void onCreate(Bundle savedInstanceState) {
142 super.onCreate(savedInstanceState);
143 if(!accountsAreSetup()){
144 showDialog(DIALOG_SETUP_ACCOUNT);
145 return;
146 }
147
148 mDirectories = new CustomArrayAdapter<String>(this,
149 R.layout.sherlock_spinner_dropdown_item);
150 mDirectories.add("/");
151 setContentView(R.layout.files);
152 mStorageManager = new FileDataStorageManager(AccountUtils.getCurrentOwnCloudAccount(this), getContentResolver());
153 ActionBar action_bar = getSupportActionBar();
154 action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
155 action_bar.setDisplayShowTitleEnabled(false);
156 action_bar.setListNavigationCallbacks(mDirectories, this);
157 action_bar.setDisplayHomeAsUpEnabled(true);
158 }
159
160 @Override
161 public boolean onOptionsItemSelected(MenuItem item) {
162 switch (item.getItemId()) {
163 case R.id.settingsItem: {
164 Intent i = new Intent(this, Preferences.class);
165 startActivity(i);
166 break;
167 }
168 case R.id.createDirectoryItem: {
169 showDialog(DIALOG_CREATE_DIR);
170 break;
171 }
172 case android.R.id.home: {
173 onBackPressed();
174 break;
175 }
176
177 }
178 return true;
179 }
180
181 @Override
182 public void onBackPressed(){
183 if(mDirectories.getCount() == 1) {
184 finish();
185 return;
186 }
187 popPath();
188 ((FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList))
189 .onNavigateUp();
190 }
191
192 @Override
193 public boolean onCreateOptionsMenu(Menu menu) {
194 MenuInflater inflater = getSherlock().getMenuInflater();
195 inflater.inflate(R.menu.menu, menu);
196 return true;
197 }
198
199 @Override
200 protected void onRestoreInstanceState(Bundle savedInstanceState) {
201 super.onRestoreInstanceState(savedInstanceState);
202 // Check, if there are ownCloud accounts
203 if(!accountsAreSetup()){
204 showDialog(DIALOG_SETUP_ACCOUNT);
205 }
206 }
207
208 @Override
209 protected void onStart() {
210 super.onStart();
211 // Check, if there are ownCloud accounts
212 if(!accountsAreSetup()){
213 showDialog(DIALOG_SETUP_ACCOUNT);
214 }
215 }
216
217 @Override
218 protected void onResume() {
219 super.onResume();
220 if(!accountsAreSetup()){
221 showDialog(DIALOG_SETUP_ACCOUNT);
222 }
223 }
224
225 @Override
226 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
227 int i = itemPosition;
228 while (i-- != 0) {
229 onBackPressed();
230 }
231 return true;
232 }
233
234 private class DirectoryCreator implements Runnable {
235 private String mTargetPath;
236 private Account mAccount;
237 private AccountManager mAm;
238
239 public DirectoryCreator(String targetPath, Account account) {
240 mTargetPath = targetPath;
241 mAccount = account;
242 mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);
243 }
244
245 @Override
246 public void run() {
247 WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(
248 mAccount, AccountAuthenticator.KEY_OC_URL)));
249
250 String username = mAccount.name.substring(0,
251 mAccount.name.lastIndexOf('@'));
252 String password = mAm.getPassword(mAccount);
253
254 wdc.setCredentials(username, password);
255 wdc.allowUnsignedCertificates();
256 wdc.createDirectory(mTargetPath);
257 }
258
259 }
260
261 // Custom array adapter to override text colors
262 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
263
264 public CustomArrayAdapter(FileDisplayActivity ctx,
265 int view) {
266 super(ctx, view);
267 }
268
269 public View getView(int position, View convertView,
270 ViewGroup parent) {
271 View v = super.getView(position, convertView, parent);
272
273 ((TextView) v).setTextColor(
274 getResources()
275 .getColorStateList(android.R.color.white));
276 return v;
277 }
278
279 public View getDropDownView(int position, View convertView,
280 ViewGroup parent) {
281 View v = super.getDropDownView(position, convertView,
282 parent);
283
284 ((TextView) v).setTextColor(getResources().getColorStateList(
285 android.R.color.white));
286
287 return v;
288 }
289
290
291
292 }
293
294 public void onClick(DialogInterface dialog, int which) {
295 // In any case - we won't need it anymore
296 dialog.dismiss();
297 switch(which){
298 case DialogInterface.BUTTON_POSITIVE:
299 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
300 intent.putExtra("authorities",
301 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
302 startActivity(intent);
303 break;
304 case DialogInterface.BUTTON_NEGATIVE:
305 finish();
306 }
307
308 }
309
310 /**
311 * Checks, whether or not there are any ownCloud accounts
312 * setup.
313 *
314 * @return true, if there is at least one account.
315 */
316 private boolean accountsAreSetup() {
317 AccountManager accMan = AccountManager.get(this);
318 Account[] accounts = accMan
319 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
320 return accounts.length > 0;
321 }
322
323
324 }