fix #85, use key_ids correctly

as bonus, guava and retrolambda! Nicer code
This commit is contained in:
Mohamed Zenadi 2015-05-17 18:08:59 +02:00
parent a23b268d9b
commit e7b766bfd4
7 changed files with 109 additions and 101 deletions

Binary file not shown.

View file

@ -14,8 +14,8 @@ android {
} }
compileOptions { compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7 sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_8
} }
// avoid Travis failures // avoid Travis failures
@ -33,9 +33,13 @@ dependencies {
compile 'org.apache.commons:commons-io:1.3.2' compile 'org.apache.commons:commons-io:1.3.2'
compile 'com.jayway.android.robotium:robotium-solo:5.3.1' compile 'com.jayway.android.robotium:robotium-solo:5.3.1'
compile 'com.melnykov:floatingactionbutton:1.2.0' compile 'com.melnykov:floatingactionbutton:1.2.0'
compile group: 'com.google.guava', name: 'guava', version: '18.0'
} }
tasks.findAll { // make all tasks whose name starts with 'assemble'... tasks.findAll { // make all tasks whose name starts with 'assemble'...
it.name.startsWith 'assemble' it.name.startsWith 'assemble'
}.each { // ... depending on 'copyDependenciesIntoLibs' task from 'copyLibs.gradle' script plugin }.each { // ... depending on 'copyDependenciesIntoLibs' task from 'copyLibs.gradle' script plugin
it.dependsOn copyDependenciesIntoLibs it.dependsOn copyDependenciesIntoLibs
} }
apply plugin: 'com.android.application' //or apply plugin: 'java'
apply plugin: 'me.tatarka.retrolambda'

View file

@ -15,3 +15,4 @@
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *; # public *;
#} #}
-dontwarn java.lang.invoke.*

View file

@ -1,8 +1,8 @@
package com.zeapo.pwdstore; package com.zeapo.pwdstore;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
@ -12,6 +12,9 @@ import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Toast; import android.widget.Toast;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.zeapo.pwdstore.crypto.PgpHandler; import com.zeapo.pwdstore.crypto.PgpHandler;
import com.zeapo.pwdstore.git.GitActivity; import com.zeapo.pwdstore.git.GitActivity;
import com.zeapo.pwdstore.utils.PasswordRepository; import com.zeapo.pwdstore.utils.PasswordRepository;
@ -19,12 +22,14 @@ import com.zeapo.pwdstore.utils.PasswordRepository;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.openintents.openpgp.util.OpenPgpKeyPreference; import org.openintents.openpgp.util.OpenPgpKeyPreference;
import org.openintents.openpgp.util.OpenPgpUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Arrays;
public class UserPreference extends AppCompatActivity implements Preference.OnPreferenceClickListener { public class UserPreference extends AppCompatActivity {
private final static int IMPORT_SSH_KEY = 1; private final static int IMPORT_SSH_KEY = 1;
private final static int IMPORT_PGP_KEY = 2; private final static int IMPORT_PGP_KEY = 2;
private final static int EDIT_GIT_INFO = 3; private final static int EDIT_GIT_INFO = 3;
@ -34,26 +39,75 @@ public class UserPreference extends AppCompatActivity implements Preference.OnPr
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Load the preferences from an XML resource final UserPreference callingActivity = (UserPreference) getActivity();
final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
addPreferencesFromResource(R.xml.preference); addPreferencesFromResource(R.xml.preference);
// Preference keyPref = findPreference("openpgp_key_id");
// keyPref.setSummary(getPreferenceManager().getSharedPreferences().getString("openpgp_key_ids", "No key selected"));
// keyPref.setOnPreferenceClickListener((UserPreference) getActivity());
findPreference("ssh_key").setOnPreferenceClickListener((UserPreference) getActivity());
findPreference("git_server_info").setOnPreferenceClickListener((UserPreference) getActivity());
findPreference("git_delete_repo").setOnPreferenceClickListener((UserPreference) getActivity());
// ((UserPreference) getActivity()).mKey = (OpenPgpKeyPreference) findPreference("openpgp_key");
// if (getPreferenceManager().getSharedPreferences().getString("openpgp_provider_list", null) != null) Preference keyPref = findPreference("openpgp_key_id_pref");
// ((UserPreference) getActivity()).mKey.setOpenPgpProvider(getPreferenceManager().getSharedPreferences().getString("openpgp_provider_list", "")); String selectedKeys = sharedPreferences.getString("openpgp_key_ids", "");
if (Strings.isNullOrEmpty(selectedKeys)) {
keyPref.setSummary("No key selected");
} else {
keyPref.setSummary(
Joiner.on(',')
.join(Iterables.transform(Arrays.asList(selectedKeys.split(",")), input -> OpenPgpUtils.convertKeyIdToHex(Long.valueOf(input))))
);
}
keyPref.setOnPreferenceClickListener((Preference pref) -> {
Intent intent = new Intent(callingActivity, PgpHandler.class);
intent.putExtra("Operation", "GET_KEY_ID");
startActivityForResult(intent, IMPORT_PGP_KEY);
return true;
});
// findPreference("openpgp_provider_list").setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { findPreference("ssh_key").setOnPreferenceClickListener((Preference pref) -> {
// @Override callingActivity.getSshKey();
// public boolean onPreferenceChange(Preference preference, Object o) { return true;
// ((UserPreference) getActivity()).mKey.setOpenPgpProvider((String) o); });
// return false;
// } findPreference("git_server_info").setOnPreferenceClickListener((Preference pref) -> {
// }); Intent intent = new Intent(callingActivity, GitActivity.class);
intent.putExtra("Operation", GitActivity.EDIT_SERVER);
startActivityForResult(intent, EDIT_GIT_INFO);
return true;
});
findPreference("git_delete_repo").setOnPreferenceClickListener((Preference pref) -> {
new AlertDialog.Builder(callingActivity).
setTitle(R.string.pref_dialog_delete_title).
setMessage(R.string.pref_dialog_delete_msg).
setCancelable(false).
setPositiveButton(R.string.dialog_delete,
(dialog, id) -> {
try {
FileUtils.deleteDirectory(PasswordRepository.getWorkTree());
} catch (Exception e) {
//TODO Handle the diffent cases of exceptions
}
sharedPreferences.edit().putBoolean("repository_initialized", false).commit();
dialog.cancel();
callingActivity.finish();
}
).
setNegativeButton(R.string.dialog_do_not_delete,
(dialog, id) -> {
dialog.cancel();
}
).
show();
return true;
});
callingActivity.mKey = (OpenPgpKeyPreference) findPreference("openpgp_key");
if (sharedPreferences.getString("openpgp_provider_list", null) != null)
((UserPreference) getActivity()).mKey.setOpenPgpProvider(sharedPreferences.getString("openpgp_provider_list", ""));
findPreference("openpgp_provider_list").setOnPreferenceChangeListener((preference, o) -> {
callingActivity.mKey.setOpenPgpProvider((String) o);
return false;
});
} }
} }
@ -104,63 +158,6 @@ public class UserPreference extends AppCompatActivity implements Preference.OnPr
sshKey.close(); sshKey.close();
} }
@Override
public boolean onPreferenceClick(Preference pref) {
switch (pref.getKey())
{
case "openpgp_key_id":
{
Intent intent = new Intent(this, PgpHandler.class);
intent.putExtra("Operation", "GET_KEY_ID");
startActivityForResult(intent, IMPORT_PGP_KEY);
}
break;
case "ssh_key":
{
getSshKey();
}
break;
case "git_server_info":
{
Intent intent = new Intent(this, GitActivity.class);
intent.putExtra("Operation", GitActivity.EDIT_SERVER);
startActivityForResult(intent, EDIT_GIT_INFO);
}
break;
case "git_delete_repo":
{
new AlertDialog.Builder(this).
setTitle(R.string.pref_dialog_delete_title).
setMessage(R.string.pref_dialog_delete_msg).
setCancelable(false).
setPositiveButton(R.string.dialog_delete,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try {
FileUtils.deleteDirectory(PasswordRepository.getWorkTree());
} catch (Exception e) {
//TODO Handle the diffent cases of exceptions
}
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("repository_initialized", false).commit();
dialog.cancel();
finish();
}
}
).
setNegativeButton(R.string.dialog_do_not_delete,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}
).
show();
}
}
return true;
}
protected void onActivityResult(int requestCode, int resultCode, protected void onActivityResult(int requestCode, int resultCode,
Intent data) { Intent data) {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
@ -181,11 +178,8 @@ public class UserPreference extends AppCompatActivity implements Preference.OnPr
new AlertDialog.Builder(this). new AlertDialog.Builder(this).
setTitle(this.getResources().getString(R.string.ssh_key_error_dialog_title)). setTitle(this.getResources().getString(R.string.ssh_key_error_dialog_title)).
setMessage(this.getResources().getString(R.string.ssh_key_error_dialog_text) + e.getMessage()). setMessage(this.getResources().getString(R.string.ssh_key_error_dialog_text) + e.getMessage()).
setPositiveButton(this.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { setPositiveButton(this.getResources().getString(R.string.dialog_ok), (dialogInterface, i) -> {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//pass //pass
}
}).show(); }).show();
} }
} }
@ -199,6 +193,7 @@ public class UserPreference extends AppCompatActivity implements Preference.OnPr
{ {
if (mKey.handleOnActivityResult(requestCode, resultCode, data)) { if (mKey.handleOnActivityResult(requestCode, resultCode, data)) {
// handled by OpenPgpKeyPreference // handled by OpenPgpKeyPreference
PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).edit().putLong("openpgp_sign_key", mKey.getValue()).apply();
return; return;
} }
} }

View file

@ -25,17 +25,16 @@ import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.google.common.primitives.Longs;
import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.R;
import com.zeapo.pwdstore.UserPreference; import com.zeapo.pwdstore.UserPreference;
import com.zeapo.pwdstore.utils.PasswordRepository; import com.zeapo.pwdstore.utils.PasswordRepository;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.util.StringUtils;
import org.openintents.openpgp.IOpenPgpService; import org.openintents.openpgp.IOpenPgpService;
import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection; import org.openintents.openpgp.util.OpenPgpServiceConnection;
import org.openintents.openpgp.util.OpenPgpUtils;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -44,12 +43,14 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConnection.OnBound{ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConnection.OnBound{
private OpenPgpServiceConnection mServiceConnection; private OpenPgpServiceConnection mServiceConnection;
private String keyIDs = ""; private Set<String> keyIDs = new HashSet<>();
SharedPreferences settings; SharedPreferences settings;
private Activity activity; private Activity activity;
ClipboardManager clipboard; ClipboardManager clipboard;
@ -82,7 +83,7 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne
// some persistance // some persistance
settings = PreferenceManager.getDefaultSharedPreferences(this); settings = PreferenceManager.getDefaultSharedPreferences(this);
String providerPackageName = settings.getString("openpgp_provider_list", ""); String providerPackageName = settings.getString("openpgp_provider_list", "");
keyIDs = settings.getString("openpgp_key_ids", ""); keyIDs = settings.getStringSet("openpgp_key_ids_set", new HashSet<>());
registered = false; registered = false;
@ -355,6 +356,7 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne
+ ".gpg"; + ".gpg";
OutputStream outputStream = FileUtils.openOutputStream(new File(path)); OutputStream outputStream = FileUtils.openOutputStream(new File(path));
outputStream.write(os.toByteArray()); outputStream.write(os.toByteArray());
outputStream.close();
Intent data = new Intent(); Intent data = new Intent();
data.putExtra("CREATED_FILE", path); data.putExtra("CREATED_FILE", path);
data.putExtra("NAME", ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString()); data.putExtra("NAME", ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString());
@ -368,18 +370,10 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne
// get key ids // get key ids
if (result.hasExtra(OpenPgpApi.RESULT_KEY_IDS)) { if (result.hasExtra(OpenPgpApi.RESULT_KEY_IDS)) {
long[] ids = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS); long[] ids = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS);
ArrayList<String> keys = new ArrayList<String>(); Set<String> keys = new HashSet<String>();
for (int i = 0; i < ids.length; i++) { for (long id : ids) keys.add(String.valueOf(id)); // use Long
keys.add(OpenPgpUtils.convertKeyIdToHex(ids[i])); settings.edit().putStringSet("openpgp_key_ids_set", keys).apply();
}
keyIDs = StringUtils.join(keys, ", ");
if (!keyIDs.isEmpty()) {
String mKeys = keyIDs.split(",").length > 1 ? keyIDs : keyIDs.split(",")[0];
// ((TextView) findViewById(R.id.crypto_key_ids)).setText(mKeys);
settings.edit().putString("openpgp_key_ids", keyIDs).apply();
}
showToast("PGP key selected"); showToast("PGP key selected");
setResult(RESULT_OK); setResult(RESULT_OK);
@ -438,9 +432,17 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne
} }
} }
/**
* Encrypts a password file
* @param data
*/
public void encrypt(Intent data) { public void encrypt(Intent data) {
data.setAction(OpenPgpApi.ACTION_ENCRYPT); data.setAction(OpenPgpApi.ACTION_ENCRYPT);
ArrayList<Long> longKeys = new ArrayList<>();
for (String keyId : keyIDs) longKeys.add(Long.valueOf(keyId));
data.putExtra(OpenPgpApi.EXTRA_KEY_IDS, Longs.toArray(longKeys));
data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
String name = ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString(); String name = ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString();

View file

@ -9,6 +9,10 @@
<PreferenceCategory android:title="@string/pref_crypto_title"> <PreferenceCategory android:title="@string/pref_crypto_title">
<org.openintents.openpgp.util.OpenPgpAppPreference <org.openintents.openpgp.util.OpenPgpAppPreference
android:key="openpgp_provider_list" android:title="@string/pref_provider_title" /> android:key="openpgp_provider_list" android:title="@string/pref_provider_title" />
<org.openintents.openpgp.util.OpenPgpKeyPreference
android:key="openpgp_key" android:title="Select key" />
<Preference android:title="@string/pref_key_title"
android:key="openpgp_key_id_pref" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_general_title"> <PreferenceCategory android:title="@string/pref_general_title">

View file

@ -3,9 +3,11 @@
buildscript { buildscript {
repositories { repositories {
jcenter() jcenter()
mavenCentral()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:1.2.2' classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'me.tatarka:gradle-retrolambda:3.1.0'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files