|
| 1 | +import React from "react"; |
| 2 | +import { Add, Remove } from "@mui/icons-material"; |
| 3 | +import { Stack, Box, Slider } from "@mui/material"; |
| 4 | +import FlatList from "flatlist-react"; |
| 5 | +import { Toolbar } from "@Components/onsenui/Toolbar"; |
| 6 | +import { BottomToolbar } from "@Components/onsenui/BottomToolbar"; |
| 7 | +import { Page } from "@Components/onsenui/Page"; |
| 8 | +import { BuildConfig } from "@Native/BuildConfig"; |
| 9 | +import { useActivity } from "@Hooks/useActivity"; |
| 10 | +import { useTheme } from "@Hooks/useTheme"; |
| 11 | +import { useNativeStorage } from "@Hooks/useNativeStorage"; |
| 12 | +import { useSettings } from "@Hooks/useSettings"; |
| 13 | +import { Ansi } from "@Components/Ansi"; |
| 14 | + |
| 15 | +const LogcatActivity = () => { |
| 16 | + const [fontSize, setFontSize] = useNativeStorage("mmrlini_log_terminal", 100); |
| 17 | + const { context } = useActivity(); |
| 18 | + const { theme } = useTheme(); |
| 19 | + const [lines, setLines] = React.useState<string[]>([]); |
| 20 | + const { settings } = useSettings(); |
| 21 | + |
| 22 | + const termEndRef = React.useRef<HTMLDivElement>(null); |
| 23 | + |
| 24 | + if (settings.term_scroll_bottom) { |
| 25 | + const termBehavior = React.useMemo(() => settings.term_scroll_behavior, [settings]); |
| 26 | + |
| 27 | + React.useEffect(() => { |
| 28 | + termEndRef.current?.scrollIntoView({ behavior: termBehavior.value, block: "end", inline: "nearest" }); |
| 29 | + }, [lines]); |
| 30 | + } |
| 31 | + |
| 32 | + const addLine = (line: string) => { |
| 33 | + setLines((lines) => [...lines, line]); |
| 34 | + }; |
| 35 | + |
| 36 | + const startLog = () => { |
| 37 | + const envp = { |
| 38 | + PACKAGENAME: BuildConfig.APPLICATION_ID, |
| 39 | + }; |
| 40 | + |
| 41 | + Terminal.exec({ |
| 42 | + command: "logcat --pid=`pidof -s $PACKAGENAME` -v color", |
| 43 | + env: envp, |
| 44 | + printError: false, |
| 45 | + onLine: (line) => { |
| 46 | + addLine(line); |
| 47 | + }, |
| 48 | + onExit: (code) => {}, |
| 49 | + }); |
| 50 | + }; |
| 51 | + |
| 52 | + return ( |
| 53 | + <Page |
| 54 | + onShow={startLog} |
| 55 | + renderToolbar={() => ( |
| 56 | + <Toolbar modifier="noshadow"> |
| 57 | + <Toolbar.Left> |
| 58 | + <Toolbar.BackButton onClick={context.popPage} /> |
| 59 | + </Toolbar.Left> |
| 60 | + <Toolbar.Center>Logcat</Toolbar.Center> |
| 61 | + </Toolbar> |
| 62 | + )} |
| 63 | + modifier="noshadow" |
| 64 | + renderBottomToolbar={() => { |
| 65 | + return ( |
| 66 | + <BottomToolbar sx={{ background: "none", backgroundColor: theme.palette.background.default }}> |
| 67 | + <Stack spacing={2} direction="row" sx={{ height: "100%", ml: 1, mr: 1 }} alignItems="center"> |
| 68 | + <Add color="secondary" /> |
| 69 | + <Slider |
| 70 | + value={fontSize} |
| 71 | + onChange={(event, newValue) => { |
| 72 | + setFontSize(Number(newValue)); |
| 73 | + }} |
| 74 | + step={10} |
| 75 | + marks |
| 76 | + min={20} |
| 77 | + max={200} |
| 78 | + /> |
| 79 | + <Remove color="secondary" /> |
| 80 | + </Stack> |
| 81 | + </BottomToolbar> |
| 82 | + ); |
| 83 | + }} |
| 84 | + > |
| 85 | + <div |
| 86 | + style={{ |
| 87 | + display: "flex", |
| 88 | + flexWrap: "wrap", |
| 89 | + }} |
| 90 | + > |
| 91 | + <Stack |
| 92 | + style={{ |
| 93 | + whiteSpace: "pre", |
| 94 | + flex: "0 0 100%", |
| 95 | + color: "white", |
| 96 | + height: "100%", |
| 97 | + }} |
| 98 | + direction="column" |
| 99 | + justifyContent="flex-start" |
| 100 | + alignItems="stretch" |
| 101 | + spacing={0} |
| 102 | + > |
| 103 | + <FlatList |
| 104 | + list={lines} |
| 105 | + renderItem={(line, key) => ( |
| 106 | + <Box |
| 107 | + key={key} |
| 108 | + component={Ansi} |
| 109 | + sx={{ |
| 110 | + fontSize: fontSize ? `${fontSize}%` : "none", |
| 111 | + ml: 1, |
| 112 | + mr: 1, |
| 113 | + }} |
| 114 | + > |
| 115 | + {line} |
| 116 | + </Box> |
| 117 | + )} |
| 118 | + renderOnScroll |
| 119 | + renderWhenEmpty={() => <></>} |
| 120 | + /> |
| 121 | + </Stack> |
| 122 | + </div> |
| 123 | + <div ref={termEndRef} /> |
| 124 | + </Page> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +export { LogcatActivity }; |
0 commit comments