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.content.SharedPreferences;
 32import android.os.Bundle;
 33import android.preference.PreferenceManager;
 34import android.support.v7.app.AlertDialog;
 35import android.support.v7.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;
 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(findTheme());
 56		super.onCreate(savedInstanceState);
 57	}
 58
 59	@Override
 60	public void onResume() {
 61		super.onResume();
 62		Intent i = getIntent();
 63		decisionId = i.getIntExtra(MemorizingTrustManager.DECISION_INTENT_ID, MTMDecision.DECISION_INVALID);
 64		int titleId = i.getIntExtra(MemorizingTrustManager.DECISION_TITLE_ID, R.string.mtm_accept_cert);
 65		String cert = i.getStringExtra(MemorizingTrustManager.DECISION_INTENT_CERT);
 66		LOGGER.log(Level.FINE, "onResume with " + i.getExtras() + " decId=" + decisionId + " data: " + i.getData());
 67		dialog = new AlertDialog.Builder(this).setTitle(titleId)
 68			.setMessage(cert)
 69			.setPositiveButton(R.string.always, this)
 70			.setNeutralButton(R.string.once, this)
 71			.setNegativeButton(R.string.cancel, this)
 72			.setOnCancelListener(this)
 73			.create();
 74		dialog.show();
 75	}
 76
 77	@Override
 78	protected void onPause() {
 79		if (dialog.isShowing())
 80			dialog.dismiss();
 81		super.onPause();
 82	}
 83
 84	void sendDecision(int decision) {
 85		LOGGER.log(Level.FINE, "Sending decision: " + decision);
 86		MemorizingTrustManager.interactResult(decisionId, decision);
 87		finish();
 88	}
 89
 90	protected int findTheme() {
 91		return getPreferences().getString(SettingsActivity.THEME, getResources().getString(R.string.theme)).equals("dark") ? R.style.ConversationsTheme_Dark : R.style.ConversationsTheme;
 92	}
 93
 94	protected SharedPreferences getPreferences() {
 95		return PreferenceManager
 96				.getDefaultSharedPreferences(getApplicationContext());
 97	}
 98
 99	// react on AlertDialog button press
100	public void onClick(DialogInterface dialog, int btnId) {
101		int decision;
102		dialog.dismiss();
103		switch (btnId) {
104		case DialogInterface.BUTTON_POSITIVE:
105			decision = MTMDecision.DECISION_ALWAYS;
106			break;
107		case DialogInterface.BUTTON_NEUTRAL:
108			decision = MTMDecision.DECISION_ONCE;
109			break;
110		default:
111			decision = MTMDecision.DECISION_ABORT;
112		}
113		sendDecision(decision);
114	}
115
116	public void onCancel(DialogInterface dialog) {
117		sendDecision(MTMDecision.DECISION_ABORT);
118	}
119}