aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java
blob: 4566e37fd303dd579e4e8de73bf70db3281e0e8d (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
package org.sufficientlysecure.keychain.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.tokenautocomplete.FilteredArrayAdapter;
import com.tokenautocomplete.TokenCompleteTextView;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.ContactHelper;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EncryptKeyCompletionView extends TokenCompleteTextView {
    public EncryptKeyCompletionView(Context context) {
        super(context);
        initView();
    }

    public EncryptKeyCompletionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public EncryptKeyCompletionView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        fromCursor(null);
        setPrefix(getContext().getString(R.string.label_to) + ": ");
        allowDuplicates(false);
    }

    private EncryptKeyAdapter mAdapter;

    @Override
    protected View getViewForObject(Object object) {
        if (object instanceof EncryptionKey) {
            LayoutInflater l = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View view = l.inflate(R.layout.recipient_box_entry, null);
            ((TextView) view.findViewById(android.R.id.text1)).setText(((EncryptionKey) object).getPrimary());
            setImageByKey((ImageView) view.findViewById(android.R.id.icon), (EncryptionKey) object);
            return view;
        }
        return null;
    }

    private void setImageByKey(ImageView view, EncryptionKey key) {
        Bitmap photo = ContactHelper.photoFromFingerprint(getContext().getContentResolver(), key.getFingerprint());

        if (photo != null) {
            view.setImageBitmap(photo);
        } else {
            view.setImageResource(R.drawable.ic_generic_man);
        }
    }

    @Override
    protected Object defaultObject(String completionText) {
        // TODO: We could try to automagically download the key if it's unknown but a key id
        /*if (completionText.startsWith("0x")) {

        }*/
        return null;
    }

    public void fromCursor(Cursor cursor) {
        if (cursor == null) {
            setAdapter(new EncryptKeyAdapter(Collections.<EncryptionKey>emptyList()));
            return;
        }
        ArrayList<EncryptionKey> keys = new ArrayList<EncryptionKey>();
        cursor.moveToFirst();
        while (cursor.moveToNext()) {
            try {
                if (cursor.getInt(cursor.getColumnIndexOrThrow(KeychainContract.KeyRings.HAS_ENCRYPT)) != 0) {
                    EncryptionKey key = new EncryptionKey(cursor);
                    keys.add(key);
                }
            } catch (Exception e) {
                Log.w(Constants.TAG, e);
                return;
            }
        }
        setAdapter(new EncryptKeyAdapter(keys));
    }

    public class EncryptionKey {
        private String mUserId;
        private long mKeyId;
        private String mFingerprint;

        public EncryptionKey(String userId, long keyId, String fingerprint) {
            this.mUserId = userId;
            this.mKeyId = keyId;
            this.mFingerprint = fingerprint;
        }

        public EncryptionKey(Cursor cursor) {
            this(cursor.getString(cursor.getColumnIndexOrThrow(KeychainContract.KeyRings.USER_ID)),
                    cursor.getLong(cursor.getColumnIndexOrThrow(KeychainContract.KeyRings.KEY_ID)),
                    PgpKeyHelper.convertFingerprintToHex(
                            cursor.getBlob(cursor.getColumnIndexOrThrow(KeychainContract.KeyRings.FINGERPRINT))));

        }

        public EncryptionKey(CachedPublicKeyRing ring) throws PgpGeneralException {
            this(ring.getPrimaryUserId(), ring.extractOrGetMasterKeyId(),
                    PgpKeyHelper.convertFingerprintToHex(ring.getFingerprint()));
        }

        public String getUserId() {
            return mUserId;
        }

        public String getFingerprint() {
            return mFingerprint;
        }

        public String getPrimary() {
            String[] userId = KeyRing.splitUserId(mUserId);
            if (userId[0] != null && userId[2] != null) {
                return userId[0] + " (" + userId[2] + ")";
            } else if (userId[0] != null) {
                return userId[0];
            } else {
                return userId[1];
            }
        }

        public String getSecondary() {
            String[] userId = KeyRing.splitUserId(mUserId);
            if (userId[0] != null) {
                return userId[1] + " (" + getKeyIdHexShort() + ")";
            } else {
                return getKeyIdHex();
            }
        }

        public long getKeyId() {
            return mKeyId;
        }

        public String getKeyIdHex() {
            return PgpKeyHelper.convertKeyIdToHex(mKeyId);
        }

        public String getKeyIdHexShort() {
            return PgpKeyHelper.convertKeyIdToHexShort(mKeyId);
        }

        @Override
        public String toString() {
            return Long.toString(mKeyId);
        }
    }

    private class EncryptKeyAdapter extends FilteredArrayAdapter<EncryptionKey> {

        public EncryptKeyAdapter(List<EncryptionKey> objs) {
            super(EncryptKeyCompletionView.this.getContext(), 0, 0, objs);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater l = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View view;
            if (convertView != null) {
                view = convertView;
            } else {
                view = l.inflate(R.layout.recipient_selection_list_entry, null);
            }
            ((TextView) view.findViewById(android.R.id.title)).setText(getItem(position).getPrimary());
            ((TextView) view.findViewById(android.R.id.text1)).setText(getItem(position).getSecondary());
            setImageByKey((ImageView) view.findViewById(android.R.id.icon), getItem(position));
            return view;
        }

        @Override
        protected boolean keepObject(EncryptionKey obj, String mask) {
            String m = mask.toLowerCase();
            return obj.getUserId().toLowerCase().contains(m) ||
                    obj.getKeyIdHex().contains(m) ||
                    obj.getKeyIdHexShort().startsWith(m);
        }
    }
}