Convert UConfigFragment to kotlin
This commit is contained in:
1 parent
c83bd885f6
commit
56fdb2d4a8
1 file changed
+116
-142
@@ -15,183 +15,157 @@
|
||||
* You should have received a copy of the GNU General Public License along with EhViewer.
|
||||
* If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.hippo.ehviewer.ui.fragment
|
||||
|
||||
package com.hippo.ehviewer.ui.fragment;
|
||||
import android.annotation.SuppressLint
|
||||
import android.graphics.Bitmap
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.Menu
|
||||
import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.webkit.CookieManager
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import androidx.core.view.MenuProvider
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import com.hippo.ehviewer.EhApplication.Companion.ehCookieStore
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.databinding.ActivityWebviewBinding
|
||||
import com.hippo.ehviewer.ui.scene.BaseScene
|
||||
import com.hippo.ehviewer.widget.DialogWebChromeClient
|
||||
import okhttp3.Cookie
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import rikka.core.res.resolveColor
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.CookieManager;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.android.material.progressindicator.CircularProgressIndicator;
|
||||
import com.hippo.ehviewer.EhApplication;
|
||||
import com.hippo.ehviewer.R;
|
||||
import com.hippo.ehviewer.client.EhCookieStore;
|
||||
import com.hippo.ehviewer.client.EhUrl;
|
||||
import com.hippo.ehviewer.ui.scene.BaseScene;
|
||||
import com.hippo.ehviewer.widget.DialogWebChromeClient;
|
||||
|
||||
import okhttp3.Cookie;
|
||||
import okhttp3.HttpUrl;
|
||||
import rikka.core.res.ResourcesKt;
|
||||
|
||||
public class UConfigFragment extends BaseFragment {
|
||||
|
||||
private WebView webView;
|
||||
private CircularProgressIndicator progress;
|
||||
private String url;
|
||||
private boolean loaded;
|
||||
class UConfigFragment : BaseFragment() {
|
||||
private var _binding: ActivityWebviewBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
private val url = EhUrl.uConfigUrl
|
||||
private var loaded = false
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.activity_webview, container, false);
|
||||
webView = view.findViewById(R.id.webview);
|
||||
webView.setBackgroundColor(ResourcesKt.resolveColor(requireActivity().getTheme(), android.R.attr.colorBackground));
|
||||
webView.getSettings().setBuiltInZoomControls(true);
|
||||
webView.getSettings().setDisplayZoomControls(false);
|
||||
webView.getSettings().setJavaScriptEnabled(true);
|
||||
webView.setWebViewClient(new UConfigWebViewClient());
|
||||
webView.setWebChromeClient(new DialogWebChromeClient(requireContext()));
|
||||
progress = view.findViewById(R.id.progress);
|
||||
return view;
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
_binding = ActivityWebviewBinding.inflate(inflater, container, false)
|
||||
binding.webview.run {
|
||||
setBackgroundColor(requireActivity().theme.resolveColor(android.R.attr.colorBackground))
|
||||
settings.builtInZoomControls = true
|
||||
settings.displayZoomControls = false
|
||||
settings.javaScriptEnabled = true
|
||||
webViewClient = UConfigWebViewClient()
|
||||
webChromeClient = DialogWebChromeClient(requireContext())
|
||||
}
|
||||
return binding.root
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
progress.setVisibility(View.VISIBLE);
|
||||
webView.loadUrl(url);
|
||||
showTip(R.string.apply_tip, BaseScene.LENGTH_LONG);
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
binding.progress.visibility = View.VISIBLE
|
||||
binding.webview.loadUrl(url)
|
||||
showTip(R.string.apply_tip, BaseScene.LENGTH_LONG)
|
||||
requireActivity().addMenuProvider(object : MenuProvider {
|
||||
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
|
||||
menuInflater.inflate(R.menu.activity_u_config, menu)
|
||||
}
|
||||
|
||||
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
|
||||
if (menuItem.itemId == R.id.action_apply) {
|
||||
if (loaded) {
|
||||
apply()
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}, viewLifecycleOwner, Lifecycle.State.RESUMED)
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
// http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
|
||||
CookieManager cookieManager = CookieManager.getInstance();
|
||||
cookieManager.flush();
|
||||
cookieManager.removeAllCookies(null);
|
||||
cookieManager.removeSessionCookies(null);
|
||||
|
||||
val cookieManager = CookieManager.getInstance()
|
||||
cookieManager.flush()
|
||||
cookieManager.removeAllCookies(null)
|
||||
cookieManager.removeSessionCookies(null)
|
||||
|
||||
// Copy cookies from okhttp cookie store to CookieManager
|
||||
url = EhUrl.getUConfigUrl();
|
||||
EhCookieStore store = EhApplication.getEhCookieStore();
|
||||
for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
|
||||
cookieManager.setCookie(url, cookie.toString());
|
||||
val store = ehCookieStore
|
||||
for (cookie in store.getCookies(url.toHttpUrl())) {
|
||||
cookieManager.setCookie(url, cookie.toString())
|
||||
}
|
||||
}
|
||||
|
||||
private void apply() {
|
||||
webView.loadUrl("javascript:"
|
||||
+ "(function() {\n"
|
||||
+ " var apply = document.getElementById(\"apply\").children[0];\n"
|
||||
+ " apply.click();\n"
|
||||
+ "})();");
|
||||
private fun apply() {
|
||||
binding.webview.loadUrl(
|
||||
"""javascript:(function() {
|
||||
var apply = document.getElementById("apply").children[0];
|
||||
apply.click();
|
||||
})();"""
|
||||
)
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
inflater.inflate(R.menu.activity_u_config, menu);
|
||||
private fun longLive(cookie: Cookie): Cookie {
|
||||
return Cookie.Builder()
|
||||
.name(cookie.name)
|
||||
.value(cookie.value)
|
||||
.domain(cookie.domain)
|
||||
.path(cookie.path)
|
||||
.expiresAt(Long.MAX_VALUE)
|
||||
.build()
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == R.id.action_apply) {
|
||||
if (loaded) {
|
||||
apply();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
private Cookie longLive(Cookie cookie) {
|
||||
return new Cookie.Builder()
|
||||
.name(cookie.name())
|
||||
.value(cookie.value())
|
||||
.domain(cookie.domain())
|
||||
.path(cookie.path())
|
||||
.expiresAt(Long.MAX_VALUE)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
webView.destroy();
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
binding.webview.destroy()
|
||||
|
||||
// Put cookies back to okhttp cookie store
|
||||
CookieManager cookieManager = CookieManager.getInstance();
|
||||
String cookiesString = cookieManager.getCookie(url);
|
||||
|
||||
if (cookiesString != null && !cookiesString.isEmpty()) {
|
||||
EhCookieStore store = EhApplication.getEhCookieStore();
|
||||
HttpUrl eUrl = HttpUrl.parse(EhUrl.HOST_E);
|
||||
HttpUrl exUrl = HttpUrl.parse(EhUrl.HOST_EX);
|
||||
if (eUrl == null || exUrl == null) {
|
||||
return;
|
||||
}
|
||||
val cookieManager = CookieManager.getInstance()
|
||||
val cookiesString = cookieManager.getCookie(url)
|
||||
if (!cookiesString.isNullOrEmpty()) {
|
||||
val store = ehCookieStore
|
||||
val eUrl = EhUrl.HOST_E.toHttpUrl()
|
||||
val exUrl = EhUrl.HOST_EX.toHttpUrl()
|
||||
|
||||
// The cookies saved in the uconfig page should be shared between e and ex
|
||||
for (String header : cookiesString.split(";")) {
|
||||
Cookie eCookie = Cookie.parse(eUrl, header);
|
||||
if (eCookie != null) {
|
||||
store.addCookie(longLive(eCookie));
|
||||
for (header in cookiesString.split(";".toRegex()).dropLastWhile { it.isEmpty() }) {
|
||||
Cookie.parse(eUrl, header)?.let {
|
||||
store.addCookie(longLive(it))
|
||||
}
|
||||
|
||||
Cookie exCookie = Cookie.parse(exUrl, header);
|
||||
if (exCookie != null) {
|
||||
store.addCookie(longLive(exCookie));
|
||||
Cookie.parse(exUrl, header)?.let {
|
||||
store.addCookie(longLive(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
_binding = null
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
override fun getFragmentTitle(): Int {
|
||||
return R.string.u_config
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFragmentTitle() {
|
||||
return R.string.u_config;
|
||||
}
|
||||
|
||||
private class UConfigWebViewClient extends WebViewClient {
|
||||
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
private inner class UConfigWebViewClient : WebViewClient() {
|
||||
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
|
||||
// Never load other urls
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
||||
progress.setVisibility(View.VISIBLE);
|
||||
loaded = false;
|
||||
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
|
||||
binding.progress.visibility = View.VISIBLE
|
||||
loaded = false
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
progress.setVisibility(View.GONE);
|
||||
loaded = true;
|
||||
override fun onPageFinished(view: WebView, url: String) {
|
||||
binding.progress.visibility = View.GONE
|
||||
loaded = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user