From: David A. Velasco Date: Thu, 30 May 2013 15:53:21 +0000 (+0200) Subject: REFACTORING: FileDetailActivity never more - simplifies transitions and removes bugs... X-Git-Tag: oc-android-1.4.3~21^2~6 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/fd39628997a8544213c32249e50bbae5f983e094 REFACTORING: FileDetailActivity never more - simplifies transitions and removes bugs in orientation changes --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index b047f918..ee9ecafd 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -50,7 +50,7 @@ + > @@ -151,7 +151,6 @@ - diff --git a/res/layout-large-land/files.xml b/res/layout-large-land/files.xml index d874bf54..e20fd229 100644 --- a/res/layout-large-land/files.xml +++ b/res/layout-large-land/files.xml @@ -15,36 +15,28 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program. If not, see . + along with this program. If not, see . --> - - - - - - - - - - + + + + - - - + android:layout_weight="2" + android:layout_height="match_parent" + /> diff --git a/res/layout/files.xml b/res/layout/files.xml index 0e4fd8b3..8a1c2210 100644 --- a/res/layout/files.xml +++ b/res/layout/files.xml @@ -17,20 +17,19 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . --> - + android:id="@+id/single_fragment_container"> + + --> - + diff --git a/src/com/owncloud/android/files/services/FileDownloader.java b/src/com/owncloud/android/files/services/FileDownloader.java index ff5e4410..1ec5591c 100644 --- a/src/com/owncloud/android/files/services/FileDownloader.java +++ b/src/com/owncloud/android/files/services/FileDownloader.java @@ -38,7 +38,7 @@ import com.owncloud.android.operations.DownloadFileOperation; import com.owncloud.android.operations.RemoteOperationResult; import com.owncloud.android.operations.RemoteOperationResult.ResultCode; import com.owncloud.android.ui.activity.FileActivity; -import com.owncloud.android.ui.activity.FileDetailActivity; +import com.owncloud.android.ui.activity.FileDisplayActivity; import com.owncloud.android.ui.preview.PreviewImageActivity; import com.owncloud.android.ui.preview.PreviewImageFragment; @@ -414,7 +414,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis if (PreviewImageFragment.canBePreviewed(download.getFile())) { showDetailsIntent = new Intent(this, PreviewImageActivity.class); } else { - showDetailsIntent = new Intent(this, FileDetailActivity.class); + showDetailsIntent = new Intent(this, FileDisplayActivity.class); } showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, download.getFile()); showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, download.getAccount()); @@ -485,7 +485,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis if (PreviewImageFragment.canBePreviewed(download.getFile())) { showDetailsIntent = new Intent(this, PreviewImageActivity.class); } else { - showDetailsIntent = new Intent(this, FileDetailActivity.class); + showDetailsIntent = new Intent(this, FileDisplayActivity.class); } showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, download.getFile()); showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, download.getAccount()); diff --git a/src/com/owncloud/android/files/services/FileUploader.java b/src/com/owncloud/android/files/services/FileUploader.java index b41f8709..2f66e305 100644 --- a/src/com/owncloud/android/files/services/FileUploader.java +++ b/src/com/owncloud/android/files/services/FileUploader.java @@ -42,7 +42,6 @@ import com.owncloud.android.operations.RemoteOperation; import com.owncloud.android.operations.RemoteOperationResult; import com.owncloud.android.operations.UploadFileOperation; import com.owncloud.android.operations.RemoteOperationResult.ResultCode; -import com.owncloud.android.ui.activity.FileDetailActivity; import com.owncloud.android.utils.OwnCloudVersion; import eu.alefzero.webdav.OnDatatransferProgressListener; @@ -74,6 +73,7 @@ import com.owncloud.android.R; import com.owncloud.android.db.DbHandler; import com.owncloud.android.ui.activity.FailedUploadActivity; import com.owncloud.android.ui.activity.FileActivity; +import com.owncloud.android.ui.activity.FileDisplayActivity; import com.owncloud.android.ui.activity.InstantUploadActivity; import com.owncloud.android.ui.preview.PreviewImageActivity; import com.owncloud.android.ui.preview.PreviewImageFragment; @@ -703,8 +703,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe if (PreviewImageFragment.canBePreviewed(upload.getFile())) { showDetailsIntent = new Intent(this, PreviewImageActivity.class); } else { - showDetailsIntent = new Intent(this, FileDetailActivity.class); - showDetailsIntent.putExtra(FileDetailActivity.EXTRA_MODE, FileDetailActivity.MODE_DETAILS); + showDetailsIntent = new Intent(this, FileDisplayActivity.class); } showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, upload.getFile()); showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, upload.getAccount()); @@ -766,8 +765,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe if (PreviewImageFragment.canBePreviewed(upload.getFile())) { showDetailsIntent = new Intent(this, PreviewImageActivity.class); } else { - showDetailsIntent = new Intent(this, FileDetailActivity.class); - showDetailsIntent.putExtra(FileDetailActivity.EXTRA_MODE, FileDetailActivity.MODE_DETAILS); + showDetailsIntent = new Intent(this, FileDisplayActivity.class); } showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, upload.getFile()); showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, upload.getAccount()); diff --git a/src/com/owncloud/android/media/MediaService.java b/src/com/owncloud/android/media/MediaService.java index d6ca0967..992bc7dd 100644 --- a/src/com/owncloud/android/media/MediaService.java +++ b/src/com/owncloud/android/media/MediaService.java @@ -41,7 +41,7 @@ import com.owncloud.android.Log_OC; import com.owncloud.android.R; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.ui.activity.FileActivity; -import com.owncloud.android.ui.activity.FileDetailActivity; +import com.owncloud.android.ui.activity.FileDisplayActivity; /** * Service that handles media playback, both audio and video. @@ -532,7 +532,7 @@ public class MediaService extends Service implements OnCompletionListener, OnPre @SuppressWarnings("deprecation") private void updateNotification(String content) { // TODO check if updating the Intent is really necessary - Intent showDetailsIntent = new Intent(this, FileDetailActivity.class); + Intent showDetailsIntent = new Intent(this, FileDisplayActivity.class); showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, mFile); showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, mAccount); showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); @@ -569,7 +569,7 @@ public class MediaService extends Service implements OnCompletionListener, OnPre /// includes a pending intent in the notification showing the details view of the file - Intent showDetailsIntent = new Intent(this, FileDetailActivity.class); + Intent showDetailsIntent = new Intent(this, FileDisplayActivity.class); showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, mFile); showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, mAccount); showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); diff --git a/src/com/owncloud/android/ui/FragmentListView.java b/src/com/owncloud/android/ui/FragmentListView.java deleted file mode 100644 index 43b2fc65..00000000 --- a/src/com/owncloud/android/ui/FragmentListView.java +++ /dev/null @@ -1,100 +0,0 @@ -/* ownCloud Android client application - * Copyright (C) 2012 Bartek Przybylski - * Copyright (C) 2012-2013 ownCloud Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package com.owncloud.android.ui; - -import com.actionbarsherlock.app.SherlockFragment; -import com.owncloud.android.R; -import com.owncloud.android.ui.fragment.LocalFileListFragment; - -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemLongClickListener; -import android.widget.ListAdapter; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.ListView; - -public class FragmentListView extends SherlockFragment implements - OnItemClickListener, OnItemLongClickListener { - protected ExtendedListView mList; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - } - - public void setListAdapter(ListAdapter listAdapter) { - mList.setAdapter(listAdapter); - mList.invalidate(); - } - - public ListView getListView() { - return mList; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - //mList = new ExtendedListView(getActivity()); - View v = inflater.inflate(R.layout.list_fragment, null); - mList = (ExtendedListView)(v.findViewById(R.id.list_root)); - mList.setOnItemClickListener(this); - mList.setOnItemLongClickListener(this); - //mList.setEmptyView(v.findViewById(R.id.empty_list_view)); // looks like it's not a cool idea - mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator)); - mList.setDividerHeight(1); - return v; - } - - public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { - } - - @Override - public boolean onItemLongClick(AdapterView arg0, View arg1, int arg2, - long arg3) { - return false; - } - - - /** - * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when - * the device is turned to other position. - * - * THe current policy is take as a reference the visible item in the center of the screen. - * - * @return The position in the list of the visible item in the center of the screen. - */ - protected int getReferencePosition() { - return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2; - } - - - /** - * Sets the visible part of the list from the reference position. - * - * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()} - */ - protected void setReferencePosition(int position) { - mList.setAndCenterSelection(position); - } - - -} diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 3b857a8f..0ce31e26 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -34,7 +34,6 @@ import com.owncloud.android.Log_OC; import com.owncloud.android.R; import com.owncloud.android.authentication.AccountAuthenticator; import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.files.FileHandler; import eu.alefzero.webdav.WebdavUtils; @@ -43,7 +42,7 @@ import eu.alefzero.webdav.WebdavUtils; * * @author David A. Velasco */ -public abstract class FileActivity extends SherlockFragmentActivity implements FileHandler { +public abstract class FileActivity extends SherlockFragmentActivity { public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE"; public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT"; @@ -61,8 +60,6 @@ public abstract class FileActivity extends SherlockFragmentActivity implements F /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */ private boolean mRedirectingToSetupAccount = false; - private FileHandlerImpl mFileHandler; - @Override protected void onCreate(Bundle savedInstanceState) { @@ -81,7 +78,6 @@ public abstract class FileActivity extends SherlockFragmentActivity implements F onAccountChanged(); } - mFileHandler = new FileHandlerImpl(); } @@ -91,7 +87,7 @@ public abstract class FileActivity extends SherlockFragmentActivity implements F */ @Override protected void onStart() { - Log_OC.e(TAG, "onStart en FileActivity"); + Log_OC.d(TAG, "onStart en FileActivity"); super.onStart(); /// Validate account, and try to fix if wrong if (mAccount == null || !AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name)) { @@ -215,11 +211,6 @@ public abstract class FileActivity extends SherlockFragmentActivity implements F } - public void openFile(OCFile file) { - mFileHandler.openFile(file); - } - - /** * Called when the ownCloud {@link Account} associated to the Activity was just updated. * @@ -228,39 +219,38 @@ public abstract class FileActivity extends SherlockFragmentActivity implements F protected abstract void onAccountChanged(); - public class FileHandlerImpl implements FileHandler { - - public void openFile(OCFile file) { - if (file != null) { - String storagePath = file.getStoragePath(); - String encodedStoragePath = WebdavUtils.encodePath(storagePath); - - Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW); - intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype()); - intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - - Intent intentForGuessedMimeType = null; - if (storagePath.lastIndexOf('.') >= 0) { - String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); - if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) { - intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW); - intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType); - intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - } - } - - Intent chooserIntent = null; - if (intentForGuessedMimeType != null) { - chooserIntent = Intent.createChooser(intentForGuessedMimeType, getString(R.string.actionbar_open_with)); - } else { - chooserIntent = Intent.createChooser(intentForSavedMimeType, getString(R.string.actionbar_open_with)); + + public void openFile(OCFile file) { + if (file != null) { + String storagePath = file.getStoragePath(); + String encodedStoragePath = WebdavUtils.encodePath(storagePath); + + Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW); + intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype()); + intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + + Intent intentForGuessedMimeType = null; + if (storagePath.lastIndexOf('.') >= 0) { + String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); + if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) { + intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW); + intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType); + intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } - - startActivity(chooserIntent); - + } + + Intent chooserIntent = null; + if (intentForGuessedMimeType != null) { + chooserIntent = Intent.createChooser(intentForGuessedMimeType, getString(R.string.actionbar_open_with)); } else { - Log_OC.wtf(TAG, "Trying to open a NULL OCFile"); + chooserIntent = Intent.createChooser(intentForSavedMimeType, getString(R.string.actionbar_open_with)); } + + startActivity(chooserIntent); + + } else { + Log_OC.wtf(TAG, "Trying to open a NULL OCFile"); } } + } diff --git a/src/com/owncloud/android/ui/activity/FileDetailActivity.java b/src/com/owncloud/android/ui/activity/FileDetailActivity.java deleted file mode 100644 index 61d769aa..00000000 --- a/src/com/owncloud/android/ui/activity/FileDetailActivity.java +++ /dev/null @@ -1,492 +0,0 @@ -/* ownCloud Android client application - * Copyright (C) 2011 Bartek Przybylski - * Copyright (C) 2012-2013 ownCloud Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ -package com.owncloud.android.ui.activity; - -import android.accounts.Account; -import android.app.Dialog; -import android.app.ProgressDialog; -import android.content.BroadcastReceiver; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.content.ServiceConnection; -import android.content.res.Configuration; -import android.os.Bundle; -import android.os.IBinder; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentTransaction; - -import com.actionbarsherlock.app.ActionBar; -import com.actionbarsherlock.view.MenuItem; -import com.owncloud.android.AccountUtils; -import com.owncloud.android.Log_OC; -import com.owncloud.android.R; -import com.owncloud.android.datamodel.FileDataStorageManager; -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.ui.fragment.FileDetailFragment; -import com.owncloud.android.ui.fragment.FileFragment; -import com.owncloud.android.ui.preview.PreviewMediaFragment; -import com.owncloud.android.ui.preview.PreviewVideoActivity; - -/** - * This activity displays the details of a file like its name, its size and so - * on. - * - * @author Bartek Przybylski - * @author David A. Velasco - */ -public class FileDetailActivity extends FileActivity implements FileFragment.ContainerActivity { - - public static final int DIALOG_SHORT_WAIT = 0; - - public static final String TAG = FileDetailActivity.class.getSimpleName(); - - public static final String EXTRA_MODE = "MODE"; - public static final int MODE_DETAILS = 0; - public static final int MODE_PREVIEW = 1; - - public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW"; - - private FileDownloaderBinder mDownloaderBinder = null; - private ServiceConnection mDownloadConnection, mUploadConnection = null; - private FileUploaderBinder mUploaderBinder = null; - private boolean mWaitingToPreview; - - private FileDataStorageManager mStorageManager; - private DownloadFinishReceiver mDownloadFinishReceiver; - - private Configuration mNewConfigurationChangeToApplyOnStart; - - private boolean mStarted; - - private boolean mDualPane; - - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mStarted = false; - - // check if configuration is proper for this activity; tablets in landscape should pass the torch to FileDisplayActivity - Configuration conf = getResources().getConfiguration(); - mDualPane = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE && - (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE - ); - - if (mDualPane) { - // only happens when notifications (downloads, uploads) are clicked at the notification bar - backToDisplayActivity(false); - - } else { - setContentView(R.layout.file_activity_details); - - ActionBar actionBar = getSupportActionBar(); - actionBar.setDisplayHomeAsUpEnabled(true); - - if (savedInstanceState == null) { - mWaitingToPreview = false; - createChildFragment(); - } else { - mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW); - } - - mDownloadConnection = new DetailsServiceConnection(); - bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE); - mUploadConnection = new DetailsServiceConnection(); - bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE); - } - } - - /** - * Creates the proper fragment depending upon the state of the handled {@link OCFile} and - * the requested {@link Intent}. - */ - private void createChildFragment() { - int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW); - - Fragment newFragment = null; - OCFile file = getFile(); - Account account = getAccount(); - if (mode == MODE_DETAILS) { - newFragment = new FileDetailFragment(file, account); - - } else if (file.isDown()) { - if (PreviewMediaFragment.canBePreviewed(file)) { - int startPlaybackPosition = getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0); - boolean autoplay = getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true); - newFragment = new PreviewMediaFragment(file, account, startPlaybackPosition, autoplay); - - } else { - newFragment = new FileDetailFragment(file, account); - // TODO open with - } - - } else { - newFragment = new FileDetailFragment(file, account); - mWaitingToPreview = true; // download will requested - } - FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); - ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG); - ft.commit(); - } - - @Override - public void onActivityResult (int requestCode, int resultCode, Intent data) { - Log_OC.e(TAG, "onActivityResult"); - super.onActivityResult(requestCode, resultCode, data); - } - - @Override - public void onConfigurationChanged (Configuration newConfig) { - super.onConfigurationChanged(newConfig); - if (mStarted) { - checkConfigurationChange(newConfig); - } else { - mNewConfigurationChangeToApplyOnStart = newConfig; - } - } - - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview); - } - - - @Override - public void onStart() { - super.onStart(); - Log_OC.e(TAG, "onStart"); - if (mNewConfigurationChangeToApplyOnStart != null && !isRedirectingToSetupAccount()) { - checkConfigurationChange(mNewConfigurationChangeToApplyOnStart); - mNewConfigurationChangeToApplyOnStart = null; - } - mStarted = true; - } - - private void checkConfigurationChange(Configuration newConfig) { - finish(); - Intent intent = null; - OCFile file = getFile(); - Account account = getAccount(); - if ((newConfig.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE - && newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { - - intent = new Intent(this, FileDisplayActivity.class); - intent.putExtra(EXTRA_FILE, file); - intent.putExtra(EXTRA_ACCOUNT, account); - intent.putExtra(EXTRA_MODE, getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW)); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fragment != null && file != null && fragment instanceof PreviewMediaFragment && file.isVideo()) { - PreviewMediaFragment videoFragment = (PreviewMediaFragment)fragment; - intent.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, videoFragment.getPosition()); - intent.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, videoFragment.isPlaying()); - } - if (mWaitingToPreview) { - intent.putExtra(FileDisplayActivity.EXTRA_WAITING_TO_PREVIEW, mWaitingToPreview); - } - - } else { - intent = new Intent(this, FileDetailActivity.class); - intent .putExtra(EXTRA_FILE, file); - intent .putExtra(EXTRA_ACCOUNT, account); - intent.putExtra(EXTRA_MODE, getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW)); - Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fragment != null && file != null && fragment instanceof PreviewMediaFragment && file.isVideo()) { - PreviewMediaFragment videoFragment = (PreviewMediaFragment)fragment; - intent.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, videoFragment.getPosition()); - intent.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, videoFragment.isPlaying()); - } - // and maybe 'waiting to preview' flag - } - startActivity(intent); - } - - @Override - public void onStop() { - super.onStop(); - Log_OC.e(TAG, "onStop"); - mStarted = false; - } - @Override - public void onPause() { - super.onPause(); - Log_OC.e(TAG, "onPause"); - if (mDownloadFinishReceiver != null) { - unregisterReceiver(mDownloadFinishReceiver); - mDownloadFinishReceiver = null; - } - } - - - @Override - public void onResume() { - super.onResume(); - Log_OC.e(TAG, "onResume"); - // TODO this is probably unnecessary - Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fragment != null && fragment instanceof FileDetailFragment) { - ((FileDetailFragment) fragment).updateFileDetails(false, false); - } - - // Listen for download messages - IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE); - downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE); - mDownloadFinishReceiver = new DownloadFinishReceiver(); - registerReceiver(mDownloadFinishReceiver, downloadIntentFilter); - } - - - /** Defines callbacks for service binding, passed to bindService() */ - private class DetailsServiceConnection implements ServiceConnection { - - @Override - public void onServiceConnected(ComponentName component, IBinder service) { - - if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) { - Log_OC.d(TAG, "Download service connected"); - mDownloaderBinder = (FileDownloaderBinder) service; - if (mWaitingToPreview) { - requestForDownload(); - } - - } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) { - Log_OC.d(TAG, "Upload service connected"); - mUploaderBinder = (FileUploaderBinder) service; - } else { - return; - } - - Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null; - if (detailsFragment != null) { - detailsFragment.listenForTransferProgress(); - detailsFragment.updateFileDetails(mWaitingToPreview, false); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais()) - } - } - - @Override - public void onServiceDisconnected(ComponentName component) { - if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) { - Log_OC.d(TAG, "Download service disconnected"); - mDownloaderBinder = null; - } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) { - Log_OC.d(TAG, "Upload service disconnected"); - mUploaderBinder = null; - } - } - }; - - - @Override - public void onDestroy() { - super.onDestroy(); - Log_OC.e(TAG, "onDestroy"); - if (mDownloadConnection != null) { - unbindService(mDownloadConnection); - mDownloadConnection = null; - } - if (mUploadConnection != null) { - unbindService(mUploadConnection); - mUploadConnection = null; - } - } - - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - boolean returnValue = false; - - switch(item.getItemId()){ - case android.R.id.home: - backToDisplayActivity(true); - returnValue = true; - break; - default: - returnValue = super.onOptionsItemSelected(item); - } - - return returnValue; - } - - @Override - public void onBackPressed() { - backToDisplayActivity(true); - } - - private void backToDisplayActivity(boolean moveToParent) { - Intent intent = new Intent(this, FileDisplayActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - OCFile targetFile = null; - OCFile file = getFile(); - if (file != null) { - targetFile = moveToParent ? mStorageManager.getFileById(file.getParentId()) : file; - } - intent.putExtra(EXTRA_FILE, targetFile); - intent.putExtra(EXTRA_ACCOUNT, getAccount()); - startActivity(intent); - finish(); - } - - @Override - protected Dialog onCreateDialog(int id) { - Dialog dialog = null; - switch (id) { - case DIALOG_SHORT_WAIT: { - ProgressDialog working_dialog = new ProgressDialog(this); - working_dialog.setMessage(getResources().getString( - R.string.wait_a_moment)); - working_dialog.setIndeterminate(true); - working_dialog.setCancelable(false); - dialog = working_dialog; - break; - } - default: - dialog = null; - } - return dialog; - } - - - /** - * {@inheritDoc} - */ - @Override - public void onFileStateChanged() { - // nothing to do here! - } - - - /** - * {@inheritDoc} - */ - @Override - public FileDownloaderBinder getFileDownloaderBinder() { - return mDownloaderBinder; - } - - - @Override - public FileUploaderBinder getFileUploaderBinder() { - return mUploaderBinder; - } - - - @Override - public void showFragmentWithDetails(OCFile file) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.fragment, new FileDetailFragment(file, getAccount()), FileDetailFragment.FTAG); - transaction.commit(); - } - - - private void requestForDownload() { - if (!mDownloaderBinder.isDownloading(getAccount(), getFile())) { - Intent i = new Intent(this, FileDownloader.class); - i.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount()); - i.putExtra(FileDownloader.EXTRA_FILE, getFile()); - startService(i); - } - } - - - /** - * Class waiting for broadcast events from the {@link FielDownloader} service. - * - * Updates the UI when a download is started or finished, provided that it is relevant for the - * current file. - */ - private class DownloadFinishReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - boolean sameAccount = isSameAccount(context, intent); - String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH); - boolean samePath = (getFile() != null && getFile().getRemotePath().equals(downloadedRemotePath)); - - if (sameAccount && samePath) { - updateChildFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false)); - } - - removeStickyBroadcast(intent); - } - - private boolean isSameAccount(Context context, Intent intent) { - String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME); - return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name)); - } - } - - - public void updateChildFragment(String downloadEvent, String downloadedRemotePath, boolean success) { - Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fragment != null && fragment instanceof FileDetailFragment) { - FileDetailFragment detailsFragment = (FileDetailFragment) fragment; - OCFile fileInFragment = detailsFragment.getFile(); - if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) { - // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver - mWaitingToPreview = false; - - } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) { - // grants that the progress bar is updated - detailsFragment.listenForTransferProgress(); - detailsFragment.updateFileDetails(true, false); - - } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) { - // refresh the details fragment - if (success && mWaitingToPreview) { - setFile(mStorageManager.getFileById(getFile().getFileId())); // update the file from database, for the local storage path - if (PreviewMediaFragment.canBePreviewed(getFile())) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.fragment, new PreviewMediaFragment(getFile(), getAccount(), 0, true), FileDetailFragment.FTAG); - transaction.commit(); - } else { - detailsFragment.updateFileDetails(false, (success)); - openFile(getFile()); - } - mWaitingToPreview = false; - - } else { - detailsFragment.updateFileDetails(false, (success)); - } - } - } // TODO else if (fragment != null && fragment ) - - } - - /** - * {@inheritDoc} - */ - @Override - protected void onAccountChanged() { - mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver()); - - FileFragment fragment = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fragment != null && mStorageManager.getFileById(fragment.getFile().getFileId()) == null) { - /// the account was forced to be changed; probably was deleted from system settings - backToDisplayActivity(false); - } - } - -} diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index cfe5b8ea..210a3d26 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -33,7 +33,6 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.content.SharedPreferences; -import android.content.res.Configuration; import android.content.res.Resources.NotFoundException; import android.database.Cursor; import android.net.Uri; @@ -43,6 +42,7 @@ import android.os.IBinder; import android.preference.PreferenceManager; import android.provider.MediaStore; import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.ViewGroup; @@ -84,7 +84,6 @@ import com.owncloud.android.ui.fragment.FileDetailFragment; import com.owncloud.android.ui.fragment.FileFragment; import com.owncloud.android.ui.fragment.OCFileListFragment; import com.owncloud.android.ui.preview.PreviewImageActivity; -import com.owncloud.android.ui.preview.PreviewImageFragment; import com.owncloud.android.ui.preview.PreviewMediaFragment; import com.owncloud.android.ui.preview.PreviewVideoActivity; @@ -99,7 +98,6 @@ public class FileDisplayActivity extends FileActivity implements OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, OnRemoteOperationListener, EditNameDialogListener { private ArrayAdapter mDirectories; - //private OCFile mCurrentDir = null; private OCFile mChosenFile = null; /** Access point to the cached database for the current ownCloud {@link Account} */ @@ -113,15 +111,17 @@ public class FileDisplayActivity extends FileActivity implements private ServiceConnection mDownloadConnection = null, mUploadConnection = null; private RemoteOperationResult mLastSslUntrustedServerResult = null; - private OCFileListFragment mFileList; - private boolean mDualPane; + private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW"; + public static final int DIALOG_SHORT_WAIT = 0; private static final int DIALOG_CHOOSE_UPLOAD_SOURCE = 1; private static final int DIALOG_SSL_VALIDATOR = 2; private static final int DIALOG_CERT_NOT_SAVED = 3; + public static final String ACTION_DETAILS = "com.owncloud.android.ui.activity.action.DETAILS"; + private static final int ACTION_SELECT_CONTENT_FROM_APPS = 1; private static final int ACTION_SELECT_MULTIPLE_FILES = 2; @@ -130,25 +130,13 @@ public class FileDisplayActivity extends FileActivity implements private OCFile mWaitingToPreview; private Handler mHandler; - private Configuration mNewConfigurationChangeToApplyOnStart; - private boolean mStarted; - @Override protected void onCreate(Bundle savedInstanceState) { Log_OC.d(TAG, "onCreate() start"); super.onCreate(savedInstanceState); - mStarted = false; mHandler = new Handler(); - /// Load of saved instance state: keep this always before initDataFromCurrentAccount() - if(savedInstanceState != null) { - mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(FileDetailActivity.KEY_WAITING_TO_PREVIEW); - - } else { - mWaitingToPreview = null; - } - /// bindings to transference services mUploadConnection = new ListServiceConnection(); mDownloadConnection = new ListServiceConnection(); @@ -165,110 +153,79 @@ public class FileDisplayActivity extends FileActivity implements observer_intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_INIT_OBSERVED_LIST); startService(observer_intent); + /// Load of saved instance state + if(savedInstanceState != null) { + mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW); + + } else { + mWaitingToPreview = null; + } + /// USER INTERFACE requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); - // Drop-down navigation - mDirectories = new CustomArrayAdapter(this, R.layout.sherlock_spinner_dropdown_item); - OCFile currFile = getFile(); - while(mStorageManager != null && currFile != null && currFile.getFileName() != OCFile.PATH_SEPARATOR) { - mDirectories.add(currFile.getFileName()); - currFile = mStorageManager.getFileById(currFile.getParentId()); - } - mDirectories.add(OCFile.PATH_SEPARATOR); - // Inflate and set the layout view setContentView(R.layout.files); - mFileList = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); - mDualPane = (findViewById(R.id.file_details_container) != null); - if (mDualPane && savedInstanceState == null) { - initFileDetailsInDualPane(); + mDualPane = (findViewById(R.id.right_fragment_container) != null); + if (savedInstanceState == null) { + Fragment secondFragment = chooseSecondFragment(mChosenFile); + mChosenFile = null; + initFragments(secondFragment); } - + // Action bar setup + mDirectories = new CustomArrayAdapter(this, R.layout.sherlock_spinner_dropdown_item); + OCFile currFile = getFile(); + if (mStorageManager != null) { + while(currFile != null && currFile.getFileName() != OCFile.PATH_SEPARATOR) { + mDirectories.add(currFile.getFileName()); + currFile = mStorageManager.getFileById(currFile.getParentId()); + } + } + mDirectories.add(OCFile.PATH_SEPARATOR); ActionBar actionBar = getSupportActionBar(); actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation - actionBar.setDisplayHomeAsUpEnabled(getFile() != null && getFile().getParentId() != 0); - actionBar.setDisplayShowTitleEnabled(false); - actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); - actionBar.setListNavigationCallbacks(mDirectories, this); setSupportProgressBarIndeterminateVisibility(false); // always AFTER setContentView(...) ; to workaround bug in its implementation Log_OC.d(TAG, "onCreate() end"); } - @Override - public void onConfigurationChanged (Configuration newConfig) { - super.onConfigurationChanged(newConfig); - if (mStarted) { - checkConfigurationChange(newConfig); - } else { - mNewConfigurationChangeToApplyOnStart = newConfig; + private void initFragments(Fragment secondFragment) { + FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); + if (!mDualPane) { + transaction.add(R.id.single_fragment_container, new OCFileListFragment()); + } // else: for dual pane, the fragment for list of files is inflated directly from the layout ; see res\larg-land\files.xml + + /// Second fragment + if (secondFragment != null) { + if (mDualPane) { + transaction.add(R.id.right_fragment_container, secondFragment); + } else { + transaction.replace(R.id.single_fragment_container, secondFragment); + transaction.addToBackStack(null); + } } + transaction.commit(); } - - - private void initFileDetailsInDualPane() { - if (mDualPane && getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG) == null) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - if (mChosenFile != null) { - if (!mChosenFile.isDown()) { - transaction.replace(R.id.file_details_container, new FileDetailFragment(mChosenFile, getAccount()), FileDetailFragment.FTAG); - if (getIntent().getBooleanExtra(EXTRA_WAITING_TO_PREVIEW, false)) { - mWaitingToPreview = mChosenFile; - } - } else if (PreviewMediaFragment.canBePreviewed(mChosenFile)) { - int startPlaybackPosition = getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0); - boolean autoplay = getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true); - transaction.replace(R.id.file_details_container, new PreviewMediaFragment(mChosenFile, getAccount(), startPlaybackPosition, autoplay), FileDetailFragment.FTAG); - } - mChosenFile = null; + + + private Fragment chooseSecondFragment(OCFile file) { + Fragment secondFragment = null; + if (file != null) { + if (file.isDown() && PreviewMediaFragment.canBePreviewed(file)) { + int startPlaybackPosition = getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0); + boolean autoplay = getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true); + secondFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition, autoplay); } else { - transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment + secondFragment = new FileDetailFragment(file, getAccount()); } - transaction.commit(); - } - } - - - @Override - protected void onStart() { - super.onStart(); - if (mNewConfigurationChangeToApplyOnStart != null) { - checkConfigurationChange(mNewConfigurationChangeToApplyOnStart); - mNewConfigurationChangeToApplyOnStart = null; - } - mStarted = true; - } - - @Override - protected void onStop() { - super.onStop(); - mStarted = false; - } - - - private void checkConfigurationChange(Configuration newConfig) { - FileFragment fragment = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fragment != null - && fragment.getFile() != null - && (newConfig.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE - && newConfig.orientation != Configuration.ORIENTATION_LANDSCAPE) { - - onFileClick(fragment.getFile(), true); - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.remove((Fragment)fragment); - transaction.commit(); - - } else { - finish(); - Intent intent = new Intent(this, FileDisplayActivity.class); - intent.putExtra(EXTRA_FILE, getFile()); - intent.putExtra(EXTRA_ACCOUNT, getAccount()); - startActivity(intent); + + } else if (mDualPane) { + secondFragment = new FileDetailFragment(null, null); // empty FileDetailFragment } + return secondFragment; } @@ -313,7 +270,9 @@ public class FileDisplayActivity extends FileActivity implements break; } case android.R.id.home: { - if(getFile() != null && getFile().getParentId() != 0){ + FileFragment second = getSecondFragment(); + if((getFile() != null && getFile().getParentId() != 0) || + (second != null && second.getFile() != null)) { onBackPressed(); } break; @@ -387,7 +346,7 @@ public class FileDisplayActivity extends FileActivity implements startService(i); } else { - Log_OC.d("FileDisplay", "User clicked on 'Update' with no selection"); + Log_OC.d(TAG, "User clicked on 'Update' with no selection"); Toast t = Toast.makeText(this, getString(R.string.filedisplay_no_file_selected), Toast.LENGTH_LONG); t.show(); return; @@ -409,12 +368,12 @@ public class FileDisplayActivity extends FileActivity implements filepath = filemanagerstring; } catch (Exception e) { - Log_OC.e("FileDisplay", "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e); + Log_OC.e(TAG, "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e); e.printStackTrace(); } finally { if (filepath == null) { - Log_OC.e("FileDisplay", "Couldnt resolve path to file"); + Log_OC.e(TAG, "Couldnt resolve path to file"); Toast t = Toast.makeText(this, getString(R.string.filedisplay_unexpected_bad_get_content), Toast.LENGTH_LONG); t.show(); return; @@ -440,55 +399,84 @@ public class FileDisplayActivity extends FileActivity implements startService(i); } + private OCFileListFragment getListOfFilesFragment() { + if (mDualPane) { + return (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.list_of_files_fragment); + } else { + Fragment singleFragment = getSupportFragmentManager().findFragmentById(R.id.single_fragment_container); + if (singleFragment instanceof OCFileListFragment) { + return (OCFileListFragment)singleFragment; + } else { + return null; + } + } + } + + protected FileFragment getSecondFragment() { + if (mDualPane) { + return (FileFragment) getSupportFragmentManager().findFragmentById(R.id.right_fragment_container); + } else { + Fragment singleFragment = getSupportFragmentManager().findFragmentById(R.id.single_fragment_container); + if (singleFragment != null && !(singleFragment instanceof OCFileListFragment)) { + return (FileFragment)singleFragment; + } else { + return null; + } + } + } @Override public void onBackPressed() { - if (mDirectories.getCount() <= 1) { - finish(); - return; + OCFileListFragment listOfFiles = getListOfFilesFragment(); + if (listOfFiles != null) { + if (mDirectories.getCount() <= 1) { + finish(); + return; + } + popDirname(); + listOfFiles.onBrowseUp(); + setFile(listOfFiles.getCurrentFile()); + cleanSecondFragment(); + + } else { + super.onBackPressed(); } - popDirname(); - mFileList.onNavigateUp(); - setFile(mFileList.getCurrentFile()); - + updateNavigationElementsInActionBar(getFile(), null); + } + + private void cleanSecondFragment() { if (mDualPane) { - // Resets the FileDetailsFragment on Tablets so that it always displays - Fragment fileFragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); + // Resets the FileDetailsFragment on tablets + Fragment fileFragment = getSupportFragmentManager().findFragmentById(R.id.right_fragment_container); if (fileFragment != null && (fileFragment instanceof PreviewMediaFragment || !((FileDetailFragment) fileFragment).isEmpty())) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment + transaction.replace(R.id.right_fragment_container, new FileDetailFragment(null, null)); // empty FileDetailFragment transaction.commit(); } } - - if(getFile().getParentId() == 0){ - ActionBar actionBar = getSupportActionBar(); - actionBar.setDisplayHomeAsUpEnabled(false); - } } + @Override protected void onSaveInstanceState(Bundle outState) { // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved - Log_OC.d(TAG, "onSaveInstanceState() start"); + Log_OC.e(TAG, "onSaveInstanceState() start"); super.onSaveInstanceState(outState); - if (mDualPane) { - FileFragment fragment = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fragment != null) { - OCFile file = fragment.getFile(); - if (file != null) { - outState.putParcelable(EXTRA_FILE, file); - } - } - } - outState.putParcelable(FileDetailActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview); + outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview); Log_OC.d(TAG, "onSaveInstanceState() end"); } @Override + protected void onStart() { + super.onStart(); + FileFragment second = getSecondFragment(); + updateNavigationElementsInActionBar(getFile(), (second == null) ? null : second.getFile()); + } + + @Override protected void onResume() { - Log_OC.d(TAG, "onResume() start"); super.onResume(); + Log_OC.e(TAG, "onResume() start"); // Listen for sync messages IntentFilter syncIntentFilter = new IntentFilter(FileSyncService.SYNC_MESSAGE); @@ -507,7 +495,10 @@ public class FileDisplayActivity extends FileActivity implements registerReceiver(mDownloadFinishReceiver, downloadIntentFilter); // List current directory - mFileList.listDirectory(getFile()); // TODO we should find the way to avoid the need of this (maybe it's not necessary yet; to check) + OCFileListFragment listOfFiles = getListOfFilesFragment(); + if (listOfFiles != null) { + listOfFiles.listDirectory(getFile()); // TODO we should find the way to avoid the need of this (maybe it's not necessary yet; to check) + } Log_OC.d(TAG, "onResume() end"); } @@ -515,8 +506,8 @@ public class FileDisplayActivity extends FileActivity implements @Override protected void onPause() { - Log_OC.d(TAG, "onPause() start"); super.onPause(); + Log_OC.e(TAG, "onPause() start"); if (mSyncBroadcastReceiver != null) { unregisterReceiver(mSyncBroadcastReceiver); mSyncBroadcastReceiver = null; @@ -702,7 +693,7 @@ public class FileDisplayActivity extends FileActivity implements boolean inProgress = intent.getBooleanExtra(FileSyncService.IN_PROGRESS, false); String accountName = intent.getStringExtra(FileSyncService.ACCOUNT_NAME); - Log_OC.d("FileDisplay", "sync of account " + accountName + " is in_progress: " + inProgress); + Log_OC.d(TAG, "sync of account " + accountName + " is in_progress: " + inProgress); if (getAccount() != null && accountName.equals(getAccount().name)) { @@ -719,8 +710,7 @@ public class FileDisplayActivity extends FileActivity implements || fillBlankRoot ) { if (!fillBlankRoot) currentDir = getStorageManager().getFileByPath(synchFolderRemotePath); - OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager() - .findFragmentById(R.id.fileList); + OCFileListFragment fileListFragment = getListOfFilesFragment(); if (fileListFragment != null) { fileListFragment.listDirectory(currentDir); } @@ -756,10 +746,7 @@ public class FileDisplayActivity extends FileActivity implements boolean sameAccount = getAccount() != null && accountName.equals(getAccount().name); boolean isDescendant = (getFile() != null) && (uploadedRemotePath != null) && (uploadedRemotePath.startsWith(getFile().getRemotePath())); if (sameAccount && isDescendant) { - OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); - if (fileListFragment != null) { - fileListFragment.listDirectory(); - } + refeshListOfFilesFragment(); } } @@ -780,10 +767,8 @@ public class FileDisplayActivity extends FileActivity implements boolean isDescendant = isDescendant(downloadedRemotePath); if (sameAccount && isDescendant) { - updateLeftPanel(); - if (mDualPane) { - updateRightPanel(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false)); - } + refeshListOfFilesFragment(); + updateSecondFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false)); } removeStickyBroadcast(intent); @@ -800,18 +785,18 @@ public class FileDisplayActivity extends FileActivity implements } - protected void updateLeftPanel() { - OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); + protected void refeshListOfFilesFragment() { + OCFileListFragment fileListFragment = getListOfFilesFragment(); if (fileListFragment != null) { fileListFragment.listDirectory(); } } - protected void updateRightPanel(String downloadEvent, String downloadedRemotePath, boolean success) { - Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); + protected void updateSecondFragment(String downloadEvent, String downloadedRemotePath, boolean success) { + FileFragment secondFragment = getSecondFragment(); boolean waitedPreview = (mWaitingToPreview != null && mWaitingToPreview.getRemotePath().equals(downloadedRemotePath)); - if (fragment != null && fragment instanceof FileDetailFragment) { - FileDetailFragment detailsFragment = (FileDetailFragment) fragment; + if (secondFragment != null && secondFragment instanceof FileDetailFragment) { + FileDetailFragment detailsFragment = (FileDetailFragment) secondFragment; OCFile fileInFragment = detailsFragment.getFile(); if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) { // the user browsed to other file ; forget the automatic preview @@ -827,13 +812,11 @@ public class FileDisplayActivity extends FileActivity implements if (success && waitedPreview) { mWaitingToPreview = mStorageManager.getFileById(mWaitingToPreview.getFileId()); // update the file from database, for the local storage path if (PreviewMediaFragment.canBePreviewed(mWaitingToPreview)) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new PreviewMediaFragment(mWaitingToPreview, getAccount(), 0, true), FileDetailFragment.FTAG); - transaction.commit(); + startMediaPreview(mWaitingToPreview, 0, true); + } else { detailsFragment.updateFileDetails(false, (success)); openFile(mWaitingToPreview); - } mWaitingToPreview = null; @@ -856,121 +839,121 @@ public class FileDisplayActivity extends FileActivity implements /** * {@inheritDoc} + * + * Updates action bar and second fragment, if in dual pane mode. */ @Override - public void onDirectoryClick(OCFile directory) { + public void onBrowsedDownTo(OCFile directory) { pushDirname(directory); - ActionBar actionBar = getSupportActionBar(); - actionBar.setDisplayHomeAsUpEnabled(true); - - if (mDualPane) { - // Resets the FileDetailsFragment on Tablets so that it always displays - Fragment fileFragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fileFragment != null && (fileFragment instanceof PreviewMediaFragment || !((FileDetailFragment) fileFragment).isEmpty())) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment - transaction.commit(); - } - } + cleanSecondFragment(); + updateNavigationElementsInActionBar(directory, null); } - /** - * {@inheritDoc} + * Opens the image gallery showing the image {@link OCFile} received as parameter. + * + * @param file Image {@link OCFile} to show. */ @Override - public void onFileClick(OCFile file, boolean onOrientationChange) { - if (file != null) { - if (PreviewImageFragment.canBePreviewed(file)) { - // preview image - it handles the download, if needed - startPreviewImage(file); - - } else if (PreviewMediaFragment.canBePreviewed(file)) { - if (file.isDown()) { - // general preview - if (!onOrientationChange) { - startMediaPreview(file, 0, true, onOrientationChange); - } else { - int startPlaybackPosition = 0; - boolean autoplay = true; - Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fragment != null && file.isVideo()) { - PreviewMediaFragment videoFragment = (PreviewMediaFragment)fragment; - startPlaybackPosition = videoFragment.getPosition(); - autoplay = videoFragment.isPlaying(); - } - startMediaPreview(file, startPlaybackPosition, autoplay, onOrientationChange); - } - - } else { - // automatic download, preview on finish - startDownloadForPreview(file, onOrientationChange); - - } - - } else if (file.isDown() && !onOrientationChange) { - openFile(file); - - } else { - startDownloadForPreview(file, onOrientationChange); - } - } - } - - private void startPreviewImage(OCFile file) { + public void startImagePreview(OCFile file) { Intent showDetailsIntent = new Intent(this, PreviewImageActivity.class); showDetailsIntent.putExtra(EXTRA_FILE, file); showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount()); startActivity(showDetailsIntent); } - private void startMediaPreview(OCFile file, int startPlaybackPosition, boolean autoplay, boolean onOrientationChange) { - if (mDualPane && !onOrientationChange) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new PreviewMediaFragment(file, getAccount(), startPlaybackPosition, autoplay), FileDetailFragment.FTAG); - transaction.commit(); - - } else { - Intent showDetailsIntent = new Intent(this, FileDetailActivity.class); - showDetailsIntent.putExtra(EXTRA_FILE, file); - showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount()); - showDetailsIntent.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, startPlaybackPosition); - showDetailsIntent.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, autoplay); - startActivity(showDetailsIntent); - } + /** + * Stars the preview of an already down media {@link OCFile}. + * + * @param file Media {@link OCFile} to preview. + * @param startPlaybackPosition Media position where the playback will be started, in milliseconds. + * @param autoplay When 'true', the playback will start without user interactions. + */ + @Override + public void startMediaPreview(OCFile file, int startPlaybackPosition, boolean autoplay) { + Fragment mediaFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition, autoplay); + setSecondFragment(mediaFragment); + updateNavigationElementsInActionBar(getFile(), file); } - private void startDownloadForPreview(OCFile file, boolean onOrientationChange) { - if (mDualPane && !onOrientationChange) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(file, getAccount()), FileDetailFragment.FTAG); - transaction.commit(); - mWaitingToPreview = file; - requestForDownload(); - + /** + * Requests the download of the received {@link OCFile} , updates the UI + * to monitor the download progress and prepares the activity to preview + * or open the file when the download finishes. + * + * @param file {@link OCFile} to download and preview. + */ + @Override + public void startDownloadForPreview(OCFile file) { + Fragment detailFragment = new FileDetailFragment(file, getAccount()); + setSecondFragment(detailFragment); + mWaitingToPreview = file; + requestForDownload(); + updateNavigationElementsInActionBar(getFile(), file); + } + + + /** + * Shows the information of the {@link OCFile} received as a + * parameter in the second fragment. + * + * @param file {@link OCFile} whose details will be shown + */ + @Override + public void showDetails(OCFile file) { + Fragment detailFragment = new FileDetailFragment(file, getAccount()); + setSecondFragment(detailFragment); + updateNavigationElementsInActionBar(getFile(), file); + } + + + /** + * Replaces the second fragment managed by the activity with the received as + * a parameter. + * + * Assumes never will be more than two fragments managed at the same time. + * + * @param fragment New second Fragment to set. + */ + private void setSecondFragment(Fragment fragment) { + FragmentManager fm = getSupportFragmentManager(); + FragmentTransaction transaction = fm.beginTransaction(); + if (mDualPane) { + transaction.replace(R.id.right_fragment_container, fragment); } else { - Intent showDetailsIntent = new Intent(this, FileDetailActivity.class); - showDetailsIntent.putExtra(EXTRA_FILE, file); - showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount()); - startActivity(showDetailsIntent); + transaction.replace(R.id.single_fragment_container, fragment); + while (fm.getBackStackEntryCount() > 0) { + fm.popBackStackImmediate(); + } + transaction.addToBackStack(null); } + transaction.commit(); + + /// TODO UPDATE ACTION BAR ACCORDING TO SECONDFRAGMENT!! } - - private void startDetails(OCFile file, boolean onOrientationChange) { - if (mDualPane && !file.isImage() && !onOrientationChange) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(file, getAccount()), FileDetailFragment.FTAG); - transaction.commit(); + + /** + * TODO + */ + private void updateNavigationElementsInActionBar(OCFile currentDir, OCFile currentFile) { + ActionBar actionBar = getSupportActionBar(); + if (currentFile == null) { + // only list of files - set for browsing through folders + actionBar.setDisplayHomeAsUpEnabled(currentDir != null && currentDir.getParentId() != 0); + actionBar.setDisplayShowTitleEnabled(false); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); + actionBar.setListNavigationCallbacks(mDirectories, this); // assuming mDirectoris is updated + } else { - Intent showDetailsIntent = new Intent(this, FileDetailActivity.class); - showDetailsIntent.putExtra(EXTRA_FILE, file); - showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount()); - startActivity(showDetailsIntent); + actionBar.setDisplayHomeAsUpEnabled(true); + actionBar.setDisplayShowTitleEnabled(true); + actionBar.setTitle(currentFile.getFileName()); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); } } - - + + /** * {@inheritDoc} */ @@ -985,10 +968,7 @@ public class FileDisplayActivity extends FileActivity implements */ @Override public void onFileStateChanged() { - OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); - if (fileListFragment != null) { - fileListFragment.listDirectory(); - } + refeshListOfFilesFragment(); } @@ -1029,15 +1009,15 @@ public class FileDisplayActivity extends FileActivity implements return; } // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS - if (mFileList != null) - mFileList.listDirectory(); - if (mDualPane) { - Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fragment != null && fragment instanceof FileDetailFragment) { - FileDetailFragment detailFragment = (FileDetailFragment)fragment; - detailFragment.listenForTransferProgress(); - detailFragment.updateFileDetails(false, false); - } + OCFileListFragment listOfFiles = getListOfFilesFragment(); + if (listOfFiles != null) { + listOfFiles.listDirectory(); + } + FileFragment secondFragment = getSecondFragment(); + if (secondFragment != null && secondFragment instanceof FileDetailFragment) { + FileDetailFragment detailFragment = (FileDetailFragment)secondFragment; + detailFragment.listenForTransferProgress(); + detailFragment.updateFileDetails(false, false); } } @@ -1119,16 +1099,15 @@ public class FileDisplayActivity extends FileActivity implements Toast msg = Toast.makeText(this, R.string.remove_success_msg, Toast.LENGTH_LONG); msg.show(); OCFile removedFile = operation.getFile(); + getSecondFragment(); if (mDualPane) { - FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (details != null && removedFile.equals(details.getFile())) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment - transaction.commit(); + FileFragment second = getSecondFragment(); + if (second != null && removedFile.equals(second.getFile())) { + cleanSecondFragment(); // TODO this only cleans in DUAL PANE!! } } if (mStorageManager.getFileById(removedFile.getParentId()).equals(getFile())) { - mFileList.listDirectory(); + refeshListOfFilesFragment(); } } else { @@ -1150,7 +1129,7 @@ public class FileDisplayActivity extends FileActivity implements private void onCreateFolderOperationFinish(CreateFolderOperation operation, RemoteOperationResult result) { if (result.isSuccess()) { dismissDialog(DIALOG_SHORT_WAIT); - mFileList.listDirectory(); + refeshListOfFilesFragment(); } else { dismissDialog(DIALOG_SHORT_WAIT); @@ -1177,13 +1156,13 @@ public class FileDisplayActivity extends FileActivity implements OCFile renamedFile = operation.getFile(); if (result.isSuccess()) { if (mDualPane) { - FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); + FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentById(R.id.right_fragment_container); if (details != null && details instanceof FileDetailFragment && renamedFile.equals(details.getFile()) ) { ((FileDetailFragment) details).updateFileDetails(renamedFile, getAccount()); } } if (mStorageManager.getFileById(renamedFile.getParentId()).equals(getFile())) { - mFileList.listDirectory(); + refeshListOfFilesFragment(); } } else { @@ -1220,7 +1199,7 @@ public class FileDisplayActivity extends FileActivity implements } else { if (operation.transferWasRequested()) { - mFileList.listDirectory(); + refeshListOfFilesFragment(); onTransferStateChanged(syncedFile, true, true); } else { @@ -1236,12 +1215,8 @@ public class FileDisplayActivity extends FileActivity implements */ @Override public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) { - /*OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); - if (fileListFragment != null) { - fileListFragment.listDirectory(); - }*/ if (mDualPane) { - FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); + FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentById(R.id.right_fragment_container); if (details != null && details instanceof FileDetailFragment && file.equals(details.getFile()) ) { if (downloading || uploading) { ((FileDetailFragment)details).updateFileDetails(file, getAccount()); @@ -1253,22 +1228,6 @@ public class FileDisplayActivity extends FileActivity implements } - @Override - public void showFragmentWithDetails(OCFile file) { - if (mDualPane) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(file, getAccount()), FileDetailFragment.FTAG); - transaction.commit(); - - } else { - Intent showDetailsIntent = new Intent(this, FileDetailActivity.class); - showDetailsIntent.putExtra(EXTRA_FILE, file); - showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount()); - showDetailsIntent.putExtra(FileDetailActivity.EXTRA_MODE, FileDetailActivity.MODE_DETAILS); - startActivity(showDetailsIntent); - } - } - public void onDismiss(EditNameDialog dialog) { if (dialog.getResult()) { String newDirectoryName = dialog.getNewFilename().trim(); diff --git a/src/com/owncloud/android/ui/fragment/ExtendedListFragment.java b/src/com/owncloud/android/ui/fragment/ExtendedListFragment.java new file mode 100644 index 00000000..3e519a8b --- /dev/null +++ b/src/com/owncloud/android/ui/fragment/ExtendedListFragment.java @@ -0,0 +1,118 @@ +/* ownCloud Android client application + * Copyright (C) 2012 Bartek Przybylski + * Copyright (C) 2012-2013 ownCloud Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.owncloud.android.ui.fragment; + +import com.actionbarsherlock.app.SherlockFragment; +import com.owncloud.android.Log_OC; +import com.owncloud.android.R; +import com.owncloud.android.ui.ExtendedListView; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ListAdapter; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ListView; + +/** + * TODO extending SherlockListFragment instead of SherlockFragment + */ +public class ExtendedListFragment extends SherlockFragment implements OnItemClickListener { + + private static final String TAG = ExtendedListFragment.class.getSimpleName(); + + private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION"; + + protected ExtendedListView mList; + + public void setListAdapter(ListAdapter listAdapter) { + mList.setAdapter(listAdapter); + mList.invalidate(); + } + + public ListView getListView() { + return mList; + } + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + Log_OC.e(TAG, "onCreateView"); + //mList = new ExtendedListView(getActivity()); + View v = inflater.inflate(R.layout.list_fragment, null); + mList = (ExtendedListView)(v.findViewById(R.id.list_root)); + mList.setOnItemClickListener(this); + //mList.setEmptyView(v.findViewById(R.id.empty_list_view)); // looks like it's not a cool idea + mList.setDivider(getResources().getDrawable(R.drawable.uploader_list_separator)); + mList.setDividerHeight(1); + + if (savedInstanceState != null) { + int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION); + setReferencePosition(referencePosition); + } + + return v; + } + + + @Override + public void onSaveInstanceState(Bundle savedInstanceState) { + super.onSaveInstanceState(savedInstanceState); + Log_OC.e(TAG, "onSaveInstanceState()"); + savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition()); + } + + + /** + * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when + * the device is turned to other position. + * + * THe current policy is take as a reference the visible item in the center of the screen. + * + * @return The position in the list of the visible item in the center of the screen. + */ + protected int getReferencePosition() { + if (mList != null) { + return (mList.getFirstVisiblePosition() + mList.getLastVisiblePosition()) / 2; + } else { + return 0; + } + } + + + /** + * Sets the visible part of the list from the reference position. + * + * @param position Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()} + */ + protected void setReferencePosition(int position) { + if (mList != null) { + mList.setAndCenterSelection(position); + } + } + + @Override + public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { + // to be @overriden + } + + +} diff --git a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java index 96dc7098..33b6479c 100644 --- a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java @@ -41,7 +41,6 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; -import com.actionbarsherlock.app.SherlockFragment; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; @@ -62,7 +61,6 @@ import com.owncloud.android.operations.RenameFileOperation; import com.owncloud.android.operations.SynchronizeFileOperation; import com.owncloud.android.ui.activity.ConflictsResolveActivity; import com.owncloud.android.ui.activity.FileActivity; -import com.owncloud.android.ui.activity.FileDetailActivity; import com.owncloud.android.ui.activity.FileDisplayActivity; import com.owncloud.android.ui.dialog.EditNameDialog; import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener; @@ -77,16 +75,14 @@ import eu.alefzero.webdav.OnDatatransferProgressListener; * @author Bartek Przybylski * @author David A. Velasco */ -public class FileDetailFragment extends SherlockFragment implements +public class FileDetailFragment extends FileFragment implements OnClickListener, - ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener, EditNameDialogListener, - FileFragment { + ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener, EditNameDialogListener { private FileFragment.ContainerActivity mContainerActivity; private int mLayout; private View mView; - private OCFile mFile; private Account mAccount; private FileDataStorageManager mStorageManager; @@ -97,7 +93,6 @@ public class FileDetailFragment extends SherlockFragment implements private RemoteOperation mLastRemoteOperation; private static final String TAG = FileDetailFragment.class.getSimpleName(); - public static final String FTAG = "FileDetails"; public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT"; @@ -107,7 +102,7 @@ public class FileDetailFragment extends SherlockFragment implements * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically. */ public FileDetailFragment() { - mFile = null; + super(); mAccount = null; mStorageManager = null; mLayout = R.layout.file_details_empty; @@ -123,7 +118,7 @@ public class FileDetailFragment extends SherlockFragment implements * @param ocAccount An ownCloud account; needed to start downloads */ public FileDetailFragment(OCFile fileToDetail, Account ocAccount) { - mFile = fileToDetail; + super(fileToDetail); mAccount = ocAccount; mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment mLayout = R.layout.file_details_empty; @@ -145,11 +140,11 @@ public class FileDetailFragment extends SherlockFragment implements super.onCreateView(inflater, container, savedInstanceState); if (savedInstanceState != null) { - mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE); + setFile((OCFile)savedInstanceState.getParcelable(FileActivity.EXTRA_FILE)); mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT); } - if(mFile != null && mAccount != null) { + if(getFile() != null && mAccount != null) { mLayout = R.layout.file_details_fragment; } @@ -198,7 +193,7 @@ public class FileDetailFragment extends SherlockFragment implements @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); - outState.putParcelable(FileActivity.EXTRA_FILE, mFile); + outState.putParcelable(FileActivity.EXTRA_FILE, getFile()); outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount); } @@ -265,11 +260,12 @@ public class FileDetailFragment extends SherlockFragment implements List toHide = new ArrayList(); List toShow = new ArrayList(); + OCFile file = getFile(); FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); - boolean downloading = downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile); + boolean downloading = downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file); FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder(); - boolean uploading = uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile); + boolean uploading = uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile()); if (downloading || uploading) { toHide.add(R.id.action_download_file); @@ -284,7 +280,7 @@ public class FileDetailFragment extends SherlockFragment implements toShow.add(R.id.action_cancel_download); } - } else if (mFile != null && mFile.isDown()) { + } else if (file != null && file.isDown()) { toHide.add(R.id.action_download_file); toHide.add(R.id.action_cancel_download); toHide.add(R.id.action_cancel_upload); @@ -294,7 +290,7 @@ public class FileDetailFragment extends SherlockFragment implements toShow.add(R.id.action_open_file_with); toShow.add(R.id.action_sync_file); - } else if (mFile != null) { + } else if (file != null) { toHide.add(R.id.action_open_file_with); toHide.add(R.id.action_cancel_download); toHide.add(R.id.action_cancel_upload); @@ -340,7 +336,7 @@ public class FileDetailFragment extends SherlockFragment implements public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_open_file_with: { - mContainerActivity.openFile(mFile); + mContainerActivity.openFile(getFile()); return true; } case R.id.action_remove_file: { @@ -382,8 +378,9 @@ public class FileDetailFragment extends SherlockFragment implements private void toggleKeepInSync() { CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync); - mFile.setKeepInSync(cb.isChecked()); - mStorageManager.saveFile(mFile); + OCFile file = getFile(); + file.setKeepInSync(cb.isChecked()); + mStorageManager.saveFile(file); /// register the OCFile instance in the observer service to monitor local updates; /// if necessary, the file is download @@ -393,22 +390,23 @@ public class FileDetailFragment extends SherlockFragment implements (cb.isChecked()? FileObserverService.CMD_ADD_OBSERVED_FILE: FileObserverService.CMD_DEL_OBSERVED_FILE)); - intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, mFile); + intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, file); intent.putExtra(FileObserverService.KEY_CMD_ARG_ACCOUNT, mAccount); getActivity().startService(intent); - if (mFile.keepInSync()) { + if (file.keepInSync()) { synchronizeFile(); // force an immediate synchronization } } private void removeFile() { + OCFile file = getFile(); ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance( R.string.confirmation_remove_alert, - new String[]{mFile.getFileName()}, - mFile.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote, - mFile.isDown() ? R.string.confirmation_remove_local : -1, + new String[]{file.getFileName()}, + file.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote, + file.isDown() ? R.string.confirmation_remove_local : -1, R.string.common_cancel); confDialog.setOnConfirmationListener(this); confDialog.show(getFragmentManager(), FTAG_CONFIRMATION); @@ -416,66 +414,69 @@ public class FileDetailFragment extends SherlockFragment implements private void renameFile() { - String fileName = mFile.getFileName(); - int extensionStart = mFile.isDirectory() ? -1 : fileName.lastIndexOf("."); + OCFile file = getFile(); + String fileName = file.getFileName(); + int extensionStart = file.isDirectory() ? -1 : fileName.lastIndexOf("."); int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length(); EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this); dialog.show(getFragmentManager(), "nameeditdialog"); } private void synchronizeFile() { + OCFile file = getFile(); FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder(); - if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) { - downloaderBinder.cancel(mAccount, mFile); - if (mFile.isDown()) { + if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) { + downloaderBinder.cancel(mAccount, file); + if (file.isDown()) { setButtonsForDown(); } else { setButtonsForRemote(); } - } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile)) { - uploaderBinder.cancel(mAccount, mFile); - if (!mFile.fileExists()) { + } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) { + uploaderBinder.cancel(mAccount, file); + if (!file.fileExists()) { // TODO make something better if (getActivity() instanceof FileDisplayActivity) { // double pane FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FTAG); // empty FileDetailFragment + transaction.replace(R.id.right_fragment_container, new FileDetailFragment(null, null)); // empty FileDetailFragment transaction.commit(); mContainerActivity.onFileStateChanged(); } else { getActivity().finish(); } - } else if (mFile.isDown()) { + } else if (file.isDown()) { setButtonsForDown(); } else { setButtonsForRemote(); } } else { - mLastRemoteOperation = new SynchronizeFileOperation(mFile, null, mStorageManager, mAccount, true, false, getActivity()); + mLastRemoteOperation = new SynchronizeFileOperation(file, null, mStorageManager, mAccount, true, false, getActivity()); mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity()); // update ui boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; - getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); + getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT); } } @Override public void onConfirmation(String callerTag) { + OCFile file = getFile(); if (callerTag.equals(FTAG_CONFIRMATION)) { - if (mStorageManager.getFileById(mFile.getFileId()) != null) { - mLastRemoteOperation = new RemoveFileOperation( mFile, + if (mStorageManager.getFileById(file.getFileId()) != null) { + mLastRemoteOperation = new RemoveFileOperation( file, true, mStorageManager); mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity()); boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; - getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); + getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT); } } } @@ -483,11 +484,12 @@ public class FileDetailFragment extends SherlockFragment implements @Override public void onNeutral(String callerTag) { File f = null; - if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) { + OCFile file = getFile(); + if (file.isDown() && (f = new File(file.getStoragePath())).exists()) { f.delete(); - mFile.setStoragePath(null); - mStorageManager.saveFile(mFile); - updateFileDetails(mFile, mAccount); + file.setStoragePath(null); + mStorageManager.saveFile(file); + updateFileDetails(file, mAccount); } } @@ -503,24 +505,17 @@ public class FileDetailFragment extends SherlockFragment implements * @return True when the fragment was created with the empty layout. */ public boolean isEmpty() { - return (mLayout == R.layout.file_details_empty || mFile == null || mAccount == null); + return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null); } /** - * {@inheritDoc} - */ - public OCFile getFile(){ - return mFile; - } - - /** * Use this method to signal this Activity that it shall update its view. * * @param file : An {@link OCFile} */ public void updateFileDetails(OCFile file, Account ocAccount) { - mFile = file; + setFile(file); if (ocAccount != null && ( mStorageManager == null || (mAccount != null && !mAccount.equals(ocAccount)) @@ -549,30 +544,31 @@ public class FileDetailFragment extends SherlockFragment implements if (readyToShow()) { if (refresh && mStorageManager != null) { - mFile = mStorageManager.getFileByPath(mFile.getRemotePath()); + setFile(mStorageManager.getFileByPath(getFile().getRemotePath())); } + OCFile file = getFile(); // set file details - setFilename(mFile.getFileName()); - setFiletype(mFile.getMimetype()); - setFilesize(mFile.getFileLength()); + setFilename(file.getFileName()); + setFiletype(file.getMimetype()); + setFilesize(file.getFileLength()); if(ocVersionSupportsTimeCreated()){ - setTimeCreated(mFile.getCreationTimestamp()); + setTimeCreated(file.getCreationTimestamp()); } - setTimeModified(mFile.getModificationTimestamp()); + setTimeModified(file.getModificationTimestamp()); CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync); - cb.setChecked(mFile.keepInSync()); + cb.setChecked(file.keepInSync()); // configure UI for depending upon local state of the file //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) { FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder(); - if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))) { + if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))) { setButtonsForTransferring(); - } else if (mFile.isDown()) { + } else if (file.isDown()) { setButtonsForDown(); @@ -590,7 +586,7 @@ public class FileDetailFragment extends SherlockFragment implements * @return 'True' when the fragment is ready to show details of a file */ private boolean readyToShow() { - return (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment); + return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment); } @@ -669,9 +665,9 @@ public class FileDetailFragment extends SherlockFragment implements progressText.setVisibility(View.VISIBLE); FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder(); - if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) { + if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) { progressText.setText(R.string.downloader_download_in_progress_ticker); - } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile)) { + } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) { progressText.setText(R.string.uploader_upload_in_progress_ticker); } } @@ -744,11 +740,11 @@ public class FileDetailFragment extends SherlockFragment implements if (!isEmpty() && accountName.equals(mAccount.name)) { boolean uploadWasFine = intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false); String uploadRemotePath = intent.getStringExtra(FileUploader.EXTRA_REMOTE_PATH); - boolean renamedInUpload = mFile.getRemotePath().equals(intent.getStringExtra(FileUploader.EXTRA_OLD_REMOTE_PATH)); - if (mFile.getRemotePath().equals(uploadRemotePath) || + boolean renamedInUpload = getFile().getRemotePath().equals(intent.getStringExtra(FileUploader.EXTRA_OLD_REMOTE_PATH)); + if (getFile().getRemotePath().equals(uploadRemotePath) || renamedInUpload) { if (uploadWasFine) { - mFile = mStorageManager.getFileByPath(uploadRemotePath); + setFile(mStorageManager.getFileByPath(uploadRemotePath)); } if (renamedInUpload) { String newName = (new File(uploadRemotePath)).getName(); @@ -767,13 +763,13 @@ public class FileDetailFragment extends SherlockFragment implements if (dialog.getResult()) { String newFilename = dialog.getNewFilename(); Log_OC.d(TAG, "name edit dialog dismissed with new name " + newFilename); - mLastRemoteOperation = new RenameFileOperation( mFile, + mLastRemoteOperation = new RenameFileOperation( getFile(), mAccount, newFilename, new FileDataStorageManager(mAccount, getActivity().getContentResolver())); mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity()); boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; - getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); + getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT); } } @@ -799,7 +795,7 @@ public class FileDetailFragment extends SherlockFragment implements private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) { boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; - getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); + getActivity().dismissDialog(FileDisplayActivity.DIALOG_SHORT_WAIT); if (result.isSuccess()) { Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG); @@ -807,7 +803,7 @@ public class FileDetailFragment extends SherlockFragment implements if (inDisplayActivity) { // double pane FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment + transaction.replace(R.id.right_fragment_container, new FileDetailFragment(null, null)); // empty FileDetailFragment transaction.commit(); mContainerActivity.onFileStateChanged(); } else { @@ -825,7 +821,7 @@ public class FileDetailFragment extends SherlockFragment implements private void onRenameFileOperationFinish(RenameFileOperation operation, RemoteOperationResult result) { boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; - getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); + getActivity().dismissDialog(FileDisplayActivity.DIALOG_SHORT_WAIT); if (result.isSuccess()) { updateFileDetails(((RenameFileOperation)operation).getFile(), mAccount); @@ -848,12 +844,12 @@ public class FileDetailFragment extends SherlockFragment implements private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation, RemoteOperationResult result) { boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; - getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); - + getActivity().dismissDialog(FileDisplayActivity.DIALOG_SHORT_WAIT); + OCFile file = getFile(); if (!result.isSuccess()) { if (result.getCode() == ResultCode.SYNC_CONFLICT) { Intent i = new Intent(getActivity(), ConflictsResolveActivity.class); - i.putExtra(ConflictsResolveActivity.EXTRA_FILE, mFile); + i.putExtra(ConflictsResolveActivity.EXTRA_FILE, file); i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mAccount); startActivity(i); @@ -862,7 +858,7 @@ public class FileDetailFragment extends SherlockFragment implements msg.show(); } - if (mFile.isDown()) { + if (file.isDown()) { setButtonsForDown(); } else { @@ -877,7 +873,7 @@ public class FileDetailFragment extends SherlockFragment implements } else { Toast msg = Toast.makeText(getActivity(), R.string.sync_file_nothing_to_do_msg, Toast.LENGTH_LONG); msg.show(); - if (mFile.isDown()) { + if (file.isDown()) { setButtonsForDown(); } else { @@ -891,10 +887,10 @@ public class FileDetailFragment extends SherlockFragment implements public void listenForTransferProgress() { if (mProgressListener != null) { if (mContainerActivity.getFileDownloaderBinder() != null) { - mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, mFile); + mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile()); } if (mContainerActivity.getFileUploaderBinder() != null) { - mContainerActivity.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, mFile); + mContainerActivity.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile()); } } } @@ -903,10 +899,10 @@ public class FileDetailFragment extends SherlockFragment implements public void leaveTransferProgress() { if (mProgressListener != null) { if (mContainerActivity.getFileDownloaderBinder() != null) { - mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, mFile); + mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile()); } if (mContainerActivity.getFileUploaderBinder() != null) { - mContainerActivity.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, mFile); + mContainerActivity.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile()); } } } @@ -946,110 +942,4 @@ public class FileDetailFragment extends SherlockFragment implements }; - /* - // this is a temporary class for sharing purposes, it need to be replaced in transfer service - @SuppressWarnings("unused") - private class ShareRunnable implements Runnable { - private String mPath; - - public ShareRunnable(String path) { - mPath = path; - } - - public void run() { - AccountManager am = AccountManager.get(getActivity()); - Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity()); - OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(account, AccountAuthenticator.KEY_OC_VERSION)); - String url = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + AccountUtils.getWebdavPath(ocv); - - Log_OC.d("share", "sharing for version " + ocv.toString()); - - if (ocv.compareTo(new OwnCloudVersion(0x040000)) >= 0) { - String APPS_PATH = "/apps/files_sharing/"; - String SHARE_PATH = "ajax/share.php"; - - String SHARED_PATH = "/apps/files_sharing/get.php?token="; - - final String WEBDAV_SCRIPT = "webdav.php"; - final String WEBDAV_FILES_LOCATION = "/files/"; - - WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(account, getActivity().getApplicationContext()); - HttpConnectionManagerParams params = new HttpConnectionManagerParams(); - params.setMaxConnectionsPerHost(wc.getHostConfiguration(), 5); - - //wc.getParams().setParameter("http.protocol.single-cookie-header", true); - //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); - - PostMethod post = new PostMethod(am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + APPS_PATH + SHARE_PATH); - - post.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" ); - post.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL)); - List formparams = new ArrayList(); - Log_OC.d("share", mPath+""); - formparams.add(new BasicNameValuePair("sources",mPath)); - formparams.add(new BasicNameValuePair("uid_shared_with", "public")); - formparams.add(new BasicNameValuePair("permissions", "0")); - post.setRequestEntity(new StringRequestEntity(URLEncodedUtils.format(formparams, HTTP.UTF_8))); - - int status; - try { - PropFindMethod find = new PropFindMethod(url+"/"); - find.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL)); - Log_OC.d("sharer", ""+ url+"/"); - - for (org.apache.commons.httpclient.Header a : find.getRequestHeaders()) { - Log_OC.d("sharer-h", a.getName() + ":"+a.getValue()); - } - - int status2 = wc.executeMethod(find); - - Log_OC.d("sharer", "propstatus "+status2); - - GetMethod get = new GetMethod(am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + "/"); - get.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL)); - - status2 = wc.executeMethod(get); - - Log_OC.d("sharer", "getstatus "+status2); - Log_OC.d("sharer", "" + get.getResponseBodyAsString()); - - for (org.apache.commons.httpclient.Header a : get.getResponseHeaders()) { - Log_OC.d("sharer", a.getName() + ":"+a.getValue()); - } - - status = wc.executeMethod(post); - for (org.apache.commons.httpclient.Header a : post.getRequestHeaders()) { - Log_OC.d("sharer-h", a.getName() + ":"+a.getValue()); - } - for (org.apache.commons.httpclient.Header a : post.getResponseHeaders()) { - Log_OC.d("sharer", a.getName() + ":"+a.getValue()); - } - String resp = post.getResponseBodyAsString(); - Log_OC.d("share", ""+post.getURI().toString()); - Log_OC.d("share", "returned status " + status); - Log_OC.d("share", " " +resp); - - if(status != HttpStatus.SC_OK ||resp == null || resp.equals("") || resp.startsWith("false")) { - return; - } - - JSONObject jsonObject = new JSONObject (resp); - String jsonStatus = jsonObject.getString("status"); - if(!jsonStatus.equals("success")) throw new Exception("Error while sharing file status != success"); - - String token = jsonObject.getString("data"); - String uri = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + SHARED_PATH + token; - Log_OC.d("Actions:shareFile ok", "url: " + uri); - - } catch (Exception e) { - e.printStackTrace(); - } - - } else if (ocv.compareTo(new OwnCloudVersion(0x030000)) >= 0) { - - } - } - } - */ - } diff --git a/src/com/owncloud/android/ui/fragment/FileFragment.java b/src/com/owncloud/android/ui/fragment/FileFragment.java index 4e3fc8a3..4c6f3e71 100644 --- a/src/com/owncloud/android/ui/fragment/FileFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileFragment.java @@ -17,9 +17,9 @@ package com.owncloud.android.ui.fragment; -import android.content.Intent; import android.support.v4.app.Fragment; +import com.actionbarsherlock.app.SherlockFragment; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.FileHandler; import com.owncloud.android.ui.activity.TransferServiceGetter; @@ -30,16 +30,43 @@ import com.owncloud.android.ui.activity.TransferServiceGetter; * @author David A. Velasco * */ -public interface FileFragment { +public class FileFragment extends SherlockFragment { + + private OCFile mFile; + + + /** + * Creates an empty fragment. + * + * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically. + */ + public FileFragment() { + mFile = null; + } /** + * Creates an instance for a given {@OCFile}. + * + * @param file + */ + public FileFragment(OCFile file) { + mFile = file; + } + + /** * Getter for the hold {@link OCFile} * * @return The {@link OCFile} hold */ - public OCFile getFile(); + public OCFile getFile() { + return mFile; + } + protected void setFile(OCFile file) { + mFile = file; + } + /** * Interface to implement by any Activity that includes some instance of FileFragment * @@ -66,7 +93,7 @@ public interface FileFragment { * * @param file File to show details */ - public void showFragmentWithDetails(OCFile file); + public void showDetails(OCFile file); } diff --git a/src/com/owncloud/android/ui/fragment/LocalFileListFragment.java b/src/com/owncloud/android/ui/fragment/LocalFileListFragment.java index 4e6affa6..24421fa5 100644 --- a/src/com/owncloud/android/ui/fragment/LocalFileListFragment.java +++ b/src/com/owncloud/android/ui/fragment/LocalFileListFragment.java @@ -19,7 +19,6 @@ package com.owncloud.android.ui.fragment; import java.io.File; -import com.owncloud.android.ui.FragmentListView; import com.owncloud.android.ui.adapter.LocalFileListAdapter; import android.app.Activity; @@ -42,9 +41,8 @@ import com.owncloud.android.R; * @author David A. Velasco * */ -public class LocalFileListFragment extends FragmentListView { +public class LocalFileListFragment extends ExtendedListFragment { private static final String TAG = "LocalFileListFragment"; - private static final String SAVED_LIST_POSITION = "LIST_POSITION"; /** Reference to the Activity which this fragment is attached to. For callbacks */ private LocalFileListFragment.ContainerActivity mContainerActivity; @@ -94,27 +92,10 @@ public class LocalFileListFragment extends FragmentListView { mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity()); setListAdapter(mAdapter); - if (savedInstanceState != null) { - Log_OC.i(TAG, "savedInstanceState is not null"); - int position = savedInstanceState.getInt(SAVED_LIST_POSITION); - setReferencePosition(position); - } - Log_OC.i(TAG, "onActivityCreated() stop"); } - @Override - public void onSaveInstanceState(Bundle savedInstanceState) { - Log_OC.i(TAG, "onSaveInstanceState() start"); - - savedInstanceState.putInt(SAVED_LIST_POSITION, getReferencePosition()); - - - Log_OC.i(TAG, "onSaveInstanceState() stop"); - } - - /** * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case. */ diff --git a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java index 1712b262..e65de231 100644 --- a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -34,13 +34,14 @@ import com.owncloud.android.operations.RemoteOperation; import com.owncloud.android.operations.RemoveFileOperation; import com.owncloud.android.operations.RenameFileOperation; import com.owncloud.android.operations.SynchronizeFileOperation; -import com.owncloud.android.ui.FragmentListView; import com.owncloud.android.ui.activity.FileDisplayActivity; import com.owncloud.android.ui.activity.TransferServiceGetter; import com.owncloud.android.ui.adapter.FileListListAdapter; import com.owncloud.android.ui.dialog.EditNameDialog; import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener; import com.owncloud.android.ui.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener; +import com.owncloud.android.ui.preview.PreviewImageFragment; +import com.owncloud.android.ui.preview.PreviewMediaFragment; import android.accounts.Account; import android.app.Activity; @@ -59,8 +60,8 @@ import android.widget.AdapterView.AdapterContextMenuInfo; * @author Bartek Przybylski * */ -public class OCFileListFragment extends FragmentListView implements EditNameDialogListener, ConfirmationDialogFragmentListener { - private static final String TAG = "FileListFragment"; +public class OCFileListFragment extends ExtendedListFragment implements EditNameDialogListener, ConfirmationDialogFragmentListener { + private static final String TAG = OCFileListFragment.class.getSimpleName(); private static final String SAVED_LIST_POSITION = "LIST_POSITION"; private OCFileListFragment.ContainerActivity mContainerActivity; @@ -77,6 +78,7 @@ public class OCFileListFragment extends FragmentListView implements EditNameDial @Override public void onAttach(Activity activity) { super.onAttach(activity); + Log_OC.e(TAG, "onAttach"); try { mContainerActivity = (ContainerActivity) activity; } catch (ClassCastException e) { @@ -90,52 +92,62 @@ public class OCFileListFragment extends FragmentListView implements EditNameDial */ @Override public void onActivityCreated(Bundle savedInstanceState) { - Log_OC.i(TAG, "onActivityCreated() start"); - super.onActivityCreated(savedInstanceState); + Log_OC.e(TAG, "onActivityCreated() start"); mAdapter = new FileListListAdapter(mContainerActivity.getInitialDirectory(), mContainerActivity.getStorageManager(), getActivity(), mContainerActivity); setListAdapter(mAdapter); - if (savedInstanceState != null) { - Log_OC.i(TAG, "savedInstanceState is not null"); - int position = savedInstanceState.getInt(SAVED_LIST_POSITION); - setReferencePosition(position); - } - registerForContextMenu(getListView()); getListView().setOnCreateContextMenuListener(this); mHandler = new Handler(); - - Log_OC.i(TAG, "onActivityCreated() stop"); } - - @Override - public void onSaveInstanceState(Bundle savedInstanceState) { - Log_OC.i(TAG, "onSaveInstanceState() start"); - - savedInstanceState.putInt(SAVED_LIST_POSITION, getReferencePosition()); + /** + * Call this, when the user presses the up button + */ + public void onBrowseUp() { + OCFile parentDir = null; - Log_OC.i(TAG, "onSaveInstanceState() stop"); + if(mFile != null){ + DataStorageManager storageManager = mContainerActivity.getStorageManager(); + parentDir = storageManager.getFileById(mFile.getParentId()); + mFile = parentDir; + } + listDirectory(parentDir); } - @Override public void onItemClick(AdapterView l, View v, int position, long id) { OCFile file = (OCFile) mAdapter.getItem(position); if (file != null) { - /// Click on a directory - if (file.getMimetype().equals("DIR")) { - // just local updates + if (file.isDirectory()) { + // update state and view of this fragment mFile = file; listDirectory(file); - // any other updates are let to the container Activity - mContainerActivity.onDirectoryClick(file); - - } else { /// Click on a file - mContainerActivity.onFileClick(file, false); + // then, notify parent activity to let it update its state and view, and other fragments + mContainerActivity.onBrowsedDownTo(file); + + } else { /// Click on a file + if (PreviewImageFragment.canBePreviewed(file)) { + // preview image - it handles the download, if needed + mContainerActivity.startImagePreview(file); + + } else if (file.isDown()) { + if (PreviewMediaFragment.canBePreviewed(file)) { + // media preview + mContainerActivity.startMediaPreview(file, 0, true); + } else { + // open with + mContainerActivity.openFile(file); + } + + } else { + // automatic download, preview on finish + mContainerActivity.startDownloadForPreview(file); + } + } } else { @@ -292,7 +304,7 @@ public class OCFileListFragment extends FragmentListView implements EditNameDial return true; } case R.id.action_see_details: { - ((FileFragment.ContainerActivity)getActivity()).showFragmentWithDetails(mTargetFile); + ((FileFragment.ContainerActivity)getActivity()).showDetails(mTargetFile); return true; } default: @@ -302,20 +314,6 @@ public class OCFileListFragment extends FragmentListView implements EditNameDial /** - * Call this, when the user presses the up button - */ - public void onNavigateUp() { - OCFile parentDir = null; - - if(mFile != null){ - DataStorageManager storageManager = mContainerActivity.getStorageManager(); - parentDir = storageManager.getFileById(mFile.getParentId()); - mFile = parentDir; - } - listDirectory(parentDir); - } - - /** * Use this to query the {@link OCFile} that is currently * being displayed by this fragment * @return The currently viewed OCFile @@ -377,18 +375,17 @@ public class OCFileListFragment extends FragmentListView implements EditNameDial public interface ContainerActivity extends TransferServiceGetter, OnRemoteOperationListener, FileHandler { /** - * Callback method invoked when a directory is clicked by the user on the files list + * Callback method invoked when a the user browsed into a different folder through the list of files * * @param file */ - public void onDirectoryClick(OCFile file); + public void onBrowsedDownTo(OCFile folder); - /** - * Callback method invoked when a file (non directory) is clicked by the user on the files list - * - * @param file - */ - public void onFileClick(OCFile file, boolean realClick); + public void startDownloadForPreview(OCFile file); + + public void startMediaPreview(OCFile file, int i, boolean b); + + public void startImagePreview(OCFile file); /** * Getter for the current DataStorageManager in the container activity diff --git a/src/com/owncloud/android/ui/preview/FileDownloadFragment.java b/src/com/owncloud/android/ui/preview/FileDownloadFragment.java index 773350af..2a97ad13 100644 --- a/src/com/owncloud/android/ui/preview/FileDownloadFragment.java +++ b/src/com/owncloud/android/ui/preview/FileDownloadFragment.java @@ -31,8 +31,6 @@ import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; -import com.actionbarsherlock.app.SherlockFragment; -import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.ui.fragment.FileFragment; @@ -47,7 +45,7 @@ import eu.alefzero.webdav.OnDatatransferProgressListener; * * @author David A. Velasco */ -public class FileDownloadFragment extends SherlockFragment implements OnClickListener, FileFragment { +public class FileDownloadFragment extends FileFragment implements OnClickListener { public static final String EXTRA_FILE = "FILE"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; @@ -56,9 +54,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis private FileFragment.ContainerActivity mContainerActivity; private View mView; - private OCFile mFile; private Account mAccount; - private FileDataStorageManager mStorageManager; public ProgressListener mProgressListener; private boolean mListening; @@ -75,9 +71,8 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically. */ public FileDownloadFragment() { - mFile = null; + super(); mAccount = null; - mStorageManager = null; mProgressListener = null; mListening = false; mIgnoreFirstSavedState = false; @@ -95,9 +90,8 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution */ public FileDownloadFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) { - mFile = fileToDetail; + super(fileToDetail); mAccount = ocAccount; - mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment mProgressListener = null; mListening = false; mIgnoreFirstSavedState = ignoreFirstSavedState; @@ -118,7 +112,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis if (savedInstanceState != null) { if (!mIgnoreFirstSavedState) { - mFile = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE); + setFile((OCFile)savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE)); mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT); mError = savedInstanceState.getBoolean(FileDownloadFragment.EXTRA_ERROR); } else { @@ -167,7 +161,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (mAccount != null) { - mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());; + //mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());; } } @@ -175,7 +169,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); - outState.putParcelable(FileDownloadFragment.EXTRA_FILE, mFile); + outState.putParcelable(FileDownloadFragment.EXTRA_FILE, getFile()); outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount); outState.putBoolean(FileDownloadFragment.EXTRA_ERROR, mError); } @@ -224,8 +218,8 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis switch (v.getId()) { case R.id.cancelBtn: { FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); - if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) { - downloaderBinder.cancel(mAccount, mFile); + if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) { + downloaderBinder.cancel(mAccount, getFile()); getActivity().finish(); // :) /* leaveTransferProgress(); @@ -245,14 +239,6 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis /** - * {@inheritDoc} - */ - public OCFile getFile(){ - return mFile; - } - - - /** * Updates the view depending upon the state of the downloading file. * * @param transferring When true, the view must be updated assuming that the holded file is @@ -261,10 +247,10 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis public void updateView(boolean transferring) { // configure UI for depending upon local state of the file FileDownloaderBinder downloaderBinder = (mContainerActivity == null) ? null : mContainerActivity.getFileDownloaderBinder(); - if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile))) { + if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile()))) { setButtonsForTransferring(); - } else if (mFile.isDown()) { + } else if (getFile().isDown()) { setButtonsForDown(); @@ -335,7 +321,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis public void listenForTransferProgress() { if (mProgressListener != null && !mListening) { if (mContainerActivity.getFileDownloaderBinder() != null) { - mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, mFile); + mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile()); mListening = true; setButtonsForTransferring(); } @@ -346,7 +332,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis public void leaveTransferProgress() { if (mProgressListener != null) { if (mContainerActivity.getFileDownloaderBinder() != null) { - mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, mFile); + mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile()); mListening = false; } } diff --git a/src/com/owncloud/android/ui/preview/PreviewImageActivity.java b/src/com/owncloud/android/ui/preview/PreviewImageActivity.java index 164f9293..4f727985 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImageActivity.java +++ b/src/com/owncloud/android/ui/preview/PreviewImageActivity.java @@ -44,7 +44,7 @@ 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.ui.activity.FileActivity; -import com.owncloud.android.ui.activity.FileDetailActivity; +import com.owncloud.android.ui.activity.FileDisplayActivity; import com.owncloud.android.ui.fragment.FileFragment; import com.owncloud.android.AccountUtils; @@ -311,11 +311,11 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C @Override - public void showFragmentWithDetails(OCFile file) { - Intent showDetailsIntent = new Intent(this, FileDetailActivity.class); + public void showDetails(OCFile file) { + Intent showDetailsIntent = new Intent(this, FileDisplayActivity.class); + showDetailsIntent.setAction(FileDisplayActivity.ACTION_DETAILS); showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, file); showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this)); - showDetailsIntent.putExtra(FileDetailActivity.EXTRA_MODE, FileDetailActivity.MODE_DETAILS); startActivity(showDetailsIntent); int pos = mPreviewImagePagerAdapter.getFilePosition(file); file = mPreviewImagePagerAdapter.getFileAt(pos); diff --git a/src/com/owncloud/android/ui/preview/PreviewImageFragment.java b/src/com/owncloud/android/ui/preview/PreviewImageFragment.java index 8df06bfe..9d491530 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImageFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewImageFragment.java @@ -47,7 +47,6 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; -import com.actionbarsherlock.app.SherlockFragment; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; @@ -74,14 +73,12 @@ import eu.alefzero.webdav.WebdavUtils; * * @author David A. Velasco */ -public class PreviewImageFragment extends SherlockFragment implements FileFragment, - OnRemoteOperationListener, +public class PreviewImageFragment extends FileFragment implements OnRemoteOperationListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener { public static final String EXTRA_FILE = "FILE"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; private View mView; - private OCFile mFile; private Account mAccount; private FileDataStorageManager mStorageManager; private ImageView mImageView; @@ -108,7 +105,7 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution */ public PreviewImageFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) { - mFile = fileToDetail; + super(fileToDetail); mAccount = ocAccount; mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment mIgnoreFirstSavedState = ignoreFirstSavedState; @@ -123,7 +120,7 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction */ public PreviewImageFragment() { - mFile = null; + super(); mAccount = null; mStorageManager = null; mIgnoreFirstSavedState = false; @@ -180,19 +177,19 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver()); if (savedInstanceState != null) { if (!mIgnoreFirstSavedState) { - mFile = savedInstanceState.getParcelable(PreviewImageFragment.EXTRA_FILE); + setFile((OCFile)savedInstanceState.getParcelable(PreviewImageFragment.EXTRA_FILE)); mAccount = savedInstanceState.getParcelable(PreviewImageFragment.EXTRA_ACCOUNT); } else { mIgnoreFirstSavedState = false; } } - if (mFile == null) { + if (getFile() == null) { throw new IllegalStateException("Instanced with a NULL OCFile"); } if (mAccount == null) { throw new IllegalStateException("Instanced with a NULL ownCloud Account"); } - if (!mFile.isDown()) { + if (!getFile().isDown()) { throw new IllegalStateException("There is no local file to preview"); } } @@ -204,7 +201,7 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); - outState.putParcelable(PreviewImageFragment.EXTRA_FILE, mFile); + outState.putParcelable(PreviewImageFragment.EXTRA_FILE, getFile()); outState.putParcelable(PreviewImageFragment.EXTRA_ACCOUNT, mAccount); } @@ -212,9 +209,9 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag @Override public void onStart() { super.onStart(); - if (mFile != null) { + if (getFile() != null) { BitmapLoader bl = new BitmapLoader(mImageView, mMessageView, mProgressWheel); - bl.execute(new String[]{mFile.getStoragePath()}); + bl.execute(new String[]{getFile().getStoragePath()}); } } @@ -272,42 +269,19 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag private void seeDetails() { - ((FileFragment.ContainerActivity)getActivity()).showFragmentWithDetails(mFile); + ((FileFragment.ContainerActivity)getActivity()).showDetails(getFile()); } @Override public void onResume() { super.onResume(); - //Log.e(TAG, "FRAGMENT, ONRESUME"); - /* - mDownloadFinishReceiver = new DownloadFinishReceiver(); - IntentFilter filter = new IntentFilter( - FileDownloader.DOWNLOAD_FINISH_MESSAGE); - getActivity().registerReceiver(mDownloadFinishReceiver, filter); - - mUploadFinishReceiver = new UploadFinishReceiver(); - filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE); - getActivity().registerReceiver(mUploadFinishReceiver, filter); - */ - } @Override public void onPause() { super.onPause(); - /* - if (mVideoPreview.getVisibility() == View.VISIBLE) { - mSavedPlaybackPosition = mVideoPreview.getCurrentPosition(); - }*/ - /* - getActivity().unregisterReceiver(mDownloadFinishReceiver); - mDownloadFinishReceiver = null; - - getActivity().unregisterReceiver(mUploadFinishReceiver); - mUploadFinishReceiver = null; - */ } @@ -328,22 +302,23 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag * available apps for the MIME type known from the file extension, to let the user choose */ private void openFile() { - String storagePath = mFile.getStoragePath(); + OCFile file = getFile(); + String storagePath = file.getStoragePath(); String encodedStoragePath = WebdavUtils.encodePath(storagePath); try { Intent i = new Intent(Intent.ACTION_VIEW); - i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype()); + i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype()); i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); } catch (Throwable t) { - Log_OC.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype()); + Log_OC.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + file.getMimetype()); boolean toastIt = true; String mimeType = ""; try { Intent i = new Intent(Intent.ACTION_VIEW); mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); - if (mimeType == null || !mimeType.equals(mFile.getMimetype())) { + if (mimeType == null || !mimeType.equals(file.getMimetype())) { if (mimeType != null) { i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType); } else { @@ -366,7 +341,7 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag } finally { if (toastIt) { - Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show(); + Toast.makeText(getActivity(), "There is no application to handle file " + file.getFileName(), Toast.LENGTH_SHORT).show(); } } @@ -384,7 +359,7 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag private void removeFile() { ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance( R.string.confirmation_remove_alert, - new String[]{mFile.getFileName()}, + new String[]{getFile().getFileName()}, R.string.confirmation_remove_remote_and_local, R.string.confirmation_remove_local, R.string.common_cancel); @@ -398,8 +373,8 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag */ @Override public void onConfirmation(String callerTag) { - if (mStorageManager.getFileById(mFile.getFileId()) != null) { // check that the file is still there; - mLastRemoteOperation = new RemoveFileOperation( mFile, // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters + if (mStorageManager.getFileById(getFile().getFileId()) != null) { // check that the file is still there; + mLastRemoteOperation = new RemoveFileOperation( getFile(), // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters true, mStorageManager); mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity()); @@ -415,11 +390,12 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag @Override public void onNeutral(String callerTag) { // TODO this code should be made in a secondary thread, - if (mFile.isDown()) { // checks it is still there - File f = new File(mFile.getStoragePath()); + OCFile file = getFile(); + if (file.isDown()) { // checks it is still there + File f = new File(file.getStoragePath()); f.delete(); - mFile.setStoragePath(null); - mStorageManager.saveFile(mFile); + file.setStoragePath(null); + mStorageManager.saveFile(file); finish(); } } @@ -433,33 +409,6 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag } - /** - * {@inheritDoc} - */ - public OCFile getFile(){ - return mFile; - } - - /* - /** - * Use this method to signal this Activity that it shall update its view. - * - * @param file : An {@link OCFile} - *-/ - public void updateFileDetails(OCFile file, Account ocAccount) { - mFile = file; - if (ocAccount != null && ( - mStorageManager == null || - (mAccount != null && !mAccount.equals(ocAccount)) - )) { - mStorageManager = new FileDataStorageManager(ocAccount, getActivity().getApplicationContext().getContentResolver()); - } - mAccount = ocAccount; - updateFileDetails(false); - } - */ - - private class BitmapLoader extends AsyncTask { /** @@ -571,7 +520,7 @@ public class PreviewImageFragment extends SherlockFragment implements FileFrag } catch (Throwable t) { mErrorMessageId = R.string.common_error_unknown; - Log_OC.e(TAG, "Unexpected error loading " + mFile.getStoragePath(), t); + Log_OC.e(TAG, "Unexpected error loading " + getFile().getStoragePath(), t); } return result; diff --git a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java index 164fe04a..7ea9f523 100644 --- a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java @@ -39,7 +39,6 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; -import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; @@ -50,7 +49,6 @@ import android.widget.ImageView; import android.widget.Toast; import android.widget.VideoView; -import com.actionbarsherlock.app.SherlockFragment; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; @@ -63,10 +61,8 @@ import com.owncloud.android.operations.OnRemoteOperationListener; import com.owncloud.android.operations.RemoteOperation; import com.owncloud.android.operations.RemoteOperationResult; import com.owncloud.android.operations.RemoveFileOperation; -import com.owncloud.android.ui.activity.FileDetailActivity; import com.owncloud.android.ui.activity.FileDisplayActivity; import com.owncloud.android.ui.fragment.ConfirmationDialogFragment; -import com.owncloud.android.ui.fragment.FileDetailFragment; import com.owncloud.android.ui.fragment.FileFragment; import com.owncloud.android.Log_OC; @@ -82,8 +78,8 @@ import eu.alefzero.webdav.WebdavUtils; * * @author David A. Velasco */ -public class PreviewMediaFragment extends SherlockFragment implements - OnTouchListener , FileFragment, +public class PreviewMediaFragment extends FileFragment implements + OnTouchListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener { public static final String EXTRA_FILE = "FILE"; @@ -92,7 +88,6 @@ public class PreviewMediaFragment extends SherlockFragment implements private static final String EXTRA_PLAYING = "PLAYING"; private View mView; - private OCFile mFile; private Account mAccount; private FileDataStorageManager mStorageManager; private ImageView mImagePreview; @@ -121,7 +116,7 @@ public class PreviewMediaFragment extends SherlockFragment implements * @param ocAccount An ownCloud account; needed to start downloads */ public PreviewMediaFragment(OCFile fileToDetail, Account ocAccount, int startPlaybackPosition, boolean autoplay) { - mFile = fileToDetail; + super(fileToDetail); mAccount = ocAccount; mSavedPlaybackPosition = startPlaybackPosition; mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment @@ -137,7 +132,7 @@ public class PreviewMediaFragment extends SherlockFragment implements * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction */ public PreviewMediaFragment() { - mFile = null; + super(); mAccount = null; mSavedPlaybackPosition = 0; mStorageManager = null; @@ -201,22 +196,23 @@ public class PreviewMediaFragment extends SherlockFragment implements mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver()); if (savedInstanceState != null) { - mFile = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE); + setFile((OCFile)savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE)); mAccount = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_ACCOUNT); mSavedPlaybackPosition = savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION); mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING); } - if (mFile == null) { + OCFile file = getFile(); + if (file == null) { throw new IllegalStateException("Instanced with a NULL OCFile"); } if (mAccount == null) { throw new IllegalStateException("Instanced with a NULL ownCloud Account"); } - if (!mFile.isDown()) { + if (!file.isDown()) { throw new IllegalStateException("There is no local file to preview"); } - if (mFile.isVideo()) { + if (file.isVideo()) { mVideoPreview.setVisibility(View.VISIBLE); mImagePreview.setVisibility(View.GONE); prepareVideo(); @@ -237,10 +233,10 @@ public class PreviewMediaFragment extends SherlockFragment implements super.onSaveInstanceState(outState); Log_OC.e(TAG, "onSaveInstanceState"); - outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, mFile); + outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, getFile()); outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount); - if (mFile.isVideo()) { + if (getFile().isVideo()) { mSavedPlaybackPosition = mVideoPreview.getCurrentPosition(); mAutoplay = mVideoPreview.isPlaying(); outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mSavedPlaybackPosition); @@ -257,11 +253,12 @@ public class PreviewMediaFragment extends SherlockFragment implements super.onStart(); Log_OC.e(TAG, "onStart"); - if (mFile != null) { - if (mFile.isAudio()) { + OCFile file = getFile(); + if (file != null) { + if (file.isAudio()) { bindMediaService(); - } else if (mFile.isVideo()) { + } else if (file.isVideo()) { stopAudio(); playVideo(); } @@ -331,7 +328,7 @@ public class PreviewMediaFragment extends SherlockFragment implements private void seeDetails() { stopPreview(false); - ((FileFragment.ContainerActivity)getActivity()).showFragmentWithDetails(mFile); + ((FileFragment.ContainerActivity)getActivity()).showDetails(getFile()); } @@ -348,7 +345,7 @@ public class PreviewMediaFragment extends SherlockFragment implements mMediaController.setMediaPlayer(mVideoPreview); // load the video file in the video player ; when done, VideoHelper#onPrepared() will be called - mVideoPreview.setVideoPath(mFile.getStoragePath()); + mVideoPreview.setVideoPath(getFile().getStoragePath()); } @@ -397,7 +394,7 @@ public class PreviewMediaFragment extends SherlockFragment implements mVideoPreview.stopPlayback(); mAutoplay = false; mSavedPlaybackPosition = 0; - mVideoPreview.setVideoPath(mFile.getStoragePath()); + mVideoPreview.setVideoPath(getFile().getStoragePath()); } } // else : called from onError() mMediaController.updatePausePlay(); @@ -481,7 +478,7 @@ public class PreviewMediaFragment extends SherlockFragment implements private void startFullScreenVideo() { Intent i = new Intent(getActivity(), PreviewVideoActivity.class); i.putExtra(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount); - i.putExtra(PreviewVideoActivity.EXTRA_FILE, mFile); + i.putExtra(PreviewVideoActivity.EXTRA_FILE, getFile()); i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying()); mVideoPreview.pause(); i.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPreview.getCurrentPosition()); @@ -505,9 +502,10 @@ public class PreviewMediaFragment extends SherlockFragment implements private void playAudio() { - if (!mMediaServiceBinder.isPlaying(mFile)) { - Log_OC.d(TAG, "starting playback of " + mFile.getStoragePath()); - mMediaServiceBinder.start(mAccount, mFile, mAutoplay, mSavedPlaybackPosition); + OCFile file = getFile(); + if (!mMediaServiceBinder.isPlaying(file)) { + Log_OC.d(TAG, "starting playback of " + file.getStoragePath()); + mMediaServiceBinder.start(mAccount, file, mAutoplay, mSavedPlaybackPosition); } else { if (!mMediaServiceBinder.isPlaying() && mAutoplay) { @@ -584,23 +582,24 @@ public class PreviewMediaFragment extends SherlockFragment implements * available apps for the MIME type known from the file extension, to let the user choose */ private void openFile() { + OCFile file = getFile(); stopPreview(true); - String storagePath = mFile.getStoragePath(); + String storagePath = file.getStoragePath(); String encodedStoragePath = WebdavUtils.encodePath(storagePath); try { Intent i = new Intent(Intent.ACTION_VIEW); - i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype()); + i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype()); i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); } catch (Throwable t) { - Log_OC.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype()); + Log_OC.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + file.getMimetype()); boolean toastIt = true; String mimeType = ""; try { Intent i = new Intent(Intent.ACTION_VIEW); mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); - if (mimeType == null || !mimeType.equals(mFile.getMimetype())) { + if (mimeType == null || !mimeType.equals(file.getMimetype())) { if (mimeType != null) { i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType); } else { @@ -623,7 +622,7 @@ public class PreviewMediaFragment extends SherlockFragment implements } finally { if (toastIt) { - Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show(); + Toast.makeText(getActivity(), "There is no application to handle file " + file.getFileName(), Toast.LENGTH_SHORT).show(); } } @@ -640,7 +639,7 @@ public class PreviewMediaFragment extends SherlockFragment implements private void removeFile() { ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance( R.string.confirmation_remove_alert, - new String[]{mFile.getFileName()}, + new String[]{getFile().getFileName()}, R.string.confirmation_remove_remote_and_local, R.string.confirmation_remove_local, R.string.common_cancel); @@ -654,15 +653,16 @@ public class PreviewMediaFragment extends SherlockFragment implements */ @Override public void onConfirmation(String callerTag) { - if (mStorageManager.getFileById(mFile.getFileId()) != null) { // check that the file is still there; + OCFile file = getFile(); + if (mStorageManager.getFileById(file.getFileId()) != null) { // check that the file is still there; stopPreview(true); - mLastRemoteOperation = new RemoveFileOperation( mFile, // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters + mLastRemoteOperation = new RemoveFileOperation( file, // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters true, mStorageManager); mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity()); boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; - getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); + getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT); } } @@ -673,12 +673,13 @@ public class PreviewMediaFragment extends SherlockFragment implements @Override public void onNeutral(String callerTag) { // TODO this code should be made in a secondary thread, - if (mFile.isDown()) { // checks it is still there + OCFile file = getFile(); + if (file.isDown()) { // checks it is still there stopPreview(true); - File f = new File(mFile.getStoragePath()); + File f = new File(file.getStoragePath()); f.delete(); - mFile.setStoragePath(null); - mStorageManager.saveFile(mFile); + file.setStoragePath(null); + mStorageManager.saveFile(file); finish(); } } @@ -693,33 +694,6 @@ public class PreviewMediaFragment extends SherlockFragment implements /** - * {@inheritDoc} - */ - public OCFile getFile(){ - return mFile; - } - - /* - /** - * Use this method to signal this Activity that it shall update its view. - * - * @param file : An {@link OCFile} - *-/ - public void updateFileDetails(OCFile file, Account ocAccount) { - mFile = file; - if (ocAccount != null && ( - mStorageManager == null || - (mAccount != null && !mAccount.equals(ocAccount)) - )) { - mStorageManager = new FileDataStorageManager(ocAccount, getActivity().getApplicationContext().getContentResolver()); - } - mAccount = ocAccount; - updateFileDetails(false); - } - */ - - - /** * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed. * * @param file File to test if can be previewed. @@ -743,7 +717,7 @@ public class PreviewMediaFragment extends SherlockFragment implements private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) { boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; - getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); + getActivity().dismissDialog(FileDisplayActivity.DIALOG_SHORT_WAIT); if (result.isSuccess()) { Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG); @@ -760,10 +734,11 @@ public class PreviewMediaFragment extends SherlockFragment implements } private void stopPreview(boolean stopAudio) { - if (mFile.isAudio() && stopAudio) { + OCFile file = getFile(); + if (file.isAudio() && stopAudio) { mMediaServiceBinder.pause(); - } else if (mFile.isVideo()) { + } else if (file.isVideo()) { mVideoPreview.stopPlayback(); } } @@ -774,16 +749,7 @@ public class PreviewMediaFragment extends SherlockFragment implements * Finishes the preview */ private void finish() { - Activity container = getActivity(); - if (container instanceof FileDisplayActivity) { - // double pane - FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment - transaction.commit(); - ((FileFragment.ContainerActivity)container).onFileStateChanged(); - } else { - container.finish(); - } + getActivity().onBackPressed(); }