mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-13 06:39:54 -04:00
Localizable changelog
This commit is contained in:
parent
bd253b2449
commit
3671d59464
@ -7,6 +7,8 @@ import android.os.Bundle;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.LayoutInflater;
|
||||
@ -17,28 +19,65 @@ import android.webkit.WebViewClient;
|
||||
|
||||
import net.kdt.pojavlaunch.R;
|
||||
import net.kdt.pojavlaunch.*;
|
||||
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
||||
public class LauncherFragment extends Fragment
|
||||
{
|
||||
private WebView webNews;
|
||||
private View view;
|
||||
private Thread validUrlSelectorThread;
|
||||
private String validChangelog = "/changelog.html";
|
||||
private boolean interruptLoad = false;
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
super.onCreateView(inflater, container, savedInstanceState);
|
||||
view = inflater.inflate(R.layout.lmaintab_news, container, false);
|
||||
return view;
|
||||
}
|
||||
public void selectValidUrl() {
|
||||
String lang = LauncherPreferences.PREF_LANGUAGE;
|
||||
if(lang.equals("default")) lang = Locale.getDefault().getLanguage();
|
||||
final String localizedUrl = "/changelog-"+lang+".html";
|
||||
if(!tryUrl(Tools.URL_HOME+localizedUrl)) return;
|
||||
else {
|
||||
requireActivity().runOnUiThread(()->{
|
||||
interruptLoad = true;
|
||||
validChangelog = localizedUrl;
|
||||
webNews.loadUrl(Tools.URL_HOME+validChangelog);
|
||||
});
|
||||
}
|
||||
}
|
||||
public boolean tryUrl(String url) {
|
||||
Log.i("ChangelogLocale","Trying localized url: "+url);
|
||||
try {
|
||||
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
|
||||
conn.connect();
|
||||
Log.i("ChangelogLocale","Code: "+conn.getResponseCode());
|
||||
return ("" + conn.getResponseCode()).startsWith("2");
|
||||
}catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onActivityCreated(Bundle p1)
|
||||
{
|
||||
super.onActivityCreated(p1);
|
||||
|
||||
webNews = (WebView) getView().findViewById(R.id.lmaintabnewsNewsView);
|
||||
webNews.setWebViewClient(new WebViewClient(){
|
||||
|
||||
// API < 23
|
||||
@Override
|
||||
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
||||
Log.i("WebNews",failingUrl + ": "+description);
|
||||
if(webNews != null){
|
||||
if(validUrlSelectorThread.isAlive()) validUrlSelectorThread.stop();
|
||||
removeWebView();
|
||||
//Change the background to match the other pages.
|
||||
//We change it only when the webView is removed to avoid huge overdraw.
|
||||
@ -48,7 +87,7 @@ public class LauncherFragment extends Fragment
|
||||
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
if(!url.equals(Tools.URL_HOME + "/changelog.html")){
|
||||
if(!url.equals(Tools.URL_HOME + validChangelog)){
|
||||
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
|
||||
startActivity(i);
|
||||
return true;
|
||||
@ -59,7 +98,9 @@ public class LauncherFragment extends Fragment
|
||||
@RequiresApi(23) //API 23+
|
||||
@Override
|
||||
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
|
||||
Log.i("WebNews",error.getDescription()+"");
|
||||
if(webNews != null){
|
||||
if(validUrlSelectorThread.isAlive()) validUrlSelectorThread.stop();
|
||||
removeWebView();
|
||||
LauncherFragment.this.view.setBackgroundColor(Color.parseColor("#44000000"));
|
||||
}
|
||||
@ -68,7 +109,7 @@ public class LauncherFragment extends Fragment
|
||||
@RequiresApi(23)
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
|
||||
if(!request.getUrl().toString().equals(Tools.URL_HOME + "/changelog.html")){
|
||||
if(!request.getUrl().toString().equals(Tools.URL_HOME + validChangelog)){
|
||||
Intent i = new Intent(Intent.ACTION_VIEW, request.getUrl());
|
||||
startActivity(i);
|
||||
return true;
|
||||
@ -78,7 +119,9 @@ public class LauncherFragment extends Fragment
|
||||
});
|
||||
webNews.clearCache(true);
|
||||
webNews.getSettings().setJavaScriptEnabled(true);
|
||||
webNews.loadUrl(Tools.URL_HOME + "/changelog.html");
|
||||
validUrlSelectorThread = new Thread(this::selectValidUrl);
|
||||
validUrlSelectorThread.start();
|
||||
if(!interruptLoad)webNews.loadUrl(Tools.URL_HOME + validChangelog);
|
||||
}
|
||||
|
||||
private void removeWebView() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user