X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/8eae3a2be5b08e2a5a0d271dbcfba91682bec368..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 0b97c685..b3202976 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -21,18 +21,19 @@ package com.owncloud.android.ui.activity; import java.io.File; import android.accounts.Account; -import android.accounts.AccountManager; import android.app.AlertDialog; import android.app.ProgressDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.content.IntentFilter; +import android.content.ServiceConnection; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; @@ -40,8 +41,8 @@ import android.content.res.Resources.NotFoundException; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; -import android.os.Environment; import android.os.Handler; +import android.os.IBinder; import android.preference.PreferenceManager; import android.provider.MediaStore; import android.support.v4.app.FragmentTransaction; @@ -61,16 +62,14 @@ import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.Window; import com.owncloud.android.AccountUtils; -import com.owncloud.android.CrashHandler; import com.owncloud.android.authenticator.AccountAuthenticator; import com.owncloud.android.datamodel.DataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; -import com.owncloud.android.files.OwnCloudFileObserver; import com.owncloud.android.files.services.FileDownloader; -import com.owncloud.android.files.services.FileObserverService; +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; @@ -90,12 +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,11 +119,16 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements public void onCreate(Bundle savedInstanceState) { Log.d(getClass().toString(), "onCreate() start"); super.onCreate(savedInstanceState); - - //Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext())); - /// 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); } @@ -131,9 +139,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } else { /// at least an account is available - 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) { @@ -162,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 @@ -200,20 +211,51 @@ 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(); + if (mDownloadConnection != null) + unbindService(mDownloadConnection); + if (mUploadConnection != null) + unbindService(mUploadConnection); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSherlock().getMenuInflater(); @@ -381,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(); } } @@ -398,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"); } @@ -411,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 @@ -429,7 +481,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements registerReceiver(mDownloadFinishReceiver, downloadIntentFilter); // List current directory - mFileList.listDirectory(mCurrentDir); + mFileList.listDirectory(mCurrentDir); // we should find the way to avoid the need of this } else { @@ -440,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"); @@ -460,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"); } @@ -640,13 +692,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements private class DirectoryCreator implements Runnable { private String mTargetPath; private Account mAccount; - private AccountManager mAm; private Handler mHandler; public DirectoryCreator(String targetPath, Account account, Handler handler) { mTargetPath = targetPath; mAccount = account; - mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE); mHandler = handler; } @@ -833,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(); } } @@ -857,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); } } @@ -867,12 +917,78 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements * {@inheritDoc} */ @Override + public OCFile getInitialDirectory() { + return mCurrentDir; + } + + + /** + * {@inheritDoc} + */ + @Override public void onFileStateChanged() { OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); if (fileListFragment != null) { fileListFragment.listDirectory(); } } + + + /** + * {@inheritDoc} + */ + @Override + public FileDownloaderBinder getFileDownloaderBinder() { + return mDownloaderBinder; + } + + + /** + * {@inheritDoc} + */ + @Override + public FileUploaderBinder getFileUploaderBinder() { + return mUploaderBinder; + } + + + /** Defines callbacks for service binding, passed to bindService() */ + private class ListServiceConnection implements ServiceConnection { + + @Override + 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); + if (fragment != null) + fragment.updateFileDetails(); + } + + } + + @Override + 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; + } + } + }; + + /** * Launch an intent to request the PIN code to the user before letting him use the app @@ -888,5 +1004,5 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } } - + }