aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/ui/VerifyOTRActivity.java
blob: 31884bd27f13ba519d7d3d309a51b686c68cebd3 (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
package eu.siacs.conversations.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import net.java.otr4j.OtrException;
import net.java.otr4j.session.Session;

import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;

public class VerifyOTRActivity extends XmppActivity implements XmppConnectionService.OnConversationUpdate {

	public static final String ACTION_VERIFY_CONTACT = "verify_contact";

	private RelativeLayout mVerificationAreaOne;
	private RelativeLayout mVerificationAreaTwo;
	private TextView mErrorNoSession;
	private TextView mRemoteJid;
	private TextView mRemoteFingerprint;
	private TextView mYourFingerprint;
	private EditText mSharedSecretHint;
	private EditText mSharedSecretSecret;
	private Button mButtonVerifyFingerprint;
	private Button mButtonSharedSecretPositive;
	private Button mButtonSharedSecretNegative;
	private TextView mStatusMessage;
	private Account mAccount;
	private Conversation mConversation;

	private View.OnClickListener mVerifyFingerprintListener = new View.OnClickListener() {

		@Override
		public void onClick(View view) {
			mConversation.verifyOtrFingerprint();
			finish();
		}
	};

	private View.OnClickListener mCreateSharedSecretListener = new View.OnClickListener() {
		@Override
		public void onClick(final View view) {
			if (isAccountOnline()) {
				final String question = mSharedSecretHint.getText().toString();
				final String secret = mSharedSecretSecret.getText().toString();
				initSmp(question, secret);
				updateView();
			}
		}
	};
	private View.OnClickListener mCancelSharedSecretListener = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			if (isAccountOnline()) {
				abortSmp();
				updateView();
			}
		}
	};
	private View.OnClickListener mRespondSharedSecretListener = new View.OnClickListener() {

		@Override
		public void onClick(View view) {
			if (isAccountOnline()) {
				final String question = mSharedSecretHint.getText().toString();
				final String secret = mSharedSecretSecret.getText().toString();
				respondSmp(question, secret);
				updateView();
			}
		}
	};
	private View.OnClickListener mRetrySharedSecretListener = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			mConversation.smp().status = Conversation.Smp.STATUS_NONE;
			mConversation.smp().hint = null;
			mConversation.smp().secret = null;
			updateView();
		}
	};
	private View.OnClickListener mFinishListener = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			mConversation.smp().status = Conversation.Smp.STATUS_NONE;
			finish();
		}
	};

	protected boolean initSmp(final String question, final String secret) {
		final Session session = mConversation.getOtrSession();
		if (session!=null) {
			try {
				session.initSmp(question, secret);
				mConversation.smp().status = Conversation.Smp.STATUS_WE_REQUESTED;
				return true;
			} catch (OtrException e) {
				return false;
			}
		} else {
			return false;
		}
	}

	protected boolean abortSmp() {
		final Session session = mConversation.getOtrSession();
		if (session!=null) {
			try {
				session.abortSmp();
				mConversation.smp().status = Conversation.Smp.STATUS_NONE;
				mConversation.smp().hint = null;
				mConversation.smp().secret = null;
				return true;
			} catch (OtrException e) {
				return false;
			}
		} else {
			return false;
		}
	}

	protected boolean respondSmp(final String question, final String secret) {
		final Session session = mConversation.getOtrSession();
		if (session!=null) {
			try {
				session.respondSmp(question,secret);
				return true;
			} catch (OtrException e) {
				return false;
			}
		} else {
			return false;
		}
	}

	protected boolean isAccountOnline() {
		if (this.mAccount.getStatus() != Account.STATUS_ONLINE) {
			Toast.makeText(this,R.string.not_connected_try_again,Toast.LENGTH_SHORT).show();
			return false;
		} else {
			return true;
		}
	}

	protected boolean handleIntent(Intent intent) {
		if (intent.getAction().equals(ACTION_VERIFY_CONTACT)) {
			try {
				this.mAccount = this.xmppConnectionService.findAccountByJid(Jid.fromString(intent.getExtras().getString("account")));
			} catch (final InvalidJidException ignored) {
				return false;
			}
			try {
				this.mConversation = this.xmppConnectionService.find(this.mAccount,Jid.fromString(intent.getExtras().getString("contact")));
				if (this.mConversation == null) {
					return false;
				}
			} catch (final InvalidJidException ignored) {
				return false;
			}
			return true;
		} else {
			return false;
		}
	}

	@Override
	protected void onBackendConnected() {
		if (handleIntent(getIntent())) {
			updateView();
		}
	}

	protected void updateView() {
		if (this.mConversation.hasValidOtrSession()) {
			this.mVerificationAreaOne.setVisibility(View.VISIBLE);
			this.mVerificationAreaTwo.setVisibility(View.VISIBLE);
			this.mErrorNoSession.setVisibility(View.GONE);
			this.mYourFingerprint.setText(this.mAccount.getOtrFingerprint(xmppConnectionService));
			this.mRemoteFingerprint.setText(this.mConversation.getOtrFingerprint());
			this.mRemoteJid.setText(this.mConversation.getContact().getJid().toBareJid().toString());
			Conversation.Smp smp = mConversation.smp();
			Session session = mConversation.getOtrSession();
			if (mConversation.isOtrFingerprintVerified()) {
				deactivateButton(mButtonVerifyFingerprint, R.string.verified);
			} else {
				activateButton(mButtonVerifyFingerprint, R.string.verify, mVerifyFingerprintListener);
			}
			if (smp.status == Conversation.Smp.STATUS_NONE) {
				activateButton(mButtonSharedSecretPositive, R.string.create, mCreateSharedSecretListener);
				deactivateButton(mButtonSharedSecretNegative, R.string.cancel);
				this.mSharedSecretHint.setFocusableInTouchMode(true);
				this.mSharedSecretSecret.setFocusableInTouchMode(true);
				this.mSharedSecretSecret.setText("");
				this.mSharedSecretHint.setText("");
				this.mSharedSecretHint.setVisibility(View.VISIBLE);
				this.mSharedSecretSecret.setVisibility(View.VISIBLE);
				this.mStatusMessage.setVisibility(View.GONE);
			} else if (smp.status == Conversation.Smp.STATUS_CONTACT_REQUESTED) {
				this.mSharedSecretHint.setFocusable(false);
				this.mSharedSecretHint.setText(smp.hint);
				this.mSharedSecretSecret.setFocusableInTouchMode(true);
				this.mSharedSecretHint.setVisibility(View.VISIBLE);
				this.mSharedSecretSecret.setVisibility(View.VISIBLE);
				this.mStatusMessage.setVisibility(View.GONE);
				deactivateButton(mButtonSharedSecretNegative, R.string.cancel);
				activateButton(mButtonSharedSecretPositive, R.string.respond, mRespondSharedSecretListener);
			} else if (smp.status == Conversation.Smp.STATUS_FAILED) {
				activateButton(mButtonSharedSecretNegative, R.string.cancel, mFinishListener);
				activateButton(mButtonSharedSecretPositive, R.string.try_again, mRetrySharedSecretListener);
				this.mSharedSecretHint.setVisibility(View.GONE);
				this.mSharedSecretSecret.setVisibility(View.GONE);
				this.mStatusMessage.setVisibility(View.VISIBLE);
				this.mStatusMessage.setText(R.string.secrets_do_not_match);
				this.mStatusMessage.setTextColor(getWarningTextColor());
			} else if (smp.status == Conversation.Smp.STATUS_VERIFIED) {
				this.mSharedSecretHint.setVisibility(View.GONE);
				this.mSharedSecretSecret.setVisibility(View.GONE);
				this.mStatusMessage.setVisibility(View.VISIBLE);
				this.mStatusMessage.setText(R.string.verified);
				this.mStatusMessage.setTextColor(getPrimaryColor());
				deactivateButton(mButtonSharedSecretNegative, R.string.cancel);
				activateButton(mButtonSharedSecretPositive, R.string.finish, mFinishListener);
			} else if (session != null && session.isSmpInProgress()) {
				deactivateButton(mButtonSharedSecretPositive, R.string.in_progress);
				activateButton(mButtonSharedSecretNegative, R.string.cancel, mCancelSharedSecretListener);
				this.mSharedSecretHint.setVisibility(View.VISIBLE);
				this.mSharedSecretSecret.setVisibility(View.VISIBLE);
				this.mSharedSecretHint.setFocusable(false);
				this.mSharedSecretSecret.setFocusable(false);
			}
		} else {
			this.mVerificationAreaOne.setVisibility(View.GONE);
			this.mVerificationAreaTwo.setVisibility(View.GONE);
			this.mErrorNoSession.setVisibility(View.VISIBLE);
		}
	}

	protected void activateButton(Button button, int text, View.OnClickListener listener) {
		button.setEnabled(true);
		button.setTextColor(getPrimaryTextColor());
		button.setText(text);
		button.setOnClickListener(listener);
	}

	protected void deactivateButton(Button button, int text) {
		button.setEnabled(false);
		button.setTextColor(getSecondaryTextColor());
		button.setText(text);
		button.setOnClickListener(null);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_verify_otr);
		this.mRemoteFingerprint = (TextView) findViewById(R.id.remote_fingerprint);
		this.mRemoteJid = (TextView) findViewById(R.id.remote_jid);
		this.mYourFingerprint = (TextView) findViewById(R.id.your_fingerprint);
		this.mButtonSharedSecretNegative = (Button) findViewById(R.id.button_shared_secret_negative);
		this.mButtonSharedSecretPositive = (Button) findViewById(R.id.button_shared_secret_positive);
		this.mButtonVerifyFingerprint = (Button) findViewById(R.id.button_verify_fingerprint);
		this.mSharedSecretSecret = (EditText) findViewById(R.id.shared_secret_secret);
		this.mSharedSecretHint = (EditText) findViewById(R.id.shared_secret_hint);
		this.mStatusMessage= (TextView) findViewById(R.id.status_message);
		this.mVerificationAreaOne = (RelativeLayout) findViewById(R.id.verification_area_one);
		this.mVerificationAreaTwo = (RelativeLayout) findViewById(R.id.verification_area_two);
		this.mErrorNoSession = (TextView) findViewById(R.id.error_no_session);
	}

	@Override
	protected String getShareableUri() {
		if (mAccount!=null) {
			return "xmpp:"+mAccount.getJid().toBareJid();
		} else {
			return "";
		}
	}

	public void onConversationUpdate() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				updateView();
			}
		});
	}
}