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;
 32import android.preference.PreferenceManager;
 33
 34import androidx.appcompat.app.AlertDialog;
 35import androidx.appcompat.app.AppCompatActivity;
 36
 37import java.util.logging.Level;
 38import java.util.logging.Logger;
 39
 40import eu.siacs.conversations.R;
 41import eu.siacs.conversations.entities.MTMDecision;
 42import eu.siacs.conversations.services.MemorizingTrustManager;
 43import eu.siacs.conversations.ui.util.SettingsUtils;
 44import eu.siacs.conversations.utils.ThemeHelper;
 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		setTheme(ThemeHelper.find(this));
 58		super.onCreate(savedInstanceState);
 59		getLayoutInflater().inflate(R.layout.toolbar, findViewById(android.R.id.content));
 60		setSupportActionBar(findViewById(R.id.toolbar));
 61	}
 62
 63	@Override
 64	public void onResume() {
 65		super.onResume();
 66		SettingsUtils.applyScreenshotPreventionSetting(this);
 67
 68		Intent i = getIntent();
 69		decisionId = i.getIntExtra(MemorizingTrustManager.DECISION_INTENT_ID, MTMDecision.DECISION_INVALID);
 70		int titleId = i.getIntExtra(MemorizingTrustManager.DECISION_TITLE_ID, R.string.mtm_accept_cert);
 71		String cert = i.getStringExtra(MemorizingTrustManager.DECISION_INTENT_CERT);
 72		LOGGER.log(Level.FINE, "onResume with " + i.getExtras() + " decId=" + decisionId + " data: " + i.getData());
 73		dialog = new AlertDialog.Builder(this).setTitle(titleId)
 74			.setMessage(cert)
 75			.setPositiveButton(R.string.always, this)
 76			.setNeutralButton(R.string.once, this)
 77			.setNegativeButton(R.string.cancel, this)
 78			.setOnCancelListener(this)
 79			.create();
 80		dialog.show();
 81	}
 82
 83	@Override
 84	protected void onPause() {
 85		if (dialog.isShowing())
 86			dialog.dismiss();
 87		super.onPause();
 88	}
 89
 90	void sendDecision(int decision) {
 91		LOGGER.log(Level.FINE, "Sending decision: " + decision);
 92		MemorizingTrustManager.interactResult(decisionId, decision);
 93		finish();
 94	}
 95
 96	// react on AlertDialog button press
 97	public void onClick(DialogInterface dialog, int btnId) {
 98		int decision;
 99		dialog.dismiss();
100		switch (btnId) {
101		case DialogInterface.BUTTON_POSITIVE:
102			decision = MTMDecision.DECISION_ALWAYS;
103			break;
104		case DialogInterface.BUTTON_NEUTRAL:
105			decision = MTMDecision.DECISION_ONCE;
106			break;
107		default:
108			decision = MTMDecision.DECISION_ABORT;
109		}
110		sendDecision(decision);
111	}
112
113	public void onCancel(DialogInterface dialog) {
114		sendDecision(MTMDecision.DECISION_ABORT);
115	}
116}