aboutsummaryrefslogtreecommitdiffstats
path: root/org_apg/src/org/apg/ui/KeyServerQueryActivity.java
blob: 606acb575754825d769e805781aa770affb4a5e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package org.apg.ui;

import java.util.List;
import java.util.Vector;

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;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
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;
import android.widget.ArrayAdapter;
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;
import android.widget.Spinner;
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;
    private Spinner mKeyServer;

    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);

        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);
        }

        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);
            }
        });

        Intent intent = getIntent();
        if (Apg.Intent.LOOK_UP_KEY_ID.equals(intent.getAction()) ||
            Apg.Intent.LOOK_UP_KEY_ID_AND_RETURN.equals(intent.getAction())) {
            long keyId = intent.getLongExtra(Apg.EXTRA_KEY_ID, 0);
            if (keyId != 0) {
                String query = "0x" + Apg.keyToHex(keyId);
                mQuery.setText(query);
                search(query);
            }
        }
    }

    private void search(String query) {
        showDialog(Id.dialog.querying);
        mQueryType = Id.keyserver.search;
        mQueryString = query;
        mAdapter.setKeys(new Vector<KeyInfo>());
        startThread();
    }

    private void get(long keyId) {
        showDialog(Id.dialog.querying);
        mQueryType = Id.keyserver.get;
        mQueryId = keyId;
        startThread();
    }

    @Override
	protected Dialog onCreateDialog(int id) {
        ProgressDialog progress = (ProgressDialog) super.onCreateDialog(id);
        progress.setMessage(this.getString(R.string.progress_queryingServer,
                                           (String)mKeyServer.getSelectedItem()));
        return progress;
    }

    @Override
    public void run() {
        String error = null;
        Bundle data = new Bundle();
        Message msg = new Message();

        try {
            HkpKeyServer server = new HkpKeyServer((String)mKeyServer.getSelectedItem());
            if (mQueryType == Id.keyserver.search) {
                mSearchResult = server.search(mQueryString);
            } else if (mQueryType == Id.keyserver.get) {
                mKeyData = server.get(mQueryId);
            }
        } catch (QueryException e) {
            error = "" + e;
        } catch (InsufficientQuery e) {
            error = "Insufficient query.";
        } catch (TooManyResponses e) {
            error = "Too many responses.";
        }

        data.putInt(Constants.extras.STATUS, Id.message.done);

        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;
        }

        if (mQueryType == Id.keyserver.search) {
            if (mSearchResult != null) {
                Toast.makeText(this, getString(R.string.keysFound, mSearchResult.size()), Toast.LENGTH_SHORT).show();
                mAdapter.setKeys(mSearchResult);
            }
        } else if (mQueryType == Id.keyserver.get) {
            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);
                    setResult(RESULT_OK, intent);
                } else {
                    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);
                    startActivity(intent);
                }
            }
        }
    }

    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);
            }

            keyId.setText(Apg.getSmallFingerPrint(keyInfo.keyId));

            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;
        }
    }
}