summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/org/lineageos/jelly/webview/WebViewExt.java
blob: bc88c22fb012bceeb9ee3e30a3c097c3e520cf5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright (C) 2017 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.lineageos.jelly.webview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.util.ArrayMap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ProgressBar;

import org.lineageos.jelly.utils.PrefsUtils;
import org.lineageos.jelly.utils.UrlUtils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WebViewExt extends WebView {

    private static final String TAG = "WebViewExt";

    private static final String DESKTOP_DEVICE = "X11; Linux x86_64";
    private static final String DESKTOP_USER_AGENT_FALLBACK =
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36";

    private WebViewExtActivity mActivity;

    private String mMobileUserAgent;
    private String mDesktopUserAgent;

    private boolean mIncognito;
    private boolean mDesktopMode;
    private String mLastLoadedUrl;

    private final Map<String, String> mRequestHeaders = new ArrayMap<>();
    private static final String HEADER_DNT = "DNT";

    public WebViewExt(Context context) {
        super(context);
    }

    public WebViewExt(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WebViewExt(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void loadUrl(String url) {
        mLastLoadedUrl = url;
        followUrl(url);
    }

    void followUrl(String url) {
        String fixedUrl = UrlUtils.smartUrlFilter(url);
        if (fixedUrl != null) {
            super.loadUrl(fixedUrl, mRequestHeaders);
            return;
        }

        String templateUri = PrefsUtils.getSearchEngine(mActivity);
        fixedUrl = UrlUtils.getFormattedUri(templateUri, url);
        if (fixedUrl != null) {
            super.loadUrl(fixedUrl, mRequestHeaders);
        }
    }

    public String getLastLoadedUrl() {
        return mLastLoadedUrl;
    }

    private void setup() {
        getSettings().setJavaScriptEnabled(PrefsUtils.getJavascript(mActivity));
        getSettings().setJavaScriptCanOpenWindowsAutomatically(PrefsUtils.getJavascript(mActivity));
        getSettings().setGeolocationEnabled(PrefsUtils.getLocation(mActivity));
        getSettings().setSaveFormData(!mIncognito && PrefsUtils.getSaveFormData(mActivity));
        getSettings().setBuiltInZoomControls(true);
        getSettings().setDisplayZoomControls(false);
        getSettings().setDomStorageEnabled(true);

        setWebViewClient(new WebClient());

        setOnLongClickListener(new OnLongClickListener() {
            boolean shouldAllowDownload;

            @Override
            public boolean onLongClick(View v) {
                HitTestResult result = getHitTestResult();
                switch (result.getType()) {
                    case HitTestResult.IMAGE_TYPE:
                    case HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
                        shouldAllowDownload = true;
                    case HitTestResult.SRC_ANCHOR_TYPE:
                        mActivity.showSheetMenu(result.getExtra(), shouldAllowDownload);
                        shouldAllowDownload = false;
                        return true;
                }
                return false;
            }
        });

        setDownloadListener((url, userAgent, contentDescription, mimeType, contentLength) ->
                mActivity.downloadFileAsk(url, contentDescription, mimeType));

        // Mobile: Remove "wv" from the WebView's user agent. Some websites don't work
        // properly if the browser reports itself as a simple WebView.
        // Desktop: Generate the desktop user agent starting from the mobile one so that
        // we always report the current engine version.
        Pattern pattern = Pattern.compile("([^)]+ \\()([^)]+)(\\) .*)");
        Matcher matcher = pattern.matcher(getSettings().getUserAgentString());
        if (matcher.matches()) {
            String mobileDevice = matcher.group(2).replace("; wv", "");
            mMobileUserAgent = matcher.group(1) + mobileDevice + matcher.group(3);
            mDesktopUserAgent = matcher.group(1) + DESKTOP_DEVICE + matcher.group(3)
                    .replace(" Mobile ", " ");
            getSettings().setUserAgentString(mMobileUserAgent);
        } else {
            Log.e(TAG, "Couldn't parse the user agent");
            mMobileUserAgent = getSettings().getUserAgentString();
            mDesktopUserAgent = DESKTOP_USER_AGENT_FALLBACK;
        }

        if (PrefsUtils.getDoNotTrack(mActivity)) {
            mRequestHeaders.put(HEADER_DNT, "1");
        }
    }

    public void init(WebViewExtActivity activity, EditText editText,
                     ProgressBar progressBar, boolean incognito) {
        mActivity = activity;
        mIncognito = incognito;
        ChromeClient chromeClient = new ChromeClient(activity, incognito);
        chromeClient.bindEditText(editText);
        chromeClient.bindProgressBar(progressBar);
        setWebChromeClient(chromeClient);
        setup();
    }

    public Bitmap getSnap() {
        measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
        setDrawingCacheEnabled(true);
        buildDrawingCache();
        int size = getMeasuredWidth() > getMeasuredHeight() ?
                getMeasuredHeight() : getMeasuredWidth();
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        int height = bitmap.getHeight();
        canvas.drawBitmap(bitmap, 0, height, paint);
        draw(canvas);
        return bitmap;
    }

    public boolean isIncognito() {
        return mIncognito;
    }

    public void setDesktopMode(boolean desktopMode) {
        mDesktopMode = desktopMode;
        WebSettings settings = getSettings();
        settings.setUserAgentString(desktopMode ? mDesktopUserAgent : mMobileUserAgent);
        settings.setUseWideViewPort(desktopMode);
        settings.setLoadWithOverviewMode(desktopMode);
        reload();
    }

    public boolean isDesktopMode() {
        return mDesktopMode;
    }

    Map<String, String> getRequestHeaders() {
        return mRequestHeaders;
    }
}