Do not try to play files in streaming by now
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileDetailFragment.java
index 5197d67..f37c609 100644 (file)
@@ -28,6 +28,7 @@ import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
 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
@@ -39,9 +40,11 @@ import android.annotation.SuppressLint;
 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
@@ -50,20 +53,25 @@ import android.net.Uri;
 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
+import android.widget.VideoView;\r
 \r
 import com.actionbarsherlock.app.SherlockFragment;\r
 import com.owncloud.android.AccountUtils;\r
@@ -76,6 +84,8 @@ import com.owncloud.android.files.services.FileObserverService;
 import com.owncloud.android.files.services.FileUploader;\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
@@ -87,7 +97,9 @@ import com.owncloud.android.operations.SynchronizeFileOperation;
 import com.owncloud.android.ui.activity.ConflictsResolveActivity;\r
 import com.owncloud.android.ui.activity.FileDetailActivity;\r
 import com.owncloud.android.ui.activity.FileDisplayActivity;\r
+import com.owncloud.android.ui.OnSwipeTouchListener;\r
 import com.owncloud.android.ui.activity.TransferServiceGetter;\r
+import com.owncloud.android.ui.activity.VideoActivity;\r
 import com.owncloud.android.ui.dialog.EditNameDialog;\r
 import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;\r
 import com.owncloud.android.utils.OwnCloudVersion;\r
@@ -100,10 +112,11 @@ import eu.alefzero.webdav.WebdavUtils;
  * This Fragment is used to display the details about a file.\r
  * \r
  * @author Bartek Przybylski\r
- * \r
+ * @author David A. Velasco\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
@@ -123,6 +136,10 @@ public class FileDetailFragment extends SherlockFragment implements
     private Handler mHandler;\r
     private RemoteOperation mLastRemoteOperation;\r
     private DialogFragment mCurrentDialog;\r
+    \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
@@ -155,10 +172,6 @@ public class FileDetailFragment extends SherlockFragment implements
         mAccount = ocAccount;\r
         mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment \r
         mLayout = R.layout.file_details_empty;\r
-        \r
-        if(fileToDetail != null && ocAccount != null) {\r
-            mLayout = R.layout.file_details_fragment;\r
-        }\r
     }\r
     \r
     \r
@@ -179,6 +192,10 @@ public class FileDetailFragment extends SherlockFragment implements
             mAccount = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_ACCOUNT);\r
         }\r
         \r
+        if(mFile != null && mAccount != null) {\r
+            mLayout = R.layout.file_details_fragment;\r
+        }\r
+        \r
         View view = null;\r
         view = inflater.inflate(mLayout, container, false);\r
         mView = view;\r
@@ -191,6 +208,7 @@ public class FileDetailFragment extends SherlockFragment implements
             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
@@ -206,6 +224,7 @@ public class FileDetailFragment extends SherlockFragment implements
         super.onAttach(activity);\r
         try {\r
             mContainerActivity = (ContainerActivity) activity;\r
+            \r
         } catch (ClassCastException e) {\r
             throw new ClassCastException(activity.toString() + " must implement " + FileDetailFragment.ContainerActivity.class.getSimpleName());\r
         }\r
@@ -220,6 +239,7 @@ public class FileDetailFragment extends SherlockFragment implements
         super.onActivityCreated(savedInstanceState);\r
         if (mAccount != null) {\r
             mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;\r
+            mView.setOnTouchListener(new OnSwipeTouchListener(getActivity()));            \r
         }\r
     }\r
         \r
@@ -233,6 +253,13 @@ public class FileDetailFragment extends SherlockFragment implements
         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
@@ -246,10 +273,12 @@ public class FileDetailFragment extends SherlockFragment implements
         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
     }\r
 \r
+\r
     @Override\r
     public void onPause() {\r
         super.onPause();\r
@@ -260,18 +289,37 @@ public class FileDetailFragment extends SherlockFragment implements
         getActivity().unregisterReceiver(mUploadFinishReceiver);\r
         mUploadFinishReceiver = null;\r
         \r
-        if (mPreview != null) {\r
+        if (mPreview != null) { // why?\r
             mPreview = null;\r
         }\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
+            if (mMediaServiceBinder != null && mMediaController != null) {\r
+                mMediaServiceBinder.unregisterMediaController(mMediaController);\r
+            }\r
+            getActivity().unbindService(mMediaServiceConnection);\r
+            mMediaServiceBinder = null;\r
+            if (mMediaController != null) {\r
+                mMediaController.hide();\r
+                mMediaController = null;\r
+            }\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
@@ -360,49 +408,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 break;\r
             }\r
             case R.id.fdOpenBtn: {\r
-                String storagePath = mFile.getStoragePath();\r
-                String encodedStoragePath = WebdavUtils.encodePath(storagePath);\r
-                try {\r
-                    Intent i = new Intent(Intent.ACTION_VIEW);\r
-                    i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());\r
-                    i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
-                    startActivity(i);\r
-                    \r
-                } catch (Throwable t) {\r
-                    Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());\r
-                    boolean toastIt = true; \r
-                    String mimeType = "";\r
-                    try {\r
-                        Intent i = new Intent(Intent.ACTION_VIEW);\r
-                        mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));\r
-                        if (mimeType == null || !mimeType.equals(mFile.getMimetype())) {\r
-                            if (mimeType != null) {\r
-                                i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);\r
-                            } else {\r
-                                // desperate try\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
-                            toastIt = false;\r
-                        }\r
-                        \r
-                    } catch (IndexOutOfBoundsException e) {\r
-                        Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);\r
-                        \r
-                    } catch (ActivityNotFoundException e) {\r
-                        Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");\r
-                        \r
-                    } catch (Throwable th) {\r
-                        Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);\r
-                        \r
-                    } finally {\r
-                        if (toastIt) {\r
-                            Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();\r
-                        }\r
-                    }\r
-                    \r
-                }\r
+                openFile();\r
                 break;\r
             }\r
             default:\r
@@ -417,6 +423,154 @@ public class FileDetailFragment extends SherlockFragment implements
     \r
     \r
     @Override\r
+    public boolean onTouch(View v, MotionEvent event) {\r
+        if (v == mPreview && event.getAction() == MotionEvent.ACTION_DOWN && mFile != null && mFile.isDown()) {\r
+            if (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
+                    /*\r
+                    mMediaController.postDelayed(new Runnable() {\r
+                        @Override\r
+                        public void run() {\r
+                            mMediaController.show(0);\r
+                        }\r
+                    } , 300);\r
+                    */\r
+                } else {\r
+                    if (mMediaController.isShowing()) {\r
+                        mMediaController.hide();\r
+                    } else {\r
+                        mMediaController.show(MediaService.MEDIA_CONTROL_LIFE);\r
+                    }\r
+                }\r
+                \r
+            } else if (mFile.isVideo()) {\r
+                startVideoActivity();\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    \r
+    private void startVideoActivity() {\r
+        Intent i = new Intent(getActivity(), VideoActivity.class);\r
+        i.putExtra(VideoActivity.EXTRA_FILE, mFile);\r
+        i.putExtra(VideoActivity.EXTRA_ACCOUNT, mAccount);\r
+        startActivity(i);\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
+            // follow the flow in MediaServiceConnection#onServiceConnected(...)\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
+                    prepareMediaController();\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
+        private void prepareMediaController() {\r
+            mMediaServiceBinder.registerMediaController(mMediaController);\r
+            mMediaController.setMediaPlayer(mMediaServiceBinder);\r
+            mMediaController.setAnchorView(getView());\r
+            mMediaController.setEnabled(mMediaServiceBinder.isInPlaybackState());\r
+        }\r
+\r
+        @Override\r
+        public void onServiceDisconnected(ComponentName component) {\r
+            if (component.equals(new ComponentName(getActivity(), MediaService.class))) {\r
+                Log.e(TAG, "Media service suddenly disconnected");\r
+                if (mMediaController != null) {\r
+                    mMediaController.hide();\r
+                    mMediaController.setMediaPlayer(null);\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
+        String storagePath = mFile.getStoragePath();\r
+        String encodedStoragePath = WebdavUtils.encodePath(storagePath);\r
+        try {\r
+            Intent i = new Intent(Intent.ACTION_VIEW);\r
+            i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());\r
+            i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
+            startActivity(i);\r
+            \r
+        } catch (Throwable t) {\r
+            Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());\r
+            boolean toastIt = true; \r
+            String mimeType = "";\r
+            try {\r
+                Intent i = new Intent(Intent.ACTION_VIEW);\r
+                mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));\r
+                if (mimeType == null || !mimeType.equals(mFile.getMimetype())) {\r
+                    if (mimeType != null) {\r
+                        i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);\r
+                    } else {\r
+                        // desperate try\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
+                    toastIt = false;\r
+                }\r
+                \r
+            } catch (IndexOutOfBoundsException e) {\r
+                Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);\r
+                \r
+            } catch (ActivityNotFoundException e) {\r
+                Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");\r
+                \r
+            } catch (Throwable th) {\r
+                Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);\r
+                \r
+            } finally {\r
+                if (toastIt) {\r
+                    Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();\r
+                }\r
+            }\r
+            \r
+        }\r
+    }\r
+\r
+\r
+    @Override\r
     public void onConfirmation(String callerTag) {\r
         if (callerTag.equals(FTAG_CONFIRMATION)) {\r
             if (mStorageManager.getFileById(mFile.getFileId()) != null) {\r
@@ -461,7 +615,7 @@ public class FileDetailFragment extends SherlockFragment implements
      * @return  True when the fragment was created with the empty layout.\r
      */\r
     public boolean isEmpty() {\r
-        return mLayout == R.layout.file_details_empty;\r
+        return (mLayout == R.layout.file_details_empty || mFile == null || mAccount == null);\r
     }\r
 \r
     \r
@@ -496,6 +650,8 @@ public class FileDetailFragment extends SherlockFragment implements
      *\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
@@ -503,12 +659,11 @@ public class FileDetailFragment extends SherlockFragment implements
      */\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
-            setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile\r
-                    .getMimetype()));\r
+            setFiletype(mFile.getMimetype());\r
             setFilesize(mFile.getFileLength());\r
             if(ocVersionSupportsTimeCreated()){\r
                 setTimeCreated(mFile.getCreationTimestamp());\r
@@ -545,6 +700,17 @@ public class FileDetailFragment extends SherlockFragment implements
     \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
@@ -560,8 +726,14 @@ public class FileDetailFragment extends SherlockFragment implements
      */\r
     private void setFiletype(String mimetype) {\r
         TextView tv = (TextView) getView().findViewById(R.id.fdType);\r
-        if (tv != null)\r
-            tv.setText(mimetype);\r
+        if (tv != null) {\r
+            String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);;        \r
+            tv.setText(printableMimetype);\r
+        }\r
+        ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);\r
+        if (iv != null) {\r
+            iv.setImageResource(DisplayUtils.getResourceId(mimetype));\r
+        }\r
     }\r
 \r
     /**\r
@@ -736,14 +908,14 @@ public class FileDetailFragment extends SherlockFragment implements
                 if (mFile.getRemotePath().equals(uploadRemotePath) ||\r
                     renamedInUpload) {\r
                     if (uploadWasFine) {\r
-                        mFile = mStorageManager.getFileByPath(mFile.getRemotePath());\r
+                        mFile = mStorageManager.getFileByPath(uploadRemotePath);\r
                     }\r
                     if (renamedInUpload) {\r
                         String newName = (new File(uploadRemotePath)).getName();\r
                         Toast msg = Toast.makeText(getActivity().getApplicationContext(), String.format(getString(R.string.filedetails_renamed_in_upload_msg), newName), Toast.LENGTH_LONG);\r
                         msg.show();\r
-                        getSherlockActivity().removeStickyBroadcast(intent);    // not the best place to do this; a small refactorization of BroadcastReceivers should be done\r
                     }\r
+                    getSherlockActivity().removeStickyBroadcast(intent);    // not the best place to do this; a small refactorization of BroadcastReceivers should be done\r
                     updateFileDetails(false);    // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server\r
                 }\r
             }\r
@@ -1053,4 +1225,5 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
     }\r
 \r
+\r
 }\r