MemorizingActivity.java

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