-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathandroid_fonts.py
More file actions
71 lines (53 loc) · 2.3 KB
/
Copy pathandroid_fonts.py
File metadata and controls
71 lines (53 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from pathlib import Path
from .android import Android
from contextlib import contextmanager
from os import close, devnull, dup, dup2, O_WRONLY, open
from sys import stderr, stdout
from typing import Set
from ..exceptions import OSNotSupported
from ..system_fonts import SystemFonts
__all__ = ["AndroidFonts"]
class AndroidFonts(SystemFonts):
@staticmethod
def get_system_fonts_filename() -> Set[str]:
android = Android()
fonts_filename = set()
# Redirect the stderr_and_stdout to null since we don't care about android logs
# There is __android_log_set_minimum_priority to set the log level, but even if I set it to 8 (which correspond to ANDROID_LOG_SILENT),
# it was still logging: https://developer.android.com/ndk/reference/group/logging#__android_log_set_minimum_priority
with AndroidFonts._silence_stderr_and_stdout():
font_iterator = android.ASystemFontIterator_open()
while True:
font = android.ASystemFontIterator_next(font_iterator)
if font is None:
break
font_filename = android.AFont_getFontFilePath(font).decode("utf-8")
fonts_filename.add(font_filename)
android.AFont_close(font)
android.ASystemFontIterator_close(font_iterator)
return fonts_filename
@staticmethod
def install_font(font_filename: Path, add_font_to_registry: bool = False) -> None:
raise OSNotSupported("You cannot install font on android.")
@staticmethod
def uninstall_font(font_filename: Path, remove_font_in_registry: bool) -> None:
raise OSNotSupported("You cannot uninstall font on android.")
@staticmethod
@contextmanager
def _silence_stderr_and_stdout():
# From: https://stackoverflow.com/a/75037627/15835974
stderr_fd = stderr.fileno()
orig_stderr_fd = dup(stderr_fd)
stdout_fd = stdout.fileno()
orig_stdout_fd = dup(stdout_fd)
null_fd = open(devnull, O_WRONLY)
dup2(null_fd, stderr_fd)
dup2(null_fd, stdout_fd)
try:
yield
finally:
dup2(orig_stderr_fd, stderr_fd)
dup2(orig_stdout_fd, stdout_fd)
close(orig_stderr_fd)
close(orig_stdout_fd)
close(null_fd)