+ /**\r
+ * Use this method to signal this Activity that it shall update its view.\r
+ * \r
+ * @param intent The {@link Intent} that contains extra information about\r
+ * this file The intent needs to have these extras:\r
+ * <p>\r
+ * \r
+ * {@link FileDetailFragment#EXTRA_FILE}: An {@link OCFile}\r
+ * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file\r
+ * belongs to (required for downloading)\r
+ */\r
+ public void updateFileDetails(Intent intent) {\r
+ mIntent = intent;\r
+ updateFileDetails();\r
+ }\r
+\r
+ /**\r
+ * Updates the view with all relevant details about that file.\r
+ */\r
+ private void updateFileDetails() {\r
+ mFile = mIntent.getParcelableExtra(EXTRA_FILE);\r
+ Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
+\r
+ if (mFile != null) {\r
+ // set file details\r
+ setFilename(mFile.getFileName());\r
+ setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile\r
+ .getMimetype()));\r
+ setFilesize(mFile.getFileLength());\r
+ if(ocVersionSupportsTimeCreated()){\r
+ setTimeCreated(mFile.getCreationTimestamp());\r
+ }\r
+ \r
+ setTimeModified(mFile.getModificationTimestamp());\r
+ \r
+ // Update preview\r
+ if (mFile.getStoragePath() != null) {\r
+ ImageView preview = (ImageView) getView().findViewById(R.id.fdPreview);\r
+ try {\r
+ if (mFile.getMimetype().startsWith("image/")) {\r
+ BitmapFactory.Options options = new Options();\r
+ options.inScaled = true;\r
+ options.inPurgeable = true;\r
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {\r
+ options.inPreferQualityOverSpeed = false;\r
+ }\r
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {\r
+ options.inMutable = false;\r
+ }\r
+\r
+ Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options);\r
+\r
+ int width = options.outWidth;\r
+ int height = options.outHeight;\r
+ int scale = 1;\r
+ if (width >= 2048 || height >= 2048) {\r
+ scale = (int) (Math.ceil(Math.max(height, width)/2048.));\r
+ options.inSampleSize = scale;\r
+ bmp.recycle();\r
+\r
+ bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options);\r
+ }\r
+ preview.setImageBitmap(bmp);\r
+ }\r
+ } catch (OutOfMemoryError e) {\r
+ preview.setVisibility(View.INVISIBLE);\r
+ Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());\r
+ \r
+ } catch (NoSuchFieldError e) {\r
+ preview.setVisibility(View.INVISIBLE);\r
+ Log.e(TAG, "Error from access to unexisting field despite protection " + mFile.getFileLength());\r
+ \r
+ } catch (Throwable t) {\r
+ preview.setVisibility(View.INVISIBLE);\r
+ Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength(), t);\r
+ }\r
+ downloadButton.setText(R.string.filedetails_open);\r
+ downloadButton.setOnClickListener(new OnClickListener() {\r
+ @Override\r
+ public void onClick(View v) {\r
+ String storagePath = mFile.getStoragePath();\r
+ try {\r
+ Intent i = new Intent(Intent.ACTION_VIEW);\r
+ i.setDataAndType(Uri.parse("file://"+ storagePath), 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 != mFile.getMimetype()) {\r
+ i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mimeType);\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
+ } else {\r
+ // Make download button effective\r
+ downloadButton.setOnClickListener(this);\r
+ }\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Updates the filename in view\r
+ * @param filename to set\r
+ */\r
+ private void setFilename(String filename) {\r
+ TextView tv = (TextView) getView().findViewById(R.id.fdFilename);\r
+ if (tv != null)\r
+ tv.setText(filename);\r
+ }\r
+\r
+ /**\r
+ * Updates the MIME type in view\r
+ * @param mimetype to set\r
+ */\r
+ private void setFiletype(String mimetype) {\r
+ TextView tv = (TextView) getView().findViewById(R.id.fdType);\r
+ if (tv != null)\r
+ tv.setText(mimetype);\r
+ }\r
+\r
+ /**\r
+ * Updates the file size in view\r
+ * @param filesize in bytes to set\r
+ */\r
+ private void setFilesize(long filesize) {\r
+ TextView tv = (TextView) getView().findViewById(R.id.fdSize);\r
+ if (tv != null)\r
+ tv.setText(DisplayUtils.bytesToHumanReadable(filesize));\r
+ }\r
+ \r
+ /**\r
+ * Updates the time that the file was created in view\r
+ * @param milliseconds Unix time to set\r
+ */\r
+ private void setTimeCreated(long milliseconds){\r
+ TextView tv = (TextView) getView().findViewById(R.id.fdCreated);\r
+ TextView tvLabel = (TextView) getView().findViewById(R.id.fdCreatedLabel);\r
+ if(tv != null){\r
+ tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));\r
+ tv.setVisibility(View.VISIBLE);\r
+ tvLabel.setVisibility(View.VISIBLE);\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Updates the time that the file was last modified\r
+ * @param milliseconds Unix time to set\r
+ */\r
+ private void setTimeModified(long milliseconds){\r
+ TextView tv = (TextView) getView().findViewById(R.id.fdModified);\r
+ if(tv != null){\r
+ tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return\r
+ * the time that the file was created. There is a chance that this will\r
+ * be fixed in future versions. Use this method to check if this version of\r
+ * ownCloud has this fix.\r
+ * @return True, if ownCloud the ownCloud version is supporting creationg time\r
+ */\r
+ private boolean ocVersionSupportsTimeCreated(){\r
+ /* if(mIntent != null){\r
+ Account ocAccount = mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT);\r
+ if(ocAccount != null){\r
+ AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);\r
+ OwnCloudVersion ocVersion = new OwnCloudVersion(accManager\r
+ .getUserData(ocAccount, AccountAuthenticator.KEY_OC_VERSION));\r
+ if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {\r
+ return true;\r
+ }\r
+ }\r
+ }*/\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Once the file download has finished -> update view\r
+ * @author Bartek Przybylski\r
+ */\r
+ private class DownloadFinishReceiver extends BroadcastReceiver {\r
+ @Override\r
+ public void onReceive(Context context, Intent intent) {\r
+ getView().findViewById(R.id.fdDownloadBtn).setEnabled(true);\r
+ if (intent.getAction().equals(FileDownloader.BAD_DOWNLOAD_MESSAGE)) {\r
+ Toast.makeText(context, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show();\r
+ \r
+ } else if (intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {\r
+ ((OCFile)mIntent.getParcelableExtra(EXTRA_FILE)).setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));\r
+ updateFileDetails(mIntent);\r
+ }\r
+ }\r
+ \r
+ }\r
+\r