select public key refactored

This commit is contained in:
Dominik 2012-11-01 18:50:41 +01:00
parent 182524bb96
commit f50d71559d
14 changed files with 488 additions and 524 deletions

View File

@ -173,7 +173,7 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity <activity
android:name=".ui.SelectPublicKeyListActivity" android:name=".ui.SelectPublicKeyActivity"
android:configChanges="orientation|screenSize|keyboardHidden|keyboard" android:configChanges="orientation|screenSize|keyboardHidden|keyboard"
android:label="@string/title_selectRecipients" android:label="@string/title_selectRecipients"
android:launchMode="singleTop" android:launchMode="singleTop"

View File

@ -1,45 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="@+id/layout_filter"
layout="@layout/filter_info" />
<ExpandableListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:isScrollContainer="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_weight="1"
android:background="@drawable/abs__ab_bottom_solid_light_holo"
android:paddingBottom="3dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:text="@string/listInformation" />
</LinearLayout>

View File

@ -1,13 +1,25 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_centerHorizontal="true" > android:orientation="vertical" >
<fragment <fragment
android:id="@+id/public_key_list_fragment" android:id="@+id/key_list_public_fragment"
android:name="org.thialfihar.android.apg.ui.KeyListPublicFragment" android:name="org.thialfihar.android.apg.ui.KeyListPublicFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent"
android:layout_weight="1" />
</RelativeLayout> <TextView
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_weight="1"
android:background="@drawable/abs__ab_bottom_solid_light_holo"
android:paddingBottom="3dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:text="@string/listInformation" />
</LinearLayout>

View File

@ -1,13 +1,25 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_centerHorizontal="true" > android:orientation="vertical" >
<fragment <fragment
android:id="@+id/secret_key_list_fragment" android:id="@+id/key_list_secret_fragment"
android:name="org.thialfihar.android.apg.ui.KeyListSecretFragment" android:name="org.thialfihar.android.apg.ui.KeyListSecretFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent"
android:layout_weight="1" />
</RelativeLayout> <TextView
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_weight="1"
android:background="@drawable/abs__ab_bottom_solid_light_holo"
android:paddingBottom="3dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:text="@string/listInformation" />
</LinearLayout>

View File

@ -1,35 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:orientation="vertical" >
<include
android:id="@+id/layout_filter"
layout="@layout/filter_info" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:choiceMode="multipleChoice" />
</LinearLayout>

View File

@ -6,7 +6,7 @@
<fragment <fragment
android:id="@+id/select_public_key_fragment" android:id="@+id/select_public_key_fragment"
android:name="org.thialfihar.android.apg.ui.SelectPublicKeyListFragment" android:name="org.thialfihar.android.apg.ui.SelectPublicKeyFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />

View File

@ -46,7 +46,7 @@
<TextView <TextView
android:id="@+id/mainUserIdRest" android:id="@+id/mainUserIdRest"
android:text="&lt;user@somewhere.com&gt;" android:text="&lt;user@example.com&gt;"
android:textAppearance="?android:attr/textAppearanceSmall" android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/>

View File

@ -441,7 +441,8 @@ public class EncryptActivity extends SherlockFragmentActivity {
long signatureKeyId = extras.getLong(EXTRA_SIGNATURE_KEY_ID); long signatureKeyId = extras.getLong(EXTRA_SIGNATURE_KEY_ID);
long encryptionKeyIds[] = extras.getLongArray(EXTRA_ENCRYPTION_KEY_IDS); long encryptionKeyIds[] = extras.getLongArray(EXTRA_ENCRYPTION_KEY_IDS);
if (signatureKeyId != 0) { if (signatureKeyId != 0) {
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId(this, signatureKeyId); PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId(this,
signatureKeyId);
PGPSecretKey masterKey = null; PGPSecretKey masterKey = null;
if (keyRing != null) { if (keyRing != null) {
masterKey = PGPHelper.getMasterKey(keyRing); masterKey = PGPHelper.getMasterKey(keyRing);
@ -958,7 +959,8 @@ public class EncryptActivity extends SherlockFragmentActivity {
} else { } else {
String uid = getResources().getString(R.string.unknownUserId); String uid = getResources().getString(R.string.unknownUserId);
String uidExtra = ""; String uidExtra = "";
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId(this, getSecretKeyId()); PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId(this,
getSecretKeyId());
if (keyRing != null) { if (keyRing != null) {
PGPSecretKey key = PGPHelper.getMasterKey(keyRing); PGPSecretKey key = PGPHelper.getMasterKey(keyRing);
if (key != null) { if (key != null) {
@ -979,7 +981,7 @@ public class EncryptActivity extends SherlockFragmentActivity {
} }
private void selectPublicKeys() { private void selectPublicKeys() {
Intent intent = new Intent(this, SelectPublicKeyListActivity.class); Intent intent = new Intent(this, SelectPublicKeyActivity.class);
Vector<Long> keyIds = new Vector<Long>(); Vector<Long> keyIds = new Vector<Long>();
if (getSecretKeyId() != 0) { if (getSecretKeyId() != 0) {
keyIds.add(getSecretKeyId()); keyIds.add(getSecretKeyId());
@ -996,7 +998,7 @@ public class EncryptActivity extends SherlockFragmentActivity {
initialKeyIds[i] = keyIds.get(i); initialKeyIds[i] = keyIds.get(i);
} }
} }
intent.putExtra(SelectPublicKeyListActivity.RESULT_EXTRA_SELECTION, initialKeyIds); intent.putExtra(SelectPublicKeyActivity.RESULT_EXTRA_MASTER_KEY_IDS, initialKeyIds);
startActivityForResult(intent, Id.request.public_keys); startActivityForResult(intent, Id.request.public_keys);
} }
@ -1040,7 +1042,7 @@ public class EncryptActivity extends SherlockFragmentActivity {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras(); Bundle bundle = data.getExtras();
mEncryptionKeyIds = bundle mEncryptionKeyIds = bundle
.getLongArray(SelectPublicKeyListActivity.RESULT_EXTRA_SELECTION); .getLongArray(SelectPublicKeyActivity.RESULT_EXTRA_MASTER_KEY_IDS);
} }
updateView(); updateView();
break; break;

View File

@ -0,0 +1,177 @@
/*
* 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.R;
import org.thialfihar.android.apg.helper.OtherHelper;
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.MenuItem;
import android.content.Intent;
import android.os.Bundle;
public class SelectPublicKeyActivity extends SherlockFragmentActivity {
// Not used in sourcode, but listed in AndroidManifest!
public static final String ACTION_SELECT_PUBLIC_KEYS = Constants.INTENT_PREFIX
+ "SELECT_PUBLIC_KEYS";
public static final String RESULT_EXTRA_MASTER_KEY_IDS = "masterKeyIds";
public static final String RESULT_EXTRA_USER_IDS = "userIds";
SelectPublicKeyFragment mSelectFragment;
long selectedKeyIds[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_public_key_activity);
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
mSelectFragment = (SelectPublicKeyFragment) getSupportFragmentManager().findFragmentById(
R.id.select_public_key_fragment);
//
// 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);
}
private 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;
// }
// }
OtherHelper.logDebugBundle(intent.getExtras(), "intent extras");
selectedKeyIds = intent.getLongArrayExtra(RESULT_EXTRA_MASTER_KEY_IDS);
// 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 SelectPublicKeyListAdapter(this, mList, searchString, selectedKeyIds);
// mList.setAdapter(mListAdapter);
}
/**
* returns preselected key ids, this is used in the fragment
*
* @return
*/
public long[] getSelectedMasterKeyIds() {
return selectedKeyIds;
}
private void cancelClicked() {
setResult(RESULT_CANCELED, null);
finish();
}
private void okClicked() {
Intent data = new Intent();
data.putExtra(RESULT_EXTRA_MASTER_KEY_IDS, mSelectFragment.getSelectedMasterKeyIds());
data.putExtra(RESULT_EXTRA_USER_IDS, mSelectFragment.getSelectedUserIds());
long[] keys = mSelectFragment.getSelectedMasterKeyIds();
for (int i = 0; i < keys.length; i++) {
Log.d(Constants.TAG, "" + i + ": " + keys[i]);
}
OtherHelper.logDebugBundle(data.getExtras(), "result extras");
setResult(RESULT_OK, data);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, Id.menu.option.search, 0, R.string.menu_search).setIcon(
android.R.drawable.ic_menu_search);
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.okay, 1, R.string.btn_okay).setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
/**
* Menu Options
*/
@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.okay:
okClicked();
return true;
case Id.menu.option.cancel:
cancelClicked();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

View File

@ -0,0 +1,228 @@
/*
* 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 java.util.Date;
import java.util.Vector;
import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.provider.ApgContract.KeyRings;
import org.thialfihar.android.apg.provider.ApgContract.Keys;
import org.thialfihar.android.apg.provider.ApgContract.UserIds;
import org.thialfihar.android.apg.provider.ApgDatabase.Tables;
import org.thialfihar.android.apg.ui.widget.SelectPublicKeyCursorAdapter;
import com.actionbarsherlock.app.SherlockListFragment;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.app.LoaderManager;
import android.widget.ListView;
public class SelectPublicKeyFragment extends SherlockListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private SelectPublicKeyActivity mActivity;
private SelectPublicKeyCursorAdapter mAdapter;
private ListView mListView;
private long mSelectedMasterKeyIds[];
public final static String ROW_AVAILABLE = "available";
public final static String ROW_VALID = "valid";
/**
* Define Adapter and Loader on create of Activity
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mActivity = (SelectPublicKeyActivity) getSherlockActivity();
mListView = getListView();
// get selected master key ids, which are given to activity by intent
mSelectedMasterKeyIds = mActivity.getSelectedMasterKeyIds();
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// register long press context menu
registerForContextMenu(mListView);
// Give some text to display if there is no data. In a real
// application this would come from a resource.
setEmptyText(getString(R.string.listEmpty));
// We have a menu item to show in action bar.
setHasOptionsMenu(true);
mAdapter = new SelectPublicKeyCursorAdapter(mActivity, mListView, null);
setListAdapter(mAdapter);
// Start out with a progress indicator.
setListShown(false);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
private void preselectMasterKeyIds(long[] masterKeyIds) {
if (masterKeyIds != null) {
for (int i = 0; i < mListView.getCount(); ++i) {
long keyId = mAdapter.getMasterKeyId(i);
for (int j = 0; j < masterKeyIds.length; ++j) {
if (keyId == masterKeyIds[j]) {
mListView.setItemChecked(i, true);
break;
}
}
}
}
}
/**
* returns all selected key ids
*
* @return
*/
public long[] getSelectedMasterKeyIds() {
// mListView.getCheckedItemIds() would give the row ids of the KeyRings not the master keys!
Vector<Long> vector = new Vector<Long>();
for (int i = 0; i < mListView.getCount(); ++i) {
if (mListView.isItemChecked(i)) {
vector.add(mAdapter.getMasterKeyId(i));
}
}
// convert to long array
long[] selectedMasterKeyIds = new long[vector.size()];
for (int i = 0; i < vector.size(); ++i) {
selectedMasterKeyIds[i] = vector.get(i);
}
return selectedMasterKeyIds;
}
/**
* returns all selected user ids
*
* @return
*/
public String[] getSelectedUserIds() {
Vector<String> userIds = new Vector<String>();
for (int i = 0; i < mListView.getCount(); ++i) {
if (mListView.isItemChecked(i)) {
userIds.add((String) mAdapter.getUserId(i));
}
}
// make empty array to not return null
String userIdArray[] = new String[0];
return userIds.toArray(userIdArray);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
Uri baseUri = KeyRings.buildPublicKeyRingsUri();
// These are the rows that we will retrieve.
long now = new Date().getTime() / 1000;
String[] projection = new String[] {
KeyRings._ID,
KeyRings.MASTER_KEY_ID,
UserIds.USER_ID,
"(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Tables.KEYS + " AS tmp WHERE tmp."
+ Keys.IS_REVOKED + " = '0' AND tmp." + Keys.CAN_ENCRYPT + " = '1') AS "
+ ROW_AVAILABLE,
"(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Tables.KEYS + " AS tmp WHERE tmp."
+ Keys.IS_REVOKED + " = '0' AND " + Keys.CAN_ENCRYPT + " = '1' AND tmp."
+ Keys.CREATION + " <= '" + now + "' AND " + "(tmp." + Keys.EXPIRY
+ " IS NULL OR tmp." + Keys.EXPIRY + " >= '" + now + "')) AS " + ROW_VALID, };
String inMasterKeyList = null;
if (mSelectedMasterKeyIds != null && mSelectedMasterKeyIds.length > 0) {
inMasterKeyList = KeyRings.MASTER_KEY_ID + " IN (";
for (int i = 0; i < mSelectedMasterKeyIds.length; ++i) {
if (i != 0) {
inMasterKeyList += ", ";
}
inMasterKeyList += DatabaseUtils.sqlEscapeString("" + mSelectedMasterKeyIds[i]);
}
inMasterKeyList += ")";
}
// 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.USER_ID + " ASC";
if (inMasterKeyList != null) {
// sort by selected master keys
orderBy = inMasterKeyList + " DESC, " + orderBy;
}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(getActivity(), baseUri, projection, null, null, orderBy);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
// preselect given master keys
preselectMasterKeyIds(mSelectedMasterKeyIds);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
}

View File

@ -1,47 +0,0 @@
package org.thialfihar.android.apg.ui;
import org.thialfihar.android.apg.R;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import android.content.Intent;
import android.os.Bundle;
public class SelectPublicKeyListActivity extends SherlockFragmentActivity {
private ActionBar mActionBar;
// TODO
public static final String RESULT_EXTRA_SELECTION = "selection";
public static final String RESULT_EXTRA_USER_IDS = "userIds";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActionBar = getSupportActionBar();
setContentView(R.layout.select_public_key_activity);
mActionBar.setDisplayShowTitleEnabled(true);
mActionBar.setDisplayHomeAsUpEnabled(true);
}
/**
* Menu Options
*/
@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;
default:
return super.onOptionsItemSelected(item);
}
}
}

View File

@ -1,110 +0,0 @@
package org.thialfihar.android.apg.ui;
import java.util.Date;
import org.thialfihar.android.apg.provider.ApgContract.KeyRings;
import org.thialfihar.android.apg.provider.ApgContract.Keys;
import org.thialfihar.android.apg.provider.ApgContract.UserIds;
import org.thialfihar.android.apg.ui.widget.SelectPublicKeyCursorAdapter;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.app.LoaderManager;
public class SelectPublicKeyListFragment extends SherlockListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private SherlockFragmentActivity mActivity;
private SelectPublicKeyCursorAdapter mAdapter;
private long mCurrentRowId;
/**
* Define Adapter and Loader on create of Activity
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mActivity = getSherlockActivity();
// register long press context menu
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 SelectPublicKeyCursorAdapter(mActivity, null);
setListAdapter(mAdapter);
// Start out with a progress indicator.
setListShown(false);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
static final String SORT_ORDER = UserIds.USER_ID + " ASC";
// static final String SELECTION =
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
Uri baseUri = KeyRings.buildPublicKeyRingsUri();
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
// These are the rows that we will retrieve.
long now = new Date().getTime() / 1000;
String[] projection = new String[] {
KeyRings._ID, // 0
KeyRings.MASTER_KEY_ID, // 1
UserIds.USER_ID, // 2
"(SELECT COUNT(" + Keys._ID + ") WHERE " + Keys.IS_REVOKED + " = '0' AND "
+ Keys.CAN_ENCRYPT + " = '1')", // 3
"(SELECT COUNT(" + Keys._ID + ") WHERE " + Keys.IS_REVOKED + " = '0' AND "
+ Keys.CAN_ENCRYPT + " = '1' AND " + Keys.CREATION + " <= '" + now
+ "' AND " + "(" + Keys.EXPIRY + " IS NULL OR " + Keys.EXPIRY + " >= '"
+ now + "'))", // 4
};
return new CursorLoader(getActivity(), baseUri, projection, null, null, SORT_ORDER);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
}

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de> * 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.
@ -19,60 +20,47 @@ package org.thialfihar.android.apg.ui.widget;
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;
import org.thialfihar.android.apg.provider.ApgContract.KeyRings;
import org.thialfihar.android.apg.provider.ApgContract.UserIds;
import org.thialfihar.android.apg.ui.SelectPublicKeyFragment;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
public class SelectPublicKeyCursorAdapter extends CursorAdapter { public class SelectPublicKeyCursorAdapter extends CursorAdapter {
private LayoutInflater mInflater; private LayoutInflater mInflater;
// private int mActivityIndex; private ListView mListView;
// private int mTimeIndex;
// private int mActionIndex;
// private int mAmountIndex;
public SelectPublicKeyCursorAdapter(Context context, Cursor c) { @SuppressWarnings("deprecation")
public SelectPublicKeyCursorAdapter(Context context, ListView listView, Cursor c) {
super(context, c); super(context, c);
//
// mActivityIndex = c.getColumnIndex(Notes.ACTIVITY);
// mTimeIndex = c.getColumnIndex(Notes.TIME);
// mActionIndex = c.getColumnIndex(Notes.ACTION);
// mAmountIndex = c.getColumnIndex(Notes.AMOUNT);
mInflater = LayoutInflater.from(context); mInflater = LayoutInflater.from(context);
mListView = listView;
}
public String getUserId(int position) {
mCursor.moveToPosition(position);
return mCursor.getString(mCursor.getColumnIndex(UserIds.USER_ID));
}
public long getMasterKeyId(int position) {
mCursor.moveToPosition(position);
return mCursor.getLong(mCursor.getColumnIndex(KeyRings.MASTER_KEY_ID));
} }
@Override @Override
public void bindView(View view, Context context, Cursor cursor) { public void bindView(View view, Context context, Cursor cursor) {
// TextView activity = (TextView) view.findViewById(android.R.id.text1); boolean enabled = cursor.getInt(cursor.getColumnIndex(SelectPublicKeyFragment.ROW_VALID)) > 0;
// TextView time = (TextView) view.findViewById(android.R.id.text2);
// TextView actionAndAmount = (TextView) view.findViewById(R.id.text3);
//
// activity.setText(cursor.getString(mActivityIndex));
//
// long lTime = cursor.getLong(mTimeIndex);
// Calendar cal = Calendar.getInstance();
// cal.setTimeInMillis(lTime);
// time.setText(cal.get(Calendar.HOUR_OF_DAY) + : + String.format(%02d,
// cal.get(Calendar.MINUTE)));
//
// String amount = cursor.getString(mAmountIndex);
// if ( amount.length() > 0){
// actionAndAmount.setText(cursor.getString(mActionIndex) + ( + amount + ));
// } else {
// actionAndAmount.setText(cursor.getString(mActionIndex));
// }
// boolean enabled = isEnabled(position);
TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId); TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId);
mainUserId.setText(R.string.unknownUserId); mainUserId.setText(R.string.unknownUserId);
TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest); TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest);
@ -82,7 +70,7 @@ public class SelectPublicKeyCursorAdapter extends CursorAdapter {
TextView status = (TextView) view.findViewById(R.id.status); TextView status = (TextView) view.findViewById(R.id.status);
status.setText(R.string.unknownStatus); status.setText(R.string.unknownStatus);
String userId = cursor.getString(2); // USER_ID String userId = cursor.getString(cursor.getColumnIndex(UserIds.USER_ID));
if (userId != null) { if (userId != null) {
String[] userIdSplit = OtherHelper.splitUserId(userId); String[] userIdSplit = OtherHelper.splitUserId(userId);
@ -92,45 +80,45 @@ public class SelectPublicKeyCursorAdapter extends CursorAdapter {
mainUserId.setText(userIdSplit[0]); mainUserId.setText(userIdSplit[0]);
} }
long masterKeyId = cursor.getLong(1); // MASTER_KEY_ID long masterKeyId = cursor.getLong(cursor.getColumnIndex(KeyRings.MASTER_KEY_ID));
keyId.setText(PGPHelper.getSmallFingerPrint(masterKeyId)); keyId.setText(PGPHelper.getSmallFingerPrint(masterKeyId));
if (mainUserIdRest.getText().length() == 0) { if (mainUserIdRest.getText().length() == 0) {
mainUserIdRest.setVisibility(View.GONE); mainUserIdRest.setVisibility(View.GONE);
} }
// if (enabled) { if (enabled) {
// status.setText(R.string.canEncrypt); status.setText(R.string.canEncrypt);
// } else { } else {
if (cursor.getInt(3) > 0) { if (cursor.getInt(cursor.getColumnIndex(SelectPublicKeyFragment.ROW_AVAILABLE)) > 0) {
// has some CAN_ENCRYPT keys, but col(4) = 0, so must be revoked or expired // has some CAN_ENCRYPT keys, but col(ROW_VALID) = 0, so must be revoked or expired
status.setText(R.string.expired); status.setText(R.string.expired);
} else { } else {
status.setText(R.string.noKey); status.setText(R.string.noKey);
} }
// } }
status.setText(status.getText() + " "); status.setText(status.getText() + " ");
CheckBox selected = (CheckBox) view.findViewById(R.id.selected); CheckBox selected = (CheckBox) view.findViewById(R.id.selected);
// if (!enabled) { if (!enabled) {
// mParent.setItemChecked(position, false); mListView.setItemChecked(cursor.getPosition(), false);
// } }
// selected.setChecked(mParent.isItemChecked(position)); selected.setChecked(mListView.isItemChecked(cursor.getPosition()));
// view.setEnabled(enabled); view.setEnabled(enabled);
// mainUserId.setEnabled(enabled); mainUserId.setEnabled(enabled);
// mainUserIdRest.setEnabled(enabled); mainUserIdRest.setEnabled(enabled);
// keyId.setEnabled(enabled); keyId.setEnabled(enabled);
// selected.setEnabled(enabled); selected.setEnabled(enabled);
// status.setEnabled(enabled); status.setEnabled(enabled);
} }
@Override @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) { public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.select_public_key, null); return mInflater.inflate(R.layout.select_public_key_item, null);
} }
} }

View File

@ -1,218 +0,0 @@
package org.thialfihar.android.apg.ui.widget;
///*
// * 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.widget;
//
//import java.util.Date;
//
//import org.thialfihar.android.apg.R;
//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.provider.KeyRings;
//import org.thialfihar.android.apg.provider.Keys;
//import org.thialfihar.android.apg.provider.UserIds;
//
//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 = PGPMain.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(PGPHelper.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;
// }
//}