1 /*
2  * wpadebug - wpa_supplicant and Wi-Fi debugging app for Android
3  * Copyright (c) 2013, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 package w1.fi.wpadebug;
10 
11 import android.app.Activity;
12 import android.app.AlertDialog;
13 import android.content.DialogInterface;
14 import android.content.Intent;
15 import android.content.res.Configuration;
16 import android.net.http.SslError;
17 import android.os.Bundle;
18 import android.util.Log;
19 import android.view.Window;
20 import android.webkit.SslErrorHandler;
21 import android.webkit.WebChromeClient;
22 import android.webkit.WebView;
23 import android.webkit.WebViewClient;
24 import android.widget.Toast;
25 
26 public class WpaWebViewActivity extends Activity
27 {
28     private static final String TAG = "wpadebug";
29     private static final String EXTRA_MESSAGE = "w1.fi.wpadebug.URL";
30     private WebView mWebView;
31     final Activity activity = this;
32 
33     @Override
onCreate(Bundle savedInstanceState)34     public void onCreate(Bundle savedInstanceState)
35     {
36 	Log.d(TAG, "WpaWebViewActivity::onCreate");
37         super.onCreate(savedInstanceState);
38 
39 	Intent intent = getIntent();
40 	String url = intent.getStringExtra(EXTRA_MESSAGE);
41 	Log.d(TAG, "url=" + url);
42 	if (url.equals("FINISH")) {
43 	    finish();
44 	    return;
45 	}
46 
47 	mWebView = new WebView(this);
48 	mWebView.getSettings().setJavaScriptEnabled(true);
49 	mWebView.setWebViewClient(new WpaWebViewClient());
50 
51 	getWindow().requestFeature(Window.FEATURE_PROGRESS);
52 
53 	mWebView.setWebChromeClient(new WebChromeClient()
54 	    {
55 		public void onProgressChanged(WebView view, int progress)
56 		{
57 		    Log.d(TAG, "progress=" + progress);
58 		    activity.setProgress(progress * 1000);
59 		}
60 	    });
61 
62         setContentView(mWebView);
63 
64 	mWebView.loadUrl(url);
65     }
66 
67     @Override
onResume()68     public void onResume()
69     {
70 	Log.d(TAG, "WpaWebViewActivity::onResume");
71         super.onResume();
72     }
73 
74     @Override
onNewIntent(Intent intent)75     protected void onNewIntent(Intent intent)
76     {
77 	Log.d(TAG, "WpaWebViewActivity::onNewIntent");
78 	super.onNewIntent(intent);
79 	String url = intent.getStringExtra(EXTRA_MESSAGE);
80 	Log.d(TAG, "url=" + url);
81 	setIntent(intent);
82 	if (url.equals("FINISH")) {
83 	    finish();
84 	    return;
85 	}
86 	mWebView.loadUrl(url);
87     }
88 
89     private class WpaWebViewClient extends WebViewClient {
90 	@Override
shouldOverrideUrlLoading(WebView view, String url)91 	public boolean shouldOverrideUrlLoading(WebView view, String url)
92 	{
93 	    Log.d(TAG, "shouldOverrideUrlLoading: url=" + url);
94 	    Intent intent = getIntent();
95 	    intent.putExtra(EXTRA_MESSAGE, url);
96 
97 	    view.loadUrl(url);
98 	    return true;
99 	}
100 
101 	@Override
onPageFinished(WebView view, String url)102 	public void onPageFinished(WebView view, String url)
103 	{
104 	    Log.d(TAG, "onPageFinished: url=" + url);
105 	}
106 
onReceivedError(WebView view, int errorCode, String description, String failingUrl)107 	public void onReceivedError(WebView view, int errorCode,
108 				    String description, String failingUrl)
109 	{
110 	    Log.d(TAG, "Failed to load page: errorCode=" +
111 		  errorCode + " description=" + description +
112 		  " URL=" + failingUrl);
113 	    Toast.makeText(activity, "Failed to load page: " +
114 			   description + " (URL=" + failingUrl + ")",
115 			   Toast.LENGTH_LONG).show();
116 	}
117 
118 	@Override
onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)119 	public void onReceivedSslError(WebView view, SslErrorHandler handler,
120 				       SslError error)
121 	{
122 	    Log.d(TAG, "SSL error: " + error);
123 
124 	    final SslErrorHandler h = handler;
125 	    AlertDialog.Builder alert = new AlertDialog.Builder(activity);
126 	    alert.setTitle("SSL error - Continue?");
127 	    alert.setMessage(error.toString())
128 		.setCancelable(false)
129 		.setPositiveButton("Yes", new DialogInterface.OnClickListener()
130 		    {
131 			public void onClick(DialogInterface dialog, int id)
132 			{
133 			    h.proceed();
134 			}
135 		    })
136 		.setNegativeButton("No", new DialogInterface.OnClickListener()
137 		    {
138 			public void onClick(DialogInterface dialog, int id)
139 			{
140 			    h.cancel();
141 			}
142 		    });
143 	    alert.show();
144 	}
145     }
146 }
147