1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.browser;
17 
18 import android.content.Context;
19 import android.content.pm.PackageManager;
20 import android.util.AttributeSet;
21 import android.view.View;
22 import android.webkit.CookieManager;
23 import android.webkit.WebView;
24 
25 /**
26  * Web view factory class for creating {@link BrowserWebView}'s.
27  */
28 public class BrowserWebViewFactory implements WebViewFactory {
29 
30     private final Context mContext;
31 
BrowserWebViewFactory(Context context)32     public BrowserWebViewFactory(Context context) {
33         mContext = context;
34     }
35 
instantiateWebView(AttributeSet attrs, int defStyle, boolean privateBrowsing)36     protected WebView instantiateWebView(AttributeSet attrs, int defStyle,
37             boolean privateBrowsing) {
38         return new BrowserWebView(mContext, attrs, defStyle, privateBrowsing);
39     }
40 
41     @Override
createSubWebView(boolean privateBrowsing)42     public WebView createSubWebView(boolean privateBrowsing) {
43         return createWebView(privateBrowsing);
44     }
45 
46     @Override
createWebView(boolean privateBrowsing)47     public WebView createWebView(boolean privateBrowsing) {
48         WebView w = instantiateWebView(null, android.R.attr.webViewStyle, privateBrowsing);
49         initWebViewSettings(w);
50         return w;
51     }
52 
initWebViewSettings(WebView w)53     protected void initWebViewSettings(WebView w) {
54         w.setScrollbarFadingEnabled(true);
55         w.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
56         w.setMapTrackballToArrowKeys(false); // use trackball directly
57         // Enable the built-in zoom
58         w.getSettings().setBuiltInZoomControls(true);
59         final PackageManager pm = mContext.getPackageManager();
60         boolean supportsMultiTouch =
61                 pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH)
62                 || pm.hasSystemFeature(PackageManager.FEATURE_FAKETOUCH_MULTITOUCH_DISTINCT);
63         w.getSettings().setDisplayZoomControls(!supportsMultiTouch);
64 
65         // Add this WebView to the settings observer list and update the
66         // settings
67         final BrowserSettings s = BrowserSettings.getInstance();
68         s.startManagingSettings(w.getSettings());
69 
70         CookieManager cookieManager = CookieManager.getInstance();
71         cookieManager.setAcceptThirdPartyCookies(w, cookieManager.acceptCookie());
72 
73         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
74             // Remote Web Debugging is always enabled, where available.
75             WebView.setWebContentsDebuggingEnabled(true);
76         }
77     }
78 
79 }
80