X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/38512099c49a32406725086074ac4973220807c1..5fc7cd13e7e561ef528e12d2fa088b58e35e00d0:/src/com/owncloud/android/ui/activity/FileDisplayActivity.java diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 50b8361f..b3202976 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -69,6 +69,7 @@ import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader; +import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; import com.owncloud.android.network.OwnCloudClientUtils; import com.owncloud.android.syncadapter.FileSyncService; import com.owncloud.android.ui.fragment.FileDetailFragment; @@ -88,13 +89,16 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener { private ArrayAdapter mDirectories; - private OCFile mCurrentDir; + private OCFile mCurrentDir = null; + private OCFile mCurrentFile = null; private DataStorageManager mStorageManager; private SyncBroadcastReceiver mSyncBroadcastReceiver; private UploadFinishReceiver mUploadFinishReceiver; private DownloadFinishReceiver mDownloadFinishReceiver; private FileDownloaderBinder mDownloaderBinder = null; + private FileUploaderBinder mUploaderBinder = null; + private ServiceConnection mDownloadConnection = null, mUploadConnection = null; private OCFileListFragment mFileList; @@ -116,8 +120,15 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements Log.d(getClass().toString(), "onCreate() start"); super.onCreate(savedInstanceState); - /// saved instance state: keep this always before initDataFromCurrentAccount() + /// Load of parameters from received intent + mCurrentDir = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE); // no check necessary, mCurrenDir == null if the parameter is not in the intent + Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT); + if (account != null) + AccountUtils.setCurrentOwnCloudAccount(this, account.name); + + /// Load of saved instance state: keep this always before initDataFromCurrentAccount() if(savedInstanceState != null) { + // TODO - test if savedInstanceState should take precedence over file in the intent ALWAYS (now), NEVER, or SOME TIMES mCurrentDir = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE); } @@ -128,10 +139,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } else { /// at least an account is available - bindService(new Intent(this, FileDownloader.class), mConnection, Context.BIND_AUTO_CREATE); - initDataFromCurrentAccount(); + initDataFromCurrentAccount(); // it checks mCurrentDir and mCurrentFile with the current account } + + mUploadConnection = new ListServiceConnection(); + mDownloadConnection = new ListServiceConnection(); + bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE); + bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE); // PIN CODE request ; best location is to decide, let's try this first if (getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN) && savedInstanceState == null) { @@ -160,10 +175,8 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements setContentView(R.layout.files); mFileList = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); mDualPane = (findViewById(R.id.file_details_container) != null); - if (mDualPane && getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG) == null) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment - transaction.commit(); + if (mDualPane) { + initFileDetailsInDualPane(); } // Action bar setup @@ -198,24 +211,48 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements AccountUtils.getCurrentOwnCloudAccount(this), getContentResolver()); - /// State recovery - CURRENT DIRECTORY ; priority: 1/ getIntent(), 2/ savedInstanceState (in onCreate()), 3/ root dir - if(getIntent().hasExtra(FileDetailFragment.EXTRA_FILE)) { - mCurrentDir = (OCFile) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE); - if(mCurrentDir != null && !mCurrentDir.isDirectory()){ - mCurrentDir = mStorageManager.getFileById(mCurrentDir.getParentId()); - } - // clear intent extra, so rotating the screen will not return us to this directory - getIntent().removeExtra(FileDetailFragment.EXTRA_FILE); + /// Check if mCurrentDir is a directory + if(mCurrentDir != null && !mCurrentDir.isDirectory()) { + mCurrentFile = mCurrentDir; + mCurrentDir = mStorageManager.getFileById(mCurrentDir.getParentId()); + } + + /// Check if mCurrentDir and mCurrentFile are in the current account, and update them + if (mCurrentDir != null) { + mCurrentDir = mStorageManager.getFileByPath(mCurrentDir.getRemotePath()); // mCurrentDir == null if it is not in the current account + } + if (mCurrentFile != null) { + mCurrentFile = mStorageManager.getFileByPath(mCurrentFile.getRemotePath()); // mCurrentFile == null if it is not in the current account + } + + /// Default to root if mCurrentDir was not found + if (mCurrentDir == null) { + mCurrentDir = mStorageManager.getFileByPath("/"); // will be NULL if the database was never synchronized } - if (mCurrentDir == null) - mCurrentDir = mStorageManager.getFileByPath("/"); // this will return NULL if the database has not ever synchronized } + private void initFileDetailsInDualPane() { + if (mDualPane && getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG) == null) { + FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); + if (mCurrentFile != null) { + transaction.replace(R.id.file_details_container, new FileDetailFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG); // empty FileDetailFragment + mCurrentFile = null; + } else { + transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment + } + transaction.commit(); + } + } + + @Override public void onDestroy() { super.onDestroy(); - unbindService(mConnection); + if (mDownloadConnection != null) + unbindService(mDownloadConnection); + if (mUploadConnection != null) + unbindService(mUploadConnection); } @@ -386,7 +423,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements if (fileDetails != null && !fileDetails.isEmpty()) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.remove(fileDetails); - transaction.add(R.id.file_details_container, new FileDetailFragment(null, null)); + transaction.add(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); transaction.commit(); } } @@ -403,6 +440,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements Log.d(getClass().toString(), "onSaveInstanceState() start"); super.onSaveInstanceState(outState); outState.putParcelable(FileDetailFragment.EXTRA_FILE, mCurrentDir); + if (mDualPane) { + FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); + OCFile file = fragment.getDisplayedFile(); + if (file != null) { + outState.putParcelable(FileDetailFragment.EXTRA_FILE, file); + } + } Log.d(getClass().toString(), "onSaveInstanceState() end"); } @@ -416,6 +460,9 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements if (mStorageManager == null) { // this is necessary for handling the come back to FileDisplayActivity when the first ownCloud account is created initDataFromCurrentAccount(); + if (mDualPane) { + initFileDetailsInDualPane(); + } } // Listen for sync messages @@ -445,6 +492,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements Log.d(getClass().toString(), "onResume() end"); } + @Override protected void onPause() { Log.d(getClass().toString(), "onPause() start"); @@ -465,7 +513,6 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements dismissDialog(DIALOG_SETUP_ACCOUNT); } - getIntent().putExtra(FileDetailFragment.EXTRA_FILE, mCurrentDir); Log.d(getClass().toString(), "onPause() end"); } @@ -836,7 +883,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements if (fileDetails != null && !fileDetails.isEmpty()) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.remove(fileDetails); - transaction.add(R.id.file_details_container, new FileDetailFragment(null, null)); + transaction.add(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); transaction.commit(); } } @@ -860,7 +907,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } else { // small or medium screen device -> new Activity Intent showDetailsIntent = new Intent(this, FileDetailActivity.class); showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file); - showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this)); + showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this)); startActivity(showDetailsIntent); } } @@ -894,15 +941,32 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements public FileDownloaderBinder getFileDownloaderBinder() { return mDownloaderBinder; } + + + /** + * {@inheritDoc} + */ + @Override + public FileUploaderBinder getFileUploaderBinder() { + return mUploaderBinder; + } /** Defines callbacks for service binding, passed to bindService() */ - private ServiceConnection mConnection = new ServiceConnection() { + private class ListServiceConnection implements ServiceConnection { @Override - public void onServiceConnected(ComponentName className, IBinder service) { - mDownloaderBinder = (FileDownloaderBinder) service; - // a new chance to get the mDownloadBinder through getDownloadBinder() - THIS IS A MESS + public void onServiceConnected(ComponentName component, IBinder service) { + if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) { + Log.d(TAG, "Download service connected"); + mDownloaderBinder = (FileDownloaderBinder) service; + } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) { + Log.d(TAG, "Upload service connected"); + mUploaderBinder = (FileUploaderBinder) service; + } else { + return; + } + // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS mFileList.listDirectory(); if (mDualPane) { FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); @@ -913,8 +977,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } @Override - public void onServiceDisconnected(ComponentName arg0) { - mDownloaderBinder = null; + public void onServiceDisconnected(ComponentName component) { + if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) { + Log.d(TAG, "Download service disconnected"); + mDownloaderBinder = null; + } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) { + Log.d(TAG, "Upload service disconnected"); + mUploaderBinder = null; + } } };