2012-03-09 10:27:29 -05:00
|
|
|
package org.apg.ui;
|
2010-08-16 21:02:39 -04:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Vector;
|
|
|
|
|
2012-03-09 10:27:29 -05:00
|
|
|
import org.apg.Apg;
|
|
|
|
import org.apg.Constants;
|
|
|
|
import org.apg.HkpKeyServer;
|
|
|
|
import org.apg.Id;
|
|
|
|
import org.apg.KeyServer.InsufficientQuery;
|
|
|
|
import org.apg.KeyServer.KeyInfo;
|
|
|
|
import org.apg.KeyServer.QueryException;
|
|
|
|
import org.apg.KeyServer.TooManyResponses;
|
|
|
|
import org.apg.R;
|
2010-08-16 21:02:39 -04:00
|
|
|
|
|
|
|
import android.app.Activity;
|
2010-08-17 19:05:41 -04:00
|
|
|
import android.app.Dialog;
|
|
|
|
import android.app.ProgressDialog;
|
2010-08-16 21:02:39 -04:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Message;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
2010-08-17 19:05:41 -04:00
|
|
|
import android.widget.ArrayAdapter;
|
2010-08-16 21:02:39 -04:00
|
|
|
import android.widget.BaseAdapter;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.LinearLayout.LayoutParams;
|
|
|
|
import android.widget.ListView;
|
2010-08-17 19:05:41 -04:00
|
|
|
import android.widget.Spinner;
|
2010-08-16 21:02:39 -04:00
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
public class KeyServerQueryActivity extends BaseActivity {
|
|
|
|
private ListView mList;
|
|
|
|
private EditText mQuery;
|
|
|
|
private Button mSearch;
|
|
|
|
private KeyInfoListAdapter mAdapter;
|
2010-08-17 19:05:41 -04:00
|
|
|
private Spinner mKeyServer;
|
2010-08-16 21:02:39 -04:00
|
|
|
|
|
|
|
private int mQueryType;
|
|
|
|
private String mQueryString;
|
|
|
|
private long mQueryId;
|
|
|
|
private volatile List<KeyInfo> mSearchResult;
|
|
|
|
private volatile String mKeyData;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
setContentView(R.layout.key_server_query_layout);
|
|
|
|
|
|
|
|
mQuery = (EditText) findViewById(R.id.query);
|
|
|
|
mSearch = (Button) findViewById(R.id.btn_search);
|
|
|
|
mList = (ListView) findViewById(R.id.list);
|
|
|
|
mAdapter = new KeyInfoListAdapter(this);
|
|
|
|
mList.setAdapter(mAdapter);
|
|
|
|
|
2010-08-17 19:05:41 -04:00
|
|
|
mKeyServer = (Spinner) findViewById(R.id.keyServer);
|
|
|
|
ArrayAdapter<String> adapter =
|
|
|
|
new ArrayAdapter<String>(this,
|
|
|
|
android.R.layout.simple_spinner_item,
|
|
|
|
mPreferences.getKeyServers());
|
|
|
|
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
|
|
mKeyServer.setAdapter(adapter);
|
|
|
|
if (adapter.getCount() > 0) {
|
|
|
|
mKeyServer.setSelection(0);
|
|
|
|
} else {
|
|
|
|
mSearch.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
2010-08-16 21:02:39 -04:00
|
|
|
mList.setOnItemClickListener(new OnItemClickListener() {
|
|
|
|
public void onItemClick(AdapterView<?> adapter, View view, int position, long keyId) {
|
|
|
|
get(keyId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mSearch.setOnClickListener(new OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
String query = mQuery.getText().toString();
|
|
|
|
search(query);
|
|
|
|
}
|
|
|
|
});
|
2010-08-17 20:23:28 -04:00
|
|
|
|
|
|
|
Intent intent = getIntent();
|
2010-08-18 08:26:13 -04:00
|
|
|
if (Apg.Intent.LOOK_UP_KEY_ID.equals(intent.getAction()) ||
|
|
|
|
Apg.Intent.LOOK_UP_KEY_ID_AND_RETURN.equals(intent.getAction())) {
|
2010-08-17 20:23:28 -04:00
|
|
|
long keyId = intent.getLongExtra(Apg.EXTRA_KEY_ID, 0);
|
|
|
|
if (keyId != 0) {
|
|
|
|
String query = "0x" + Apg.keyToHex(keyId);
|
|
|
|
mQuery.setText(query);
|
|
|
|
search(query);
|
|
|
|
}
|
|
|
|
}
|
2010-08-16 21:02:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private void search(String query) {
|
|
|
|
showDialog(Id.dialog.querying);
|
2011-10-16 21:07:37 -04:00
|
|
|
mQueryType = Id.keyserver.search;
|
2010-08-16 21:02:39 -04:00
|
|
|
mQueryString = query;
|
2010-08-17 10:51:39 -04:00
|
|
|
mAdapter.setKeys(new Vector<KeyInfo>());
|
2010-08-16 21:02:39 -04:00
|
|
|
startThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void get(long keyId) {
|
|
|
|
showDialog(Id.dialog.querying);
|
2011-10-16 21:07:37 -04:00
|
|
|
mQueryType = Id.keyserver.get;
|
2010-08-16 21:02:39 -04:00
|
|
|
mQueryId = keyId;
|
|
|
|
startThread();
|
|
|
|
}
|
|
|
|
|
2011-09-17 17:44:06 -04:00
|
|
|
@Override
|
|
|
|
protected Dialog onCreateDialog(int id) {
|
2010-08-17 19:05:41 -04:00
|
|
|
ProgressDialog progress = (ProgressDialog) super.onCreateDialog(id);
|
|
|
|
progress.setMessage(this.getString(R.string.progress_queryingServer,
|
|
|
|
(String)mKeyServer.getSelectedItem()));
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
2010-08-16 21:02:39 -04:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
String error = null;
|
|
|
|
Bundle data = new Bundle();
|
|
|
|
Message msg = new Message();
|
|
|
|
|
|
|
|
try {
|
2010-08-17 19:05:41 -04:00
|
|
|
HkpKeyServer server = new HkpKeyServer((String)mKeyServer.getSelectedItem());
|
2011-10-16 21:07:37 -04:00
|
|
|
if (mQueryType == Id.keyserver.search) {
|
2010-08-16 21:02:39 -04:00
|
|
|
mSearchResult = server.search(mQueryString);
|
2011-10-16 21:07:37 -04:00
|
|
|
} else if (mQueryType == Id.keyserver.get) {
|
2010-08-16 21:02:39 -04:00
|
|
|
mKeyData = server.get(mQueryId);
|
|
|
|
}
|
2010-08-17 10:51:39 -04:00
|
|
|
} catch (QueryException e) {
|
2010-08-16 21:02:39 -04:00
|
|
|
error = "" + e;
|
2010-08-17 10:51:39 -04:00
|
|
|
} catch (InsufficientQuery e) {
|
|
|
|
error = "Insufficient query.";
|
|
|
|
} catch (TooManyResponses e) {
|
|
|
|
error = "Too many responses.";
|
2010-08-16 21:02:39 -04:00
|
|
|
}
|
|
|
|
|
2012-03-09 12:43:10 -05:00
|
|
|
data.putInt(Constants.extras.STATUS, Id.message.done);
|
2010-08-16 21:02:39 -04:00
|
|
|
|
|
|
|
if (error != null) {
|
|
|
|
data.putString(Apg.EXTRA_ERROR, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.setData(data);
|
|
|
|
sendMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doneCallback(Message msg) {
|
|
|
|
super.doneCallback(msg);
|
|
|
|
|
|
|
|
removeDialog(Id.dialog.querying);
|
|
|
|
|
|
|
|
Bundle data = msg.getData();
|
|
|
|
String error = data.getString(Apg.EXTRA_ERROR);
|
|
|
|
if (error != null) {
|
|
|
|
Toast.makeText(this, getString(R.string.errorMessage, error), Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-16 21:07:37 -04:00
|
|
|
if (mQueryType == Id.keyserver.search) {
|
2010-08-16 21:02:39 -04:00
|
|
|
if (mSearchResult != null) {
|
2010-08-17 10:51:39 -04:00
|
|
|
Toast.makeText(this, getString(R.string.keysFound, mSearchResult.size()), Toast.LENGTH_SHORT).show();
|
2010-08-16 21:02:39 -04:00
|
|
|
mAdapter.setKeys(mSearchResult);
|
|
|
|
}
|
2011-10-16 21:07:37 -04:00
|
|
|
} else if (mQueryType == Id.keyserver.get) {
|
2010-08-18 08:26:13 -04:00
|
|
|
Intent orgIntent = getIntent();
|
|
|
|
if (Apg.Intent.LOOK_UP_KEY_ID_AND_RETURN.equals(orgIntent.getAction())) {
|
|
|
|
if (mKeyData != null) {
|
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.putExtra(Apg.EXTRA_TEXT, mKeyData);
|
2010-08-17 20:23:28 -04:00
|
|
|
setResult(RESULT_OK, intent);
|
|
|
|
} else {
|
2010-08-18 08:26:13 -04:00
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
}
|
|
|
|
finish();
|
|
|
|
} else {
|
|
|
|
if (mKeyData != null) {
|
|
|
|
Intent intent = new Intent(this, PublicKeyListActivity.class);
|
|
|
|
intent.setAction(Apg.Intent.IMPORT);
|
|
|
|
intent.putExtra(Apg.EXTRA_TEXT, mKeyData);
|
2010-08-17 20:23:28 -04:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
2010-08-16 21:02:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class KeyInfoListAdapter extends BaseAdapter {
|
|
|
|
protected LayoutInflater mInflater;
|
|
|
|
protected Activity mActivity;
|
|
|
|
protected List<KeyInfo> mKeys;
|
|
|
|
|
|
|
|
public KeyInfoListAdapter(Activity activity) {
|
|
|
|
mActivity = activity;
|
|
|
|
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
mKeys = new Vector<KeyInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setKeys(List<KeyInfo> keys) {
|
|
|
|
mKeys = keys;
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasStableIds() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getCount() {
|
|
|
|
return mKeys.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object getItem(int position) {
|
|
|
|
return mKeys.get(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return mKeys.get(position).keyId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
KeyInfo keyInfo = mKeys.get(position);
|
|
|
|
|
|
|
|
View view = mInflater.inflate(R.layout.key_server_query_result_item, null);
|
|
|
|
|
|
|
|
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 algorithm = (TextView) view.findViewById(R.id.algorithm);
|
|
|
|
algorithm.setText("");
|
|
|
|
TextView status = (TextView) view.findViewById(R.id.status);
|
|
|
|
status.setText("");
|
|
|
|
|
|
|
|
String userId = keyInfo.userIds.get(0);
|
|
|
|
if (userId != null) {
|
|
|
|
String chunks[] = userId.split(" <", 2);
|
|
|
|
userId = chunks[0];
|
|
|
|
if (chunks.length > 1) {
|
|
|
|
mainUserIdRest.setText("<" + chunks[1]);
|
|
|
|
}
|
|
|
|
mainUserId.setText(userId);
|
|
|
|
}
|
|
|
|
|
2010-12-25 14:12:35 -05:00
|
|
|
keyId.setText(Apg.getSmallFingerPrint(keyInfo.keyId));
|
2010-08-16 21:02:39 -04:00
|
|
|
|
|
|
|
if (mainUserIdRest.getText().length() == 0) {
|
|
|
|
mainUserIdRest.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
algorithm.setText("" + keyInfo.size + "/" + keyInfo.algorithm);
|
|
|
|
|
|
|
|
if (keyInfo.revoked != null) {
|
|
|
|
status.setText("revoked");
|
|
|
|
} else {
|
|
|
|
status.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
LinearLayout ll = (LinearLayout) view.findViewById(R.id.list);
|
|
|
|
if (keyInfo.userIds.size() == 1) {
|
|
|
|
ll.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
boolean first = true;
|
|
|
|
boolean second = true;
|
|
|
|
for (String uid : keyInfo.userIds) {
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!second) {
|
|
|
|
View sep = new View(mActivity);
|
|
|
|
sep.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
|
|
|
|
sep.setBackgroundResource(android.R.drawable.divider_horizontal_dark);
|
|
|
|
ll.addView(sep);
|
|
|
|
}
|
|
|
|
TextView uidView = (TextView) mInflater.inflate(R.layout.key_server_query_result_user_id, null);
|
|
|
|
uidView.setText(uid);
|
|
|
|
ll.addView(uidView);
|
|
|
|
second = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|