Force spaces in eclipse via project specifc settings
[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 java.io.File;
22 import java.net.URLEncoder;
23
24 import android.accounts.Account;
25 import android.accounts.AccountManager;
26 import android.app.AlertDialog;
27 import android.app.AlertDialog.Builder;
28 import android.app.Dialog;
29 import android.content.BroadcastReceiver;
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.content.DialogInterface;
33 import android.content.DialogInterface.OnClickListener;
34 import android.content.Intent;
35 import android.content.IntentFilter;
36 import android.database.Cursor;
37 import android.net.Uri;
38 import android.os.Bundle;
39 import android.provider.MediaStore;
40 import android.util.Log;
41 import android.view.View;
42 import android.view.ViewGroup;
43 import android.widget.ArrayAdapter;
44 import android.widget.EditText;
45 import android.widget.TextView;
46
47 import com.actionbarsherlock.app.ActionBar;
48 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
49 import com.actionbarsherlock.app.SherlockFragmentActivity;
50 import com.actionbarsherlock.view.Menu;
51 import com.actionbarsherlock.view.MenuInflater;
52 import com.actionbarsherlock.view.MenuItem;
53 import com.actionbarsherlock.view.Window;
54
55 import eu.alefzero.owncloud.AccountUtils;
56 import eu.alefzero.owncloud.R;
57 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
58 import eu.alefzero.owncloud.datamodel.DataStorageManager;
59 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
60 import eu.alefzero.owncloud.datamodel.OCFile;
61 import eu.alefzero.owncloud.files.services.FileUploader;
62 import eu.alefzero.owncloud.syncadapter.FileSyncService;
63 import eu.alefzero.owncloud.ui.fragment.FileListFragment;
64 import eu.alefzero.owncloud.utils.OwnCloudVersion;
65 import eu.alefzero.webdav.WebdavClient;
66
67 /**
68 * Displays, what files the user has available in his ownCloud.
69 *
70 * @author Bartek Przybylski
71 *
72 */
73
74 public class FileDisplayActivity extends SherlockFragmentActivity implements
75 OnNavigationListener, OnClickListener {
76 private ArrayAdapter<String> mDirectories;
77 private DataStorageManager mStorageManager;
78
79 private SyncBroadcastReceiver syncBroadcastRevceiver;
80
81 private static final int DIALOG_SETUP_ACCOUNT = 0;
82 private static final int DIALOG_CREATE_DIR = 1;
83
84 private static final int REQUEST_ACCOUNT_SETUP = 0;
85 private static final int ACTION_SELECT_FILE = 1;
86
87 public void pushPath(String path) {
88 mDirectories.insert(path, 0);
89 }
90
91 public boolean popPath() {
92 mDirectories.remove(mDirectories.getItem(0));
93 return !mDirectories.isEmpty();
94 }
95
96 @Override
97 protected Dialog onCreateDialog(int id) {
98 Dialog dialog;
99 AlertDialog.Builder builder;
100 switch (id) {
101 case DIALOG_SETUP_ACCOUNT:
102 builder = new AlertDialog.Builder(this);
103 builder.setTitle(R.string.main_tit_accsetup);
104 builder.setMessage(R.string.main_wrn_accsetup);
105 builder.setCancelable(false);
106 builder.setPositiveButton(android.R.string.ok, this);
107 builder.setNegativeButton(android.R.string.cancel, this);
108 dialog = builder.create();
109 break;
110 case DIALOG_CREATE_DIR: {
111 builder = new Builder(this);
112 final EditText dirName = new EditText(getBaseContext());
113 final Account a = AccountUtils.getCurrentOwnCloudAccount(this);
114 builder.setView(dirName);
115 builder.setTitle(R.string.uploader_info_dirname);
116 int typed_color = getResources().getColor(R.color.setup_text_typed);
117 dirName.setTextColor(typed_color);
118
119 builder.setPositiveButton(android.R.string.ok,
120 new OnClickListener() {
121 public void onClick(DialogInterface dialog, int which) {
122 String s = dirName.getText().toString();
123 if (s.trim().length() == 0) {
124 dialog.cancel();
125 return;
126 }
127
128 String path = "";
129 for (int i = mDirectories.getCount() - 2; i >= 0; --i) {
130 path += "/" + mDirectories.getItem(i);
131 }
132 OCFile parent = mStorageManager.getFileByPath(path
133 + "/");
134 path += s + "/";
135 Thread thread = new Thread(new DirectoryCreator(
136 path, a));
137 thread.start();
138
139 OCFile new_file = new OCFile(path);
140 new_file.setMimetype("DIR");
141 new_file.setParentId(parent.getParentId());
142 mStorageManager.saveFile(new_file);
143
144 dialog.dismiss();
145 }
146 });
147 builder.setNegativeButton(R.string.common_cancel,
148 new OnClickListener() {
149 public void onClick(DialogInterface dialog, int which) {
150 dialog.cancel();
151 }
152 });
153 dialog = builder.create();
154 break;
155 }
156 default:
157 dialog = null;
158 }
159
160 return dialog;
161 }
162
163 @Override
164 public void onCreate(Bundle savedInstanceState) {
165 super.onCreate(savedInstanceState);
166
167 if (!accountsAreSetup()) {
168 showDialog(DIALOG_SETUP_ACCOUNT);
169 return;
170 }
171
172 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
173 setProgressBarIndeterminateVisibility(false);
174 // if (getSupportFragmentManager().findFragmentById(R.id.fileList) ==
175 // null)
176 setContentView(R.layout.files);
177
178 }
179
180 @Override
181 public boolean onOptionsItemSelected(MenuItem item) {
182 boolean retval = true;
183 switch (item.getItemId()) {
184 case R.id.settingsItem: {
185 Intent i = new Intent(this, Preferences.class);
186 startActivity(i);
187 break;
188 }
189 case R.id.createDirectoryItem: {
190 showDialog(DIALOG_CREATE_DIR);
191 break;
192 }
193 case R.id.startSync: {
194 Bundle bundle = new Bundle();
195 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
196 ContentResolver.requestSync(
197 AccountUtils.getCurrentOwnCloudAccount(this),
198 "org.owncloud", bundle);
199 break;
200 }
201 case R.id.action_upload: {
202 Intent action = new Intent(Intent.ACTION_GET_CONTENT);
203 action = action.setType("*/*")
204 .addCategory(Intent.CATEGORY_OPENABLE);
205 startActivityForResult(
206 Intent.createChooser(action, "Upload file from..."),
207 ACTION_SELECT_FILE);
208 break;
209 }
210
211 case android.R.id.home: {
212 Intent i = new Intent(this, AccountSelectActivity.class);
213 startActivity(i);
214 finish();
215 break;
216 }
217 default:
218 retval = false;
219 }
220 return retval;
221 }
222
223 @Override
224 public void onBackPressed() {
225 if (mDirectories.getCount() == 1) {
226 finish();
227 return;
228 }
229 popPath();
230 ((FileListFragment) getSupportFragmentManager().findFragmentById(
231 R.id.fileList)).onNavigateUp();
232 }
233
234 @Override
235 public boolean onCreateOptionsMenu(Menu menu) {
236 MenuInflater inflater = getSherlock().getMenuInflater();
237 inflater.inflate(R.menu.menu, menu);
238 return true;
239 }
240
241 @Override
242 protected void onRestoreInstanceState(Bundle savedInstanceState) {
243 super.onRestoreInstanceState(savedInstanceState);
244 // Check, if there are ownCloud accounts
245 if (!accountsAreSetup()) {
246 showDialog(DIALOG_SETUP_ACCOUNT);
247 }
248 }
249
250 @Override
251 protected void onResume() {
252 super.onResume();
253 if (!accountsAreSetup()) {
254 showDialog(DIALOG_SETUP_ACCOUNT);
255 return;
256 }
257
258 IntentFilter f = new IntentFilter(FileSyncService.SYNC_MESSAGE);
259 syncBroadcastRevceiver = new SyncBroadcastReceiver();
260 registerReceiver(syncBroadcastRevceiver, f);
261
262 mDirectories = new CustomArrayAdapter<String>(this,
263 R.layout.sherlock_spinner_dropdown_item);
264 mDirectories.add("/");
265
266 mStorageManager = new FileDataStorageManager(
267 AccountUtils.getCurrentOwnCloudAccount(this),
268 getContentResolver());
269 ActionBar action_bar = getSupportActionBar();
270 action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
271 action_bar.setDisplayShowTitleEnabled(false);
272 action_bar.setListNavigationCallbacks(mDirectories, this);
273 action_bar.setDisplayHomeAsUpEnabled(true);
274 }
275
276 public void onActivityResult(int requestCode, int resultCode, Intent data) {
277 Log.e("ASD", requestCode + " " + resultCode);
278 if (resultCode == RESULT_OK) {
279 if (requestCode == ACTION_SELECT_FILE) {
280 Uri selectedImageUri = data.getData();
281
282 String filemanagerstring = selectedImageUri.getPath();
283 String selectedImagePath = getPath(selectedImageUri);
284 String filepath;
285
286 if (selectedImagePath != null)
287 filepath = selectedImagePath;
288 else
289 filepath = filemanagerstring;
290
291 if (filepath == null) {
292 Log.e("FileDisplay", "Couldnt resolve path to file");
293 return;
294 }
295
296 Intent i = new Intent(this, FileUploader.class);
297 i.putExtra(FileUploader.KEY_ACCOUNT,
298 AccountUtils.getCurrentOwnCloudAccount(this));
299 String remotepath = new String();
300 for (int j = mDirectories.getCount() - 2; j >= 0; --j) {
301 remotepath += "/" + mDirectories.getItem(j);
302 }
303 if (!remotepath.endsWith("/"))
304 remotepath += "/";
305 remotepath += new File(filepath).getName();
306 Log.e("ASD", remotepath + "");
307
308 i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath);
309 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath);
310 i.putExtra(FileUploader.KEY_UPLOAD_TYPE,
311 FileUploader.UPLOAD_SINGLE_FILE);
312 startService(i);
313 }
314 }
315 }
316
317 public String getPath(Uri uri) {
318 String[] projection = { MediaStore.Images.Media.DATA };
319 Cursor cursor = managedQuery(uri, projection, null, null, null);
320 if (cursor != null) {
321 int column_index = cursor
322 .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
323 cursor.moveToFirst();
324 return cursor.getString(column_index);
325 } else
326 return null;
327 }
328
329 @Override
330 protected void onPause() {
331 super.onPause();
332 if (syncBroadcastRevceiver != null) {
333 unregisterReceiver(syncBroadcastRevceiver);
334 syncBroadcastRevceiver = null;
335 }
336
337 }
338
339 @Override
340 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
341 int i = itemPosition;
342 while (i-- != 0) {
343 onBackPressed();
344 }
345 return true;
346 }
347
348 private class DirectoryCreator implements Runnable {
349 private String mTargetPath;
350 private Account mAccount;
351 private AccountManager mAm;
352
353 public DirectoryCreator(String targetPath, Account account) {
354 mTargetPath = targetPath;
355 mAccount = account;
356 mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);
357 }
358
359 @Override
360 public void run() {
361 WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(
362 mAccount, AccountAuthenticator.KEY_OC_URL)));
363
364 String username = mAccount.name.substring(0,
365 mAccount.name.lastIndexOf('@'));
366 String password = mAm.getPassword(mAccount);
367
368 wdc.setCredentials(username, password);
369 wdc.allowUnsignedCertificates();
370 wdc.createDirectory(mTargetPath);
371 }
372
373 }
374
375 // Custom array adapter to override text colors
376 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
377
378 public CustomArrayAdapter(FileDisplayActivity ctx, int view) {
379 super(ctx, view);
380 }
381
382 public View getView(int position, View convertView, ViewGroup parent) {
383 View v = super.getView(position, convertView, parent);
384
385 ((TextView) v).setTextColor(getResources().getColorStateList(
386 android.R.color.white));
387 return v;
388 }
389
390 public View getDropDownView(int position, View convertView,
391 ViewGroup parent) {
392 View v = super.getDropDownView(position, convertView, parent);
393
394 ((TextView) v).setTextColor(getResources().getColorStateList(
395 android.R.color.white));
396
397 return v;
398 }
399
400 }
401
402 public void onClick(DialogInterface dialog, int which) {
403 // In any case - we won't need it anymore
404 dialog.dismiss();
405 switch (which) {
406 case DialogInterface.BUTTON_POSITIVE:
407 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
408 intent.putExtra("authorities",
409 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
410 startActivity(intent);
411 break;
412 case DialogInterface.BUTTON_NEGATIVE:
413 finish();
414 }
415
416 }
417
418 /**
419 * Checks, whether or not there are any ownCloud accounts setup.
420 *
421 * @return true, if there is at least one account.
422 */
423 private boolean accountsAreSetup() {
424 AccountManager accMan = AccountManager.get(this);
425 Account[] accounts = accMan
426 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
427 return accounts.length > 0;
428 }
429
430 private class SyncBroadcastReceiver extends BroadcastReceiver {
431 /**
432 * {@link BroadcastReceiver} to enable syncing feedback in UI
433 */
434 @Override
435 public void onReceive(Context context, Intent intent) {
436 boolean inProgress = intent.getBooleanExtra(
437 FileSyncService.IN_PROGRESS, false);
438 String account_name = intent
439 .getStringExtra(FileSyncService.ACCOUNT_NAME);
440 Log.d("FileDisplay", "sync of account " + account_name
441 + " is in_progress: " + inProgress);
442 setProgressBarIndeterminateVisibility(inProgress);
443 if (!inProgress) {
444 FileListFragment fileListFramgent = (FileListFragment) getSupportFragmentManager()
445 .findFragmentById(R.id.fileList);
446 if (fileListFramgent != null)
447 fileListFramgent.populateFileList();
448 }
449 }
450
451 }
452
453 }