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 com.google.android.material.dialog.MaterialAlertDialogBuilder;
 37
 38import java.util.logging.Level;
 39import java.util.logging.Logger;
 40
 41import eu.siacs.conversations.R;
 42import eu.siacs.conversations.entities.MTMDecision;
 43import eu.siacs.conversations.services.MemorizingTrustManager;
 44import eu.siacs.conversations.ui.util.SettingsUtils;
 45
 46public class MemorizingActivity extends AppCompatActivity implements OnClickListener, OnCancelListener {
 47
 48	private final static Logger LOGGER = Logger.getLogger(MemorizingActivity.class.getName());
 49
 50	int decisionId;
 51
 52	AlertDialog dialog;
 53	
 54	@Override
 55	public void onCreate(Bundle savedInstanceState) {
 56		LOGGER.log(Level.FINE, "onCreate");
 57		super.onCreate(savedInstanceState);
 58	}
 59
 60	@Override
 61	public void onResume() {
 62		super.onResume();
 63		SettingsUtils.applyScreenshotSetting(this);
 64
 65		Intent i = getIntent();
 66		decisionId = i.getIntExtra(MemorizingTrustManager.DECISION_INTENT_ID, MTMDecision.DECISION_INVALID);
 67		int titleId = i.getIntExtra(MemorizingTrustManager.DECISION_TITLE_ID, R.string.mtm_accept_cert);
 68		String cert = i.getStringExtra(MemorizingTrustManager.DECISION_INTENT_CERT);
 69		LOGGER.log(Level.FINE, "onResume with " + i.getExtras() + " decId=" + decisionId + " data: " + i.getData());
 70		dialog = new MaterialAlertDialogBuilder(this).setTitle(titleId)
 71			.setMessage(cert)
 72			.setPositiveButton(R.string.always, this)
 73			.setNeutralButton(R.string.once, this)
 74			.setNegativeButton(R.string.cancel, this)
 75			.setOnCancelListener(this)
 76			.create();
 77		dialog.show();
 78	}
 79
 80	@Override
 81	protected void onPause() {
 82		if (dialog.isShowing())
 83			dialog.dismiss();
 84		super.onPause();
 85	}
 86
 87	void sendDecision(int decision) {
 88		LOGGER.log(Level.FINE, "Sending decision: " + decision);
 89		MemorizingTrustManager.interactResult(decisionId, decision);
 90		finish();
 91	}
 92
 93	// react on AlertDialog button press
 94	public void onClick(DialogInterface dialog, int btnId) {
 95		int decision;
 96		dialog.dismiss();
 97		switch (btnId) {
 98		case DialogInterface.BUTTON_POSITIVE:
 99			decision = MTMDecision.DECISION_ALWAYS;
100			break;
101		case DialogInterface.BUTTON_NEUTRAL:
102			decision = MTMDecision.DECISION_ONCE;
103			break;
104		default:
105			decision = MTMDecision.DECISION_ABORT;
106		}
107		sendDecision(decision);
108	}
109
110	public void onCancel(DialogInterface dialog) {
111		sendDecision(MTMDecision.DECISION_ABORT);
112	}
113}