Recently Written · git

subread-android

SubRead for Android: times an audiobook against its ebook on the device

git clone https://github.com/equwal/subread-android

Log | Files | Refs


tools/write_res.py (2529 bytes)

1 """Writes the app's small static resources. Run once; the output is committed."""
2 from pathlib import Path
3 
4 RES = Path(__file__).resolve().parent.parent / "app/src/main/res"
5 
6 
7 def w(rel: str, text: str) -> None:
8     p = RES / rel
9     p.parent.mkdir(parents=True, exist_ok=True)
10     p.write_text(text, encoding="utf-8", newline="\n")
11 
12 
13 HEAD = '<?xml version="1.0" encoding="utf-8"?>\n'
14 
15 w("xml/file_paths.xml", HEAD + """<paths>
16     <files-path name="subtitles" path="subtitles/" />
17     <files-path name="video" path="video/" />
18 </paths>
19 """)
20 
21 w("values/strings.xml", HEAD + """<resources>
22     <string name="app_name">SubRead</string>
23 </resources>
24 """)
25 
26 w("values/themes.xml", HEAD + """<resources>
27     <!-- Compose draws everything; this only covers the moment before it does. -->
28     <style name="Theme.SubRead" parent="android:Theme.Material.Light.NoActionBar">
29         <item name="android:windowBackground">@android:color/white</item>
30         <item name="android:statusBarColor">@android:color/white</item>
31         <item name="android:windowLightStatusBar">true</item>
32     </style>
33 </resources>
34 """)
35 
36 w("values/colors.xml", HEAD + """<resources>
37     <color name="ic_launcher_background">#FFFFFF</color>
38 </resources>
39 """)
40 
41 # An open book with a sound wave across its pages, in one colour.
42 BOOK = ("M30,34 C38,30 46,30 53,34 L53,76 C46,72 38,72 30,76 Z "
43         "M78,34 C70,30 62,30 55,34 L55,76 C62,72 70,72 78,76 Z")
44 WAVE = ("M35,50 h2 v10 h-2 Z M40,45 h2 v20 h-2 Z M45,52 h2 v6 h-2 Z "
45         "M61,52 h2 v6 h-2 Z M66,45 h2 v20 h-2 Z M71,50 h2 v10 h-2 Z")
46 
47 
48 def vector(size: int, paths: list[tuple[str, str]]) -> str:
49     body = "".join(f'    <path android:fillColor="{c}" android:pathData="{d}" />\n' for c, d in paths)
50     return (HEAD + '<vector xmlns:android="http://schemas.android.com/apk/res/android"\n'
51             f'    android:width="{size}dp" android:height="{size}dp"\n'
52             '    android:viewportWidth="108" android:viewportHeight="108">\n' + body + "</vector>\n")
53 
54 
55 w("drawable/ic_launcher_foreground.xml", vector(108, [("#000000", BOOK), ("#FFFFFF", WAVE)]))
56 
57 for name in ("ic_launcher", "ic_launcher_round"):
58     w(f"mipmap-anydpi-v26/{name}.xml", HEAD + """<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
59     <background android:drawable="@color/ic_launcher_background" />
60     <foreground android:drawable="@drawable/ic_launcher_foreground" />
61     <monochrome android:drawable="@drawable/ic_launcher_foreground" />
62 </adaptive-icon>
63 """)
64 
65 print("resources written to", RES)