Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android-webviewapp/src/org/adblockplus/libadblockplus/webviewapp/MainActivity.java

Issue 29351744: Issue 4399 - Add WebView inheritor with ad blocking (Closed)
Patch Set: moved from experimental gradle plugin to standard one (with new ndk support) Created Oct. 7, 2016, 9:48 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.adblockplus.libadblockplus.webviewapp;
19
20 import org.adblockplus.android.AdblockWebView;
21
22 import android.app.Activity;
23 import android.graphics.Bitmap;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.view.inputmethod.InputMethodManager;
27 import android.webkit.WebChromeClient;
28 import android.webkit.WebView;
29 import android.webkit.WebViewClient;
30 import android.widget.Button;
31 import android.widget.CheckBox;
32 import android.widget.CompoundButton;
33 import android.widget.EditText;
34 import android.widget.ProgressBar;
35
36 public class MainActivity extends Activity
37 {
38 private static final boolean DEVELOPMENT_BUILD = true;
39
40 private ProgressBar progress;
41 private EditText url;
42 private Button ok;
43 private Button back;
44 private Button forward;
45 private CheckBox aa;
46 private AdblockWebView webView;
47
48 @Override
49 protected void onCreate(Bundle savedInstanceState)
50 {
51 super.onCreate(savedInstanceState);
52 setContentView(R.layout.activity_main);
53
54 bindControls();
55 initControls();
56 }
57
58 private void bindControls()
59 {
60 url = (EditText) findViewById(R.id.main_url);
61 ok = (Button) findViewById(R.id.main_ok);
62 back = (Button) findViewById(R.id.main_back);
63 forward = (Button) findViewById(R.id.main_forward);
64 aa = (CheckBox) findViewById(R.id.main_aa);
65 progress = (ProgressBar) findViewById(R.id.main_progress);
66 webView = (AdblockWebView) findViewById(R.id.main_webview);
67 }
68
69 private void setProgressVisible(boolean visible)
70 {
71 progress.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
72 }
73
74 private WebViewClient webViewClient = new WebViewClient()
75 {
76 @Override
77 public void onPageStarted(WebView view, String url, Bitmap favicon)
78 {
79 setProgressVisible(true);
80
81 // show updated URL (because of possible redirection)
82 MainActivity.this.url.setText(url);
83 }
84
85 @Override
86 public void onPageFinished(WebView view, String url)
87 {
88 setProgressVisible(false);
89 updateButtons();
90 }
91
92 @Override
93 public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
94 {
95 updateButtons();
96 }
97 };
98
99 private void updateButtons()
100 {
101 back.setEnabled(webView.canGoBack());
102 forward.setEnabled(webView.canGoForward());
103 }
104
105 private WebChromeClient webChromeClient = new WebChromeClient()
106 {
107 @Override
108 public void onProgressChanged(WebView view, int newProgress)
109 {
110 progress.setProgress(newProgress);
111 }
112 };
113
114 private void initControls()
115 {
116 ok.setOnClickListener(new View.OnClickListener()
117 {
118 @Override
119 public void onClick(View view)
120 {
121 loadUrl();
122 }
123 });
124
125 back.setOnClickListener(new View.OnClickListener()
126 {
127 @Override
128 public void onClick(View v)
129 {
130 loadPrev();
131 }
132 });
133
134 forward.setOnClickListener(new View.OnClickListener()
135 {
136 @Override
137 public void onClick(View v)
138 {
139 loadForward();
140 }
141 });
142
143 aa.setChecked(webView.isAcceptableAdsEnabled());
144 aa.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
145 {
146 @Override
147 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
148 {
149 enableAcceptableAds(isChecked);
150 }
151 });
152
153 setProgressVisible(false);
154 updateButtons();
155
156 // to get debug/warning log output
157 webView.setDebugMode(DEVELOPMENT_BUILD);
158
159 // render as fast as we can
160 webView.setAllowDrawDelay(0);
161
162 // to show that external WebViewClient is still working
163 webView.setWebViewClient(webViewClient);
164
165 // to show that external WebChromeClient is still working
166 webView.setWebChromeClient(webChromeClient);
167 }
168
169 private void enableAcceptableAds(boolean enabled)
170 {
171 webView.setAcceptableAdsEnabled(enabled);
172 }
173
174 private void hideSoftwareKeyboard()
175 {
176 InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_S ERVICE);
177 imm.hideSoftInputFromWindow(url.getWindowToken(), 0);
178 }
179
180 private void loadPrev()
181 {
182 hideSoftwareKeyboard();
183 if (webView.canGoBack())
184 {
185 webView.goBack();
186 }
187 }
188
189 private void loadForward()
190 {
191 hideSoftwareKeyboard();
192 if (webView.canGoForward())
193 {
194 webView.goForward();
195 }
196 }
197
198 private String prepareUrl(String url)
199 {
200 if (!url.startsWith("http"))
201 url = "http://" + url;
202
203 // make sure url is valid URL
204 return url;
205 }
206
207 private void loadUrl()
208 {
209 hideSoftwareKeyboard();
210 webView.loadUrl(prepareUrl(url.getText().toString()));
211 }
212
213 @Override
214 protected void onDestroy()
215 {
216 webView.dispose();
217
218 super.onDestroy();
219 }
220 }
OLDNEW
« no previous file with comments | « libadblockplus-android-webviewapp/res/values/strings.xml ('k') | libadblockplus-android/AndroidManifest.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld