Add a setting for request news page on start

This commit is contained in:
tehcneko committed 2021-04-27 18:20:15 +08:00
1 parent 89d79425bf
commit 67b5e79d2a
7 files changed
+89 -14

No files matched your search

@@ -27,11 +27,16 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Debug;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.collection.LruCache;
import androidx.core.text.HtmlCompat;
import com.hippo.a7zip.A7Zip;
import com.hippo.beerbelly.SimpleDiskCache;
@@ -45,6 +50,7 @@ import com.hippo.ehviewer.client.EhSSLSocketFactory;
import com.hippo.ehviewer.client.EhUrl;
import com.hippo.ehviewer.client.EhX509TrustManager;
import com.hippo.ehviewer.client.data.GalleryDetail;
import com.hippo.ehviewer.client.parser.EventPaneParser;
import com.hippo.ehviewer.download.DownloadManager;
import com.hippo.ehviewer.spider.SpiderDen;
import com.hippo.ehviewer.ui.CommonOperations;
@@ -83,6 +89,8 @@ import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import rikka.material.app.DayNightDelegate;
import rikka.material.app.LocaleDelegate;
@@ -392,6 +400,9 @@ public class EhApplication extends SceneApplication {
}
private void theDawnOfNewDay() {
if (!Settings.getRequestNews()) {
return;
}
EhCookieStore store = getEhCookieStore(this);
HttpUrl eh = HttpUrl.get(EhUrl.HOST_E);
@@ -401,7 +412,16 @@ public class EhApplication extends SceneApplication {
Request request = new EhRequestBuilder(EhUrl.HOST_E + "news.php", referer).build();
Call call = getOkHttpClient(this).newCall(request);
try {
call.execute();
Response response = call.execute();
ResponseBody responseBody = response.body();
if (responseBody == null) {
return;
}
String body = responseBody.string();
String html = EventPaneParser.parse(body);
if (html != null) {
showEventPane(html);
}
} catch (Throwable e) {
e.printStackTrace();
}
@@ -409,6 +429,27 @@ public class EhApplication extends SceneApplication {
}
}
public void showEventPane(String html) {
Activity activity = getTopActivity();
if (activity != null) {
activity.runOnUiThread(() -> {
AlertDialog dialog = new AlertDialog.Builder(activity)
.setMessage(HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY))
.setPositiveButton(android.R.string.ok, null)
.create();
final View messageView = dialog.findViewById(android.R.id.message);
if (messageView instanceof TextView) {
((TextView) messageView).setMovementMethod(LinkMovementMethod.getInstance());
}
try {
dialog.show();
} catch (Throwable t) {
// ignore
}
});
}
}
private void clearTempDir() {
File dir = AppConfig.getTempDir();
if (null != dir) {
@@ -267,6 +267,8 @@ public class Settings {
private static final int DEFAULT_CLIPBOARD_TEXT_HASH_CODE = 0;
private static final String KEY_DOWNLOAD_DELAY = "download_delay";
private static final int DEFAULT_DOWNLOAD_DELAY = 0;
private static final String KEY_REQUEST_NEWS = "request_news";
private static final boolean DEFAULT_REQUEST_NEWS = true;
private static Context sContext;
private static SharedPreferences sSettingsPre;
private static EhConfig sEhConfig;
@@ -293,19 +295,6 @@ public class Settings {
}
}
// Xiaomi (and Huawei?) devices have some kind of "dynamic refresh rate" thing,
// which will report a low refresh rate causing false positive reports
//if (!sSettingsPre.contains(KEY_E_INK_MODE)) {
// WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
// if (wm != null) {
// Display display = wm.getDefaultDisplay();
// if (display != null && display.getRefreshRate() > 0 && display.getRefreshRate() < 5.0) {
// // Probably an E-Ink device
// putReadTheme(2);
// putEInkMode(true);
// }
// }
//}
}
public static Locale getLocale() {
@@ -553,6 +542,10 @@ public class Settings {
return getBoolean(KEY_FIX_THUMB_URL, DEFAULT_FIX_THUMB_URL);
}
public static boolean getRequestNews() {
return getBoolean(KEY_REQUEST_NEWS, DEFAULT_REQUEST_NEWS);
}
public static boolean getShowJpnTitle() {
return getBoolean(KEY_SHOW_JPN_TITLE, DEFAULT_SHOW_JPN_TITLE);
}
@@ -23,6 +23,7 @@ import android.util.Pair;
import androidx.annotation.Nullable;
import com.hippo.ehviewer.AppConfig;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.GetText;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.Settings;
@@ -47,6 +48,7 @@ import com.hippo.ehviewer.client.parser.GalleryTokenApiParser;
import com.hippo.ehviewer.client.parser.ProfileParser;
import com.hippo.ehviewer.client.parser.RateGalleryParser;
import com.hippo.ehviewer.client.parser.SignInParser;
import com.hippo.ehviewer.client.parser.EventPaneParser;
import com.hippo.ehviewer.client.parser.TorrentParser;
import com.hippo.ehviewer.client.parser.VoteCommentParser;
import com.hippo.ehviewer.client.parser.VoteTagParser;
@@ -363,6 +365,10 @@ public class EhEngine {
code = response.code();
headers = response.headers();
body = response.body().string();
String html = EventPaneParser.parse(body);
if (html != null) {
EhApplication.getInstance().showEventPane(html);
}
return GalleryDetailParser.parse(body);
} catch (Throwable e) {
ExceptionUtils.throwIfFatal(e);
@@ -0,0 +1,27 @@
package com.hippo.ehviewer.client.parser;
import com.hippo.util.ExceptionUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class EventPaneParser {
public static String parse(String body) {
String event = null;
try {
Document d = Jsoup.parse(body);
Element eventpane = d.getElementById("eventpane");
if (eventpane != null) {
event = eventpane.html();
}
} catch (Throwable e) {
ExceptionUtils.throwIfFatal(e);
e.printStackTrace();
}
return event;
}
}
@@ -750,4 +750,5 @@
<string name="settings_eh_metered_network_warning">流量计费网络警告</string>
<string name="metered_network_warning">正在使用流量计费网络</string>
<string name="read_from">从第 %d 页阅读</string>
<string name="settings_request_news_title">启动时请求新闻页面</string>
</resources>
+1
View File
@@ -814,4 +814,5 @@
<string name="settings_eh_metered_network_warning">Metered network warning</string>
<string name="metered_network_warning">Connected to metered networks</string>
<string name="read_from">Read page %d</string>
<string name="settings_request_news_title">Request news page on start</string>
</resources>
+6
View File
@@ -157,4 +157,10 @@
android:title="@string/settings_eh_metered_network_warning"
app:iconSpaceReserved="false" />
<SwitchPreference
android:defaultValue="true"
android:key="request_news"
android:title="@string/settings_request_news_title"
app:iconSpaceReserved="false" />
</PreferenceScreen>