188d1f0b0f5419365313b99262d3d72de0b31f48
[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.util.ArrayList;
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.content.pm.PackageInfo;
37 import android.content.pm.PackageManager.NameNotFoundException;
38 import android.database.Cursor;
39 import android.net.Uri;
40 import android.os.Bundle;
41 import android.provider.MediaStore;
42 import android.support.v4.app.FragmentTransaction;
43 import android.util.Log;
44 import android.view.View;
45 import android.view.ViewGroup;
46 import android.widget.ArrayAdapter;
47 import android.widget.EditText;
48 import android.widget.TextView;
49
50 import com.actionbarsherlock.app.ActionBar;
51 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
52 import com.actionbarsherlock.app.SherlockFragmentActivity;
53 import com.actionbarsherlock.view.Menu;
54 import com.actionbarsherlock.view.MenuInflater;
55 import com.actionbarsherlock.view.MenuItem;
56 import com.actionbarsherlock.view.Window;
57
58 import eu.alefzero.owncloud.AccountUtils;
59 import eu.alefzero.owncloud.CrashHandler;
60 import eu.alefzero.owncloud.R;
61 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
62 import eu.alefzero.owncloud.datamodel.DataStorageManager;
63 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
64 import eu.alefzero.owncloud.datamodel.OCFile;
65 import eu.alefzero.owncloud.files.services.FileDownloader;
66 import eu.alefzero.owncloud.files.services.FileUploader;
67 import eu.alefzero.owncloud.syncadapter.FileSyncService;
68 import eu.alefzero.owncloud.ui.fragment.FileDetailFragment;
69 import eu.alefzero.owncloud.ui.fragment.FileListFragment;
70 import eu.alefzero.webdav.WebdavClient;
71
72 /**
73 * Displays, what files the user has available in his ownCloud.
74 *
75 * @author Bartek Przybylski
76 *
77 */
78
79 public class FileDisplayActivity extends SherlockFragmentActivity implements
80 FileListFragment.ContainerActivity, OnNavigationListener, OnClickListener, android.view.View.OnClickListener {
81
82 private ArrayAdapter<String> mDirectories;
83 private OCFile mCurrentDir;
84 private String[] mDirs = null;
85
86 private DataStorageManager mStorageManager;
87 private SyncBroadcastReceiver mSyncBroadcastReceiver;
88 private UploadFinishReceiver mUploadFinishReceiver;
89
90 private View mLayoutView = null;
91 private FileListFragment mFileList;
92
93 private boolean mDualPane;
94
95 private boolean mForcedLoginToCreateFirstAccount = false;
96
97 private static final String KEY_DIR_ARRAY = "DIR_ARRAY";
98 private static final String KEY_CURRENT_DIR = "DIR";
99
100 private static final int DIALOG_SETUP_ACCOUNT = 0;
101 private static final int DIALOG_CREATE_DIR = 1;
102 private static final int DIALOG_ABOUT_APP = 2;
103
104 private static final int ACTION_SELECT_FILE = 1;
105
106 @Override
107 public void onCreate(Bundle savedInstanceState) {
108 Log.i(getClass().toString(), "onCreate() start");
109 super.onCreate(savedInstanceState);
110
111 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
112 setSupportProgressBarIndeterminateVisibility(false);
113
114 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));
115
116 if(savedInstanceState != null) {
117 mDirs = savedInstanceState.getStringArray(KEY_DIR_ARRAY);
118 mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
119 mDirectories.add("/");
120 if (mDirs != null)
121 for (String s : mDirs)
122 mDirectories.insert(s, 0);
123 mCurrentDir = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE);
124 }
125
126 mLayoutView = getLayoutInflater().inflate(R.layout.files, null); // always inflate this at onCreate() ; just once!
127
128 if (AccountUtils.accountsAreSetup(this)) {
129
130 initDelayedTilAccountAvailabe();
131
132 } else {
133
134 setContentView(R.layout.no_account_available);
135 getSupportActionBar().setNavigationMode(ActionBar.DISPLAY_SHOW_TITLE);
136 findViewById(R.id.setup_account).setOnClickListener(this);
137
138 Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);
139 intent.putExtra(android.provider.Settings.EXTRA_AUTHORITIES, new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
140 startActivity(intent); // although the code is here, the activity won't be created until this.onStart() and this.onResume() are finished;
141 mForcedLoginToCreateFirstAccount = true;
142 }
143
144 Log.i(getClass().toString(), "onCreate() end");
145 }
146
147 @Override
148 public boolean onCreateOptionsMenu(Menu menu) {
149 MenuInflater inflater = getSherlock().getMenuInflater();
150 inflater.inflate(R.menu.menu, menu);
151 return true;
152 }
153
154 @Override
155 public boolean onOptionsItemSelected(MenuItem item) {
156 boolean retval = true;
157 switch (item.getItemId()) {
158 case R.id.createDirectoryItem: {
159 showDialog(DIALOG_CREATE_DIR);
160 break;
161 }
162 case R.id.startSync: {
163 Bundle bundle = new Bundle();
164 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
165 ContentResolver.requestSync(
166 AccountUtils.getCurrentOwnCloudAccount(this),
167 "org.owncloud", bundle);
168 break;
169 }
170 case R.id.action_upload: {
171 Intent action = new Intent(Intent.ACTION_GET_CONTENT);
172 action = action.setType("*/*")
173 .addCategory(Intent.CATEGORY_OPENABLE);
174 startActivityForResult(
175 Intent.createChooser(action, "Upload file from..."),
176 ACTION_SELECT_FILE);
177 break;
178 }
179 case R.id.action_settings: {
180 Intent settingsIntent = new Intent(this, Preferences.class);
181 startActivity(settingsIntent);
182 break;
183 }
184 case R.id.about_app : {
185 showDialog(DIALOG_ABOUT_APP);
186 break;
187 }
188 case android.R.id.home: {
189 if(mCurrentDir != null && mCurrentDir.getParentId() != 0){
190 onBackPressed();
191 }
192 break;
193 }
194 default:
195 retval = false;
196 }
197 return retval;
198 }
199
200 @Override
201 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
202 int i = itemPosition;
203 while (i-- != 0) {
204 onBackPressed();
205 }
206 return true;
207 }
208
209 /**
210 * Called, when the user selected something for uploading
211 */
212 public void onActivityResult(int requestCode, int resultCode, Intent data) {
213 if (requestCode == ACTION_SELECT_FILE) {
214 if (resultCode == RESULT_OK) {
215 Uri selectedImageUri = data.getData();
216
217 String filemanagerstring = selectedImageUri.getPath();
218 String selectedImagePath = getPath(selectedImageUri);
219 String filepath;
220
221 if (selectedImagePath != null)
222 filepath = selectedImagePath;
223 else
224 filepath = filemanagerstring;
225
226 if (filepath == null) {
227 Log.e("FileDisplay", "Couldnt resolve path to file");
228 return;
229 }
230
231 Intent i = new Intent(this, FileUploader.class);
232 i.putExtra(FileUploader.KEY_ACCOUNT,
233 AccountUtils.getCurrentOwnCloudAccount(this));
234 String remotepath = new String();
235 for (int j = mDirectories.getCount() - 2; j >= 0; --j) {
236 remotepath += "/" + mDirectories.getItem(j);
237 }
238 if (!remotepath.endsWith("/"))
239 remotepath += "/";
240 remotepath += new File(filepath).getName();
241 remotepath = Uri.encode(remotepath, "/");
242
243 i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath);
244 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath);
245 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
246 startService(i);
247 }
248
249 }/* dvelasco: WIP - not working as expected ... yet :)
250 else if (requestCode == ACTION_CREATE_FIRST_ACCOUNT) {
251 if (resultCode != RESULT_OK) {
252 finish(); // the user cancelled the AuthenticatorActivity
253 }
254 }*/
255 }
256
257 @Override
258 public void onBackPressed() {
259 if (mDirectories == null || mDirectories.getCount() <= 1) {
260 finish();
261 return;
262 }
263 popDirname();
264 mFileList.onNavigateUp();
265 mCurrentDir = mFileList.getCurrentFile();
266
267 if(mCurrentDir.getParentId() == 0){
268 ActionBar actionBar = getSupportActionBar();
269 actionBar.setDisplayHomeAsUpEnabled(false);
270 }
271 }
272
273 @Override
274 protected void onSaveInstanceState(Bundle outState) {
275 // responsability of restore is prefered in onCreate() before than in onRestoreInstanceState when there are Fragments involved
276 Log.i(getClass().toString(), "onSaveInstanceState() start");
277 super.onSaveInstanceState(outState);
278 if(mDirectories != null && mDirectories.getCount() != 0){
279 mDirs = new String[mDirectories.getCount()-1];
280 for (int j = mDirectories.getCount() - 2, i = 0; j >= 0; --j, ++i) {
281 mDirs[i] = mDirectories.getItem(j);
282 }
283 }
284 outState.putStringArray(KEY_DIR_ARRAY, mDirs);
285 outState.putParcelable(FileDetailFragment.EXTRA_FILE, mCurrentDir);
286 Log.i(getClass().toString(), "onSaveInstanceState() end");
287 }
288
289 @Override
290 protected void onResume() {
291 Log.i(getClass().toString(), "onResume() start");
292 super.onResume();
293
294 if (AccountUtils.accountsAreSetup(this)) {
295 // at least an account exist: normal operation
296
297 // set the layout only if it couldn't be set in onCreate
298 if (mForcedLoginToCreateFirstAccount) {
299 initDelayedTilAccountAvailabe();
300 mForcedLoginToCreateFirstAccount = false;
301 }
302
303 // Listen for sync messages
304 IntentFilter syncIntentFilter = new IntentFilter(FileSyncService.SYNC_MESSAGE);
305 mSyncBroadcastReceiver = new SyncBroadcastReceiver();
306 registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
307
308 // Listen for upload messages
309 IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
310 mUploadFinishReceiver = new UploadFinishReceiver();
311 registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
312
313 // Storage manager initialization
314 mStorageManager = new FileDataStorageManager(
315 AccountUtils.getCurrentOwnCloudAccount(this),
316 getContentResolver());
317
318 // File list fragments
319 mFileList = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
320
321
322 // Figure out what directory to list.
323 // Priority: Intent (here), savedInstanceState (onCreate), root dir (dir is null)
324 if(getIntent().hasExtra(FileDetailFragment.EXTRA_FILE)){
325 mCurrentDir = (OCFile) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
326 if(mCurrentDir != null && !mCurrentDir.isDirectory()){
327 mCurrentDir = mStorageManager.getFileById(mCurrentDir.getParentId());
328 }
329
330 // Clear intent extra, so rotating the screen will not return us to this directory
331 getIntent().removeExtra(FileDetailFragment.EXTRA_FILE);
332 }
333
334 if (mCurrentDir == null)
335 mCurrentDir = mStorageManager.getFileByPath("/");
336
337 // Drop-Down navigation and file list restore
338 mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
339
340
341 // Given the case we have a file to display:
342 if(mCurrentDir != null){
343 ArrayList<OCFile> files = new ArrayList<OCFile>();
344 OCFile currFile = mCurrentDir;
345 while(currFile != null){
346 files.add(currFile);
347 currFile = mStorageManager.getFileById(currFile.getParentId());
348 }
349
350 // Insert in mDirs
351 mDirs = new String[files.size()];
352 for(int i = files.size() - 1; i >= 0; i--){
353 mDirs[i] = files.get(i).getFileName();
354 }
355 }
356
357 if (mDirs != null) {
358 for (String s : mDirs)
359 mDirectories.add(s);
360 } else {
361 mDirectories.add("/");
362 }
363
364 // Actionbar setup
365 ActionBar action_bar = getSupportActionBar();
366 action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
367 action_bar.setDisplayShowTitleEnabled(false);
368 action_bar.setListNavigationCallbacks(mDirectories, this);
369 if(mCurrentDir != null && mCurrentDir.getParentId() != 0){
370 action_bar.setDisplayHomeAsUpEnabled(true);
371 } else {
372 action_bar.setDisplayHomeAsUpEnabled(false);
373 }
374
375 // List dir here
376 mFileList.listDirectory(mCurrentDir);
377 }
378 Log.i(getClass().toString(), "onResume() end");
379 }
380
381 @Override
382 protected void onPause() {
383 Log.i(getClass().toString(), "onPause() start");
384 super.onPause();
385 if (mSyncBroadcastReceiver != null) {
386 unregisterReceiver(mSyncBroadcastReceiver);
387 mSyncBroadcastReceiver = null;
388 }
389 if (mUploadFinishReceiver != null) {
390 unregisterReceiver(mUploadFinishReceiver);
391 mUploadFinishReceiver = null;
392 }
393 getIntent().putExtra(FileDetailFragment.EXTRA_FILE, mCurrentDir);
394 Log.i(getClass().toString(), "onPause() end");
395 }
396
397 @Override
398 protected Dialog onCreateDialog(int id) {
399 Dialog dialog = null;
400 AlertDialog.Builder builder;
401 switch (id) {
402 case DIALOG_SETUP_ACCOUNT:
403 builder = new AlertDialog.Builder(this);
404 builder.setTitle(R.string.main_tit_accsetup);
405 builder.setMessage(R.string.main_wrn_accsetup);
406 builder.setCancelable(false);
407 builder.setPositiveButton(android.R.string.ok, this);
408 builder.setNegativeButton(android.R.string.cancel, this);
409 dialog = builder.create();
410 break;
411 case DIALOG_ABOUT_APP: {
412 builder = new AlertDialog.Builder(this);
413 builder.setTitle("About");
414 PackageInfo pkg;
415 try {
416 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
417 builder.setMessage("ownCloud android client\n\nversion: " + pkg.versionName );
418 builder.setIcon(android.R.drawable.ic_menu_info_details);
419 dialog = builder.create();
420 } catch (NameNotFoundException e) {
421 builder = null;
422 dialog = null;
423 e.printStackTrace();
424 }
425 break;
426 }
427 case DIALOG_CREATE_DIR: {
428 builder = new Builder(this);
429 final EditText dirNameInput = new EditText(getBaseContext());
430 final Account a = AccountUtils.getCurrentOwnCloudAccount(this);
431 builder.setView(dirNameInput);
432 builder.setTitle(R.string.uploader_info_dirname);
433 int typed_color = getResources().getColor(R.color.setup_text_typed);
434 dirNameInput.setTextColor(typed_color);
435 builder.setPositiveButton(android.R.string.ok,
436 new OnClickListener() {
437 public void onClick(DialogInterface dialog, int which) {
438 String directoryName = dirNameInput.getText().toString();
439 if (directoryName.trim().length() == 0) {
440 dialog.cancel();
441 return;
442 }
443
444 // Figure out the path where the dir needs to be created
445 String path;
446 if (mCurrentDir == null) {
447 // this is just a patch; we should ensure that mCurrentDir never is null
448 if (!mStorageManager.fileExists("/")) {
449 OCFile file = new OCFile("/");
450 mStorageManager.saveFile(file);
451 }
452 mCurrentDir = mStorageManager.getFileByPath("/");
453 }
454 path = FileDisplayActivity.this.mCurrentDir.getRemotePath();
455
456 // Create directory
457 path += Uri.encode(directoryName) + "/";
458 Thread thread = new Thread(new DirectoryCreator(path, a));
459 thread.start();
460
461 // Save new directory in local database
462 OCFile newDir = new OCFile(path);
463 newDir.setMimetype("DIR");
464 newDir.setParentId(mCurrentDir.getFileId());
465 mStorageManager.saveFile(newDir);
466
467 // Display the new folder right away
468 dialog.dismiss();
469 mFileList.listDirectory(mCurrentDir);
470 }
471 });
472 builder.setNegativeButton(R.string.common_cancel,
473 new OnClickListener() {
474 public void onClick(DialogInterface dialog, int which) {
475 dialog.cancel();
476 }
477 });
478 dialog = builder.create();
479 break;
480 }
481 default:
482 dialog = null;
483 }
484
485 return dialog;
486 }
487
488
489 /**
490 * Responds to the "There are no ownCloud Accounts setup" dialog
491 * TODO: Dialog is 100% useless -> Remove
492 */
493 @Override
494 public void onClick(DialogInterface dialog, int which) {
495 // In any case - we won't need it anymore
496 dialog.dismiss();
497 switch (which) {
498 case DialogInterface.BUTTON_POSITIVE:
499 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
500 intent.putExtra("authorities",
501 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
502 startActivity(intent);
503 break;
504 case DialogInterface.BUTTON_NEGATIVE:
505 finish();
506 }
507
508 }
509
510 /**
511 * Translates a content URI of an image to a physical path
512 * on the disk
513 * @param uri The URI to resolve
514 * @return The path to the image or null if it could not be found
515 */
516 public String getPath(Uri uri) {
517 String[] projection = { MediaStore.Images.Media.DATA };
518 Cursor cursor = managedQuery(uri, projection, null, null, null);
519 if (cursor != null) {
520 int column_index = cursor
521 .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
522 cursor.moveToFirst();
523 return cursor.getString(column_index);
524 }
525 return null;
526 }
527
528 /**
529 * Pushes a directory to the drop down list
530 * @param directory to push
531 * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.
532 */
533 public void pushDirname(OCFile directory) {
534 if(!directory.isDirectory()){
535 throw new IllegalArgumentException("Only directories may be pushed!");
536 }
537 mDirectories.insert(directory.getFileName(), 0);
538 mCurrentDir = directory;
539 }
540
541 /**
542 * Pops a directory name from the drop down list
543 * @return True, unless the stack is empty
544 */
545 public boolean popDirname() {
546 mDirectories.remove(mDirectories.getItem(0));
547 return !mDirectories.isEmpty();
548 }
549
550 private class DirectoryCreator implements Runnable {
551 private String mTargetPath;
552 private Account mAccount;
553 private AccountManager mAm;
554
555 public DirectoryCreator(String targetPath, Account account) {
556 mTargetPath = targetPath;
557 mAccount = account;
558 mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);
559 }
560
561 @Override
562 public void run() {
563 WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext());
564
565 String username = mAccount.name.substring(0,
566 mAccount.name.lastIndexOf('@'));
567 String password = mAm.getPassword(mAccount);
568
569 wdc.setCredentials(username, password);
570 wdc.allowSelfsignedCertificates();
571 wdc.createDirectory(mTargetPath);
572 }
573
574 }
575
576 // Custom array adapter to override text colors
577 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
578
579 public CustomArrayAdapter(FileDisplayActivity ctx, int view) {
580 super(ctx, view);
581 }
582
583 public View getView(int position, View convertView, ViewGroup parent) {
584 View v = super.getView(position, convertView, parent);
585
586 ((TextView) v).setTextColor(getResources().getColorStateList(
587 android.R.color.white));
588 return v;
589 }
590
591 public View getDropDownView(int position, View convertView,
592 ViewGroup parent) {
593 View v = super.getDropDownView(position, convertView, parent);
594
595 ((TextView) v).setTextColor(getResources().getColorStateList(
596 android.R.color.white));
597
598 return v;
599 }
600
601 }
602
603 private class SyncBroadcastReceiver extends BroadcastReceiver {
604 /**
605 * {@link BroadcastReceiver} to enable syncing feedback in UI
606 */
607 @Override
608 public void onReceive(Context context, Intent intent) {
609 boolean inProgress = intent.getBooleanExtra(
610 FileSyncService.IN_PROGRESS, false);
611 String account_name = intent
612 .getStringExtra(FileSyncService.ACCOUNT_NAME);
613
614 Log.d("FileDisplay", "sync of account " + account_name
615 + " is in_progress: " + inProgress);
616
617 //if (account_name.equals(AccountUtils.getCurrentOwnCloudAccount(context).name)) { // TODO - probably this check should be added, but won't push it until really tests are done; no time now
618
619 /// try to refresh the view with every message received from the FileSyncAdapter; brute, but more user friendly when there are a lot of files in the server
620 OCFile currentDir;
621 if (mCurrentDir == null)
622 currentDir = mStorageManager.getFileByPath("/");
623 else
624 currentDir = mStorageManager.getFileByPath(mCurrentDir.getRemotePath());
625
626 if (currentDir != null) {
627 mCurrentDir = currentDir;
628 FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager()
629 .findFragmentById(R.id.fileList);
630 if (fileListFragment != null) {
631 if (!mCurrentDir.equals(fileListFragment.getCurrentFile())) {
632 fileListFragment.listDirectory(mCurrentDir); // only set the directory in the fragment first time
633 } else
634 fileListFragment.listDirectory(); // enough to show new files in the current directory if they are added after
635 }
636 }
637
638 setSupportProgressBarIndeterminateVisibility(inProgress);
639 //}
640 }
641 }
642
643
644 private class UploadFinishReceiver extends BroadcastReceiver {
645 /**
646 * Once the file upload has finished -> update view
647 * @author David A. Velasco
648 * {@link BroadcastReceiver} to enable upload feedback in UI
649 */
650 @Override
651 public void onReceive(Context context, Intent intent) {
652 long parentDirId = intent.getLongExtra(FileUploader.EXTRA_PARENT_DIR_ID, -1);
653 OCFile parentDir = mStorageManager.getFileById(parentDirId);
654
655 if (parentDir != null && (
656 (mCurrentDir == null && parentDir.getFileName().equals("/")) ||
657 parentDir.equals(mCurrentDir))
658 ) {
659 FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
660 if (fileListFragment != null) {
661 fileListFragment.listDirectory();
662 }
663 }
664 }
665
666 }
667
668
669 @Override
670 public void onClick(View v) {
671 if (v.getId() == R.id.setup_account) {
672 Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);
673 intent.putExtra(android.provider.Settings.EXTRA_AUTHORITIES, new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
674 startActivity(intent);
675 mForcedLoginToCreateFirstAccount = true;
676 }
677 }
678
679
680
681
682
683 /**
684 * {@inheritDoc}
685 */
686 @Override
687 public DataStorageManager getStorageManager() {
688 return mStorageManager;
689 }
690
691
692 /**
693 * {@inheritDoc}
694 */
695 @Override
696 public void onDirectoryClick(OCFile directory) {
697 pushDirname(directory);
698 ActionBar actionBar = getSupportActionBar();
699 actionBar.setDisplayHomeAsUpEnabled(true);
700
701 if (mDualPane) {
702 // Resets the FileDetailsFragment on Tablets so that it always displays
703 FileDetailFragment fileDetails = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
704 if (fileDetails != null) {
705 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
706 transaction.remove(fileDetails);
707 transaction.add(R.id.file_details_container, new FileDetailFragment(null, null));
708 transaction.commit();
709 }
710 }
711 }
712
713
714 /**
715 * {@inheritDoc}
716 */
717 @Override
718 public void onFileClick(OCFile file) {
719
720 // If we are on a large device -> update fragment
721 if (mDualPane) {
722 // buttons in the details view are problematic when trying to reuse an existing fragment; create always a new one solves some of them, BUT no all; downloads are 'dangerous'
723 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
724 transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
725 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
726 transaction.commit();
727
728 } else { // small or medium screen device -> new Activity
729 Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
730 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
731 showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
732 startActivity(showDetailsIntent);
733 }
734 }
735
736 /**
737 * Operations in this method should be preferably performed in onCreate to have a lighter onResume method.
738 *
739 * But we need to delay them to onResume for the first start of the application, when no account exists and the login activity must be shown; and
740 * put instead the ugly view that shows the 'Setup' button to restart the login activity.
741 *
742 * In other way, if the users cancels or presses BACK in the login page that first time (users can be cruel sometimes) would show a blank view (the
743 * FragmentList view empty).
744 *
745 * This is temporal, until we found out how to get a result in this activity after launching the ADD_ACCOUNT Intent with startActivityForResult (not trivial)
746 */
747 private void initDelayedTilAccountAvailabe() {
748 setContentView(mLayoutView);
749 mDualPane = (findViewById(R.id.file_details_container) != null);
750 if (mDualPane && getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG) == null) {
751 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
752 transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment
753 transaction.commit();
754 }
755
756 }
757
758
759 }