Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e11ce89b21 | |||
| d57fbe3ef1 | |||
|
|
ba10b8ef7b | ||
|
|
995f2e5ae0 | ||
|
|
a2d6d2a925 | ||
| f6a3cd6b5e | |||
| 23845391d4 | |||
|
|
6220238068 | ||
| 85a1f9dc8a | |||
| 755220d08a | |||
|
|
a757743161 | ||
| 5eff98a639 | |||
| 95f9be70bd | |||
|
|
75a7898c68 | ||
|
|
ee31e0ee7c | ||
| 43200dda05 | |||
| 2e194da4e3 | |||
|
|
b37e3db037 |
98 changed files with 4225 additions and 121 deletions
|
|
@ -2,9 +2,10 @@
|
|||
# 1. semantic-release reads the commits since the last release (Conventional Commits), works out the next version,
|
||||
# writes CHANGELOG.md, makes the release on the forge and tags it. A merge with nothing to release (docs, tests, ci)
|
||||
# makes none.
|
||||
# 2. When there is a release, the image is built from the tag and pushed to git.kmalloc.org/squid/sonda as
|
||||
# X.Y.Z, X.Y and latest.
|
||||
# The runner has no Docker, so the image is built with kaniko, which needs no daemon.
|
||||
# 2. When there is a release, the image is built from the tag, for amd64 and for arm64, and pushed to
|
||||
# git.kmalloc.org/squid/sonda as X.Y.Z-ARCH; then X.Y.Z, X.Y and latest are made to point at both.
|
||||
# The runner has no Docker and cannot emulate another architecture, so the images are built with kaniko, which needs no
|
||||
# daemon, and the Dockerfile crosses to arm64 without running anything for it.
|
||||
# Secret needed: FORGEJO_TOKEN, a token of the owner with the scopes write:repository and write:package.
|
||||
name: release
|
||||
|
||||
|
|
@ -58,16 +59,37 @@ jobs:
|
|||
echo "version=${tag#v}" >> "$GITHUB_OUTPUT"
|
||||
echo "released: ${tag:-nothing}"
|
||||
|
||||
image:
|
||||
# The Alpine of the arm64 image, by digest: kaniko, which has no emulation here, takes the base of every stage for the
|
||||
# platform of the runner, so the last stage is told which one to use.
|
||||
base:
|
||||
needs: release
|
||||
if: ${{ needs.release.outputs.version != '' }}
|
||||
runs-on: docker
|
||||
container:
|
||||
image: gcr.io/go-containerregistry/crane:debug
|
||||
outputs:
|
||||
arm64: ${{ steps.digest.outputs.arm64 }}
|
||||
steps:
|
||||
- id: digest
|
||||
run: echo "arm64=alpine@$(crane digest --platform linux/arm64 alpine:3.22)" >> "$GITHUB_OUTPUT"
|
||||
|
||||
# One image for each architecture. The Go program crosses to the other one with no emulation, and the last stage of the
|
||||
# Dockerfile runs no command (see the Dockerfile).
|
||||
image:
|
||||
needs: [release, base]
|
||||
if: ${{ needs.release.outputs.version != '' }}
|
||||
runs-on: docker
|
||||
strategy:
|
||||
matrix:
|
||||
arch: [amd64, arm64]
|
||||
container:
|
||||
image: gcr.io/kaniko-project/executor:v1.23.2-debug
|
||||
steps:
|
||||
- name: Build the image from the tag and push it
|
||||
- name: Build the image from the tag and push it as X.Y.Z-ARCH
|
||||
env:
|
||||
VERSION: ${{ needs.release.outputs.version }}
|
||||
ARCH: ${{ matrix.arch }}
|
||||
ARM64_BASE: ${{ needs.base.outputs.arm64 }}
|
||||
REGISTRY_USER: squid
|
||||
REGISTRY_TOKEN: ${{ secrets.FORGEJO_TOKEN }}
|
||||
run: |
|
||||
|
|
@ -75,14 +97,42 @@ jobs:
|
|||
mkdir -p /kaniko/.docker
|
||||
auth="$(printf '%s:%s' "$REGISTRY_USER" "$REGISTRY_TOKEN" | base64 | tr -d '\n')"
|
||||
printf '{"auths":{"git.kmalloc.org":{"auth":"%s"}}}' "$auth" > /kaniko/.docker/config.json
|
||||
image=git.kmalloc.org/squid/sonda
|
||||
minor="${VERSION%.*}"
|
||||
base=alpine:3.22
|
||||
if [ "$ARCH" = arm64 ]; then base="$ARM64_BASE"; fi
|
||||
/kaniko/executor \
|
||||
--dockerfile Dockerfile \
|
||||
--context "git://git.kmalloc.org/squid/sonda.git#refs/tags/v${VERSION}" \
|
||||
--build-arg "VERSION=${VERSION}" \
|
||||
--build-arg "SONDA_ARCH=${ARCH}" \
|
||||
--build-arg "RUNTIME_BASE=${base}" \
|
||||
--label "org.opencontainers.image.version=${VERSION}" \
|
||||
--destination "${image}:${VERSION}" \
|
||||
--destination "${image}:${minor}" \
|
||||
--destination "${image}:latest" \
|
||||
--destination "git.kmalloc.org/squid/sonda:${VERSION}-${ARCH}" \
|
||||
--verbosity info
|
||||
|
||||
# The tags that people use (X.Y.Z, X.Y, latest) point at a list of the two images, so that each machine takes its own.
|
||||
manifest:
|
||||
needs: [release, image]
|
||||
if: ${{ needs.release.outputs.version != '' }}
|
||||
runs-on: docker
|
||||
container:
|
||||
image: gcr.io/go-containerregistry/crane:debug
|
||||
steps:
|
||||
- name: Join the images under X.Y.Z, X.Y and latest
|
||||
env:
|
||||
VERSION: ${{ needs.release.outputs.version }}
|
||||
REGISTRY_USER: squid
|
||||
REGISTRY_TOKEN: ${{ secrets.FORGEJO_TOKEN }}
|
||||
run: |
|
||||
set -eu
|
||||
image=git.kmalloc.org/squid/sonda
|
||||
crane auth login git.kmalloc.org -u "$REGISTRY_USER" -p "$REGISTRY_TOKEN"
|
||||
# Kaniko writes the architecture of the runner in both images: say the right one for each.
|
||||
for arch in amd64 arm64; do
|
||||
crane mutate "${image}:${VERSION}-${arch}" --set-platform "linux/${arch}" --tag "${image}:${VERSION}-${arch}"
|
||||
done
|
||||
crane index append \
|
||||
--tag "${image}:${VERSION}" \
|
||||
--manifest "${image}:${VERSION}-amd64" \
|
||||
--manifest "${image}:${VERSION}-arm64"
|
||||
crane tag "${image}:${VERSION}" "${VERSION%.*}"
|
||||
crane tag "${image}:${VERSION}" latest
|
||||
|
|
|
|||
39
CHANGELOG.md
39
CHANGELOG.md
|
|
@ -1,5 +1,44 @@
|
|||
# Changelog
|
||||
|
||||
## [0.5.0](https://git.kmalloc.org/squid/sonda/compare/v0.4.2...v0.5.0) (2026-11-17)
|
||||
|
||||
### Features
|
||||
|
||||
* **android:** add an Android app ([ba10b8e](https://git.kmalloc.org/squid/sonda/commit/ba10b8ef7bbcf4fe3735e7b931d1b687259cc044)), closes [#47](https://git.kmalloc.org/squid/sonda/issues/47)
|
||||
* **web:** talk to the Android app ([995f2e5](https://git.kmalloc.org/squid/sonda/commit/995f2e5ae013897a56743b60eeedea909fe04db9))
|
||||
|
||||
### Bug fixes
|
||||
|
||||
* **web:** show a restored queue on the lock screen ([a2d6d2a](https://git.kmalloc.org/squid/sonda/commit/a2d6d2a925edf8de89a7a4df0afbfa82004ccf87))
|
||||
|
||||
## [0.4.2](https://git.kmalloc.org/squid/sonda/compare/v0.4.1...v0.4.2) (2026-11-17)
|
||||
|
||||
### Bug fixes
|
||||
|
||||
* **web:** make the seek bar fit the width of a phone ([6220238](https://git.kmalloc.org/squid/sonda/commit/6220238068c446a97d1726697601f7ba5e126028)), closes [#45](https://git.kmalloc.org/squid/sonda/issues/45)
|
||||
|
||||
## [0.4.1](https://git.kmalloc.org/squid/sonda/compare/v0.4.0...v0.4.1) (2026-11-17)
|
||||
|
||||
### Bug fixes
|
||||
|
||||
* **web:** keep the controls of the full-screen player on screen on a phone ([a757743](https://git.kmalloc.org/squid/sonda/commit/a757743161ac3f7a348884a9f7b2490ab09b99e1)), closes [#43](https://git.kmalloc.org/squid/sonda/issues/43)
|
||||
|
||||
## [0.4.0](https://git.kmalloc.org/squid/sonda/compare/v0.3.0...v0.4.0) (2026-11-16)
|
||||
|
||||
### Features
|
||||
|
||||
* **web:** make Sonda feel like an app on a phone ([75a7898](https://git.kmalloc.org/squid/sonda/commit/75a7898c68ca65c8c4efa0f51b72d62ac9a4db6e))
|
||||
|
||||
### Documentation
|
||||
|
||||
* note that this repo moved to git.kmalloc.org ([ee31e0e](https://git.kmalloc.org/squid/sonda/commit/ee31e0ee7caf9e7510b7cfde1f4e096721966e90))
|
||||
|
||||
## [0.3.0](https://git.kmalloc.org/squid/sonda/compare/v0.2.0...v0.3.0) (2026-11-16)
|
||||
|
||||
### Features
|
||||
|
||||
* publish the Docker image for ARM as well ([b37e3db](https://git.kmalloc.org/squid/sonda/commit/b37e3db037a442af439c5183cf4937a9be630a14))
|
||||
|
||||
## [0.2.0](https://git.kmalloc.org/squid/sonda/compare/v0.1.0...v0.2.0) (2026-11-16)
|
||||
|
||||
### Features
|
||||
|
|
|
|||
38
Dockerfile
38
Dockerfile
|
|
@ -1,8 +1,11 @@
|
|||
# syntax=docker/dockerfile:1
|
||||
|
||||
# RUNTIME_BASE is alpine:3.22 unless the CI release asks for another architecture, with the digest of that one.
|
||||
ARG RUNTIME_BASE=alpine:3.22
|
||||
|
||||
# The web app: built once here, written (by its Vite config) to /src/internal/webui/dist, where the Go
|
||||
# build below embeds it. Nothing of Node reaches the final image.
|
||||
FROM node:24-alpine AS web
|
||||
FROM --platform=$BUILDPLATFORM node:24-alpine AS web
|
||||
WORKDIR /src/web
|
||||
COPY web/package.json web/package-lock.json ./
|
||||
RUN npm ci
|
||||
|
|
@ -12,7 +15,13 @@ RUN npm run build
|
|||
# Build: Sonda has no CGO (SQLite, PostgreSQL and the tag reader are pure Go), so the binary
|
||||
# is static and the final image needs nothing but it and the CA certificates. The web app is inside it
|
||||
# (-tags webui), so sondad serves it on / and the API on /api.
|
||||
FROM golang:1.27-alpine AS build
|
||||
FROM --platform=$BUILDPLATFORM golang:1.27-alpine AS build
|
||||
# Built on the machine that builds, for the machine that runs: Go crosses to another architecture with no emulation
|
||||
# (TARGETOS and TARGETARCH come from the platform asked for).
|
||||
ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
# SONDA_ARCH is for a builder that cannot say TARGETARCH (the CI release, see .forgejo/workflows/release.yml).
|
||||
ARG SONDA_ARCH
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
|
@ -22,19 +31,26 @@ COPY migrations ./migrations
|
|||
COPY --from=web /src/internal/webui/dist ./internal/webui/dist
|
||||
# The version of the release (see .releaserc.json), as GET /version and the User-Agent of the providers say it.
|
||||
ARG VERSION=dev
|
||||
RUN CGO_ENABLED=0 go build -tags webui -trimpath -ldflags="-s -w -X main.version=${VERSION}" -o /out/sondad ./cmd/sondad
|
||||
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${SONDA_ARCH:-${TARGETARCH:-amd64}} go build -tags webui -trimpath -ldflags="-s -w -X main.version=${VERSION}" -o /out/sondad ./cmd/sondad
|
||||
|
||||
FROM alpine:3.22
|
||||
# What the image has besides the program (the zones of tzdata, the user, the folders) is made here, on the machine that
|
||||
# builds. The last stage then runs no command, so it can be built for another architecture with no emulation.
|
||||
FROM --platform=$BUILDPLATFORM alpine:3.22 AS rootfs
|
||||
RUN apk add --no-cache tzdata \
|
||||
&& addgroup -S -g 10001 sonda \
|
||||
&& adduser -S -u 10001 -G sonda -h /data -H sonda \
|
||||
&& mkdir -p /out/data /out/music
|
||||
|
||||
FROM ${RUNTIME_BASE}
|
||||
LABEL org.opencontainers.image.title="Sonda" \
|
||||
org.opencontainers.image.description="A privacy-first, self-hosted music server" \
|
||||
org.opencontainers.image.source="https://git.kmalloc.org/squid/sonda"
|
||||
# ca-certificates: the providers an admin may switch on are reached over https.
|
||||
# tzdata: log times in the zone the operator sets with TZ.
|
||||
RUN apk add --no-cache ca-certificates tzdata \
|
||||
&& addgroup -S -g 10001 sonda \
|
||||
&& adduser -S -u 10001 -G sonda -h /data -H sonda \
|
||||
&& mkdir -p /data /music \
|
||||
&& chown sonda:sonda /data
|
||||
# The certificates of alpine (ca-certificates-bundle) are what the providers an admin may switch on are reached with,
|
||||
# over https. tzdata is for the log times in the zone the operator sets with TZ.
|
||||
COPY --from=rootfs /usr/share/zoneinfo /usr/share/zoneinfo
|
||||
COPY --from=rootfs /etc/passwd /etc/group /etc/shadow /etc/
|
||||
COPY --from=rootfs --chown=10001:10001 /out/data /data
|
||||
COPY --from=rootfs /out/music /music
|
||||
COPY --from=build /out/sondad /usr/local/bin/sondad
|
||||
|
||||
# Everything Sonda writes is under /data: the database and the files it makes (covers, pictures).
|
||||
|
|
|
|||
29
README.md
29
README.md
|
|
@ -1,5 +1,9 @@
|
|||
# Sonda
|
||||
|
||||
**This repository has moved.** Development continues at
|
||||
[git.kmalloc.org/squid/sonda](https://git.kmalloc.org/squid/sonda); this
|
||||
Codeberg mirror is archived and read-only.
|
||||
|
||||
Sonda is a privacy-first, self-hosted music server written in Go. It streams the music you already own, from your own disks, and keeps everything it knows about you on your own machine.
|
||||
|
||||
It is a single executable, one database (SQLite by default, PostgreSQL if you prefer) and the folders that hold your music. There is no Redis, no message broker and no cloud account.
|
||||
|
|
@ -65,7 +69,7 @@ A plain `go build -o bin/sondad ./cmd/sondad` needs no Node and gives the API on
|
|||
|
||||
The image holds `sondad` with the web app inside, and nothing else: it builds the app itself, so you need no Node on your machine. It runs as an ordinary user, keeps the database and the files Sonda makes under `/data`, listens on port 8080 inside the container, and does not start without `SONDA_JWT_SECRET`.
|
||||
|
||||
The image is published on the registry of git.kmalloc.org, so you do not have to build it: `git.kmalloc.org/squid/sonda:latest` is the last one, and `git.kmalloc.org/squid/sonda:<commit>` (the first seven characters of a commit of `main`) is one you can stay on. Anyone can pull it, with no login. To build it yourself instead, run `docker build -t sonda .` and use `sonda` where the commands below say `git.kmalloc.org/squid/sonda:latest`.
|
||||
The image is published on the registry of git.kmalloc.org, so you do not have to build it: `git.kmalloc.org/squid/sonda:latest` is the last one, and `git.kmalloc.org/squid/sonda:0.3.0` (a version from [CHANGELOG.md](CHANGELOG.md)) is one you can stay on. It runs on amd64 and on arm64 (a Raspberry Pi, an ARM NAS): one tag gives each machine its own. Anyone can pull it, with no login. To build it yourself instead, run `docker build -t sonda .` and use `sonda` where the commands below say `git.kmalloc.org/squid/sonda:latest`.
|
||||
|
||||
```bash
|
||||
docker pull git.kmalloc.org/squid/sonda:latest
|
||||
|
|
@ -390,7 +394,7 @@ Back up the database and `sonda.toml`. Your music stays wherever you keep it.
|
|||
|
||||
## Web app
|
||||
|
||||
A web client lives in `web/` (React, Vite and shadcn/ui). It talks only to the Sonda server: no CDN, no external fonts, no analytics. With the app built into the binary (`make build`, or the Docker image) `sondad` serves it: open the address of the server, for example `http://127.0.0.1:8080`, and sign in. The page carries a strict Content-Security-Policy (only resources of the server itself, no inline scripts), cannot be framed and sends no referrer, and a link inside the app (`/admin/users`) works when you reload it. The player draws a live picture of what is playing, in the browser, from the sound itself (Web Audio). Settings has a choice for it, kept on the device: a spectrogram (the last seconds, low notes at the bottom, the default), the classic bars with falling caps, an oscilloscope, Milkdrop, or nothing. The bars, the oscilloscope and the spectrogram take the colour of this server or, for a friend's song, the colour of the friend. Milkdrop fills the background of the full-screen player with the pictures Winamp made famous, drawn on the device with [butterchurn](https://github.com/jberg/butterchurn) (MIT) and its presets, which are part of the app: nothing is fetched from outside. It is loaded only when chosen, and anyone who asked the system for less motion gets the spectrogram instead, because the pictures flash. The player listens to the sound and changes nothing of it, and draws only while something plays.
|
||||
A web client lives in `web/` (React, Vite and shadcn/ui). It talks only to the Sonda server: no CDN, no external fonts, no analytics. With the app built into the binary (`make build`, or the Docker image) `sondad` serves it: open the address of the server, for example `http://127.0.0.1:8080`, and sign in. The page carries a strict Content-Security-Policy (only resources of the server itself, no inline scripts), cannot be framed and sends no referrer, and a link inside the app (`/admin/users`) works when you reload it. The player draws a live picture of what is playing, in the browser, from the sound itself (Web Audio). Settings has a choice for it, kept on the device: a spectrogram (the last seconds, low notes at the bottom, the default), the classic bars with falling caps, an oscilloscope, Milkdrop, or nothing. The bars, the oscilloscope and the spectrogram take the colour of this server or, for a friend's song, the colour of the friend. Milkdrop fills the background of the full-screen player with the pictures Winamp made famous, drawn on the device with [butterchurn](https://github.com/jberg/butterchurn) (MIT) and its presets, which are part of the app: nothing is fetched from outside. It is loaded only when chosen, and anyone who asked the system for less motion gets the spectrogram instead, because the pictures flash. The player listens to the sound and changes nothing of it, and draws only while something plays. On a phone the player is one row at the foot of the page (and is not there while nothing plays); a tap or a swipe up opens the full-screen player, a swipe down or the Back gesture closes it, and the lock screen shows the song with its seek bar and follows play and pause. The queue is kept in the browser, so a page that is reloaded, or a tab that Android throws away, comes back paused on the same song at the same second.
|
||||
|
||||
To work on the interface itself, run the Vite dev server next to a running `sondad` (set `SONDA_URL` to where it listens: the default is `http://127.0.0.1:8080`):
|
||||
|
||||
|
|
@ -404,6 +408,25 @@ What it does today: sign in (and create the first account of a new server), brow
|
|||
|
||||
More commands, all run in `web/`: `npm test`, `npm run build`, and `npm run gen:api`, which rebuilds `src/api/schema.d.ts` from the OpenAPI spec (it fetches the generator with `npx`, so it needs the network once). `npm run build` writes to `internal/webui/dist` (git-ignored), not to `web/dist`.
|
||||
|
||||
## Android app
|
||||
|
||||
`android/` holds a small Android app: a Capacitor shell that opens a Sonda server in a WebView, with its own icon and no browser bars. It is for a phone whose browser cannot install a PWA. It has no copy of the web app: it shows the one of your server, so a new version of the server is a new version of the app.
|
||||
|
||||
- The first time it asks for the address of the server (`music.example.org`, or `http://192.168.1.5:8080`) and remembers it. Settings has Change server, shown only inside the app.
|
||||
- It trusts the certificate authorities installed on the phone, so a server behind a private CA opens once the CA is installed (Settings, Security, Install a certificate). It also opens a server on plain `http`.
|
||||
- A WebView has no Media Session API, so the app keeps its own: a notification with the song, its cover and the buttons, and the lock screen and headset buttons work. The music goes on with the screen off. From Android 13 it asks to show notifications; without a yes the music still plays and only the notification is hidden.
|
||||
|
||||
To build it you need Node, a JDK 21 and the Android SDK (platform 36 and its build tools; set `ANDROID_HOME`):
|
||||
|
||||
```bash
|
||||
cd android
|
||||
npm ci
|
||||
npm run apk # android/android/app/build/outputs/apk/debug/app-debug.apk
|
||||
adb install -r android/app/build/outputs/apk/debug/app-debug.apk
|
||||
```
|
||||
|
||||
The APK is signed with the debug key, which is fine for your own phone. It is not part of the release pipeline, because the runner has no Android SDK. `npm test` checks how an address typed by hand is read.
|
||||
|
||||
## Releases
|
||||
|
||||
Every merge into `main` can make a release, by itself (`.forgejo/workflows/release.yml`, [semantic-release](https://semantic-release.gitbook.io)). The next version comes from the messages of the commits since the last one, written as [Conventional Commits](https://www.conventionalcommits.org):
|
||||
|
|
@ -415,7 +438,7 @@ Every merge into `main` can make a release, by itself (`.forgejo/workflows/relea
|
|||
| `feat!: ...`, or `BREAKING CHANGE:` in the body | major (0.2.0 to 1.0.0) |
|
||||
| `docs:`, `test:`, `chore:`, `ci:`, `style:` | none by themselves |
|
||||
|
||||
A release adds its entry to [CHANGELOG.md](CHANGELOG.md), tags the commit (`v0.3.0`), makes the release on the forge, and the image is built from the tag and pushed to `git.kmalloc.org/squid/sonda` as `0.3.0`, `0.3` and `latest`. The version is inside the binary: `GET /version` and the User-Agent of the providers say it (a build that no release made says `dev`). The commit that writes `CHANGELOG.md` says `[skip ci]`. The runner has no Docker, so the image is built with [kaniko](https://github.com/GoogleContainerTools/kaniko). The workflow needs the secret `FORGEJO_TOKEN`: a token of the owner with the scopes `write:repository` and `write:package`.
|
||||
A release adds its entry to [CHANGELOG.md](CHANGELOG.md), tags the commit (`v0.3.0`), makes the release on the forge, and the image is built from the tag and pushed to `git.kmalloc.org/squid/sonda` as `0.3.0`, `0.3` and `latest`. The version is inside the binary: `GET /version` and the User-Agent of the providers say it (a build that no release made says `dev`). The commit that writes `CHANGELOG.md` says `[skip ci]`. The image is made for amd64 and for arm64, and each tag points at both. The runner has no Docker and cannot emulate ARM, so the images are built with [kaniko](https://github.com/GoogleContainerTools/kaniko): the Go program is compiled for ARM with no emulation, the last stage of the Dockerfile runs no command, and a last job (with [crane](https://github.com/google/go-containerregistry)) says the architecture of each image and joins them. The workflow needs the secret `FORGEJO_TOKEN`: a token of the owner with the scopes `write:repository` and `write:package`.
|
||||
|
||||
## Development
|
||||
|
||||
|
|
|
|||
14
android/.gitignore
vendored
Normal file
14
android/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
node_modules/
|
||||
# Build output and local settings of the Android project
|
||||
android/.gradle/
|
||||
android/build/
|
||||
android/app/build/
|
||||
android/local.properties
|
||||
android/.idea/
|
||||
android/captures/
|
||||
android/app/src/main/assets/public/
|
||||
android/app/src/main/assets/capacitor.config.json
|
||||
android/app/src/main/assets/capacitor.plugins.json
|
||||
android/capacitor-cordova-android-plugins/
|
||||
*.keystore
|
||||
*.jks
|
||||
101
android/android/.gitignore
vendored
Normal file
101
android/android/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
# Using Android gitignore template: https://github.com/github/gitignore/blob/HEAD/Android.gitignore
|
||||
|
||||
# Built application files
|
||||
*.apk
|
||||
*.aar
|
||||
*.ap_
|
||||
*.aab
|
||||
|
||||
# Files for the ART/Dalvik VM
|
||||
*.dex
|
||||
|
||||
# Java class files
|
||||
*.class
|
||||
|
||||
# Generated files
|
||||
bin/
|
||||
gen/
|
||||
out/
|
||||
# Uncomment the following line in case you need and you don't have the release build type files in your app
|
||||
# release/
|
||||
|
||||
# Gradle files
|
||||
.gradle/
|
||||
build/
|
||||
|
||||
# Local configuration file (sdk path, etc)
|
||||
local.properties
|
||||
|
||||
# Proguard folder generated by Eclipse
|
||||
proguard/
|
||||
|
||||
# Log Files
|
||||
*.log
|
||||
|
||||
# Android Studio Navigation editor temp files
|
||||
.navigation/
|
||||
|
||||
# Android Studio captures folder
|
||||
captures/
|
||||
|
||||
# IntelliJ
|
||||
*.iml
|
||||
.idea/workspace.xml
|
||||
.idea/tasks.xml
|
||||
.idea/gradle.xml
|
||||
.idea/assetWizardSettings.xml
|
||||
.idea/dictionaries
|
||||
.idea/libraries
|
||||
# Android Studio 3 in .gitignore file.
|
||||
.idea/caches
|
||||
.idea/modules.xml
|
||||
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
|
||||
.idea/navEditor.xml
|
||||
|
||||
# Keystore files
|
||||
# Uncomment the following lines if you do not want to check your keystore files in.
|
||||
#*.jks
|
||||
#*.keystore
|
||||
|
||||
# External native build folder generated in Android Studio 2.2 and later
|
||||
.externalNativeBuild
|
||||
.cxx/
|
||||
|
||||
# Google Services (e.g. APIs or Firebase)
|
||||
# google-services.json
|
||||
|
||||
# Freeline
|
||||
freeline.py
|
||||
freeline/
|
||||
freeline_project_description.json
|
||||
|
||||
# fastlane
|
||||
fastlane/report.xml
|
||||
fastlane/Preview.html
|
||||
fastlane/screenshots
|
||||
fastlane/test_output
|
||||
fastlane/readme.md
|
||||
|
||||
# Version control
|
||||
vcs.xml
|
||||
|
||||
# lint
|
||||
lint/intermediates/
|
||||
lint/generated/
|
||||
lint/outputs/
|
||||
lint/tmp/
|
||||
# lint/reports/
|
||||
|
||||
# Android Profiling
|
||||
*.hprof
|
||||
|
||||
# Cordova plugins for Capacitor
|
||||
capacitor-cordova-android-plugins
|
||||
|
||||
# Copied web assets
|
||||
app/src/main/assets/public
|
||||
|
||||
# Generated Config files
|
||||
app/src/main/assets/capacitor.config.json
|
||||
app/src/main/assets/capacitor.plugins.json
|
||||
app/src/main/res/xml/config.xml
|
||||
2
android/android/app/.gitignore
vendored
Normal file
2
android/android/app/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/build/*
|
||||
!/build/.npmkeep
|
||||
55
android/android/app/build.gradle
Normal file
55
android/android/app/build.gradle
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
namespace = "app.sonda"
|
||||
compileSdk = rootProject.ext.compileSdkVersion
|
||||
defaultConfig {
|
||||
applicationId "app.sonda"
|
||||
minSdkVersion rootProject.ext.minSdkVersion
|
||||
targetSdkVersion rootProject.ext.targetSdkVersion
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
aaptOptions {
|
||||
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.
|
||||
// Default: https://android.googlesource.com/platform/frameworks/base/+/282e181b58cf72b6ca770dc7ca5f91f135444502/tools/aapt/AaptAssets.cpp#61
|
||||
ignoreAssetsPattern = '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~'
|
||||
}
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
flatDir{
|
||||
dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(include: ['*.jar'], dir: 'libs')
|
||||
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
||||
implementation "androidx.coordinatorlayout:coordinatorlayout:$androidxCoordinatorLayoutVersion"
|
||||
implementation "androidx.core:core-splashscreen:$coreSplashScreenVersion"
|
||||
implementation "androidx.media:media:1.7.0"
|
||||
implementation project(':capacitor-android')
|
||||
testImplementation "junit:junit:$junitVersion"
|
||||
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
||||
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
||||
implementation project(':capacitor-cordova-android-plugins')
|
||||
}
|
||||
|
||||
apply from: 'capacitor.build.gradle'
|
||||
|
||||
try {
|
||||
def servicesJSON = file('google-services.json')
|
||||
if (servicesJSON.text) {
|
||||
apply plugin: 'com.google.gms.google-services'
|
||||
}
|
||||
} catch(Exception e) {
|
||||
logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work")
|
||||
}
|
||||
19
android/android/app/capacitor.build.gradle
Normal file
19
android/android/app/capacitor.build.gradle
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN
|
||||
|
||||
android {
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_21
|
||||
targetCompatibility JavaVersion.VERSION_21
|
||||
}
|
||||
}
|
||||
|
||||
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
|
||||
dependencies {
|
||||
implementation project(':capacitor-app')
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (hasProperty('postBuildExtras')) {
|
||||
postBuildExtras()
|
||||
}
|
||||
21
android/android/app/proguard-rules.pro
vendored
Normal file
21
android/android/app/proguard-rules.pro
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
50
android/android/app/src/main/AndroidManifest.xml
Normal file
50
android/android/app/src/main/AndroidManifest.xml
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:networkSecurityConfig="@xml/network_security_config"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
|
||||
<activity
|
||||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode|navigation|density"
|
||||
android:name=".MainActivity"
|
||||
android:label="@string/title_activity_main"
|
||||
android:theme="@style/AppTheme.NoActionBarLaunch"
|
||||
android:launchMode="singleTask"
|
||||
android:exported="true">
|
||||
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
</activity>
|
||||
|
||||
<service
|
||||
android:name=".MediaService"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="mediaPlayback" />
|
||||
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:authorities="${applicationId}.fileprovider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/file_paths"></meta-data>
|
||||
</provider>
|
||||
</application>
|
||||
|
||||
<!-- Permissions -->
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
</manifest>
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package app.sonda;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.webkit.WebView;
|
||||
import com.getcapacitor.BridgeActivity;
|
||||
|
||||
public class MainActivity extends BridgeActivity {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
WebView web = getBridge().getWebView();
|
||||
web.addJavascriptInterface(new SondaNative(this), "SondaNative");
|
||||
// The buttons of the notification and of the lock screen go back to the page.
|
||||
MediaService.listener = (name, ms) -> web.post(() -> web.evaluateJavascript(
|
||||
"window.sondaNativeAction && window.sondaNativeAction('" + name + "'," + ms + ")", null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
MediaService.listener = null;
|
||||
if (isFinishing()) MediaService.end(this);
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
||||
219
android/android/app/src/main/java/app/sonda/MediaService.java
Normal file
219
android/android/app/src/main/java/app/sonda/MediaService.java
Normal file
|
|
@ -0,0 +1,219 @@
|
|||
package app.sonda;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.support.v4.media.MediaMetadataCompat;
|
||||
import android.support.v4.media.session.MediaSessionCompat;
|
||||
import android.support.v4.media.session.PlaybackStateCompat;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.media.app.NotificationCompat.MediaStyle;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Keeps the playback alive while the screen is off or another app is in front, and shows what plays with its
|
||||
* controls in a notification and on the lock screen. The web app does the playing; this only mirrors it, and
|
||||
* passes the buttons back through {@link #listener}.
|
||||
*/
|
||||
public class MediaService extends Service {
|
||||
/** Where a press of a button goes: the name is play, pause, next, previous or seekto. */
|
||||
interface Listener {
|
||||
void onAction(String name, long ms);
|
||||
}
|
||||
|
||||
static volatile Listener listener;
|
||||
|
||||
private static final String CHANNEL = "playback";
|
||||
private static final int NOTIFICATION_ID = 1;
|
||||
private static final String PLAY = "app.sonda.PLAY";
|
||||
private static final String PAUSE = "app.sonda.PAUSE";
|
||||
private static final String NEXT = "app.sonda.NEXT";
|
||||
private static final String PREVIOUS = "app.sonda.PREVIOUS";
|
||||
|
||||
// The latest word of the page, kept here because the service may start after it was said.
|
||||
private static NowPlaying latest;
|
||||
private static MediaService instance;
|
||||
|
||||
private MediaSessionCompat session;
|
||||
private String artworkUrl = "";
|
||||
private Bitmap artwork;
|
||||
|
||||
/** Shows what plays. The first time it starts the service; after that it only updates it. */
|
||||
static void show(Context context, NowPlaying now) {
|
||||
latest = now;
|
||||
if (instance != null) {
|
||||
instance.apply(now);
|
||||
} else {
|
||||
ContextCompat.startForegroundService(context, new Intent(context, MediaService.class));
|
||||
}
|
||||
}
|
||||
|
||||
/** Nothing plays any more: the notification goes. */
|
||||
static void end(Context context) {
|
||||
latest = null;
|
||||
if (instance != null) instance.shutDown();
|
||||
else context.stopService(new Intent(context, MediaService.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
instance = this;
|
||||
createChannel();
|
||||
session = new MediaSessionCompat(this, "Sonda");
|
||||
session.setCallback(new MediaSessionCompat.Callback() {
|
||||
@Override public void onPlay() { send("play", 0); }
|
||||
@Override public void onPause() { send("pause", 0); }
|
||||
@Override public void onSkipToNext() { send("next", 0); }
|
||||
@Override public void onSkipToPrevious() { send("previous", 0); }
|
||||
@Override public void onSeekTo(long pos) { send("seekto", pos); }
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
String action = intent == null ? null : intent.getAction();
|
||||
if (PLAY.equals(action)) send("play", 0);
|
||||
else if (PAUSE.equals(action)) send("pause", 0);
|
||||
else if (NEXT.equals(action)) send("next", 0);
|
||||
else if (PREVIOUS.equals(action)) send("previous", 0);
|
||||
if (latest != null) {
|
||||
apply(latest);
|
||||
} else {
|
||||
// Started with nothing to show, or after the app was killed: Android wants a notification anyway, then it goes.
|
||||
startForeground(NOTIFICATION_ID, notification(null));
|
||||
shutDown();
|
||||
}
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
|
||||
/** The person swiped the app away: the page is gone, so the music and its notification go too. */
|
||||
@Override
|
||||
public void onTaskRemoved(Intent rootIntent) {
|
||||
latest = null;
|
||||
shutDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
instance = null;
|
||||
if (session != null) {
|
||||
session.setActive(false);
|
||||
session.release();
|
||||
session = null;
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void send(String name, long ms) {
|
||||
Listener l = listener;
|
||||
if (l != null) l.onAction(name, ms);
|
||||
}
|
||||
|
||||
private void shutDown() {
|
||||
stopForeground(STOP_FOREGROUND_REMOVE);
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
private void apply(NowPlaying now) {
|
||||
session.setMetadata(
|
||||
new MediaMetadataCompat.Builder()
|
||||
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, now.title)
|
||||
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, now.artist)
|
||||
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, now.album)
|
||||
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, now.durationMs)
|
||||
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, now.artworkUrl.equals(artworkUrl) ? artwork : null)
|
||||
.build());
|
||||
long actions = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE
|
||||
| PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_SEEK_TO;
|
||||
session.setPlaybackState(
|
||||
new PlaybackStateCompat.Builder()
|
||||
.setActions(actions)
|
||||
.setState(now.playing ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED, now.positionNow(), now.playing ? 1f : 0f)
|
||||
.build());
|
||||
session.setActive(true);
|
||||
|
||||
Notification n = notification(now);
|
||||
if (Build.VERSION.SDK_INT >= 29) {
|
||||
startForeground(NOTIFICATION_ID, n, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
|
||||
} else {
|
||||
startForeground(NOTIFICATION_ID, n);
|
||||
}
|
||||
if (!now.artworkUrl.isEmpty() && !now.artworkUrl.equals(artworkUrl)) fetchArtwork(now.artworkUrl);
|
||||
}
|
||||
|
||||
private Notification notification(NowPlaying now) {
|
||||
NotificationCompat.Builder b = new NotificationCompat.Builder(this, CHANNEL)
|
||||
.setSmallIcon(R.drawable.ic_stat_sonda)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
.setOnlyAlertOnce(true)
|
||||
.setShowWhen(false)
|
||||
.setContentIntent(PendingIntent.getActivity(
|
||||
this, 0, new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT));
|
||||
if (now == null) return b.setContentTitle("Sonda").build();
|
||||
b.setContentTitle(now.title).setContentText(now.artist).setSubText(now.album);
|
||||
if (now.artworkUrl.equals(artworkUrl) && artwork != null) b.setLargeIcon(artwork);
|
||||
b.addAction(android.R.drawable.ic_media_previous, getString(R.string.previous), button(PREVIOUS));
|
||||
if (now.playing) b.addAction(android.R.drawable.ic_media_pause, getString(R.string.pause), button(PAUSE));
|
||||
else b.addAction(android.R.drawable.ic_media_play, getString(R.string.play), button(PLAY));
|
||||
b.addAction(android.R.drawable.ic_media_next, getString(R.string.next), button(NEXT));
|
||||
b.setStyle(new MediaStyle().setMediaSession(session.getSessionToken()).setShowActionsInCompactView(0, 1, 2));
|
||||
return b.build();
|
||||
}
|
||||
|
||||
private PendingIntent button(String action) {
|
||||
return PendingIntent.getService(
|
||||
this, action.hashCode(), new Intent(this, MediaService.class).setAction(action), PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
|
||||
private void createChannel() {
|
||||
if (Build.VERSION.SDK_INT < 26) return;
|
||||
NotificationChannel channel = new NotificationChannel(CHANNEL, getString(R.string.channel_playback), NotificationManager.IMPORTANCE_LOW);
|
||||
channel.setShowBadge(false);
|
||||
getSystemService(NotificationManager.class).createNotificationChannel(channel);
|
||||
}
|
||||
|
||||
/** The cover comes from the server with the token in its address; a cover that does not come leaves the notification without one. */
|
||||
private void fetchArtwork(String url) {
|
||||
artworkUrl = url;
|
||||
artwork = null;
|
||||
new Thread(() -> {
|
||||
Bitmap bitmap = null;
|
||||
try {
|
||||
HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
|
||||
c.setConnectTimeout(8000);
|
||||
c.setReadTimeout(8000);
|
||||
try (InputStream in = c.getInputStream()) {
|
||||
bitmap = BitmapFactory.decodeStream(in);
|
||||
} finally {
|
||||
c.disconnect();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// No cover: the notification stays as it is.
|
||||
}
|
||||
final Bitmap result = bitmap;
|
||||
ContextCompat.getMainExecutor(this).execute(() -> {
|
||||
if (instance != this || !url.equals(artworkUrl) || result == null) return;
|
||||
artwork = result;
|
||||
if (latest != null) apply(latest);
|
||||
});
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
51
android/android/app/src/main/java/app/sonda/NowPlaying.java
Normal file
51
android/android/app/src/main/java/app/sonda/NowPlaying.java
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package app.sonda;
|
||||
|
||||
import android.os.SystemClock;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/** What the web app says is playing: the song, whether it plays and where it is. */
|
||||
final class NowPlaying {
|
||||
final String title;
|
||||
final String artist;
|
||||
final String album;
|
||||
final String artworkUrl;
|
||||
final boolean playing;
|
||||
final long durationMs;
|
||||
private final long positionMs;
|
||||
private final long receivedAt;
|
||||
|
||||
private NowPlaying(String title, String artist, String album, String artworkUrl, boolean playing, long positionMs, long durationMs) {
|
||||
this.title = title;
|
||||
this.artist = artist;
|
||||
this.album = album;
|
||||
this.artworkUrl = artworkUrl;
|
||||
this.playing = playing;
|
||||
this.positionMs = positionMs;
|
||||
this.durationMs = durationMs;
|
||||
this.receivedAt = SystemClock.elapsedRealtime();
|
||||
}
|
||||
|
||||
/** Where the song is now: what the page said, plus the time that has passed if it plays. */
|
||||
long positionNow() {
|
||||
long position = playing ? positionMs + (SystemClock.elapsedRealtime() - receivedAt) : positionMs;
|
||||
return durationMs > 0 ? Math.min(position, durationMs) : position;
|
||||
}
|
||||
|
||||
/** The JSON the page sends; null when it is not what was expected. */
|
||||
static NowPlaying parse(String json) {
|
||||
try {
|
||||
JSONObject o = new JSONObject(json);
|
||||
return new NowPlaying(
|
||||
o.optString("title"),
|
||||
o.optString("artist"),
|
||||
o.optString("album"),
|
||||
o.optString("artworkUrl"),
|
||||
o.optBoolean("playing"),
|
||||
Math.max(0, o.optLong("positionMs")),
|
||||
Math.max(0, o.optLong("durationMs")));
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
47
android/android/app/src/main/java/app/sonda/SondaNative.java
Normal file
47
android/android/app/src/main/java/app/sonda/SondaNative.java
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package app.sonda;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.webkit.JavascriptInterface;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
/**
|
||||
* What the web page can ask of the app. A WebView has no Media Session API, so the page tells the app what plays
|
||||
* and the app keeps a notification with the controls, and the playback alive while the screen is off.
|
||||
* It is on every page the WebView loads, which is the server the person chose.
|
||||
*/
|
||||
final class SondaNative {
|
||||
private final Activity activity;
|
||||
private boolean askedForNotifications = false;
|
||||
|
||||
SondaNative(Activity activity) {
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public void update(String json) {
|
||||
NowPlaying now = NowPlaying.parse(json);
|
||||
if (now == null) return;
|
||||
activity.runOnUiThread(() -> {
|
||||
askForNotifications();
|
||||
MediaService.show(activity, now);
|
||||
});
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public void stop() {
|
||||
activity.runOnUiThread(() -> MediaService.end(activity));
|
||||
}
|
||||
|
||||
/** From Android 13 a notification needs a yes. Without it the playback goes on, only the notification is hidden. */
|
||||
private void askForNotifications() {
|
||||
if (askedForNotifications || Build.VERSION.SDK_INT < 33) return;
|
||||
askedForNotifications = true;
|
||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.POST_NOTIFICATIONS}, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
android/android/app/src/main/res/drawable/ic_stat_sonda.xml
Normal file
10
android/android/app/src/main/res/drawable/ic_stat_sonda.xml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="#FFFFFFFF"
|
||||
android:pathData="M12,3v10.55c-0.59,-0.34 -1.27,-0.55 -2,-0.55 -2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4V7h4V3h-6z" />
|
||||
</vector>
|
||||
6
android/android/app/src/main/res/drawable/splash.xml
Normal file
6
android/android/app/src/main/res/drawable/splash.xml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item>
|
||||
<color android:color="#08161D" />
|
||||
</item>
|
||||
</layer-list>
|
||||
12
android/android/app/src/main/res/layout/activity_main.xml
Normal file
12
android/android/app/src/main/res/layout/activity_main.xml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<WebView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@color/ic_launcher_background"/>
|
||||
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
|
||||
</adaptive-icon>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@color/ic_launcher_background"/>
|
||||
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
|
||||
</adaptive-icon>
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue