import org.apache.http.HttpStatus;\r
import org.apache.http.NameValuePair;\r
import org.apache.http.client.utils.URLEncodedUtils;\r
+import org.apache.http.entity.FileEntity;\r
import org.apache.http.message.BasicNameValuePair;\r
import org.apache.http.protocol.HTTP;\r
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;\r
import android.app.Activity;\r
import android.content.ActivityNotFoundException;\r
import android.content.BroadcastReceiver;\r
+import android.content.ComponentName;\r
import android.content.Context;\r
import android.content.Intent;\r
import android.content.IntentFilter;\r
+import android.content.ServiceConnection;\r
import android.graphics.Bitmap;\r
import android.graphics.BitmapFactory;\r
import android.graphics.BitmapFactory.Options;\r
import android.os.AsyncTask;\r
import android.os.Bundle;\r
import android.os.Handler;\r
+import android.os.IBinder;\r
import android.support.v4.app.DialogFragment;\r
import android.support.v4.app.FragmentTransaction;\r
import android.util.Log;\r
import android.view.Display;\r
import android.view.LayoutInflater;\r
+import android.view.MotionEvent;\r
import android.view.View;\r
import android.view.View.OnClickListener;\r
+import android.view.View.OnTouchListener;\r
import android.view.ViewGroup;\r
import android.webkit.MimeTypeMap;\r
import android.widget.Button;\r
import android.widget.CheckBox;\r
import android.widget.ImageView;\r
+import android.widget.MediaController;\r
import android.widget.TextView;\r
import android.widget.Toast;\r
\r
import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
import com.owncloud.android.media.MediaService;\r
+import com.owncloud.android.media.MediaServiceBinder;\r
import com.owncloud.android.network.OwnCloudClientUtils;\r
import com.owncloud.android.operations.OnRemoteOperationListener;\r
import com.owncloud.android.operations.RemoteOperation;\r
* \r
*/\r
public class FileDetailFragment extends SherlockFragment implements\r
- OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener, EditNameDialogListener {\r
+ OnClickListener, OnTouchListener, \r
+ ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener, EditNameDialogListener {\r
\r
public static final String EXTRA_FILE = "FILE";\r
public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
private Handler mHandler;\r
private RemoteOperation mLastRemoteOperation;\r
private DialogFragment mCurrentDialog;\r
+ private MediaServiceBinder mMediaServiceBinder = null;\r
+ private MediaController mMediaController = null;\r
+ private MediaServiceConnection mMediaServiceConnection = null;\r
\r
private static final String TAG = FileDetailFragment.class.getSimpleName();\r
public static final String FTAG = "FileDetails"; \r
mView.findViewById(R.id.fdRemoveBtn).setOnClickListener(this);\r
//mView.findViewById(R.id.fdShareBtn).setOnClickListener(this);\r
mPreview = (ImageView)mView.findViewById(R.id.fdPreview);\r
+ mPreview.setOnTouchListener(this);\r
}\r
\r
updateFileDetails(false);\r
Log.i(getClass().toString(), "onSaveInstanceState() end");\r
}\r
\r
+ @Override\r
+ public void onStart() {\r
+ super.onStart();\r
+ if (mFile != null && mFile.isAudio()) {\r
+ bindMediaService();\r
+ }\r
+ }\r
\r
@Override\r
public void onResume() {\r
mUploadFinishReceiver = new UploadFinishReceiver();\r
filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);\r
getActivity().registerReceiver(mUploadFinishReceiver, filter);\r
+\r
+ mPreview = (ImageView)mView.findViewById(R.id.fdPreview); // this is here just because it is nullified in onPause()\r
\r
- mPreview = (ImageView)mView.findViewById(R.id.fdPreview);\r
+ if (mMediaController != null) {\r
+ mMediaController.show();\r
+ }\r
}\r
\r
+\r
@Override\r
public void onPause() {\r
super.onPause();\r
getActivity().unregisterReceiver(mUploadFinishReceiver);\r
mUploadFinishReceiver = null;\r
\r
- if (mPreview != null) {\r
+ if (mPreview != null) { // why?\r
mPreview = null;\r
}\r
+ \r
+ if (mMediaController != null) {\r
+ mMediaController.hide();\r
+ }\r
}\r
\r
+\r
+ @Override\r
+ public void onStop() {\r
+ super.onStop();\r
+ if (mMediaServiceConnection != null) {\r
+ Log.d(TAG, "Unbinding from MediaService ...");\r
+ getActivity().unbindService(mMediaServiceConnection);\r
+ mMediaServiceBinder = null;\r
+ mMediaController = null;\r
+ }\r
+ }\r
+ \r
+ \r
@Override\r
public View getView() {\r
return super.getView() == null ? mView : super.getView();\r
}\r
\r
\r
- \r
@Override\r
public void onClick(View v) {\r
switch (v.getId()) {\r
}*/\r
}\r
\r
+ \r
+ @Override\r
+ public boolean onTouch(View v, MotionEvent event) {\r
+ if (v == mPreview && event.getAction() == MotionEvent.ACTION_DOWN && mFile != null && mFile.isDown() && mFile.isAudio()) {\r
+ if (!mMediaServiceBinder.isPlaying(mFile)) {\r
+ Log.d(TAG, "starting playback of " + mFile.getStoragePath());\r
+ mMediaServiceBinder.start(mAccount, mFile);\r
+ // this is a patch; need to synchronize this with the onPrepared() coming from MediaPlayer in the MediaService\r
+ mMediaController.postDelayed(new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ mMediaController.show(0);\r
+ }\r
+ } , 300);\r
+ } else {\r
+ mMediaController.show(0);\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ \r
+ private void bindMediaService() {\r
+ Log.d(TAG, "Binding to MediaService...");\r
+ if (mMediaServiceConnection == null) {\r
+ mMediaServiceConnection = new MediaServiceConnection();\r
+ }\r
+ getActivity().bindService( new Intent(getActivity(), \r
+ MediaService.class),\r
+ mMediaServiceConnection, \r
+ Context.BIND_AUTO_CREATE);\r
+ }\r
+ \r
+ /** Defines callbacks for service binding, passed to bindService() */\r
+ private class MediaServiceConnection implements ServiceConnection {\r
+\r
+ @Override\r
+ public void onServiceConnected(ComponentName component, IBinder service) {\r
+ if (component.equals(new ComponentName(getActivity(), MediaService.class))) {\r
+ Log.d(TAG, "Media service connected");\r
+ mMediaServiceBinder = (MediaServiceBinder) service;\r
+ if (mMediaServiceBinder != null) {\r
+ if (mMediaController == null) {\r
+ mMediaController = new MediaController(getSherlockActivity());\r
+ }\r
+ mMediaController.setMediaPlayer(mMediaServiceBinder);\r
+ mMediaController.setAnchorView(mPreview);\r
+ mMediaController.setEnabled(true);\r
+ \r
+ Log.d(TAG, "Successfully bound to MediaService, MediaController ready");\r
+ \r
+ } else {\r
+ Log.e(TAG, "Unexpected response from MediaService while binding");\r
+ }\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void onServiceDisconnected(ComponentName component) {\r
+ if (component.equals(new ComponentName(getActivity(), MediaService.class))) {\r
+ Log.d(TAG, "Media service suddenly disconnected");\r
+ if (mMediaController != null) {\r
+ mMediaController.hide();\r
+ mMediaController.setMediaPlayer(null); // TODO check this is not an error\r
+ mMediaController = null;\r
+ }\r
+ mMediaServiceBinder = null;\r
+ mMediaServiceConnection = null;\r
+ }\r
+ }\r
+ } \r
+\r
+\r
/**\r
* Opens mFile.\r
*/\r
private void openFile() {\r
\r
- Intent i = new Intent(getActivity(), MediaService.class);\r
- i.putExtra(MediaService.EXTRA_ACCOUNT, mAccount);\r
- i.putExtra(MediaService.EXTRA_FILE, mFile);\r
- i.setAction(MediaService.ACTION_PLAY_FILE);\r
- getActivity().startService(i);\r
-\r
- /*\r
String storagePath = mFile.getStoragePath();\r
String encodedStoragePath = WebdavUtils.encodePath(storagePath);\r
try {\r
i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);\r
} else {\r
// desperate try\r
- i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*-/*");\r
+ i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*/*");\r
}\r
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
startActivity(i);\r
}\r
}\r
\r
- }*/\r
+ }\r
}\r
\r
\r
*\r
* TODO Remove parameter when the transferring state of files is kept in database. \r
* \r
+ * TODO REFACTORING! this method called 5 times before every time the fragment is shown! \r
+ * \r
* @param transferring Flag signaling if the file should be considered as downloading or uploading, \r
* although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and \r
* {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.\r
*/\r
public void updateFileDetails(boolean transferring) {\r
\r
- if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) {\r
+ if (readyToShow()) {\r
\r
// set file details\r
setFilename(mFile.getFileName());\r
\r
\r
/**\r
+ * Checks if the fragment is ready to show details of a OCFile\r
+ * \r
+ * @return 'True' when the fragment is ready to show details of a file\r
+ */\r
+ private boolean readyToShow() {\r
+ return (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment); \r
+ }\r
+\r
+\r
+\r
+ /**\r
* Updates the filename in view\r
* @param filename to set\r
*/\r
}\r
}\r
\r
+\r
}\r