fae2b0fda2ee9596cb6e0ff9024e7bb4c399a771
[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.BufferedReader;
22 import java.io.File;
23 import java.io.InputStreamReader;
24 import java.lang.Thread.UncaughtExceptionHandler;
25 import java.net.URLEncoder;
26 import java.util.ArrayList;
27
28 import android.accounts.Account;
29 import android.accounts.AccountManager;
30 import android.app.AlertDialog;
31 import android.app.AlertDialog.Builder;
32 import android.app.Dialog;
33 import android.content.BroadcastReceiver;
34 import android.content.ContentResolver;
35 import android.content.Context;
36 import android.content.DialogInterface;
37 import android.content.DialogInterface.OnClickListener;
38 import android.content.Intent;
39 import android.content.IntentFilter;
40 import android.database.Cursor;
41 import android.net.Uri;
42 import android.os.Bundle;
43 import android.provider.MediaStore;
44 import android.telephony.TelephonyManager;
45 import android.util.Log;
46 import android.view.View;
47 import android.view.ViewGroup;
48 import android.widget.ArrayAdapter;
49 import android.widget.CheckedTextView;
50 import android.widget.EditText;
51 import android.widget.TextView;
52
53 import com.actionbarsherlock.app.ActionBar;
54 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
55 import com.actionbarsherlock.app.SherlockFragmentActivity;
56 import com.actionbarsherlock.view.Menu;
57 import com.actionbarsherlock.view.MenuInflater;
58 import com.actionbarsherlock.view.MenuItem;
59 import com.actionbarsherlock.view.Window;
60
61 import eu.alefzero.owncloud.AccountUtils;
62 import eu.alefzero.owncloud.CrashHandler;
63 import eu.alefzero.owncloud.R;
64 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
65 import eu.alefzero.owncloud.datamodel.DataStorageManager;
66 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
67 import eu.alefzero.owncloud.datamodel.OCFile;
68 import eu.alefzero.owncloud.files.services.FileUploader;
69 import eu.alefzero.owncloud.syncadapter.FileSyncService;
70 import eu.alefzero.owncloud.ui.fragment.FileDetailFragment;
71 import eu.alefzero.owncloud.ui.fragment.FileListFragment;
72 import eu.alefzero.webdav.WebdavClient;
73
74 /**
75 * Displays, what files the user has available in his ownCloud.
76 *
77 * @author Bartek Przybylski
78 *
79 */
80
81 public class FileDisplayActivity extends SherlockFragmentActivity implements
82 OnNavigationListener, OnClickListener, android.view.View.OnClickListener {
83 private ArrayAdapter<String> mDirectories;
84 private DataStorageManager mStorageManager;
85 private FileListFragment mFileList;
86 private OCFile mCurrentDir;
87 private String[] mDirs = null;
88
89 private SyncBroadcastReceiver syncBroadcastRevceiver;
90
91 private View mLayoutView = null;
92
93 private static final String KEY_DIR_ARRAY = "DIR_ARRAY";
94 private static final String KEY_CURRENT_DIR = "DIR";
95
96 private static final int DIALOG_SETUP_ACCOUNT = 0;
97 private static final int DIALOG_CREATE_DIR = 1;
98 private static final int ACTION_SELECT_FILE = 1;
99
100 @Override
101 public void onCreate(Bundle savedInstanceState) {
102 super.onCreate(savedInstanceState);
103
104 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
105 setProgressBarIndeterminateVisibility(false);
106
107 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));
108
109 if(savedInstanceState != null){
110 mCurrentDir = (OCFile) savedInstanceState.getParcelable(KEY_CURRENT_DIR);
111 }
112
113 if (findViewById(R.id.file_list_view) == null)
114 mLayoutView = getLayoutInflater().inflate(R.layout.files, null); // always inflate this at onCreate() ; just once!
115
116 //TODO: Dialog useless -> get rid of this
117 if (!accountsAreSetup()) {
118 setContentView(R.layout.no_account_available);
119 setProgressBarIndeterminateVisibility(false);
120 getSupportActionBar().setNavigationMode(ActionBar.DISPLAY_SHOW_TITLE);
121 findViewById(R.id.setup_account).setOnClickListener(this);
122
123 } else if (findViewById(R.id.file_list_view) == null) {
124 setContentView(mLayoutView);
125 }
126
127 }
128
129 @Override
130 public boolean onCreateOptionsMenu(Menu menu) {
131 MenuInflater inflater = getSherlock().getMenuInflater();
132 inflater.inflate(R.menu.menu, menu);
133 return true;
134 }
135
136 @Override
137 public boolean onOptionsItemSelected(MenuItem item) {
138 boolean retval = true;
139 switch (item.getItemId()) {
140 case R.id.createDirectoryItem: {
141 showDialog(DIALOG_CREATE_DIR);
142 break;
143 }
144 case R.id.startSync: {
145 Bundle bundle = new Bundle();
146 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
147 ContentResolver.requestSync(
148 AccountUtils.getCurrentOwnCloudAccount(this),
149 "org.owncloud", bundle);
150 break;
151 }
152 case R.id.action_upload: {
153 Intent action = new Intent(Intent.ACTION_GET_CONTENT);
154 action = action.setType("*/*")
155 .addCategory(Intent.CATEGORY_OPENABLE);
156 startActivityForResult(
157 Intent.createChooser(action, "Upload file from..."),
158 ACTION_SELECT_FILE);
159 break;
160 }
161 case R.id.action_settings: {
162 Intent settingsIntent = new Intent(this, Preferences.class);
163 startActivity(settingsIntent);
164 }
165 case android.R.id.home: {
166 if(mCurrentDir != null && mCurrentDir.getParentId() != 0){
167 onBackPressed();
168 }
169 break;
170 }
171 default:
172 retval = false;
173 }
174 return retval;
175 }
176
177 @Override
178 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
179 int i = itemPosition;
180 while (i-- != 0) {
181 onBackPressed();
182 }
183 return true;
184 }
185
186 /**
187 * Called, when the user selected something for uploading
188 */
189 public void onActivityResult(int requestCode, int resultCode, Intent data) {
190 if (resultCode == RESULT_OK) {
191 if (requestCode == ACTION_SELECT_FILE) {
192 Uri selectedImageUri = data.getData();
193
194 String filemanagerstring = selectedImageUri.getPath();
195 String selectedImagePath = getPath(selectedImageUri);
196 String filepath;
197
198 if (selectedImagePath != null)
199 filepath = selectedImagePath;
200 else
201 filepath = filemanagerstring;
202
203 if (filepath == null) {
204 Log.e("FileDisplay", "Couldnt resolve path to file");
205 return;
206 }
207
208 Intent i = new Intent(this, FileUploader.class);
209 i.putExtra(FileUploader.KEY_ACCOUNT,
210 AccountUtils.getCurrentOwnCloudAccount(this));
211 String remotepath = new String();
212 for (int j = mDirectories.getCount() - 2; j >= 0; --j) {
213 remotepath += "/" + URLEncoder.encode(mDirectories.getItem(j));
214 }
215 if (!remotepath.endsWith("/"))
216 remotepath += "/";
217 remotepath += URLEncoder.encode(new File(filepath).getName());
218
219 i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath);
220 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath);
221 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
222 startService(i);
223 }
224 }
225 }
226
227 @Override
228 public void onBackPressed() {
229 if (mDirectories == null || mDirectories.getCount() <= 1) {
230 finish();
231 return;
232 }
233 popDirname();
234 mFileList.onNavigateUp();
235 mCurrentDir = mFileList.getCurrentFile();
236
237 if(mCurrentDir.getParentId() == 0){
238 ActionBar actionBar = getSupportActionBar();
239 actionBar.setDisplayHomeAsUpEnabled(false);
240 }
241 }
242
243 @Override
244 protected void onRestoreInstanceState(Bundle savedInstanceState) {
245 super.onRestoreInstanceState(savedInstanceState);
246 mDirs = savedInstanceState.getStringArray(KEY_DIR_ARRAY);
247 mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
248 mDirectories.add("/");
249 if (mDirs != null)
250 for (String s : mDirs)
251 mDirectories.insert(s, 0);
252 mCurrentDir = savedInstanceState.getParcelable(KEY_CURRENT_DIR);
253 }
254
255 @Override
256 protected void onSaveInstanceState(Bundle outState) {
257 super.onSaveInstanceState(outState);
258 if(mDirectories != null && mDirectories.getCount() != 0){
259 mDirs = new String[mDirectories.getCount()-1];
260 for (int j = mDirectories.getCount() - 2, i = 0; j >= 0; --j, ++i) {
261 mDirs[i] = mDirectories.getItem(j);
262 }
263 }
264 outState.putStringArray(KEY_DIR_ARRAY, mDirs);
265 outState.putParcelable(KEY_CURRENT_DIR, mCurrentDir);
266 }
267
268 @Override
269 protected void onResume() {
270 super.onResume();
271
272 if (accountsAreSetup()) {
273
274 setContentView(mLayoutView); // this should solve the crash by repeated inflating in big screens (DROIDCLOUD-27)
275
276 // Listen for sync messages
277 IntentFilter syncIntentFilter = new IntentFilter(FileSyncService.SYNC_MESSAGE);
278 syncBroadcastRevceiver = new SyncBroadcastReceiver();
279 registerReceiver(syncBroadcastRevceiver, syncIntentFilter);
280
281 // Storage manager initialization
282 mStorageManager = new FileDataStorageManager(
283 AccountUtils.getCurrentOwnCloudAccount(this),
284 getContentResolver());
285
286 // File list
287 mFileList = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
288
289 // Figure out what directory to list.
290 // Priority: Intent (here), savedInstanceState (onCreate), root dir (dir is null)
291 if(getIntent().hasExtra(FileDetailFragment.EXTRA_FILE)){
292 mCurrentDir = (OCFile) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
293 if(!mCurrentDir.isDirectory()){
294 mCurrentDir = mStorageManager.getFileById(mCurrentDir.getParentId());
295 }
296
297 // Clear intent extra, so rotating the screen will not return us to this directory
298 getIntent().removeExtra(FileDetailFragment.EXTRA_FILE);
299 } else {
300 mCurrentDir = mStorageManager.getFileByPath("/");
301 }
302
303 // Drop-Down navigation and file list restore
304 mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
305
306
307 // Given the case we have a file to display:
308 if(mCurrentDir != null){
309 ArrayList<OCFile> files = new ArrayList<OCFile>();
310 OCFile currFile = mCurrentDir;
311 while(currFile != null){
312 files.add(currFile);
313 currFile = mStorageManager.getFileById(currFile.getParentId());
314 }
315
316 // Insert in mDirs
317 mDirs = new String[files.size()];
318 for(int i = files.size() - 1; i >= 0; i--){
319 mDirs[i] = files.get(i).getFileName();
320 }
321 }
322
323 if (mDirs != null) {
324 for (String s : mDirs)
325 mDirectories.add(s);
326 } else {
327 mDirectories.add("/");
328 }
329
330 // Actionbar setup
331 ActionBar action_bar = getSupportActionBar();
332 action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
333 action_bar.setDisplayShowTitleEnabled(false);
334 action_bar.setListNavigationCallbacks(mDirectories, this);
335 if(mCurrentDir != null && mCurrentDir.getParentId() != 0){
336 action_bar.setDisplayHomeAsUpEnabled(true);
337 } else {
338 action_bar.setDisplayHomeAsUpEnabled(false);
339 }
340
341 // List dir here
342 mFileList.listDirectory(mCurrentDir);
343 }
344 }
345
346 @Override
347 protected void onPause() {
348 super.onPause();
349 if (syncBroadcastRevceiver != null) {
350 unregisterReceiver(syncBroadcastRevceiver);
351 syncBroadcastRevceiver = null;
352 }
353 }
354
355 @Override
356 protected Dialog onCreateDialog(int id) {
357 Dialog dialog;
358 AlertDialog.Builder builder;
359 switch (id) {
360 case DIALOG_SETUP_ACCOUNT:
361 builder = new AlertDialog.Builder(this);
362 builder.setTitle(R.string.main_tit_accsetup);
363 builder.setMessage(R.string.main_wrn_accsetup);
364 builder.setCancelable(false);
365 builder.setPositiveButton(android.R.string.ok, this);
366 builder.setNegativeButton(android.R.string.cancel, this);
367 dialog = builder.create();
368 break;
369 case DIALOG_CREATE_DIR: {
370 builder = new Builder(this);
371 final EditText dirNameInput = new EditText(getBaseContext());
372 final Account a = AccountUtils.getCurrentOwnCloudAccount(this);
373 builder.setView(dirNameInput);
374 builder.setTitle(R.string.uploader_info_dirname);
375 int typed_color = getResources().getColor(R.color.setup_text_typed);
376 dirNameInput.setTextColor(typed_color);
377 builder.setPositiveButton(android.R.string.ok,
378 new OnClickListener() {
379 public void onClick(DialogInterface dialog, int which) {
380 String directoryName = dirNameInput.getText().toString();
381 if (directoryName.trim().length() == 0) {
382 dialog.cancel();
383 return;
384 }
385
386 // Figure out the path where the dir needs to be created
387 String path = FileDisplayActivity.this.mCurrentDir.getRemotePath();
388
389 // Create directory
390 path += directoryName + "/";
391 Thread thread = new Thread(new DirectoryCreator(path, a));
392 thread.start();
393
394 // Save new directory in local database
395 OCFile newDir = new OCFile(path);
396 newDir.setMimetype("DIR");
397 newDir.setParentId(mCurrentDir.getFileId());
398 mStorageManager.saveFile(newDir);
399
400 // Display the new folder right away
401 dialog.dismiss();
402 mFileList.listDirectory(mCurrentDir);
403 }
404 });
405 builder.setNegativeButton(R.string.common_cancel,
406 new OnClickListener() {
407 public void onClick(DialogInterface dialog, int which) {
408 dialog.cancel();
409 }
410 });
411 dialog = builder.create();
412 break;
413 }
414 default:
415 dialog = null;
416 }
417
418 return dialog;
419 }
420
421
422 /**
423 * Responds to the "There are no ownCloud Accounts setup" dialog
424 * TODO: Dialog is 100% useless -> Remove
425 */
426 @Override
427 public void onClick(DialogInterface dialog, int which) {
428 // In any case - we won't need it anymore
429 dialog.dismiss();
430 switch (which) {
431 case DialogInterface.BUTTON_POSITIVE:
432 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
433 intent.putExtra("authorities",
434 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
435 startActivity(intent);
436 break;
437 case DialogInterface.BUTTON_NEGATIVE:
438 finish();
439 }
440
441 }
442
443 /**
444 * Translates a content URI of an image to a physical path
445 * on the disk
446 * @param uri The URI to resolve
447 * @return The path to the image or null if it could not be found
448 */
449 public String getPath(Uri uri) {
450 String[] projection = { MediaStore.Images.Media.DATA };
451 Cursor cursor = managedQuery(uri, projection, null, null, null);
452 if (cursor != null) {
453 int column_index = cursor
454 .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
455 cursor.moveToFirst();
456 return cursor.getString(column_index);
457 }
458 return null;
459 }
460
461 /**
462 * Pushes a directory to the drop down list
463 * @param directory to push
464 * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.
465 */
466 public void pushDirname(OCFile directory) {
467 if(!directory.isDirectory()){
468 throw new IllegalArgumentException("Only directories may be pushed!");
469 }
470 mDirectories.insert(directory.getFileName(), 0);
471 mCurrentDir = directory;
472 }
473
474 /**
475 * Pops a directory name from the drop down list
476 * @return True, unless the stack is empty
477 */
478 public boolean popDirname() {
479 mDirectories.remove(mDirectories.getItem(0));
480 return !mDirectories.isEmpty();
481 }
482
483 /**
484 * Checks, whether or not there are any ownCloud accounts setup.
485 *
486 * @return true, if there is at least one account.
487 */
488 private boolean accountsAreSetup() {
489 AccountManager accMan = AccountManager.get(this);
490 Account[] accounts = accMan
491 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
492 return accounts.length > 0;
493 }
494
495 private class DirectoryCreator implements Runnable {
496 private String mTargetPath;
497 private Account mAccount;
498 private AccountManager mAm;
499
500 public DirectoryCreator(String targetPath, Account account) {
501 mTargetPath = targetPath;
502 mAccount = account;
503 mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);
504 }
505
506 @Override
507 public void run() {
508 WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(
509 mAccount, AccountAuthenticator.KEY_OC_URL)));
510
511 String username = mAccount.name.substring(0,
512 mAccount.name.lastIndexOf('@'));
513 String password = mAm.getPassword(mAccount);
514
515 wdc.setCredentials(username, password);
516 wdc.allowSelfsignedCertificates();
517 wdc.createDirectory(mTargetPath);
518 }
519
520 }
521
522 // Custom array adapter to override text colors
523 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
524
525 public CustomArrayAdapter(FileDisplayActivity ctx, int view) {
526 super(ctx, view);
527 }
528
529 public View getView(int position, View convertView, ViewGroup parent) {
530 View v = super.getView(position, convertView, parent);
531
532 ((TextView) v).setTextColor(getResources().getColorStateList(
533 android.R.color.white));
534 return v;
535 }
536
537 public View getDropDownView(int position, View convertView,
538 ViewGroup parent) {
539 View v = super.getDropDownView(position, convertView, parent);
540
541 ((TextView) v).setTextColor(getResources().getColorStateList(
542 android.R.color.white));
543
544 return v;
545 }
546
547 }
548
549 private class SyncBroadcastReceiver extends BroadcastReceiver {
550 /**
551 * {@link BroadcastReceiver} to enable syncing feedback in UI
552 */
553 @Override
554 public void onReceive(Context context, Intent intent) {
555 boolean inProgress = intent.getBooleanExtra(
556 FileSyncService.IN_PROGRESS, false);
557 String account_name = intent
558 .getStringExtra(FileSyncService.ACCOUNT_NAME);
559 Log.d("FileDisplay", "sync of account " + account_name
560 + " is in_progress: " + inProgress);
561 setProgressBarIndeterminateVisibility(inProgress);
562 if (!inProgress) {
563 FileListFragment fileListFramgent = (FileListFragment) getSupportFragmentManager()
564 .findFragmentById(R.id.fileList);
565 if (fileListFramgent != null)
566 fileListFramgent.listDirectory();
567 }
568 }
569
570 }
571
572 @Override
573 public void onClick(View v) {
574 if (v.getId() == R.id.setup_account) {
575 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
576 intent.putExtra("authorities", new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
577 startActivity(intent);
578 }
579 }
580
581 public DataStorageManager getStorageManager() {
582 return mStorageManager;
583 }
584 }