fixing public and secret key activities

This commit is contained in:
Dominik 2012-10-31 00:39:32 +01:00
parent 691d888e92
commit 825ec676d0
17 changed files with 469 additions and 1525 deletions

View File

@ -228,7 +228,8 @@
<string name="keySignSuccess">Successfully signed key</string> <string name="keySignSuccess">Successfully signed key</string>
<string name="qrScanImportSuccess">Successfully validated and imported key</string> <string name="qrScanImportSuccess">Successfully validated and imported key</string>
<string name="listInformation">Long press one entry in this list to show more options!</string> <string name="listInformation">Long press one entry in this list to show more options!</string>
<string name="listEmpty">This list is empty!</string>
<!-- <!--
error_lowerCase: phrases, no punctuation, all lowercase, error_lowerCase: phrases, no punctuation, all lowercase,
they will be put after "errorMessage", e.g. "Error: file not found" they will be put after "errorMessage", e.g. "Error: file not found"

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import org.spongycastle.openpgp.PGPKeyRing;
import org.spongycastle.openpgp.PGPObjectFactory; import org.spongycastle.openpgp.PGPObjectFactory;
import org.spongycastle.openpgp.PGPPublicKeyRing; import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey; import org.spongycastle.openpgp.PGPSecretKey;
@ -57,19 +58,19 @@ public class PGPConversionHelper {
* @param keysBytes * @param keysBytes
* @return * @return
*/ */
public static PGPSecretKeyRing BytesToPGPSecretKeyRing(byte[] keysBytes) { // public static PGPSecretKeyRing BytesToPGPSecretKeyRing(byte[] keysBytes) {
PGPObjectFactory factory = new PGPObjectFactory(keysBytes); // PGPObjectFactory factory = new PGPObjectFactory(keysBytes);
PGPSecretKeyRing keyRing = null; // PGPSecretKeyRing keyRing = null;
try { // try {
if ((keyRing = (PGPSecretKeyRing) factory.nextObject()) == null) { // if ((keyRing = (PGPSecretKeyRing) factory.nextObject()) == null) {
Log.e(Constants.TAG, "No keys given!"); // Log.e(Constants.TAG, "No keys given!");
} // }
} catch (IOException e) { // } catch (IOException e) {
Log.e(Constants.TAG, "Error while converting to PGPSecretKeyRing!", e); // Log.e(Constants.TAG, "Error while converting to PGPSecretKeyRing!", e);
} // }
//
return keyRing; // return keyRing;
} // }
/** /**
* Convert from byte[] to PGPPublicKeyRing * Convert from byte[] to PGPPublicKeyRing
@ -77,11 +78,11 @@ public class PGPConversionHelper {
* @param keysBytes * @param keysBytes
* @return * @return
*/ */
public static PGPPublicKeyRing BytesToPGPPublicKeyRing(byte[] keysBytes) { public static PGPKeyRing BytesToPGPKeyRing(byte[] keysBytes) {
PGPObjectFactory factory = new PGPObjectFactory(keysBytes); PGPObjectFactory factory = new PGPObjectFactory(keysBytes);
PGPPublicKeyRing keyRing = null; PGPKeyRing keyRing = null;
try { try {
if ((keyRing = (PGPPublicKeyRing) factory.nextObject()) == null) { if ((keyRing = (PGPKeyRing) factory.nextObject()) == null) {
Log.e(Constants.TAG, "No keys given!"); Log.e(Constants.TAG, "No keys given!");
} }
} catch (IOException e) { } catch (IOException e) {
@ -98,7 +99,7 @@ public class PGPConversionHelper {
* @return * @return
*/ */
public static ArrayList<PGPSecretKey> BytesToPGPSecretKeyList(byte[] keysBytes) { public static ArrayList<PGPSecretKey> BytesToPGPSecretKeyList(byte[] keysBytes) {
PGPSecretKeyRing keyRing = BytesToPGPSecretKeyRing(keysBytes); PGPSecretKeyRing keyRing = (PGPSecretKeyRing) BytesToPGPKeyRing(keysBytes);
ArrayList<PGPSecretKey> keys = new ArrayList<PGPSecretKey>(); ArrayList<PGPSecretKey> keys = new ArrayList<PGPSecretKey>();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -245,7 +245,7 @@ public class ApgProvider extends ContentProvider {
} }
/** /**
* Returns weather the key is a public or secret one * Returns type of the query (secret/public)
* *
* @param uri * @param uri
* @return * @return
@ -254,6 +254,7 @@ public class ApgProvider extends ContentProvider {
int type; int type;
switch (match) { switch (match) {
case PUBLIC_KEY_RING: case PUBLIC_KEY_RING:
case PUBLIC_KEY_RING_BY_ROW_ID:
case PUBLIC_KEY_RING_BY_MASTER_KEY_ID: case PUBLIC_KEY_RING_BY_MASTER_KEY_ID:
case PUBLIC_KEY_RING_BY_KEY_ID: case PUBLIC_KEY_RING_BY_KEY_ID:
case PUBLIC_KEY_RING_BY_EMAILS: case PUBLIC_KEY_RING_BY_EMAILS:
@ -265,6 +266,7 @@ public class ApgProvider extends ContentProvider {
break; break;
case SECRET_KEY_RING: case SECRET_KEY_RING:
case SECRET_KEY_RING_BY_ROW_ID:
case SECRET_KEY_RING_BY_MASTER_KEY_ID: case SECRET_KEY_RING_BY_MASTER_KEY_ID:
case SECRET_KEY_RING_BY_KEY_ID: case SECRET_KEY_RING_BY_KEY_ID:
case SECRET_KEY_RING_BY_EMAILS: case SECRET_KEY_RING_BY_EMAILS:

View File

@ -47,7 +47,7 @@ public class ProviderHelper {
byte[] data = cursor.getBlob(keyRingDataCol); byte[] data = cursor.getBlob(keyRingDataCol);
if (data != null) { if (data != null) {
keyRing = PGPConversionHelper.BytesToPGPPublicKeyRing(data); keyRing = PGPConversionHelper.BytesToPGPKeyRing(data);
} }
} }
@ -395,4 +395,34 @@ public class ProviderHelper {
cr.delete(KeyRings.buildSecretKeyRingsUri(Long.toString(rowId)), null, null); cr.delete(KeyRings.buildSecretKeyRingsUri(Long.toString(rowId)), null, null);
} }
public static long getPublicMasterKeyId(Context context, long keyRingRowId) {
Uri queryUri = KeyRings.buildPublicKeyRingsUri(String.valueOf(keyRingRowId));
return getMasterKeyId(context, queryUri, keyRingRowId);
}
public static long getSecretMasterKeyId(Context context, long keyRingRowId) {
Uri queryUri = KeyRings.buildSecretKeyRingsUri(String.valueOf(keyRingRowId));
return getMasterKeyId(context, queryUri, keyRingRowId);
}
private static long getMasterKeyId(Context context, Uri queryUri, long keyRingRowId) {
String[] projection = new String[] { KeyRings.MASTER_KEY_ID };
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(queryUri, projection, null, null, null);
long masterKeyId = -1;
if (cursor != null && cursor.moveToFirst()) {
int masterKeyIdCol = cursor.getColumnIndex(KeyRings.MASTER_KEY_ID);
masterKeyId = cursor.getLong(masterKeyIdCol);
}
if (cursor != null) {
cursor.close();
}
return masterKeyId;
}
} }

View File

@ -244,11 +244,11 @@ public class EditKeyActivity extends SherlockFragmentActivity {
if (message.arg1 == ApgServiceHandler.MESSAGE_OKAY) { if (message.arg1 == ApgServiceHandler.MESSAGE_OKAY) {
// get new key from data bundle returned from service // get new key from data bundle returned from service
Bundle data = message.getData(); Bundle data = message.getData();
PGPSecretKeyRing masterKeyRing = PGPConversionHelper PGPSecretKeyRing masterKeyRing = (PGPSecretKeyRing) PGPConversionHelper
.BytesToPGPSecretKeyRing(data .BytesToPGPKeyRing(data
.getByteArray(ApgService.RESULT_NEW_KEY)); .getByteArray(ApgService.RESULT_NEW_KEY));
PGPSecretKeyRing subKeyRing = PGPConversionHelper PGPSecretKeyRing subKeyRing = (PGPSecretKeyRing) PGPConversionHelper
.BytesToPGPSecretKeyRing(data .BytesToPGPKeyRing(data
.getByteArray(ApgService.RESULT_NEW_KEY2)); .getByteArray(ApgService.RESULT_NEW_KEY2));
// add master key // add master key

View File

@ -1,3 +1,20 @@
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thialfihar.android.apg.ui; package org.thialfihar.android.apg.ui;
import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Constants;
@ -5,7 +22,6 @@ import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.R; import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.service.ApgService; import org.thialfihar.android.apg.service.ApgService;
import org.thialfihar.android.apg.service.ApgServiceHandler; import org.thialfihar.android.apg.service.ApgServiceHandler;
import org.thialfihar.android.apg.ui.KeyListActivityOld.KeyListAdapter;
import org.thialfihar.android.apg.ui.dialog.DeleteFileDialogFragment; import org.thialfihar.android.apg.ui.dialog.DeleteFileDialogFragment;
import org.thialfihar.android.apg.ui.dialog.DeleteKeyDialogFragment; import org.thialfihar.android.apg.ui.dialog.DeleteKeyDialogFragment;
import org.thialfihar.android.apg.ui.dialog.FileDialogFragment; import org.thialfihar.android.apg.ui.dialog.FileDialogFragment;
@ -20,15 +36,10 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.os.Messenger; import android.os.Messenger;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
public class KeyListActivity extends SherlockFragmentActivity { public class KeyListActivity extends SherlockFragmentActivity {
@ -37,14 +48,9 @@ public class KeyListActivity extends SherlockFragmentActivity {
public static final String EXTRA_TEXT = "text"; public static final String EXTRA_TEXT = "text";
protected ExpandableListView mList; // protected View mFilterLayout;
protected KeyListAdapter mListAdapter; // protected Button mClearFilterButton;
protected View mFilterLayout; // protected TextView mFilterInfo;
protected Button mClearFilterButton;
protected TextView mFilterInfo;
protected int mSelectedItem = -1;
// protected int mTask = 0;
protected String mImportFilename = Constants.path.APP_DIR + "/"; protected String mImportFilename = Constants.path.APP_DIR + "/";
protected String mExportFilename = Constants.path.APP_DIR + "/"; protected String mExportFilename = Constants.path.APP_DIR + "/";
@ -52,7 +58,7 @@ public class KeyListActivity extends SherlockFragmentActivity {
protected String mImportData; protected String mImportData;
protected boolean mDeleteAfterImport = false; protected boolean mDeleteAfterImport = false;
protected int mKeyType = Id.type.public_key; protected int mKeyType;
FileDialogFragment mFileDialog; FileDialogFragment mFileDialog;
@ -123,6 +129,44 @@ public class KeyListActivity extends SherlockFragmentActivity {
importKeys(); importKeys();
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case Id.request.filename: {
if (resultCode == RESULT_OK && data != null) {
try {
String path = data.getData().getPath();
Log.d(Constants.TAG, "path=" + path);
// set filename used in export/import dialogs
mFileDialog.setFilename(path);
} catch (NullPointerException e) {
Log.e(Constants.TAG, "Nullpointer while retrieving path!", e);
}
}
return;
}
default: {
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(3, Id.menu.option.search, 0, R.string.menu_search)
.setIcon(R.drawable.ic_menu_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0, Id.menu.option.import_keys, 2, R.string.menu_importKeys).setShowAsAction(
MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(0, Id.menu.option.export_keys, 3, R.string.menu_exportKeys).setShowAsAction(
MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
@ -140,13 +184,13 @@ public class KeyListActivity extends SherlockFragmentActivity {
} }
case Id.menu.option.export_keys: { case Id.menu.option.export_keys: {
showExportKeysDialog(false); showExportKeysDialog(-1);
return true; return true;
} }
// case Id.menu.option.search: // case Id.menu.option.search:
// startSearch("", false, null, false); // startSearch("", false, null, false);
// return true; // return true;
default: { default: {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
@ -154,7 +198,10 @@ public class KeyListActivity extends SherlockFragmentActivity {
} }
} }
private void showImportKeysDialog() { /**
* Show to dialog from where to import keys
*/
public void showImportKeysDialog() {
// Message is received after file is selected // Message is received after file is selected
Handler returnHandler = new Handler() { Handler returnHandler = new Handler() {
@Override @Override
@ -179,11 +226,27 @@ public class KeyListActivity extends SherlockFragmentActivity {
mFileDialog.show(getSupportFragmentManager(), "fileDialog"); mFileDialog.show(getSupportFragmentManager(), "fileDialog");
} }
private void showExportKeysDialog(boolean singleKeyExport) { /**
String title = (singleKeyExport ? getString(R.string.title_exportKey) * Show dialog where to export keys
: getString(R.string.title_exportKeys)); *
String message = getString(mKeyType == Id.type.public_key ? R.string.specifyFileToExportTo * @param keyRingId
: R.string.specifyFileToExportSecretKeysTo); * if -1 export all keys
*/
public void showExportKeysDialog(final long keyRingId) {
String title = null;
if (keyRingId != -1) {
// single key export
title = getString(R.string.title_exportKey);
} else {
title = getString(R.string.title_exportKeys);
}
String message = null;
if (mKeyType == Id.type.public_key) {
message = getString(R.string.specifyFileToExportTo);
} else {
message = getString(R.string.specifyFileToExportSecretKeysTo);
}
// Message is received after file is selected // Message is received after file is selected
Handler returnHandler = new Handler() { Handler returnHandler = new Handler() {
@ -193,7 +256,7 @@ public class KeyListActivity extends SherlockFragmentActivity {
Bundle data = message.getData(); Bundle data = message.getData();
mExportFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME); mExportFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME);
exportKeys(); exportKeys(keyRingId);
} }
} }
}; };
@ -207,45 +270,18 @@ public class KeyListActivity extends SherlockFragmentActivity {
mFileDialog.show(getSupportFragmentManager(), "fileDialog"); mFileDialog.show(getSupportFragmentManager(), "fileDialog");
} }
@Override /**
public boolean onContextItemSelected(android.view.MenuItem menuItem) { * Show dialog to delete key
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuItem.getMenuInfo(); *
int type = ExpandableListView.getPackedPositionType(info.packedPosition); * @param keyRingId
int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition); */
public void showDeleteKeyDialog(long keyRingId) {
if (type != ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
return super.onContextItemSelected(menuItem);
}
switch (menuItem.getItemId()) {
case Id.menu.export: {
mSelectedItem = groupPosition;
showExportKeysDialog(true);
return true;
}
case Id.menu.delete: {
mSelectedItem = groupPosition;
showDeleteKeyDialog();
return true;
}
default: {
return super.onContextItemSelected(menuItem);
}
}
}
private void showDeleteKeyDialog() {
final int keyRingId = mListAdapter.getKeyRingId(mSelectedItem);
mSelectedItem = -1;
// Message is received after key is deleted // Message is received after key is deleted
Handler returnHandler = new Handler() { Handler returnHandler = new Handler() {
@Override @Override
public void handleMessage(Message message) { public void handleMessage(Message message) {
if (message.what == DeleteKeyDialogFragment.MESSAGE_OKAY) { if (message.what == DeleteKeyDialogFragment.MESSAGE_OKAY) {
// refreshList(); // refreshList();
} }
} }
}; };
@ -259,6 +295,9 @@ public class KeyListActivity extends SherlockFragmentActivity {
deleteKeyDialog.show(getSupportFragmentManager(), "deleteKeyDialog"); deleteKeyDialog.show(getSupportFragmentManager(), "deleteKeyDialog");
} }
/**
* Import keys with mImportData
*/
public void importKeys() { public void importKeys() {
Log.d(Constants.TAG, "importKeys started"); Log.d(Constants.TAG, "importKeys started");
@ -306,8 +345,7 @@ public class KeyListActivity extends SherlockFragmentActivity {
} else { } else {
toastMessage = getString(R.string.noKeysAddedOrUpdated); toastMessage = getString(R.string.noKeysAddedOrUpdated);
} }
Toast.makeText(KeyListActivity.this, toastMessage, Toast.LENGTH_SHORT) Toast.makeText(KeyListActivity.this, toastMessage, Toast.LENGTH_SHORT).show();
.show();
if (bad > 0) { if (bad > 0) {
AlertDialog.Builder alert = new AlertDialog.Builder(KeyListActivity.this); AlertDialog.Builder alert = new AlertDialog.Builder(KeyListActivity.this);
@ -330,7 +368,7 @@ public class KeyListActivity extends SherlockFragmentActivity {
.newInstance(mImportFilename); .newInstance(mImportFilename);
deleteFileDialog.show(getSupportFragmentManager(), "deleteDialog"); deleteFileDialog.show(getSupportFragmentManager(), "deleteDialog");
} }
// refreshList(); // refreshList();
} }
}; };
@ -347,7 +385,13 @@ public class KeyListActivity extends SherlockFragmentActivity {
startService(intent); startService(intent);
} }
public void exportKeys() { /**
* Export keys
*
* @param keyRingId
* if -1 export all keys
*/
public void exportKeys(long keyRingId) {
Log.d(Constants.TAG, "exportKeys started"); Log.d(Constants.TAG, "exportKeys started");
// Send all information needed to service to export key in other thread // Send all information needed to service to export key in other thread
@ -361,12 +405,10 @@ public class KeyListActivity extends SherlockFragmentActivity {
data.putString(ApgService.EXPORT_FILENAME, mExportFilename); data.putString(ApgService.EXPORT_FILENAME, mExportFilename);
data.putInt(ApgService.EXPORT_KEY_TYPE, mKeyType); data.putInt(ApgService.EXPORT_KEY_TYPE, mKeyType);
if (mSelectedItem == -1) { if (keyRingId == -1) {
data.putBoolean(ApgService.EXPORT_ALL, true); data.putBoolean(ApgService.EXPORT_ALL, true);
} else { } else {
int keyRingId = mListAdapter.getKeyRingId(mSelectedItem); data.putLong(ApgService.EXPORT_KEY_RING_ID, keyRingId);
data.putInt(ApgService.EXPORT_KEY_RING_ID, keyRingId);
mSelectedItem = -1;
} }
intent.putExtra(ApgService.EXTRA_DATA, data); intent.putExtra(ApgService.EXTRA_DATA, data);
@ -391,8 +433,7 @@ public class KeyListActivity extends SherlockFragmentActivity {
} else { } else {
toastMessage = getString(R.string.noKeysExported); toastMessage = getString(R.string.noKeysExported);
} }
Toast.makeText(KeyListActivity.this, toastMessage, Toast.LENGTH_SHORT) Toast.makeText(KeyListActivity.this, toastMessage, Toast.LENGTH_SHORT).show();
.show();
} }
}; };

View File

@ -1,767 +0,0 @@
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thialfihar.android.apg.ui;
import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.helper.OtherHelper;
import org.thialfihar.android.apg.helper.PGPHelper;
import org.thialfihar.android.apg.helper.PGPMain;
import org.thialfihar.android.apg.service.ApgServiceHandler;
import org.thialfihar.android.apg.service.ApgService;
import org.thialfihar.android.apg.ui.dialog.DeleteFileDialogFragment;
import org.thialfihar.android.apg.ui.dialog.DeleteKeyDialogFragment;
import org.thialfihar.android.apg.ui.dialog.FileDialogFragment;
import org.thialfihar.android.apg.R;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.MenuItem;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import org.thialfihar.android.apg.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Vector;
public class KeyListActivityOld extends SherlockFragmentActivity {
public static final String ACTION_IMPORT = Constants.INTENT_PREFIX + "IMPORT";
public static final String EXTRA_TEXT = "text";
protected ExpandableListView mList;
protected KeyListAdapter mListAdapter;
protected View mFilterLayout;
protected Button mClearFilterButton;
protected TextView mFilterInfo;
protected int mSelectedItem = -1;
// protected int mTask = 0;
protected String mImportFilename = Constants.path.APP_DIR + "/";
protected String mExportFilename = Constants.path.APP_DIR + "/";
protected String mImportData;
protected boolean mDeleteAfterImport = false;
protected int mKeyType = Id.type.public_key;
FileDialogFragment mFileDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.key_list);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
mList = (ExpandableListView) findViewById(R.id.list);
registerForContextMenu(mList);
mFilterLayout = findViewById(R.id.layout_filter);
mFilterInfo = (TextView) mFilterLayout.findViewById(R.id.filterInfo);
mClearFilterButton = (Button) mFilterLayout.findViewById(R.id.btn_clear);
mClearFilterButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
handleIntent(new Intent());
}
});
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
protected void handleIntent(Intent intent) {
String searchString = null;
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
searchString = intent.getStringExtra(SearchManager.QUERY);
if (searchString != null && searchString.trim().length() == 0) {
searchString = null;
}
}
if (searchString == null) {
mFilterLayout.setVisibility(View.GONE);
} else {
mFilterLayout.setVisibility(View.VISIBLE);
mFilterInfo.setText(getString(R.string.filterInfo, searchString));
}
if (mListAdapter != null) {
mListAdapter.cleanup();
}
mListAdapter = new KeyListAdapter(this, searchString);
mList.setAdapter(mListAdapter);
// Get intent, action
// Intent intent = getIntent();
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
// Android's Action when opening file associated to APG (see AndroidManifest.xml)
handleActionImport(intent);
} else if (ACTION_IMPORT.equals(action)) {
// APG's own Actions
handleActionImport(intent);
}
}
/**
* Handles import action
*
* @param intent
*/
private void handleActionImport(Intent intent) {
if ("file".equals(intent.getScheme()) && intent.getDataString() != null) {
mImportFilename = intent.getData().getPath();
} else {
mImportData = intent.getStringExtra(EXTRA_TEXT);
}
importKeys();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in Action Bar clicked; go home
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case Id.menu.option.import_keys: {
showImportKeysDialog();
return true;
}
case Id.menu.option.export_keys: {
showExportKeysDialog(false);
return true;
}
case Id.menu.option.search:
startSearch("", false, null, false);
return true;
default: {
return super.onOptionsItemSelected(item);
}
}
}
private void showImportKeysDialog() {
// Message is received after file is selected
Handler returnHandler = new Handler() {
@Override
public void handleMessage(Message message) {
if (message.what == FileDialogFragment.MESSAGE_OKAY) {
Bundle data = message.getData();
mImportFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME);
mDeleteAfterImport = data.getBoolean(FileDialogFragment.MESSAGE_DATA_CHECKED);
importKeys();
}
}
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(returnHandler);
mFileDialog = FileDialogFragment.newInstance(messenger,
getString(R.string.title_importKeys), getString(R.string.specifyFileToImportFrom),
mImportFilename, null, Id.request.filename);
mFileDialog.show(getSupportFragmentManager(), "fileDialog");
}
private void showExportKeysDialog(boolean singleKeyExport) {
String title = (singleKeyExport ? getString(R.string.title_exportKey)
: getString(R.string.title_exportKeys));
String message = getString(mKeyType == Id.type.public_key ? R.string.specifyFileToExportTo
: R.string.specifyFileToExportSecretKeysTo);
// Message is received after file is selected
Handler returnHandler = new Handler() {
@Override
public void handleMessage(Message message) {
if (message.what == FileDialogFragment.MESSAGE_OKAY) {
Bundle data = message.getData();
mExportFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME);
exportKeys();
}
}
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(returnHandler);
mFileDialog = FileDialogFragment.newInstance(messenger, title, message, mExportFilename,
null, Id.request.filename);
mFileDialog.show(getSupportFragmentManager(), "fileDialog");
}
@Override
public boolean onContextItemSelected(android.view.MenuItem menuItem) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuItem.getMenuInfo();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
if (type != ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
return super.onContextItemSelected(menuItem);
}
switch (menuItem.getItemId()) {
case Id.menu.export: {
mSelectedItem = groupPosition;
showExportKeysDialog(true);
return true;
}
case Id.menu.delete: {
mSelectedItem = groupPosition;
showDeleteKeyDialog();
return true;
}
default: {
return super.onContextItemSelected(menuItem);
}
}
}
private void showDeleteKeyDialog() {
final int keyRingId = mListAdapter.getKeyRingId(mSelectedItem);
mSelectedItem = -1;
// Message is received after key is deleted
Handler returnHandler = new Handler() {
@Override
public void handleMessage(Message message) {
if (message.what == DeleteKeyDialogFragment.MESSAGE_OKAY) {
refreshList();
}
}
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(returnHandler);
DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(messenger,
keyRingId, mKeyType);
deleteKeyDialog.show(getSupportFragmentManager(), "deleteKeyDialog");
}
public void importKeys() {
Log.d(Constants.TAG, "importKeys started");
// Send all information needed to service to import key in other thread
Intent intent = new Intent(this, ApgService.class);
intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_IMPORT_KEY);
// fill values for this action
Bundle data = new Bundle();
data.putInt(ApgService.IMPORT_KEY_TYPE, mKeyType);
if (mImportData != null) {
data.putInt(ApgService.TARGET, ApgService.TARGET_BYTES);
data.putByteArray(ApgService.IMPORT_BYTES, mImportData.getBytes());
} else {
data.putInt(ApgService.TARGET, ApgService.TARGET_FILE);
data.putString(ApgService.IMPORT_FILENAME, mImportFilename);
}
intent.putExtra(ApgService.EXTRA_DATA, data);
// Message is received after importing is done in ApgService
ApgServiceHandler saveHandler = new ApgServiceHandler(this, R.string.progress_importing,
ProgressDialog.STYLE_HORIZONTAL) {
public void handleMessage(Message message) {
// handle messages by standard ApgHandler first
super.handleMessage(message);
if (message.arg1 == ApgServiceHandler.MESSAGE_OKAY) {
// get returned data bundle
Bundle returnData = message.getData();
int added = returnData.getInt(ApgService.RESULT_IMPORT_ADDED);
int updated = returnData.getInt(ApgService.RESULT_IMPORT_UPDATED);
int bad = returnData.getInt(ApgService.RESULT_IMPORT_BAD);
String toastMessage;
if (added > 0 && updated > 0) {
toastMessage = getString(R.string.keysAddedAndUpdated, added, updated);
} else if (added > 0) {
toastMessage = getString(R.string.keysAdded, added);
} else if (updated > 0) {
toastMessage = getString(R.string.keysUpdated, updated);
} else {
toastMessage = getString(R.string.noKeysAddedOrUpdated);
}
Toast.makeText(KeyListActivityOld.this, toastMessage, Toast.LENGTH_SHORT).show();
if (bad > 0) {
AlertDialog.Builder alert = new AlertDialog.Builder(KeyListActivityOld.this);
alert.setIcon(android.R.drawable.ic_dialog_alert);
alert.setTitle(R.string.warning);
alert.setMessage(KeyListActivityOld.this.getString(
R.string.badKeysEncountered, bad));
alert.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alert.setCancelable(true);
alert.create().show();
} else if (mDeleteAfterImport) {
// everything went well, so now delete, if that was turned on
DeleteFileDialogFragment deleteFileDialog = DeleteFileDialogFragment
.newInstance(mImportFilename);
deleteFileDialog.show(getSupportFragmentManager(), "deleteDialog");
}
refreshList();
}
};
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler);
intent.putExtra(ApgService.EXTRA_MESSENGER, messenger);
// show progress dialog
saveHandler.showProgressDialog(this);
// start service with intent
startService(intent);
}
public void exportKeys() {
Log.d(Constants.TAG, "exportKeys started");
// Send all information needed to service to export key in other thread
Intent intent = new Intent(this, ApgService.class);
intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_EXPORT_KEY);
// fill values for this action
Bundle data = new Bundle();
data.putString(ApgService.EXPORT_FILENAME, mExportFilename);
data.putInt(ApgService.EXPORT_KEY_TYPE, mKeyType);
if (mSelectedItem == -1) {
data.putBoolean(ApgService.EXPORT_ALL, true);
} else {
int keyRingId = mListAdapter.getKeyRingId(mSelectedItem);
data.putInt(ApgService.EXPORT_KEY_RING_ID, keyRingId);
mSelectedItem = -1;
}
intent.putExtra(ApgService.EXTRA_DATA, data);
// Message is received after exporting is done in ApgService
ApgServiceHandler exportHandler = new ApgServiceHandler(this, R.string.progress_exporting,
ProgressDialog.STYLE_HORIZONTAL) {
public void handleMessage(Message message) {
// handle messages by standard ApgHandler first
super.handleMessage(message);
if (message.arg1 == ApgServiceHandler.MESSAGE_OKAY) {
// get returned data bundle
Bundle returnData = message.getData();
int exported = returnData.getInt(ApgService.RESULT_EXPORT);
String toastMessage;
if (exported == 1) {
toastMessage = getString(R.string.keyExported);
} else if (exported > 0) {
toastMessage = getString(R.string.keysExported, exported);
} else {
toastMessage = getString(R.string.noKeysExported);
}
Toast.makeText(KeyListActivityOld.this, toastMessage, Toast.LENGTH_SHORT).show();
}
};
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(exportHandler);
intent.putExtra(ApgService.EXTRA_MESSENGER, messenger);
// show progress dialog
exportHandler.showProgressDialog(this);
// start service with intent
startService(intent);
}
protected void refreshList() {
mListAdapter.rebuild(true);
mListAdapter.notifyDataSetChanged();
}
protected class KeyListAdapter extends BaseExpandableListAdapter {
private LayoutInflater mInflater;
private Vector<Vector<KeyChild>> mChildren;
private SQLiteDatabase mDatabase;
private Cursor mCursor;
private String mSearchString;
private class KeyChild {
public static final int KEY = 0;
public static final int USER_ID = 1;
public static final int FINGER_PRINT = 2;
public int type;
public String userId;
public long keyId;
public boolean isMasterKey;
public int algorithm;
public int keySize;
public boolean canSign;
public boolean canEncrypt;
public String fingerPrint;
public KeyChild(long keyId, boolean isMasterKey, int algorithm, int keySize,
boolean canSign, boolean canEncrypt) {
this.type = KEY;
this.keyId = keyId;
this.isMasterKey = isMasterKey;
this.algorithm = algorithm;
this.keySize = keySize;
this.canSign = canSign;
this.canEncrypt = canEncrypt;
}
public KeyChild(String userId) {
type = USER_ID;
this.userId = userId;
}
public KeyChild(String fingerPrint, boolean isFingerPrint) {
type = FINGER_PRINT;
this.fingerPrint = fingerPrint;
}
}
public KeyListAdapter(Context context, String searchString) {
mSearchString = searchString;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// mDatabase = PGPMain.getDatabase().db();
// SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
// qb.setTables(KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " + "("
// + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + Keys.TABLE_NAME + "."
// + Keys.KEY_RING_ID + " AND " + Keys.TABLE_NAME + "." + Keys.IS_MASTER_KEY
// + " = '1'" + ") " + " INNER JOIN " + UserIds.TABLE_NAME + " ON " + "("
// + Keys.TABLE_NAME + "." + Keys._ID + " = " + UserIds.TABLE_NAME + "."
// + UserIds.KEY_ID + " AND " + UserIds.TABLE_NAME + "." + UserIds.RANK
// + " = '0')");
//
// if (searchString != null && searchString.trim().length() > 0) {
// String[] chunks = searchString.trim().split(" +");
// qb.appendWhere("EXISTS (SELECT tmp." + UserIds._ID + " FROM " + UserIds.TABLE_NAME
// + " AS tmp WHERE " + "tmp." + UserIds.KEY_ID + " = " + Keys.TABLE_NAME
// + "." + Keys._ID);
// for (int i = 0; i < chunks.length; ++i) {
// qb.appendWhere(" AND tmp." + UserIds.USER_ID + " LIKE ");
// qb.appendWhereEscapeString("%" + chunks[i] + "%");
// }
// qb.appendWhere(")");
// }
//
// mCursor = qb.query(mDatabase, new String[] { KeyRings.TABLE_NAME + "." + KeyRings._ID, // 0
// KeyRings.TABLE_NAME + "." + KeyRings.MASTER_KEY_ID, // 1
// UserIds.TABLE_NAME + "." + UserIds.USER_ID, // 2
// }, KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?", new String[] { ""
// + (mKeyType == Id.type.public_key ? Id.database.type_public
// : Id.database.type_secret) }, null, null, UserIds.TABLE_NAME + "."
// + UserIds.USER_ID + " ASC");
// content provider way for reference, might have to go back to it sometime:
/*
* Uri contentUri = null; if (mKeyType == Id.type.secret_key) { contentUri =
* Apg.CONTENT_URI_SECRET_KEY_RINGS; } else { contentUri =
* Apg.CONTENT_URI_PUBLIC_KEY_RINGS; } mCursor = getContentResolver().query( contentUri,
* new String[] { DataProvider._ID, // 0 DataProvider.MASTER_KEY_ID, // 1
* DataProvider.USER_ID, // 2 }, null, null, null);
*/
startManagingCursor(mCursor);
rebuild(false);
}
public void cleanup() {
if (mCursor != null) {
stopManagingCursor(mCursor);
mCursor.close();
}
}
public void rebuild(boolean requery) {
if (requery) {
mCursor.requery();
}
mChildren = new Vector<Vector<KeyChild>>();
for (int i = 0; i < mCursor.getCount(); ++i) {
mChildren.add(null);
}
}
protected Vector<KeyChild> getChildrenOfGroup(int groupPosition) {
Vector<KeyChild> children = mChildren.get(groupPosition);
if (children != null) {
return children;
}
mCursor.moveToPosition(groupPosition);
children = new Vector<KeyChild>();
// Cursor c = mDatabase.query(Keys.TABLE_NAME, new String[] { Keys._ID, // 0
// Keys.KEY_ID, // 1
// Keys.IS_MASTER_KEY, // 2
// Keys.ALGORITHM, // 3
// Keys.KEY_SIZE, // 4
// Keys.CAN_SIGN, // 5
// Keys.CAN_ENCRYPT, // 6
// }, Keys.KEY_RING_ID + " = ?", new String[] { mCursor.getString(0) }, null, null,
// Keys.RANK + " ASC");
// int masterKeyId = -1;
// long fingerPrintId = -1;
// for (int i = 0; i < c.getCount(); ++i) {
// c.moveToPosition(i);
// children.add(new KeyChild(c.getLong(1), c.getInt(2) == 1, c.getInt(3), c.getInt(4),
// c.getInt(5) == 1, c.getInt(6) == 1));
// if (i == 0) {
// masterKeyId = c.getInt(0);
// fingerPrintId = c.getLong(1);
// }
// }
// c.close();
//
// if (masterKeyId != -1) {
// children.insertElementAt(
// new KeyChild(PGPHelper.getFingerPrint(KeyListActivity.this, fingerPrintId),
// true), 0);
// c = mDatabase.query(UserIds.TABLE_NAME, new String[] { UserIds.USER_ID, // 0
// }, UserIds.KEY_ID + " = ? AND " + UserIds.RANK + " > 0", new String[] { ""
// + masterKeyId }, null, null, UserIds.RANK + " ASC");
//
// for (int i = 0; i < c.getCount(); ++i) {
// c.moveToPosition(i);
// children.add(new KeyChild(c.getString(0)));
// }
// c.close();
// }
mChildren.set(groupPosition, children);
return children;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public int getGroupCount() {
return mCursor.getCount();
}
public Object getChild(int groupPosition, int childPosition) {
return null;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return getChildrenOfGroup(groupPosition).size();
}
public Object getGroup(int position) {
return position;
}
public long getGroupId(int position) {
mCursor.moveToPosition(position);
return mCursor.getLong(1); // MASTER_KEY_ID
}
public int getKeyRingId(int position) {
mCursor.moveToPosition(position);
return mCursor.getInt(0); // _ID
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
mCursor.moveToPosition(groupPosition);
View view = mInflater.inflate(R.layout.key_list_group_item, null);
view.setBackgroundResource(android.R.drawable.list_selector_background);
TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId);
mainUserId.setText("");
TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest);
mainUserIdRest.setText("");
String userId = mCursor.getString(2); // USER_ID
if (userId != null) {
String chunks[] = userId.split(" <", 2);
userId = chunks[0];
if (chunks.length > 1) {
mainUserIdRest.setText("<" + chunks[1]);
}
mainUserId.setText(userId);
}
if (mainUserId.getText().length() == 0) {
mainUserId.setText(R.string.unknownUserId);
}
if (mainUserIdRest.getText().length() == 0) {
mainUserIdRest.setVisibility(View.GONE);
}
return view;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
mCursor.moveToPosition(groupPosition);
Vector<KeyChild> children = getChildrenOfGroup(groupPosition);
KeyChild child = children.get(childPosition);
View view = null;
switch (child.type) {
case KeyChild.KEY: {
if (child.isMasterKey) {
view = mInflater.inflate(R.layout.key_list_child_item_master_key, null);
} else {
view = mInflater.inflate(R.layout.key_list_child_item_sub_key, null);
}
TextView keyId = (TextView) view.findViewById(R.id.keyId);
String keyIdStr = PGPHelper.getSmallFingerPrint(child.keyId);
keyId.setText(keyIdStr);
TextView keyDetails = (TextView) view.findViewById(R.id.keyDetails);
String algorithmStr = PGPHelper.getAlgorithmInfo(child.algorithm, child.keySize);
keyDetails.setText("(" + algorithmStr + ")");
ImageView encryptIcon = (ImageView) view.findViewById(R.id.ic_encryptKey);
if (!child.canEncrypt) {
encryptIcon.setVisibility(View.GONE);
}
ImageView signIcon = (ImageView) view.findViewById(R.id.ic_signKey);
if (!child.canSign) {
signIcon.setVisibility(View.GONE);
}
break;
}
case KeyChild.USER_ID: {
view = mInflater.inflate(R.layout.key_list_child_item_user_id, null);
TextView userId = (TextView) view.findViewById(R.id.userId);
userId.setText(child.userId);
break;
}
case KeyChild.FINGER_PRINT: {
view = mInflater.inflate(R.layout.key_list_child_item_user_id, null);
TextView userId = (TextView) view.findViewById(R.id.userId);
userId.setText(getString(R.string.fingerprint) + ":\n"
+ child.fingerPrint.replace(" ", "\n"));
break;
}
}
return view;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case Id.request.filename: {
if (resultCode == RESULT_OK && data != null) {
try {
String path = data.getData().getPath();
Log.d(Constants.TAG, "path=" + path);
mFileDialog.setFilename(path);
} catch (NullPointerException e) {
Log.e(Constants.TAG, "Nullpointer while retrieving path!", e);
}
}
return;
}
default: {
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}

View File

@ -1,24 +1,31 @@
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thialfihar.android.apg.ui; package org.thialfihar.android.apg.ui;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.Id; import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.R; import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.helper.PGPHelper;
import org.thialfihar.android.apg.provider.ProviderHelper;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
public class KeyListPublicActivity extends KeyListActivity { public class KeyListPublicActivity extends KeyListActivity {
@ -26,28 +33,23 @@ public class KeyListPublicActivity extends KeyListActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mKeyType = Id.type.public_key;
setContentView(R.layout.key_list_public_activity); setContentView(R.layout.key_list_public_activity);
mExportFilename = Constants.path.APP_DIR + "/pubexport.asc"; mExportFilename = Constants.path.APP_DIR + "/pubexport.asc";
mKeyType = Id.type.public_key;
} }
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, Id.menu.option.search, 0, R.string.menu_search)
.setIcon(R.drawable.ic_menu_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(1, Id.menu.option.scanQRCode, 1, R.string.menu_scanQRCode)
.setIcon(R.drawable.ic_menu_scan_qrcode)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(1, Id.menu.option.key_server, 2, R.string.menu_keyServer) menu.add(1, Id.menu.option.key_server, 2, R.string.menu_keyServer)
.setIcon(R.drawable.ic_menu_search_list) .setIcon(R.drawable.ic_menu_search_list)
.setShowAsAction( .setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(0, Id.menu.option.import_keys, 3, R.string.menu_importKeys).setShowAsAction( menu.add(1, Id.menu.option.scanQRCode, 1, R.string.menu_scanQRCode)
MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT); .setIcon(R.drawable.ic_menu_scan_qrcode)
menu.add(0, Id.menu.option.export_keys, 4, R.string.menu_exportKeys).setShowAsAction( .setShowAsAction(
MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT); MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true; return true;
} }
@ -74,95 +76,6 @@ public class KeyListPublicActivity extends KeyListActivity {
} }
} }
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
// TODO: user id? menu.setHeaderTitle("Key");
menu.add(0, Id.menu.export, 0, R.string.menu_exportKey);
menu.add(0, Id.menu.delete, 1, R.string.menu_deleteKey);
menu.add(0, Id.menu.update, 1, R.string.menu_updateKey);
menu.add(0, Id.menu.exportToServer, 1, R.string.menu_exportKeyToServer);
menu.add(0, Id.menu.signKey, 1, R.string.menu_signKey);
}
}
@Override
public boolean onContextItemSelected(android.view.MenuItem menuItem) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuItem.getMenuInfo();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
if (type != ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
return super.onContextItemSelected(menuItem);
}
switch (menuItem.getItemId()) {
case Id.menu.update: {
mSelectedItem = groupPosition;
final int keyRingId = mListAdapter.getKeyRingId(groupPosition);
long keyId = 0;
PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRingByMasterKeyId(this,
keyRingId);
if (keyRing != null) {
keyId = PGPHelper.getMasterKey((PGPPublicKeyRing) keyRing).getKeyID();
}
if (keyId == 0) {
// this shouldn't happen
return true;
}
Intent intent = new Intent(this, KeyServerQueryActivity.class);
intent.setAction(KeyServerQueryActivity.ACTION_LOOK_UP_KEY_ID_AND_RETURN);
intent.putExtra(KeyServerQueryActivity.EXTRA_KEY_ID, keyId);
startActivityForResult(intent, Id.request.look_up_key_id);
return true;
}
case Id.menu.exportToServer: {
mSelectedItem = groupPosition;
final int keyRingId = mListAdapter.getKeyRingId(groupPosition);
Intent intent = new Intent(this, KeyServerUploadActivity.class);
intent.setAction(KeyServerUploadActivity.ACTION_EXPORT_KEY_TO_SERVER);
intent.putExtra(KeyServerUploadActivity.EXTRA_KEY_ID, keyRingId);
startActivityForResult(intent, Id.request.export_to_server);
return true;
}
case Id.menu.signKey: {
mSelectedItem = groupPosition;
final int keyRingId = mListAdapter.getKeyRingId(groupPosition);
long keyId = 0;
PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRingByMasterKeyId(this,
keyRingId);
if (keyRing != null) {
keyId = PGPHelper.getMasterKey((PGPPublicKeyRing) keyRing).getKeyID();
}
if (keyId == 0) {
// this shouldn't happen
return true;
}
Intent intent = new Intent(this, SignKeyActivity.class);
intent.putExtra(SignKeyActivity.EXTRA_KEY_ID, keyId);
startActivity(intent);
return true;
}
default: {
return super.onContextItemSelected(menuItem);
}
}
}
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) { switch (requestCode) {
@ -175,7 +88,7 @@ public class KeyListPublicActivity extends KeyListActivity {
Intent intent = new Intent(this, KeyListPublicActivity.class); Intent intent = new Intent(this, KeyListPublicActivity.class);
intent.setAction(KeyListPublicActivity.ACTION_IMPORT); intent.setAction(KeyListPublicActivity.ACTION_IMPORT);
intent.putExtra(KeyListPublicActivity.EXTRA_TEXT, intent.putExtra(KeyListPublicActivity.EXTRA_TEXT,
data.getStringExtra(KeyListActivityOld.EXTRA_TEXT)); data.getStringExtra(KeyListActivity.EXTRA_TEXT));
handleIntent(intent); handleIntent(intent);
break; break;
} }

View File

@ -1,198 +0,0 @@
///*
// * Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package org.thialfihar.android.apg.ui;
//
//import org.spongycastle.openpgp.PGPPublicKeyRing;
//import org.thialfihar.android.apg.Constants;
//import org.thialfihar.android.apg.Id;
//import org.thialfihar.android.apg.R;
//import org.thialfihar.android.apg.helper.PGPHelper;
//import org.thialfihar.android.apg.helper.PGPMain;
//import org.thialfihar.android.apg.provider.ProviderHelper;
//
//import com.actionbarsherlock.view.Menu;
//import com.actionbarsherlock.view.MenuItem;
//
//import android.content.Intent;
//import android.os.Bundle;
//import android.view.ContextMenu;
//import android.view.ContextMenu.ContextMenuInfo;
//import android.view.View;
//import android.widget.ExpandableListView;
//import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
//
//public class KeyListPublicActivityOld extends KeyListActivityOld {
// @Override
// public void onCreate(Bundle savedInstanceState) {
// mExportFilename = Constants.path.APP_DIR + "/pubexport.asc";
// mKeyType = Id.type.public_key;
// super.onCreate(savedInstanceState);
// }
//
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// menu.add(1, Id.menu.option.search, 0, R.string.menu_search)
// .setIcon(R.drawable.ic_menu_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
// menu.add(1, Id.menu.option.scanQRCode, 1, R.string.menu_scanQRCode)
// .setIcon(R.drawable.ic_menu_scan_qrcode)
// .setShowAsAction(
// MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// menu.add(1, Id.menu.option.key_server, 2, R.string.menu_keyServer)
// .setIcon(R.drawable.ic_menu_search_list)
// .setShowAsAction(
// MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// menu.add(0, Id.menu.option.import_keys, 3, R.string.menu_importKeys).setShowAsAction(
// MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// menu.add(0, Id.menu.option.export_keys, 4, R.string.menu_exportKeys).setShowAsAction(
// MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
//
// return true;
// }
//
// @Override
// public boolean onOptionsItemSelected(MenuItem item) {
// switch (item.getItemId()) {
// case Id.menu.option.key_server: {
// startActivity(new Intent(this, KeyServerQueryActivity.class));
//
// return true;
// }
// case Id.menu.option.scanQRCode: {
// Intent intent = new Intent(this, ImportFromQRCodeActivity.class);
// intent.setAction(ImportFromQRCodeActivity.IMPORT_FROM_QR_CODE);
// startActivityForResult(intent, Id.request.import_from_qr_code);
//
// return true;
// }
//
// default: {
// return super.onOptionsItemSelected(item);
// }
// }
// }
//
// @Override
// public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// super.onCreateContextMenu(menu, v, menuInfo);
// ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
// int type = ExpandableListView.getPackedPositionType(info.packedPosition);
//
// if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
// // TODO: user id? menu.setHeaderTitle("Key");
// menu.add(0, Id.menu.export, 0, R.string.menu_exportKey);
// menu.add(0, Id.menu.delete, 1, R.string.menu_deleteKey);
// menu.add(0, Id.menu.update, 1, R.string.menu_updateKey);
// menu.add(0, Id.menu.exportToServer, 1, R.string.menu_exportKeyToServer);
// menu.add(0, Id.menu.signKey, 1, R.string.menu_signKey);
// }
// }
//
// @Override
// public boolean onContextItemSelected(android.view.MenuItem menuItem) {
// ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuItem.getMenuInfo();
// int type = ExpandableListView.getPackedPositionType(info.packedPosition);
// int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
//
// if (type != ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
// return super.onContextItemSelected(menuItem);
// }
//
// switch (menuItem.getItemId()) {
// case Id.menu.update: {
// mSelectedItem = groupPosition;
// final int keyRingId = mListAdapter.getKeyRingId(groupPosition);
// long keyId = 0;
// PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRingByMasterKeyId(this, keyRingId);
// if (keyRing != null) {
// keyId = PGPHelper.getMasterKey((PGPPublicKeyRing) keyRing).getKeyID();
// }
// if (keyId == 0) {
// // this shouldn't happen
// return true;
// }
//
// Intent intent = new Intent(this, KeyServerQueryActivity.class);
// intent.setAction(KeyServerQueryActivity.ACTION_LOOK_UP_KEY_ID_AND_RETURN);
// intent.putExtra(KeyServerQueryActivity.EXTRA_KEY_ID, keyId);
// startActivityForResult(intent, Id.request.look_up_key_id);
//
// return true;
// }
//
// case Id.menu.exportToServer: {
// mSelectedItem = groupPosition;
// final int keyRingId = mListAdapter.getKeyRingId(groupPosition);
//
// Intent intent = new Intent(this, KeyServerUploadActivity.class);
// intent.setAction(KeyServerUploadActivity.ACTION_EXPORT_KEY_TO_SERVER);
// intent.putExtra(KeyServerUploadActivity.EXTRA_KEY_ID, keyRingId);
// startActivityForResult(intent, Id.request.export_to_server);
//
// return true;
// }
//
// case Id.menu.signKey: {
// mSelectedItem = groupPosition;
// final int keyRingId = mListAdapter.getKeyRingId(groupPosition);
// long keyId = 0;
// PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRingByMasterKeyId(this, keyRingId);
// if (keyRing != null) {
// keyId = PGPHelper.getMasterKey((PGPPublicKeyRing) keyRing).getKeyID();
// }
//
// if (keyId == 0) {
// // this shouldn't happen
// return true;
// }
//
// Intent intent = new Intent(this, SignKeyActivity.class);
// intent.putExtra(SignKeyActivity.EXTRA_KEY_ID, keyId);
// startActivity(intent);
//
// return true;
// }
//
// default: {
// return super.onContextItemSelected(menuItem);
// }
// }
// }
//
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// switch (requestCode) {
// case Id.request.look_up_key_id: {
// if (resultCode == RESULT_CANCELED || data == null
// || data.getStringExtra(KeyServerQueryActivity.RESULT_EXTRA_TEXT) == null) {
// return;
// }
//
// Intent intent = new Intent(this, KeyListPublicActivity.class);
// intent.setAction(KeyListPublicActivity.ACTION_IMPORT);
// intent.putExtra(KeyListPublicActivity.EXTRA_TEXT,
// data.getStringExtra(KeyListActivityOld.EXTRA_TEXT));
// handleIntent(intent);
// break;
// }
//
// default: {
// super.onActivityResult(requestCode, resultCode, data);
// break;
// }
// }
// }
//}

View File

@ -1,25 +1,50 @@
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thialfihar.android.apg.ui; package org.thialfihar.android.apg.ui;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.helper.PGPHelper;
import org.thialfihar.android.apg.provider.ProviderHelper;
import org.thialfihar.android.apg.provider.ApgContract.KeyRings; import org.thialfihar.android.apg.provider.ApgContract.KeyRings;
import org.thialfihar.android.apg.provider.ApgContract.UserIds; import org.thialfihar.android.apg.provider.ApgContract.UserIds;
import org.thialfihar.android.apg.ui.widget.ExpandableListFragment;
import org.thialfihar.android.apg.ui.widget.KeyListAdapter; import org.thialfihar.android.apg.ui.widget.KeyListAdapter;
import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.content.CursorLoader; import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader; import android.support.v4.content.Loader;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager; import android.support.v4.app.LoaderManager;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
public class KeyListPublicFragment extends ExpandableListFragment implements public class KeyListPublicFragment extends KeyListFragment implements
LoaderManager.LoaderCallbacks<Cursor> { LoaderManager.LoaderCallbacks<Cursor> {
private FragmentActivity mActivity; private KeyListPublicActivity mKeyListPublicActivity;
private KeyListAdapter mAdapter;
// private long mCurrentRowId; private KeyListAdapter mAdapter;
/** /**
* Define Adapter and Loader on create of Activity * Define Adapter and Loader on create of Activity
@ -28,19 +53,9 @@ public class KeyListPublicFragment extends ExpandableListFragment implements
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
mActivity = getActivity(); mKeyListPublicActivity = (KeyListPublicActivity) getActivity();
// register long press context menu mAdapter = new KeyListAdapter(mKeyListPublicActivity, null, Id.type.public_key);
registerForContextMenu(getListView());
// Give some text to display if there is no data. In a real
// application this would come from a resource.
setEmptyText("TODO empty");
// We have a menu item to show in action bar.
setHasOptionsMenu(true);
mAdapter = new KeyListAdapter(mActivity, null, KeyListAdapter.KEY_TYPE_PUBLIC);
setListAdapter(mAdapter); setListAdapter(mAdapter);
// Start out with a progress indicator. // Start out with a progress indicator.
@ -48,9 +63,93 @@ public class KeyListPublicFragment extends ExpandableListFragment implements
// Prepare the loader. Either re-connect with an existing one, // Prepare the loader. Either re-connect with an existing one,
// or start a new one. // or start a new one.
// id is -1 as the child cursors are numbered 0,...,n
getLoaderManager().initLoader(-1, null, this); getLoaderManager().initLoader(-1, null, this);
} }
/**
* Context Menu on Long Click
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// TODO: better inflate menu from xml
// android.view.MenuInflater inflater = (android.view.MenuInflater) mActivity
// .getMenuInflater();
// menu.setHeaderTitle(R.string.checkbox_list_context_title);
// inflater.inflate(R.menu.checkbox_list_context, menu);
menu.add(0, Id.menu.update, 1, R.string.menu_updateKey);
menu.add(0, Id.menu.exportToServer, 1, R.string.menu_exportKeyToServer);
menu.add(0, Id.menu.signKey, 1, R.string.menu_signKey);
}
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
ExpandableListContextMenuInfo expInfo = (ExpandableListContextMenuInfo) item.getMenuInfo();
// expInfo.id would also return row id of childs, but we always want to get the row id of
// the group item, thus we are using the following way
int groupPosition = ExpandableListView.getPackedPositionGroup(expInfo.packedPosition);
long keyRingRowId = getExpandableListAdapter().getGroupId(groupPosition);
switch (item.getItemId()) {
case Id.menu.update:
long updateKeyId = 0;
PGPPublicKeyRing updateKeyRing = ProviderHelper.getPGPPublicKeyRing(mKeyListActivity,
keyRingRowId);
if (updateKeyRing != null) {
updateKeyId = PGPHelper.getMasterKey(updateKeyRing).getKeyID();
}
if (updateKeyId == 0) {
// this shouldn't happen
return true;
}
Intent queryIntent = new Intent(mKeyListActivity, KeyServerQueryActivity.class);
queryIntent.setAction(KeyServerQueryActivity.ACTION_LOOK_UP_KEY_ID_AND_RETURN);
queryIntent.putExtra(KeyServerQueryActivity.EXTRA_KEY_ID, updateKeyId);
// TODO: lookup??
startActivityForResult(queryIntent, Id.request.look_up_key_id);
return true;
case Id.menu.exportToServer:
// TODO: do it better directly with keyRingRowId?
long masterKeyId = ProviderHelper.getPublicMasterKeyId(mKeyListActivity, keyRingRowId);
Intent uploadIntent = new Intent(mKeyListActivity, KeyServerUploadActivity.class);
uploadIntent.setAction(KeyServerUploadActivity.ACTION_EXPORT_KEY_TO_SERVER);
uploadIntent.putExtra(KeyServerUploadActivity.EXTRA_KEY_ID, masterKeyId);
startActivityForResult(uploadIntent, Id.request.export_to_server);
return true;
case Id.menu.signKey:
long keyId = 0;
PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRing(mKeyListActivity,
keyRingRowId);
if (signKeyRing != null) {
keyId = PGPHelper.getMasterKey(signKeyRing).getKeyID();
}
if (keyId == 0) {
// this shouldn't happen
return true;
}
Intent signIntent = new Intent(mKeyListActivity, SignKeyActivity.class);
signIntent.putExtra(SignKeyActivity.EXTRA_KEY_ID, keyId);
startActivity(signIntent);
return true;
default:
return super.onContextItemSelected(item);
}
}
// These are the rows that we will retrieve. // These are the rows that we will retrieve.
static final String[] PROJECTION = new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID, static final String[] PROJECTION = new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID,
UserIds.USER_ID }; UserIds.USER_ID };

View File

@ -1,20 +1,32 @@
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thialfihar.android.apg.ui; package org.thialfihar.android.apg.ui;
import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.Id; import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.R; import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.helper.PGPHelper;
import org.thialfihar.android.apg.helper.PGPMain; import org.thialfihar.android.apg.helper.PGPMain;
import org.thialfihar.android.apg.service.PassphraseCacheService; import org.thialfihar.android.apg.service.PassphraseCacheService;
import org.thialfihar.android.apg.ui.dialog.PassphraseDialogFragment; import org.thialfihar.android.apg.ui.dialog.PassphraseDialogFragment;
import org.thialfihar.android.apg.ui.widget.KeyListAdapter;
import org.thialfihar.android.apg.util.Log; import org.thialfihar.android.apg.util.Log;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import com.google.zxing.integration.android.IntentIntegrator;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
@ -25,7 +37,6 @@ import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo; import android.view.ContextMenu.ContextMenuInfo;
import android.view.View; import android.view.View;
import android.widget.ExpandableListView; import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
public class KeyListSecretActivity extends KeyListActivity { public class KeyListSecretActivity extends KeyListActivity {
@ -33,23 +44,20 @@ public class KeyListSecretActivity extends KeyListActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mKeyType = Id.type.secret_key;
setContentView(R.layout.key_list_secret_activity); setContentView(R.layout.key_list_secret_activity);
mExportFilename = Constants.path.APP_DIR + "/secexport.asc"; mExportFilename = Constants.path.APP_DIR + "/secexport.asc";
mKeyType = Id.type.secret_key;
// mList.setOnChildClickListener(this);
} }
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
menu.add(3, Id.menu.option.search, 0, R.string.menu_search) super.onCreateOptionsMenu(menu);
.setIcon(R.drawable.ic_menu_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(1, Id.menu.option.create, 1, R.string.menu_createKey).setShowAsAction( menu.add(1, Id.menu.option.create, 1, R.string.menu_createKey).setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(0, Id.menu.option.import_keys, 2, R.string.menu_importKeys).setShowAsAction(
MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(0, Id.menu.option.export_keys, 3, R.string.menu_exportKeys).setShowAsAction(
MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true; return true;
} }
@ -68,70 +76,21 @@ public class KeyListSecretActivity extends KeyListActivity {
} }
} }
@Override //
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
super.onCreateContextMenu(menu, v, menuInfo); // int childPosition, long id) {
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; // mSelectedItem = groupPosition;
int type = ExpandableListView.getPackedPositionType(info.packedPosition); // checkPassPhraseAndEdit();
// return true;
if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { // }
// TODO: user id? menu.setHeaderTitle("Key"); //
menu.add(0, Id.menu.edit, 0, R.string.menu_editKey); public void checkPassPhraseAndEdit(long keyId) {
menu.add(0, Id.menu.export, 1, R.string.menu_exportKey);
menu.add(0, Id.menu.delete, 2, R.string.menu_deleteKey);
menu.add(0, Id.menu.share_qr_code, 2, R.string.menu_share);
}
}
@Override
public boolean onContextItemSelected(android.view.MenuItem menuItem) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuItem.getMenuInfo();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
if (type != ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
return super.onContextItemSelected(menuItem);
}
switch (menuItem.getItemId()) {
case Id.menu.edit: {
mSelectedItem = groupPosition;
checkPassPhraseAndEdit();
return true;
}
case Id.menu.share_qr_code: {
mSelectedItem = groupPosition;
long keyId = ((KeyListAdapter) mList.getExpandableListAdapter())
.getGroupId(mSelectedItem);
// String msg = keyId + "," + PGPHelper.getFingerPrint(keyId);
String msg = PGPHelper.getPubkeyAsArmoredString(this, keyId);
new IntentIntegrator(this).shareText(msg);
}
default: {
return super.onContextItemSelected(menuItem);
}
}
}
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
mSelectedItem = groupPosition;
checkPassPhraseAndEdit();
return true;
}
public void checkPassPhraseAndEdit() {
long keyId = ((KeyListAdapter) mList.getExpandableListAdapter()).getGroupId(mSelectedItem);
String passPhrase = PassphraseCacheService.getCachedPassphrase(this, keyId); String passPhrase = PassphraseCacheService.getCachedPassphrase(this, keyId);
if (passPhrase == null) { if (passPhrase == null) {
showPassphraseDialog(keyId); showPassphraseDialog(keyId);
} else { } else {
PGPMain.setEditPassPhrase(passPhrase); PGPMain.setEditPassPhrase(passPhrase);
editKey(); editKey(keyId);
} }
} }
@ -144,7 +103,7 @@ public class KeyListSecretActivity extends KeyListActivity {
String passPhrase = PassphraseCacheService.getCachedPassphrase( String passPhrase = PassphraseCacheService.getCachedPassphrase(
KeyListSecretActivity.this, secretKeyId); KeyListSecretActivity.this, secretKeyId);
PGPMain.setEditPassPhrase(passPhrase); PGPMain.setEditPassPhrase(passPhrase);
editKey(); editKey(secretKeyId);
} }
} }
}; };
@ -170,8 +129,7 @@ public class KeyListSecretActivity extends KeyListActivity {
startActivityForResult(intent, Id.message.create_key); startActivityForResult(intent, Id.message.create_key);
} }
private void editKey() { private void editKey(long keyId) {
long keyId = ((KeyListAdapter) mList.getExpandableListAdapter()).getGroupId(mSelectedItem);
Intent intent = new Intent(EditKeyActivity.ACTION_EDIT_KEY); Intent intent = new Intent(EditKeyActivity.ACTION_EDIT_KEY);
intent.putExtra(EditKeyActivity.EXTRA_KEY_ID, keyId); intent.putExtra(EditKeyActivity.EXTRA_KEY_ID, keyId);
startActivityForResult(intent, Id.message.edit_key); startActivityForResult(intent, Id.message.edit_key);

View File

@ -1,210 +0,0 @@
///*
// * Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package org.thialfihar.android.apg.ui;
//
//import org.thialfihar.android.apg.R;
//import org.thialfihar.android.apg.Constants;
//import org.thialfihar.android.apg.Id;
//import org.thialfihar.android.apg.helper.PGPHelper;
//import org.thialfihar.android.apg.helper.PGPMain;
//import org.thialfihar.android.apg.service.PassphraseCacheService;
//import org.thialfihar.android.apg.ui.dialog.PassphraseDialogFragment;
//import org.thialfihar.android.apg.util.Log;
//
//import com.actionbarsherlock.view.Menu;
//import com.actionbarsherlock.view.MenuItem;
//
//import android.content.Intent;
//import android.os.Bundle;
//import android.os.Handler;
//import android.os.Message;
//import android.os.Messenger;
//import android.view.ContextMenu;
//import android.view.ContextMenu.ContextMenuInfo;
//import android.view.View;
//import android.widget.ExpandableListView;
//import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
//import android.widget.ExpandableListView.OnChildClickListener;
//
//import com.google.zxing.integration.android.IntentIntegrator;
//
//public class KeyListSecretActivityOld extends KeyListActivityOld implements OnChildClickListener {
//
// @Override
// public void onCreate(Bundle savedInstanceState) {
// mExportFilename = Constants.path.APP_DIR + "/secexport.asc";
// mKeyType = Id.type.secret_key;
// super.onCreate(savedInstanceState);
// mList.setOnChildClickListener(this);
// }
//
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// menu.add(3, Id.menu.option.search, 0, R.string.menu_search)
// .setIcon(R.drawable.ic_menu_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
// menu.add(1, Id.menu.option.create, 1, R.string.menu_createKey).setShowAsAction(
// MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// menu.add(0, Id.menu.option.import_keys, 2, R.string.menu_importKeys).setShowAsAction(
// MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// menu.add(0, Id.menu.option.export_keys, 3, R.string.menu_exportKeys).setShowAsAction(
// MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
//
// return true;
// }
//
// @Override
// public boolean onOptionsItemSelected(MenuItem item) {
// switch (item.getItemId()) {
// case Id.menu.option.create: {
// createKey();
// return true;
// }
//
// default: {
// return super.onOptionsItemSelected(item);
// }
// }
// }
//
// @Override
// public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// super.onCreateContextMenu(menu, v, menuInfo);
// ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
// int type = ExpandableListView.getPackedPositionType(info.packedPosition);
//
// if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
// // TODO: user id? menu.setHeaderTitle("Key");
// menu.add(0, Id.menu.edit, 0, R.string.menu_editKey);
// menu.add(0, Id.menu.export, 1, R.string.menu_exportKey);
// menu.add(0, Id.menu.delete, 2, R.string.menu_deleteKey);
// menu.add(0, Id.menu.share_qr_code, 2, R.string.menu_share);
// }
// }
//
// @Override
// public boolean onContextItemSelected(android.view.MenuItem menuItem) {
// ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuItem.getMenuInfo();
// int type = ExpandableListView.getPackedPositionType(info.packedPosition);
// int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
//
// if (type != ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
// return super.onContextItemSelected(menuItem);
// }
//
// switch (menuItem.getItemId()) {
// case Id.menu.edit: {
// mSelectedItem = groupPosition;
// checkPassPhraseAndEdit();
// return true;
// }
//
// case Id.menu.share_qr_code: {
// mSelectedItem = groupPosition;
//
// long keyId = ((KeyListAdapter) mList.getExpandableListAdapter())
// .getGroupId(mSelectedItem);
// // String msg = keyId + "," + PGPHelper.getFingerPrint(keyId);
// String msg = PGPHelper.getPubkeyAsArmoredString(this, keyId);
//
// new IntentIntegrator(this).shareText(msg);
// }
//
// default: {
// return super.onContextItemSelected(menuItem);
// }
// }
// }
//
// public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
// int childPosition, long id) {
// mSelectedItem = groupPosition;
// checkPassPhraseAndEdit();
// return true;
// }
//
// public void checkPassPhraseAndEdit() {
// long keyId = ((KeyListAdapter) mList.getExpandableListAdapter()).getGroupId(mSelectedItem);
// String passPhrase = PassphraseCacheService.getCachedPassphrase(this, keyId);
// if (passPhrase == null) {
// showPassphraseDialog(keyId);
// } else {
// PGPMain.setEditPassPhrase(passPhrase);
// editKey();
// }
// }
//
// private void showPassphraseDialog(final long secretKeyId) {
// // Message is received after passphrase is cached
// Handler returnHandler = new Handler() {
// @Override
// public void handleMessage(Message message) {
// if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) {
// String passPhrase = PassphraseCacheService.getCachedPassphrase(
// KeyListSecretActivity.this, secretKeyId);
// PGPMain.setEditPassPhrase(passPhrase);
// editKey();
// }
// }
// };
//
// // Create a new Messenger for the communication back
// Messenger messenger = new Messenger(returnHandler);
//
// try {
// PassphraseDialogFragment passphraseDialog = PassphraseDialogFragment.newInstance(
// KeyListSecretActivity.this, messenger, secretKeyId);
//
// passphraseDialog.show(getSupportFragmentManager(), "passphraseDialog");
// } catch (PGPMain.ApgGeneralException e) {
// Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!");
// // send message to handler to start encryption directly
// returnHandler.sendEmptyMessage(PassphraseDialogFragment.MESSAGE_OKAY);
// }
// }
//
// private void createKey() {
// PGPMain.setEditPassPhrase("");
// Intent intent = new Intent(EditKeyActivity.ACTION_CREATE_KEY);
// startActivityForResult(intent, Id.message.create_key);
// }
//
// private void editKey() {
// long keyId = ((KeyListAdapter) mList.getExpandableListAdapter()).getGroupId(mSelectedItem);
// Intent intent = new Intent(EditKeyActivity.ACTION_EDIT_KEY);
// intent.putExtra(EditKeyActivity.EXTRA_KEY_ID, keyId);
// startActivityForResult(intent, Id.message.edit_key);
// }
//
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// switch (requestCode) {
// case Id.message.create_key: // intentionally no break
// case Id.message.edit_key: {
// if (resultCode == RESULT_OK) {
// refreshList();
// }
// break;
// }
//
// default: {
// break;
// }
// }
//
// super.onActivityResult(requestCode, resultCode, data);
// }
//}

View File

@ -1,25 +1,50 @@
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thialfihar.android.apg.ui; package org.thialfihar.android.apg.ui;
import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.helper.PGPHelper;
import org.thialfihar.android.apg.provider.ProviderHelper;
import org.thialfihar.android.apg.provider.ApgContract.KeyRings; import org.thialfihar.android.apg.provider.ApgContract.KeyRings;
import org.thialfihar.android.apg.provider.ApgContract.UserIds; import org.thialfihar.android.apg.provider.ApgContract.UserIds;
import org.thialfihar.android.apg.ui.widget.ExpandableListFragment;
import org.thialfihar.android.apg.ui.widget.KeyListAdapter; import org.thialfihar.android.apg.ui.widget.KeyListAdapter;
import com.google.zxing.integration.android.IntentIntegrator;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.content.CursorLoader; import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader; import android.support.v4.content.Loader;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager; import android.support.v4.app.LoaderManager;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
public class KeyListSecretFragment extends ExpandableListFragment implements public class KeyListSecretFragment extends KeyListFragment implements
LoaderManager.LoaderCallbacks<Cursor> { LoaderManager.LoaderCallbacks<Cursor> {
private FragmentActivity mActivity; private KeyListSecretActivity mKeyListSecretActivity;
private KeyListAdapter mAdapter;
// private long mCurrentRowId; private KeyListAdapter mAdapter;
/** /**
* Define Adapter and Loader on create of Activity * Define Adapter and Loader on create of Activity
@ -28,19 +53,9 @@ public class KeyListSecretFragment extends ExpandableListFragment implements
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
mActivity = getActivity(); mKeyListSecretActivity = (KeyListSecretActivity) getActivity();
// register long press context menu mAdapter = new KeyListAdapter(mKeyListSecretActivity, null, Id.type.secret_key);
registerForContextMenu(getListView());
// Give some text to display if there is no data. In a real
// application this would come from a resource.
setEmptyText("TODO empty");
// We have a menu item to show in action bar.
setHasOptionsMenu(true);
mAdapter = new KeyListAdapter(mActivity, null, KeyListAdapter.KEY_TYPE_SECRET);
setListAdapter(mAdapter); setListAdapter(mAdapter);
// Start out with a progress indicator. // Start out with a progress indicator.
@ -48,9 +63,72 @@ public class KeyListSecretFragment extends ExpandableListFragment implements
// Prepare the loader. Either re-connect with an existing one, // Prepare the loader. Either re-connect with an existing one,
// or start a new one. // or start a new one.
// id is -1 as the child cursors are numbered 0,...,n
getLoaderManager().initLoader(-1, null, this); getLoaderManager().initLoader(-1, null, this);
} }
/**
* Context Menu on Long Click
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// TODO: better inflate menu from xml
// android.view.MenuInflater inflater = (android.view.MenuInflater) mActivity
// .getMenuInflater();
// menu.setHeaderTitle(R.string.checkbox_list_context_title);
// inflater.inflate(R.menu.checkbox_list_context, menu);
// ExpandableListView.ExpandableListContextMenuInfo info =
// (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
// int type = ExpandableListView.getPackedPositionType(info.packedPosition);
//
// if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
// // TODO: user id? menu.setHeaderTitle("Key");
// menu.add(0, Id.menu.edit, 0, R.string.menu_editKey);
// menu.add(0, Id.menu.export, 1, R.string.menu_exportKey);
// menu.add(0, Id.menu.delete, 2, R.string.menu_deleteKey);
// menu.add(0, Id.menu.share_qr_code, 2, R.string.menu_share);
// }
menu.add(0, Id.menu.edit, 0, R.string.menu_editKey);
menu.add(0, Id.menu.share_qr_code, 2, R.string.menu_share);
}
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
ExpandableListContextMenuInfo expInfo = (ExpandableListContextMenuInfo) item.getMenuInfo();
// expInfo.id would also return row id of childs, but we always want to get the row id of
// the group item, thus we are using the following way
int groupPosition = ExpandableListView.getPackedPositionGroup(expInfo.packedPosition);
long keyRingRowId = getExpandableListAdapter().getGroupId(groupPosition);
switch (item.getItemId()) {
case Id.menu.edit:
// TODO: do it better directly with keyRingRowId?
long masterKeyId = ProviderHelper.getSecretMasterKeyId(mKeyListSecretActivity,
keyRingRowId);
mKeyListSecretActivity.checkPassPhraseAndEdit(masterKeyId);
return true;
case Id.menu.share_qr_code:
// TODO: do it better directly with keyRingRowId?
long masterKeyId2 = ProviderHelper.getSecretMasterKeyId(mKeyListSecretActivity,
keyRingRowId);
String msg = PGPHelper.getPubkeyAsArmoredString(mKeyListSecretActivity, masterKeyId2);
new IntentIntegrator(mKeyListSecretActivity).shareText(msg);
return true;
default:
return super.onContextItemSelected(item);
}
}
// These are the rows that we will retrieve. // These are the rows that we will retrieve.
static final String[] PROJECTION = new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID, static final String[] PROJECTION = new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID,
UserIds.USER_ID }; UserIds.USER_ID };

View File

@ -1,4 +1,7 @@
/* /*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
*
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
@ -224,8 +227,8 @@ public class KeyServerQueryActivity extends SherlockFragmentActivity {
if (mKeyData != null) { if (mKeyData != null) {
Intent intent = new Intent(KeyServerQueryActivity.this, Intent intent = new Intent(KeyServerQueryActivity.this,
KeyListPublicActivity.class); KeyListPublicActivity.class);
intent.setAction(KeyListActivityOld.ACTION_IMPORT); intent.setAction(KeyListActivity.ACTION_IMPORT);
intent.putExtra(KeyListActivityOld.EXTRA_TEXT, mKeyData); intent.putExtra(KeyListActivity.EXTRA_TEXT, mKeyData);
startActivity(intent); startActivity(intent);
} }
} }

View File

@ -48,13 +48,13 @@ public class DeleteKeyDialogFragment extends DialogFragment {
/** /**
* Creates new instance of this delete file dialog fragment * Creates new instance of this delete file dialog fragment
*/ */
public static DeleteKeyDialogFragment newInstance(Messenger messenger, int deleteKeyRingRowId, public static DeleteKeyDialogFragment newInstance(Messenger messenger, long deleteKeyRingRowId,
int keyType) { int keyType) {
DeleteKeyDialogFragment frag = new DeleteKeyDialogFragment(); DeleteKeyDialogFragment frag = new DeleteKeyDialogFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putParcelable(ARG_MESSENGER, messenger); args.putParcelable(ARG_MESSENGER, messenger);
args.putInt(ARG_DELETE_KEY_RING_ROW_ID, deleteKeyRingRowId); args.putLong(ARG_DELETE_KEY_RING_ROW_ID, deleteKeyRingRowId);
args.putInt(ARG_KEY_TYPE, keyType); args.putInt(ARG_KEY_TYPE, keyType);
frag.setArguments(args); frag.setArguments(args);
@ -69,7 +69,7 @@ public class DeleteKeyDialogFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final FragmentActivity activity = getActivity(); final FragmentActivity activity = getActivity();
final int deleteKeyRingRowId = getArguments().getInt(ARG_DELETE_KEY_RING_ROW_ID); final long deleteKeyRingRowId = getArguments().getLong(ARG_DELETE_KEY_RING_ROW_ID);
final int keyType = getArguments().getInt(ARG_KEY_TYPE); final int keyType = getArguments().getInt(ARG_KEY_TYPE);
// TODO: better way to do this? // TODO: better way to do this?

View File

@ -17,6 +17,7 @@
package org.thialfihar.android.apg.ui.widget; package org.thialfihar.android.apg.ui.widget;
import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.R; import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.helper.OtherHelper; import org.thialfihar.android.apg.helper.OtherHelper;
import org.thialfihar.android.apg.helper.PGPHelper; import org.thialfihar.android.apg.helper.PGPHelper;
@ -29,7 +30,6 @@ import android.database.Cursor;
import android.database.DatabaseUtils; import android.database.DatabaseUtils;
import android.database.MergeCursor; import android.database.MergeCursor;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns; import android.provider.BaseColumns;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -44,12 +44,9 @@ public class KeyListAdapter extends CursorTreeAdapter {
protected int mKeyType; protected int mKeyType;
public static int KEY_TYPE_PUBLIC = 0; private static final int CHILD_KEY = 0;
public static int KEY_TYPE_SECRET = 1; private static final int CHILD_USER_ID = 1;
private static final int CHILD_FINGERPRINT = 2;
private static final int KEY = 0;
private static final int USER_ID = 1;
private static final int FINGERPRINT = 2;
public KeyListAdapter(Context context, Cursor groupCursor, int keyType) { public KeyListAdapter(Context context, Cursor groupCursor, int keyType) {
super(groupCursor, context); super(groupCursor, context);
@ -170,18 +167,15 @@ public class KeyListAdapter extends CursorTreeAdapter {
/** /**
* Given the group cursor, we start cursors for a fingerprint, keys, and userIds, which are * Given the group cursor, we start cursors for a fingerprint, keys, and userIds, which are
* merged together and form the child cursor * merged together and build the child cursor
*/ */
@Override @Override
protected Cursor getChildrenCursor(Cursor groupCursor) { protected Cursor getChildrenCursor(Cursor groupCursor) {
// put keyRingRowId into a bundle to have it when querying child cursors final long keyRingRowId = groupCursor.getLong(groupCursor.getColumnIndex(BaseColumns._ID));
final long idGroup = groupCursor.getLong(groupCursor.getColumnIndex(BaseColumns._ID));
Bundle bundle = new Bundle();
bundle.putLong("keyRingRowId", idGroup);
Cursor fingerprintCursor = getChildCursor(bundle, FINGERPRINT); Cursor fingerprintCursor = getChildCursor(keyRingRowId, CHILD_FINGERPRINT);
Cursor keyCursor = getChildCursor(bundle, KEY); Cursor keyCursor = getChildCursor(keyRingRowId, CHILD_KEY);
Cursor userIdCursor = getChildCursor(bundle, USER_ID); Cursor userIdCursor = getChildCursor(keyRingRowId, CHILD_USER_ID);
MergeCursor mergeCursor = new MergeCursor(new Cursor[] { fingerprintCursor, keyCursor, MergeCursor mergeCursor = new MergeCursor(new Cursor[] { fingerprintCursor, keyCursor,
userIdCursor }); userIdCursor });
@ -193,20 +187,19 @@ public class KeyListAdapter extends CursorTreeAdapter {
/** /**
* This builds a cursor for a specific type of children * This builds a cursor for a specific type of children
* *
* @param bundle * @param keyRingRowId
* foreign row id of the keyRing
* @param type * @param type
* @return * @return
*/ */
private Cursor getChildCursor(Bundle bundle, int type) { private Cursor getChildCursor(long keyRingRowId, int type) {
long keyRingRowId = bundle.getLong("keyRingRowId");
Uri uri = null; Uri uri = null;
String[] projection = null; String[] projection = null;
String sortOrder = null; String sortOrder = null;
String selection = null; String selection = null;
switch (type) { switch (type) {
case FINGERPRINT: case CHILD_FINGERPRINT:
projection = new String[] { Keys._ID, Keys.KEY_ID, Keys.IS_MASTER_KEY, Keys.ALGORITHM, projection = new String[] { Keys._ID, Keys.KEY_ID, Keys.IS_MASTER_KEY, Keys.ALGORITHM,
Keys.KEY_SIZE, Keys.CAN_SIGN, Keys.CAN_ENCRYPT, }; Keys.KEY_SIZE, Keys.CAN_SIGN, Keys.CAN_ENCRYPT, };
sortOrder = Keys.RANK + " ASC"; sortOrder = Keys.RANK + " ASC";
@ -214,19 +207,19 @@ public class KeyListAdapter extends CursorTreeAdapter {
// use only master key for fingerprint // use only master key for fingerprint
selection = Keys.IS_MASTER_KEY + " = 1 "; selection = Keys.IS_MASTER_KEY + " = 1 ";
if (mKeyType == KEY_TYPE_PUBLIC) { if (mKeyType == Id.type.public_key) {
uri = Keys.buildPublicKeysUri(String.valueOf(keyRingRowId)); uri = Keys.buildPublicKeysUri(String.valueOf(keyRingRowId));
} else { } else {
uri = Keys.buildSecretKeysUri(String.valueOf(keyRingRowId)); uri = Keys.buildSecretKeysUri(String.valueOf(keyRingRowId));
} }
break; break;
case KEY: case CHILD_KEY:
projection = new String[] { Keys._ID, Keys.KEY_ID, Keys.IS_MASTER_KEY, Keys.ALGORITHM, projection = new String[] { Keys._ID, Keys.KEY_ID, Keys.IS_MASTER_KEY, Keys.ALGORITHM,
Keys.KEY_SIZE, Keys.CAN_SIGN, Keys.CAN_ENCRYPT, }; Keys.KEY_SIZE, Keys.CAN_SIGN, Keys.CAN_ENCRYPT, };
sortOrder = Keys.RANK + " ASC"; sortOrder = Keys.RANK + " ASC";
if (mKeyType == KEY_TYPE_PUBLIC) { if (mKeyType == Id.type.public_key) {
uri = Keys.buildPublicKeysUri(String.valueOf(keyRingRowId)); uri = Keys.buildPublicKeysUri(String.valueOf(keyRingRowId));
} else { } else {
uri = Keys.buildSecretKeysUri(String.valueOf(keyRingRowId)); uri = Keys.buildSecretKeysUri(String.valueOf(keyRingRowId));
@ -234,14 +227,14 @@ public class KeyListAdapter extends CursorTreeAdapter {
break; break;
case USER_ID: case CHILD_USER_ID:
projection = new String[] { UserIds._ID, UserIds.USER_ID, UserIds.RANK, }; projection = new String[] { UserIds._ID, UserIds.USER_ID, UserIds.RANK, };
sortOrder = UserIds.RANK + " ASC"; sortOrder = UserIds.RANK + " ASC";
// not the main user id: // not the main user id
selection = UserIds.RANK + " > 0 "; selection = UserIds.RANK + " > 0 ";
if (mKeyType == KEY_TYPE_PUBLIC) { if (mKeyType == Id.type.public_key) {
uri = UserIds.buildPublicUserIdsUri(String.valueOf(keyRingRowId)); uri = UserIds.buildPublicUserIdsUri(String.valueOf(keyRingRowId));
} else { } else {
uri = UserIds.buildSecretUserIdsUri(String.valueOf(keyRingRowId)); uri = UserIds.buildSecretUserIdsUri(String.valueOf(keyRingRowId));

View File

@ -286,8 +286,8 @@ public class SectionView extends LinearLayout implements OnClickListener, Editor
if (message.arg1 == ApgServiceHandler.MESSAGE_OKAY) { if (message.arg1 == ApgServiceHandler.MESSAGE_OKAY) {
// get new key from data bundle returned from service // get new key from data bundle returned from service
Bundle data = message.getData(); Bundle data = message.getData();
PGPSecretKeyRing newKeyRing = PGPConversionHelper.BytesToPGPSecretKeyRing(data PGPSecretKeyRing newKeyRing = (PGPSecretKeyRing) PGPConversionHelper
.getByteArray(ApgService.RESULT_NEW_KEY)); .BytesToPGPKeyRing(data.getByteArray(ApgService.RESULT_NEW_KEY));
boolean isMasterKey = (mEditors.getChildCount() == 0); boolean isMasterKey = (mEditors.getChildCount() == 0);