X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/18bf35a8099ce585ef30767a9f1f7e595c866b45..fb8194b7ebcc386a6749bf81ec498445ef851f13:/src/com/owncloud/android/ui/fragment/FileDetailFragment.java diff --git a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java index 0d07c448..eca461c5 100644 --- a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java @@ -18,6 +18,7 @@ package com.owncloud.android.ui.fragment; import java.io.File; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; @@ -65,12 +66,15 @@ import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.webkit.MimeTypeMap; +import android.webkit.WebView.FindListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.MediaController; +import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; +import android.widget.VideoView; import com.actionbarsherlock.app.SherlockFragment; import com.owncloud.android.AccountUtils; @@ -96,12 +100,16 @@ import com.owncloud.android.operations.SynchronizeFileOperation; import com.owncloud.android.ui.activity.ConflictsResolveActivity; import com.owncloud.android.ui.activity.FileDetailActivity; import com.owncloud.android.ui.activity.FileDisplayActivity; +import com.owncloud.android.ui.OnSwipeTouchListener; import com.owncloud.android.ui.activity.TransferServiceGetter; +import com.owncloud.android.ui.activity.VideoActivity; import com.owncloud.android.ui.dialog.EditNameDialog; import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener; import com.owncloud.android.utils.OwnCloudVersion; import com.owncloud.android.R; + +import eu.alefzero.webdav.OnDatatransferProgressListener; import eu.alefzero.webdav.WebdavClient; import eu.alefzero.webdav.WebdavUtils; @@ -109,30 +117,30 @@ import eu.alefzero.webdav.WebdavUtils; * This Fragment is used to display the details about a file. * * @author Bartek Przybylski - * + * @author David A. Velasco */ public class FileDetailFragment extends SherlockFragment implements - OnClickListener, OnTouchListener, - ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener, EditNameDialogListener { + OnClickListener, + ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener, EditNameDialogListener, + FileFragment { public static final String EXTRA_FILE = "FILE"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; - private FileDetailFragment.ContainerActivity mContainerActivity; + private FileFragment.ContainerActivity mContainerActivity; private int mLayout; private View mView; private OCFile mFile; private Account mAccount; private FileDataStorageManager mStorageManager; - private ImageView mPreview; private DownloadFinishReceiver mDownloadFinishReceiver; private UploadFinishReceiver mUploadFinishReceiver; private Handler mHandler; private RemoteOperation mLastRemoteOperation; - private DialogFragment mCurrentDialog; + private MediaServiceBinder mMediaServiceBinder = null; private MediaController mMediaController = null; private MediaServiceConnection mMediaServiceConnection = null; @@ -203,8 +211,6 @@ public class FileDetailFragment extends SherlockFragment implements mView.findViewById(R.id.fdOpenBtn).setOnClickListener(this); mView.findViewById(R.id.fdRemoveBtn).setOnClickListener(this); //mView.findViewById(R.id.fdShareBtn).setOnClickListener(this); - mPreview = (ImageView)mView.findViewById(R.id.fdPreview); - mPreview.setOnTouchListener(this); } updateFileDetails(false); @@ -220,6 +226,7 @@ public class FileDetailFragment extends SherlockFragment implements super.onAttach(activity); try { mContainerActivity = (ContainerActivity) activity; + } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement " + FileDetailFragment.ContainerActivity.class.getSimpleName()); } @@ -234,6 +241,7 @@ public class FileDetailFragment extends SherlockFragment implements super.onActivityCreated(savedInstanceState); if (mAccount != null) { mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());; + mView.setOnTouchListener(new OnSwipeTouchListener(getActivity())); } } @@ -268,11 +276,6 @@ public class FileDetailFragment extends SherlockFragment implements filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE); getActivity().registerReceiver(mUploadFinishReceiver, filter); - mPreview = (ImageView)mView.findViewById(R.id.fdPreview); // this is here just because it is nullified in onPause() - - if (mMediaController != null) { - mMediaController.show(); - } } @@ -286,13 +289,6 @@ public class FileDetailFragment extends SherlockFragment implements getActivity().unregisterReceiver(mUploadFinishReceiver); mUploadFinishReceiver = null; - if (mPreview != null) { // why? - mPreview = null; - } - - if (mMediaController != null) { - mMediaController.hide(); - } } @@ -301,9 +297,15 @@ public class FileDetailFragment extends SherlockFragment implements super.onStop(); if (mMediaServiceConnection != null) { Log.d(TAG, "Unbinding from MediaService ..."); + if (mMediaServiceBinder != null && mMediaController != null) { + mMediaServiceBinder.unregisterMediaController(mMediaController); + } getActivity().unbindService(mMediaServiceConnection); mMediaServiceBinder = null; - mMediaController = null; + if (mMediaController != null) { + mMediaController.hide(); + mMediaController = null; + } } } @@ -397,8 +399,7 @@ public class FileDetailFragment extends SherlockFragment implements mFile.isDown() ? R.string.confirmation_remove_local : -1, R.string.common_cancel); confDialog.setOnConfirmationListener(this); - mCurrentDialog = confDialog; - mCurrentDialog.show(getFragmentManager(), FTAG_CONFIRMATION); + confDialog.show(getFragmentManager(), FTAG_CONFIRMATION); break; } case R.id.fdOpenBtn: { @@ -416,27 +417,14 @@ public class FileDetailFragment extends SherlockFragment implements } - @Override - public boolean onTouch(View v, MotionEvent event) { - if (v == mPreview && event.getAction() == MotionEvent.ACTION_DOWN && mFile != null && mFile.isDown() && mFile.isAudio()) { - if (!mMediaServiceBinder.isPlaying(mFile)) { - Log.d(TAG, "starting playback of " + mFile.getStoragePath()); - mMediaServiceBinder.start(mAccount, mFile); - // this is a patch; need to synchronize this with the onPrepared() coming from MediaPlayer in the MediaService - mMediaController.postDelayed(new Runnable() { - @Override - public void run() { - mMediaController.show(0); - } - } , 300); - } else { - mMediaController.show(0); - } - } - return false; + private void startVideoActivity() { + Intent i = new Intent(getActivity(), VideoActivity.class); + i.putExtra(VideoActivity.EXTRA_FILE, mFile); + i.putExtra(VideoActivity.EXTRA_ACCOUNT, mAccount); + startActivity(i); } - + private void bindMediaService() { Log.d(TAG, "Binding to MediaService..."); if (mMediaServiceConnection == null) { @@ -446,6 +434,7 @@ public class FileDetailFragment extends SherlockFragment implements MediaService.class), mMediaServiceConnection, Context.BIND_AUTO_CREATE); + // follow the flow in MediaServiceConnection#onServiceConnected(...) } /** Defines callbacks for service binding, passed to bindService() */ @@ -460,9 +449,7 @@ public class FileDetailFragment extends SherlockFragment implements if (mMediaController == null) { mMediaController = new MediaController(getSherlockActivity()); } - mMediaController.setMediaPlayer(mMediaServiceBinder); - mMediaController.setAnchorView(mPreview); - mMediaController.setEnabled(true); + prepareMediaController(); Log.d(TAG, "Successfully bound to MediaService, MediaController ready"); @@ -472,13 +459,20 @@ public class FileDetailFragment extends SherlockFragment implements } } + private void prepareMediaController() { + mMediaServiceBinder.registerMediaController(mMediaController); + mMediaController.setMediaPlayer(mMediaServiceBinder); + mMediaController.setAnchorView(getView()); + mMediaController.setEnabled(mMediaServiceBinder.isInPlaybackState()); + } + @Override public void onServiceDisconnected(ComponentName component) { if (component.equals(new ComponentName(getActivity(), MediaService.class))) { - Log.d(TAG, "Media service suddenly disconnected"); + Log.e(TAG, "Media service suddenly disconnected"); if (mMediaController != null) { mMediaController.hide(); - mMediaController.setMediaPlayer(null); // TODO check this is not an error + mMediaController.setMediaPlayer(null); mMediaController = null; } mMediaServiceBinder = null; @@ -553,8 +547,6 @@ public class FileDetailFragment extends SherlockFragment implements getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); } } - mCurrentDialog.dismiss(); - mCurrentDialog = null; } @Override @@ -566,15 +558,11 @@ public class FileDetailFragment extends SherlockFragment implements mStorageManager.saveFile(mFile); updateFileDetails(mFile, mAccount); } - mCurrentDialog.dismiss(); - mCurrentDialog = null; } @Override public void onCancel(String callerTag) { Log.d(TAG, "REMOVAL CANCELED"); - mCurrentDialog.dismiss(); - mCurrentDialog = null; } @@ -589,10 +577,9 @@ public class FileDetailFragment extends SherlockFragment implements /** - * Can be used to get the file that is currently being displayed. - * @return The file on the screen. + * {@inheritDoc} */ - public OCFile getDisplayedFile(){ + public OCFile getFile(){ return mFile; } @@ -651,11 +638,6 @@ public class FileDetailFragment extends SherlockFragment implements setButtonsForTransferring(); } else if (mFile.isDown()) { - // Update preview - if (mFile.getMimetype().startsWith("image/")) { - BitmapLoader bl = new BitmapLoader(); - bl.execute(new String[]{mFile.getStoragePath()}); - } setButtonsForDown(); @@ -754,9 +736,23 @@ public class FileDetailFragment extends SherlockFragment implements ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(false); ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(false); getView().findViewById(R.id.fdKeepInSync).setEnabled(false); + + // show the progress bar for the transfer + ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.fdProgressBar); + progressBar.setVisibility(View.VISIBLE); + TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText); + progressText.setVisibility(View.VISIBLE); + FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); + FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder(); + if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) { + progressText.setText(R.string.downloader_download_in_progress_ticker); + } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile)) { + progressText.setText(R.string.uploader_upload_in_progress_ticker); + } } } + /** * Enables or disables buttons for a file locally available */ @@ -769,6 +765,10 @@ public class FileDetailFragment extends SherlockFragment implements ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true); ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(true); getView().findViewById(R.id.fdKeepInSync).setEnabled(true); + + // hides the progress bar + ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.fdProgressBar); + progressBar.setVisibility(View.GONE); } } @@ -784,6 +784,10 @@ public class FileDetailFragment extends SherlockFragment implements ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true); ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(true); getView().findViewById(R.id.fdKeepInSync).setEnabled(true); + + // hides the progress bar + ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.fdProgressBar); + progressBar.setVisibility(View.GONE); } } @@ -809,30 +813,6 @@ public class FileDetailFragment extends SherlockFragment implements /** - * Interface to implement by any Activity that includes some instance of FileDetailFragment - * - * @author David A. Velasco - */ - public interface ContainerActivity extends TransferServiceGetter { - - /** - * Callback method invoked when the detail fragment wants to notice its container - * activity about a relevant state the file shown by the fragment. - * - * Added to notify to FileDisplayActivity about the need of refresh the files list. - * - * Currently called when: - * - a download is started; - * - a rename is completed; - * - a deletion is completed; - * - the 'inSync' flag is changed; - */ - public void onFileStateChanged(); - - } - - - /** * Once the file download has finished -> update view * @author Bartek Przybylski */ @@ -1012,80 +992,6 @@ public class FileDetailFragment extends SherlockFragment implements } - class BitmapLoader extends AsyncTask { - @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20 - @Override - protected Bitmap doInBackground(String... params) { - Bitmap result = null; - if (params.length != 1) return result; - String storagePath = params[0]; - try { - - BitmapFactory.Options options = new Options(); - options.inScaled = true; - options.inPurgeable = true; - options.inJustDecodeBounds = true; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { - options.inPreferQualityOverSpeed = false; - } - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { - options.inMutable = false; - } - - result = BitmapFactory.decodeFile(storagePath, options); - options.inJustDecodeBounds = false; - - int width = options.outWidth; - int height = options.outHeight; - int scale = 1; - if (width >= 2048 || height >= 2048) { - scale = (int) Math.ceil((Math.ceil(Math.max(height, width) / 2048.))); - options.inSampleSize = scale; - } - Display display = getActivity().getWindowManager().getDefaultDisplay(); - Point size = new Point(); - int screenwidth; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) { - display.getSize(size); - screenwidth = size.x; - } else { - screenwidth = display.getWidth(); - } - - Log.e("ASD", "W " + width + " SW " + screenwidth); - - if (width > screenwidth) { - scale = (int) Math.ceil((float)width / screenwidth); - options.inSampleSize = scale; - } - - result = BitmapFactory.decodeFile(storagePath, options); - - Log.e("ASD", "W " + options.outWidth + " SW " + options.outHeight); - - } catch (OutOfMemoryError e) { - result = null; - Log.e(TAG, "Out of memory occured for file with size " + storagePath); - - } catch (NoSuchFieldError e) { - result = null; - Log.e(TAG, "Error from access to unexisting field despite protection " + storagePath); - - } catch (Throwable t) { - result = null; - Log.e(TAG, "Unexpected error while creating image preview " + storagePath, t); - } - return result; - } - @Override - protected void onPostExecute(Bitmap result) { - if (result != null && mPreview != null) { - mPreview.setImageBitmap(result); - } - } - - } - /** * {@inheritDoc} */ @@ -1195,4 +1101,14 @@ public class FileDetailFragment extends SherlockFragment implements } + public ProgressBar getProgressBar() { + View v = getView(); + if (v != null) { + return (ProgressBar) v.findViewById(R.id.fdProgressBar); + } else { + return null; + } + } + + }