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.webviewapp; |
| 19 |
| 20 import org.adblockplus.android.AdblockWebView; |
| 21 import org.adblockplus.libadblockplus.FilterEngine; |
| 22 import org.adblockplus.libadblockplus.Subscription; |
| 23 |
| 24 import android.app.Activity; |
| 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.CheckBox; |
| 34 import android.widget.CompoundButton; |
| 35 import android.widget.EditText; |
| 36 import android.widget.ProgressBar; |
| 37 import android.widget.Toast; |
| 38 |
| 39 public class MainActivity extends Activity |
| 40 { |
| 41 private static final boolean DEVELOPMENT_BUILD = true; |
| 42 |
| 43 private ProgressBar progress; |
| 44 private EditText url; |
| 45 private Button ok; |
| 46 private Button back; |
| 47 private Button forward; |
| 48 private CheckBox aa; |
| 49 private AdblockWebView webView; |
| 50 |
| 51 @Override |
| 52 protected void onCreate(Bundle savedInstanceState) |
| 53 { |
| 54 super.onCreate(savedInstanceState); |
| 55 setContentView(R.layout.activity_main); |
| 56 |
| 57 bindControls(); |
| 58 initControls(); |
| 59 } |
| 60 |
| 61 private void bindControls() |
| 62 { |
| 63 url = (EditText) findViewById(R.id.main_url); |
| 64 ok = (Button) findViewById(R.id.main_ok); |
| 65 back = (Button) findViewById(R.id.main_back); |
| 66 forward = (Button) findViewById(R.id.main_forward); |
| 67 aa = (CheckBox) findViewById(R.id.main_aa); |
| 68 progress = (ProgressBar) findViewById(R.id.main_progress); |
| 69 webView = (AdblockWebView) findViewById(R.id.main_webview); |
| 70 } |
| 71 |
| 72 private void setProgressVisible(boolean visible) |
| 73 { |
| 74 progress.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); |
| 75 } |
| 76 |
| 77 private WebViewClient webViewClient = new WebViewClient() |
| 78 { |
| 79 @Override |
| 80 public void onPageStarted(WebView view, String url, Bitmap favicon) |
| 81 { |
| 82 setProgressVisible(true); |
| 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 } |
OLD | NEW |