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

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

Issue 29678581: Issue 6000 - Rename "libadblockplus-android" (Closed)
Patch Set: Created Jan. 24, 2018, 6:53 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-present 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.android.webviewapp;
19
20 import org.adblockplus.libadblockplus.android.settings.AdblockHelper;
21 import org.adblockplus.libadblockplus.android.webview.AdblockWebView;
22
23 import android.app.Activity;
24 import android.content.Intent;
25 import android.graphics.Bitmap;
26 import android.os.Bundle;
27 import android.view.View;
28 import android.view.inputmethod.InputMethodManager;
29 import android.webkit.WebChromeClient;
30 import android.webkit.WebView;
31 import android.webkit.WebViewClient;
32 import android.widget.Button;
33 import android.widget.EditText;
34 import android.widget.ProgressBar;
35
36 public class MainActivity extends Activity
37 {
38 public static final boolean DEVELOPMENT_BUILD = true;
39
40 // webView can create AdblockEngine instance itself if not passed with `webVie w.setProvider()`
41 public static final boolean USE_EXTERNAL_ADBLOCKENGINE = true;
42
43 private ProgressBar progress;
44 private EditText url;
45 private Button ok;
46 private Button back;
47 private Button forward;
48 private Button settings;
49
50 private AdblockWebView webView;
51
52 @Override
53 protected void onCreate(Bundle savedInstanceState)
54 {
55 super.onCreate(savedInstanceState);
56 setContentView(R.layout.activity_main);
57
58 bindControls();
59 initControls();
60
61 // allow WebView debugging in "Debug" build variant
62 // https://developers.google.com/web/tools/chrome-devtools/remote-debugging/ webviews
63 if (BuildConfig.DEBUG)
64 {
65 WebView.setWebContentsDebuggingEnabled(true);
66 }
67 }
68
69 private void bindControls()
70 {
71 url = (EditText) findViewById(R.id.main_url);
72 ok = (Button) findViewById(R.id.main_ok);
73 back = (Button) findViewById(R.id.main_back);
74 forward = (Button) findViewById(R.id.main_forward);
75 settings = (Button) findViewById(R.id.main_settings);
76 progress = (ProgressBar) findViewById(R.id.main_progress);
77 webView = (AdblockWebView) findViewById(R.id.main_webview);
78 }
79
80 private void setProgressVisible(boolean visible)
81 {
82 progress.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
83 }
84
85 private WebViewClient webViewClient = new WebViewClient()
86 {
87 @Override
88 public void onPageStarted(WebView view, String url, Bitmap favicon)
89 {
90 setProgressVisible(true);
91
92 // show updated URL (because of possible redirection)
93 MainActivity.this.url.setText(url);
94 }
95
96 @Override
97 public void onPageFinished(WebView view, String url)
98 {
99 setProgressVisible(false);
100 updateButtons();
101 }
102
103 @Override
104 public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
105 {
106 updateButtons();
107 }
108 };
109
110 private void updateButtons()
111 {
112 back.setEnabled(webView.canGoBack());
113 forward.setEnabled(webView.canGoForward());
114 }
115
116 private WebChromeClient webChromeClient = new WebChromeClient()
117 {
118 @Override
119 public void onProgressChanged(WebView view, int newProgress)
120 {
121 progress.setProgress(newProgress);
122 }
123 };
124
125 private void initControls()
126 {
127 ok.setOnClickListener(new View.OnClickListener()
128 {
129 @Override
130 public void onClick(View view)
131 {
132 loadUrl();
133 }
134 });
135
136 back.setOnClickListener(new View.OnClickListener()
137 {
138 @Override
139 public void onClick(View v)
140 {
141 loadPrev();
142 }
143 });
144
145 forward.setOnClickListener(new View.OnClickListener()
146 {
147 @Override
148 public void onClick(View v)
149 {
150 loadForward();
151 }
152 });
153
154 if (USE_EXTERNAL_ADBLOCKENGINE)
155 {
156 settings.setOnClickListener(new View.OnClickListener()
157 {
158 @Override
159 public void onClick(View v)
160 {
161 navigateSettings();
162 }
163 });
164 }
165 else
166 {
167 /*
168 We're able to show settings if we're using AdblockHelper facade only.
169 Otherwise pass AdblockEngine instance to the fragments and not it's neithe r Serializable nor Parcelable.
170 */
171 settings.setVisibility(View.GONE);
172 }
173
174 initAdblockWebView();
175
176 setProgressVisible(false);
177 updateButtons();
178
179 // to get debug/warning log output
180 webView.setDebugMode(DEVELOPMENT_BUILD);
181
182 // render as fast as we can
183 webView.setAllowDrawDelay(0);
184
185 // to show that external WebViewClient is still working
186 webView.setWebViewClient(webViewClient);
187
188 // to show that external WebChromeClient is still working
189 webView.setWebChromeClient(webChromeClient);
190 }
191
192 private void navigateSettings()
193 {
194 startActivity(new Intent(this, SettingsActivity.class));
195 }
196
197 private void initAdblockWebView()
198 {
199 if (USE_EXTERNAL_ADBLOCKENGINE)
200 {
201 // external AdblockEngine
202 webView.setProvider(AdblockHelper.get().getProvider());
203 }
204 else
205 {
206 // AdblockWebView will create internal AdblockEngine instance
207 }
208 }
209
210 private void hideSoftwareKeyboard()
211 {
212 InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_S ERVICE);
213 imm.hideSoftInputFromWindow(url.getWindowToken(), 0);
214 }
215
216 private void loadPrev()
217 {
218 hideSoftwareKeyboard();
219 if (webView.canGoBack())
220 {
221 webView.goBack();
222 }
223 }
224
225 private void loadForward()
226 {
227 hideSoftwareKeyboard();
228 if (webView.canGoForward())
229 {
230 webView.goForward();
231 }
232 }
233
234 private String prepareUrl(String url)
235 {
236 if (!url.startsWith("http"))
237 url = "http://" + url;
238
239 // make sure url is valid URL
240 return url;
241 }
242
243 private void loadUrl()
244 {
245 hideSoftwareKeyboard();
246 webView.loadUrl(prepareUrl(url.getText().toString()));
247 }
248
249 @Override
250 protected void onDestroy()
251 {
252 webView.dispose(null);
253
254 super.onDestroy();
255 }
256 }
OLDNEW

Powered by Google App Engine
This is Rietveld