fixed crash when file scheme is passed, broadened deletion scope

This commit is contained in:
Abraham Philip 2015-01-09 05:07:50 +05:30
parent 0c3f9ae0a6
commit 16f924eef3

View File

@ -18,6 +18,7 @@
package org.sufficientlysecure.keychain.ui.dialog; package org.sufficientlysecure.keychain.ui.dialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.ContentResolver;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
@ -27,9 +28,12 @@ import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.widget.Toast; import android.widget.Toast;
import org.apache.http.conn.scheme.Scheme;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.util.FileHelper; import org.sufficientlysecure.keychain.util.FileHelper;
import java.io.File;
public class DeleteFileDialogFragment extends DialogFragment { public class DeleteFileDialogFragment extends DialogFragment {
private static final String ARG_DELETE_URI = "delete_uri"; private static final String ARG_DELETE_URI = "delete_uri";
@ -69,21 +73,38 @@ public class DeleteFileDialogFragment extends DialogFragment {
@Override @Override
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
dismiss(); dismiss();
String scheme = deleteUri.getScheme();
// We can not securely delete Uris, so just use usual delete on them if(scheme.equals(ContentResolver.SCHEME_FILE)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if(new File(deleteUri.getPath()).delete()) {
if (DocumentsContract.deleteDocument(getActivity().getContentResolver(), deleteUri)) { Toast.makeText(getActivity(), R.string.file_delete_successful, Toast.LENGTH_SHORT).show();
return;
}
}
else if(scheme.equals(ContentResolver.SCHEME_CONTENT)) {
// We can not securely delete Uris, so just use usual delete on them
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.deleteDocument(getActivity().getContentResolver(), deleteUri)) {
Toast.makeText(getActivity(), R.string.file_delete_successful, Toast.LENGTH_SHORT).show();
return;
}
}
if (getActivity().getContentResolver().delete(deleteUri, null, null) > 0) {
Toast.makeText(getActivity(), R.string.file_delete_successful, Toast.LENGTH_SHORT).show();
return;
}
// some Uri's a ContentResolver fails to delete is handled by the java.io.File's delete
// via the path of the Uri
if(new File(deleteUri.getPath()).delete()) {
Toast.makeText(getActivity(), R.string.file_delete_successful, Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), R.string.file_delete_successful, Toast.LENGTH_SHORT).show();
return; return;
} }
} }
if (getActivity().getContentResolver().delete(deleteUri, null, null) > 0) { Toast.makeText(getActivity(), getActivity().getString(R.string.error_file_delete_failed,
Toast.makeText(getActivity(), R.string.file_delete_successful, Toast.LENGTH_SHORT).show(); deleteFilename), Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(getActivity(), getActivity().getString(R.string.error_file_delete_failed, deleteFilename), Toast.LENGTH_SHORT).show();
// Note: We can't delete every file... // Note: We can't delete every file...
// If possible we should find out if deletion is possible before even showing the option to do so. // If possible we should find out if deletion is possible before even showing the option to do so.