-
Notifications
You must be signed in to change notification settings - Fork 63
/
MyAppWebViewClient.java
125 lines (110 loc) · 5.99 KB
/
MyAppWebViewClient.java
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
package org.indywidualni.fblite;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MyAppWebViewClient extends WebViewClient {
// variable for onReceivedError
private boolean refreshed;
public static int errorChecker;
// get application context from MainActivity
private static Context context = MyApplication.getContextOfApplication();
// get shared preferences
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
// convert css file to string only one time
private static String cssFile;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// handling external links as intents
if (Uri.parse(url).getHost().endsWith("facebook.com") || Uri.parse(url).getHost().endsWith("m.facebook.com") || Uri.parse(url).getHost().endsWith("h.facebook.com") || Uri.parse(url).getHost().endsWith("l.facebook.com") || Uri.parse(url).getHost().endsWith("0.facebook.com") || Uri.parse(url).getHost().endsWith("zero.facebook.com") || Uri.parse(url).getHost().endsWith("fb.me")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// refresh on connection error (sometimes there is an error even when there is a network connection)
if (!refreshed) {
view.loadUrl(failingUrl);
// when network error is real do not reload url again
refreshed = true;
}
// increment first run error checker
errorChecker++;
}
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError err) {
// redirect to deprecated method, so we can use it in all SDK versions
onReceivedError(view, err.getErrorCode(), err.getDescription().toString(), req.getUrl().toString());
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// increment first run error checker
errorChecker++;
// make sure splash screen will disappear
if (url.matches("(.*)sharer(.*)") || url.matches("(.*)messages(.*)"))
errorChecker += 2;
}
@Override
public void onPageFinished(WebView view, String url) {
// when Zero is activated and there is a mobile network connection ignore extra customizations
if (!preferences.getBoolean("facebook_zero", false) || !Connectivity.isConnectedMobile(context)) {
// turn facebook black (experimental)
if (preferences.getBoolean("dark_theme", false)) {
// fill it with data only one time
if (cssFile == null)
cssFile = readRawTextFile(context, R.raw.black);
view.loadUrl("javascript:function addStyleString(str) { var node = document.createElement('style'); node.innerHTML = str; document.body.appendChild(node); } addStyleString('" + cssFile + "');");
}
// blue navigation bar always on top
if (preferences.getBoolean("fixed_nav", false)) {
String cssFixed = "#header{ position: fixed; z-index: 11; top: 0px; } #root{ padding-top: 44px; } .flyout{ max-height: " + heightForFixedFacebookNavbar() + "px; overflow-y: scroll; }";
view.loadUrl("javascript:function addStyleString(str) { var node = document.createElement('style'); node.innerHTML = str; document.body.appendChild(node); } addStyleString('" + cssFixed + "');");
}
}
// apply extra bottom padding for transparent navigation
if (preferences.getBoolean("transparent_nav", false))
view.loadUrl("javascript:function addStyleString(str) { var node = document.createElement('style'); node.innerHTML = str; document.body.appendChild(node); } addStyleString('body{ padding-bottom: 48px; }');");
// don't display images when they are disabled, we don't need empty placeholders
if (preferences.getBoolean("no_images", false))
view.loadUrl("javascript:function addStyleString(str) { var node = document.createElement('style'); node.innerHTML = str; document.body.appendChild(node); } addStyleString('.img, ._5s61, ._5sgg{ display: none; }');");
}
// read raw files to string (for css files)
private static String readRawTextFile(Context ctx, int resId) {
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while ((line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return " ";
}
return text.toString();
}
// window's height minus navbar minus extra top padding, all divided by density
private static int heightForFixedFacebookNavbar() {
final int navbar = MainActivity.getNavigationBarHeight(context, context.getResources().getConfiguration().orientation);
final float density = context.getResources().getDisplayMetrics().density;
return (int) ((context.getResources().getDisplayMetrics().heightPixels - navbar - 44) / density);
}
}