aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/OmemoActivity.java
blob: 499654cba132218794d7866b18181aeaf5966804 (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
package de.pixart.messenger.ui;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.security.cert.X509Certificate;
import java.util.Arrays;

import de.pixart.messenger.Config;
import de.pixart.messenger.R;
import de.pixart.messenger.crypto.axolotl.FingerprintStatus;
import de.pixart.messenger.crypto.axolotl.XmppAxolotlSession;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.ui.widget.Switch;
import de.pixart.messenger.utils.CryptoHelper;
import de.pixart.messenger.utils.XmppUri;

public abstract class OmemoActivity extends XmppActivity {

    private Account mSelectedAccount;
    private String mSelectedFingerprint;
    protected XmppUri mPendingFingerprintVerificationUri = null;

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        Object account = v.getTag(R.id.TAG_ACCOUNT);
        Object fingerprint = v.getTag(R.id.TAG_FINGERPRINT);
        Object fingerprintStatus = v.getTag(R.id.TAG_FINGERPRINT_STATUS);
        if (account != null
                && fingerprint != null
                && account instanceof Account
                && fingerprintStatus != null
                && fingerprint instanceof String
                && fingerprintStatus instanceof FingerprintStatus) {
            getMenuInflater().inflate(R.menu.omemo_key_context, menu);
            MenuItem distrust = menu.findItem(R.id.distrust_key);
            MenuItem verifyScan = menu.findItem(R.id.verify_scan);
            if (this instanceof TrustKeysActivity) {
                distrust.setVisible(false);
                verifyScan.setVisible(false);
            } else {
                FingerprintStatus status = (FingerprintStatus) fingerprintStatus;
                if (!status.isActive() || status.isVerified()) {
                    verifyScan.setVisible(false);
                }
                distrust.setVisible(status.isVerified());
            }
            this.mSelectedAccount = (Account) account;
            this.mSelectedFingerprint = (String) fingerprint;
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.distrust_key:
                showPurgeKeyDialog(mSelectedAccount, mSelectedFingerprint);
                break;
            case R.id.copy_omemo_key:
                copyOmemoFingerprint(mSelectedFingerprint);
                break;
            case R.id.verify_scan:
                new IntentIntegrator(this).initiateScan(Arrays.asList("AZTEC","QR_CODE"));
                break;
        }
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanResult != null && scanResult.getFormatName() != null) {
            String data = scanResult.getContents();
            XmppUri uri = new XmppUri(data);
            if (xmppConnectionServiceBound) {
                processFingerprintVerification(uri);
            } else {
                this.mPendingFingerprintVerificationUri =uri;
            }
        }
    }

    protected abstract void processFingerprintVerification(XmppUri uri);

    protected void copyOmemoFingerprint(String fingerprint) {
        if (copyTextToClipboard(CryptoHelper.prettifyFingerprint(fingerprint.substring(2)), R.string.omemo_fingerprint)) {
            Toast.makeText(
                    this,
                    R.string.toast_message_omemo_fingerprint,
                    Toast.LENGTH_SHORT).show();
        }
    }

    protected void addFingerprintRow(LinearLayout keys, final XmppAxolotlSession session, boolean highlight) {
        final Account account = session.getAccount();
        final String fingerprint = session.getFingerprint();
        addFingerprintRowWithListeners(keys,
                session.getAccount(),
                session.getFingerprint(),
                highlight,
                session.getTrust(),
                true,
                true,
                new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        account.getAxolotlService().setFingerprintTrust(fingerprint, FingerprintStatus.createActive(isChecked));
                    }
                });
    }

    protected void addFingerprintRowWithListeners(LinearLayout keys, final Account account,
                                                  final String fingerprint,
                                                  boolean highlight,
                                                  FingerprintStatus status,
                                                  boolean showTag,
                                                  boolean undecidedNeedEnablement,
                                                  CompoundButton.OnCheckedChangeListener
                                                          onCheckedChangeListener) {

        View view = getLayoutInflater().inflate(R.layout.contact_key, keys, false);
        TextView key = (TextView) view.findViewById(R.id.key);
        TextView keyType = (TextView) view.findViewById(R.id.key_type);
        if (Config.X509_VERIFICATION && status.getTrust() == FingerprintStatus.Trust.VERIFIED_X509) {
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showX509Certificate(account, fingerprint);
                }
            };
            key.setOnClickListener(listener);
            keyType.setOnClickListener(listener);
        }
        Switch trustToggle = (Switch) view.findViewById(R.id.tgl_trust);
        ImageView verifiedFingerprintSymbol = (ImageView) view.findViewById(R.id.verified_fingerprint);
        trustToggle.setVisibility(View.VISIBLE);
        registerForContextMenu(view);
        view.setTag(R.id.TAG_ACCOUNT, account);
        view.setTag(R.id.TAG_FINGERPRINT, fingerprint);
        view.setTag(R.id.TAG_FINGERPRINT_STATUS, status);
        boolean x509 = Config.X509_VERIFICATION && status.getTrust() == FingerprintStatus.Trust.VERIFIED_X509;
        final View.OnClickListener toast;
        trustToggle.setChecked(status.isTrusted(), false);

        if (status.isActive()) {
            key.setTextColor(getPrimaryTextColor());
            keyType.setTextColor(getSecondaryTextColor());
            if (status.isVerified()) {
                verifiedFingerprintSymbol.setVisibility(View.VISIBLE);
                verifiedFingerprintSymbol.setAlpha(1.0f);
                trustToggle.setVisibility(View.GONE);
                verifiedFingerprintSymbol.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        replaceToast(getString(R.string.this_device_has_been_verified), false);
                    }
                });
                toast = null;
            } else {
                verifiedFingerprintSymbol.setVisibility(View.GONE);
                trustToggle.setVisibility(View.VISIBLE);
                trustToggle.setOnCheckedChangeListener(onCheckedChangeListener);
                if (status.getTrust() == FingerprintStatus.Trust.UNDECIDED && undecidedNeedEnablement) {
                    trustToggle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            account.getAxolotlService().setFingerprintTrust(fingerprint, FingerprintStatus.createActive(false));
                            v.setEnabled(true);
                            v.setOnClickListener(null);
                        }
                    });
                    trustToggle.setEnabled(false);
                } else {
                    trustToggle.setOnClickListener(null);
                    trustToggle.setEnabled(true);
                }
                toast = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hideToast();
                    }
                };
            }
        } else {
            key.setTextColor(getTertiaryTextColor());
            keyType.setTextColor(getTertiaryTextColor());
            toast = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    replaceToast(getString(R.string.this_device_is_no_longer_in_use), false);
                }
            };
            if (status.isVerified()) {
                trustToggle.setVisibility(View.GONE);
                verifiedFingerprintSymbol.setVisibility(View.VISIBLE);
                verifiedFingerprintSymbol.setAlpha(0.4368f);
                verifiedFingerprintSymbol.setOnClickListener(toast);
            } else {
                trustToggle.setVisibility(View.VISIBLE);
                verifiedFingerprintSymbol.setVisibility(View.GONE);
                trustToggle.setOnClickListener(null);
                trustToggle.setEnabled(false);
                trustToggle.setOnClickListener(toast);
            }
        }

        view.setOnClickListener(toast);
        key.setOnClickListener(toast);
        keyType.setOnClickListener(toast);
        if (showTag) {
            keyType.setText(getString(x509 ? R.string.omemo_fingerprint_x509 : R.string.omemo_fingerprint));
        } else {
            keyType.setVisibility(View.GONE);
        }
        if (highlight) {
            keyType.setTextColor(ContextCompat.getColor(this, R.color.accent));
            keyType.setText(getString(x509 ? R.string.omemo_fingerprint_x509_selected_message : R.string.omemo_fingerprint_selected_message));
        } else {
            keyType.setText(getString(x509 ? R.string.omemo_fingerprint_x509 : R.string.omemo_fingerprint));
        }

        key.setText(CryptoHelper.prettifyFingerprint(fingerprint.substring(2)));

        keys.addView(view);
    }

    public void showPurgeKeyDialog(final Account account, final String fingerprint) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.distrust_omemo_key);
        builder.setMessage(R.string.distrust_omemo_key_text);
        builder.setNegativeButton(getString(R.string.cancel), null);
        builder.setPositiveButton(R.string.confirm,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        account.getAxolotlService().distrustFingerprint(fingerprint);
                        refreshUi();
                    }
                });
        builder.create().show();
    }

    private void showX509Certificate(Account account, String fingerprint) {
        X509Certificate x509Certificate = account.getAxolotlService().getFingerprintCertificate(fingerprint);
        if (x509Certificate != null) {
            showCertificateInformationDialog(CryptoHelper.extractCertificateInformation(x509Certificate));
        } else {
            Toast.makeText(this, R.string.certificate_not_found, Toast.LENGTH_SHORT).show();
        }
    }

    private void showCertificateInformationDialog(Bundle bundle) {
        View view = getLayoutInflater().inflate(R.layout.certificate_information, null);
        final String not_available = getString(R.string.certicate_info_not_available);
        TextView subject_cn = (TextView) view.findViewById(R.id.subject_cn);
        TextView subject_o = (TextView) view.findViewById(R.id.subject_o);
        TextView issuer_cn = (TextView) view.findViewById(R.id.issuer_cn);
        TextView issuer_o = (TextView) view.findViewById(R.id.issuer_o);
        TextView sha1 = (TextView) view.findViewById(R.id.sha1);

        subject_cn.setText(bundle.getString("subject_cn", not_available));
        subject_o.setText(bundle.getString("subject_o", not_available));
        issuer_cn.setText(bundle.getString("issuer_cn", not_available));
        issuer_o.setText(bundle.getString("issuer_o", not_available));
        sha1.setText(bundle.getString("sha1", not_available));

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.certificate_information);
        builder.setView(view);
        builder.setPositiveButton(R.string.ok, null);
        builder.create().show();
    }
}