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 de.duenndns.ssl;
 25
 26
 27import java.util.logging.Level;
 28import java.util.logging.Logger;
 29
 30import android.app.Activity;
 31import android.app.AlertDialog;
 32import android.content.DialogInterface;
 33import android.content.DialogInterface.*;
 34import android.content.Intent;
 35import android.os.Bundle;
 36
 37public class MemorizingActivity extends Activity
 38		implements OnClickListener,OnCancelListener {
 39
 40	private final static Logger LOGGER = Logger.getLogger(MemorizingActivity.class.getName());
 41
 42	int decisionId;
 43
 44	AlertDialog dialog;
 45	
 46	@Override
 47	public void onCreate(Bundle savedInstanceState) {
 48		LOGGER.log(Level.FINE, "onCreate");
 49		super.onCreate(savedInstanceState);
 50	}
 51
 52	@Override
 53	public void onResume() {
 54		super.onResume();
 55		Intent i = getIntent();
 56		decisionId = i.getIntExtra(MemorizingTrustManager.DECISION_INTENT_ID, MTMDecision.DECISION_INVALID);
 57		int titleId = i.getIntExtra(MemorizingTrustManager.DECISION_TITLE_ID, R.string.mtm_accept_cert);
 58		String cert = i.getStringExtra(MemorizingTrustManager.DECISION_INTENT_CERT);
 59		LOGGER.log(Level.FINE, "onResume with " + i.getExtras() + " decId=" + decisionId + " data: " + i.getData());
 60		dialog = new AlertDialog.Builder(this).setTitle(titleId)
 61			.setMessage(cert)
 62			.setPositiveButton(R.string.mtm_decision_always, this)
 63			.setNeutralButton(R.string.mtm_decision_once, this)
 64			.setNegativeButton(R.string.mtm_decision_abort, this)
 65			.setOnCancelListener(this)
 66			.create();
 67		dialog.show();
 68	}
 69
 70	@Override
 71	protected void onPause() {
 72		if (dialog.isShowing())
 73			dialog.dismiss();
 74		super.onPause();
 75	}
 76
 77	void sendDecision(int decision) {
 78		LOGGER.log(Level.FINE, "Sending decision: " + decision);
 79		MemorizingTrustManager.interactResult(decisionId, decision);
 80		finish();
 81	}
 82
 83	// react on AlertDialog button press
 84	public void onClick(DialogInterface dialog, int btnId) {
 85		int decision;
 86		dialog.dismiss();
 87		switch (btnId) {
 88		case DialogInterface.BUTTON_POSITIVE:
 89			decision = MTMDecision.DECISION_ALWAYS;
 90			break;
 91		case DialogInterface.BUTTON_NEUTRAL:
 92			decision = MTMDecision.DECISION_ONCE;
 93			break;
 94		default:
 95			decision = MTMDecision.DECISION_ABORT;
 96		}
 97		sendDecision(decision);
 98	}
 99
100	public void onCancel(DialogInterface dialog) {
101		sendDecision(MTMDecision.DECISION_ABORT);
102	}
103}