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