-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMainActivity.java
More file actions
419 lines (370 loc) · 18.6 KB
/
MainActivity.java
File metadata and controls
419 lines (370 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package com.webview.myapplication;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.SslErrorHandler;
import android.webkit.URLUtil;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
class AdBlockerUtil {
private final HashSet<String> adHosts = new HashSet<>();
private static AdBlockerUtil mInstance;
public static AdBlockerUtil getInstance() {
if (mInstance == null) {
mInstance = new AdBlockerUtil();
}
return mInstance;
}
public void initialize(Context context) {
try {
InputStream inputStream = context.getAssets().open("adblock.txt");
loadHostsFromInputStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isAd(String url) {
if (url == null) {
return false;
}
String requestHost = Uri.parse(url).getHost();
if (requestHost == null) {
return false;
}
if (adHosts.contains(requestHost)) {
return true;
}
String[] subDomains = requestHost.split("\\.");
for (int i = subDomains.length - 1; i >= 0; i--) {
String domain = String.join(".", Arrays.copyOfRange(subDomains, i, subDomains.length));
if (adHosts.contains(domain)) {
return true;
}
}
return false;
}
private void loadHostsFromInputStream(InputStream inputStream) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
adHosts.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class MainActivity extends Activity {
private final int STORAGE_PERMISSION_CODE = 1;
private static final String[] ALLOWED_DOMAINS = BuildConfig.ALLOWED_DOMAINS.split(",");
private static final String STARTUP_URL = BuildConfig.STARTUP_URL;
private static final String VIEW_MODE = BuildConfig.VIEW_MODE;
private static final boolean BLOCK_MEDIA = BuildConfig.BLOCK_MEDIA;
private static final boolean BLOCK_ADS = BuildConfig.BLOCK_ADS;
private static final boolean NO_SSL = BuildConfig.NO_SSL;
private static final boolean HIDE_NETFREE = BuildConfig.HIDE_NETFREE;
private static final boolean ALLOW_GOOGLE_LOGIN = BuildConfig.ALLOW_GOOGLE_LOGIN;
private static final String CHROME_USER_AGENT = "Mozilla/5.0 (Linux; Android 12; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Mobile Safari/537.36";
private String defaultUserAgent;
private WebView mWebView;
private View mCustomView;
private CustomViewCallback mCustomViewCallback;
private ProgressBar mProgressBar;
private void requestStoragePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(this)
.setTitle("Permission needed")
.setMessage("This permission is needed to download files")
.setPositiveButton("ok", (dialog, which) -> ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE))
.setNegativeButton("cancel", (dialog, which) -> dialog.dismiss())
.create().show();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage && null == mFilePathCallback) return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
if (mFilePathCallback != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mFilePathCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
} else {
mFilePathCallback.onReceiveValue(result != null ? new Uri[]{result} : null);
}
mFilePathCallback = null;
} else if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
}
private ValueCallback<Uri> mUploadMessage;
private ValueCallback<Uri[]> mFilePathCallback;
private final static int FILECHOOSER_RESULTCODE = 1;
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
@SuppressLint("SetJavaScriptEnabled")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdBlockerUtil adBlockerUtil = AdBlockerUtil.getInstance();
adBlockerUtil.initialize(this);
if (VIEW_MODE.equals("PORTRAIT")) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (VIEW_MODE.equals("LANDSCAPE")) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.activity_main_webview);
mProgressBar = findViewById(R.id.progressBar);
WebSettings webSettings = mWebView.getSettings();
defaultUserAgent = webSettings.getUserAgentString();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setPluginState(PluginState.ON);
webSettings.setAllowFileAccess(false);
if (BLOCK_MEDIA) {
webSettings.setLoadsImagesAutomatically(false);
}
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
mWebView.setVisibility(View.GONE);
mCustomViewCallback = callback;
((FrameLayout) getWindow().getDecorView()).addView(mCustomView);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
@Override
public void onHideCustomView() {
((FrameLayout) getWindow().getDecorView()).removeView(mCustomView);
mCustomView = null;
mWebView.setVisibility(View.VISIBLE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
mCustomViewCallback.onCustomViewHidden();
mCustomViewCallback = null;
if (VIEW_MODE == "PORTRAIT") {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (VIEW_MODE == "LANDSCAPE") {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
@Override
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
mFilePathCallback = filePathCallback;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
if (fileChooserParams.getAcceptTypes().length > 0 && !Objects.equals(fileChooserParams.getAcceptTypes()[0], "")) {
intent.setType(fileChooserParams.getAcceptTypes()[0]);
} else {
intent.setType("*/*");
}
startActivityForResult(intent, FILECHOOSER_RESULTCODE);
return true;
}
// support android < 5.0
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
if (acceptType != null && !acceptType.isEmpty()) {
i.setType(acceptType);
} else {
i.setType("*/*");
}
startActivityForResult(Intent.createChooser(i, "Choice File"), FILECHOOSER_RESULTCODE);
}
});
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setDownloadListener((url, userAgent, contentDisposition, mimeType, contentLength) -> {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q && ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestStoragePermission();
}
Uri source = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(source);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading File...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File...", Toast.LENGTH_LONG).show();
});
mWebView.loadUrl(STARTUP_URL.isEmpty() ? "https://" + ALLOWED_DOMAINS[0] : STARTUP_URL);
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
String host = Uri.parse(url).getHost();
boolean isAllowed = false;
if (url != null && url.startsWith("whatsapp://")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
view.getContext().startActivity(intent);
return true;
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error opening whatsapp, the application may not be installed", Toast.LENGTH_LONG).show();
return true;
}
}
if (url.startsWith("tel:")) {
Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(tel);
return true;
}
if (url.contains("mailto:")){
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
if (host == null) {
isAllowed = true; // mailto: links
}
if (!STARTUP_URL.isEmpty() && url.equals(STARTUP_URL)) {
isAllowed = true;
}
for (String domain : ALLOWED_DOMAINS) {
if (host == null || host.equals(domain) || host.equals("www." + domain) || host.equals("m." + domain)) {
isAllowed = true;
break;
}
}
if (isAllowed) {
boolean isGoogleLogin = host != null && (host.equals("accounts.google.com") || host.equals("www.accounts.google.com"));
if (isGoogleLogin && ALLOW_GOOGLE_LOGIN) {
view.getSettings().setUserAgentString(CHROME_USER_AGENT);
SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE);
boolean warningAccepted = prefs.getBoolean("google_login_warning_accepted", false);
if (!warningAccepted) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("אזהרה")
.setMessage("גוגל אוסרת התחברות מדפדפנים מותאמים אישית. השימוש על אחריותך בלבד.")
.setPositiveButton("הבנתי", (dialog, which) -> {
prefs.edit().putBoolean("google_login_warning_accepted", true).apply();
mProgressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
})
.setCancelable(false)
.show();
}
return true;
} else if (!isGoogleLogin) {
view.getSettings().setUserAgentString(defaultUserAgent);
}
mProgressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
} else {
Toast.makeText(view.getContext(), "כתובת זו אינה מורשית (" + host + ")", Toast.LENGTH_SHORT).show();
return true;
}
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (BLOCK_ADS && AdBlockerUtil.getInstance().isAd(url)) {
WebResourceResponse emptyResponse = new WebResourceResponse("text/plain", "utf-8", new ByteArrayInputStream("".getBytes()));
return emptyResponse;
} else {
return super.shouldInterceptRequest(view, url);
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);
if (BLOCK_MEDIA) {
view.loadUrl("javascript: (() => { function handle(node) { if (node.tagName === 'IMG' && node.style.visibility !== 'hidden' && node.width > 32 && node.height > 32) { const blankImageUrl = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='; const { width, height } = window.getComputedStyle(node); node.src = blankImageUrl; node.style.visibility = 'hidden'; node.style.background = 'none'; node.style.backgroundImage = `url(${blankImageUrl})`; node.style.width = width; node.style.height = height; } else if (node.tagName === 'VIDEO' || node.tagName === 'IFRAME' || ((!node.type || node.type.includes('video')) && node.tagName === 'SOURCE') || node.tagName === 'OBJECT') { node.remove(); } } document.querySelectorAll('img,video,source,object,embed,iframe,[type^=video]').forEach(handle); const observer = new MutationObserver((mutations) => mutations.forEach((mutation) => mutation.addedNodes.forEach(handle))); observer.observe(document.body, { childList: true, subtree: true }); })();");
}
// remove netfree card
if (HIDE_NETFREE)
view.loadUrl("javascript: (() => { " +
"const element = document.querySelector('div#netfree-popup-window'); " +
"if (element) { " +
" element.parentNode.remove(); " +
"} " +
"})();");
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
String issuerName = error.getCertificate().getIssuedBy().getOName();
if (NO_SSL) {
handler.proceed();
} else {
handler.cancel();
view.loadData("<html><body><h1 style='color: grey'>SSL Error</h1></body></html>", "text/html; charset=utf-8", "UTF-8");
if (Objects.equals(issuerName, "NetFree")) {
Toast.makeText(view.getContext(), "טיפ: נראה שלא מותקנת תעודת אבטחה של נטפרי", Toast.LENGTH_LONG).show();
} else if (Objects.equals(issuerName, "Netspark")) {
Toast.makeText(view.getContext(), "טיפ: נראה שלא מותקנת תעודת אבטחה של אתרוג/רימון", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
public void onBackPressed() {
if (mCustomView != null) {
mWebView.getWebChromeClient().onHideCustomView();
} else if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}