added an option to delete files after encryption or decryption, also no longer delete the output file if either fails

This commit is contained in:
Thialfihar 2010-05-04 15:56:55 +00:00
parent f34fcaabf3
commit 3008f2c51f
8 changed files with 128 additions and 7 deletions

View File

@ -24,9 +24,7 @@
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dip"
android:paddingRight="5dip">
android:orientation="horizontal">
<TextView
android:id="@+id/label_filename"
@ -49,6 +47,35 @@
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/label_delete_after_decryption"
android:text="@string/label_delete_after_decryption"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_gravity="center_vertical"
android:paddingRight="10dip"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"/>
<CheckBox
android:id="@+id/delete_after_decryption"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider"
android:layout_marginBottom="5dip"/>
<LinearLayout
android:id="@+id/layout_signature"
android:orientation="horizontal"

View File

@ -60,6 +60,29 @@
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/label_delete_after_encryption"
android:text="@string/label_delete_after_encryption"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_gravity="center_vertical"
android:paddingRight="10dip"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"/>
<CheckBox
android:id="@+id/delete_after_encryption"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"

View File

@ -65,7 +65,8 @@
<string name="label_algorithm">Algorithm</string>
<string name="label_ascii_armour">ASCII Armour</string>
<string name="label_select_public_keys">Public Key(s)</string>
<string name="label_delete_after_encryption">Delete After Encryption</string>
<string name="label_delete_after_decryption">Delete After Decryption</string>
<string name="label_encryption_algorithm">Encryption Algorithm</string>
<string name="label_hash_algorithm">Hash Algorithm</string>

View File

@ -16,6 +16,8 @@
package org.thialfihar.android.apg;
import java.io.File;
import org.bouncycastle2.bcpg.HashAlgorithmTags;
import org.bouncycastle2.openpgp.PGPEncryptedData;
@ -29,6 +31,7 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
public class BaseActivity extends Activity
implements Runnable, ProgressDialogUpdater,
@ -38,6 +41,7 @@ public class BaseActivity extends Activity
private Thread mRunningThread = null;
private long mSecretKeyId = 0;
private String mDeleteFile = null;
protected static SharedPreferences mPreferences = null;
private Handler mHandler = new Handler() {
@ -137,6 +141,38 @@ public class BaseActivity extends Activity
return alert.create();
}
case Id.dialog.delete_file: {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setIcon(android.R.drawable.ic_dialog_alert);
alert.setTitle("Warning");
alert.setMessage("Are you sure you want to delete\n" + getDeleteFile() + "?");
alert.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
removeDialog(Id.dialog.delete_file);
File file = new File(getDeleteFile());
String msg = "";
if (file.delete()) {
msg = "Successfully deleted.";
} else {
msg = "Error: deleting '" + file + "' failed";
}
Toast.makeText(BaseActivity.this,
msg, Toast.LENGTH_SHORT).show();
}
});
alert.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
removeDialog(Id.dialog.delete_file);
}
});
alert.setCancelable(true);
return alert.create();
}
default: {
break;
@ -295,4 +331,12 @@ public class BaseActivity extends Activity
editor.putBoolean(Constants.pref.has_seen_change_log, value);
editor.commit();
}
protected void setDeleteFile(String deleteFile) {
mDeleteFile = deleteFile;
}
protected String getDeleteFile() {
return mDeleteFile;
}
}

View File

@ -40,6 +40,7 @@ import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
@ -49,6 +50,7 @@ import android.widget.Toast;
public class DecryptFileActivity extends BaseActivity {
private EditText mFilename = null;
private CheckBox mDeleteAfter = null;
private ImageButton mBrowse = null;
private Button mDecryptButton = null;
private LinearLayout mSignatureLayout = null;
@ -75,6 +77,8 @@ public class DecryptFileActivity extends BaseActivity {
}
});
mDeleteAfter = (CheckBox) findViewById(R.id.delete_after_decryption);
mDecryptButton = (Button) findViewById(R.id.btn_decrypt);
mDecryptButton.setOnClickListener(new OnClickListener() {
@Override
@ -127,6 +131,12 @@ public class DecryptFileActivity extends BaseActivity {
return;
}
File file = new File(mInputFilename);
if (!file.exists() || !file.isFile()) {
Toast.makeText(this, "Error: file not found", Toast.LENGTH_SHORT).show();
return;
}
try {
InputStream in = new FileInputStream(mInputFilename);
try {
@ -303,6 +313,10 @@ public class DecryptFileActivity extends BaseActivity {
Toast.makeText(DecryptFileActivity.this,
"Successfully decrypted.",
Toast.LENGTH_SHORT).show();
if (mDeleteAfter.isChecked()) {
setDeleteFile(mInputFilename);
showDialog(Id.dialog.delete_file);
}
}
mSignatureLayout.setVisibility(View.INVISIBLE);

View File

@ -58,6 +58,7 @@ import android.widget.RadioGroup.OnCheckedChangeListener;
public class EncryptFileActivity extends BaseActivity {
private EditText mFilename = null;
private CheckBox mDeleteAfter = null;
private ImageButton mBrowse = null;
private CheckBox mSign = null;
private TextView mMainUserId = null;
@ -110,6 +111,8 @@ public class EncryptFileActivity extends BaseActivity {
}
});
mDeleteAfter = (CheckBox) findViewById(R.id.delete_after_encryption);
mEncryptButton = (Button) findViewById(R.id.btn_encrypt);
mSign = (CheckBox) findViewById(R.id.sign);
mMainUserId = (TextView) findViewById(R.id.main_user_id);
@ -278,6 +281,12 @@ public class EncryptFileActivity extends BaseActivity {
return;
}
File file = new File(mInputFilename);
if (!file.exists() || !file.isFile()) {
Toast.makeText(this, "Error: file not found", Toast.LENGTH_SHORT).show();
return;
}
// symmetric encryption
if (mEncryptionMode.getCheckedRadioButtonId() == R.id.use_symmetric) {
boolean gotPassPhrase = false;
@ -385,9 +394,6 @@ public class EncryptFileActivity extends BaseActivity {
if (error != null) {
data.putString("error", error);
// delete the file if an error occurred
File file = new File(mOutputFilename);
file.delete();
}
msg.setData(data);
@ -505,6 +511,10 @@ public class EncryptFileActivity extends BaseActivity {
Toast.makeText(EncryptFileActivity.this,
"Successfully encrypted.",
Toast.LENGTH_SHORT).show();
if (mDeleteAfter.isChecked()) {
setDeleteFile(mInputFilename);
showDialog(Id.dialog.delete_file);
}
}
}
}

View File

@ -70,6 +70,7 @@ public final class Id {
public static final int about = 0x2107000f;
public static final int change_log = 0x21070010;
public static final int output_filename = 0x21070011;
public static final int delete_file = 0x21070012;
}
public static final class task {

View File

@ -221,6 +221,7 @@ public class MainActivity extends BaseActivity {
SpannableString info =
new SpannableString("Read the warnings!\n\n" +
"Changes:\n" +
"* option to delete file after encryption/decryption\n" +
"* fixed several crashes\n" +
"* can encrypt large files\n" +
"* better progress bar calculation\n" +