-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullscreenActivity.java
174 lines (151 loc) · 6.81 KB
/
FullscreenActivity.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
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
package com.anonace.android;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class FullscreenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
WebView webView = (WebView) findViewById(R.id.webView);
setSettings(webView);
setWebViewClient(webView);
webView.loadUrl(getAppURL());
findViewById(R.id.splashView).setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
// webView.invalidate();
}
@Override
public void onBackPressed() {
WebView webView = (WebView) findViewById(R.id.webView);
if (!webView.canGoBack()) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(getResources().getString(R.string.app_name))
.setMessage("Are you sure you want to close application?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setNegativeButton("No", null).show();
} else {
webView.goBack();
}
}
private void setWebViewClient(WebView webView) {
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(
WebView view, String url, String message, final android.webkit.JsResult result) {
new AlertDialog.Builder(FullscreenActivity.this)
.setTitle(R.string.app_name)
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
}).setCancelable(false).create().show();
return true;
}
@Override
public boolean onJsConfirm(
WebView view, String url, String message, final android.webkit.JsResult result) {
new AlertDialog.Builder(FullscreenActivity.this)
.setTitle(R.string.app_name)
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
}).create().show();
return true;
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
String html = "<center style='margin-top:50%'>"+
"<p>Could not connect.</p>" +
"<p><a href='" + getAppURL() + "'>Refresh</a></p>" +
"</center>";
view.loadDataWithBaseURL(null, html, "text/html", "UTF-8",null);
view.invalidate();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest req) {
return shouldOverrideUrlLoading(view, req.getUrl());
}
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return shouldOverrideUrlLoading(view, Uri.parse(url));
}
private boolean shouldOverrideUrlLoading(WebView view, Uri uri) {
boolean result = false;
String hostname = uri.getHost();
if (hostname != null && !hostname.contains(getResources().getString(R.string.site_domain))) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
result = true;
}
return result;
}
});
}
@SuppressLint({"SetJavaScriptEnabled"})
private void setSettings(WebView webView) {
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(null);
} else {
cookieManager.removeAllCookie();
}
cookieManager.setAcceptCookie(false);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(true);
// settings.setBuiltInZoomControls(true);
settings.setAppCacheEnabled(true);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
settings.setUserAgentString(
settings.getUserAgentString() + " " +
getResources().getString(R.string.app_name) + "/" + getAppVersion());
}
private String getAppVersion() {
try {
return this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
return "0.0";
}
}
private String getAppURL() {
String url = getResources().getString(R.string.initial_url);
url += (url.contains("?") ? "&" : "?") + "v=" + getAppVersion();
return url + "&nocache=" + String.valueOf(System.currentTimeMillis());
}
}