X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/f2b890ae53b5c0c44239504eb516df9f8c438236..4f2f33c9bcfce7fb96c149f8e50188372a3ec61b:/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java diff --git a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java index 9b410936..c94719ee 100644 --- a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java +++ b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java @@ -17,6 +17,7 @@ */ package eu.alefzero.owncloud.ui.fragment; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -35,6 +36,7 @@ import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.jackrabbit.webdav.client.methods.DavMethodBase; +import org.apache.jackrabbit.webdav.client.methods.DeleteMethod; import org.apache.jackrabbit.webdav.client.methods.MoveMethod; import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; import org.json.JSONException; @@ -42,6 +44,8 @@ import org.json.JSONObject; import android.accounts.Account; import android.accounts.AccountManager; +import android.app.AlertDialog; +import android.app.Dialog; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.Context; @@ -49,6 +53,7 @@ import android.content.DialogInterface; import android.content.DialogInterface.OnDismissListener; import android.content.Intent; import android.content.IntentFilter; +import android.content.res.Resources.NotFoundException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; @@ -56,9 +61,12 @@ import android.graphics.Point; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; +import android.os.Environment; import android.os.Handler; import android.preference.PreferenceActivity.Header; +import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.Display; import android.view.LayoutInflater; @@ -84,6 +92,7 @@ import eu.alefzero.owncloud.authenticator.AccountAuthenticator; import eu.alefzero.owncloud.datamodel.FileDataStorageManager; import eu.alefzero.owncloud.datamodel.OCFile; import eu.alefzero.owncloud.files.services.FileDownloader; +import eu.alefzero.owncloud.ui.activity.FileDisplayActivity; import eu.alefzero.owncloud.utils.OwnCloudVersion; import eu.alefzero.webdav.WebdavClient; @@ -94,7 +103,7 @@ import eu.alefzero.webdav.WebdavClient; * */ public class FileDetailFragment extends SherlockFragment implements - OnClickListener { + OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener { public static final String EXTRA_FILE = "FILE"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; @@ -103,11 +112,13 @@ public class FileDetailFragment extends SherlockFragment implements private View mView; private OCFile mFile; private Account mAccount; + private ImageView mPreview; private DownloadFinishReceiver mDownloadFinishReceiver; private static final String TAG = "FileDetailFragment"; public static final String FTAG = "FileDetails"; + public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT"; /** @@ -155,6 +166,14 @@ public class FileDetailFragment extends SherlockFragment implements view = inflater.inflate(mLayout, container, false); mView = view; + if (mLayout == R.layout.file_details_fragment) { + mView.findViewById(R.id.fdKeepInSync).setOnClickListener(this); + //mView.findViewById(R.id.fdShareBtn).setOnClickListener(this); + mView.findViewById(R.id.fdRenameBtn).setOnClickListener(this); + mView.findViewById(R.id.fdRemoveBtn).setOnClickListener(this); + mPreview = (ImageView)mView.findViewById(R.id.fdPreview); + } + updateFileDetails(); return view; } @@ -184,6 +203,9 @@ public class FileDetailFragment extends SherlockFragment implements super.onPause(); getActivity().unregisterReceiver(mDownloadFinishReceiver); mDownloadFinishReceiver = null; + if (mPreview != null) { + mPreview = null; + } } @Override @@ -222,6 +244,12 @@ public class FileDetailFragment extends SherlockFragment implements dialog.show(getFragmentManager(), "nameeditdialog"); dialog.setOnDismissListener(this); break; + } + case R.id.fdRemoveBtn: { + ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance("to remove " + mFile.getFileName()); + confDialog.setOnConfirmationListener(this); + confDialog.show(getFragmentManager(), FTAG_CONFIRMATION); + break; } default: Log.e(TAG, "Incorrect view clicked!"); @@ -232,8 +260,20 @@ public class FileDetailFragment extends SherlockFragment implements t.start(); }*/ } - - + + + @Override + public void onConfirmation(boolean confirmation, String callerTag) { + if (confirmation && callerTag.equals(FTAG_CONFIRMATION)) { + Log.e("ASD","onConfirmation"); + FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver()); + if (fdsm.getFileById(mFile.getFileId()) != null) { + new Thread(new RemoveRunnable(mFile, mAccount, new Handler())).start(); + } + } else if (!confirmation) Log.d(TAG, "REMOVAL CANCELED"); + } + + /** * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced. * @@ -285,75 +325,12 @@ public class FileDetailFragment extends SherlockFragment implements CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync); cb.setChecked(mFile.keepInSync()); - cb.setOnClickListener(this); - //getView().findViewById(R.id.fdShareBtn).setOnClickListener(this); - getView().findViewById(R.id.fdRenameBtn).setOnClickListener(this); if (mFile.getStoragePath() != null) { // Update preview - ImageView preview = (ImageView) getView().findViewById(R.id.fdPreview); - try { - if (mFile.getMimetype().startsWith("image/")) { - BitmapFactory.Options options = new Options(); - options.inScaled = true; - options.inPurgeable = true; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { - options.inPreferQualityOverSpeed = false; - } - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { - options.inMutable = false; - } - - Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options); - - if (bmp != null) { - int width = options.outWidth; - int height = options.outHeight; - int scale = 1; - boolean recycle = false; - if (width >= 2048 || height >= 2048) { - scale = (int) (Math.ceil(Math.max(height, width)/2048.)); - options.inSampleSize = scale; - recycle = true; - } - Display display = getActivity().getWindowManager().getDefaultDisplay(); - Point size = new Point(); - int screenwidth; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) { - display.getSize(size); - screenwidth = size.x; - } else { - screenwidth = display.getWidth(); - } - - Log.e("ASD", "W " + width + " SW " + screenwidth); - - if (width > screenwidth) { - scale = (int) (Math.ceil(Math.max(height, width)/screenwidth)); - options.inSampleSize = scale; - recycle = true; - } - - - if (recycle) bmp.recycle(); - bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options); - - } - if (bmp != null) { - preview.setImageBitmap(bmp); - } - } - } catch (OutOfMemoryError e) { - preview.setVisibility(View.INVISIBLE); - Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength()); - - } catch (NoSuchFieldError e) { - preview.setVisibility(View.INVISIBLE); - Log.e(TAG, "Error from access to unexisting field despite protection " + mFile.getFileLength()); - - } catch (Throwable t) { - preview.setVisibility(View.INVISIBLE); - Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength(), t); + if (mFile.getMimetype().startsWith("image/")) { + BitmapLoader bl = new BitmapLoader(); + bl.execute(new String[]{mFile.getStoragePath()}); } // Change download button to open button @@ -502,7 +479,7 @@ public class FileDetailFragment extends SherlockFragment implements } - // this is a temporary class for sharing purposes, it need to be replacead in transfer service + // this is a temporary class for sharing purposes, it need to be replaced in transfer service private class ShareRunnable implements Runnable { private String mPath; @@ -732,7 +709,9 @@ public class FileDetailFragment extends SherlockFragment implements public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.edit_box_dialog, container, false); - String currentName = getArguments().getString("filename", ""); + String currentName = getArguments().getString("filename"); + if (currentName == null) + currentName = ""; ((Button)v.findViewById(R.id.cancel)).setOnClickListener(this); ((Button)v.findViewById(R.id.ok)).setOnClickListener(this); @@ -772,4 +751,164 @@ public class FileDetailFragment extends SherlockFragment implements } } + + + private class RemoveRunnable implements Runnable { + + Account mAccount; + OCFile mFileToRemove; + Handler mHandler; + + public RemoveRunnable(OCFile fileToRemove, Account account, Handler handler) { + mFileToRemove = fileToRemove; + mAccount = account; + mHandler = handler; + } + + public void run() { + WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext()); + AccountManager am = AccountManager.get(getSherlockActivity()); + String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL); + OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION)); + String webdav_path = AccountUtils.getWebdavPath(ocv); + Log.d("ASD", ""+baseUrl + webdav_path + mFileToRemove.getRemotePath()); + + DeleteMethod delete = new DeleteMethod(baseUrl + webdav_path + mFileToRemove.getRemotePath()); + HttpMethodParams params = delete.getParams(); + params.setSoTimeout(1000); + delete.setParams(params); + + boolean success = false; + try { + int status = wc.executeMethod(delete); + if (delete.succeeded()) { + FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver()); + fdsm.removeFile(mFileToRemove); + mHandler.post(new Runnable() { + @Override + public void run() { + try { + Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG); + msg.show(); + if (getActivity() instanceof FileDisplayActivity) { + // double pane + FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); + transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment + transaction.commit(); + + } else { + getActivity().finish(); + } + + } catch (NotFoundException e) { + e.printStackTrace(); + } + } + }); + success = true; + } + Log.e("ASD", ""+ delete.getQueryString()); + Log.d("delete", "returned status " + status); + + } catch (HttpException e) { + e.printStackTrace(); + + } catch (IOException e) { + e.printStackTrace(); + + } finally { + if (!success) { + mHandler.post(new Runnable() { + @Override + public void run() { + try { + Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG); + msg.show(); + + } catch (NotFoundException e) { + e.printStackTrace(); + } + } + }); + } + } + } + + } + + class BitmapLoader extends AsyncTask { + @Override + protected Bitmap doInBackground(String... params) { + Bitmap result = null; + if (params.length != 1) return result; + String storagePath = params[0]; + try { + + BitmapFactory.Options options = new Options(); + options.inScaled = true; + options.inPurgeable = true; + options.inJustDecodeBounds = true; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { + options.inPreferQualityOverSpeed = false; + } + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { + options.inMutable = false; + } + + result = BitmapFactory.decodeFile(storagePath, options); + options.inJustDecodeBounds = false; + + int width = options.outWidth; + int height = options.outHeight; + int scale = 1; + boolean recycle = false; + if (width >= 2048 || height >= 2048) { + scale = (int) Math.ceil((Math.ceil(Math.max(height, width) / 2048.))); + options.inSampleSize = scale; + } + Display display = getActivity().getWindowManager().getDefaultDisplay(); + Point size = new Point(); + int screenwidth; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) { + display.getSize(size); + screenwidth = size.x; + } else { + screenwidth = display.getWidth(); + } + + Log.e("ASD", "W " + width + " SW " + screenwidth); + + if (width > screenwidth) { + scale = (int) Math.ceil((float)width / screenwidth); + options.inSampleSize = scale; + } + + result = BitmapFactory.decodeFile(storagePath, options); + + Log.e("ASD", "W " + options.outWidth + " SW " + options.outHeight); + + } catch (OutOfMemoryError e) { + result = null; + Log.e(TAG, "Out of memory occured for file with size " + storagePath); + + } catch (NoSuchFieldError e) { + result = null; + Log.e(TAG, "Error from access to unexisting field despite protection " + storagePath); + + } catch (Throwable t) { + result = null; + Log.e(TAG, "Unexpected error while creating image preview " + storagePath, t); + } + return result; + } + @Override + protected void onPostExecute(Bitmap result) { + if (result != null) { + mPreview.setImageBitmap(result); + } + } + + } + + }