+++ /dev/null
-/* ownCloud Android client application\r
- * Copyright (C) 2011 Bartek Przybylski\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- *\r
- */\r
-package eu.alefzero.owncloud.ui.fragment;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import org.apache.commons.httpclient.HttpException;\r
-import org.apache.commons.httpclient.methods.GetMethod;\r
-import org.apache.commons.httpclient.methods.PostMethod;\r
-import org.apache.commons.httpclient.methods.StringRequestEntity;\r
-import org.apache.commons.httpclient.params.HttpConnectionManagerParams;\r
-import org.apache.http.HttpStatus;\r
-import org.apache.http.NameValuePair;\r
-import org.apache.http.client.utils.URLEncodedUtils;\r
-import org.apache.http.message.BasicNameValuePair;\r
-import org.apache.http.protocol.HTTP;\r
-import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;\r
-import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;\r
-import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;\r
-import org.json.JSONException;\r
-import org.json.JSONObject;\r
-\r
-import android.accounts.Account;\r
-import android.accounts.AccountManager;\r
-import android.annotation.SuppressLint;\r
-import android.app.Activity;\r
-import android.content.ActivityNotFoundException;\r
-import android.content.BroadcastReceiver;\r
-import android.content.Context;\r
-import android.content.Intent;\r
-import android.content.IntentFilter;\r
-import android.content.res.Resources.NotFoundException;\r
-import android.graphics.Bitmap;\r
-import android.graphics.BitmapFactory;\r
-import android.graphics.BitmapFactory.Options;\r
-import android.graphics.Point;\r
-import android.net.Uri;\r
-import android.os.AsyncTask;\r
-import android.os.Bundle;\r
-import android.os.Handler;\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.View;\r
-import android.view.View.OnClickListener;\r
-import android.view.ViewGroup;\r
-import android.view.WindowManager.LayoutParams;\r
-import android.webkit.MimeTypeMap;\r
-import android.widget.Button;\r
-import android.widget.CheckBox;\r
-import android.widget.ImageView;\r
-import android.widget.TextView;\r
-import android.widget.Toast;\r
-\r
-import com.actionbarsherlock.app.SherlockDialogFragment;\r
-import com.actionbarsherlock.app.SherlockFragment;\r
-\r
-import eu.alefzero.owncloud.AccountUtils;\r
-import eu.alefzero.owncloud.DisplayUtils;\r
-import eu.alefzero.owncloud.R;\r
-import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
-import eu.alefzero.owncloud.datamodel.FileDataStorageManager;\r
-import eu.alefzero.owncloud.datamodel.OCFile;\r
-import eu.alefzero.owncloud.files.services.FileDownloader;\r
-import eu.alefzero.owncloud.files.services.FileUploader;\r
-import eu.alefzero.owncloud.ui.activity.FileDetailActivity;\r
-import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;\r
-import eu.alefzero.owncloud.utils.OwnCloudVersion;\r
-import eu.alefzero.webdav.WebdavClient;\r
-import eu.alefzero.webdav.WebdavUtils;\r
-\r
-/**\r
- * This Fragment is used to display the details about a file.\r
- * \r
- * @author Bartek Przybylski\r
- * \r
- */\r
-public class FileDetailFragment extends SherlockFragment implements\r
- OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener {\r
-\r
- public static final String EXTRA_FILE = "FILE";\r
- public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
-\r
- private FileDetailFragment.ContainerActivity mContainerActivity;\r
- \r
- private int mLayout;\r
- private View mView;\r
- private OCFile mFile;\r
- private Account mAccount;\r
- private ImageView mPreview;\r
- \r
- private DownloadFinishReceiver mDownloadFinishReceiver;\r
- private UploadFinishReceiver mUploadFinishReceiver;\r
-\r
- private static final String TAG = "FileDetailFragment";\r
- public static final String FTAG = "FileDetails"; \r
- public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";\r
-\r
- \r
- /**\r
- * Creates an empty details fragment.\r
- * \r
- * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically. \r
- */\r
- public FileDetailFragment() {\r
- mFile = null;\r
- mAccount = null;\r
- mLayout = R.layout.file_details_empty;\r
- }\r
- \r
- \r
- /**\r
- * Creates a details fragment.\r
- * \r
- * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).\r
- * \r
- * @param fileToDetail An {@link OCFile} to show in the fragment\r
- * @param ocAccount An ownCloud account; needed to start downloads\r
- */\r
- public FileDetailFragment(OCFile fileToDetail, Account ocAccount){\r
- mFile = fileToDetail;\r
- mAccount = ocAccount;\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
- /**\r
- * {@inheritDoc}\r
- */\r
- @Override\r
- public void onAttach(Activity activity) {\r
- super.onAttach(activity);\r
- try {\r
- mContainerActivity = (ContainerActivity) activity;\r
- } catch (ClassCastException e) {\r
- throw new ClassCastException(activity.toString() + " must implement FileListFragment.ContainerActivity");\r
- }\r
- }\r
- \r
- \r
- @Override\r
- public View onCreateView(LayoutInflater inflater, ViewGroup container,\r
- Bundle savedInstanceState) {\r
- super.onCreateView(inflater, container, savedInstanceState);\r
- \r
- if (savedInstanceState != null) {\r
- mFile = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE);\r
- mAccount = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_ACCOUNT);\r
- }\r
- \r
- View view = null;\r
- view = inflater.inflate(mLayout, container, false);\r
- mView = view;\r
- \r
- if (mLayout == R.layout.file_details_fragment) {\r
- mView.findViewById(R.id.fdKeepInSync).setOnClickListener(this);\r
- mView.findViewById(R.id.fdRenameBtn).setOnClickListener(this);\r
- mView.findViewById(R.id.fdDownloadBtn).setOnClickListener(this);\r
- mView.findViewById(R.id.fdOpenBtn).setOnClickListener(this);\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
- }\r
- \r
- updateFileDetails();\r
- return view;\r
- }\r
- \r
-\r
- @Override\r
- public void onSaveInstanceState(Bundle outState) {\r
- Log.i(getClass().toString(), "onSaveInstanceState() start");\r
- super.onSaveInstanceState(outState);\r
- outState.putParcelable(FileDetailFragment.EXTRA_FILE, mFile);\r
- outState.putParcelable(FileDetailFragment.EXTRA_ACCOUNT, mAccount);\r
- Log.i(getClass().toString(), "onSaveInstanceState() end");\r
- }\r
-\r
- \r
- @Override\r
- public void onResume() {\r
- super.onResume();\r
- \r
- mDownloadFinishReceiver = new DownloadFinishReceiver();\r
- IntentFilter filter = new IntentFilter(\r
- FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
- getActivity().registerReceiver(mDownloadFinishReceiver, filter);\r
- \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);\r
- }\r
-\r
- @Override\r
- public void onPause() {\r
- super.onPause();\r
- \r
- getActivity().unregisterReceiver(mDownloadFinishReceiver);\r
- mDownloadFinishReceiver = null;\r
- \r
- getActivity().unregisterReceiver(mUploadFinishReceiver);\r
- mUploadFinishReceiver = null;\r
- \r
- if (mPreview != null) {\r
- mPreview = null;\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
- case R.id.fdDownloadBtn: {\r
- Intent i = new Intent(getActivity(), FileDownloader.class);\r
- i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);\r
- i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath());\r
- i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath());\r
- i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength());\r
- \r
- // update ui \r
- setButtonsForTransferring();\r
- \r
- getActivity().startService(i);\r
- mContainerActivity.onFileStateChanged(); // this is not working; it is performed before the fileDownloadService registers it as 'in progress'\r
- break;\r
- }\r
- case R.id.fdKeepInSync: {\r
- CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);\r
- mFile.setKeepInSync(cb.isChecked());\r
- FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());\r
- fdsm.saveFile(mFile);\r
- if (mFile.keepInSync()) {\r
- onClick(getView().findViewById(R.id.fdDownloadBtn));\r
- } else { \r
- mContainerActivity.onFileStateChanged(); // put inside 'else' to not call it twice (here, and in the virtual click on fdDownloadBtn)\r
- }\r
- break;\r
- }\r
- case R.id.fdRenameBtn: {\r
- EditNameFragment dialog = EditNameFragment.newInstance(mFile.getFileName());\r
- dialog.show(getFragmentManager(), "nameeditdialog");\r
- dialog.setOnDismissListener(this);\r
- break;\r
- } \r
- case R.id.fdRemoveBtn: {\r
- ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(\r
- R.string.confirmation_remove_alert,\r
- new String[]{mFile.getFileName()},\r
- mFile.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote,\r
- mFile.isDown() ? R.string.confirmation_remove_local : -1,\r
- R.string.common_cancel);\r
- confDialog.setOnConfirmationListener(this);\r
- confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);\r
- 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
- i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), 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
- break;\r
- }\r
- default:\r
- Log.e(TAG, "Incorrect view clicked!");\r
- }\r
- \r
- /* else if (v.getId() == R.id.fdShareBtn) {\r
- Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));\r
- t.start();\r
- }*/\r
- }\r
- \r
- \r
- @Override\r
- public void onConfirmation(String callerTag) {\r
- if (callerTag.equals(FTAG_CONFIRMATION)) {\r
- FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
- if (fdsm.getFileById(mFile.getFileId()) != null) {\r
- new Thread(new RemoveRunnable(mFile, mAccount, new Handler())).start();\r
- boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
- getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
- }\r
- }\r
- }\r
- \r
- @Override\r
- public void onNeutral(String callerTag) {\r
- FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
- File f = null;\r
- if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) {\r
- f.delete();\r
- mFile.setStoragePath(null);\r
- fdsm.saveFile(mFile);\r
- updateFileDetails(mFile, mAccount);\r
- }\r
- }\r
- \r
- @Override\r
- public void onCancel(String callerTag) {\r
- Log.d(TAG, "REMOVAL CANCELED");\r
- }\r
- \r
- \r
- /**\r
- * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.\r
- * \r
- * @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
- }\r
-\r
- \r
- /**\r
- * Can be used to get the file that is currently being displayed.\r
- * @return The file on the screen.\r
- */\r
- public OCFile getDisplayedFile(){\r
- return mFile;\r
- }\r
- \r
- /**\r
- * Use this method to signal this Activity that it shall update its view.\r
- * \r
- * @param file : An {@link OCFile}\r
- */\r
- public void updateFileDetails(OCFile file, Account ocAccount) {\r
- mFile = file;\r
- mAccount = ocAccount;\r
- updateFileDetails();\r
- }\r
- \r
-\r
- /**\r
- * Updates the view with all relevant details about that file.\r
- */\r
- public void updateFileDetails() {\r
-\r
- if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) {\r
- \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
- CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync);\r
- cb.setChecked(mFile.keepInSync());\r
-\r
- // configure UI for depending upon local state of the file\r
- if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) {\r
- setButtonsForTransferring();\r
- \r
- } else if (mFile.isDown()) {\r
- // Update preview\r
- if (mFile.getMimetype().startsWith("image/")) {\r
- BitmapLoader bl = new BitmapLoader();\r
- bl.execute(new String[]{mFile.getStoragePath()});\r
- }\r
- \r
- setButtonsForDown();\r
- \r
- } else {\r
- setButtonsForRemote();\r
- }\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
- * Enables or disables buttons for a file being downloaded\r
- */\r
- private void setButtonsForTransferring() {\r
- if (!isEmpty()) {\r
- Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
- //downloadButton.setText(R.string.filedetails_download_in_progress); // ugly\r
- downloadButton.setEnabled(false); // TODO replace it with a 'cancel download' button\r
- \r
- // let's protect the user from himself ;)\r
- ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(false);\r
- ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(false);\r
- ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(false);\r
- }\r
- }\r
- \r
- /**\r
- * Enables or disables buttons for a file locally available \r
- */\r
- private void setButtonsForDown() {\r
- if (!isEmpty()) {\r
- Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
- //downloadButton.setText(R.string.filedetails_redownload); // ugly\r
- downloadButton.setEnabled(true);\r
- \r
- ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(true);\r
- ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true);\r
- ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(true);\r
- }\r
- }\r
-\r
- /**\r
- * Enables or disables buttons for a file not locally available \r
- */\r
- private void setButtonsForRemote() {\r
- if (!isEmpty()) {\r
- Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
- //downloadButton.setText(R.string.filedetails_download); // unnecessary\r
- downloadButton.setEnabled(true);\r
- \r
- ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(false);\r
- ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true);\r
- ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(true);\r
- }\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 creation time\r
- */\r
- private boolean ocVersionSupportsTimeCreated(){\r
- /*if(mAccount != null){\r
- AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);\r
- OwnCloudVersion ocVersion = new OwnCloudVersion(accManager\r
- .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
- if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {\r
- return true;\r
- }\r
- }*/\r
- return false;\r
- }\r
- \r
- \r
- /**\r
- * Interface to implement by any Activity that includes some instance of FileDetailFragment\r
- * \r
- * @author David A. Velasco\r
- */\r
- public interface ContainerActivity {\r
-\r
- /**\r
- * Callback method invoked when the detail fragment wants to notice its container \r
- * activity about a relevant state the file shown by the fragment.\r
- * \r
- * Added to notify to FileDisplayActivity about the need of refresh the files list. \r
- * \r
- * Currently called when:\r
- * - a download is started;\r
- * - a rename is completed;\r
- * - a deletion is completed;\r
- * - the 'inSync' flag is changed;\r
- */\r
- public void onFileStateChanged();\r
- \r
- }\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
- String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
-\r
- if (!isEmpty() && accountName.equals(mAccount.name)) {\r
- boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);\r
- String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
- if (mFile.getRemotePath().equals(downloadedRemotePath)) {\r
- if (downloadWasFine) {\r
- mFile.setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH)); // updates the local object without accessing the database again\r
- }\r
- updateFileDetails(); // it updates the buttons; must be called although !downloadWasFine\r
- }\r
- }\r
- }\r
- }\r
- \r
- \r
- /**\r
- * Once the file upload has finished -> update view\r
- * \r
- * Being notified about the finish of an upload is necessary for the next sequence:\r
- * 1. Upload a big file.\r
- * 2. Force a synchronization; if it finished before the upload, the file in transfer will be included in the local database and in the file list\r
- * of its containing folder; the the server includes it in the PROPFIND requests although it's not fully upload. \r
- * 3. Click the file in the list to see its details.\r
- * 4. Wait for the upload finishes; at this moment, the details view must be refreshed to enable the action buttons.\r
- */\r
- private class UploadFinishReceiver extends BroadcastReceiver {\r
- @Override\r
- public void onReceive(Context context, Intent intent) {\r
- String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);\r
-\r
- if (!isEmpty() && accountName.equals(mAccount.name)) {\r
- boolean uploadWasFine = intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false);\r
- String uploadRemotePath = intent.getStringExtra(FileUploader.EXTRA_REMOTE_PATH);\r
- if (mFile.getRemotePath().equals(uploadRemotePath)) {\r
- if (uploadWasFine) {\r
- FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());\r
- mFile = fdsm.getFileByPath(mFile.getRemotePath());\r
- }\r
- updateFileDetails(); // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server\r
- }\r
- }\r
- }\r
- }\r
- \r
-\r
- // this is a temporary class for sharing purposes, it need to be replaced in transfer service\r
- private class ShareRunnable implements Runnable {\r
- private String mPath;\r
-\r
- public ShareRunnable(String path) {\r
- mPath = path;\r
- }\r
- \r
- public void run() {\r
- AccountManager am = AccountManager.get(getActivity());\r
- Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());\r
- OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(account, AccountAuthenticator.KEY_OC_VERSION));\r
- String url = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + AccountUtils.getWebdavPath(ocv);\r
-\r
- Log.d("share", "sharing for version " + ocv.toString());\r
-\r
- if (ocv.compareTo(new OwnCloudVersion(0x040000)) >= 0) {\r
- String APPS_PATH = "/apps/files_sharing/";\r
- String SHARE_PATH = "ajax/share.php";\r
-\r
- String SHARED_PATH = "/apps/files_sharing/get.php?token=";\r
- \r
- final String WEBDAV_SCRIPT = "webdav.php";\r
- final String WEBDAV_FILES_LOCATION = "/files/";\r
- \r
- WebdavClient wc = new WebdavClient();\r
- HttpConnectionManagerParams params = new HttpConnectionManagerParams();\r
- params.setMaxConnectionsPerHost(wc.getHostConfiguration(), 5);\r
-\r
- //wc.getParams().setParameter("http.protocol.single-cookie-header", true);\r
- //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);\r
-\r
- PostMethod post = new PostMethod(am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + APPS_PATH + SHARE_PATH);\r
-\r
- post.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" );\r
- post.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));\r
- List<NameValuePair> formparams = new ArrayList<NameValuePair>();\r
- Log.d("share", mPath+"");\r
- formparams.add(new BasicNameValuePair("sources",mPath));\r
- formparams.add(new BasicNameValuePair("uid_shared_with", "public"));\r
- formparams.add(new BasicNameValuePair("permissions", "0"));\r
- post.setRequestEntity(new StringRequestEntity(URLEncodedUtils.format(formparams, HTTP.UTF_8)));\r
-\r
- int status;\r
- try {\r
- PropFindMethod find = new PropFindMethod(url+"/");\r
- find.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));\r
- Log.d("sharer", ""+ url+"/");\r
- wc.setCredentials(account.name.substring(0, account.name.lastIndexOf('@')), am.getPassword(account));\r
- \r
- for (org.apache.commons.httpclient.Header a : find.getRequestHeaders()) {\r
- Log.d("sharer-h", a.getName() + ":"+a.getValue());\r
- }\r
- \r
- int status2 = wc.executeMethod(find);\r
-\r
- Log.d("sharer", "propstatus "+status2);\r
- \r
- GetMethod get = new GetMethod(am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + "/");\r
- get.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));\r
- \r
- status2 = wc.executeMethod(get);\r
-\r
- Log.d("sharer", "getstatus "+status2);\r
- Log.d("sharer", "" + get.getResponseBodyAsString());\r
- \r
- for (org.apache.commons.httpclient.Header a : get.getResponseHeaders()) {\r
- Log.d("sharer", a.getName() + ":"+a.getValue());\r
- }\r
-\r
- status = wc.executeMethod(post);\r
- for (org.apache.commons.httpclient.Header a : post.getRequestHeaders()) {\r
- Log.d("sharer-h", a.getName() + ":"+a.getValue());\r
- }\r
- for (org.apache.commons.httpclient.Header a : post.getResponseHeaders()) {\r
- Log.d("sharer", a.getName() + ":"+a.getValue());\r
- }\r
- String resp = post.getResponseBodyAsString();\r
- Log.d("share", ""+post.getURI().toString());\r
- Log.d("share", "returned status " + status);\r
- Log.d("share", " " +resp);\r
- \r
- if(status != HttpStatus.SC_OK ||resp == null || resp.equals("") || resp.startsWith("false")) {\r
- return;\r
- }\r
-\r
- JSONObject jsonObject = new JSONObject (resp);\r
- String jsonStatus = jsonObject.getString("status");\r
- if(!jsonStatus.equals("success")) throw new Exception("Error while sharing file status != success");\r
- \r
- String token = jsonObject.getString("data");\r
- String uri = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + SHARED_PATH + token; \r
- Log.d("Actions:shareFile ok", "url: " + uri); \r
- \r
- } catch (HttpException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- } catch (IOException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- } catch (JSONException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- } catch (Exception e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- }\r
- \r
- } else if (ocv.compareTo(new OwnCloudVersion(0x030000)) >= 0) {\r
- \r
- }\r
- }\r
- }\r
- \r
- public void onDismiss(EditNameFragment dialog) {\r
- if (dialog instanceof EditNameFragment) {\r
- if (((EditNameFragment)dialog).getResult()) {\r
- String newFilename = ((EditNameFragment)dialog).getNewFilename();\r
- Log.d(TAG, "name edit dialog dismissed with new name " + newFilename);\r
- if (!newFilename.equals(mFile.getFileName())) {\r
- FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
- if (fdsm.getFileById(mFile.getFileId()) != null) {\r
- OCFile newFile = new OCFile(fdsm.getFileById(mFile.getParentId()).getRemotePath() + newFilename);\r
- newFile.setCreationTimestamp(mFile.getCreationTimestamp());\r
- newFile.setFileId(mFile.getFileId());\r
- newFile.setFileLength(mFile.getFileLength());\r
- newFile.setKeepInSync(mFile.keepInSync());\r
- newFile.setLastSyncDate(mFile.getLastSyncDate());\r
- newFile.setMimetype(mFile.getMimetype());\r
- newFile.setModificationTimestamp(mFile.getModificationTimestamp());\r
- newFile.setParentId(mFile.getParentId());\r
- boolean localRenameFails = false;\r
- if (mFile.isDown()) {\r
- File f = new File(mFile.getStoragePath());\r
- Log.e(TAG, f.getAbsolutePath());\r
- localRenameFails = !(f.renameTo(new File(f.getParent() + File.separator + newFilename)));\r
- Log.e(TAG, f.getParent() + File.separator + newFilename);\r
- newFile.setStoragePath(f.getParent() + File.separator + newFilename);\r
- }\r
- \r
- if (localRenameFails) {\r
- Toast msg = Toast.makeText(getActivity(), R.string.rename_local_fail_msg, Toast.LENGTH_LONG); \r
- msg.show();\r
- \r
- } else {\r
- new Thread(new RenameRunnable(mFile, newFile, mAccount, new Handler())).start();\r
- boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
- getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
- }\r
-\r
- }\r
- }\r
- }\r
- } else {\r
- Log.e(TAG, "Unknown dialog instance passed to onDismissDalog: " + dialog.getClass().getCanonicalName());\r
- }\r
- \r
- }\r
- \r
- private class RenameRunnable implements Runnable {\r
- \r
- Account mAccount;\r
- OCFile mOld, mNew;\r
- Handler mHandler;\r
- \r
- public RenameRunnable(OCFile oldFile, OCFile newFile, Account account, Handler handler) {\r
- mOld = oldFile;\r
- mNew = newFile;\r
- mAccount = account;\r
- mHandler = handler;\r
- }\r
- \r
- public void run() {\r
- WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext());\r
- wc.allowSelfsignedCertificates();\r
- AccountManager am = AccountManager.get(getSherlockActivity());\r
- String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);\r
- OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
- String webdav_path = AccountUtils.getWebdavPath(ocv);\r
- Log.d("ASD", ""+baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()));\r
-\r
- Log.e("ASD", Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath()));\r
- LocalMoveMethod move = new LocalMoveMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()),\r
- Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath()));\r
- \r
- boolean success = false;\r
- try {\r
- int status = wc.executeMethod(move);\r
- success = move.succeeded();\r
- Log.d(TAG, "Move returned status: " + status);\r
- \r
- } catch (HttpException e) {\r
- Log.e(TAG, "HTTP Exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);\r
- \r
- } catch (IOException e) {\r
- Log.e(TAG, "I/O Exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);\r
- \r
- } catch (Exception e) {\r
- Log.e(TAG, "Unexpected exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);\r
- }\r
- \r
- if (success) {\r
- FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
- fdsm.removeFile(mOld);\r
- fdsm.saveFile(mNew);\r
- mFile = mNew;\r
- mHandler.post(new Runnable() {\r
- @Override\r
- public void run() { \r
- boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
- getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
- updateFileDetails(mFile, mAccount);\r
- mContainerActivity.onFileStateChanged();\r
- }\r
- });\r
- \r
- } else {\r
- mHandler.post(new Runnable() {\r
- @Override\r
- public void run() {\r
- // undo the local rename\r
- if (mNew.isDown()) {\r
- File f = new File(mNew.getStoragePath());\r
- if (!f.renameTo(new File(mOld.getStoragePath()))) {\r
- // the local rename undoing failed; last chance: save the new local storage path in the old file\r
- mFile.setStoragePath(mNew.getStoragePath());\r
- FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
- fdsm.saveFile(mFile);\r
- }\r
- }\r
- boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
- getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
- try {\r
- Toast msg = Toast.makeText(getActivity(), R.string.rename_server_fail_msg, Toast.LENGTH_LONG); \r
- msg.show();\r
- \r
- } catch (NotFoundException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- });\r
- }\r
- }\r
- private class LocalMoveMethod extends DavMethodBase {\r
-\r
- public LocalMoveMethod(String uri, String dest) {\r
- super(uri);\r
- addRequestHeader(new org.apache.commons.httpclient.Header("Destination", dest));\r
- }\r
-\r
- @Override\r
- public String getName() {\r
- return "MOVE";\r
- }\r
-\r
- @Override\r
- protected boolean isSuccess(int status) {\r
- return status == 201 || status == 204;\r
- }\r
- \r
- }\r
- }\r
- \r
- private static class EditNameFragment extends SherlockDialogFragment implements OnClickListener {\r
-\r
- private String mNewFilename;\r
- private boolean mResult;\r
- private FileDetailFragment mListener;\r
- \r
- static public EditNameFragment newInstance(String filename) {\r
- EditNameFragment f = new EditNameFragment();\r
- Bundle args = new Bundle();\r
- args.putString("filename", filename);\r
- f.setArguments(args);\r
- return f;\r
- }\r
- \r
- @Override\r
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {\r
- View v = inflater.inflate(R.layout.edit_box_dialog, container, false);\r
-\r
- String currentName = getArguments().getString("filename");\r
- if (currentName == null)\r
- currentName = "";\r
- \r
- ((Button)v.findViewById(R.id.cancel)).setOnClickListener(this);\r
- ((Button)v.findViewById(R.id.ok)).setOnClickListener(this);\r
- ((TextView)v.findViewById(R.id.user_input)).setText(currentName);\r
- ((TextView)v.findViewById(R.id.user_input)).requestFocus();\r
- getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);\r
-\r
- mResult = false;\r
- return v;\r
- }\r
- \r
- @Override\r
- public void onClick(View view) {\r
- switch (view.getId()) {\r
- case R.id.ok: {\r
- mNewFilename = ((TextView)getView().findViewById(R.id.user_input)).getText().toString();\r
- mResult = true;\r
- }\r
- case R.id.cancel: { // fallthought\r
- dismiss();\r
- mListener.onDismiss(this);\r
- }\r
- }\r
- }\r
- \r
- void setOnDismissListener(FileDetailFragment listener) {\r
- mListener = listener;\r
- }\r
- \r
- public String getNewFilename() {\r
- return mNewFilename;\r
- }\r
- \r
- // true if user click ok\r
- public boolean getResult() {\r
- return mResult;\r
- }\r
- \r
- }\r
- \r
- private class RemoveRunnable implements Runnable {\r
- \r
- Account mAccount;\r
- OCFile mFileToRemove;\r
- Handler mHandler;\r
- \r
- public RemoveRunnable(OCFile fileToRemove, Account account, Handler handler) {\r
- mFileToRemove = fileToRemove;\r
- mAccount = account;\r
- mHandler = handler;\r
- }\r
- \r
- public void run() {\r
- WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext());\r
- wc.allowSelfsignedCertificates();\r
- AccountManager am = AccountManager.get(getSherlockActivity());\r
- String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);\r
- OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
- String webdav_path = AccountUtils.getWebdavPath(ocv);\r
- Log.d("ASD", ""+baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));\r
-\r
- DeleteMethod delete = new DeleteMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));\r
- \r
- boolean success = false;\r
- int status = -1;\r
- try {\r
- status = wc.executeMethod(delete);\r
- success = (delete.succeeded());\r
- Log.d(TAG, "Delete: returned status " + status);\r
- \r
- } catch (HttpException e) {\r
- Log.e(TAG, "HTTP Exception removing file " + mFileToRemove.getRemotePath(), e);\r
- \r
- } catch (IOException e) {\r
- Log.e(TAG, "I/O Exception removing file " + mFileToRemove.getRemotePath(), e);\r
- \r
- } catch (Exception e) {\r
- Log.e(TAG, "Unexpected exception removing file " + mFileToRemove.getRemotePath(), e);\r
- }\r
- \r
- if (success) {\r
- FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
- fdsm.removeFile(mFileToRemove);\r
- mHandler.post(new Runnable() {\r
- @Override\r
- public void run() {\r
- boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
- getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
- try {\r
- Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);\r
- msg.show();\r
- if (inDisplayActivity) {\r
- // double pane\r
- FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();\r
- transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment\r
- transaction.commit();\r
- mContainerActivity.onFileStateChanged();\r
- \r
- } else {\r
- getActivity().finish();\r
- }\r
- \r
- } catch (NotFoundException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- });\r
- \r
- } else {\r
- mHandler.post(new Runnable() {\r
- @Override\r
- public void run() {\r
- boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
- getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
- try {\r
- Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG); \r
- msg.show();\r
- \r
- } catch (NotFoundException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- });\r
- }\r
- }\r
- \r
- }\r
- \r
- class BitmapLoader extends AsyncTask<String, Void, Bitmap> {\r
- @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20\r
- @Override\r
- protected Bitmap doInBackground(String... params) {\r
- Bitmap result = null;\r
- if (params.length != 1) return result;\r
- String storagePath = params[0];\r
- try {\r
-\r
- BitmapFactory.Options options = new Options();\r
- options.inScaled = true;\r
- options.inPurgeable = true;\r
- options.inJustDecodeBounds = 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
- result = BitmapFactory.decodeFile(storagePath, options);\r
- options.inJustDecodeBounds = false;\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.ceil(Math.max(height, width) / 2048.)));\r
- options.inSampleSize = scale;\r
- }\r
- Display display = getActivity().getWindowManager().getDefaultDisplay();\r
- Point size = new Point();\r
- int screenwidth;\r
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {\r
- display.getSize(size);\r
- screenwidth = size.x;\r
- } else {\r
- screenwidth = display.getWidth();\r
- }\r
-\r
- Log.e("ASD", "W " + width + " SW " + screenwidth);\r
-\r
- if (width > screenwidth) {\r
- scale = (int) Math.ceil((float)width / screenwidth);\r
- options.inSampleSize = scale;\r
- }\r
-\r
- result = BitmapFactory.decodeFile(storagePath, options);\r
-\r
- Log.e("ASD", "W " + options.outWidth + " SW " + options.outHeight);\r
-\r
- } catch (OutOfMemoryError e) {\r
- result = null;\r
- Log.e(TAG, "Out of memory occured for file with size " + storagePath);\r
- \r
- } catch (NoSuchFieldError e) {\r
- result = null;\r
- Log.e(TAG, "Error from access to unexisting field despite protection " + storagePath);\r
- \r
- } catch (Throwable t) {\r
- result = null;\r
- Log.e(TAG, "Unexpected error while creating image preview " + storagePath, t);\r
- }\r
- return result;\r
- }\r
- @Override\r
- protected void onPostExecute(Bitmap result) {\r
- if (result != null && mPreview != null) {\r
- mPreview.setImageBitmap(result);\r
- }\r
- }\r
- \r
- }\r
- \r
-\r
-}\r