OLD | NEW |
(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.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.setAdblockEngine()` |
| 41 public static final boolean USE_EXTERNAL_ADBLOCKENGINE = true; |
| 42 |
| 43 // adblock retain() may be long-running, pass `true` to do it in background th
read |
| 44 public static final boolean ADBLOCKENGINE_RETAIN_ASYNC = true; |
| 45 |
| 46 private ProgressBar progress; |
| 47 private EditText url; |
| 48 private Button ok; |
| 49 private Button back; |
| 50 private Button forward; |
| 51 private Button settings; |
| 52 |
| 53 private AdblockWebView webView; |
| 54 |
| 55 @Override |
| 56 protected void onCreate(Bundle savedInstanceState) |
| 57 { |
| 58 super.onCreate(savedInstanceState); |
| 59 setContentView(R.layout.activity_main); |
| 60 |
| 61 bindControls(); |
| 62 initControls(); |
| 63 } |
| 64 |
| 65 private void bindControls() |
| 66 { |
| 67 url = (EditText) findViewById(R.id.main_url); |
| 68 ok = (Button) findViewById(R.id.main_ok); |
| 69 back = (Button) findViewById(R.id.main_back); |
| 70 forward = (Button) findViewById(R.id.main_forward); |
| 71 settings = (Button) findViewById(R.id.main_settings); |
| 72 progress = (ProgressBar) findViewById(R.id.main_progress); |
| 73 webView = (AdblockWebView) findViewById(R.id.main_webview); |
| 74 } |
| 75 |
| 76 private void setProgressVisible(boolean visible) |
| 77 { |
| 78 progress.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); |
| 79 } |
| 80 |
| 81 private WebViewClient webViewClient = new WebViewClient() |
| 82 { |
| 83 @Override |
| 84 public void onPageStarted(WebView view, String url, Bitmap favicon) |
| 85 { |
| 86 setProgressVisible(true); |
| 87 |
| 88 // show updated URL (because of possible redirection) |
| 89 MainActivity.this.url.setText(url); |
| 90 } |
| 91 |
| 92 @Override |
| 93 public void onPageFinished(WebView view, String url) |
| 94 { |
| 95 setProgressVisible(false); |
| 96 updateButtons(); |
| 97 } |
| 98 |
| 99 @Override |
| 100 public void onReceivedError(WebView view, int errorCode, String description,
String failingUrl) |
| 101 { |
| 102 updateButtons(); |
| 103 } |
| 104 }; |
| 105 |
| 106 private void updateButtons() |
| 107 { |
| 108 back.setEnabled(webView.canGoBack()); |
| 109 forward.setEnabled(webView.canGoForward()); |
| 110 } |
| 111 |
| 112 private WebChromeClient webChromeClient = new WebChromeClient() |
| 113 { |
| 114 @Override |
| 115 public void onProgressChanged(WebView view, int newProgress) |
| 116 { |
| 117 progress.setProgress(newProgress); |
| 118 } |
| 119 }; |
| 120 |
| 121 private void initControls() |
| 122 { |
| 123 ok.setOnClickListener(new View.OnClickListener() |
| 124 { |
| 125 @Override |
| 126 public void onClick(View view) |
| 127 { |
| 128 loadUrl(); |
| 129 } |
| 130 }); |
| 131 |
| 132 back.setOnClickListener(new View.OnClickListener() |
| 133 { |
| 134 @Override |
| 135 public void onClick(View v) |
| 136 { |
| 137 loadPrev(); |
| 138 } |
| 139 }); |
| 140 |
| 141 forward.setOnClickListener(new View.OnClickListener() |
| 142 { |
| 143 @Override |
| 144 public void onClick(View v) |
| 145 { |
| 146 loadForward(); |
| 147 } |
| 148 }); |
| 149 |
| 150 if (USE_EXTERNAL_ADBLOCKENGINE) |
| 151 { |
| 152 settings.setOnClickListener(new View.OnClickListener() |
| 153 { |
| 154 @Override |
| 155 public void onClick(View v) |
| 156 { |
| 157 navigateSettings(); |
| 158 } |
| 159 }); |
| 160 } |
| 161 else |
| 162 { |
| 163 /* |
| 164 We're able to show settings if we're using AdblockHelper facade only. |
| 165 Otherwise pass AdblockEngine instance to the fragments and not it's neithe
r Serializable nor Parcelable. |
| 166 */ |
| 167 settings.setVisibility(View.GONE); |
| 168 } |
| 169 |
| 170 initAdblockWebView(); |
| 171 |
| 172 setProgressVisible(false); |
| 173 updateButtons(); |
| 174 |
| 175 // to get debug/warning log output |
| 176 webView.setDebugMode(DEVELOPMENT_BUILD); |
| 177 |
| 178 // render as fast as we can |
| 179 webView.setAllowDrawDelay(0); |
| 180 |
| 181 // to show that external WebViewClient is still working |
| 182 webView.setWebViewClient(webViewClient); |
| 183 |
| 184 // to show that external WebChromeClient is still working |
| 185 webView.setWebChromeClient(webChromeClient); |
| 186 } |
| 187 |
| 188 private void navigateSettings() |
| 189 { |
| 190 startActivity(new Intent(this, SettingsActivity.class)); |
| 191 } |
| 192 |
| 193 private void initAdblockWebView() |
| 194 { |
| 195 if (USE_EXTERNAL_ADBLOCKENGINE) |
| 196 { |
| 197 // external adblockEngine |
| 198 AdblockHelper.get().retain(ADBLOCKENGINE_RETAIN_ASYNC); |
| 199 |
| 200 if (!ADBLOCKENGINE_RETAIN_ASYNC) |
| 201 { |
| 202 webView.setAdblockEngine(AdblockHelper.get().getEngine()); |
| 203 } |
| 204 } |
| 205 else |
| 206 { |
| 207 // AdblockWebView will create internal AdblockEngine instance |
| 208 } |
| 209 } |
| 210 |
| 211 private void hideSoftwareKeyboard() |
| 212 { |
| 213 InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_S
ERVICE); |
| 214 imm.hideSoftInputFromWindow(url.getWindowToken(), 0); |
| 215 } |
| 216 |
| 217 private void loadPrev() |
| 218 { |
| 219 hideSoftwareKeyboard(); |
| 220 if (webView.canGoBack()) |
| 221 { |
| 222 webView.goBack(); |
| 223 } |
| 224 } |
| 225 |
| 226 private void loadForward() |
| 227 { |
| 228 hideSoftwareKeyboard(); |
| 229 if (webView.canGoForward()) |
| 230 { |
| 231 webView.goForward(); |
| 232 } |
| 233 } |
| 234 |
| 235 private String prepareUrl(String url) |
| 236 { |
| 237 if (!url.startsWith("http")) |
| 238 url = "http://" + url; |
| 239 |
| 240 // make sure url is valid URL |
| 241 return url; |
| 242 } |
| 243 |
| 244 private void loadUrl() |
| 245 { |
| 246 hideSoftwareKeyboard(); |
| 247 |
| 248 // if retained with `true` we need to make sure it's ready now |
| 249 if (USE_EXTERNAL_ADBLOCKENGINE && ADBLOCKENGINE_RETAIN_ASYNC) |
| 250 { |
| 251 AdblockHelper.get().waitForReady(); |
| 252 webView.setAdblockEngine(AdblockHelper.get().getEngine()); |
| 253 } |
| 254 webView.loadUrl(prepareUrl(url.getText().toString())); |
| 255 } |
| 256 |
| 257 @Override |
| 258 protected void onDestroy() |
| 259 { |
| 260 webView.dispose(new Runnable() |
| 261 { |
| 262 @Override |
| 263 public void run() |
| 264 { |
| 265 if (USE_EXTERNAL_ADBLOCKENGINE) |
| 266 { |
| 267 AdblockHelper.get().release(); |
| 268 } |
| 269 } |
| 270 }); |
| 271 |
| 272 super.onDestroy(); |
| 273 } |
| 274 } |
OLD | NEW |