put return values into the Id class as well, changed encrypt file layout to use symmetric OR asymmetric again, layout and string adjustments

This commit is contained in:
Thialfihar 2010-04-29 01:11:26 +00:00
parent e83a5311ae
commit 88fc659ff6
5 changed files with 164 additions and 124 deletions

View File

@ -60,6 +60,12 @@
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider"
android:layout_marginBottom="5dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
@ -112,76 +118,29 @@
android:background="?android:attr/listDivider"
android:layout_marginBottom="5dip"/>
<LinearLayout
<RadioGroup
android:id="@+id/encryption_mode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/label_sign"
android:text="@string/label_sign"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="5dip">
<TextView
android:id="@+id/main_user_id"
android:text="Main User Id"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
<TextView
android:id="@+id/main_user_id_rest"
android:text="Main User Id Rest"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
</LinearLayout>
<CheckBox
android:id="@+id/sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="3dip"
android:orientation="horizontal">
<TextView
android:id="@+id/label_select_public_keys"
android:text="@string/label_select_public_keys"
android:textAppearance="?android:attr/textAppearanceMedium"
<RadioButton
android:id="@+id/use_asymmetric"
android:text="@string/use_asymmetric"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
<Button
android:text="@string/btn_selectEncryptKeys"
android:id="@+id/btn_selectEncryptKeys"
android:layout_width="wrap_content"
<RadioButton
android:id="@+id/use_symmetric"
android:text="@string/use_symmetric"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</RadioGroup>
<View
android:layout_width="fill_parent"
@ -190,30 +149,85 @@
android:layout_marginBottom="5dip"/>
<LinearLayout
android:id="@+id/layout_asymmetric"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:orientation="vertical">
<TextView
android:id="@+id/label_use_pass_phrase"
android:text="@string/label_use_pass_phrase"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_gravity="center_vertical"
android:paddingRight="10dip"
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"/>
android:orientation="horizontal">
<CheckBox
android:id="@+id/use_pass_phrase"
<TextView
android:id="@+id/label_sign"
android:text="@string/label_sign"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="5dip">
<TextView
android:id="@+id/main_user_id"
android:text="Main User Id"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
<TextView
android:id="@+id/main_user_id_rest"
android:text="Main User Id Rest"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
</LinearLayout>
<CheckBox
android:id="@+id/sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"/>
android:paddingBottom="3dip"
android:orientation="horizontal">
<TextView
android:id="@+id/label_select_public_keys"
android:text="@string/label_select_public_keys"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
<Button
android:text="@string/btn_selectEncryptKeys"
android:id="@+id/btn_selectEncryptKeys"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>
<TableLayout
android:id="@+id/layout_pass_phrase"
android:id="@+id/layout_symmetric"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:stretchColumns="1">
@ -223,6 +237,7 @@
<TextView
android:id="@+id/label_pass_phrase"
android:text="@string/label_pass_phrase"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
@ -241,6 +256,7 @@
<TextView
android:id="@+id/label_pass_phrase_again"
android:text="@string/label_pass_phrase_again"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
@ -256,6 +272,12 @@
</TableLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider"
android:layout_marginBottom="5dip"/>
</LinearLayout>
</ScrollView>

View File

@ -65,11 +65,13 @@
<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_use_pass_phrase">Use Pass Phrase</string>
<string name="label_encryption_algorithm">Encryption Algorithm</string>
<string name="label_hash_algorithm">Hash Algorithm</string>
<string name="use_asymmetric">Public Key</string>
<string name="use_symmetric">Pass Phrase</string>
<string name="section_defaults">Defaults</string>
<string name="sign_only">Sign only</string>

View File

@ -138,11 +138,6 @@ public class Apg {
protected static boolean mInitialized = false;
protected static final int RETURN_NO_MASTER_KEY = -2;
protected static final int RETURN_ERROR = -1;
protected static final int RETURN_OK = 0;
protected static final int RETURN_UPDATED = 1;
protected static HashMap<Long, Integer> mSecretKeyIdToIdMap;
protected static HashMap<Long, PGPSecretKeyRing> mSecretKeyIdToKeyRingMap;
protected static HashMap<Long, Integer> mPublicKeyIdToIdMap;
@ -586,14 +581,14 @@ public class Apg {
PGPPublicKey masterKey = getMasterKey(keyRing);
if (masterKey == null) {
return RETURN_NO_MASTER_KEY;
return Id.return_value.no_master_key;
}
try {
keyRing.encode(out);
out.close();
} catch (IOException e) {
return RETURN_ERROR;
return Id.return_value.error;
}
values.put(PublicKeys.KEY_ID, masterKey.getKeyID());
@ -603,10 +598,10 @@ public class Apg {
Cursor cursor = context.managedQuery(uri, PUBLIC_KEY_PROJECTION, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
context.getContentResolver().update(uri, values, null, null);
return RETURN_UPDATED;
return Id.return_value.updated;
} else {
context.getContentResolver().insert(PublicKeys.CONTENT_URI, values);
return RETURN_OK;
return Id.return_value.ok;
}
}
@ -616,14 +611,14 @@ public class Apg {
PGPSecretKey masterKey = getMasterKey(keyRing);
if (masterKey == null) {
return RETURN_NO_MASTER_KEY;
return Id.return_value.no_master_key;
}
try {
keyRing.encode(out);
out.close();
} catch (IOException e) {
return RETURN_ERROR;
return Id.return_value.error;
}
values.put(SecretKeys.KEY_ID, masterKey.getKeyID());
@ -633,10 +628,10 @@ public class Apg {
Cursor cursor = context.managedQuery(uri, SECRET_KEY_PROJECTION, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
context.getContentResolver().update(uri, values, null, null);
return RETURN_UPDATED;
return Id.return_value.updated;
} else {
context.getContentResolver().insert(SecretKeys.CONTENT_URI, values);
return RETURN_OK;
return Id.return_value.ok;
}
}
@ -690,13 +685,13 @@ public class Apg {
retValue = saveKeyRing(context, publicKeyRing);
}
if (retValue == RETURN_ERROR) {
if (retValue == Id.return_value.error) {
throw new GeneralException("error saving some key(s)");
}
if (retValue == RETURN_UPDATED) {
if (retValue == Id.return_value.updated) {
++oldKeys;
} else if (retValue == RETURN_OK) {
} else if (retValue == Id.return_value.ok) {
++newKeys;
}
}

View File

@ -51,9 +51,11 @@ import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class EncryptFileActivity extends BaseActivity {
private EditText mFilename = null;
@ -65,8 +67,9 @@ public class EncryptFileActivity extends BaseActivity {
private EditText mPassPhrase = null;
private EditText mPassPhraseAgain = null;
private CheckBox mAsciiArmour = null;
private CheckBox mUsePassPhrase = null;
private ViewGroup mPassPhraseLayout = null;
private RadioGroup mEncryptionMode = null;
private ViewGroup mAsymmetricLayout = null;
private ViewGroup mSymmetricLayout = null;
private Button mEncryptButton = null;
private Button mSelectKeysButton = null;
@ -145,28 +148,36 @@ public class EncryptFileActivity extends BaseActivity {
}
}
mUsePassPhrase = (CheckBox) findViewById(R.id.use_pass_phrase);
mPassPhraseLayout = (ViewGroup) findViewById(R.id.layout_pass_phrase);
mEncryptionMode = (RadioGroup) findViewById(R.id.encryption_mode);
mAsymmetricLayout = (ViewGroup) findViewById(R.id.layout_asymmetric);
mSymmetricLayout = (ViewGroup) findViewById(R.id.layout_symmetric);
mPassPhrase = (EditText) findViewById(R.id.pass_phrase);
mPassPhraseAgain = (EditText) findViewById(R.id.pass_phrase_again);
mUsePassPhrase.setOnClickListener(new OnClickListener() {
mEncryptionMode.check(R.id.use_asymmetric);
mEncryptionMode.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
if (checkBox.isChecked()) {
mPassPhraseLayout.setVisibility(ViewGroup.VISIBLE);
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.use_symmetric) {
mAsymmetricLayout.setVisibility(ViewGroup.GONE);
mSymmetricLayout.setVisibility(ViewGroup.VISIBLE);
mEncryptionKeyIds = null;
setSecretKeyId(0);
} else {
mAsymmetricLayout.setVisibility(ViewGroup.VISIBLE);
mSymmetricLayout.setVisibility(ViewGroup.GONE);
mPassPhrase.setText("");
mPassPhraseAgain.setText("");
mPassPhraseLayout.setVisibility(ViewGroup.GONE);
}
updateView();
}
});
if (mUsePassPhrase.isChecked()) {
mPassPhraseLayout.setVisibility(ViewGroup.VISIBLE);
if (mEncryptionMode.getCheckedRadioButtonId() == R.id.use_symmetric) {
mAsymmetricLayout.setVisibility(ViewGroup.GONE);
mSymmetricLayout.setVisibility(ViewGroup.VISIBLE);
} else {
mPassPhraseLayout.setVisibility(ViewGroup.GONE);
mAsymmetricLayout.setVisibility(ViewGroup.VISIBLE);
mSymmetricLayout.setVisibility(ViewGroup.GONE);
}
mEncryptButton.setOnClickListener(new OnClickListener() {
@ -258,8 +269,8 @@ public class EncryptFileActivity extends BaseActivity {
}
// symmetric encryption
boolean gotPassPhrase = false;
if (mUsePassPhrase.isChecked()) {
if (mEncryptionMode.getCheckedRadioButtonId() == R.id.use_symmetric) {
boolean gotPassPhrase = false;
String passPhrase = mPassPhrase.getText().toString();
String passPhraseAgain = mPassPhraseAgain.getText().toString();
if (!passPhrase.equals(passPhraseAgain)) {
@ -274,19 +285,19 @@ public class EncryptFileActivity extends BaseActivity {
Toast.LENGTH_SHORT).show();
return;
}
}
} else {
boolean encryptIt = mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0;
// for now require at least one form of encryption
if (!encryptIt) {
Toast.makeText(this, "Select at least one encryption key.",
Toast.LENGTH_SHORT).show();
return;
}
boolean encryptIt = mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0;
// for now require at least one form of encryption
if (!encryptIt && !gotPassPhrase) {
Toast.makeText(this, "Select at least one encryption key or a pass phrase.",
Toast.LENGTH_SHORT).show();
return;
}
if (getSecretKeyId() != 0 && Apg.getPassPhrase() == null) {
showDialog(Id.dialog.pass_phrase);
return;
if (getSecretKeyId() != 0 && Apg.getPassPhrase() == null) {
showDialog(Id.dialog.pass_phrase);
return;
}
}
askForOutputFilename();
@ -324,16 +335,19 @@ public class EncryptFileActivity extends BaseActivity {
InputStream in = new FileInputStream(mInputFilename);
ByteArrayOutputStream out = new ByteArrayOutputStream();
String passPhrase = mPassPhrase.getText().toString();
if (passPhrase.length() == 0) {
passPhrase = null;
String passPhrase = null;
if (mEncryptionMode.getCheckedRadioButtonId() == R.id.use_symmetric) {
passPhrase = mPassPhrase.getText().toString();
if (passPhrase.length() == 0) {
passPhrase = null;
}
}
Apg.encrypt(in, out, mAsciiArmour.isChecked(),
mEncryptionKeyIds, getSecretKeyId(),
Apg.getPassPhrase(), this,
((Choice) mAlgorithm.getSelectedItem()).getId(),
getDefaultHashAlgorithm(),
passPhrase);
mEncryptionKeyIds, getSecretKeyId(),
Apg.getPassPhrase(), this,
((Choice) mAlgorithm.getSelectedItem()).getId(),
getDefaultHashAlgorithm(),
passPhrase);
out.close();
OutputStream fileOut = new FileOutputStream(mOutputFilename);

View File

@ -95,4 +95,11 @@ public final class Id {
public static final int sign_and_encrypt = 0x21070003;
}
}
public static final class return_value {
public static final int ok = 0;
public static final int error = -1;
public static final int no_master_key = -2;
public static final int updated = 1;
}
}