-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathAriaNgActivity.java
More file actions
70 lines (60 loc) · 2.27 KB
/
AriaNgActivity.java
File metadata and controls
70 lines (60 loc) · 2.27 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
package com.gianlu.aria2android;
import android.os.Bundle;
import android.util.Base64;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import androidx.annotation.Nullable;
import com.gianlu.aria2android.databinding.AriangLayoutBinding;
import com.gianlu.aria2lib.Aria2PK;
import java.io.IOException;
import java.util.Objects;
public class AriaNgActivity extends BastActivity {
WebView webView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AriangLayoutBinding binding = AriangLayoutBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
webView = binding.ariangWebview;
setupWebView();
}
void setupWebView() {
WebSettings settings = webView.getSettings();
webView.setWebViewClient(new WebViewClient());
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
webView.loadUrl(getUri());
}
private String getUri() {
String secret = Base64.encodeToString(Aria2PK.RPC_TOKEN.fallback().getBytes(), Base64.DEFAULT);
return "http://ariang.local/index.html" +
"#!/settings/rpc/set?" +
"protocol=ws&" +
"host=127.0.0.1&" +
"port=" + Aria2PK.RPC_PORT.fallback() + "&" +
"interface=jsonrpc&" +
"secret=" + secret;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (webView != null) webView.destroy();
}
static class WebViewClient extends android.webkit.WebViewClient {
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
if (Objects.equals(request.getUrl().getPath(), "/index.html")) {
try {
return new WebResourceResponse("text/html", "utf-8",
view.getContext().getAssets().open("ariang/index.html"));
} catch (IOException e) {
return null;
}
}
return super.shouldInterceptRequest(view, request);
}
}
}