more refactoring in preparation for a first version

This commit is contained in:
Zeapo 2014-08-07 20:53:29 +01:00
parent 4cd5a11185
commit 9db42440fb
5 changed files with 108 additions and 61 deletions

View file

@ -230,6 +230,11 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI
Intent data) { Intent data) {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
refreshListAdapter(); refreshListAdapter();
// do not froget to commit the file
if (requestCode == PgpHandler.REQUEST_CODE_ENCRYPT) {
}
} }
} }
} }

View file

@ -1,15 +1,24 @@
package com.zeapo.pwdstore; package com.zeapo.pwdstore;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
public class UserPreference extends PreferenceActivity { import com.zeapo.pwdstore.crypto.PgpHandler;
import com.zeapo.pwdstore.utils.PasswordRepository;
import org.apache.commons.io.FileUtils;
public class UserPreference extends PreferenceActivity implements Preference.OnPreferenceClickListener {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference); addPreferencesFromResource(R.xml.preference);
findPreference("openpgp_key_id").setOnPreferenceClickListener(this);
getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true);
} }
@ -29,4 +38,21 @@ public class UserPreference extends PreferenceActivity {
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override
public boolean onPreferenceClick(Preference pref) {
System.out.println(pref);
if (pref.getKey().equals("openpgp_key_id")) {
Intent intent = new Intent(this, PgpHandler.class);
intent.putExtra("Operation", "GET_KEY_ID");
startActivityForResult(intent, 0);
}
return true;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (resultCode == RESULT_OK) {
}
}
} }

View file

@ -4,6 +4,8 @@ import android.app.ActionBar;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentSender; import android.content.IntentSender;
@ -66,17 +68,6 @@ public class PgpHandler extends Activity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Bundle extra = getIntent().getExtras();
if (extra.getString("Operation").equals("DECRYPT")) {
setContentView(R.layout.decrypt_layout);
((TextView) findViewById(R.id.crypto_password_file)).setText(extra.getString("NAME"));
} else if (extra.getString("Operation").equals("ENCRYPT")) {
setContentView(R.layout.encrypt_layout);
String cat = extra.getString("FILE_PATH");
cat = cat.replace(PasswordRepository.getWorkTree().getAbsolutePath(), "");
cat = cat + "/";
((TextView) findViewById(R.id.crypto_password_category)).setText(cat);
}
// some persistance // some persistance
settings = PreferenceManager.getDefaultSharedPreferences(this); settings = PreferenceManager.getDefaultSharedPreferences(this);
@ -89,17 +80,33 @@ public class PgpHandler extends Activity {
startActivity(intent); startActivity(intent);
} }
// bind to service // bind to service
mServiceConnection = new OpenPgpServiceConnection( mServiceConnection = new OpenPgpServiceConnection(
PgpHandler.this, providerPackageName); PgpHandler.this, providerPackageName);
mServiceConnection.bindToService(); mServiceConnection.bindToService();
Bundle extra = getIntent().getExtras();
if (extra.getString("Operation").equals("DECRYPT")) {
setContentView(R.layout.decrypt_layout);
((TextView) findViewById(R.id.crypto_password_file)).setText(extra.getString("NAME"));
} else if (extra.getString("Operation").equals("ENCRYPT")) {
setContentView(R.layout.encrypt_layout);
String cat = extra.getString("FILE_PATH");
cat = cat.replace(PasswordRepository.getWorkTree().getAbsolutePath(), "");
cat = cat + "/";
((TextView) findViewById(R.id.crypto_password_category)).setText(cat);
} else if (extra.getString("Operation").equals("GET_KEY_ID")) {
// wait until the service is bound
while (!mServiceConnection.isBound());
getKeyIds(new Intent());
}
ActionBar actionBar = getActionBar(); ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true);
Log.i("", accountName);
} }
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
@ -214,12 +221,39 @@ public class PgpHandler extends Activity {
} }
private class MyCallback implements OpenPgpApi.IOpenPgpCallback {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(Constants.TAG, "onActivityResult resultCode: " + resultCode);
// try again after user interaction
if (resultCode == RESULT_OK) {
/*
* The data originally given to one of the methods above, is again
* returned here to be used when calling the method again after user
* interaction. The Intent now also contains results from the user
* interaction, for example selected key ids.
*/
switch (requestCode) {
case REQUEST_CODE_ENCRYPT: {
encrypt(data);
break;
}
case REQUEST_CODE_DECRYPT_AND_VERIFY: {
decryptAndVerify(data);
break;
}
}
}
}
public class PgpCallback implements OpenPgpApi.IOpenPgpCallback {
boolean returnToCiphertextField; boolean returnToCiphertextField;
ByteArrayOutputStream os; ByteArrayOutputStream os;
int requestCode; int requestCode;
private MyCallback(boolean returnToCiphertextField, ByteArrayOutputStream os, int requestCode) { private PgpCallback(boolean returnToCiphertextField, ByteArrayOutputStream os, int requestCode) {
this.returnToCiphertextField = returnToCiphertextField; this.returnToCiphertextField = returnToCiphertextField;
this.os = os; this.os = os;
this.requestCode = requestCode; this.requestCode = requestCode;
@ -280,14 +314,6 @@ public class PgpHandler extends Activity {
} }
} }
// verify
if (result.hasExtra(OpenPgpApi.RESULT_SIGNATURE)) {
OpenPgpSignatureResult sigResult
= result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
showToast(sigResult.toString());
}
// 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);
@ -295,6 +321,7 @@ public class PgpHandler extends Activity {
for (int i = 0; i < ids.length; i++) { for (int i = 0; i < ids.length; i++) {
keyIDs += OpenPgpUtils.convertKeyIdToHex(ids[i]) + ", "; keyIDs += OpenPgpUtils.convertKeyIdToHex(ids[i]) + ", ";
} }
settings.edit().putString("openpgp_key_ids", keyIDs);
} }
break; break;
} }
@ -322,6 +349,17 @@ public class PgpHandler extends Activity {
} }
public void getKeyIds(Intent data) {
accountName = settings.getString("openpgp_account_name", "");
data.setAction(OpenPgpApi.ACTION_GET_KEY_IDS);
data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{accountName});
OpenPgpApi api = new OpenPgpApi(this, mServiceConnection.getService());
api.executeApiAsync(data, null, null, new PgpCallback(false, null, PgpHandler.REQUEST_CODE_GET_KEY_IDS));
}
public void decryptAndVerify(Intent data) { public void decryptAndVerify(Intent data) {
data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
@ -332,7 +370,7 @@ public class PgpHandler extends Activity {
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
OpenPgpApi api = new OpenPgpApi(this, mServiceConnection.getService()); OpenPgpApi api = new OpenPgpApi(this, mServiceConnection.getService());
api.executeApiAsync(data, is, os, new MyCallback(true, os, REQUEST_CODE_DECRYPT_AND_VERIFY)); api.executeApiAsync(data, is, os, new PgpCallback(true, os, REQUEST_CODE_DECRYPT_AND_VERIFY));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -358,16 +396,17 @@ public class PgpHandler extends Activity {
} }
} }
}).setNegativeButton("No thanks", new DialogInterface.OnClickListener() { }).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
// Do nothing... // Do nothing...
} }
}).show(); }).show();
} else { } else {
data.setAction(OpenPgpApi.ACTION_ENCRYPT); data.setAction(OpenPgpApi.ACTION_ENCRYPT);
data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{accountName}); data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{accountName});
data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
Log.i("BABABOU", settings.getString("openpgpg_key_ids", "") + "");
String name = ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString(); String name = ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString();
String pass = ((EditText) findViewById(R.id.crypto_password_edit)).getText().toString(); String pass = ((EditText) findViewById(R.id.crypto_password_edit)).getText().toString();
@ -391,7 +430,7 @@ public class PgpHandler extends Activity {
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
OpenPgpApi api = new OpenPgpApi(this, mServiceConnection.getService()); OpenPgpApi api = new OpenPgpApi(this, mServiceConnection.getService());
api.executeApiAsync(data, is, os, new MyCallback(true, os, REQUEST_CODE_ENCRYPT)); api.executeApiAsync(data, is, os, new PgpCallback(true, os, REQUEST_CODE_ENCRYPT));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -420,31 +459,4 @@ public class PgpHandler extends Activity {
}) })
.show(); .show();
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(Constants.TAG, "onActivityResult resultCode: " + resultCode);
// try again after user interaction
if (resultCode == RESULT_OK) {
/*
* The data originally given to one of the methods above, is again
* returned here to be used when calling the method again after user
* interaction. The Intent now also contains results from the user
* interaction, for example selected key ids.
*/
switch (requestCode) {
case REQUEST_CODE_ENCRYPT: {
encrypt(data);
break;
}
case REQUEST_CODE_DECRYPT_AND_VERIFY: {
decryptAndVerify(data);
break;
}
}
}
}
} }

View file

@ -4,8 +4,8 @@
<item android:id="@+id/menu_add_password" <item android:id="@+id/menu_add_password"
android:title="New password"/> android:title="New password"/>
<item android:id="@+id/menu_add_category" <!--<item android:id="@+id/menu_add_category"-->
android:title="New category"/> <!--android:title="New category"/>-->
<item android:id="@+id/referesh" <item android:id="@+id/referesh"
android:title="Refresh" android:title="Refresh"

View file

@ -6,9 +6,13 @@
android:key="openpgp_provider_list" android:key="openpgp_provider_list"
android:title="Select OpenPGP Provider!" /> android:title="Select OpenPGP Provider!" />
<EditTextPreference android:title="OpenPGP account" <EditTextPreference android:title="Set your OpenPGP account"
android:hint="mail@somewhere.tld" android:hint="mail@somewhere.tld"
android:key="openpgp_account_name"/> android:key="openpgp_account_name"/>
<Preference
android:title="Select OpenPGP Key id"
android:key="openpgp_key_id"/>
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>