1/* MemorizingTrustManager - a TrustManager which asks the user about invalid
2 * certificates and memorizes their decision.
3 *
4 * Copyright (c) 2010 Georg Lukas <georg@op-co.de>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24package eu.siacs.conversations.ui;
25
26
27import android.content.DialogInterface;
28import android.content.DialogInterface.OnCancelListener;
29import android.content.DialogInterface.OnClickListener;
30import android.content.Intent;
31import android.os.Bundle;
32
33import androidx.appcompat.app.AlertDialog;
34import androidx.appcompat.app.AppCompatActivity;
35
36import java.util.logging.Level;
37import java.util.logging.Logger;
38
39import eu.siacs.conversations.R;
40import eu.siacs.conversations.entities.MTMDecision;
41import eu.siacs.conversations.services.MemorizingTrustManager;
42import eu.siacs.conversations.ui.util.SettingsUtils;
43import eu.siacs.conversations.utils.ThemeHelper;
44
45public class MemorizingActivity extends AppCompatActivity implements OnClickListener, OnCancelListener {
46
47 private final static Logger LOGGER = Logger.getLogger(MemorizingActivity.class.getName());
48
49 int decisionId;
50
51 AlertDialog dialog;
52
53 @Override
54 public void onCreate(Bundle savedInstanceState) {
55 LOGGER.log(Level.FINE, "onCreate");
56 setTheme(ThemeHelper.find(this));
57 super.onCreate(savedInstanceState);
58 getLayoutInflater().inflate(R.layout.toolbar, findViewById(android.R.id.content));
59 setSupportActionBar(findViewById(R.id.toolbar));
60 }
61
62 @Override
63 public void onResume() {
64 super.onResume();
65 SettingsUtils.applyScreenshotPreventionSetting(this);
66
67 Intent i = getIntent();
68 decisionId = i.getIntExtra(MemorizingTrustManager.DECISION_INTENT_ID, MTMDecision.DECISION_INVALID);
69 int titleId = i.getIntExtra(MemorizingTrustManager.DECISION_TITLE_ID, R.string.mtm_accept_cert);
70 String cert = i.getStringExtra(MemorizingTrustManager.DECISION_INTENT_CERT);
71 LOGGER.log(Level.FINE, "onResume with " + i.getExtras() + " decId=" + decisionId + " data: " + i.getData());
72 dialog = new AlertDialog.Builder(this).setTitle(titleId)
73 .setMessage(cert)
74 .setPositiveButton(R.string.always, this)
75 .setNeutralButton(R.string.once, this)
76 .setNegativeButton(R.string.cancel, this)
77 .setOnCancelListener(this)
78 .create();
79 dialog.show();
80 }
81
82 @Override
83 protected void onPause() {
84 if (dialog.isShowing())
85 dialog.dismiss();
86 super.onPause();
87 }
88
89 void sendDecision(int decision) {
90 LOGGER.log(Level.FINE, "Sending decision: " + decision);
91 MemorizingTrustManager.interactResult(decisionId, decision);
92 finish();
93 }
94
95 // react on AlertDialog button press
96 public void onClick(DialogInterface dialog, int btnId) {
97 int decision;
98 dialog.dismiss();
99 switch (btnId) {
100 case DialogInterface.BUTTON_POSITIVE:
101 decision = MTMDecision.DECISION_ALWAYS;
102 break;
103 case DialogInterface.BUTTON_NEUTRAL:
104 decision = MTMDecision.DECISION_ONCE;
105 break;
106 default:
107 decision = MTMDecision.DECISION_ABORT;
108 }
109 sendDecision(decision);
110 }
111
112 public void onCancel(DialogInterface dialog) {
113 sendDecision(MTMDecision.DECISION_ABORT);
114 }
115}