• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.hotspot2.osu;
2 
3 import android.annotation.Nullable;
4 import android.app.Activity;
5 import android.graphics.Bitmap;
6 import android.net.ConnectivityManager;
7 import android.net.Network;
8 import android.net.http.SslError;
9 import android.os.Bundle;
10 import android.util.Log;
11 import android.util.TypedValue;
12 import android.webkit.SslErrorHandler;
13 import android.webkit.WebSettings;
14 import android.webkit.WebView;
15 import android.webkit.WebViewClient;
16 
17 import com.android.hotspot2.R;
18 
19 public class OSUWebView extends Activity {
20     public static final String OSU_URL = "com.android.hotspot2.osu.URL";
21     public static final String OSU_NETWORK = "com.android.hotspot2.osu.NETWORK";
22 
23     private String mUrl;
24 
25     @Override
onCreate(@ullable Bundle savedInstanceState)26     protected void onCreate(@Nullable Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         Log.d(OSUManager.TAG, "Opening OSU Web View");
29 
30         ConnectivityManager connectivityManager = ConnectivityManager.from(this);
31 
32         mUrl = getIntent().getStringExtra(OSU_URL);
33         Network network = getIntent().getParcelableExtra(OSU_NETWORK);
34         connectivityManager.bindProcessToNetwork(network);
35 
36         getActionBar().setDisplayShowHomeEnabled(false);
37         setContentView(R.layout.osu_web_view);
38         getActionBar().setDisplayShowHomeEnabled(false);
39 
40         final WebView myWebView = findViewById(R.id.webview);
41         myWebView.clearCache(true);
42         WebSettings webSettings = myWebView.getSettings();
43         webSettings.setJavaScriptEnabled(true);
44         MyWebViewClient mWebViewClient = new MyWebViewClient();
45         myWebView.setWebViewClient(mWebViewClient);
46         Log.d(OSUManager.TAG, "OSU Web View to " + mUrl);
47         myWebView.loadUrl(mUrl);
48         Log.d(OSUManager.TAG, "OSU Web View loading");
49         //myWebView.setWebChromeClient(new MyWebChromeClient());
50         // Start initial page load so WebView finishes loading proxy settings.
51         // Actual load of mUrl is initiated by MyWebViewClient.
52         //myWebView.loadData("", "text/html", null);
53     }
54 
55     private class MyWebViewClient extends WebViewClient {
56         private static final String INTERNAL_ASSETS = "file:///android_asset/";
57         // How many Android device-independent-pixels per scaled-pixel
58         // dp/sp = (px/sp) / (px/dp) = (1/sp) / (1/dp)
59         private final float mDpPerSp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 1,
60                 getResources().getDisplayMetrics()) /
61                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1,
62                         getResources().getDisplayMetrics());
63         private int mPagesLoaded;
64 
65         // If we haven't finished cleaning up the history, don't allow going back.
allowBack()66         public boolean allowBack() {
67             return mPagesLoaded > 1;
68         }
69 
70         // Convert Android device-independent-pixels (dp) to HTML size.
dp(int dp)71         private String dp(int dp) {
72             // HTML px's are scaled just like dp's, so just add "px" suffix.
73             return Integer.toString(dp) + "px";
74         }
75 
76         // Convert Android scaled-pixels (sp) to HTML size.
sp(int sp)77         private String sp(int sp) {
78             // Convert sp to dp's.
79             float dp = sp * mDpPerSp;
80             // Apply a scale factor to make things look right.
81             dp *= 1.3;
82             // Convert dp's to HTML size.
83             return dp((int)dp);
84         }
85 
86         @Override
onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)87         public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
88             Log.d(OSUManager.TAG, "TLS error in Web View: " + error);
89         }
90     }
91 }
92