From 1a33f4d886ce45596dd7f6dfc3356fe32ec71c1c Mon Sep 17 00:00:00 2001 From: Dominik Date: Thu, 12 Apr 2012 15:23:00 +0200 Subject: intent to create a new key --- org_apg/src/org/apg/Apg.java | 3 +- org_apg/src/org/apg/ui/DecryptActivity.java | 7 + org_apg/src/org/apg/ui/EditKeyActivity.java | 40 ++-- org_apg/src/org/apg/ui/EncryptActivity.java | 18 +- .../org/apg/ui/SelectPublicKeyListActivity.java | 1 + .../src/org/apg/ui/SelectPublicKeyListAdapter.java | 216 --------------------- .../org/apg/ui/SelectSecretKeyListActivity.java | 1 + .../src/org/apg/ui/SelectSecretKeyListAdapter.java | 181 ----------------- .../apg/ui/widget/SelectPublicKeyListAdapter.java | 216 +++++++++++++++++++++ .../apg/ui/widget/SelectSecretKeyListAdapter.java | 181 +++++++++++++++++ org_apg/src/org/apg/ui/widget/UserIdEditor.java | 9 +- 11 files changed, 451 insertions(+), 422 deletions(-) delete mode 100644 org_apg/src/org/apg/ui/SelectPublicKeyListAdapter.java delete mode 100644 org_apg/src/org/apg/ui/SelectSecretKeyListAdapter.java create mode 100644 org_apg/src/org/apg/ui/widget/SelectPublicKeyListAdapter.java create mode 100644 org_apg/src/org/apg/ui/widget/SelectSecretKeyListAdapter.java (limited to 'org_apg/src/org') diff --git a/org_apg/src/org/apg/Apg.java b/org_apg/src/org/apg/Apg.java index e3a2b5eb8..dbe60e65e 100644 --- a/org_apg/src/org/apg/Apg.java +++ b/org_apg/src/org/apg/Apg.java @@ -125,6 +125,7 @@ public class Apg { public static final String GENERATE_SIGNATURE = "org.apg.intent.GENERATE_SIGNATURE"; public static final String EXPORT_KEY_TO_SERVER = "org.apg.intent.EXPORT_KEY_TO_SERVER"; public static final String IMPORT_FROM_QR_CODE = "org.apg.intent.IMPORT_FROM_QR_CODE"; + public static final String EDIT_KEY = "org.apg.intent.EDIT_KEY"; } public static final String EXTRA_TEXT = "text"; @@ -153,7 +154,7 @@ public class Apg { public static final String EXTRA_ASCII_ARMOUR = "asciiArmour"; public static final String EXTRA_BINARY = "binary"; public static final String EXTRA_KEY_SERVERS = "keyServers"; - public static final String EXTRA_EXPECTED_FINGERPRINT = "org.apg.EXPECTED_FINGERPRINT"; + public static final String EXTRA_EXPECTED_FINGERPRINT = "expectedFingerprint"; public static final String AUTHORITY = DataProvider.AUTHORITY; diff --git a/org_apg/src/org/apg/ui/DecryptActivity.java b/org_apg/src/org/apg/ui/DecryptActivity.java index 53df18867..b1726194b 100644 --- a/org_apg/src/org/apg/ui/DecryptActivity.java +++ b/org_apg/src/org/apg/ui/DecryptActivity.java @@ -31,6 +31,7 @@ import org.spongycastle.openpgp.PGPException; import org.spongycastle.openpgp.PGPPublicKeyRing; import org.apg.R; +import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; @@ -249,6 +250,12 @@ public class DecryptActivity extends BaseActivity { Log.d(Constants.TAG, "got extras"); } + // disable home button on actionbar because this activity is run from another app + final ActionBar actionBar = getSupportActionBar(); + actionBar.setDisplayShowTitleEnabled(true); + actionBar.setDisplayHomeAsUpEnabled(false); + actionBar.setHomeButtonEnabled(false); + mData = extras.getByteArray(Apg.EXTRA_DATA); String textData = null; if (mData == null) { diff --git a/org_apg/src/org/apg/ui/EditKeyActivity.java b/org_apg/src/org/apg/ui/EditKeyActivity.java index 8f7a642f9..b0e59a7bf 100644 --- a/org_apg/src/org/apg/ui/EditKeyActivity.java +++ b/org_apg/src/org/apg/ui/EditKeyActivity.java @@ -28,6 +28,7 @@ import org.spongycastle.openpgp.PGPSecretKey; import org.spongycastle.openpgp.PGPSecretKeyRing; import org.apg.R; +import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; @@ -36,6 +37,7 @@ import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.os.Message; import android.view.LayoutInflater; @@ -53,6 +55,7 @@ import java.security.SignatureException; import java.util.Vector; public class EditKeyActivity extends BaseActivity { + private Intent mIntent = null; private PGPSecretKeyRing mKeyRing = null; @@ -66,12 +69,10 @@ public class EditKeyActivity extends BaseActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { - menu.add(1, Id.menu.option.cancel, 0, R.string.btn_doNotSave) - // .setIcon(R.drawable.ic_menu_search_holo_light) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); - menu.add(1, Id.menu.option.save, 1, R.string.btn_save) - // .setIcon(R.drawable.ic_suggestions_add) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + menu.add(1, Id.menu.option.cancel, 0, R.string.btn_doNotSave).setShowAsAction( + MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + menu.add(1, Id.menu.option.save, 1, R.string.btn_save).setShowAsAction( + MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); return true; } @@ -128,6 +129,26 @@ public class EditKeyActivity extends BaseActivity { } } + // Catch Intents opened from other apps + mIntent = getIntent(); + if (Apg.Intent.EDIT_KEY.equals(mIntent.getAction())) { + Bundle extras = mIntent.getExtras(); + if (extras == null) { + extras = new Bundle(); + } + + // disable home button on actionbar because this activity is run from another app + final ActionBar actionBar = getSupportActionBar(); + actionBar.setDisplayShowTitleEnabled(true); + actionBar.setDisplayHomeAsUpEnabled(false); + actionBar.setHomeButtonEnabled(false); + + // if userId is given, prefill the fields + if (extras.containsKey(Apg.EXTRA_USER_IDS)) { + userIds.add(extras.getString(Apg.EXTRA_USER_IDS)); + } + } + mChangePassPhrase = (Button) findViewById(R.id.btn_change_pass_phrase); mChangePassPhrase.setOnClickListener(new OnClickListener() { public void onClick(View v) { @@ -135,12 +156,7 @@ public class EditKeyActivity extends BaseActivity { } }); - // mSaveButton = (Button) findViewById(R.id.btn_save); - // mDiscardButton = (Button) findViewById(R.id.btn_discard); - - // mSaveButton.setOnClickListener(this); - // mDiscardButton.setOnClickListener(this); - + // Build layout based on given userIds and keys LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout container = (LinearLayout) findViewById(R.id.container); diff --git a/org_apg/src/org/apg/ui/EncryptActivity.java b/org_apg/src/org/apg/ui/EncryptActivity.java index a4194d778..c437ebe27 100644 --- a/org_apg/src/org/apg/ui/EncryptActivity.java +++ b/org_apg/src/org/apg/ui/EncryptActivity.java @@ -33,6 +33,7 @@ import org.spongycastle.openpgp.PGPSecretKey; import org.spongycastle.openpgp.PGPSecretKeyRing; import org.apg.R; +import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; @@ -125,15 +126,12 @@ public class EncryptActivity extends BaseActivity { if (mEncryptToClipboardEnabled) { menu.add(1, Id.menu.option.encrypt_to_clipboard, 0, mEncryptToClipboardString) - // .setIcon(R.drawable.ic_menu_encrypt) .setShowAsAction( MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); } if (mEncryptEnabled) { - menu.add(1, Id.menu.option.encrypt, 1, mEncryptString) - // .setIcon(R.drawable.ic_menu_decrypt) - .setShowAsAction( - MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + menu.add(1, Id.menu.option.encrypt, 1, mEncryptString).setShowAsAction( + MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); } return true; @@ -233,8 +231,6 @@ public class EncryptActivity extends BaseActivity { mMessage = (EditText) findViewById(R.id.message); mSelectKeysButton = (Button) findViewById(R.id.btn_selectEncryptKeys); - // mEncryptButton = (Button) findViewById(R.id.btn_encrypt); - // mEncryptToClipboardButton = (Button) findViewById(R.id.btn_encryptToClipboard); mSign = (CheckBox) findViewById(R.id.sign); mMainUserId = (TextView) findViewById(R.id.mainUserId); mMainUserIdRest = (TextView) findViewById(R.id.mainUserIdRest); @@ -317,6 +313,12 @@ public class EncryptActivity extends BaseActivity { extras = new Bundle(); } + // disable home button on actionbar because this activity is run from another app + final ActionBar actionBar = getSupportActionBar(); + actionBar.setDisplayShowTitleEnabled(true); + actionBar.setDisplayHomeAsUpEnabled(false); + actionBar.setHomeButtonEnabled(false); + if (Apg.Intent.ENCRYPT_AND_RETURN.equals(mIntent.getAction()) || Apg.Intent.GENERATE_SIGNATURE.equals(mIntent.getAction())) { mReturnResult = true; @@ -483,8 +485,6 @@ public class EncryptActivity extends BaseActivity { mEncryptEnabled = true; mEncryptToClipboardEnabled = false; - // mEncryptToClipboardButton.setVisibility(View.INVISIBLE); - // mEncryptButton.setText(R.string.btn_encrypt); break; } diff --git a/org_apg/src/org/apg/ui/SelectPublicKeyListActivity.java b/org_apg/src/org/apg/ui/SelectPublicKeyListActivity.java index 0b6d6c43a..e2dcc8d4d 100644 --- a/org_apg/src/org/apg/ui/SelectPublicKeyListActivity.java +++ b/org_apg/src/org/apg/ui/SelectPublicKeyListActivity.java @@ -21,6 +21,7 @@ import java.util.Vector; import org.apg.Apg; import org.apg.Id; import org.apg.R; +import org.apg.ui.widget.SelectPublicKeyListAdapter; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.view.Menu; diff --git a/org_apg/src/org/apg/ui/SelectPublicKeyListAdapter.java b/org_apg/src/org/apg/ui/SelectPublicKeyListAdapter.java deleted file mode 100644 index 93844601b..000000000 --- a/org_apg/src/org/apg/ui/SelectPublicKeyListAdapter.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright (C) 2010 Thialfihar - * - * 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.apg.ui; - -import java.util.Date; - -import org.apg.Apg; -import org.apg.Id; -import org.apg.provider.KeyRings; -import org.apg.provider.Keys; -import org.apg.provider.UserIds; -import org.apg.R; - -import android.app.Activity; -import android.content.Context; -import android.database.Cursor; -import android.database.DatabaseUtils; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteQueryBuilder; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; -import android.widget.CheckBox; -import android.widget.ListView; -import android.widget.TextView; - -public class SelectPublicKeyListAdapter extends BaseAdapter { - protected LayoutInflater mInflater; - protected ListView mParent; - protected SQLiteDatabase mDatabase; - protected Cursor mCursor; - protected String mSearchString; - protected Activity mActivity; - - public SelectPublicKeyListAdapter(Activity activity, ListView parent, String searchString, - long selectedKeyIds[]) { - mSearchString = searchString; - - mActivity = activity; - mParent = parent; - mDatabase = Apg.getDatabase().db(); - mInflater = (LayoutInflater) parent.getContext().getSystemService( - Context.LAYOUT_INFLATER_SERVICE); - long now = new Date().getTime() / 1000; - 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') "); - - String inIdList = null; - - if (selectedKeyIds != null && selectedKeyIds.length > 0) { - inIdList = KeyRings.TABLE_NAME + "." + KeyRings.MASTER_KEY_ID + " IN ("; - for (int i = 0; i < selectedKeyIds.length; ++i) { - if (i != 0) { - inIdList += ", "; - } - inIdList += DatabaseUtils.sqlEscapeString("" + selectedKeyIds[i]); - } - inIdList += ")"; - } - - 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("))"); - - if (inIdList != null) { - qb.appendWhere(" OR (" + inIdList + ")"); - } - } - - String orderBy = UserIds.TABLE_NAME + "." + UserIds.USER_ID + " ASC"; - if (inIdList != null) { - orderBy = inIdList + " DESC, " + orderBy; - } - - 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 - "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " - + "tmp." + Keys.KEY_RING_ID + " = " + KeyRings.TABLE_NAME + "." - + KeyRings._ID + " AND " + "tmp." + Keys.IS_REVOKED + " = '0' AND " - + "tmp." + Keys.CAN_ENCRYPT + " = '1')", // 3 - "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " - + "tmp." + Keys.KEY_RING_ID + " = " + KeyRings.TABLE_NAME + "." - + KeyRings._ID + " AND " + "tmp." + Keys.IS_REVOKED + " = '0' AND " - + "tmp." + Keys.CAN_ENCRYPT + " = '1' AND " + "tmp." + Keys.CREATION - + " <= '" + now + "' AND " + "(tmp." + Keys.EXPIRY + " IS NULL OR " - + "tmp." + Keys.EXPIRY + " >= '" + now + "'))", // 4 - }, KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?", new String[] { "" - + Id.database.type_public }, null, null, orderBy); - - activity.startManagingCursor(mCursor); - } - - public void cleanup() { - if (mCursor != null) { - mActivity.stopManagingCursor(mCursor); - mCursor.close(); - } - } - - @Override - public boolean isEnabled(int position) { - mCursor.moveToPosition(position); - return mCursor.getInt(4) > 0; // valid CAN_ENCRYPT - } - - @Override - public boolean hasStableIds() { - return true; - } - - public int getCount() { - return mCursor.getCount(); - } - - public Object getItem(int position) { - mCursor.moveToPosition(position); - return mCursor.getString(2); // USER_ID - } - - public long getItemId(int position) { - mCursor.moveToPosition(position); - return mCursor.getLong(1); // MASTER_KEY_ID - } - - public View getView(int position, View convertView, ViewGroup parent) { - mCursor.moveToPosition(position); - - View view = mInflater.inflate(R.layout.select_public_key_item, null); - boolean enabled = isEnabled(position); - - TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId); - mainUserId.setText(R.string.unknownUserId); - TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest); - mainUserIdRest.setText(""); - TextView keyId = (TextView) view.findViewById(R.id.keyId); - keyId.setText(R.string.noKey); - TextView status = (TextView) view.findViewById(R.id.status); - status.setText(R.string.unknownStatus); - - 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); - } - - long masterKeyId = mCursor.getLong(1); // MASTER_KEY_ID - keyId.setText(Apg.getSmallFingerPrint(masterKeyId)); - - if (mainUserIdRest.getText().length() == 0) { - mainUserIdRest.setVisibility(View.GONE); - } - - if (enabled) { - status.setText(R.string.canEncrypt); - } else { - if (mCursor.getInt(3) > 0) { - // has some CAN_ENCRYPT keys, but col(4) = 0, so must be revoked or expired - status.setText(R.string.expired); - } else { - status.setText(R.string.noKey); - } - } - - status.setText(status.getText() + " "); - - CheckBox selected = (CheckBox) view.findViewById(R.id.selected); - - if (!enabled) { - mParent.setItemChecked(position, false); - } - - selected.setChecked(mParent.isItemChecked(position)); - - view.setEnabled(enabled); - mainUserId.setEnabled(enabled); - mainUserIdRest.setEnabled(enabled); - keyId.setEnabled(enabled); - selected.setEnabled(enabled); - status.setEnabled(enabled); - - return view; - } -} diff --git a/org_apg/src/org/apg/ui/SelectSecretKeyListActivity.java b/org_apg/src/org/apg/ui/SelectSecretKeyListActivity.java index 774ce6e41..dadc6f5bf 100644 --- a/org_apg/src/org/apg/ui/SelectSecretKeyListActivity.java +++ b/org_apg/src/org/apg/ui/SelectSecretKeyListActivity.java @@ -19,6 +19,7 @@ package org.apg.ui; import org.apg.Apg; import org.apg.Id; import org.apg.R; +import org.apg.ui.widget.SelectSecretKeyListAdapter; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.view.Menu; diff --git a/org_apg/src/org/apg/ui/SelectSecretKeyListAdapter.java b/org_apg/src/org/apg/ui/SelectSecretKeyListAdapter.java deleted file mode 100644 index 3c0b803eb..000000000 --- a/org_apg/src/org/apg/ui/SelectSecretKeyListAdapter.java +++ /dev/null @@ -1,181 +0,0 @@ -/* - * 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.apg.ui; - -import java.util.Date; - -import org.apg.Apg; -import org.apg.Id; -import org.apg.provider.KeyRings; -import org.apg.provider.Keys; -import org.apg.provider.UserIds; -import org.apg.R; - -import android.app.Activity; -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteQueryBuilder; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; -import android.widget.ListView; -import android.widget.TextView; - -public class SelectSecretKeyListAdapter extends BaseAdapter { - protected LayoutInflater mInflater; - protected ListView mParent; - protected SQLiteDatabase mDatabase; - protected Cursor mCursor; - protected String mSearchString; - protected Activity mActivity; - - public SelectSecretKeyListAdapter(Activity activity, ListView parent, String searchString) { - mSearchString = searchString; - - mActivity = activity; - mParent = parent; - mDatabase = Apg.getDatabase().db(); - mInflater = (LayoutInflater) parent.getContext().getSystemService( - Context.LAYOUT_INFLATER_SERVICE); - long now = new Date().getTime() / 1000; - 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 - "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " - + "tmp." + Keys.KEY_RING_ID + " = " + KeyRings.TABLE_NAME + "." - + KeyRings._ID + " AND " + "tmp." + Keys.IS_REVOKED + " = '0' AND " - + "tmp." + Keys.CAN_SIGN + " = '1')", // 3, - "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " - + "tmp." + Keys.KEY_RING_ID + " = " + KeyRings.TABLE_NAME + "." - + KeyRings._ID + " AND " + "tmp." + Keys.IS_REVOKED + " = '0' AND " - + "tmp." + Keys.CAN_SIGN + " = '1' AND " + "tmp." + Keys.CREATION + " <= '" - + now + "' AND " + "(tmp." + Keys.EXPIRY + " IS NULL OR " + "tmp." - + Keys.EXPIRY + " >= '" + now + "'))", // 4 - }, KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?", new String[] { "" - + Id.database.type_secret }, null, null, UserIds.TABLE_NAME + "." + UserIds.USER_ID - + " ASC"); - - activity.startManagingCursor(mCursor); - } - - public void cleanup() { - if (mCursor != null) { - mActivity.stopManagingCursor(mCursor); - mCursor.close(); - } - } - - @Override - public boolean isEnabled(int position) { - mCursor.moveToPosition(position); - return mCursor.getInt(4) > 0; // valid CAN_SIGN - } - - @Override - public boolean hasStableIds() { - return true; - } - - public int getCount() { - return mCursor.getCount(); - } - - public Object getItem(int position) { - mCursor.moveToPosition(position); - return mCursor.getString(2); // USER_ID - } - - public long getItemId(int position) { - mCursor.moveToPosition(position); - return mCursor.getLong(1); // MASTER_KEY_ID - } - - public View getView(int position, View convertView, ViewGroup parent) { - mCursor.moveToPosition(position); - - View view = mInflater.inflate(R.layout.select_secret_key_item, null); - boolean enabled = isEnabled(position); - - TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId); - mainUserId.setText(R.string.unknownUserId); - TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest); - mainUserIdRest.setText(""); - TextView keyId = (TextView) view.findViewById(R.id.keyId); - keyId.setText(R.string.noKey); - TextView status = (TextView) view.findViewById(R.id.status); - status.setText(R.string.unknownStatus); - - 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); - } - - long masterKeyId = mCursor.getLong(1); // MASTER_KEY_ID - keyId.setText(Apg.getSmallFingerPrint(masterKeyId)); - - if (mainUserIdRest.getText().length() == 0) { - mainUserIdRest.setVisibility(View.GONE); - } - - if (enabled) { - status.setText(R.string.canSign); - } else { - if (mCursor.getInt(3) > 0) { - // has some CAN_SIGN keys, but col(4) = 0, so must be revoked or expired - status.setText(R.string.expired); - } else { - status.setText(R.string.noKey); - } - } - - status.setText(status.getText() + " "); - - view.setEnabled(enabled); - mainUserId.setEnabled(enabled); - mainUserIdRest.setEnabled(enabled); - keyId.setEnabled(enabled); - status.setEnabled(enabled); - - return view; - } -} \ No newline at end of file diff --git a/org_apg/src/org/apg/ui/widget/SelectPublicKeyListAdapter.java b/org_apg/src/org/apg/ui/widget/SelectPublicKeyListAdapter.java new file mode 100644 index 000000000..b98a76661 --- /dev/null +++ b/org_apg/src/org/apg/ui/widget/SelectPublicKeyListAdapter.java @@ -0,0 +1,216 @@ +/* + * Copyright (C) 2010 Thialfihar + * + * 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.apg.ui.widget; + +import java.util.Date; + +import org.apg.Apg; +import org.apg.Id; +import org.apg.provider.KeyRings; +import org.apg.provider.Keys; +import org.apg.provider.UserIds; +import org.apg.R; + +import android.app.Activity; +import android.content.Context; +import android.database.Cursor; +import android.database.DatabaseUtils; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteQueryBuilder; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.CheckBox; +import android.widget.ListView; +import android.widget.TextView; + +public class SelectPublicKeyListAdapter extends BaseAdapter { + protected LayoutInflater mInflater; + protected ListView mParent; + protected SQLiteDatabase mDatabase; + protected Cursor mCursor; + protected String mSearchString; + protected Activity mActivity; + + public SelectPublicKeyListAdapter(Activity activity, ListView parent, String searchString, + long selectedKeyIds[]) { + mSearchString = searchString; + + mActivity = activity; + mParent = parent; + mDatabase = Apg.getDatabase().db(); + mInflater = (LayoutInflater) parent.getContext().getSystemService( + Context.LAYOUT_INFLATER_SERVICE); + long now = new Date().getTime() / 1000; + 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') "); + + String inIdList = null; + + if (selectedKeyIds != null && selectedKeyIds.length > 0) { + inIdList = KeyRings.TABLE_NAME + "." + KeyRings.MASTER_KEY_ID + " IN ("; + for (int i = 0; i < selectedKeyIds.length; ++i) { + if (i != 0) { + inIdList += ", "; + } + inIdList += DatabaseUtils.sqlEscapeString("" + selectedKeyIds[i]); + } + inIdList += ")"; + } + + 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("))"); + + if (inIdList != null) { + qb.appendWhere(" OR (" + inIdList + ")"); + } + } + + String orderBy = UserIds.TABLE_NAME + "." + UserIds.USER_ID + " ASC"; + if (inIdList != null) { + orderBy = inIdList + " DESC, " + orderBy; + } + + 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 + "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " + + "tmp." + Keys.KEY_RING_ID + " = " + KeyRings.TABLE_NAME + "." + + KeyRings._ID + " AND " + "tmp." + Keys.IS_REVOKED + " = '0' AND " + + "tmp." + Keys.CAN_ENCRYPT + " = '1')", // 3 + "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " + + "tmp." + Keys.KEY_RING_ID + " = " + KeyRings.TABLE_NAME + "." + + KeyRings._ID + " AND " + "tmp." + Keys.IS_REVOKED + " = '0' AND " + + "tmp." + Keys.CAN_ENCRYPT + " = '1' AND " + "tmp." + Keys.CREATION + + " <= '" + now + "' AND " + "(tmp." + Keys.EXPIRY + " IS NULL OR " + + "tmp." + Keys.EXPIRY + " >= '" + now + "'))", // 4 + }, KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?", new String[] { "" + + Id.database.type_public }, null, null, orderBy); + + activity.startManagingCursor(mCursor); + } + + public void cleanup() { + if (mCursor != null) { + mActivity.stopManagingCursor(mCursor); + mCursor.close(); + } + } + + @Override + public boolean isEnabled(int position) { + mCursor.moveToPosition(position); + return mCursor.getInt(4) > 0; // valid CAN_ENCRYPT + } + + @Override + public boolean hasStableIds() { + return true; + } + + public int getCount() { + return mCursor.getCount(); + } + + public Object getItem(int position) { + mCursor.moveToPosition(position); + return mCursor.getString(2); // USER_ID + } + + public long getItemId(int position) { + mCursor.moveToPosition(position); + return mCursor.getLong(1); // MASTER_KEY_ID + } + + public View getView(int position, View convertView, ViewGroup parent) { + mCursor.moveToPosition(position); + + View view = mInflater.inflate(R.layout.select_public_key_item, null); + boolean enabled = isEnabled(position); + + TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId); + mainUserId.setText(R.string.unknownUserId); + TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest); + mainUserIdRest.setText(""); + TextView keyId = (TextView) view.findViewById(R.id.keyId); + keyId.setText(R.string.noKey); + TextView status = (TextView) view.findViewById(R.id.status); + status.setText(R.string.unknownStatus); + + 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); + } + + long masterKeyId = mCursor.getLong(1); // MASTER_KEY_ID + keyId.setText(Apg.getSmallFingerPrint(masterKeyId)); + + if (mainUserIdRest.getText().length() == 0) { + mainUserIdRest.setVisibility(View.GONE); + } + + if (enabled) { + status.setText(R.string.canEncrypt); + } else { + if (mCursor.getInt(3) > 0) { + // has some CAN_ENCRYPT keys, but col(4) = 0, so must be revoked or expired + status.setText(R.string.expired); + } else { + status.setText(R.string.noKey); + } + } + + status.setText(status.getText() + " "); + + CheckBox selected = (CheckBox) view.findViewById(R.id.selected); + + if (!enabled) { + mParent.setItemChecked(position, false); + } + + selected.setChecked(mParent.isItemChecked(position)); + + view.setEnabled(enabled); + mainUserId.setEnabled(enabled); + mainUserIdRest.setEnabled(enabled); + keyId.setEnabled(enabled); + selected.setEnabled(enabled); + status.setEnabled(enabled); + + return view; + } +} diff --git a/org_apg/src/org/apg/ui/widget/SelectSecretKeyListAdapter.java b/org_apg/src/org/apg/ui/widget/SelectSecretKeyListAdapter.java new file mode 100644 index 000000000..1b6d115b7 --- /dev/null +++ b/org_apg/src/org/apg/ui/widget/SelectSecretKeyListAdapter.java @@ -0,0 +1,181 @@ +/* + * 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.apg.ui.widget; + +import java.util.Date; + +import org.apg.Apg; +import org.apg.Id; +import org.apg.provider.KeyRings; +import org.apg.provider.Keys; +import org.apg.provider.UserIds; +import org.apg.R; + +import android.app.Activity; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteQueryBuilder; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListView; +import android.widget.TextView; + +public class SelectSecretKeyListAdapter extends BaseAdapter { + protected LayoutInflater mInflater; + protected ListView mParent; + protected SQLiteDatabase mDatabase; + protected Cursor mCursor; + protected String mSearchString; + protected Activity mActivity; + + public SelectSecretKeyListAdapter(Activity activity, ListView parent, String searchString) { + mSearchString = searchString; + + mActivity = activity; + mParent = parent; + mDatabase = Apg.getDatabase().db(); + mInflater = (LayoutInflater) parent.getContext().getSystemService( + Context.LAYOUT_INFLATER_SERVICE); + long now = new Date().getTime() / 1000; + 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 + "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " + + "tmp." + Keys.KEY_RING_ID + " = " + KeyRings.TABLE_NAME + "." + + KeyRings._ID + " AND " + "tmp." + Keys.IS_REVOKED + " = '0' AND " + + "tmp." + Keys.CAN_SIGN + " = '1')", // 3, + "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " + + "tmp." + Keys.KEY_RING_ID + " = " + KeyRings.TABLE_NAME + "." + + KeyRings._ID + " AND " + "tmp." + Keys.IS_REVOKED + " = '0' AND " + + "tmp." + Keys.CAN_SIGN + " = '1' AND " + "tmp." + Keys.CREATION + " <= '" + + now + "' AND " + "(tmp." + Keys.EXPIRY + " IS NULL OR " + "tmp." + + Keys.EXPIRY + " >= '" + now + "'))", // 4 + }, KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?", new String[] { "" + + Id.database.type_secret }, null, null, UserIds.TABLE_NAME + "." + UserIds.USER_ID + + " ASC"); + + activity.startManagingCursor(mCursor); + } + + public void cleanup() { + if (mCursor != null) { + mActivity.stopManagingCursor(mCursor); + mCursor.close(); + } + } + + @Override + public boolean isEnabled(int position) { + mCursor.moveToPosition(position); + return mCursor.getInt(4) > 0; // valid CAN_SIGN + } + + @Override + public boolean hasStableIds() { + return true; + } + + public int getCount() { + return mCursor.getCount(); + } + + public Object getItem(int position) { + mCursor.moveToPosition(position); + return mCursor.getString(2); // USER_ID + } + + public long getItemId(int position) { + mCursor.moveToPosition(position); + return mCursor.getLong(1); // MASTER_KEY_ID + } + + public View getView(int position, View convertView, ViewGroup parent) { + mCursor.moveToPosition(position); + + View view = mInflater.inflate(R.layout.select_secret_key_item, null); + boolean enabled = isEnabled(position); + + TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId); + mainUserId.setText(R.string.unknownUserId); + TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest); + mainUserIdRest.setText(""); + TextView keyId = (TextView) view.findViewById(R.id.keyId); + keyId.setText(R.string.noKey); + TextView status = (TextView) view.findViewById(R.id.status); + status.setText(R.string.unknownStatus); + + 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); + } + + long masterKeyId = mCursor.getLong(1); // MASTER_KEY_ID + keyId.setText(Apg.getSmallFingerPrint(masterKeyId)); + + if (mainUserIdRest.getText().length() == 0) { + mainUserIdRest.setVisibility(View.GONE); + } + + if (enabled) { + status.setText(R.string.canSign); + } else { + if (mCursor.getInt(3) > 0) { + // has some CAN_SIGN keys, but col(4) = 0, so must be revoked or expired + status.setText(R.string.expired); + } else { + status.setText(R.string.noKey); + } + } + + status.setText(status.getText() + " "); + + view.setEnabled(enabled); + mainUserId.setEnabled(enabled); + mainUserIdRest.setEnabled(enabled); + keyId.setEnabled(enabled); + status.setEnabled(enabled); + + return view; + } +} \ No newline at end of file diff --git a/org_apg/src/org/apg/ui/widget/UserIdEditor.java b/org_apg/src/org/apg/ui/widget/UserIdEditor.java index ebbc608f0..ce5594c8e 100644 --- a/org_apg/src/org/apg/ui/widget/UserIdEditor.java +++ b/org_apg/src/org/apg/ui/widget/UserIdEditor.java @@ -40,9 +40,12 @@ public class UserIdEditor extends LinearLayout implements Editor, OnClickListene private EditText mEmail; private EditText mComment; - private static final Pattern EMAIL_PATTERN = Pattern.compile( - "^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+[.]([a-zA-Z])+([a-zA-Z])+", - Pattern.CASE_INSENSITIVE); + // see http://www.regular-expressions.info/email.html + // RFC 2822 if we omit the syntax using double quotes and square brackets + private static final Pattern EMAIL_PATTERN = Pattern + .compile( + "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", + Pattern.CASE_INSENSITIVE); public static class NoNameException extends Exception { static final long serialVersionUID = 0xf812773343L; -- cgit v1.2.3