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