+ private void startVideoActivity() {\r
+ Intent i = new Intent(getActivity(), PreviewVideoActivity.class);\r
+ i.putExtra(PreviewVideoActivity.EXTRA_FILE, mFile);\r
+ i.putExtra(PreviewVideoActivity.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