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.webview.AdblockWebView; |
| 21 import org.adblockplus.libadblockplus.android.AdblockEngine; |
| 22 |
| 23 import android.app.Activity; |
| 24 import android.graphics.Bitmap; |
| 25 import android.os.Bundle; |
| 26 import android.view.View; |
| 27 import android.view.inputmethod.InputMethodManager; |
| 28 import android.webkit.WebChromeClient; |
| 29 import android.webkit.WebView; |
| 30 import android.webkit.WebViewClient; |
| 31 import android.widget.Button; |
| 32 import android.widget.CheckBox; |
| 33 import android.widget.CompoundButton; |
| 34 import android.widget.EditText; |
| 35 import android.widget.ProgressBar; |
| 36 |
| 37 public class MainActivity extends Activity |
| 38 { |
| 39 private static final boolean DEVELOPMENT_BUILD = true; |
| 40 |
| 41 // webView can create AdblockEngine instance itself if not passed with `webVie
w.setAdblockEngine()` |
| 42 private final boolean USE_EXTERNAL_ADBLOCKENGINE = true; |
| 43 |
| 44 private ProgressBar progress; |
| 45 private EditText url; |
| 46 private Button ok; |
| 47 private Button back; |
| 48 private Button forward; |
| 49 private CheckBox abpEnabled; |
| 50 private CheckBox aaEnabled; |
| 51 |
| 52 private AdblockEngine adblockEngine; |
| 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 abpEnabled = (CheckBox) findViewById(R.id.main_abp_enabled); |
| 72 aaEnabled = (CheckBox) findViewById(R.id.main_aa_enabled); |
| 73 progress = (ProgressBar) findViewById(R.id.main_progress); |
| 74 webView = (AdblockWebView) findViewById(R.id.main_webview); |
| 75 } |
| 76 |
| 77 private void setProgressVisible(boolean visible) |
| 78 { |
| 79 progress.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); |
| 80 } |
| 81 |
| 82 private WebViewClient webViewClient = new WebViewClient() |
| 83 { |
| 84 @Override |
| 85 public void onPageStarted(WebView view, String url, Bitmap favicon) |
| 86 { |
| 87 setProgressVisible(true); |
| 88 |
| 89 // show updated URL (because of possible redirection) |
| 90 MainActivity.this.url.setText(url); |
| 91 } |
| 92 |
| 93 @Override |
| 94 public void onPageFinished(WebView view, String url) |
| 95 { |
| 96 setProgressVisible(false); |
| 97 updateButtons(); |
| 98 |
| 99 if (!USE_EXTERNAL_ADBLOCKENGINE) |
| 100 { |
| 101 // as the page is finished internal adblockEngine is created and we can
get actual AA value |
| 102 aaEnabled.setChecked(webView.getAdblockEngine().isAcceptableAdsEnabled()
); |
| 103 } |
| 104 } |
| 105 |
| 106 @Override |
| 107 public void onReceivedError(WebView view, int errorCode, String description,
String failingUrl) |
| 108 { |
| 109 updateButtons(); |
| 110 } |
| 111 }; |
| 112 |
| 113 private void updateButtons() |
| 114 { |
| 115 back.setEnabled(webView.canGoBack()); |
| 116 forward.setEnabled(webView.canGoForward()); |
| 117 } |
| 118 |
| 119 private WebChromeClient webChromeClient = new WebChromeClient() |
| 120 { |
| 121 @Override |
| 122 public void onProgressChanged(WebView view, int newProgress) |
| 123 { |
| 124 progress.setProgress(newProgress); |
| 125 } |
| 126 }; |
| 127 |
| 128 private void initControls() |
| 129 { |
| 130 ok.setOnClickListener(new View.OnClickListener() |
| 131 { |
| 132 @Override |
| 133 public void onClick(View view) |
| 134 { |
| 135 loadUrl(); |
| 136 } |
| 137 }); |
| 138 |
| 139 back.setOnClickListener(new View.OnClickListener() |
| 140 { |
| 141 @Override |
| 142 public void onClick(View v) |
| 143 { |
| 144 loadPrev(); |
| 145 } |
| 146 }); |
| 147 |
| 148 forward.setOnClickListener(new View.OnClickListener() |
| 149 { |
| 150 @Override |
| 151 public void onClick(View v) |
| 152 { |
| 153 loadForward(); |
| 154 } |
| 155 }); |
| 156 |
| 157 initAdblockEngine(); |
| 158 initAbp(); |
| 159 initAcceptableAds(); |
| 160 |
| 161 setProgressVisible(false); |
| 162 updateButtons(); |
| 163 |
| 164 // to get debug/warning log output |
| 165 webView.setDebugMode(DEVELOPMENT_BUILD); |
| 166 |
| 167 // render as fast as we can |
| 168 webView.setAllowDrawDelay(0); |
| 169 |
| 170 // to show that external WebViewClient is still working |
| 171 webView.setWebViewClient(webViewClient); |
| 172 |
| 173 // to show that external WebChromeClient is still working |
| 174 webView.setWebChromeClient(webChromeClient); |
| 175 } |
| 176 |
| 177 private void initAdblockEngine() |
| 178 { |
| 179 if (USE_EXTERNAL_ADBLOCKENGINE) |
| 180 { |
| 181 adblockEngine = AdblockEngine.create( |
| 182 this, |
| 183 AdblockEngine.generateAppInfo(this, true), |
| 184 getCacheDir().getAbsolutePath(), |
| 185 true); |
| 186 webView.setAdblockEngine(adblockEngine); // external (activity-owned) adbl
ockEngine |
| 187 } |
| 188 } |
| 189 |
| 190 private void initAbp() |
| 191 { |
| 192 abpEnabled.setChecked(webView.isAdblockEnabled()); |
| 193 abpEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeList
ener() |
| 194 { |
| 195 @Override |
| 196 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) |
| 197 { |
| 198 webView.setAdblockEnabled(isChecked); |
| 199 } |
| 200 }); |
| 201 } |
| 202 |
| 203 private void initAcceptableAds() |
| 204 { |
| 205 if (USE_EXTERNAL_ADBLOCKENGINE) |
| 206 { |
| 207 // we can't set this checkbox if not using external engine as internal one
is not yet created |
| 208 // (it will be created during the first load) |
| 209 aaEnabled.setChecked(adblockEngine.isAcceptableAdsEnabled()); |
| 210 } |
| 211 aaEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListe
ner() |
| 212 { |
| 213 @Override |
| 214 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) |
| 215 { |
| 216 // not using this.adblockEngine as it can be internal webView engine |
| 217 webView.getAdblockEngine().setAcceptableAdsEnabled(isChecked); |
| 218 } |
| 219 }); |
| 220 } |
| 221 |
| 222 private void hideSoftwareKeyboard() |
| 223 { |
| 224 InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_S
ERVICE); |
| 225 imm.hideSoftInputFromWindow(url.getWindowToken(), 0); |
| 226 } |
| 227 |
| 228 private void loadPrev() |
| 229 { |
| 230 hideSoftwareKeyboard(); |
| 231 if (webView.canGoBack()) |
| 232 { |
| 233 webView.goBack(); |
| 234 } |
| 235 } |
| 236 |
| 237 private void loadForward() |
| 238 { |
| 239 hideSoftwareKeyboard(); |
| 240 if (webView.canGoForward()) |
| 241 { |
| 242 webView.goForward(); |
| 243 } |
| 244 } |
| 245 |
| 246 private String prepareUrl(String url) |
| 247 { |
| 248 if (!url.startsWith("http")) |
| 249 url = "http://" + url; |
| 250 |
| 251 // make sure url is valid URL |
| 252 return url; |
| 253 } |
| 254 |
| 255 private void loadUrl() |
| 256 { |
| 257 hideSoftwareKeyboard(); |
| 258 webView.loadUrl(prepareUrl(url.getText().toString())); |
| 259 } |
| 260 |
| 261 @Override |
| 262 protected void onDestroy() |
| 263 { |
| 264 webView.dispose(new Runnable() |
| 265 { |
| 266 @Override |
| 267 public void run() |
| 268 { |
| 269 if (USE_EXTERNAL_ADBLOCKENGINE) |
| 270 { |
| 271 adblockEngine.dispose(); |
| 272 } |
| 273 } |
| 274 }); |
| 275 super.onDestroy(); |
| 276 } |
| 277 } |
OLD | NEW |