Skip to content

Commit

Permalink
support timezone, fix #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Anillc committed Feb 28, 2023
1 parent 252b93a commit 52fa329
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 32 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/cn/anillc/koishi/Constant.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package cn.anillc.koishi

const val DEFAULT_DNS = "223.5.5.5"
const val ABOUT = "Koishi Android v${BuildConfig.VERSION_NAME}\n项目开源于 GitHub koishijs/koishi-android"
const val DEFAULT_TIMEZONE = "Asia/Shanghai"
const val ABOUT = "Koishi Android v${BuildConfig.VERSION_NAME}\n项目开源于 GitHub koishijs/koishi-android"
26 changes: 12 additions & 14 deletions app/src/main/java/cn/anillc/koishi/services/ProotService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,20 @@ open class ProotService : Service(), Runnable {
protected fun startProot(cmd: String, env: Map<String, String> = mapOf()) {
if (process.get() != null) return
val prootEnv = mutableMapOf(
"KOISHI_DNS" to preferences.getString("KOISHI_DNS", DEFAULT_DNS)!!
"KOISHI_DNS" to preferences.getString("KOISHI_DNS", DEFAULT_DNS)!!,
"KOISHI_TIMEZONE" to preferences.getString("KOISHI_TIMEZONE", DEFAULT_TIMEZONE)!!,
)
prootEnv.putAll(env)
this.process.set(
startProotProcess(
"""
setsid sh <<PROOT_EOF
trap : SIGINT # to get status of process
echo __PID__: \$$
$cmd
echo __STATUS__: \$?
echo -e '\n[Process exited.]\n\n'
PROOT_EOF
""", packagePath, envPath, prootEnv
)
)
val prootCmd = """
setsid sh <<PROOT_EOF
trap : SIGINT # to get status of process
echo __PID__: \$$
@PLACEHOLDER@
echo __STATUS__: \$?
echo -e '\n[Process exited.]\n\n'
PROOT_EOF
""".trimIndent().replace("@PLACEHOLDER@", cmd)
this.process.set(startProotProcess(prootCmd, packagePath, envPath, prootEnv))
Thread(this).start()
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/drawable/timezone.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- drawable/map_clock.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24"><path android:fillColor="#000000" android:pathData="M15,12H16.5V16.25L19.36,17.94L18.61,19.16L15,17V12M23,16A7,7 0 0,1 16,23C13,23 10.4,21.08 9.42,18.4L8,17.9L2.66,19.97L2.5,20A0.5,0.5 0 0,1 2,19.5V4.38C2,4.15 2.15,3.97 2.36,3.9L8,2L14,4.1L19.34,2H19.5A0.5,0.5 0 0,1 20,2.5V10.25C21.81,11.5 23,13.62 23,16M9,16C9,12.83 11.11,10.15 14,9.29V6.11L8,4V15.89L9,16.24C9,16.16 9,16.08 9,16M16,11A5,5 0 0,0 11,16A5,5 0 0,0 16,21A5,5 0 0,0 21,16A5,5 0 0,0 16,11Z" /></vector>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<string name="koishi_is_not_started">koishi 尚未启动或启动未完成</string>
<string name="dns_settings">DNS 设置</string>
<string name="edit_dns_settings">点击修改 DNS 设置</string>
<string name="timezone_settings">DNS 设置</string>
<string name="edit_timezone_settings">点击修改 DNS 设置</string>
<string name="backup_koishi">导出 Koishi 备份</string>
<string name="import_koishi">从文件中导入 Koishi</string>
<string name="import_koishi_alert">警告:此操作将会覆盖您已有的 Koishi 实例,请确认已备份后再操作</string>
Expand All @@ -24,4 +26,4 @@
<string name="export_file_succeed">导出文件已保存至 %s</string>
<string name="failed_to_initialize">初始化失败: %s</string>
<string name="stop_before_import">请在导入前停止 Koishi 实例</string>
</resources>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
android:title="@string/dns_settings"
android:icon="@drawable/dns"/>

<EditTextPreference
android:key="KOISHI_TIMEZONE"
android:summary="@string/edit_timezone_settings"
android:title="@string/timezone_settings"
android:icon="@drawable/timezone"/>

<Preference
android:key="export"
android:title="导出"
Expand Down
6 changes: 2 additions & 4 deletions bootstrap/bootstrap.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{ pkgs, callPackage, lib, extraPackages ? [], withFonts ? false, ... }: let
aarch64-pkgs = pkgs.pkgsCross.aarch64-multiplatform;
in
{ pkgs, callPackage, lib, full ? false, ... }:

with builtins;
with lib;

let
env = callPackage ./environment { inherit extraPackages withFonts; };
env = callPackage ./environment { inherit full; };
info = readFile "${pkgs.closureInfo { rootPaths = [ env ]; }}/store-paths";

bootstrap = pkgs.runCommand "bootstrap" {} ''
Expand Down
12 changes: 8 additions & 4 deletions bootstrap/environment/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs, buildEnv, callPackage, lib, inputs, extraPackages ? [], withFonts ? false }:
{ pkgs, buildEnv, callPackage, lib, inputs, full ? false }:

with builtins;
with lib;
Expand All @@ -7,16 +7,20 @@ let
aarch64-pkgs = import inputs.nixpkgs { system = "aarch64-linux"; };
login = callPackage ./login.nix {};
env = callPackage ./env.nix { inherit (aarch64-pkgs) busybox; };
resolvconf = callPackage ./resolvconf.nix {};
fonts = callPackage ./fonts.nix {};
certs = callPackage ./certs.nix {};
timezone = callPackage ./timezone.nix {};
in buildEnv {
name = "koishi-env";
# dns is in login.nix
paths = with aarch64-pkgs; [
login env
certs
busybox zip
nodejs
] ++ (optional withFonts fonts)
++ extraPackages;
] ++ (optionals full [
fonts
chromium
timezone
]);
}
9 changes: 5 additions & 4 deletions bootstrap/environment/login.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ in writeScriptBin "login" ''
set -e
# /nix is writable
if [ "$KOISHI_DNS" ]; then
/bin/cat > /etc/resolv.conf <<EOF
nameserver $KOISHI_DNS
EOF
if [ -n "$KOISHI_DNS" ]; then
echo "nameserver $KOISHI_DNS" > /etc/resolv.conf
fi
if [ -n "$KOISHI_TIMEZONE" ] && [ -e /etc/zoneinfo ]; then
/bin/ln -sf /etc/zoneinfo/$KOISHI_TIMEZONE /etc/localtiome
fi
for var in $(/bin/env | /bin/cut -d '=' -f 1); do unset $var; done
Expand Down
6 changes: 6 additions & 0 deletions bootstrap/environment/timezone.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ tzdata, runCommand }:

runCommand "timezone" {} ''
mkdir -p $out/etc
ln -s ${tzdata}/share/zoneinfo $out/etc/zoneinfo
''
5 changes: 1 addition & 4 deletions bootstrap/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
in {
packages = {
bootstrap = pkgs.callPackage ./bootstrap.nix {};
bootstrap-extra = pkgs.callPackage ./bootstrap.nix {
extraPackages = with aarch64-pkgs; [ chromium ];
withFonts = true;
};
bootstrap-extra = pkgs.callPackage ./bootstrap.nix { full = true; };
};
apps = let
app = bootstrap: mkApp {
Expand Down

0 comments on commit 52fa329

Please sign in to comment.