diff --git a/.editorconfig b/.editorconfig index 005f2c7ad..2b8edd122 100644 --- a/.editorconfig +++ b/.editorconfig @@ -16,5 +16,5 @@ indent_size = 4 trim_trailing_whitespace = false max_line_length = off -[*.{py,java,r,R,kt,xml,kts}] +[*.{py,java,r,R,kt,xml,kts,h,hpp,cpp,qml}] indent_size = 4 diff --git a/.github/ISSUE_TEMPLATE/01-bug-report-android.yml b/.github/ISSUE_TEMPLATE/01-bug-report-android.yml new file mode 100644 index 000000000..5b8d6a264 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/01-bug-report-android.yml @@ -0,0 +1,110 @@ +name: Bug report (Android) +description: Report a bug in the Android app +labels: ["bug", "android"] +body: + - type: markdown + attributes: + value: | + Thanks for taking the time to report a bug. Please fill in as much as you can. + - type: input + id: app-version + attributes: + label: App version + description: "Find this in `Settings → About → Version` in the app, or in your phone's app info." + placeholder: "v0.2.5 (build 46)" + validations: + required: true + - type: dropdown + id: app-source + attributes: + label: App source + options: + - GitHub + - Play + - Built from source + validations: + required: true + - type: input + id: device + attributes: + label: Device + description: Manufacturer and model. + placeholder: "Google Pixel 8 Pro" + validations: + required: true + - type: input + id: android-version + attributes: + label: Android / OS version + description: Include the OEM skin if relevant. + placeholder: "Android 16, OxygenOS 16, ColorOS 16, ..." + validations: + required: true + - type: dropdown + id: root-method + attributes: + label: Root / hook method + options: + - No root (native L2CAP support) + - Magisk + Xposed + - KernelSU + Xposed + - Other (describe in additional context) + validations: + required: true + - type: dropdown + id: airpods-model + attributes: + label: AirPods model + options: + - AirPods (1st gen) + - AirPods (2nd gen) + - AirPods (3rd gen) + - AirPods (4th gen) + - AirPods (4th gen) with ANC + - AirPods Pro (1st gen) + - AirPods Pro 2 (Lightning) + - AirPods Pro 2 (USB-C) + - AirPods Pro 3 + - Other / not sure + validations: + required: true + - type: input + id: firmware + attributes: + label: AirPods firmware + description: Find this under `About` in the app once connected. + placeholder: "8454768" + - type: textarea + id: description + attributes: + label: What happened + description: Describe what you observed and what you expected. Include steps to reproduce if applicable. + validations: + required: true + - type: textarea + id: logs + attributes: + label: Logs + description: | + If you are rooted, give the app root access, open the app, go to `Settings → Troubleshooting → Collect Logs`, and attach the resulting file here. + + Without logs most bugs are very hard to diagnose. If you are not, follow these instructions: + (Needs access to a computer, and USB/Wireless Debugging under developer options enabled) + + Commands: + - Get the uid: Linux/Mac: `adb shell dumpsys package me.kavishdevar.librepods | grep uid` + - Start logs: `adb logcat --uid=,1002 >> librepods-logs.txt` (1002 is for bluetooth) + + Steps for proper logs + - force close the app + - turn off bluetooth + - start logs + - open the app + - turn on bluetooth and connect + + placeholder: Paste log content or attach the file + - type: textarea + id: extra + attributes: + label: Additional context + description: Anything else that might help (screenshots, video, related issues, what you've already tried). diff --git a/.github/ISSUE_TEMPLATE/02-bug-report-linux.yml b/.github/ISSUE_TEMPLATE/02-bug-report-linux.yml new file mode 100644 index 000000000..46aa9a4dd --- /dev/null +++ b/.github/ISSUE_TEMPLATE/02-bug-report-linux.yml @@ -0,0 +1,83 @@ +name: Bug report (Linux) +description: Report a bug in the Linux program +labels: ["bug", "linux"] +title: "[Linux] " +body: + - type: markdown + attributes: + value: | + Thanks for the report. Please fill in as much as you can. + - type: input + id: app-version + attributes: + label: App version + placeholder: "linux-v0.1.0, or linux-rust commit abc1234" + validations: + required: true + - type: dropdown + id: variant + attributes: + label: Variant + options: + - Rust rewrite (`linux-rust` branch) + - QT version (NOT MAINTAINED! issues will be closed) + validations: + required: true + - type: input + id: distro + attributes: + label: Distro and version + placeholder: "Arch Linux, Fedora 41, Ubuntu 24.04, NixOS 25.05" + validations: + required: true + - type: input + id: desktop + attributes: + label: Desktop environment / compositor + placeholder: "GNOME 47 (Wayland), KDE 6 (X11), Hyprland, ..." + validations: + required: true + - type: dropdown + id: install-method + attributes: + label: Install method (only official sources) + options: + - Built from source (`nix` or otherwise) + - Pre-built binary + - AppImage + - Other + validations: + required: true + - type: dropdown + id: airpods-model + attributes: + label: AirPods model + options: + - AirPods (1st gen) + - AirPods (2nd gen) + - AirPods (3rd gen) + - AirPods (4th gen) + - AirPods (4th gen) with ANC + - AirPods Pro (1st gen) + - AirPods Pro 2 (Lightning) + - AirPods Pro 2 (USB-C) + - AirPods Pro 3 + - Other / not sure + validations: + required: true + - type: textarea + id: description + attributes: + label: What happened + description: Describe what you observed and what you expected. Include steps to reproduce if applicable. + validations: + required: true + - type: textarea + id: logs + attributes: + label: Logs and stderr + description: Run the app from a terminal with `--debug` and paste the output. + - type: textarea + id: extra + attributes: + label: Additional context diff --git a/.github/ISSUE_TEMPLATE/03-feature-request.yml b/.github/ISSUE_TEMPLATE/03-feature-request.yml new file mode 100644 index 000000000..682e63839 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/03-feature-request.yml @@ -0,0 +1,31 @@ +name: Feature request +description: Suggest a new feature or improvement +labels: ["enhancement"] +body: + - type: dropdown + id: scope + attributes: + label: Scope + options: + - Android + - Linux + - Both + - Other + validations: + required: true + - type: textarea + id: problem + attributes: + label: Problem or use case + description: What are you trying to do? What is missing or hard today? + validations: + required: true + - type: textarea + id: proposal + attributes: + label: Proposed solution + description: How might it work? UI sketches, behavior, edge cases. + - type: textarea + id: alternatives + attributes: + label: Alternatives considered diff --git a/.github/workflows/ci-android.yml b/.github/workflows/ci-android.yml index 9d098a300..50922efa1 100644 --- a/.github/workflows/ci-android.yml +++ b/.github/workflows/ci-android.yml @@ -1,100 +1,212 @@ -name: Build APK and root module (and create nightly release) +name: Android CI on: push: branches: - '*' + paths: + - 'android/**' + - 'root-module-manual/**' + pull_request: paths: - 'android/**' workflow_dispatch: inputs: - release: - description: 'Create a nightly release' + branch: + description: Branch to build required: true - type: boolean - default: false - custom_notes: - description: 'Custom updates to add to What''s Changed section' - required: false - type: string + default: main workflow_call: jobs: - build-debug-apk: + build: runs-on: ubuntu-latest + outputs: + short_sha: ${{ steps.vars.outputs.short_sha }} + app_version: ${{ steps.version.outputs.app_version }} steps: - uses: actions/checkout@v4 with: submodules: true + ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.branch || github.ref }} - uses: actions/setup-java@v4 with: - distribution: 'zulu' + distribution: zulu java-version: 21 - uses: gradle/actions/setup-gradle@v4 - - name: Build debug APK - run: ./gradlew assembleDebug + - name: Decode keystore + if: github.event_name != 'pull_request' + run: echo "${{ secrets.RELEASE_KEYSTORE_FILE }}" | base64 --decode > android/release.keystore + - name: Setup Android SDK + uses: android-actions/setup-android@v3 + - name: Accept Licenses + run: yes | sdkmanager --licenses + - name: Install NDK + run: sdkmanager "ndk;30.0.14904198" + - name: Create local.properties + if: github.event_name != 'pull_request' + run: | + cat < android/local.properties + RELEASE_STORE_FILE=../release.keystore + RELEASE_STORE_PASSWORD=${{ secrets.RELEASE_STORE_PASSWORD }} + RELEASE_KEY_ALIAS=${{ secrets.RELEASE_KEY_ALIAS }} + RELEASE_KEY_PASSWORD=${{ secrets.RELEASE_KEY_PASSWORD }} + EOF + - name: Build debug APK for PRs + if: github.event_name == 'pull_request' + run: ./gradlew assembleFossDebug working-directory: android - - name: Upload artifact - uses: actions/upload-artifact@v4 + - name: Build release artifacts + if: github.event_name != 'pull_request' + run: ./gradlew packageReleaseArtifacts + working-directory: android + - name: Get app version + id: version + run: echo "app_version=$(grep 'appVersionName =' android/app/build.gradle.kts | sed 's/.*= "\(.*\)"/\1/')" >> $GITHUB_OUTPUT + - id: vars + run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + - uses: actions/upload-artifact@v4 + if: github.event_name != 'pull_request' + with: + name: apk-release + path: release/*release.apk + + - uses: actions/upload-artifact@v4 + if: github.event_name == 'pull_request' + with: + name: apk-debug + path: android/app/build/outputs/apk/foss/debug/app-foss-debug.apk + + - uses: actions/upload-artifact@v4 + if: github.event_name != 'pull_request' + with: + name: apk-debug + path: release/*debug.apk + + - uses: actions/upload-artifact@v4 + if: github.event_name != 'pull_request' + with: + name: root-module-release + path: release/*release.zip + + - uses: actions/upload-artifact@v4 + if: github.event_name != 'pull_request' + with: + name: root-module-debug + path: release/*debug.zip + + - uses: actions/upload-artifact@v4 + if: github.event_name != 'pull_request' with: - name: Debug APK - path: android/app/build/outputs/apk/**/*.apk - nightly-release: + name: release-bundle + path: release/*.aab + + release: + if: github.event_name == 'push' runs-on: ubuntu-latest - if: github.event_name == 'push' && github.ref == 'refs/heads/release-nightly' || github.event_name == 'workflow_dispatch' && github.event.inputs.release == 'true' - needs: build-debug-apk + needs: build permissions: contents: write + steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 + fetch-tags: true - uses: actions/download-artifact@v4 - - name: Export APK_NAME for later use - run: echo "APK_NAME=LibrePods-$(echo ${{ github.sha }} | cut -c1-7).apk" >> $GITHUB_ENV - - name: Rename .apk file - run: mv "./Debug APK/debug/"*.apk "./$APK_NAME" - - name: Decode keystore file - run: echo "${{ secrets.DEBUG_KEYSTORE_FILE }}" | base64 --decode > debug.keystore - - name: Install apksigner - run: sudo apt-get update && sudo apt-get install -y apksigner - - name: Sign APK - run: | - apksigner sign --ks debug.keystore --ks-key-alias androiddebugkey --ks-pass pass:android --key-pass pass:android "./$APK_NAME" - - name: Verify APK - run: apksigner verify "./$APK_NAME" - - name: Fetch the latest non-nightly release tag - id: fetch-tag - run: echo "::set-output name=tag::$(git describe --tags $(git rev-list --tags --max-count=1))" - - name: Retrieve commits since the last release - id: get-commits + with: + name: apk-release + path: artifacts/apk-release + + - uses: actions/download-artifact@v4 + with: + name: apk-debug + path: artifacts/apk-debug + + - uses: actions/download-artifact@v4 + with: + name: root-module-release + path: artifacts/root-module-release + + - uses: actions/download-artifact@v4 + with: + name: root-module-debug + path: artifacts/root-module-debug + - id: prev + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - COMMITS=$(git log ${{ steps.fetch-tag.outputs.tag }}..HEAD --pretty=format:"- %s (%h)" --abbrev-commit) - echo "::set-output name=commits::${COMMITS}" - - name: Prepare release notes - id: release-notes + TAG=$(gh release list \ + --limit 1 \ + --json tagName \ + --jq '.[0].tagName') + + echo "tag=$TAG" >> $GITHUB_OUTPUT + - id: changelog run: | - # Create a temporary file for release notes - NOTES_FILE=$(mktemp) - - # Process custom notes if they exist - if [ "${{ github.event_name }}" == "workflow_dispatch" ] && [ -n "${{ github.event.inputs.custom_notes }}" ]; then - CUSTOM_NOTES="${{ github.event.inputs.custom_notes }}" - - # Check if custom notes already have bullet points or GitHub-style formatting - if echo "$CUSTOM_NOTES" | grep -q "^\*\|^- \|http.*commit\|in #[0-9]\+"; then - # Already formatted, use as is - echo "$CUSTOM_NOTES" > "$NOTES_FILE" - else - # Add bullet point formatting - echo "- $CUSTOM_NOTES" > "$NOTES_FILE" - fi + if [ -z "${{ steps.prev.outputs.tag }}" ]; then + NOTES=$(git log --pretty=format:"- %s ([%h](https://github.com/kavishdevar/librepods/commit/%H))") + else + NOTES=$(git log ${{ steps.prev.outputs.tag }}..HEAD --pretty=format:"- %s ([%h](https://github.com/kavishdevar/librepods/commit/%H))") fi - - echo "notes_file=$NOTES_FILE" >> $GITHUB_OUTPUT - - name: Zip root-module directory - run: sh ./build-magisk-module.sh - - name: Delete release if exist then create release - env: + echo "notes<> $GITHUB_OUTPUT + echo "$NOTES" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + + - id: tag + run: echo "tag=nightly-${{ needs.build.outputs.short_sha }}" >> $GITHUB_OUTPUT + + - env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - gh release view "nightly" && gh release delete "nightly" -y --cleanup-tag - gh release create "nightly" "./$APK_NAME" "./btl2capfix.zip" -p -t "Nightly Release" --notes-file "${{ steps.release-notes.outputs.notes_file }}" --generate-notes + gh release create "${{ steps.tag.outputs.tag }}" \ + artifacts/**/* \ + -t "Nightly ${{ needs.build.outputs.short_sha }}" \ + --notes "${{ steps.changelog.outputs.notes }}" \ + --prerelease + - name: Get timestamp + id: timestamp + run: echo "timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> $GITHUB_OUTPUT + - name: Post to Discord + run: | + curl -X POST "${{ secrets.DISCORD_ANDROID_CI_URL }}?with_components=true" \ + -H "Content-Type: application/json" \ + -d '{ + "embeds": [ + { + "title": "LibrePods Nightly Build", + "description": "Download the latest debug and release APKs.", + "color": 253060, + "fields": [ + { + "name": "Changelog", + "value": "${{ steps.changelog.outputs.notes }}", + "inline": false + } + ], + "timestamp": "${{ steps.timestamp.outputs.timestamp }}", + "footer": { + "text": "GitHub Actions" + } + } + ], + "components": [ + { + "type": 1, + "components": [ + { + "type": 2, + "label": "Download Release APK", + "style": 5, + "url": "https://github.com/kavishdevar/librepods/releases/download/nightly-${{ needs.build.outputs.short_sha }}/LibrePods-FOSS-v${{ needs.build.outputs.app_version }}-release.apk" + }, + { + "type": 2, + "label": "Download Debug APK", + "style": 5, + "url": "https://github.com/kavishdevar/librepods/releases/download/nightly-${{ needs.build.outputs.short_sha }}/LibrePods-FOSS-v${{ needs.build.outputs.app_version }}-debug.apk" + } + ] + } + ] + }' diff --git a/.gitignore b/.gitignore index 853ec03d0..72177552d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,5 @@ -root-module/radare2-5.9.9-android-aarch64.tar.gz -wak.toml -log.txt -btl2capfix.zip -root-module-manual +release .vscode -testing.py .DS_Store CMakeLists.txt.user* diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md deleted file mode 100644 index a75bca6fd..000000000 --- a/CODE_OF_CONDUCT.md +++ /dev/null @@ -1,128 +0,0 @@ -# Contributor Covenant Code of Conduct - -## Our Pledge - -We as members, contributors, and leaders pledge to make participation in our -community a harassment-free experience for everyone, regardless of age, body -size, visible or invisible disability, ethnicity, sex characteristics, gender -identity and expression, level of experience, education, socio-economic status, -nationality, personal appearance, race, religion, or sexual identity -and orientation. - -We pledge to act and interact in ways that contribute to an open, welcoming, -diverse, inclusive, and healthy community. - -## Our Standards - -Examples of behavior that contributes to a positive environment for our -community include: - -* Demonstrating empathy and kindness toward other people -* Being respectful of differing opinions, viewpoints, and experiences -* Giving and gracefully accepting constructive feedback -* Accepting responsibility and apologizing to those affected by our mistakes, - and learning from the experience -* Focusing on what is best not just for us as individuals, but for the - overall community - -Examples of unacceptable behavior include: - -* The use of sexualized language or imagery, and sexual attention or - advances of any kind -* Trolling, insulting or derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or email - address, without their explicit permission -* Other conduct which could reasonably be considered inappropriate in a - professional setting - -## Enforcement Responsibilities - -Community leaders are responsible for clarifying and enforcing our standards of -acceptable behavior and will take appropriate and fair corrective action in -response to any behavior that they deem inappropriate, threatening, offensive, -or harmful. - -Community leaders have the right and responsibility to remove, edit, or reject -comments, commits, code, wiki edits, issues, and other contributions that are -not aligned to this Code of Conduct, and will communicate reasons for moderation -decisions when appropriate. - -## Scope - -This Code of Conduct applies within all community spaces, and also applies when -an individual is officially representing the community in public spaces. -Examples of representing our community include using an official e-mail address, -posting via an official social media account, or acting as an appointed -representative at an online or offline event. - -## Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported to the community leaders responsible for enforcement at -report@kavishdevar.me. -All complaints will be reviewed and investigated promptly and fairly. - -All community leaders are obligated to respect the privacy and security of the -reporter of any incident. - -## Enforcement Guidelines - -Community leaders will follow these Community Impact Guidelines in determining -the consequences for any action they deem in violation of this Code of Conduct: - -### 1. Correction - -**Community Impact**: Use of inappropriate language or other behavior deemed -unprofessional or unwelcome in the community. - -**Consequence**: A private, written warning from community leaders, providing -clarity around the nature of the violation and an explanation of why the -behavior was inappropriate. A public apology may be requested. - -### 2. Warning - -**Community Impact**: A violation through a single incident or series -of actions. - -**Consequence**: A warning with consequences for continued behavior. No -interaction with the people involved, including unsolicited interaction with -those enforcing the Code of Conduct, for a specified period of time. This -includes avoiding interactions in community spaces as well as external channels -like social media. Violating these terms may lead to a temporary or -permanent ban. - -### 3. Temporary Ban - -**Community Impact**: A serious violation of community standards, including -sustained inappropriate behavior. - -**Consequence**: A temporary ban from any sort of interaction or public -communication with the community for a specified period of time. No public or -private interaction with the people involved, including unsolicited interaction -with those enforcing the Code of Conduct, is allowed during this period. -Violating these terms may lead to a permanent ban. - -### 4. Permanent Ban - -**Community Impact**: Demonstrating a pattern of violation of community -standards, including sustained inappropriate behavior, harassment of an -individual, or aggression toward or disparagement of classes of individuals. - -**Consequence**: A permanent ban from any sort of public interaction within -the community. - -## Attribution - -This Code of Conduct is adapted from the [Contributor Covenant][homepage], -version 2.0, available at -https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. - -Community Impact Guidelines were inspired by [Mozilla's code of conduct -enforcement ladder](https://github.com/mozilla/diversity). - -[homepage]: https://www.contributor-covenant.org - -For answers to common questions about this code of conduct, see the FAQ at -https://www.contributor-covenant.org/faq. Translations are available at -https://www.contributor-covenant.org/translations. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 91629e341..000000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,70 +0,0 @@ -# Welcome to LibrePods contributing guide - -Thank you for considering a contribution to LibrePods! Your support helps bring Apple-exclusive AirPods features to Linux and Android. - -Read our [Code of Conduct](./CODE_OF_CONDUCT.md) to keep our community approachable and respectful. - -This guide provides an overview of the contribution workflow, from opening an issue to creating and reviewing a pull request (PR). - -## New contributor guide - -To get an overview of the project, read the [README](./README.md). Here are some resources to help you get started with open-source contributions: - -- [Finding ways to contribute to open source on GitHub](https://docs.github.com/en/get-started/exploring-projects-on-github/finding-ways-to-contribute-to-open-source-on-github) -- [Set up Git](https://docs.github.com/en/get-started/getting-started-with-git/set-up-git) -- [GitHub flow](https://docs.github.com/en/get-started/using-github/github-flow) -- [Collaborating with pull requests](https://docs.github.com/en/github/collaborating-with-pull-requests) - -## Getting started - -To navigate our codebase with confidence, see the [README](./README.md) for setup instructions and usage details. We accept various types of contributions, which don’t always require writing code (like translations). - -To develop for the Android App, Android Studio is the preferred IDE. And you can use any IDE for the linux program, it is just python! - -### Issues - -#### Create a new issue - -If you find a bug or want to suggest a feature, check if an issue already exists by searching through our [existing issues](https://github.com/kavishdevar/librepods/issues). If no relevant issue exists, open a new one and fill in the details. - -#### Solve an issue - -Browse our [issues list](https://github.com/kavishdevar/librepods/issues) to find an interesting issue to work on. Use labels to filter issues and pick one that matches your expertise. If you’d like to work on an issue, open a PR with your solution. - -### Make Changes - -#### Make changes locally - -1. Fork the repository and clone it to your local environment. -``` -git clone https://github.com/kavishdevar/librepods.git -cd AirPods-Like-Normal -``` -2. Create a working branch to start your changes. -``` -git checkout -b your-feature-branch -``` -3. Make your changes, following the existing style and structure. - -4. Test your changes to ensure they work as expected and do not introduce new issues. - -### Commit your changes - -Commit your changes with a descriptive message. - -### Pull Request - -When your changes are ready, create a pull request (PR): -- Fill out the PR template to help reviewers understand your changes. -- If your PR is related to an issue, don’t forget to [link your PR to it](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue). -- Enable the checkbox to allow maintainers to edit your PR, so any required changes can be merged easily. - -Once your PR is open, a team member will review it. They may ask questions or request additional information. - -- If changes are requested, apply them in your fork and commit them to the PR branch. -- Mark conversations as resolved as you apply feedback. -- For merge conflicts, follow this [git tutorial](https://github.com/skills/resolve-merge-conflicts) to resolve them. - -### Your PR is merged! - -Congratulations! :tada: Once merged, your contributions will be publicly available in LibrePods. \ No newline at end of file diff --git a/LICENSE b/LICENSE index 29ebfa545..81aaff562 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ - GNU AFFERO GENERAL PUBLIC LICENSE - Version 3, 19 November 2007 + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies @@ -7,15 +7,17 @@ Preamble - The GNU Affero General Public License is a free, copyleft license for -software and other kinds of works, specifically designed to ensure -cooperation with the community in the case of network server software. + The GNU General Public License is a free, copyleft license for +software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, -our General Public Licenses are intended to guarantee your freedom to +the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free -software for all its users. +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you @@ -24,34 +26,44 @@ them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. - Developers that use our General Public Licenses protect your rights -with two steps: (1) assert copyright on the software, and (2) offer -you this License which gives you legal permission to copy, distribute -and/or modify the software. - - A secondary benefit of defending all users' freedom is that -improvements made in alternate versions of the program, if they -receive widespread use, become available for other developers to -incorporate. Many developers of free software are heartened and -encouraged by the resulting cooperation. However, in the case of -software used on network servers, this result may fail to come about. -The GNU General Public License permits making a modified version and -letting the public access it on a server without ever releasing its -source code to the public. - - The GNU Affero General Public License is designed specifically to -ensure that, in such cases, the modified source code becomes available -to the community. It requires the operator of a network server to -provide the source code of the modified version running there to the -users of that server. Therefore, public use of a modified version, on -a publicly accessible server, gives the public access to the source -code of the modified version. - - An older license, called the Affero General Public License and -published by Affero, was designed to accomplish similar goals. This is -a different license, not a version of the Affero GPL, but Affero has -released a new version of the Affero GPL which permits relicensing under -this license. + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. @@ -60,7 +72,7 @@ modification follow. 0. Definitions. - "This License" refers to version 3 of the GNU Affero General Public License. + "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. @@ -537,45 +549,35 @@ to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. - 13. Remote Network Interaction; Use with the GNU General Public License. - - Notwithstanding any other provision of this License, if you modify the -Program, your modified version must prominently offer all users -interacting with it remotely through a computer network (if your version -supports such interaction) an opportunity to receive the Corresponding -Source of your version by providing access to the Corresponding Source -from a network server at no charge, through some standard or customary -means of facilitating copying of software. This Corresponding Source -shall include the Corresponding Source for any work covered by version 3 -of the GNU General Public License that is incorporated pursuant to the -following paragraph. + 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed -under version 3 of the GNU General Public License into a single +under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, -but the work with which it is combined will remain governed by version -3 of the GNU General Public License. +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of -the GNU Affero General Public License from time to time. Such new versions -will be similar in spirit to the present version, but may differ in detail to +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU Affero General +Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the -GNU Affero General Public License, you may choose any version ever published +GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future -versions of the GNU Affero General Public License can be used, that proxy's +versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. @@ -633,29 +635,40 @@ the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. + GNU General Public License for more details. - You should have received a copy of the GNU Affero General Public License + You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. - If your software can interact with users remotely through a computer -network, you should also make sure that it provides a way for users to -get its source. For example, if your program is a web application, its -interface could display a "Source" link that leads users to an archive -of the code. There are many ways you could offer source, and different -solutions will be better for different programs; see section 13 for the -specific requirements. + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU AGPL, see -. \ No newline at end of file +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. \ No newline at end of file diff --git a/Proximity Pairing Message.md b/Proximity Pairing Message.md deleted file mode 100644 index 4f34b6169..000000000 --- a/Proximity Pairing Message.md +++ /dev/null @@ -1,164 +0,0 @@ -# Bluetooth Low Energy (BLE) - Apple Proximity Pairing Message - -This document describes how the AirPods BLE "Proximity Pairing Message" is parsed and interpreted in the application. This message is broadcast by Apple devices (such as AirPods) and contains key information about the device's state, battery, and other properties. - -## Overview - -When scanning for BLE devices, the application looks for manufacturer data with Apple's ID (`0x004C`). If the data starts with `0x07`, it is identified as a Proximity Pairing Message. The message contains various fields, each representing a specific property of the AirPods. - -## Proximity Pairing Message Structure - -| Byte Index | Field Name | Description | Example Value(s) | -|------------|-------------------------|---------------------------------------------------------|--------------------------| -| 0 | Prefix | Message type (should be `0x07` for proximity pairing) | `0x07` | -| 1 | Length | Length of the message | `0x12` | -| 2 | Pairing Mode | `0x01` = Paired, `0x00` = Pairing mode | `0x01`, `0x00` | -| 3-4 | Device Model | Big-endian: [3]=high, [4]=low | `0x0E20` (AirPods Pro) | -| 5 | Status | Bitfield, see below | `0x62` | -| 6 | Pods Battery Byte | Nibbles for left/right pod battery | `0xA7` | -| 7 | Flags & Case Battery | Upper nibble: case battery, lower: flags | `0xB3` | -| 8 | Lid Indicator | Bits for lid state and open counter | `0x09` | -| 9 | Device Color | Color code | `0x02` | -| 10 | Connection State | Enum, see below | `0x04` | -| 11-26 | Encrypted Payload | 16 bytes, not parsed | | - -## Field Details - -### Device Model - -| Value (hex) | Model Name | -|-------------|--------------------------| -| 0x0220 | AirPods 1st Gen | -| 0x0F20 | AirPods 2nd Gen | -| 0x1320 | AirPods 3rd Gen | -| 0x1920 | AirPods 4th Gen | -| 0x1B20 | AirPods 4th Gen (ANC) | -| 0x0A20 | AirPods Max | -| 0x1F20 | AirPods Max (USB-C) | -| 0x0E20 | AirPods Pro | -| 0x1420 | AirPods Pro 2nd Gen | -| 0x2420 | AirPods Pro 2nd Gen (USB-C) | - -### Status Byte (Bitfield) - -| Bit | Meaning | Value if Set | -|-----|--------------------------------|-------------| -| 0 | Right Pod In Ear (XOR logic) | true | -| 1 | Right Pod In Ear (XOR logic) | true | -| 2 | Both Pods In Case | true | -| 3 | Left Pod In Ear (XOR logic) | true | -| 4 | One Pod In Case | true | -| 5 | Primary Pod (1=Left, 0=Right) | true/false | -| 6 | This Pod In Case | true | - -### Ear Detection Logic - -The in-ear detection uses XOR logic based on: -- Whether the right pod is primary (`areValuesFlipped`) -- Whether this pod is in the case (`isThisPodInTheCase`) - -```cpp -bool xorFactor = areValuesFlipped ^ deviceInfo.isThisPodInTheCase; -deviceInfo.isLeftPodInEar = xorFactor ? (status & 0x08) != 0 : (status & 0x02) != 0; // Bit 3 or 1 -deviceInfo.isRightPodInEar = xorFactor ? (status & 0x02) != 0 : (status & 0x08) != 0; // Bit 1 or 3 -``` - -### Primary Pod - -Determined by bit 5 of the status byte: -- `1` = Left pod is primary -- `0` = Right pod is primary - -This affects: -1. Battery level interpretation (which nibble corresponds to which pod) -2. Microphone assignment -3. Ear detection logic - -### Microphone Status - -The active microphone is determined by: -```cpp -deviceInfo.isLeftPodMicrophone = primaryLeft ^ deviceInfo.isThisPodInTheCase; -deviceInfo.isRightPodMicrophone = !primaryLeft ^ deviceInfo.isThisPodInTheCase; -``` - -### Pods Battery Byte - -- Upper nibble: one pod battery (depends on primary) -- Lower nibble: other pod battery - -| Value | Meaning | -|-------|----------------| -| 0x0-0x9 | 0-90% (x10) | -| 0xA-0xE | 100% | -| 0xF | Not available| - -### Flags & Case Battery Byte - -- Upper nibble: case battery (same encoding as pods) -- Lower nibble: flags - -#### Flags (Lower Nibble) - -| Bit | Meaning | -|-----|--------------------------| -| 0 | Right Pod Charging (XOR) | -| 1 | Left Pod Charging (XOR) | -| 2 | Case Charging | - -### Lid Indicator - -| Bits | Meaning | -|------|------------------------| -| 0-2 | Lid Open Counter | -| 3 | Lid State (0=Open, 1=Closed) | - -### Device Color - -| Value | Color | -|-------|-------------| -| 0x00 | White | -| 0x01 | Black | -| 0x02 | Red | -| 0x03 | Blue | -| 0x04 | Pink | -| 0x05 | Gray | -| 0x06 | Silver | -| 0x07 | Gold | -| 0x08 | Rose Gold | -| 0x09 | Space Gray | -| 0x0A | Dark Blue | -| 0x0B | Light Blue | -| 0x0C | Yellow | -| 0x0D+ | Unknown | - -### Connection State - -| Value | State | -|-------|--------------| -| 0x00 | Disconnected | -| 0x04 | Idle | -| 0x05 | Music | -| 0x06 | Call | -| 0x07 | Ringing | -| 0x09 | Hanging Up | -| 0xFF | Unknown | - -## Example Message - -| Byte Index | Example Value | Description | -|------------|--------------|----------------------------| -| 0 | 0x07 | Proximity Pairing Message | -| 1 | 0x12 | Length | -| 2 | 0x01 | Paired | -| 3-4 | 0x0E 0x20 | AirPods Pro | -| 5 | 0x62 | Status | -| 6 | 0xA7 | Pods Battery | -| 7 | 0xB3 | Flags & Case Battery | -| 8 | 0x09 | Lid Indicator | -| 9 | 0x02 | Device Color | -| 10 | 0x04 | Connection State (Idle) | - ---- - -For further details, see [`BleManager`](linux/ble/blemanager.cpp) and [`BleScanner`](linux/ble/blescanner.cpp). \ No newline at end of file diff --git a/README.md b/README.md index c533b170e..10eb54471 100644 --- a/README.md +++ b/README.md @@ -1,118 +1,236 @@ -![LibrePods Banner](/imgs/banner.png) +>[!IMPORTANT] +Development paused due to lack of time until June 2026 (JEE Advanced). PRs and issues might not be responded to until then. -[![XDA Thread](https://img.shields.io/badge/XDA_Forums-Thread-orange)](https://xdaforums.com/t/app-root-for-now-airpodslikenormal-unlock-apple-exclusive-airpods-features-on-android.4707585/) -[![GitHub release (latest by date)](https://img.shields.io/github/v/release/kavishdevar/librepods)](https://github.com/kavishdevar/librepods/releases/latest) -[![GitHub all releases](https://img.shields.io/github/downloads/kavishdevar/librepods/total)](https://github.com/kavishdevar/librepods/releases) -[![GitHub stars](https://img.shields.io/github/stars/kavishdevar/librepods)](https://github.com/kavishdevar/librepods/stargazers) -[![GitHub issues](https://img.shields.io/github/issues/kavishdevar/librepods)](https://github.com/kavishdevar/librepods/issues) -[![GitHub license](https://img.shields.io/github/license/kavishdevar/librepods)](https://github.com/kavishdevar/librepods/blob/main/LICENSE) -[![GitHub contributors](https://img.shields.io/github/contributors/kavishdevar/librepods)](https://github.com/kavishdevar/librepods/graphs/contributors) +--- + + + + LibrePods + -## What is LibrePods? + -LibrePods unlocks Apple's exclusive AirPods features on non-Apple devices. Get access to noise control modes, adaptive transparency, ear detection, hearing aid, customized transparency mode, battery status, and more - all the premium features you paid for but Apple locked to their ecosystem. +# What is LibrePods? -## Device Compatibility +LibrePods allows you to use AirPods features that are exclusive to Apple devices. It implements the proprietary protocol used to exchange data between AirPods and Apple devices, enabling features like changing noise control modes, fast ear detection, accurate battery status, head gestures, conversational awareness, and more on non-Apple platforms. -| Status | Device | Features | -| ------ | --------------------- | ---------------------------------------------------------- | -| ✅ | AirPods Pro (2nd Gen) | Fully supported and tested | -| ✅ | AirPods Pro (3rd Gen) | Fully supported (except heartrate monitoring) | -| ⚠️ | Other AirPods models | Basic features (battery status, ear detection) should work | +# Feature availability -Most features should work with any AirPods. Currently, I've only got AirPods Pro 2 to test with. +| Feature | Linux | Android | +| ----------------------------------------------------------- | ----- | ------- | +| Changing Listening Mode | ✅ | ✅ | +| Ear detection | ✅ | ✅ | +| Battery status | ✅ | ✅ | +| Renaming AirPods
Note for AndroidOn Android, you need to re-pair your AirPods after renaming them because Android might not use the latest name.
| ✅ | ✅ | +| Loud Sound Reduction | 🔴 | ⚪ | +| Head Gestures | ⛔ | ✅ | +| Conversational Awareness | ✅ | ✅ | +| Automatically connect to AirPods | ✅ | ✅ | +| Hearing Aid | 🔴 | ⚪ | +| Transparency Mode customization | 🔴 | ⚪ | +| Multi-device connectivity (Bluetooth Multipoint; 2 devices only) | ⚪ | ⚪ | +|
Other accessibility configs (click to expand)
  • Press speed
  • Press and Hold duration
  • Noise Cancellation with single AirPod
  • Volume control on swipe
  • Volume swipe speed
| 🔴 | ✅ | +|
Other general configs
  • Press and Hold to cycle between listening modes/invoke digital assistant (invoking digital assistant needs a recent firmware)
  • Configure call controls
  • Personalized volume
  • Loud Sound Reduction (needs VendorID spoofing)
  • Microphone side
  • Pause media when falling asleep (needs a recent firmware)
  • Enable Off listening mode to switch to Off
| 🔴 | ✅ | +| [Head-tracked Spatial Audio](#spatial-audio) | ❓ | ❓ | +| [Heart Rate Monitoring](#heart-rate-monitoring) | ⛔ | 🔴 | +| [Find My](#find-my) | ❓ | ❓ | +| [High quality two-way audio](#high-quality-two-way-audio) | 🔴 | 🔴 | -## Key Features +| Symbol | Meaning | +| ------ | ------------------------------------------------------------------- | +| ✅ | Implemented and works well | +| ⚪ | Needs [VendorID spoofing](#vendorid-spoofing); use at your own risk | +| 🔴 | Not implemented yet; planned | +| ⛔ | Will not be implemented | +| ❓ | Unknown | -- **Noise Control Modes**: Easily switch between noise control modes without having to reach out to your AirPods to long press -- **Ear Detection**: Controls your music automatically when you put your AirPods in or take them out, and switch to phone speaker when you take them out -- **Battery Status**: Accurate battery levels -- **Head Gestures**: Answer calls just by nodding your head -- **Conversational Awareness**: Volume automatically lowers when you speak -- **Hearing Aid\*** -- **Customize Transparency Mode\*** -- **Multi-device connectivity\*** (upto 2 devices) -- **Other customizations**: - - Rename your AirPods - - Customize long-press actions - - Few accessibility features - - And more! +## Find My -See our [pinned issue](https://github.com/kavishdevar/librepods/issues/20) for a complete feature list and roadmap. +The following features related to Find My are planned, but require further RE and might need root on Android: -## Platform Support +- Add your AirPods to the Find My network +- Play sound through charging case to find it +- Notify when leaving behind +- Toggle case charging sounds -### Linux +## Spatial Audio -The Linux version runs as a system tray app. Connect your AirPods and enjoy: +The app does not currently provide head tracking information to Android for the OS to perform HRTF. This has not been explored completely, and it might need root. -- Battery monitoring -- Automatic Ear detection -- Conversational Awareness -- Switching Noise Control modes -- Device renaming +Spatializing stereo sound is beyond this project's scope and will never be available. Many OEMs have an implementation of their own for this. -> [!NOTE] -> Work in progress, but core functionality is stable and usable. +## Heart Rate Monitoring (AirPods Pro 3 and later) +This is being worked upon, check the #⁠reverse-engineering channel on the LibrePods Discord server for more information. If it is ever implemented, it will most likely need root on Android. -For installation and detailed info, see the [Linux README](/linux/README.md). +## High quality two-way audio +On iOS/iPadOS, you can continue using A2DP while AirPods send the audio stream from its microphone over AACP. -### Android +Since this needs deeper integration with audio on Android, it will most likely need root. -#### Screenshots +# Installation -| | | | -| -------------------------------------------------------------------------------------- | ------------------------------------------------- | --------------------------------------------------------------------------- | -| ![Settings 1](/android/imgs/settings-1.png) | ![Settings 2](/android/imgs/settings-2.png) | ![Debug Screen](/android/imgs/debug.png) | -| ![Battery Notification and QS Tile for NC Mode](/android/imgs/notification-and-qs.png) | ![Popup](/android/imgs/popup.png) | ![Head Tracking and Gestures](/android/imgs/head-tracking-and-gestures.png) | -| ![Long Press Configuration](/android/imgs/long-press.png) | ![Widget](/android/imgs/widget.png) | ![Customizations 1](/android/imgs/customizations-1.png) | -| ![Customizations 2](/android/imgs/customizations-2.png) | ![accessibility](/android/imgs/accessibility.png) | ![transparency](/android/imgs/transparency.png) | -| ![hearing-aid](/android/imgs/hearing-aid.png) | ![hearing-test](/android/imgs/hearing-test.png) | ![hearing-aid-adjustments](/android/imgs/hearing-aid-adjustments.png) | +- [**Android**](/android/README.md) +- [**Linux**](/linux/README.md) +# VendorID Spoofing -here's a very unprofessional demo video +Turns out, if you change the VendorID in DID Profile to that of Apple, you get access to several special features! -https://github.com/user-attachments/assets/43911243-0576-4093-8c55-89c1db5ea533 +You can do this on Linux by editing the DeviceID in `/etc/bluetooth/main.conf`. Add this line to the config file `DeviceID = bluetooth:004C:0000:0000`. For android you can enable the `act as Apple device` setting in the app's settings (shown only when Xposed is available and LibrePods module is enabled). -#### Root Requirement - -> [!CAUTION] -> **You must have a rooted device with Xposed to use LibrePods on Android.** This is due to a [bug in the Android Bluetooth stack](https://issuetracker.google.com/issues/371713238). Please upvote the issue by clicking the '+1' icon on the IssueTracker page. -> -> There are **no exceptions** to the root requirement until Google merges the fix. - -Until then, you must xposed. I used to provide a non-xposed method too, where the module used overlayfs to replace the bluetooth library with a locally patched one, but that was broken due to how various devices handled overlayfs and a patched library. With xposed, you can also enable the DID hook enabling a few extra features. - -## Bluetooth DID (Device Identification) Hook - -Turns out, if you change the manufacturerid to that of Apple, you get access to several special features! - -### Multi-device Connectivity +## Multi-device Connectivity Upto two devices can be simultaneously connected to AirPods, for audio and control both. Seamless connection switching. The same notification shows up on Apple device when Android takes over the AirPods as if it were an Apple device ("Move to iPhone"). Android also shows a popup when the other device takes over. -### Accessibility Settings and Hearing Aid +## Accessibility Settings and Hearing Aid Accessibility settings like customizing transparency mode (amplification, balance, tone, conversation boost, and ambient noise reduction), and loud sound reduction can be configured. -All hearing aid customizations can be done from Android, including setting the audiogram result. The app doesn't provide a way to take a hearing test because it requires much more precision. It is much better to use an already available audiogram result. - -To enable these features, enable App Settings -> `act as Apple Device`. - -#### A few notes - -- Due to recent AirPods' firmware upgrades, you must enable `Off listening mode` to switch to `Off`. This is because in this mode, louds sounds are not reduced. - -- If you have take both AirPods out, the app will automatically switch to the phone speaker. But, Android might keep on trying to connect to the AirPods because the phone is still connected to them, just the A2DP profile is not connected. The app tries to disconnect the A2DP profile as soon as it detects that Android has connected again if they're not in the ear. - -- When renaming your AirPods through the app, you'll need to re-pair them with your phone for the name change to take effect. This is a limitation of how Bluetooth device naming works on Android. - -- If you want the AirPods icon and battery status to show in Android Settings app, install the app as a system app by using the root module. - -## Star History - -[![Star History Chart](https://api.star-history.com/svg?repos=kavishdevar/librepods&type=Date)](https://star-history.com/#kavishdevar/librepods&Date) +All hearing aid customizations can be done from Android (linux soon), including setting the audiogram result. The app doesn't provide a way to take a hearing test because it requires much more precision. It is much better to use an already available audiogram result. + +# Protocol and Reverse Engineering + +Please refer to the Wireshark dissector plugin by Nojus ([@pabloaul](https://github.com/pabloaul)) for more information on the protocols used: [pabloaul/apple-wireshark](https://github.com/pabloaul/apple-wireshark) + +The dissector had not been used in LibrePods for most of the implementation; I had reverse engineered the protocol myself before this dissector was made. But many (future) features including two-way high quality audio and spatial audio would not have been possible without their RE efforts! + +# Use of AI + +## Android app + +These parts of the app were completely AI-generated: +- Head Gestures - all of it, including logic and the UI +- The offset setup with r2+the xposed module (both versions) +- Troubleshooter and LogCollector + +Rest everything- the background service, the Bluetooth manager classes (AACP and ATT), the entire UI, even the smallest components were written manually. + +Some parts of the UI components were borrowed from [Kyant0's demo app](https://github.com/Kyant0/AndroidLiquidGlass/tree/master/catalog), which is licensed under [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). + +## Linux (rewrite) + +The `aacp.rs` and the `att.rs` files were translated from Kotlin to Rust with AI. Some parts of the `media_controller.rs` file, mainly the pulse integration, was also AI-generated. + +# Supporters + +A huge thank you to everyone supporting the project! + + + + + + + + + + + + + + + + + + + + + +
+ + davdroman
+ @davdroman +
+
+ + tedsalmon
+ @tedsalmon +
+
+ + wiless
+ @wiless +
+
+ + SmartMsg
+ @SmartMsg +
+
+ + lunaroyster
+ @lunaroyster +
+
+ + ressiwage
+ @ressiwage +
+
+ + kkjdroid
+ @kkjdroid +
+
+ + CitrusJoules
+ @CitrusJoules +
+
+ + DanielReyesDev
+ @DanielReyesDev +
+
+ + sumitduster
+ @sumitduster +
+
+ + GrifTheDev
+ @GrifTheDev +
+
+ +# Special Thanks +- @tyalie for making the first documentation on the protocol! ([tyalie/AAP-Protocol-Definition](https://github.com/tyalie/AAP-Protocol-Defintion)) +- @rithvikvibhu and folks over at lagrangepoint for helping with the hearing aid feature ([gist](https://gist.github.com/rithvikvibhu/45e24bbe5ade30125f152383daf07016)) +- @devnoname120 for helping with the first root patch +- @timgromeyer for making the first version of the linux app +- @hackclub for hosting [High Seas](https://highseas.hackclub.com) and [Low Skies](https://low-skies.hackclub.com)! +- Of course, everyone who has contributed to the project in any way, including by testing, sharing feedback, or just showing interest! + +# Alternates for other platforms: +- CAPod - A companion app for AirPods on Android. ([play store](https://play.google.com/store/apps/details?id=eu.darken.capod) | [source code](https://github.com/d4rken-org/capod)). Use this if you're using Android version 16 QPR3 or below and are not rooted. +- MagicPods for Steam Deck ([website](https://magicpods.app/steamdeck/)) +- MagicPods - if you're looking for "LibrePods for Windows" ([ms store](https://apps.microsoft.com/store/detail/9P6SKKFKSHKM) [installer](https://magicpods.app/installer/MagicPods.appinstaller) | [website](https://magicpods.app/)) + +# Star History + + + + + + Star History Chart + + # License @@ -120,15 +238,17 @@ LibrePods - AirPods liberated from Apple’s ecosystem Copyright (C) 2025 LibrePods contributors This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published -by the Free Software Foundation, either version 3 of the License. +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. +GNU General Public License for more details. -You should have received a copy of the GNU Affero General Public License -along with this program over [here](/LICENSE). If not, see . +You should have received a copy of the GNU General Public License +along with this program. If not, see . All trademarks, logos, and brand names are the property of their respective owners. Use of them does not imply any affiliation with or endorsement by them. All AirPods images, symbols, and the SF Pro font are the property of Apple Inc. + diff --git a/android/README.md b/android/README.md new file mode 100644 index 000000000..5596f742d --- /dev/null +++ b/android/README.md @@ -0,0 +1,67 @@ +## Root Requirement + +LibrePods *may* require root depending on your device/OS and what features you want access to: + +- Features requiring the VendorID hook ([the features marked with an asterisk here](https://github.com/kavishdevar/librepods#key-features)) will always require root regardless of your device/OS. +- On **ColorOS/OxygenOS 16 and realme UI 7.0** and **Pixel devices on Android 16 QPR3** (with the latest Google Play system update), LibrePods does not need root for most features. +- On other devices, LibrePods needs root because of a bug in the Android Bluetooth stack Fluoride/non-compliance of Apple with Bluetooth standards. You must have Xposed installed for the app to workaround this bug and connect to AirPods. [This issue is being tracked here](https://issuetracker.google.com/issues/371713238). **Please do not comment on the issue thread.** The issue has already been resolved and should be available in **Android 17** for all devices. + +> [!IMPORTANT] +> This workaround with Xposed is not guaranteed to work on all devices. + + +## Installation + +### Google Play Store + +If you are using a supported device/OS combination, you can install LibrePods from the Google Play Store. You can use the VendorID hook features with root even from the Play Store version. + +GetItOnGooglePlay_Badge_Web_color_English + +### GitHub Releases + +If you need xposed because of the [root requirement](#root-requirement), you will have to use the apk/zip from the [GitHub releases](https://github.com/kavishdevar/librepods/releases/latest). + +### As a system app (root module) + +If you want LibrePods to have privileged Bluetooth permissions to +- show battery status in the system settings and widgets +- show AirPods icon in the system settings (xposed is also currently required for this) +- switch audio to phone speakers when you are not wearing your AirPods + +you can install the root module. This is optional and only provides extra features, but it is not required for the app to work. + +> [!IMPORTANT] +> When using the root module, do not install the Play Store version. There might be issues because of the signature mismatch between the Play Store version and the root module. + +## Nightly/Development Builds + +Want to try the latest features before they're officially released? You can grab nightly builds from the [latest nightly release](https://github.com/kavishdevar/librepods/releases?q=nightly). + +> [!WARNING] +> These builds are automatically generated from the latest code and may contain new features and bug fixes that haven't been included in a stable release yet. However, please note that they may also be less stable than official releases, so use them at your own risk. + +## Screenshots + +| | | | +| ------------------------------------------------------------------------------- | ------------------------------------------ | -------------------------------------------------------------------- | +| ![Settings 1](./imgs/settings-1.png) | ![Settings 2](./imgs/settings-2.png) | ![Head Tracking and Gestures](./imgs/head-tracking-and-gestures.png) | +| ![Long Press Configuration](./imgs/long-press.png) | ![Customizations 1](./imgs/customizations-1.png) | ![accessibility](./imgs/accessibility.png) | +| ![transparency](./imgs/transparency.png) | ![hearing-aid](./imgs/hearing-aid.png) | ![hearing-test](./imgs/hearing-test.png) | +| ![hearing-aid-adjustments](./imgs/hearing-aid-adjustments.png) | ![Battery Notification and QS Tile for NC Mode](./imgs/notification-and-qs.png) | ![Widget](./imgs/widget.png) | + + +here's a very unprofessional demo video + +https://github.com/user-attachments/assets/43911243-0576-4093-8c55-89c1db5ea533 + +### Troubleshooting steps for common errors +- Ensure the correct scope is set in LSPosed/Vector. +- Ensure there is no root-hiding module preventing the hook from loading on the Bluetooth app. +- Restart your phone after confirming the scope. + +### A few notes + +- Due to recent AirPods' firmware upgrades, you must enable `Off listening mode` to switch to `Off`. This is because in this mode, loud sounds are not reduced. + +- When renaming your AirPods through the app, you'll need to re-pair them with your phone for the name change to take effect. This is a limitation of how Bluetooth device naming works on Android. diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index fe0c8bd61..40385faac 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -1,42 +1,101 @@ +import java.util.Properties + +val appVersionName = "0.3.0" + plugins { alias(libs.plugins.android.application) - alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) alias(libs.plugins.aboutLibraries) +// alias(libs.plugins.hilt) id("kotlin-parcelize") } +val localPropsFile = rootProject.file("local.properties") +val props = Properties().apply { + if (localPropsFile.exists()) { + load(localPropsFile.inputStream()) + } +} + +val releaseSigningAvailable = listOf( + "RELEASE_STORE_FILE", + "RELEASE_STORE_PASSWORD", + "RELEASE_KEY_ALIAS", + "RELEASE_KEY_PASSWORD" +).all { props[it]?.toString()?.isNotBlank() == true } + android { + signingConfigs { + if (releaseSigningAvailable) { + create("release") { + storeFile = file(props["RELEASE_STORE_FILE"] as String) + storePassword = props["RELEASE_STORE_PASSWORD"] as String + keyAlias = props["RELEASE_KEY_ALIAS"] as String + keyPassword = props["RELEASE_KEY_PASSWORD"] as String + } + } + } namespace = "me.kavishdevar.librepods" - compileSdk = 36 + compileSdk = 37 defaultConfig { applicationId = "me.kavishdevar.librepods" - minSdk = 28 - targetSdk = 36 - versionCode = 8 - versionName = "0.2.0" + targetSdk = 37 + versionCode = 56 + versionName = appVersionName } - buildTypes { release { - isMinifyEnabled = false + isMinifyEnabled = true + isShrinkResources = true proguardFiles( - getDefaultProguardFile("proguard-android-optimize.txt"), - "proguard-rules.pro" + getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) + externalNativeBuild { + cmake { + arguments += "-DCMAKE_BUILD_TYPE=Release" + } + } + if (releaseSigningAvailable) { + signingConfig = signingConfigs.getByName("release") + } + defaultConfig { + minSdk = 33 + } + } + debug { + if (releaseSigningAvailable) { + signingConfig = signingConfigs.getByName("release") + } + versionNameSuffix = "-debug" + defaultConfig { + minSdk = 33 + } } } - compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + productFlavors { + create("foss") { + dimension = "env" + buildConfigField("Boolean", "PLAY_BUILD", "false") + } + create("play") { + dimension = "env" + buildConfigField("Boolean", "PLAY_BUILD", "true") + versionNameSuffix = "-play" + minSdk = 36 + } } - kotlinOptions { - jvmTarget = "1.8" + compileOptions { + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 } buildFeatures { compose = true viewBinding = true + buildConfig = true + } + androidResources { + generateLocaleConfig = true } externalNativeBuild { cmake { @@ -46,18 +105,22 @@ android { } sourceSets { getByName("main") { - res.srcDirs("src/main/res", "src/main/res-apple") + res.directories += "src/main/res-apple" } } + + ndkVersion = "30.0.14904198" + + flavorDimensions += "env" } dependencies { + implementation(platform(libs.androidx.compose.bom)) implementation(libs.accompanist.permissions) - implementation(libs.hiddenapibypass) implementation(libs.androidx.core.ktx) + implementation(libs.androidx.lifecycle.process) implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.activity.compose) - implementation(platform(libs.androidx.compose.bom)) implementation(libs.androidx.ui) implementation(libs.androidx.ui.graphics) implementation(libs.androidx.ui.tooling.preview) @@ -69,21 +132,101 @@ dependencies { implementation(libs.haze.materials) implementation(libs.androidx.dynamicanimation) implementation(libs.androidx.compose.ui) + implementation(libs.androidx.compose.material.icons.core) + implementation(libs.billing) debugImplementation(libs.androidx.compose.ui.tooling) implementation(libs.androidx.compose.foundation.layout) implementation(libs.aboutlibraries) implementation(libs.aboutlibraries.compose.m3) - // compileOnly(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar")))) - // implementation(fileTree(mapOf("dir" to "lib", "include" to listOf("*.aar")))) - compileOnly(files("libs/libxposed-api-100.aar")) - debugImplementation(files("libs/backdrop-debug.aar")) - releaseImplementation(files("libs/backdrop-release.aar")) + implementation(libs.backdrop) +// implementation(libs.hilt) +// implementation(libs.hilt.compiler) + compileOnly(libs.libxposed.api) + implementation(libs.libxposed.service) + implementation(libs.play.review) + implementation(libs.play.review.ktx) } aboutLibraries { - export{ + export { prettyPrint = true excludeFields = listOf("generated") outputFile = file("src/main/res/raw/aboutlibraries.json") } } + +val rootModuleDir = rootProject.file("../root-module-manual") +val releaseDir = rootProject.file("../release") + +fun cap(s: String) = s.replaceFirstChar { it.uppercase() } + +fun registerRootModuleZipTask( + name: String, + flavor: String, + buildType: String +) = tasks.register(name) { + + val variantTask = "assemble${cap(flavor)}${cap(buildType)}" + dependsOn(variantTask) + + val apkPath = "outputs/apk/$flavor/$buildType/app-$flavor-$buildType.apk" + + from(rootModuleDir) + + duplicatesStrategy = DuplicatesStrategy.WARN + + from(layout.buildDirectory.file(apkPath)) { + into("system/priv-app/LibrePods") + rename { "LibrePods.apk" } + } + + delete(layout.buildDirectory.dir("outputs/rootModuleZips")) + + archiveFileName.set("LibrePods-FOSS-v$appVersionName-$buildType.zip") + destinationDirectory.set(layout.buildDirectory.dir("outputs/rootModuleZips")) +} + +val zipRelease = registerRootModuleZipTask( + "zipReleaseModule", + "foss", + "release" +) + +val zipDebug = registerRootModuleZipTask( + "zipDebugModule", + "foss", + "debug" +) + +val collect = tasks.register("collectReleaseArtifacts") { + + dependsOn( + zipRelease, + zipDebug, + "bundlePlayRelease" + ) + + into(releaseDir) + + from(layout.buildDirectory.dir("outputs/apk/foss/release")) { + include("*.apk") + rename(".*", "LibrePods-FOSS-v$appVersionName-release.apk") + } + + from(layout.buildDirectory.dir("outputs/apk/foss/debug")) { + include("*.apk") + rename(".*", "LibrePods-FOSS-v$appVersionName-debug.apk") + } + + from(layout.buildDirectory.dir("outputs/bundle/playRelease")) { + include("*.aab") + } + + from(layout.buildDirectory.dir("outputs/rootModuleZips")) { + include("*.zip") + } +} + +tasks.register("packageReleaseArtifacts") { + dependsOn(collect) +} diff --git a/android/app/libs/backdrop-debug.aar b/android/app/libs/backdrop-debug.aar deleted file mode 100644 index 9ed9a7164..000000000 Binary files a/android/app/libs/backdrop-debug.aar and /dev/null differ diff --git a/android/app/libs/backdrop-release.aar b/android/app/libs/backdrop-release.aar deleted file mode 100644 index bfddcab76..000000000 Binary files a/android/app/libs/backdrop-release.aar and /dev/null differ diff --git a/android/app/libs/libxposed-api-100.aar b/android/app/libs/libxposed-api-100.aar deleted file mode 100644 index 1b03d8a6e..000000000 Binary files a/android/app/libs/libxposed-api-100.aar and /dev/null differ diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro index 481bb4348..e996ec5ba 100644 --- a/android/app/proguard-rules.pro +++ b/android/app/proguard-rules.pro @@ -18,4 +18,6 @@ # If you keep the line number information, uncomment this to # hide the original source file name. -#-renamesourcefileattribute SourceFile \ No newline at end of file +#-renamesourcefileattribute SourceFile + +-keep class me.kavishdevar.librepods.utils.KotlinModule { *; } diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 8474e9107..0474dfd88 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -10,8 +10,6 @@ - @@ -19,6 +17,15 @@ + + + - - - - + + + + + + + tools:ignore="UnusedAttribute" > @@ -62,7 +68,7 @@ @@ -116,17 +122,17 @@ - - - - - - + + + + + + + + + + + +#include +#include +#include + +static JavaVM* gVm = nullptr; + +template +constexpr auto encryptString(const char (&str)[N], char key) { + std::array encrypted{}; + for (size_t i = 0; i < N; i++) { + encrypted[i] = str[i] ^ key; + } + return encrypted; +} + +template +static std::string decryptString(const std::array& encrypted, char key) { + std::string result(N - 1, '\0'); + for (size_t i = 0; i < N - 1; i++) { + result[i] = encrypted[i] ^ key; + } + return result; +} + +#define ENC(str) encryptString(str, 0x47) +#define DEC(arr) decryptString(arr, 0x47).c_str() + +__attribute__((visibility("hidden"))) +static JavaVM* getVm() { return gVm; } + +__attribute__((visibility("default"))) +JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { + gVm = vm; + + auto fn = [](void*) -> void* { + constexpr auto c1 = ENC("dalvik/system/VMRuntime"); + constexpr auto c2 = ENC("getRuntime"); + constexpr auto c3 = ENC("()Ldalvik/system/VMRuntime;"); + constexpr auto c4 = ENC("setHiddenApiExemptions"); + constexpr auto c5 = ENC("([Ljava/lang/String;)V"); + constexpr auto c6 = ENC("java/lang/String"); + constexpr auto c7 = ENC("Landroid/bluetooth/BluetoothSocket;"); + constexpr auto c8 = ENC("Landroid/bluetooth/BluetoothDevice;"); + + JNIEnv* env; + getVm()->AttachCurrentThread(&env, nullptr); + + jclass vmRuntime = env->FindClass(DEC(c1)); + jmethodID getRuntime = env->GetStaticMethodID(vmRuntime, DEC(c2), DEC(c3)); + jmethodID setExemptions = env->GetMethodID(vmRuntime, DEC(c4), DEC(c5)); + + jobject runtime = env->CallStaticObjectMethod(vmRuntime, getRuntime); + jobjectArray prefixes = env->NewObjectArray( + 2, env->FindClass(DEC(c6)), nullptr); + env->SetObjectArrayElement(prefixes, 0, env->NewStringUTF(DEC(c7))); + env->SetObjectArrayElement(prefixes, 1, env->NewStringUTF(DEC(c8))); + + env->CallVoidMethod(runtime, setExemptions, prefixes); + getVm()->DetachCurrentThread(); + return nullptr; + }; + + pthread_t t; + pthread_create(&t, nullptr, fn, nullptr); + pthread_join(t, nullptr); + return JNI_VERSION_1_6; +} diff --git a/android/app/src/main/cpp/l2c_fcr_hook.cpp b/android/app/src/main/cpp/l2c_fcr_hook.cpp index 70fb3fdd6..529f86270 100644 --- a/android/app/src/main/cpp/l2c_fcr_hook.cpp +++ b/android/app/src/main/cpp/l2c_fcr_hook.cpp @@ -1,491 +1,435 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#include -#include -#include + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + #include -#include +#include #include -#include +#include +#include +#include +#include +#include +#include +#include + #include "l2c_fcr_hook.h" -#include -#include -#define LOG_TAG "AirPodsHook" +extern "C" { +#include "xz.h" +} + +#define LOG_TAG "LibrePodsHook" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) static HookFunType hook_func = nullptr; -#define L2CEVT_L2CAP_CONFIG_REQ 4 -#define L2CEVT_L2CAP_CONFIG_RSP 15 - -struct t_l2c_lcb; -typedef struct _BT_HDR { - uint16_t event; - uint16_t len; - uint16_t offset; - uint16_t layer_specific; - uint8_t data[]; -} BT_HDR; - -typedef struct { - uint8_t mode; - uint8_t tx_win_sz; - uint8_t max_transmit; - uint16_t rtrans_tout; - uint16_t mon_tout; - uint16_t mps; -} tL2CAP_FCR; - -// Flow spec structure -typedef struct { - uint8_t qos_present; - uint8_t flow_direction; - uint8_t service_type; - uint32_t token_rate; - uint32_t token_bucket_size; - uint32_t peak_bandwidth; - uint32_t latency; - uint32_t delay_variation; -} FLOW_SPEC; - -// Configuration info structure -typedef struct { - uint16_t result; - uint16_t mtu_present; - uint16_t mtu; - uint16_t flush_to_present; - uint16_t flush_to; - uint16_t qos_present; - FLOW_SPEC qos; - uint16_t fcr_present; - tL2CAP_FCR fcr; - uint16_t fcs_present; - uint16_t fcs; - uint16_t ext_flow_spec_present; - FLOW_SPEC ext_flow_spec; -} tL2CAP_CFG_INFO; - -// Basic L2CAP link control block -typedef struct { - bool wait_ack; - // Other FCR fields - not needed for our specific hook -} tL2C_FCRB; - -// Forward declarations for needed types -struct t_l2c_rcb; -struct t_l2c_lcb; - -typedef struct t_l2c_ccb { - struct t_l2c_ccb* p_next_ccb; // Next CCB in the chain - struct t_l2c_ccb* p_prev_ccb; // Previous CCB in the chain - struct t_l2c_lcb* p_lcb; // Link this CCB belongs to - struct t_l2c_rcb* p_rcb; // Registration CB for this Channel - uint16_t local_cid; // Local CID - uint16_t remote_cid; // Remote CID - uint16_t p_lcb_next; // For linking CCBs to an LCB - uint8_t ccb_priority; // Channel priority - uint16_t tx_mps; // MPS for outgoing messages - uint16_t max_rx_mtu; // Max MTU we will receive - // State variables - bool in_use; // True when channel active - uint8_t chnl_state; // Channel state - uint8_t local_id; // Transaction ID for local trans - uint8_t remote_id; // Transaction ID for remote - uint8_t timer_entry; // Timer entry - uint8_t is_flushable; // True if flushable - // Configuration variables - uint16_t our_cfg_bits; // Bitmap of local config bits - uint16_t peer_cfg_bits; // Bitmap of peer config bits - uint16_t config_done; // Configuration bitmask - uint16_t remote_config_rsp_result; // Remote config response result - tL2CAP_CFG_INFO our_cfg; // Our saved configuration options - tL2CAP_CFG_INFO peer_cfg; // Peer's saved configuration options - // Additional control fields - uint8_t remote_credit_count; // Credits sent to peer - tL2C_FCRB fcrb; // FCR info - bool ecoc; // Enhanced Credit-based mode -} tL2C_CCB; - -static uint8_t (*original_l2c_fcr_chk_chan_modes)(void* p_ccb) = nullptr; -static void (*original_l2cu_process_our_cfg_req)(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg) = nullptr; -static void (*original_l2c_csm_config)(tL2C_CCB* p_ccb, uint8_t event, void* p_data) = nullptr; -static void (*original_l2cu_send_peer_info_req)(tL2C_LCB* p_lcb, uint16_t info_type) = nullptr; - -// Add original pointer for BTA_DmSetLocalDiRecord -static tBTA_STATUS (*original_BTA_DmSetLocalDiRecord)(tSDP_DI_RECORD* p_device_info, uint32_t* p_handle) = nullptr; - -uint8_t fake_l2c_fcr_chk_chan_modes(void* p_ccb) { - LOGI("l2c_fcr_chk_chan_modes hooked, returning true."); + +static uint8_t (*original_l2c_fcr_chk_chan_modes)(void *) = nullptr; + +static tBTA_STATUS (*original_BTA_DmSetLocalDiRecord)(tSDP_DI_RECORD *, uint32_t *) = nullptr; + +static std::atomic enableSdpHook(false); + +uint8_t fake_l2c_fcr_chk_chan_modes(void *p_ccb) { + LOGI("fake_l2c_fcr_chk_chan_modes called"); + uint8_t orig = 0; + if (original_l2c_fcr_chk_chan_modes) + orig = original_l2c_fcr_chk_chan_modes(p_ccb); + + LOGI("fake_l2c_fcr_chk_chan_modes: orig = %d, returning 1", orig); return 1; } -void fake_l2cu_process_our_cfg_req(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg) { - original_l2cu_process_our_cfg_req(p_ccb, p_cfg); - p_ccb->our_cfg.fcr.mode = 0x00; - LOGI("Set FCR mode to Basic Mode in outgoing config request"); -} +tBTA_STATUS fake_BTA_DmSetLocalDiRecord(tSDP_DI_RECORD *p_device_info, uint32_t *p_handle) { -void fake_l2c_csm_config(tL2C_CCB* p_ccb, uint8_t event, void* p_data) { - // Call the original function first to handle the specific code path where the FCR mode is checked - original_l2c_csm_config(p_ccb, event, p_data); + LOGI("fake_BTA_DmSetLocalDiRecord called"); - // Check if this happens during CONFIG_RSP event handling - if (event == L2CEVT_L2CAP_CONFIG_RSP) { - p_ccb->our_cfg.fcr.mode = p_ccb->peer_cfg.fcr.mode; - LOGI("Forced compatibility in l2c_csm_config: set our_mode=%d to match peer_mode=%d", - p_ccb->our_cfg.fcr.mode, p_ccb->peer_cfg.fcr.mode); + if (original_BTA_DmSetLocalDiRecord && + enableSdpHook.load(std::memory_order_relaxed)) + original_BTA_DmSetLocalDiRecord(p_device_info, p_handle); + + LOGI("fake_BTA_DmSetLocalDiRecord: modifying vendor to 0x004C, vendor_id_source to 0x0001"); + + if (p_device_info) { + p_device_info->vendor = 0x004C; + p_device_info->vendor_id_source = 0x0001; } -} -// Replacement function that does nothing -void fake_l2cu_send_peer_info_req(tL2C_LCB* p_lcb, uint16_t info_type) { - LOGI("Intercepted l2cu_send_peer_info_req for info_type 0x%04x - doing nothing", info_type); - // Just return without doing anything - return; + LOGI("fake_BTA_DmSetLocalDiRecord: returning status %d", + original_BTA_DmSetLocalDiRecord ? original_BTA_DmSetLocalDiRecord(p_device_info, p_handle) + : BTA_FAILURE); + return original_BTA_DmSetLocalDiRecord ? original_BTA_DmSetLocalDiRecord(p_device_info, + p_handle) + : BTA_FAILURE; } -// New loader for SDP hook offset (persist.librepods.sdp_offset) -uintptr_t loadSdpOffset() { - const char* property_name = "persist.librepods.sdp_offset"; - char value[PROP_VALUE_MAX] = {0}; +static bool decompressXZ(const uint8_t *input, size_t input_size, std::vector &output) { - int len = __system_property_get(property_name, value); - if (len > 0) { - LOGI("Read sdp offset from property: %s", value); - uintptr_t offset; - char* endptr = nullptr; + LOGI("decompressXZ called with input_size: %zu", input_size); - const char* parse_start = value; - if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) { - parse_start = value + 2; - } + xz_crc32_init(); +#ifdef XZ_USE_CRC64 + xz_crc64_init(); +#endif + + struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 64U << 20); + if (!dec) { + LOGE("decompressXZ: xz_dec_init failed"); + return false; + } + LOGI("decompressXZ: xz_dec_init succeeded"); + + struct xz_buf buf{}; + buf.in = input; + buf.in_pos = 0; + buf.in_size = input_size; + + output.resize(input_size * 8); + + buf.out = output.data(); + buf.out_pos = 0; + buf.out_size = output.size(); + + LOGI("decompressXZ: entering decompression loop"); + while (true) { + LOGI("decompressXZ: xz_dec_run iteration, buf.in_pos: %zu, buf.out_pos: %zu", buf.in_pos, + buf.out_pos); + enum xz_ret ret = xz_dec_run(dec, &buf); + + LOGI("decompressXZ: xz_dec_run returned %d", ret); - errno = 0; - offset = strtoul(parse_start, &endptr, 16); + if (ret == XZ_STREAM_END) + break; - if (errno == 0 && endptr != parse_start && *endptr == '\0' && offset > 0) { - LOGI("Parsed sdp offset: 0x%x", offset); - return offset; + if (ret != XZ_OK) { + LOGE("decompressXZ: xz_dec_run error"); + xz_dec_end(dec); + return false; } - LOGE("Failed to parse sdp offset from property value: %s", value); + if (buf.out_pos == buf.out_size) { + size_t old = output.size(); + LOGI("decompressXZ: resizing output to %zu", old * 2); + output.resize(old * 2); + buf.out = output.data(); + buf.out_size = output.size(); + } } - LOGI("No sdp offset property present - skipping SDP hook"); - return 0; + output.resize(buf.out_pos); + xz_dec_end(dec); + LOGI("decompressXZ: decompression successful, output size: %zu", output.size()); + return true; } -// Fake BTA_DmSetLocalDiRecord: set vendor/vendor_id_source then call original -tBTA_STATUS fake_BTA_DmSetLocalDiRecord(tSDP_DI_RECORD* p_device_info, uint32_t* p_handle) { - LOGI("BTA_DmSetLocalDiRecord hooked - forcing vendor fields"); - if (p_device_info) { - p_device_info->vendor = 0x004C; - p_device_info->vendor_id_source = 0x0001; +static bool getLibraryPath(const char *name, std::string &out) { + LOGI("getLibraryPath called with name: %s", name); + + FILE *fp = fopen("/proc/self/maps", "r"); + if (!fp) { + LOGE("getLibraryPath: fopen failed"); + return false; } - LOGI("Set vendor=0x%04x, vendor_id_source=0x%04x", p_device_info->vendor, p_device_info->vendor_id_source); - if (original_BTA_DmSetLocalDiRecord) { - return original_BTA_DmSetLocalDiRecord(p_device_info, p_handle); + + char line[1024]; + + LOGI("getLibraryPath: scanning /proc/self/maps"); + while (fgets(line, sizeof(line), fp)) { + if (strstr(line, name)) { + LOGI("getLibraryPath: found line containing %s", name); + char *path = strchr(line, '/'); + if (path) { + out = path; + out.erase(out.find('\n')); + LOGI("getLibraryPath: path found: %s", out.c_str()); + fclose(fp); + return true; + } + } } - LOGE("Original BTA_DmSetLocalDiRecord not available"); - return BTA_FAILURE; + fclose(fp); + LOGI("getLibraryPath: failed to find path for %s", name); + return false; } -uintptr_t loadHookOffset([[maybe_unused]] const char* package_name) { - const char* property_name = "persist.librepods.hook_offset"; - char value[PROP_VALUE_MAX] = {0}; - - int len = __system_property_get(property_name, value); - if (len > 0) { - LOGI("Read hook offset from property: %s", value); - uintptr_t offset; - char* endptr = nullptr; +static uintptr_t getModuleBase(const char *name) { + LOGI("getModuleBase called with name: %s", name); - const char* parse_start = value; - if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) { - parse_start = value + 2; - } + FILE *fp = fopen("/proc/self/maps", "r"); + if (!fp) { + LOGE("getModuleBase: fopen failed"); + return 0; + } - errno = 0; - offset = strtoul(parse_start, &endptr, 16); + char line[1024]; + uintptr_t base = 0; - if (errno == 0 && endptr != parse_start && *endptr == '\0' && offset > 0) { - LOGI("Parsed offset: 0x%x", offset); - return offset; + LOGI("getModuleBase: scanning /proc/self/maps"); + while (fgets(line, sizeof(line), fp)) { + if (strstr(line, name)) { + base = strtoull(line, nullptr, 16); + LOGI("getModuleBase: found base at 0x%lx", base); + break; } - - LOGE("Failed to parse offset from property value: %s", value); } - LOGI("Using hardcoded fallback offset"); - return 0x00a55e30; + fclose(fp); + LOGI("getModuleBase: failed to find base for %s", name); + return base; } -uintptr_t loadL2cuProcessCfgReqOffset() { - const char* property_name = "persist.librepods.cfg_req_offset"; - char value[PROP_VALUE_MAX] = {0}; +static uint64_t +findSymbolOffsetDynsym(const std::vector &elf, const char *symbol_substring) { - int len = __system_property_get(property_name, value); - if (len > 0) { - LOGI("Read l2cu_process_our_cfg_req offset from property: %s", value); - uintptr_t offset; - char* endptr = nullptr; + LOGI("findSymbolOffsetDynsym called with %s", symbol_substring); - const char* parse_start = value; - if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) { - parse_start = value + 2; - } + auto *eh = reinterpret_cast(elf.data()); + auto *shdr = reinterpret_cast( + elf.data() + eh->e_shoff); - errno = 0; - offset = strtoul(parse_start, &endptr, 16); + const char *shstr = reinterpret_cast( + elf.data() + shdr[eh->e_shstrndx].sh_offset); - if (errno == 0 && endptr != parse_start && *endptr == '\0' && offset > 0) { - LOGI("Parsed l2cu_process_our_cfg_req offset: 0x%x", offset); - return offset; - } + const Elf64_Shdr *dynsym = nullptr; + const Elf64_Shdr *dynstr = nullptr; + + for (int i = 0; i < eh->e_shnum; ++i) { + const char *secname = shstr + shdr[i].sh_name; - LOGE("Failed to parse l2cu_process_our_cfg_req offset from property value: %s", value); + if (!strcmp(secname, ".dynsym")) + dynsym = &shdr[i]; + if (!strcmp(secname, ".dynstr")) + dynstr = &shdr[i]; } - // Return 0 if not found - we'll skip this hook - return 0; -} + if (!dynsym || !dynstr) { + LOGE("findSymbolOffsetDynsym: dynsym or dynstr not found"); + return 0; + } -uintptr_t loadL2cCsmConfigOffset() { - const char* property_name = "persist.librepods.csm_config_offset"; - char value[PROP_VALUE_MAX] = {0}; + auto *symbols = reinterpret_cast( + elf.data() + dynsym->sh_offset); - int len = __system_property_get(property_name, value); - if (len > 0) { - LOGI("Read l2c_csm_config offset from property: %s", value); - uintptr_t offset; - char* endptr = nullptr; + const char *strings = reinterpret_cast( + elf.data() + dynstr->sh_offset); - const char* parse_start = value; - if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) { - parse_start = value + 2; - } + size_t count = dynsym->sh_size / sizeof(Elf64_Sym); - errno = 0; - offset = strtoul(parse_start, &endptr, 16); + LOGI("findSymbolOffsetDynsym: scanning %zu symbols", count); - if (errno == 0 && endptr != parse_start && *endptr == '\0' && offset > 0) { - LOGI("Parsed l2c_csm_config offset: 0x%x", offset); - return offset; - } + for (size_t i = 0; i < count; ++i) { + const char *name = strings + symbols[i].st_name; + + if (strstr(name, symbol_substring) && ELF64_ST_TYPE(symbols[i].st_info) == STT_FUNC) { - LOGE("Failed to parse l2c_csm_config offset from property value: %s", value); + LOGI("findSymbolOffsetDynsym: matched %s @ 0x%lx", name, + (unsigned long) symbols[i].st_value); + + return symbols[i].st_value; + } } - // Return 0 if not found - we'll skip this hook + LOGI("findSymbolOffsetDynsym: no match for %s", symbol_substring); return 0; } -uintptr_t loadL2cuSendPeerInfoReqOffset() { - const char* property_name = "persist.librepods.peer_info_req_offset"; - char value[PROP_VALUE_MAX] = {0}; +static uint64_t findSymbolOffset(const std::vector &elf, const char *symbol_substring) { - int len = __system_property_get(property_name, value); - if (len > 0) { - LOGI("Read l2cu_send_peer_info_req offset from property: %s", value); - uintptr_t offset; - char* endptr = nullptr; + LOGI("findSymbolOffset called with symbol_substring: %s", symbol_substring); - const char* parse_start = value; - if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) { - parse_start = value + 2; - } + auto *eh = reinterpret_cast(elf.data()); + auto *shdr = reinterpret_cast( + elf.data() + eh->e_shoff); - errno = 0; - offset = strtoul(parse_start, &endptr, 16); + const char *shstr = reinterpret_cast( + elf.data() + shdr[eh->e_shstrndx].sh_offset); - if (errno == 0 && endptr != parse_start && *endptr == '\0' && offset > 0) { - LOGI("Parsed l2cu_send_peer_info_req offset: 0x%x", offset); - return offset; - } + const Elf64_Shdr *symtab = nullptr; + const Elf64_Shdr *strtab = nullptr; - LOGE("Failed to parse l2cu_send_peer_info_req offset from property value: %s", value); + LOGI("findSymbolOffset: parsing ELF sections"); + for (int i = 0; i < eh->e_shnum; ++i) { + const char *secname = shstr + shdr[i].sh_name; + if (!strcmp(secname, ".symtab")) + symtab = &shdr[i]; + if (!strcmp(secname, ".strtab")) + strtab = &shdr[i]; } - // Return 0 if not found - we'll skip this hook - return 0; -} - -uintptr_t getModuleBase(const char *module_name) { - FILE *fp; - char line[1024]; - uintptr_t base_addr = 0; - - fp = fopen("/proc/self/maps", "r"); - if (!fp) { - LOGE("Failed to open /proc/self/maps"); + if (!symtab || !strtab) { + LOGE("findSymbolOffset: symtab or strtab not found"); return 0; } + LOGI("findSymbolOffset: found symtab and strtab"); - while (fgets(line, sizeof(line), fp)) { - if (strstr(line, module_name)) { - char *start_addr_str = line; - char *end_addr_str = strchr(line, '-'); - if (end_addr_str) { - *end_addr_str = '\0'; - base_addr = strtoull(start_addr_str, nullptr, 16); - break; - } + auto *symbols = reinterpret_cast( + elf.data() + symtab->sh_offset); + + const char *strings = reinterpret_cast( + elf.data() + strtab->sh_offset); + + size_t count = symtab->sh_size / sizeof(Elf64_Sym); + + LOGI("findSymbolOffset: scanning %zu symbols", count); + for (size_t i = 0; i < count; ++i) { + const char *name = strings + symbols[i].st_name; + + if (strstr(name, symbol_substring) && ELF64_ST_TYPE(symbols[i].st_info) == STT_FUNC) { + + LOGI("findSymbolOffset: matched symbol %s at 0x%lx", name, + (unsigned long) symbols[i].st_value); + + return symbols[i].st_value; } } - fclose(fp); - return base_addr; + LOGI("findSymbolOffset: no match found for %s", symbol_substring); + return 0; } -bool findAndHookFunction(const char *library_name) { +static bool hookLibrary(const char *libname) { + LOGI("hookLibrary called with libname: %s", libname); + if (!hook_func) { - LOGE("Hook function not initialized"); + LOGE("hook_func not initialized"); + return false; + } + + std::string path; + if (!getLibraryPath(libname, path)) { + LOGE("Failed to locate %s", libname); return false; } + LOGI("hookLibrary: located path: %s", path.c_str()); - uintptr_t base_addr = getModuleBase(library_name); - if (!base_addr) { - LOGE("Failed to get base address of %s", library_name); + int fd = open(path.c_str(), O_RDONLY); + if (fd < 0) { + LOGE("hookLibrary: open failed"); return false; } - // Load all offsets from system properties - no hardcoding - uintptr_t l2c_fcr_offset = loadHookOffset(nullptr); - uintptr_t l2cu_process_our_cfg_req_offset = loadL2cuProcessCfgReqOffset(); - uintptr_t l2c_csm_config_offset = loadL2cCsmConfigOffset(); - uintptr_t l2cu_send_peer_info_req_offset = loadL2cuSendPeerInfoReqOffset(); - uintptr_t sdp_offset = loadSdpOffset(); + struct stat st{}; + if (fstat(fd, &st) != 0) { + LOGE("hookLibrary: fstat failed"); + close(fd); + return false; + } + LOGI("hookLibrary: opened file, size: %lld", (long long) st.st_size); - bool success = false; + std::vector file(st.st_size); + read(fd, file.data(), st.st_size); + close(fd); - // Hook l2c_fcr_chk_chan_modes - this is our primary hook - if (l2c_fcr_offset > 0) { - void* target = reinterpret_cast(base_addr + l2c_fcr_offset); - LOGI("Hooking l2c_fcr_chk_chan_modes at offset: 0x%x, base: %p, target: %p", - l2c_fcr_offset, (void*)base_addr, target); + auto *eh = reinterpret_cast(file.data()); + auto *shdr = reinterpret_cast( + file.data() + eh->e_shoff); - int result = hook_func(target, (void*)fake_l2c_fcr_chk_chan_modes, (void**)&original_l2c_fcr_chk_chan_modes); - if (result != 0) { - LOGE("Failed to hook l2c_fcr_chk_chan_modes, error: %d", result); - return false; + const char *shstr = reinterpret_cast( + file.data() + shdr[eh->e_shstrndx].sh_offset); + + uint64_t chk_offset = 0; + uint64_t sdp_offset = 0; + + for (int i = 0; i < eh->e_shnum; ++i) { + if (!strcmp(shstr + shdr[i].sh_name, ".gnu_debugdata")) { + LOGI("hookLibrary: found .gnu_debugdata section"); + + std::vector compressed(file.begin() + shdr[i].sh_offset, + file.begin() + shdr[i].sh_offset + shdr[i].sh_size); + + std::vector decompressed; + + if (decompressXZ(compressed.data(), compressed.size(), decompressed)) { + + chk_offset = findSymbolOffset(decompressed, "l2c_fcr_chk_chan_modes"); + + sdp_offset = findSymbolOffset(decompressed, "BTA_DmSetLocalDiRecord"); + } else { + LOGE("debugdata decompress failed"); + } + + break; } - LOGI("Successfully hooked l2c_fcr_chk_chan_modes"); - success = true; - } else { - LOGE("No valid offset for l2c_fcr_chk_chan_modes found, cannot proceed"); - return false; } - // Hook l2cu_process_our_cfg_req if offset is available - if (l2cu_process_our_cfg_req_offset > 0) { - void* target = reinterpret_cast(base_addr + l2cu_process_our_cfg_req_offset); - LOGI("Hooking l2cu_process_our_cfg_req at offset: 0x%x, base: %p, target: %p", - l2cu_process_our_cfg_req_offset, (void*)base_addr, target); - - int result = hook_func(target, (void*)fake_l2cu_process_our_cfg_req, (void**)&original_l2cu_process_our_cfg_req); - if (result != 0) { - LOGE("Failed to hook l2cu_process_our_cfg_req, error: %d", result); - // Continue even if this hook fails - } else { - LOGI("Successfully hooked l2cu_process_our_cfg_req"); - } - } else { - LOGI("Skipping l2cu_process_our_cfg_req hook as offset is not available"); + if (!chk_offset) { + LOGI("fallback dynsym chk"); + chk_offset = findSymbolOffsetDynsym(file, "l2c_fcr_chk_chan_modes"); } - // Hook l2c_csm_config if offset is available - if (l2c_csm_config_offset > 0) { - void* target = reinterpret_cast(base_addr + l2c_csm_config_offset); - LOGI("Hooking l2c_csm_config at offset: 0x%x, base: %p, target: %p", - l2c_csm_config_offset, (void*)base_addr, target); - - int result = hook_func(target, (void*)fake_l2c_csm_config, (void**)&original_l2c_csm_config); - if (result != 0) { - LOGE("Failed to hook l2c_csm_config, error: %d", result); - // Continue even if this hook fails - } else { - LOGI("Successfully hooked l2c_csm_config"); - } - } else { - LOGI("Skipping l2c_csm_config hook as offset is not available"); + if (!sdp_offset) { + LOGI("fallback dynsym sdp"); + sdp_offset = findSymbolOffsetDynsym(file, "BTA_DmSetLocalDiRecord"); } - // Hook l2cu_send_peer_info_req if offset is available - if (l2cu_send_peer_info_req_offset > 0) { - void* target = reinterpret_cast(base_addr + l2cu_send_peer_info_req_offset); - LOGI("Hooking l2cu_send_peer_info_req at offset: 0x%x, base: %p, target: %p", - l2cu_send_peer_info_req_offset, (void*)base_addr, target); - - int result = hook_func(target, (void*)fake_l2cu_send_peer_info_req, (void**)&original_l2cu_send_peer_info_req); - if (result != 0) { - LOGE("Failed to hook l2cu_send_peer_info_req, error: %d", result); - // Continue even if this hook fails - } else { - LOGI("Successfully hooked l2cu_send_peer_info_req"); - } - } else { - LOGI("Skipping l2cu_send_peer_info_req hook as offset is not available"); + uintptr_t base = getModuleBase(libname); + if (!base) { + LOGE("hookLibrary: getModuleBase failed"); + return false; } - if (sdp_offset > 0) { - void* target = reinterpret_cast(base_addr + sdp_offset); - LOGI("Hooking BTA_DmSetLocalDiRecord at offset: 0x%x, base: %p, target: %p", - sdp_offset, (void*)base_addr, target); + if (chk_offset) { + void *target = reinterpret_cast(base + chk_offset); + hook_func(target, (void *) fake_l2c_fcr_chk_chan_modes, + (void **) &original_l2c_fcr_chk_chan_modes); + LOGI("hooked chk"); + } - int result = hook_func(target, (void*)fake_BTA_DmSetLocalDiRecord, (void**)&original_BTA_DmSetLocalDiRecord); - if (result != 0) { - LOGE("Failed to hook BTA_DmSetLocalDiRecord, error: %d", result); - } else { - LOGI("Successfully hooked BTA_DmSetLocalDiRecord (SDP)"); - } - } else { - LOGI("Skipping BTA_DmSetLocalDiRecord hook as sdp offset is not available"); + if (sdp_offset) { + void *target = reinterpret_cast(base + sdp_offset); + hook_func(target, (void *) fake_BTA_DmSetLocalDiRecord, + (void **) &original_BTA_DmSetLocalDiRecord); + LOGI("hooked sdp"); } - return success; + return chk_offset || sdp_offset; } -void on_library_loaded(const char *name, [[maybe_unused]] void *handle) { - if (strstr(name, "libbluetooth_jni.so")) { - LOGI("Detected Bluetooth JNI library: %s", name); +static void on_library_loaded(const char *name, void *) { + LOGI("on_library_loaded called with name: %s", name); - bool hooked = findAndHookFunction("libbluetooth_jni.so"); - if (!hooked) { - LOGE("Failed to hook Bluetooth JNI library function"); - } - } else if (strstr(name, "libbluetooth_qti.so")) { - LOGI("Detected Bluetooth QTI library: %s", name); + if (strstr(name, "libbluetooth_jni.so")) { + LOGI("Bluetooth JNI loaded"); + hookLibrary("libbluetooth_jni.so"); + } - bool hooked = findAndHookFunction("libbluetooth_qti.so"); - if (!hooked) { - LOGE("Failed to hook Bluetooth QTI library function"); - } + if (strstr(name, "libbluetooth_qti.so")) { + LOGI("Bluetooth QTI loaded"); + hookLibrary("libbluetooth_qti.so"); } } -extern "C" [[gnu::visibility("default")]] [[gnu::used]] -NativeOnModuleLoaded native_init(const NativeAPIEntries* entries) { - LOGI("L2C FCR Hook module initialized"); +extern "C" [[gnu::visibility("default")]] +[[gnu::used]] +NativeOnModuleLoaded native_init(const NativeAPIEntries *entries) { + LOGI("native_init called with entries: %p", entries); + hook_func = (HookFunType) entries->hook_func; + LOGI("LibrePodsNativeHook initialized, sdp hook enabled: %d", + enableSdpHook.load(std::memory_order_relaxed)); + return on_library_loaded; +} - hook_func = entries->hook_func; +extern "C" JNIEXPORT void JNICALL +Java_me_kavishdevar_librepods_utils_NativeBridge_setSdpHook(JNIEnv *, jobject thiz, + jboolean enable) { + LOGI("setSdpHook called with enable: %d", enable); + enableSdpHook.store(enable, std::memory_order_relaxed); - return on_library_loaded; -} \ No newline at end of file + LOGI("sdp hook enabled: %d", enable); +} diff --git a/android/app/src/main/cpp/l2c_fcr_hook.h b/android/app/src/main/cpp/l2c_fcr_hook.h index 2ab325632..0ca78516f 100644 --- a/android/app/src/main/cpp/l2c_fcr_hook.h +++ b/android/app/src/main/cpp/l2c_fcr_hook.h @@ -1,50 +1,52 @@ -#pragma once +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#pragma once #include -#include typedef int (*HookFunType)(void *func, void *replace, void **backup); -typedef int (*UnhookFunType)(void *func); - typedef void (*NativeOnModuleLoaded)(const char *name, void *handle); typedef struct { uint32_t version; - HookFunType hook_func; - UnhookFunType unhook_func; + void* hook_func; + void* unhook_func; } NativeAPIEntries; -[[maybe_unused]] typedef NativeOnModuleLoaded (*NativeInit)(const NativeAPIEntries *entries); +typedef NativeOnModuleLoaded (*NativeInit)(const NativeAPIEntries *entries); -typedef struct t_l2c_ccb tL2C_CCB; -typedef struct t_l2c_lcb tL2C_LCB; - -uintptr_t loadHookOffset(const char* package_name); -uintptr_t getModuleBase(const char *module_name); -uintptr_t loadL2cuProcessCfgReqOffset(); -uintptr_t loadL2cCsmConfigOffset(); -uintptr_t loadL2cuSendPeerInfoReqOffset(); -bool findAndHookFunction(const char *library_path); - -#define SDP_MAX_ATTR_LEN 400 +typedef enum : uint8_t { + BTA_SUCCESS = 0, /* Successful operation. */ + BTA_FAILURE = 1, /* Generic failure. */ + BTA_PENDING = 2, /* API cannot be completed right now */ + BTA_BUSY = 3, + BTA_NO_RESOURCES = 4, + BTA_WRONG_MODE = 5, +} tBTA_STATUS; typedef struct t_sdp_di_record { - uint16_t vendor; - uint16_t vendor_id_source; - uint16_t product; - uint16_t version; - bool primary_record; - char client_executable_url[SDP_MAX_ATTR_LEN]; - char service_description[SDP_MAX_ATTR_LEN]; - char documentation_url[SDP_MAX_ATTR_LEN]; + uint16_t vendor; + uint16_t vendor_id_source; + uint16_t product; + uint16_t version; + bool primary_record; + char client_executable_url[400]; + char service_description[400]; + char documentation_url[400]; } tSDP_DI_RECORD; - -typedef enum : uint8_t { - BTA_SUCCESS = 0, /* Successful operation. */ - BTA_FAILURE = 1, /* Generic failure. */ - BTA_PENDING = 2, /* API cannot be completed right now */ - BTA_BUSY = 3, - BTA_NO_RESOURCES = 4, - BTA_WRONG_MODE = 5, -} tBTA_STATUS; \ No newline at end of file diff --git a/android/app/src/main/cpp/xz/xz.h b/android/app/src/main/cpp/xz/xz.h new file mode 100644 index 000000000..c317c4941 --- /dev/null +++ b/android/app/src/main/cpp/xz/xz.h @@ -0,0 +1,448 @@ +/* SPDX-License-Identifier: 0BSD */ + +/* + * XZ decompressor + * + * Authors: Lasse Collin + * Igor Pavlov + */ + +#ifndef XZ_H +#define XZ_H + +#ifdef __KERNEL__ +# include +# include +#else +# include +# include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* "#define XZ_EXTERN static" can be used to make extern functions static. */ +#ifndef XZ_EXTERN +# define XZ_EXTERN extern +#endif + +/** + * enum xz_mode - Operation mode + * + * @XZ_SINGLE: Single-call mode. This uses less RAM than + * multi-call modes, because the LZMA2 + * dictionary doesn't need to be allocated as + * part of the decoder state. All required data + * structures are allocated at initialization, + * so xz_dec_run() cannot return XZ_MEM_ERROR. + * @XZ_PREALLOC: Multi-call mode with preallocated LZMA2 + * dictionary buffer. All data structures are + * allocated at initialization, so xz_dec_run() + * cannot return XZ_MEM_ERROR. + * @XZ_DYNALLOC: Multi-call mode. The LZMA2 dictionary is + * allocated once the required size has been + * parsed from the stream headers. If the + * allocation fails, xz_dec_run() will return + * XZ_MEM_ERROR. + * + * It is possible to enable support only for a subset of the above + * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC, + * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled + * with support for all operation modes, but the preboot code may + * be built with fewer features to minimize code size. + */ +enum xz_mode { + XZ_SINGLE, + XZ_PREALLOC, + XZ_DYNALLOC +}; + +/** + * enum xz_ret - Return codes + * @XZ_OK: Everything is OK so far. More input or more + * output space is required to continue. This + * return code is possible only in multi-call mode + * (XZ_PREALLOC or XZ_DYNALLOC). + * @XZ_STREAM_END: Operation finished successfully. + * @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding + * is still possible in multi-call mode by simply + * calling xz_dec_run() again. + * Note that this return value is used only if + * XZ_DEC_ANY_CHECK was defined at build time, + * which is not used in the kernel. Unsupported + * check types return XZ_OPTIONS_ERROR if + * XZ_DEC_ANY_CHECK was not defined at build time. + * @XZ_MEM_ERROR: Allocating memory failed. This return code is + * possible only if the decoder was initialized + * with XZ_DYNALLOC. The amount of memory that was + * tried to be allocated was no more than the + * dict_max argument given to xz_dec_init(). + * @XZ_MEMLIMIT_ERROR: A bigger LZMA2 dictionary would be needed than + * allowed by the dict_max argument given to + * xz_dec_init(). This return value is possible + * only in multi-call mode (XZ_PREALLOC or + * XZ_DYNALLOC); the single-call mode (XZ_SINGLE) + * ignores the dict_max argument. + * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic + * bytes). + * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested + * compression options. In the decoder this means + * that the header CRC32 matches, but the header + * itself specifies something that we don't support. + * @XZ_DATA_ERROR: Compressed data is corrupt. + * @XZ_BUF_ERROR: Cannot make any progress. Details are slightly + * different between multi-call and single-call + * mode; more information below. + * + * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls + * to XZ code cannot consume any input and cannot produce any new output. + * This happens when there is no new input available, or the output buffer + * is full while at least one output byte is still pending. Assuming your + * code is not buggy, you can get this error only when decoding a compressed + * stream that is truncated or otherwise corrupt. + * + * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer + * is too small or the compressed input is corrupt in a way that makes the + * decoder produce more output than the caller expected. When it is + * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR + * is used instead of XZ_BUF_ERROR. + */ +enum xz_ret { + XZ_OK, + XZ_STREAM_END, + XZ_UNSUPPORTED_CHECK, + XZ_MEM_ERROR, + XZ_MEMLIMIT_ERROR, + XZ_FORMAT_ERROR, + XZ_OPTIONS_ERROR, + XZ_DATA_ERROR, + XZ_BUF_ERROR +}; + +/** + * struct xz_buf - Passing input and output buffers to XZ code + * @in: Beginning of the input buffer. This may be NULL if and only + * if in_pos is equal to in_size. + * @in_pos: Current position in the input buffer. This must not exceed + * in_size. + * @in_size: Size of the input buffer + * @out: Beginning of the output buffer. This may be NULL if and only + * if out_pos is equal to out_size. + * @out_pos: Current position in the output buffer. This must not exceed + * out_size. + * @out_size: Size of the output buffer + * + * Only the contents of the output buffer from out[out_pos] onward, and + * the variables in_pos and out_pos are modified by the XZ code. + */ +struct xz_buf { + const uint8_t *in; + size_t in_pos; + size_t in_size; + + uint8_t *out; + size_t out_pos; + size_t out_size; +}; + +/* + * struct xz_dec - Opaque type to hold the XZ decoder state + */ +struct xz_dec; + +/** + * xz_dec_init() - Allocate and initialize a XZ decoder state + * @mode: Operation mode + * @dict_max: Maximum size of the LZMA2 dictionary (history buffer) for + * multi-call decoding. This is ignored in single-call mode + * (mode == XZ_SINGLE). LZMA2 dictionary is always 2^n bytes + * or 2^n + 2^(n-1) bytes (the latter sizes are less common + * in practice), so other values for dict_max don't make sense. + * In the kernel, dictionary sizes of 64 KiB, 128 KiB, 256 KiB, + * 512 KiB, and 1 MiB are probably the only reasonable values, + * except for kernel and initramfs images where a bigger + * dictionary can be fine and useful. + * + * Single-call mode (XZ_SINGLE): xz_dec_run() decodes the whole stream at + * once. The caller must provide enough output space or the decoding will + * fail. The output space is used as the dictionary buffer, which is why + * there is no need to allocate the dictionary as part of the decoder's + * internal state. + * + * Because the output buffer is used as the workspace, streams encoded using + * a big dictionary are not a problem in single-call mode. It is enough that + * the output buffer is big enough to hold the actual uncompressed data; it + * can be smaller than the dictionary size stored in the stream headers. + * + * Multi-call mode with preallocated dictionary (XZ_PREALLOC): dict_max bytes + * of memory is preallocated for the LZMA2 dictionary. This way there is no + * risk that xz_dec_run() could run out of memory, since xz_dec_run() will + * never allocate any memory. Instead, if the preallocated dictionary is too + * small for decoding the given input stream, xz_dec_run() will return + * XZ_MEMLIMIT_ERROR. Thus, it is important to know what kind of data will be + * decoded to avoid allocating excessive amount of memory for the dictionary. + * + * Multi-call mode with dynamically allocated dictionary (XZ_DYNALLOC): + * dict_max specifies the maximum allowed dictionary size that xz_dec_run() + * may allocate once it has parsed the dictionary size from the stream + * headers. This way excessive allocations can be avoided while still + * limiting the maximum memory usage to a sane value to prevent running the + * system out of memory when decompressing streams from untrusted sources. + * + * On success, xz_dec_init() returns a pointer to struct xz_dec, which is + * ready to be used with xz_dec_run(). If memory allocation fails, + * xz_dec_init() returns NULL. + */ +XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max); + +/** + * xz_dec_run() - Run the XZ decoder for a single XZ stream + * @s: Decoder state allocated using xz_dec_init() + * @b: Input and output buffers + * + * The possible return values depend on build options and operation mode. + * See enum xz_ret for details. + * + * Note that if an error occurs in single-call mode (return value is not + * XZ_STREAM_END), b->in_pos and b->out_pos are not modified and the + * contents of the output buffer from b->out[b->out_pos] onward are + * undefined. This is true even after XZ_BUF_ERROR, because with some filter + * chains, there may be a second pass over the output buffer, and this pass + * cannot be properly done if the output buffer is truncated. Thus, you + * cannot give the single-call decoder a too small buffer and then expect to + * get that amount valid data from the beginning of the stream. You must use + * the multi-call decoder if you don't want to uncompress the whole stream. + * + * Use xz_dec_run() when XZ data is stored inside some other file format. + * The decoding will stop after one XZ stream has been decompressed. To + * decompress regular .xz files which might have multiple concatenated + * streams, use xz_dec_catrun() instead. + */ +XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b); + +/** + * xz_dec_catrun() - Run the XZ decoder with support for concatenated streams + * @s: Decoder state allocated using xz_dec_init() + * @b: Input and output buffers + * @finish: This is an int instead of bool to avoid requiring stdbool.h. + * As long as more input might be coming, finish must be false. + * When the caller knows that it has provided all the input to + * the decoder (some possibly still in b->in), it must set finish + * to true. Only when finish is true can this function return + * XZ_STREAM_END to indicate successful decompression of the + * file. In single-call mode (XZ_SINGLE) finish is assumed to + * always be true; the caller-provided value is ignored. + * + * This is like xz_dec_run() except that this makes it easy to decode .xz + * files with multiple streams (multiple .xz files concatenated as is). + * The rarely-used Stream Padding feature is supported too, that is, there + * can be null bytes after or between the streams. The number of null bytes + * must be a multiple of four. + * + * When finish is false and b->in_pos == b->in_size, it is possible that + * XZ_BUF_ERROR isn't returned even when no progress is possible (XZ_OK is + * returned instead). This shouldn't matter because in this situation a + * reasonable caller will attempt to provide more input or set finish to + * true for the next xz_dec_catrun() call anyway. + * + * For any struct xz_dec that has been initialized for multi-call mode: + * Once decoding has been started with xz_dec_run() or xz_dec_catrun(), + * the same function must be used until xz_dec_reset() or xz_dec_end(). + * Switching between the two decoding functions without resetting results + * in undefined behavior. + * + * xz_dec_catrun() is only available if XZ_DEC_CONCATENATED was defined + * at compile time. + */ +XZ_EXTERN enum xz_ret xz_dec_catrun(struct xz_dec *s, struct xz_buf *b, + int finish); + +/** + * xz_dec_reset() - Reset an already allocated decoder state + * @s: Decoder state allocated using xz_dec_init() + * + * This function can be used to reset the multi-call decoder state without + * freeing and reallocating memory with xz_dec_end() and xz_dec_init(). + * + * In single-call mode, xz_dec_reset() is always called in the beginning of + * xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in + * multi-call mode. + */ +XZ_EXTERN void xz_dec_reset(struct xz_dec *s); + +/** + * xz_dec_end() - Free the memory allocated for the decoder state + * @s: Decoder state allocated using xz_dec_init(). If s is NULL, + * this function does nothing. + */ +XZ_EXTERN void xz_dec_end(struct xz_dec *s); + +/** + * DOC: MicroLZMA decompressor + * + * This MicroLZMA header format was created for use in EROFS but may be used + * by others too. **In most cases one needs the XZ APIs above instead.** + * + * The compressed format supported by this decoder is a raw LZMA stream + * whose first byte (always 0x00) has been replaced with bitwise-negation + * of the LZMA properties (lc/lp/pb) byte. For example, if lc/lp/pb is + * 3/0/2, the first byte is 0xA2. This way the first byte can never be 0x00. + * Just like with LZMA2, lc + lp <= 4 must be true. The LZMA end-of-stream + * marker must not be used. The unused values are reserved for future use. + */ + +/* + * struct xz_dec_microlzma - Opaque type to hold the MicroLZMA decoder state + */ +struct xz_dec_microlzma; + +/** + * xz_dec_microlzma_alloc() - Allocate memory for the MicroLZMA decoder + * @mode: XZ_SINGLE or XZ_PREALLOC + * @dict_size: LZMA dictionary size. This must be at least 4 KiB and + * at most 3 GiB. + * + * In contrast to xz_dec_init(), this function only allocates the memory + * and remembers the dictionary size. xz_dec_microlzma_reset() must be used + * before calling xz_dec_microlzma_run(). + * + * The amount of allocated memory is a little less than 30 KiB with XZ_SINGLE. + * With XZ_PREALLOC also a dictionary buffer of dict_size bytes is allocated. + * + * On success, xz_dec_microlzma_alloc() returns a pointer to + * struct xz_dec_microlzma. If memory allocation fails or + * dict_size is invalid, NULL is returned. + */ +XZ_EXTERN struct xz_dec_microlzma *xz_dec_microlzma_alloc(enum xz_mode mode, + uint32_t dict_size); + +/** + * xz_dec_microlzma_reset() - Reset the MicroLZMA decoder state + * @s: Decoder state allocated using xz_dec_microlzma_alloc() + * @comp_size: Compressed size of the input stream + * @uncomp_size: Uncompressed size of the input stream. A value smaller + * than the real uncompressed size of the input stream can + * be specified if uncomp_size_is_exact is set to false. + * uncomp_size can never be set to a value larger than the + * expected real uncompressed size because it would eventually + * result in XZ_DATA_ERROR. + * @uncomp_size_is_exact: This is an int instead of bool to avoid + * requiring stdbool.h. This should normally be set to true. + * When this is set to false, error detection is weaker. + */ +XZ_EXTERN void xz_dec_microlzma_reset(struct xz_dec_microlzma *s, + uint32_t comp_size, uint32_t uncomp_size, + int uncomp_size_is_exact); + +/** + * xz_dec_microlzma_run() - Run the MicroLZMA decoder + * @s: Decoder state initialized using xz_dec_microlzma_reset() + * @b: Input and output buffers + * + * This works similarly to xz_dec_run() with a few important differences. + * Only the differences are documented here. + * + * The only possible return values are XZ_OK, XZ_STREAM_END, and + * XZ_DATA_ERROR. This function cannot return XZ_BUF_ERROR: if no progress + * is possible due to lack of input data or output space, this function will + * keep returning XZ_OK. Thus, the calling code must be written so that it + * will eventually provide input and output space matching (or exceeding) + * comp_size and uncomp_size arguments given to xz_dec_microlzma_reset(). + * If the caller cannot do this (for example, if the input file is truncated + * or otherwise corrupt), the caller must detect this error by itself to + * avoid an infinite loop. + * + * If the compressed data seems to be corrupt, XZ_DATA_ERROR is returned. + * This can happen also when incorrect dictionary, uncompressed, or + * compressed sizes have been specified. + * + * With XZ_PREALLOC only: As an extra feature, b->out may be NULL to skip over + * uncompressed data. This way the caller doesn't need to provide a temporary + * output buffer for the bytes that will be ignored. + * + * With XZ_SINGLE only: In contrast to xz_dec_run(), the return value XZ_OK + * is also possible and thus XZ_SINGLE is actually a limited multi-call mode. + * After XZ_OK the bytes decoded so far may be read from the output buffer. + * It is possible to continue decoding but the variables b->out and b->out_pos + * MUST NOT be changed by the caller. Increasing the value of b->out_size is + * allowed to make more output space available; one doesn't need to provide + * space for the whole uncompressed data on the first call. The input buffer + * may be changed normally like with XZ_PREALLOC. This way input data can be + * provided from non-contiguous memory. + */ +XZ_EXTERN enum xz_ret xz_dec_microlzma_run(struct xz_dec_microlzma *s, + struct xz_buf *b); + +/** + * xz_dec_microlzma_end() - Free the memory allocated for the decoder state + * @s: Decoder state allocated using xz_dec_microlzma_alloc(). + * If s is NULL, this function does nothing. + */ +XZ_EXTERN void xz_dec_microlzma_end(struct xz_dec_microlzma *s); + +/* + * Standalone build (userspace build or in-kernel build for boot time use) + * needs a CRC32 implementation. For normal in-kernel use, kernel's own + * CRC32 module is used instead, and users of this module don't need to + * care about the functions below. + */ +#ifndef XZ_INTERNAL_CRC32 +# ifdef __KERNEL__ +# define XZ_INTERNAL_CRC32 0 +# else +# define XZ_INTERNAL_CRC32 1 +# endif +#endif + +/* + * If CRC64 support has been enabled with XZ_USE_CRC64, a CRC64 + * implementation is needed too. + */ +#ifndef XZ_USE_CRC64 +# undef XZ_INTERNAL_CRC64 +# define XZ_INTERNAL_CRC64 0 +#endif +#ifndef XZ_INTERNAL_CRC64 +# ifdef __KERNEL__ +# error Using CRC64 in the kernel has not been implemented. +# else +# define XZ_INTERNAL_CRC64 1 +# endif +#endif + +#if XZ_INTERNAL_CRC32 +/* + * This must be called before any other xz_* function to initialize + * the CRC32 lookup table. + */ +XZ_EXTERN void xz_crc32_init(void); + +/* + * Update CRC32 value using the polynomial from IEEE-802.3. To start a new + * calculation, the third argument must be zero. To continue the calculation, + * the previously returned value is passed as the third argument. + */ +XZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc); +#endif + +#if XZ_INTERNAL_CRC64 +/* + * This must be called before any other xz_* function (except xz_crc32_init()) + * to initialize the CRC64 lookup table. + */ +XZ_EXTERN void xz_crc64_init(void); + +/* + * Update CRC64 value using the polynomial from ECMA-182. To start a new + * calculation, the third argument must be zero. To continue the calculation, + * the previously returned value is passed as the third argument. + */ +XZ_EXTERN uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc); +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/android/app/src/main/cpp/xz/xz_config.h b/android/app/src/main/cpp/xz/xz_config.h new file mode 100644 index 000000000..d7d4031da --- /dev/null +++ b/android/app/src/main/cpp/xz/xz_config.h @@ -0,0 +1,138 @@ +/* SPDX-License-Identifier: 0BSD */ + +/* + * Private includes and definitions for userspace use of XZ Embedded + * + * Author: Lasse Collin + */ + +#ifndef XZ_CONFIG_H +#define XZ_CONFIG_H + +/* Uncomment to enable building of xz_dec_catrun(). */ +/* #define XZ_DEC_CONCATENATED */ + +/* Uncomment to enable CRC64 support. */ +/* #define XZ_USE_CRC64 */ + +/* Uncomment as needed to enable BCJ filter decoders. */ +/* #define XZ_DEC_X86 */ +/* #define XZ_DEC_ARM */ +/* #define XZ_DEC_ARMTHUMB */ +/* #define XZ_DEC_ARM64 */ +/* #define XZ_DEC_RISCV */ +/* #define XZ_DEC_POWERPC */ +/* #define XZ_DEC_IA64 */ +/* #define XZ_DEC_SPARC */ + +/* + * Visual Studio 2013 update 2 supports only __inline, not inline. + * MSVC v19.0 / VS 2015 and newer support both. + */ +#if defined(_MSC_VER) && _MSC_VER < 1900 && !defined(inline) +# define inline __inline +#endif + +#include +#include +#include + +#include "xz.h" + +#define kmalloc(size, flags) malloc(size) +#define kfree(ptr) free(ptr) +#define vmalloc(size) malloc(size) +#define vfree(ptr) free(ptr) + +#define memeq(a, b, size) (memcmp(a, b, size) == 0) +#define memzero(buf, size) memset(buf, 0, size) + +#ifndef min +# define min(x, y) ((x) < (y) ? (x) : (y)) +#endif +#define min_t(type, x, y) min(x, y) + +#ifndef fallthrough +# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311 +# define fallthrough [[fallthrough]] +# elif (defined(__GNUC__) && __GNUC__ >= 7) \ + || (defined(__clang_major__) && __clang_major__ >= 10) +# define fallthrough __attribute__((__fallthrough__)) +# else +# define fallthrough do {} while (0) +# endif +#endif + +/* + * Some functions have been marked with __always_inline to keep the + * performance reasonable even when the compiler is optimizing for + * small code size. You may be able to save a few bytes by #defining + * __always_inline to plain inline, but don't complain if the code + * becomes slow. + * + * NOTE: System headers on GNU/Linux may #define this macro already, + * so if you want to change it, you need to #undef it first. + */ +#ifndef __always_inline +# ifdef __GNUC__ +# define __always_inline \ + inline __attribute__((__always_inline__)) +# else +# define __always_inline inline +# endif +#endif + +/* Inline functions to access unaligned unsigned 32-bit integers */ +#ifndef get_unaligned_le32 +static inline uint32_t get_unaligned_le32(const uint8_t *buf) +{ + return (uint32_t)buf[0] + | ((uint32_t)buf[1] << 8) + | ((uint32_t)buf[2] << 16) + | ((uint32_t)buf[3] << 24); +} +#endif + +#ifndef get_unaligned_be32 +static inline uint32_t get_unaligned_be32(const uint8_t *buf) +{ + return (uint32_t)((uint32_t)buf[0] << 24) + | ((uint32_t)buf[1] << 16) + | ((uint32_t)buf[2] << 8) + | (uint32_t)buf[3]; +} +#endif + +#ifndef put_unaligned_le32 +static inline void put_unaligned_le32(uint32_t val, uint8_t *buf) +{ + buf[0] = (uint8_t)val; + buf[1] = (uint8_t)(val >> 8); + buf[2] = (uint8_t)(val >> 16); + buf[3] = (uint8_t)(val >> 24); +} +#endif + +#ifndef put_unaligned_be32 +static inline void put_unaligned_be32(uint32_t val, uint8_t *buf) +{ + buf[0] = (uint8_t)(val >> 24); + buf[1] = (uint8_t)(val >> 16); + buf[2] = (uint8_t)(val >> 8); + buf[3] = (uint8_t)val; +} +#endif + +/* + * To keep things simpler, use the generic unaligned methods also for + * aligned access. The only place where performance could matter is + * SHA-256 but files using SHA-256 aren't common. + */ +#ifndef get_le32 +# define get_le32 get_unaligned_le32 +#endif +#ifndef get_be32 +# define get_be32 get_unaligned_be32 +#endif + +#endif diff --git a/android/app/src/main/cpp/xz/xz_crc32.c b/android/app/src/main/cpp/xz/xz_crc32.c new file mode 100644 index 000000000..effdf34ec --- /dev/null +++ b/android/app/src/main/cpp/xz/xz_crc32.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: 0BSD + +/* + * CRC32 using the polynomial from IEEE-802.3 + * + * Authors: Lasse Collin + * Igor Pavlov + */ + +/* + * This is not the fastest implementation, but it is pretty compact. + * The fastest versions of xz_crc32() on modern CPUs without hardware + * accelerated CRC instruction are 3-5 times as fast as this version, + * but they are bigger and use more memory for the lookup table. + */ + +#include "xz_private.h" + +/* + * STATIC_RW_DATA is used in the pre-boot environment on some architectures. + * See for details. + */ +#ifndef STATIC_RW_DATA +# define STATIC_RW_DATA static +#endif + +STATIC_RW_DATA uint32_t xz_crc32_table[256]; + +XZ_EXTERN void xz_crc32_init(void) +{ + const uint32_t poly = 0xEDB88320; + + uint32_t i; + uint32_t j; + uint32_t r; + + for (i = 0; i < 256; ++i) { + r = i; + for (j = 0; j < 8; ++j) + r = (r >> 1) ^ (poly & ~((r & 1) - 1)); + + xz_crc32_table[i] = r; + } + + return; +} + +XZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc) +{ + crc = ~crc; + + while (size != 0) { + crc = xz_crc32_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8); + --size; + } + + return ~crc; +} diff --git a/android/app/src/main/cpp/xz/xz_crc64.c b/android/app/src/main/cpp/xz/xz_crc64.c new file mode 100644 index 000000000..20049ea55 --- /dev/null +++ b/android/app/src/main/cpp/xz/xz_crc64.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: 0BSD + +/* + * CRC64 using the polynomial from ECMA-182 + * + * This file is similar to xz_crc32.c. See the comments there. + * + * Authors: Lasse Collin + * Igor Pavlov + */ + +#include "xz_private.h" + +#ifndef STATIC_RW_DATA +# define STATIC_RW_DATA static +#endif + +STATIC_RW_DATA uint64_t xz_crc64_table[256]; + +XZ_EXTERN void xz_crc64_init(void) +{ + /* + * The ULL suffix is needed for -std=gnu89 compatibility + * on 32-bit platforms. + */ + const uint64_t poly = 0xC96C5795D7870F42ULL; + + uint32_t i; + uint32_t j; + uint64_t r; + + for (i = 0; i < 256; ++i) { + r = i; + for (j = 0; j < 8; ++j) + r = (r >> 1) ^ (poly & ~((r & 1) - 1)); + + xz_crc64_table[i] = r; + } + + return; +} + +XZ_EXTERN uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc) +{ + crc = ~crc; + + while (size != 0) { + crc = xz_crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8); + --size; + } + + return ~crc; +} diff --git a/android/app/src/main/cpp/xz/xz_dec_bcj.c b/android/app/src/main/cpp/xz/xz_dec_bcj.c new file mode 100644 index 000000000..42d7f2687 --- /dev/null +++ b/android/app/src/main/cpp/xz/xz_dec_bcj.c @@ -0,0 +1,738 @@ +// SPDX-License-Identifier: 0BSD + +/* + * Branch/Call/Jump (BCJ) filter decoders + * + * Authors: Lasse Collin + * Igor Pavlov + */ + +#include "xz_private.h" + +/* + * The rest of the file is inside this ifdef. It makes things a little more + * convenient when building without support for any BCJ filters. + */ +#ifdef XZ_DEC_BCJ + +struct xz_dec_bcj { + /* Type of the BCJ filter being used */ + enum { + BCJ_X86 = 4, /* x86 or x86-64 */ + BCJ_POWERPC = 5, /* Big endian only */ + BCJ_IA64 = 6, /* Big or little endian */ + BCJ_ARM = 7, /* Little endian only */ + BCJ_ARMTHUMB = 8, /* Little endian only */ + BCJ_SPARC = 9, /* Big or little endian */ + BCJ_ARM64 = 10, /* AArch64 */ + BCJ_RISCV = 11 /* RV32GQC_Zfh, RV64GQC_Zfh */ + } type; + + /* + * Return value of the next filter in the chain. We need to preserve + * this information across calls, because we must not call the next + * filter anymore once it has returned XZ_STREAM_END. + */ + enum xz_ret ret; + + /* True if we are operating in single-call mode. */ + bool single_call; + + /* + * Absolute position relative to the beginning of the uncompressed + * data (in a single .xz Block). We care only about the lowest 32 + * bits so this doesn't need to be uint64_t even with big files. + */ + uint32_t pos; + + /* x86 filter state */ + uint32_t x86_prev_mask; + + /* Temporary space to hold the variables from struct xz_buf */ + uint8_t *out; + size_t out_pos; + size_t out_size; + + struct { + /* Amount of already filtered data in the beginning of buf */ + size_t filtered; + + /* Total amount of data currently stored in buf */ + size_t size; + + /* + * Buffer to hold a mix of filtered and unfiltered data. This + * needs to be big enough to hold Alignment + 2 * Look-ahead: + * + * Type Alignment Look-ahead + * x86 1 4 + * PowerPC 4 0 + * IA-64 16 0 + * ARM 4 0 + * ARM-Thumb 2 2 + * SPARC 4 0 + */ + uint8_t buf[16]; + } temp; +}; + +#ifdef XZ_DEC_X86 +/* + * This is used to test the most significant byte of a memory address + * in an x86 instruction. + */ +static inline int bcj_x86_test_msbyte(uint8_t b) +{ + return b == 0x00 || b == 0xFF; +} + +static size_t bcj_x86(struct xz_dec_bcj *s, uint8_t *buf, size_t size) +{ + static const bool mask_to_allowed_status[8] + = { true, true, true, false, true, false, false, false }; + + static const uint8_t mask_to_bit_num[8] = { 0, 1, 2, 2, 3, 3, 3, 3 }; + + size_t i; + size_t prev_pos = (size_t)-1; + uint32_t prev_mask = s->x86_prev_mask; + uint32_t src; + uint32_t dest; + uint32_t j; + uint8_t b; + + if (size <= 4) + return 0; + + size -= 4; + for (i = 0; i < size; ++i) { + if ((buf[i] & 0xFE) != 0xE8) + continue; + + prev_pos = i - prev_pos; + if (prev_pos > 3) { + prev_mask = 0; + } else { + prev_mask = (prev_mask << (prev_pos - 1)) & 7; + if (prev_mask != 0) { + b = buf[i + 4 - mask_to_bit_num[prev_mask]]; + if (!mask_to_allowed_status[prev_mask] + || bcj_x86_test_msbyte(b)) { + prev_pos = i; + prev_mask = (prev_mask << 1) | 1; + continue; + } + } + } + + prev_pos = i; + + if (bcj_x86_test_msbyte(buf[i + 4])) { + src = get_unaligned_le32(buf + i + 1); + while (true) { + dest = src - (s->pos + (uint32_t)i + 5); + if (prev_mask == 0) + break; + + j = mask_to_bit_num[prev_mask] * 8; + b = (uint8_t)(dest >> (24 - j)); + if (!bcj_x86_test_msbyte(b)) + break; + + src = dest ^ (((uint32_t)1 << (32 - j)) - 1); + } + + dest &= 0x01FFFFFF; + dest |= (uint32_t)0 - (dest & 0x01000000); + put_unaligned_le32(dest, buf + i + 1); + i += 4; + } else { + prev_mask = (prev_mask << 1) | 1; + } + } + + prev_pos = i - prev_pos; + s->x86_prev_mask = prev_pos > 3 ? 0 : prev_mask << (prev_pos - 1); + return i; +} +#endif + +#ifdef XZ_DEC_POWERPC +static size_t bcj_powerpc(struct xz_dec_bcj *s, uint8_t *buf, size_t size) +{ + size_t i; + uint32_t instr; + + size &= ~(size_t)3; + + for (i = 0; i < size; i += 4) { + instr = get_unaligned_be32(buf + i); + if ((instr & 0xFC000003) == 0x48000001) { + instr &= 0x03FFFFFC; + instr -= s->pos + (uint32_t)i; + instr &= 0x03FFFFFC; + instr |= 0x48000001; + put_unaligned_be32(instr, buf + i); + } + } + + return i; +} +#endif + +#ifdef XZ_DEC_IA64 +static size_t bcj_ia64(struct xz_dec_bcj *s, uint8_t *buf, size_t size) +{ + static const uint8_t branch_table[32] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 4, 4, 6, 6, 0, 0, 7, 7, + 4, 4, 0, 0, 4, 4, 0, 0 + }; + + /* + * The local variables take a little bit stack space, but it's less + * than what LZMA2 decoder takes, so it doesn't make sense to reduce + * stack usage here without doing that for the LZMA2 decoder too. + */ + + /* Loop counters */ + size_t i; + size_t j; + + /* Instruction slot (0, 1, or 2) in the 128-bit instruction word */ + uint32_t slot; + + /* Bitwise offset of the instruction indicated by slot */ + uint32_t bit_pos; + + /* bit_pos split into byte and bit parts */ + uint32_t byte_pos; + uint32_t bit_res; + + /* Address part of an instruction */ + uint32_t addr; + + /* Mask used to detect which instructions to convert */ + uint32_t mask; + + /* 41-bit instruction stored somewhere in the lowest 48 bits */ + uint64_t instr; + + /* Instruction normalized with bit_res for easier manipulation */ + uint64_t norm; + + size &= ~(size_t)15; + + for (i = 0; i < size; i += 16) { + mask = branch_table[buf[i] & 0x1F]; + for (slot = 0, bit_pos = 5; slot < 3; ++slot, bit_pos += 41) { + if (((mask >> slot) & 1) == 0) + continue; + + byte_pos = bit_pos >> 3; + bit_res = bit_pos & 7; + instr = 0; + for (j = 0; j < 6; ++j) + instr |= (uint64_t)(buf[i + j + byte_pos]) + << (8 * j); + + norm = instr >> bit_res; + + if (((norm >> 37) & 0x0F) == 0x05 + && ((norm >> 9) & 0x07) == 0) { + addr = (norm >> 13) & 0x0FFFFF; + addr |= ((uint32_t)(norm >> 36) & 1) << 20; + addr <<= 4; + addr -= s->pos + (uint32_t)i; + addr >>= 4; + + norm &= ~((uint64_t)0x8FFFFF << 13); + norm |= (uint64_t)(addr & 0x0FFFFF) << 13; + norm |= (uint64_t)(addr & 0x100000) + << (36 - 20); + + instr &= (1 << bit_res) - 1; + instr |= norm << bit_res; + + for (j = 0; j < 6; j++) + buf[i + j + byte_pos] + = (uint8_t)(instr >> (8 * j)); + } + } + } + + return i; +} +#endif + +#ifdef XZ_DEC_ARM +static size_t bcj_arm(struct xz_dec_bcj *s, uint8_t *buf, size_t size) +{ + size_t i; + uint32_t addr; + + size &= ~(size_t)3; + + for (i = 0; i < size; i += 4) { + if (buf[i + 3] == 0xEB) { + addr = (uint32_t)buf[i] | ((uint32_t)buf[i + 1] << 8) + | ((uint32_t)buf[i + 2] << 16); + addr <<= 2; + addr -= s->pos + (uint32_t)i + 8; + addr >>= 2; + buf[i] = (uint8_t)addr; + buf[i + 1] = (uint8_t)(addr >> 8); + buf[i + 2] = (uint8_t)(addr >> 16); + } + } + + return i; +} +#endif + +#ifdef XZ_DEC_ARMTHUMB +static size_t bcj_armthumb(struct xz_dec_bcj *s, uint8_t *buf, size_t size) +{ + size_t i; + uint32_t addr; + + if (size < 4) + return 0; + + size -= 4; + + for (i = 0; i <= size; i += 2) { + if ((buf[i + 1] & 0xF8) == 0xF0 + && (buf[i + 3] & 0xF8) == 0xF8) { + addr = (((uint32_t)buf[i + 1] & 0x07) << 19) + | ((uint32_t)buf[i] << 11) + | (((uint32_t)buf[i + 3] & 0x07) << 8) + | (uint32_t)buf[i + 2]; + addr <<= 1; + addr -= s->pos + (uint32_t)i + 4; + addr >>= 1; + buf[i + 1] = (uint8_t)(0xF0 | ((addr >> 19) & 0x07)); + buf[i] = (uint8_t)(addr >> 11); + buf[i + 3] = (uint8_t)(0xF8 | ((addr >> 8) & 0x07)); + buf[i + 2] = (uint8_t)addr; + i += 2; + } + } + + return i; +} +#endif + +#ifdef XZ_DEC_SPARC +static size_t bcj_sparc(struct xz_dec_bcj *s, uint8_t *buf, size_t size) +{ + size_t i; + uint32_t instr; + + size &= ~(size_t)3; + + for (i = 0; i < size; i += 4) { + instr = get_unaligned_be32(buf + i); + if ((instr >> 22) == 0x100 || (instr >> 22) == 0x1FF) { + instr <<= 2; + instr -= s->pos + (uint32_t)i; + instr >>= 2; + instr = ((uint32_t)0x40000000 - (instr & 0x400000)) + | 0x40000000 | (instr & 0x3FFFFF); + put_unaligned_be32(instr, buf + i); + } + } + + return i; +} +#endif + +#ifdef XZ_DEC_ARM64 +static size_t bcj_arm64(struct xz_dec_bcj *s, uint8_t *buf, size_t size) +{ + size_t i; + uint32_t instr; + uint32_t addr; + + size &= ~(size_t)3; + + for (i = 0; i < size; i += 4) { + instr = get_unaligned_le32(buf + i); + + if ((instr >> 26) == 0x25) { + /* BL instruction */ + addr = instr - ((s->pos + (uint32_t)i) >> 2); + instr = 0x94000000 | (addr & 0x03FFFFFF); + put_unaligned_le32(instr, buf + i); + + } else if ((instr & 0x9F000000) == 0x90000000) { + /* ADRP instruction */ + addr = ((instr >> 29) & 3) | ((instr >> 3) & 0x1FFFFC); + + /* Only convert values in the range +/-512 MiB. */ + if ((addr + 0x020000) & 0x1C0000) + continue; + + addr -= (s->pos + (uint32_t)i) >> 12; + + instr &= 0x9000001F; + instr |= (addr & 3) << 29; + instr |= (addr & 0x03FFFC) << 3; + instr |= (0U - (addr & 0x020000)) & 0xE00000; + + put_unaligned_le32(instr, buf + i); + } + } + + return i; +} +#endif + +#ifdef XZ_DEC_RISCV +static size_t bcj_riscv(struct xz_dec_bcj *s, uint8_t *buf, size_t size) +{ + size_t i; + uint32_t b1; + uint32_t b2; + uint32_t b3; + uint32_t instr; + uint32_t instr2; + uint32_t instr2_rs1; + uint32_t addr; + + if (size < 8) + return 0; + + size -= 8; + + for (i = 0; i <= size; i += 2) { + instr = buf[i]; + + if (instr == 0xEF) { + /* JAL */ + b1 = buf[i + 1]; + if ((b1 & 0x0D) != 0) + continue; + + b2 = buf[i + 2]; + b3 = buf[i + 3]; + + addr = ((b1 & 0xF0) << 13) | (b2 << 9) | (b3 << 1); + addr -= s->pos + (uint32_t)i; + + buf[i + 1] = (uint8_t)((b1 & 0x0F) + | ((addr >> 8) & 0xF0)); + + buf[i + 2] = (uint8_t)(((addr >> 16) & 0x0F) + | ((addr >> 7) & 0x10) + | ((addr << 4) & 0xE0)); + + buf[i + 3] = (uint8_t)(((addr >> 4) & 0x7F) + | ((addr >> 13) & 0x80)); + + i += 4 - 2; + + } else if ((instr & 0x7F) == 0x17) { + /* AUIPC */ + instr |= (uint32_t)buf[i + 1] << 8; + instr |= (uint32_t)buf[i + 2] << 16; + instr |= (uint32_t)buf[i + 3] << 24; + + if (instr & 0xE80) { + /* AUIPC's rd doesn't equal x0 or x2. */ + instr2 = get_unaligned_le32(buf + i + 4); + + if (((instr << 8) ^ (instr2 - 3)) & 0xF8003) { + i += 6 - 2; + continue; + } + + addr = (instr & 0xFFFFF000) + (instr2 >> 20); + + instr = 0x17 | (2 << 7) | (instr2 << 12); + instr2 = addr; + } else { + /* AUIPC's rd equals x0 or x2. */ + instr2_rs1 = instr >> 27; + + if ((uint32_t)((instr - 0x3117) << 18) + >= (instr2_rs1 & 0x1D)) { + i += 4 - 2; + continue; + } + + addr = get_unaligned_be32(buf + i + 4); + addr -= s->pos + (uint32_t)i; + + instr2 = (instr >> 12) | (addr << 20); + + instr = 0x17 | (instr2_rs1 << 7) + | ((addr + 0x800) & 0xFFFFF000); + } + + put_unaligned_le32(instr, buf + i); + put_unaligned_le32(instr2, buf + i + 4); + + i += 8 - 2; + } + } + + return i; +} +#endif + +/* + * Apply the selected BCJ filter. Update *pos and s->pos to match the amount + * of data that got filtered. + * + * NOTE: This is implemented as a switch statement to avoid using function + * pointers, which could be problematic in the kernel boot code, which must + * avoid pointers to static data (at least on x86). + */ +static void bcj_apply(struct xz_dec_bcj *s, + uint8_t *buf, size_t *pos, size_t size) +{ + size_t filtered; + + buf += *pos; + size -= *pos; + + switch (s->type) { +#ifdef XZ_DEC_X86 + case BCJ_X86: + filtered = bcj_x86(s, buf, size); + break; +#endif +#ifdef XZ_DEC_POWERPC + case BCJ_POWERPC: + filtered = bcj_powerpc(s, buf, size); + break; +#endif +#ifdef XZ_DEC_IA64 + case BCJ_IA64: + filtered = bcj_ia64(s, buf, size); + break; +#endif +#ifdef XZ_DEC_ARM + case BCJ_ARM: + filtered = bcj_arm(s, buf, size); + break; +#endif +#ifdef XZ_DEC_ARMTHUMB + case BCJ_ARMTHUMB: + filtered = bcj_armthumb(s, buf, size); + break; +#endif +#ifdef XZ_DEC_SPARC + case BCJ_SPARC: + filtered = bcj_sparc(s, buf, size); + break; +#endif +#ifdef XZ_DEC_ARM64 + case BCJ_ARM64: + filtered = bcj_arm64(s, buf, size); + break; +#endif +#ifdef XZ_DEC_RISCV + case BCJ_RISCV: + filtered = bcj_riscv(s, buf, size); + break; +#endif + default: + /* Never reached but silence compiler warnings. */ + filtered = 0; + break; + } + + *pos += filtered; + s->pos += filtered; +} + +/* + * Flush pending filtered data from temp to the output buffer. + * Move the remaining mixture of possibly filtered and unfiltered + * data to the beginning of temp. + */ +static void bcj_flush(struct xz_dec_bcj *s, struct xz_buf *b) +{ + size_t copy_size; + + copy_size = min_t(size_t, s->temp.filtered, b->out_size - b->out_pos); + memcpy(b->out + b->out_pos, s->temp.buf, copy_size); + b->out_pos += copy_size; + + s->temp.filtered -= copy_size; + s->temp.size -= copy_size; + memmove(s->temp.buf, s->temp.buf + copy_size, s->temp.size); +} + +/* + * The BCJ filter functions are primitive in sense that they process the + * data in chunks of 1-16 bytes. To hide this issue, this function does + * some buffering. + */ +XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s, + struct xz_dec_lzma2 *lzma2, + struct xz_buf *b) +{ + size_t out_start; + + /* + * Flush pending already filtered data to the output buffer. Return + * immediately if we couldn't flush everything, or if the next + * filter in the chain had already returned XZ_STREAM_END. + */ + if (s->temp.filtered > 0) { + bcj_flush(s, b); + if (s->temp.filtered > 0) + return XZ_OK; + + if (s->ret == XZ_STREAM_END) + return XZ_STREAM_END; + } + + /* + * If we have more output space than what is currently pending in + * temp, copy the unfiltered data from temp to the output buffer + * and try to fill the output buffer by decoding more data from the + * next filter in the chain. Apply the BCJ filter on the new data + * in the output buffer. If everything cannot be filtered, copy it + * to temp and rewind the output buffer position accordingly. + * + * This needs to be always run when temp.size == 0 to handle a special + * case where the output buffer is full and the next filter has no + * more output coming but hasn't returned XZ_STREAM_END yet. + */ + if (s->temp.size < b->out_size - b->out_pos || s->temp.size == 0) { + out_start = b->out_pos; + memcpy(b->out + b->out_pos, s->temp.buf, s->temp.size); + b->out_pos += s->temp.size; + + s->ret = xz_dec_lzma2_run(lzma2, b); + if (s->ret != XZ_STREAM_END + && (s->ret != XZ_OK || s->single_call)) + return s->ret; + + bcj_apply(s, b->out, &out_start, b->out_pos); + + /* + * As an exception, if the next filter returned XZ_STREAM_END, + * we can do that too, since the last few bytes that remain + * unfiltered are meant to remain unfiltered. + */ + if (s->ret == XZ_STREAM_END) + return XZ_STREAM_END; + + s->temp.size = b->out_pos - out_start; + b->out_pos -= s->temp.size; + memcpy(s->temp.buf, b->out + b->out_pos, s->temp.size); + + /* + * If there wasn't enough input to the next filter to fill + * the output buffer with unfiltered data, there's no point + * to try decoding more data to temp. + */ + if (b->out_pos + s->temp.size < b->out_size) + return XZ_OK; + } + + /* + * We have unfiltered data in temp. If the output buffer isn't full + * yet, try to fill the temp buffer by decoding more data from the + * next filter. Apply the BCJ filter on temp. Then we hopefully can + * fill the actual output buffer by copying filtered data from temp. + * A mix of filtered and unfiltered data may be left in temp; it will + * be taken care on the next call to this function. + */ + if (b->out_pos < b->out_size) { + /* Make b->out{,_pos,_size} temporarily point to s->temp. */ + s->out = b->out; + s->out_pos = b->out_pos; + s->out_size = b->out_size; + b->out = s->temp.buf; + b->out_pos = s->temp.size; + b->out_size = sizeof(s->temp.buf); + + s->ret = xz_dec_lzma2_run(lzma2, b); + + s->temp.size = b->out_pos; + b->out = s->out; + b->out_pos = s->out_pos; + b->out_size = s->out_size; + + if (s->ret != XZ_OK && s->ret != XZ_STREAM_END) + return s->ret; + + bcj_apply(s, s->temp.buf, &s->temp.filtered, s->temp.size); + + /* + * If the next filter returned XZ_STREAM_END, we mark that + * everything is filtered, since the last unfiltered bytes + * of the stream are meant to be left as is. + */ + if (s->ret == XZ_STREAM_END) + s->temp.filtered = s->temp.size; + + bcj_flush(s, b); + if (s->temp.filtered > 0) + return XZ_OK; + } + + return s->ret; +} + +XZ_EXTERN struct xz_dec_bcj *xz_dec_bcj_create(bool single_call) +{ + struct xz_dec_bcj *s = kmalloc(sizeof(*s), GFP_KERNEL); + if (s != NULL) + s->single_call = single_call; + + return s; +} + +XZ_EXTERN enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id) +{ + switch (id) { +#ifdef XZ_DEC_X86 + case BCJ_X86: +#endif +#ifdef XZ_DEC_POWERPC + case BCJ_POWERPC: +#endif +#ifdef XZ_DEC_IA64 + case BCJ_IA64: +#endif +#ifdef XZ_DEC_ARM + case BCJ_ARM: +#endif +#ifdef XZ_DEC_ARMTHUMB + case BCJ_ARMTHUMB: +#endif +#ifdef XZ_DEC_SPARC + case BCJ_SPARC: +#endif +#ifdef XZ_DEC_ARM64 + case BCJ_ARM64: +#endif +#ifdef XZ_DEC_RISCV + case BCJ_RISCV: +#endif + break; + + default: + /* Unsupported Filter ID */ + return XZ_OPTIONS_ERROR; + } + + s->type = id; + s->ret = XZ_OK; + s->pos = 0; + s->x86_prev_mask = 0; + s->temp.filtered = 0; + s->temp.size = 0; + + return XZ_OK; +} + +#endif diff --git a/android/app/src/main/cpp/xz/xz_dec_lzma2.c b/android/app/src/main/cpp/xz/xz_dec_lzma2.c new file mode 100644 index 000000000..475c378e2 --- /dev/null +++ b/android/app/src/main/cpp/xz/xz_dec_lzma2.c @@ -0,0 +1,1345 @@ +// SPDX-License-Identifier: 0BSD + +/* + * LZMA2 decoder + * + * Authors: Lasse Collin + * Igor Pavlov + */ + +#include "xz_private.h" +#include "xz_lzma2.h" + +/* + * Range decoder initialization eats the first five bytes of each LZMA chunk. + */ +#define RC_INIT_BYTES 5 + +/* + * Minimum number of usable input buffer to safely decode one LZMA symbol. + * The worst case is that we decode 22 bits using probabilities and 26 + * direct bits. This may decode at maximum of 20 bytes of input. However, + * lzma_main() does an extra normalization before returning, thus we + * need to put 21 here. + */ +#define LZMA_IN_REQUIRED 21 + +/* + * Dictionary (history buffer) + * + * These are always true: + * start <= pos <= full <= end + * pos <= limit <= end + * + * In multi-call mode, also these are true: + * end == size + * size <= size_max + * allocated <= size + * + * Most of these variables are size_t to support single-call mode, + * in which the dictionary variables address the actual output + * buffer directly. + */ +struct dictionary { + /* Beginning of the history buffer */ + uint8_t *buf; + + /* Old position in buf (before decoding more data) */ + size_t start; + + /* Position in buf */ + size_t pos; + + /* + * How full dictionary is. This is used to detect corrupt input that + * would read beyond the beginning of the uncompressed stream. + */ + size_t full; + + /* Write limit; we don't write to buf[limit] or later bytes. */ + size_t limit; + + /* + * End of the dictionary buffer. In multi-call mode, this is + * the same as the dictionary size. In single-call mode, this + * indicates the size of the output buffer. + */ + size_t end; + + /* + * Size of the dictionary as specified in Block Header. This is used + * together with "full" to detect corrupt input that would make us + * read beyond the beginning of the uncompressed stream. + */ + uint32_t size; + + /* + * Maximum allowed dictionary size in multi-call mode. + * This is ignored in single-call mode. + */ + uint32_t size_max; + + /* + * Amount of memory currently allocated for the dictionary. + * This is used only with XZ_DYNALLOC. (With XZ_PREALLOC, + * size_max is always the same as the allocated size.) + */ + uint32_t allocated; + + /* Operation mode */ + enum xz_mode mode; +}; + +/* Range decoder */ +struct rc_dec { + uint32_t range; + uint32_t code; + + /* + * Number of initializing bytes remaining to be read + * by rc_read_init(). + */ + uint32_t init_bytes_left; + + /* + * Buffer from which we read our input. It can be either + * temp.buf or the caller-provided input buffer. + */ + const uint8_t *in; + size_t in_pos; + size_t in_limit; +}; + +/* Probabilities for a length decoder. */ +struct lzma_len_dec { + /* Probability of match length being at least 10 */ + uint16_t choice; + + /* Probability of match length being at least 18 */ + uint16_t choice2; + + /* Probabilities for match lengths 2-9 */ + uint16_t low[POS_STATES_MAX][LEN_LOW_SYMBOLS]; + + /* Probabilities for match lengths 10-17 */ + uint16_t mid[POS_STATES_MAX][LEN_MID_SYMBOLS]; + + /* Probabilities for match lengths 18-273 */ + uint16_t high[LEN_HIGH_SYMBOLS]; +}; + +struct lzma_dec { + /* Distances of latest four matches */ + uint32_t rep0; + uint32_t rep1; + uint32_t rep2; + uint32_t rep3; + + /* Types of the most recently seen LZMA symbols */ + enum lzma_state state; + + /* + * Length of a match. This is updated so that dict_repeat can + * be called again to finish repeating the whole match. + */ + uint32_t len; + + /* + * LZMA properties or related bit masks (number of literal + * context bits, a mask derived from the number of literal + * position bits, and a mask derived from the number + * position bits) + */ + uint32_t lc; + uint32_t literal_pos_mask; /* (1 << lp) - 1 */ + uint32_t pos_mask; /* (1 << pb) - 1 */ + + /* If 1, it's a match. Otherwise it's a single 8-bit literal. */ + uint16_t is_match[STATES][POS_STATES_MAX]; + + /* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */ + uint16_t is_rep[STATES]; + + /* + * If 0, distance of a repeated match is rep0. + * Otherwise check is_rep1. + */ + uint16_t is_rep0[STATES]; + + /* + * If 0, distance of a repeated match is rep1. + * Otherwise check is_rep2. + */ + uint16_t is_rep1[STATES]; + + /* If 0, distance of a repeated match is rep2. Otherwise it is rep3. */ + uint16_t is_rep2[STATES]; + + /* + * If 1, the repeated match has length of one byte. Otherwise + * the length is decoded from rep_len_decoder. + */ + uint16_t is_rep0_long[STATES][POS_STATES_MAX]; + + /* + * Probability tree for the highest two bits of the match + * distance. There is a separate probability tree for match + * lengths of 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273]. + */ + uint16_t dist_slot[DIST_STATES][DIST_SLOTS]; + + /* + * Probability trees for additional bits for match distance + * when the distance is in the range [4, 127]. + */ + uint16_t dist_special[FULL_DISTANCES - DIST_MODEL_END]; + + /* + * Probability tree for the lowest four bits of a match + * distance that is equal to or greater than 128. + */ + uint16_t dist_align[ALIGN_SIZE]; + + /* Length of a normal match */ + struct lzma_len_dec match_len_dec; + + /* Length of a repeated match */ + struct lzma_len_dec rep_len_dec; + + /* Probabilities of literals */ + uint16_t literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE]; +}; + +struct lzma2_dec { + /* Position in xz_dec_lzma2_run(). */ + enum lzma2_seq { + SEQ_CONTROL, + SEQ_UNCOMPRESSED_1, + SEQ_UNCOMPRESSED_2, + SEQ_COMPRESSED_0, + SEQ_COMPRESSED_1, + SEQ_PROPERTIES, + SEQ_LZMA_PREPARE, + SEQ_LZMA_RUN, + SEQ_COPY + } sequence; + + /* Next position after decoding the compressed size of the chunk. */ + enum lzma2_seq next_sequence; + + /* Uncompressed size of LZMA chunk (2 MiB at maximum) */ + uint32_t uncompressed; + + /* + * Compressed size of LZMA chunk or compressed/uncompressed + * size of uncompressed chunk (64 KiB at maximum) + */ + uint32_t compressed; + + /* + * True if dictionary reset is needed. This is false before + * the first chunk (LZMA or uncompressed). + */ + bool need_dict_reset; + + /* + * True if new LZMA properties are needed. This is false + * before the first LZMA chunk. + */ + bool need_props; + +#ifdef XZ_DEC_MICROLZMA + bool pedantic_microlzma; +#endif +}; + +struct xz_dec_lzma2 { + /* + * The order below is important on x86 to reduce code size and + * it shouldn't hurt on other platforms. Everything up to and + * including lzma.pos_mask are in the first 128 bytes on x86-32, + * which allows using smaller instructions to access those + * variables. On x86-64, fewer variables fit into the first 128 + * bytes, but this is still the best order without sacrificing + * the readability by splitting the structures. + */ + struct rc_dec rc; + struct dictionary dict; + struct lzma2_dec lzma2; + struct lzma_dec lzma; + + /* + * Temporary buffer which holds small number of input bytes between + * decoder calls. See lzma2_lzma() for details. + */ + struct { + uint32_t size; + uint8_t buf[3 * LZMA_IN_REQUIRED]; + } temp; +}; + +/************** + * Dictionary * + **************/ + +/* + * Reset the dictionary state. When in single-call mode, set up the beginning + * of the dictionary to point to the actual output buffer. + */ +static void dict_reset(struct dictionary *dict, struct xz_buf *b) +{ + if (DEC_IS_SINGLE(dict->mode)) { + dict->buf = b->out + b->out_pos; + dict->end = b->out_size - b->out_pos; + } + + dict->start = 0; + dict->pos = 0; + dict->limit = 0; + dict->full = 0; +} + +/* Set dictionary write limit */ +static void dict_limit(struct dictionary *dict, size_t out_max) +{ + if (dict->end - dict->pos <= out_max) + dict->limit = dict->end; + else + dict->limit = dict->pos + out_max; +} + +/* Return true if at least one byte can be written into the dictionary. */ +static inline bool dict_has_space(const struct dictionary *dict) +{ + return dict->pos < dict->limit; +} + +/* + * Get a byte from the dictionary at the given distance. The distance is + * assumed to valid, or as a special case, zero when the dictionary is + * still empty. This special case is needed for single-call decoding to + * avoid writing a '\0' to the end of the destination buffer. + */ +static inline uint32_t dict_get(const struct dictionary *dict, uint32_t dist) +{ + size_t offset = dict->pos - dist - 1; + + if (dist >= dict->pos) + offset += dict->end; + + return dict->full > 0 ? dict->buf[offset] : 0; +} + +/* + * Put one byte into the dictionary. It is assumed that there is space for it. + */ +static inline void dict_put(struct dictionary *dict, uint8_t byte) +{ + dict->buf[dict->pos++] = byte; + + if (dict->full < dict->pos) + dict->full = dict->pos; +} + +/* + * Repeat given number of bytes from the given distance. If the distance is + * invalid, false is returned. On success, true is returned and *len is + * updated to indicate how many bytes were left to be repeated. + */ +static bool dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist) +{ + size_t back; + uint32_t left; + + if (dist >= dict->full || dist >= dict->size) + return false; + + left = min_t(size_t, dict->limit - dict->pos, *len); + *len -= left; + + back = dict->pos - dist - 1; + if (dist >= dict->pos) + back += dict->end; + + do { + dict->buf[dict->pos++] = dict->buf[back++]; + if (back == dict->end) + back = 0; + } while (--left > 0); + + if (dict->full < dict->pos) + dict->full = dict->pos; + + return true; +} + +/* Copy uncompressed data as is from input to dictionary and output buffers. */ +static void dict_uncompressed(struct dictionary *dict, struct xz_buf *b, + uint32_t *left) +{ + size_t copy_size; + + while (*left > 0 && b->in_pos < b->in_size + && b->out_pos < b->out_size) { + copy_size = min(b->in_size - b->in_pos, + b->out_size - b->out_pos); + if (copy_size > dict->end - dict->pos) + copy_size = dict->end - dict->pos; + if (copy_size > *left) + copy_size = *left; + + *left -= copy_size; + + /* + * If doing in-place decompression in single-call mode and the + * uncompressed size of the file is larger than the caller + * thought (i.e. it is invalid input!), the buffers below may + * overlap and cause undefined behavior with memcpy(). + * With valid inputs memcpy() would be fine here. + */ + memmove(dict->buf + dict->pos, b->in + b->in_pos, copy_size); + dict->pos += copy_size; + + if (dict->full < dict->pos) + dict->full = dict->pos; + + if (DEC_IS_MULTI(dict->mode)) { + if (dict->pos == dict->end) + dict->pos = 0; + + /* + * Like above but for multi-call mode: use memmove() + * to avoid undefined behavior with invalid input. + */ + memmove(b->out + b->out_pos, b->in + b->in_pos, + copy_size); + } + + dict->start = dict->pos; + + b->out_pos += copy_size; + b->in_pos += copy_size; + } +} + +#ifdef XZ_DEC_MICROLZMA +# define DICT_FLUSH_SUPPORTS_SKIPPING true +#else +# define DICT_FLUSH_SUPPORTS_SKIPPING false +#endif + +/* + * Flush pending data from dictionary to b->out. It is assumed that there is + * enough space in b->out. This is guaranteed because caller uses dict_limit() + * before decoding data into the dictionary. + */ +static uint32_t dict_flush(struct dictionary *dict, struct xz_buf *b) +{ + size_t copy_size = dict->pos - dict->start; + + if (DEC_IS_MULTI(dict->mode)) { + if (dict->pos == dict->end) + dict->pos = 0; + + /* + * These buffers cannot overlap even if doing in-place + * decompression because in multi-call mode dict->buf + * has been allocated by us in this file; it's not + * provided by the caller like in single-call mode. + * + * With MicroLZMA, b->out can be NULL to skip bytes that + * the caller doesn't need. This cannot be done with XZ + * because it would break BCJ filters. + */ + if (!DICT_FLUSH_SUPPORTS_SKIPPING || b->out != NULL) + memcpy(b->out + b->out_pos, dict->buf + dict->start, + copy_size); + } + + dict->start = dict->pos; + b->out_pos += copy_size; + return copy_size; +} + +/***************** + * Range decoder * + *****************/ + +/* Reset the range decoder. */ +static void rc_reset(struct rc_dec *rc) +{ + rc->range = (uint32_t)-1; + rc->code = 0; + rc->init_bytes_left = RC_INIT_BYTES; +} + +/* + * Read the first five initial bytes into rc->code if they haven't been + * read already. (Yes, the first byte gets completely ignored.) + */ +static bool rc_read_init(struct rc_dec *rc, struct xz_buf *b) +{ + while (rc->init_bytes_left > 0) { + if (b->in_pos == b->in_size) + return false; + + rc->code = (rc->code << 8) + b->in[b->in_pos++]; + --rc->init_bytes_left; + } + + return true; +} + +/* Return true if there may not be enough input for the next decoding loop. */ +static inline bool rc_limit_exceeded(const struct rc_dec *rc) +{ + return rc->in_pos > rc->in_limit; +} + +/* + * Return true if it is possible (from point of view of range decoder) that + * we have reached the end of the LZMA chunk. + */ +static inline bool rc_is_finished(const struct rc_dec *rc) +{ + return rc->code == 0; +} + +/* Read the next input byte if needed. */ +static __always_inline void rc_normalize(struct rc_dec *rc) +{ + if (rc->range < RC_TOP_VALUE) { + rc->range <<= RC_SHIFT_BITS; + rc->code = (rc->code << RC_SHIFT_BITS) + rc->in[rc->in_pos++]; + } +} + +/* + * Decode one bit. In some versions, this function has been split in three + * functions so that the compiler is supposed to be able to more easily avoid + * an extra branch. In this particular version of the LZMA decoder, this + * doesn't seem to be a good idea (tested with GCC 3.3.6, 3.4.6, and 4.3.3 + * on x86). Using a non-split version results in nicer looking code too. + * + * NOTE: This must return an int. Do not make it return a bool or the speed + * of the code generated by GCC 3.x decreases 10-15 %. (GCC 4.3 doesn't care, + * and it generates 10-20 % faster code than GCC 3.x from this file anyway.) + */ +static __always_inline int rc_bit(struct rc_dec *rc, uint16_t *prob) +{ + uint32_t bound; + int bit; + + rc_normalize(rc); + bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob; + if (rc->code < bound) { + rc->range = bound; + *prob += (RC_BIT_MODEL_TOTAL - *prob) >> RC_MOVE_BITS; + bit = 0; + } else { + rc->range -= bound; + rc->code -= bound; + *prob -= *prob >> RC_MOVE_BITS; + bit = 1; + } + + return bit; +} + +/* Decode a bittree starting from the most significant bit. */ +static __always_inline uint32_t rc_bittree(struct rc_dec *rc, + uint16_t *probs, uint32_t limit) +{ + uint32_t symbol = 1; + + do { + if (rc_bit(rc, &probs[symbol])) + symbol = (symbol << 1) + 1; + else + symbol <<= 1; + } while (symbol < limit); + + return symbol; +} + +/* Decode a bittree starting from the least significant bit. */ +static __always_inline void rc_bittree_reverse(struct rc_dec *rc, + uint16_t *probs, + uint32_t *dest, uint32_t limit) +{ + uint32_t symbol = 1; + uint32_t i = 0; + + do { + if (rc_bit(rc, &probs[symbol])) { + symbol = (symbol << 1) + 1; + *dest += 1 << i; + } else { + symbol <<= 1; + } + } while (++i < limit); +} + +/* Decode direct bits (fixed fifty-fifty probability) */ +static inline void rc_direct(struct rc_dec *rc, uint32_t *dest, uint32_t limit) +{ + uint32_t mask; + + do { + rc_normalize(rc); + rc->range >>= 1; + rc->code -= rc->range; + mask = (uint32_t)0 - (rc->code >> 31); + rc->code += rc->range & mask; + *dest = (*dest << 1) + (mask + 1); + } while (--limit > 0); +} + +/******** + * LZMA * + ********/ + +/* Get pointer to literal coder probability array. */ +static uint16_t *lzma_literal_probs(struct xz_dec_lzma2 *s) +{ + uint32_t prev_byte = dict_get(&s->dict, 0); + uint32_t low = prev_byte >> (8 - s->lzma.lc); + uint32_t high = (s->dict.pos & s->lzma.literal_pos_mask) << s->lzma.lc; + return s->lzma.literal[low + high]; +} + +/* Decode a literal (one 8-bit byte) */ +static void lzma_literal(struct xz_dec_lzma2 *s) +{ + uint16_t *probs; + uint32_t symbol; + uint32_t match_byte; + uint32_t match_bit; + uint32_t offset; + uint32_t i; + + probs = lzma_literal_probs(s); + + if (lzma_state_is_literal(s->lzma.state)) { + symbol = rc_bittree(&s->rc, probs, 0x100); + } else { + symbol = 1; + match_byte = dict_get(&s->dict, s->lzma.rep0) << 1; + offset = 0x100; + + do { + match_bit = match_byte & offset; + match_byte <<= 1; + i = offset + match_bit + symbol; + + if (rc_bit(&s->rc, &probs[i])) { + symbol = (symbol << 1) + 1; + offset &= match_bit; + } else { + symbol <<= 1; + offset &= ~match_bit; + } + } while (symbol < 0x100); + } + + dict_put(&s->dict, (uint8_t)symbol); + lzma_state_literal(&s->lzma.state); +} + +/* Decode the length of the match into s->lzma.len. */ +static void lzma_len(struct xz_dec_lzma2 *s, struct lzma_len_dec *l, + uint32_t pos_state) +{ + uint16_t *probs; + uint32_t limit; + + if (!rc_bit(&s->rc, &l->choice)) { + probs = l->low[pos_state]; + limit = LEN_LOW_SYMBOLS; + s->lzma.len = MATCH_LEN_MIN; + } else { + if (!rc_bit(&s->rc, &l->choice2)) { + probs = l->mid[pos_state]; + limit = LEN_MID_SYMBOLS; + s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS; + } else { + probs = l->high; + limit = LEN_HIGH_SYMBOLS; + s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS + + LEN_MID_SYMBOLS; + } + } + + s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit; +} + +/* Decode a match. The distance will be stored in s->lzma.rep0. */ +static void lzma_match(struct xz_dec_lzma2 *s, uint32_t pos_state) +{ + uint16_t *probs; + uint32_t dist_slot; + uint32_t limit; + + lzma_state_match(&s->lzma.state); + + s->lzma.rep3 = s->lzma.rep2; + s->lzma.rep2 = s->lzma.rep1; + s->lzma.rep1 = s->lzma.rep0; + + lzma_len(s, &s->lzma.match_len_dec, pos_state); + + probs = s->lzma.dist_slot[lzma_get_dist_state(s->lzma.len)]; + dist_slot = rc_bittree(&s->rc, probs, DIST_SLOTS) - DIST_SLOTS; + + if (dist_slot < DIST_MODEL_START) { + s->lzma.rep0 = dist_slot; + } else { + limit = (dist_slot >> 1) - 1; + s->lzma.rep0 = 2 + (dist_slot & 1); + + if (dist_slot < DIST_MODEL_END) { + s->lzma.rep0 <<= limit; + probs = s->lzma.dist_special + s->lzma.rep0 + - dist_slot - 1; + rc_bittree_reverse(&s->rc, probs, + &s->lzma.rep0, limit); + } else { + rc_direct(&s->rc, &s->lzma.rep0, limit - ALIGN_BITS); + s->lzma.rep0 <<= ALIGN_BITS; + rc_bittree_reverse(&s->rc, s->lzma.dist_align, + &s->lzma.rep0, ALIGN_BITS); + } + } +} + +/* + * Decode a repeated match. The distance is one of the four most recently + * seen matches. The distance will be stored in s->lzma.rep0. + */ +static void lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state) +{ + uint32_t tmp; + + if (!rc_bit(&s->rc, &s->lzma.is_rep0[s->lzma.state])) { + if (!rc_bit(&s->rc, &s->lzma.is_rep0_long[ + s->lzma.state][pos_state])) { + lzma_state_short_rep(&s->lzma.state); + s->lzma.len = 1; + return; + } + } else { + if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) { + tmp = s->lzma.rep1; + } else { + if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) { + tmp = s->lzma.rep2; + } else { + tmp = s->lzma.rep3; + s->lzma.rep3 = s->lzma.rep2; + } + + s->lzma.rep2 = s->lzma.rep1; + } + + s->lzma.rep1 = s->lzma.rep0; + s->lzma.rep0 = tmp; + } + + lzma_state_long_rep(&s->lzma.state); + lzma_len(s, &s->lzma.rep_len_dec, pos_state); +} + +/* LZMA decoder core */ +static bool lzma_main(struct xz_dec_lzma2 *s) +{ + uint32_t pos_state; + + /* + * If the dictionary was reached during the previous call, try to + * finish the possibly pending repeat in the dictionary. + */ + if (dict_has_space(&s->dict) && s->lzma.len > 0) + dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0); + + /* + * Decode more LZMA symbols. One iteration may consume up to + * LZMA_IN_REQUIRED - 1 bytes. + */ + while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) { + pos_state = s->dict.pos & s->lzma.pos_mask; + + if (!rc_bit(&s->rc, &s->lzma.is_match[ + s->lzma.state][pos_state])) { + lzma_literal(s); + } else { + if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state])) + lzma_rep_match(s, pos_state); + else + lzma_match(s, pos_state); + + if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0)) + return false; + } + } + + /* + * Having the range decoder always normalized when we are outside + * this function makes it easier to correctly handle end of the chunk. + */ + rc_normalize(&s->rc); + + return true; +} + +/* + * Reset the LZMA decoder and range decoder state. Dictionary is not reset + * here, because LZMA state may be reset without resetting the dictionary. + */ +static void lzma_reset(struct xz_dec_lzma2 *s) +{ + uint16_t *probs; + size_t i; + + s->lzma.state = STATE_LIT_LIT; + s->lzma.rep0 = 0; + s->lzma.rep1 = 0; + s->lzma.rep2 = 0; + s->lzma.rep3 = 0; + s->lzma.len = 0; + + /* + * All probabilities are initialized to the same value. This hack + * makes the code smaller by avoiding a separate loop for each + * probability array. + * + * This could be optimized so that only that part of literal + * probabilities that are actually required. In the common case + * we would write 12 KiB less. + */ + probs = s->lzma.is_match[0]; + for (i = 0; i < PROBS_TOTAL; ++i) + probs[i] = RC_BIT_MODEL_TOTAL / 2; + + rc_reset(&s->rc); +} + +/* + * Decode and validate LZMA properties (lc/lp/pb) and calculate the bit masks + * from the decoded lp and pb values. On success, the LZMA decoder state is + * reset and true is returned. + */ +static bool lzma_props(struct xz_dec_lzma2 *s, uint8_t props) +{ + if (props > (4 * 5 + 4) * 9 + 8) + return false; + + s->lzma.pos_mask = 0; + while (props >= 9 * 5) { + props -= 9 * 5; + ++s->lzma.pos_mask; + } + + s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1; + + s->lzma.literal_pos_mask = 0; + while (props >= 9) { + props -= 9; + ++s->lzma.literal_pos_mask; + } + + s->lzma.lc = props; + + if (s->lzma.lc + s->lzma.literal_pos_mask > 4) + return false; + + s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1; + + lzma_reset(s); + + return true; +} + +/********* + * LZMA2 * + *********/ + +/* + * The LZMA decoder assumes that if the input limit (s->rc.in_limit) hasn't + * been exceeded, it is safe to read up to LZMA_IN_REQUIRED bytes. This + * wrapper function takes care of making the LZMA decoder's assumption safe. + * + * As long as there is plenty of input left to be decoded in the current LZMA + * chunk, we decode directly from the caller-supplied input buffer until + * there's LZMA_IN_REQUIRED bytes left. Those remaining bytes are copied into + * s->temp.buf, which (hopefully) gets filled on the next call to this + * function. We decode a few bytes from the temporary buffer so that we can + * continue decoding from the caller-supplied input buffer again. + */ +static bool lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b) +{ + size_t in_avail; + uint32_t tmp; + + in_avail = b->in_size - b->in_pos; + if (s->temp.size > 0 || s->lzma2.compressed == 0) { + tmp = 2 * LZMA_IN_REQUIRED - s->temp.size; + if (tmp > s->lzma2.compressed - s->temp.size) + tmp = s->lzma2.compressed - s->temp.size; + if (tmp > in_avail) + tmp = in_avail; + + memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp); + + if (s->temp.size + tmp == s->lzma2.compressed) { + memzero(s->temp.buf + s->temp.size + tmp, + sizeof(s->temp.buf) + - s->temp.size - tmp); + s->rc.in_limit = s->temp.size + tmp; + } else if (s->temp.size + tmp < LZMA_IN_REQUIRED) { + s->temp.size += tmp; + b->in_pos += tmp; + return true; + } else { + s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED; + } + + s->rc.in = s->temp.buf; + s->rc.in_pos = 0; + + if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp) + return false; + + s->lzma2.compressed -= s->rc.in_pos; + + if (s->rc.in_pos < s->temp.size) { + s->temp.size -= s->rc.in_pos; + memmove(s->temp.buf, s->temp.buf + s->rc.in_pos, + s->temp.size); + return true; + } + + b->in_pos += s->rc.in_pos - s->temp.size; + s->temp.size = 0; + } + + in_avail = b->in_size - b->in_pos; + if (in_avail >= LZMA_IN_REQUIRED) { + s->rc.in = b->in; + s->rc.in_pos = b->in_pos; + + if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED) + s->rc.in_limit = b->in_pos + s->lzma2.compressed; + else + s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED; + + if (!lzma_main(s)) + return false; + + in_avail = s->rc.in_pos - b->in_pos; + if (in_avail > s->lzma2.compressed) + return false; + + s->lzma2.compressed -= in_avail; + b->in_pos = s->rc.in_pos; + } + + in_avail = b->in_size - b->in_pos; + if (in_avail < LZMA_IN_REQUIRED) { + if (in_avail > s->lzma2.compressed) + in_avail = s->lzma2.compressed; + + memcpy(s->temp.buf, b->in + b->in_pos, in_avail); + s->temp.size = in_avail; + b->in_pos += in_avail; + } + + return true; +} + +/* + * Take care of the LZMA2 control layer, and forward the job of actual LZMA + * decoding or copying of uncompressed chunks to other functions. + */ +XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, + struct xz_buf *b) +{ + uint32_t tmp; + + while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) { + switch (s->lzma2.sequence) { + case SEQ_CONTROL: + /* + * LZMA2 control byte + * + * Exact values: + * 0x00 End marker + * 0x01 Dictionary reset followed by + * an uncompressed chunk + * 0x02 Uncompressed chunk (no dictionary reset) + * + * Highest three bits (s->control & 0xE0): + * 0xE0 Dictionary reset, new properties and state + * reset, followed by LZMA compressed chunk + * 0xC0 New properties and state reset, followed + * by LZMA compressed chunk (no dictionary + * reset) + * 0xA0 State reset using old properties, + * followed by LZMA compressed chunk (no + * dictionary reset) + * 0x80 LZMA chunk (no dictionary or state reset) + * + * For LZMA compressed chunks, the lowest five bits + * (s->control & 1F) are the highest bits of the + * uncompressed size (bits 16-20). + * + * A new LZMA2 stream must begin with a dictionary + * reset. The first LZMA chunk must set new + * properties and reset the LZMA state. + * + * Values that don't match anything described above + * are invalid and we return XZ_DATA_ERROR. + */ + tmp = b->in[b->in_pos++]; + + if (tmp == 0x00) + return XZ_STREAM_END; + + if (tmp >= 0xE0 || tmp == 0x01) { + s->lzma2.need_props = true; + s->lzma2.need_dict_reset = false; + dict_reset(&s->dict, b); + } else if (s->lzma2.need_dict_reset) { + return XZ_DATA_ERROR; + } + + if (tmp >= 0x80) { + s->lzma2.uncompressed = (tmp & 0x1F) << 16; + s->lzma2.sequence = SEQ_UNCOMPRESSED_1; + + if (tmp >= 0xC0) { + /* + * When there are new properties, + * state reset is done at + * SEQ_PROPERTIES. + */ + s->lzma2.need_props = false; + s->lzma2.next_sequence + = SEQ_PROPERTIES; + + } else if (s->lzma2.need_props) { + return XZ_DATA_ERROR; + + } else { + s->lzma2.next_sequence + = SEQ_LZMA_PREPARE; + if (tmp >= 0xA0) + lzma_reset(s); + } + } else { + if (tmp > 0x02) + return XZ_DATA_ERROR; + + s->lzma2.sequence = SEQ_COMPRESSED_0; + s->lzma2.next_sequence = SEQ_COPY; + } + + break; + + case SEQ_UNCOMPRESSED_1: + s->lzma2.uncompressed + += (uint32_t)b->in[b->in_pos++] << 8; + s->lzma2.sequence = SEQ_UNCOMPRESSED_2; + break; + + case SEQ_UNCOMPRESSED_2: + s->lzma2.uncompressed + += (uint32_t)b->in[b->in_pos++] + 1; + s->lzma2.sequence = SEQ_COMPRESSED_0; + break; + + case SEQ_COMPRESSED_0: + s->lzma2.compressed + = (uint32_t)b->in[b->in_pos++] << 8; + s->lzma2.sequence = SEQ_COMPRESSED_1; + break; + + case SEQ_COMPRESSED_1: + s->lzma2.compressed + += (uint32_t)b->in[b->in_pos++] + 1; + s->lzma2.sequence = s->lzma2.next_sequence; + break; + + case SEQ_PROPERTIES: + if (!lzma_props(s, b->in[b->in_pos++])) + return XZ_DATA_ERROR; + + s->lzma2.sequence = SEQ_LZMA_PREPARE; + + fallthrough; + + case SEQ_LZMA_PREPARE: + if (s->lzma2.compressed < RC_INIT_BYTES) + return XZ_DATA_ERROR; + + if (!rc_read_init(&s->rc, b)) + return XZ_OK; + + s->lzma2.compressed -= RC_INIT_BYTES; + s->lzma2.sequence = SEQ_LZMA_RUN; + + fallthrough; + + case SEQ_LZMA_RUN: + /* + * Set dictionary limit to indicate how much we want + * to be encoded at maximum. Decode new data into the + * dictionary. Flush the new data from dictionary to + * b->out. Check if we finished decoding this chunk. + * In case the dictionary got full but we didn't fill + * the output buffer yet, we may run this loop + * multiple times without changing s->lzma2.sequence. + */ + dict_limit(&s->dict, min_t(size_t, + b->out_size - b->out_pos, + s->lzma2.uncompressed)); + if (!lzma2_lzma(s, b)) + return XZ_DATA_ERROR; + + s->lzma2.uncompressed -= dict_flush(&s->dict, b); + + if (s->lzma2.uncompressed == 0) { + if (s->lzma2.compressed > 0 || s->lzma.len > 0 + || !rc_is_finished(&s->rc)) + return XZ_DATA_ERROR; + + rc_reset(&s->rc); + s->lzma2.sequence = SEQ_CONTROL; + + } else if (b->out_pos == b->out_size + || (b->in_pos == b->in_size + && s->temp.size + < s->lzma2.compressed)) { + return XZ_OK; + } + + break; + + case SEQ_COPY: + dict_uncompressed(&s->dict, b, &s->lzma2.compressed); + if (s->lzma2.compressed > 0) + return XZ_OK; + + s->lzma2.sequence = SEQ_CONTROL; + break; + } + } + + return XZ_OK; +} + +XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode, + uint32_t dict_max) +{ + struct xz_dec_lzma2 *s = kmalloc(sizeof(*s), GFP_KERNEL); + if (s == NULL) + return NULL; + + s->dict.mode = mode; + s->dict.size_max = dict_max; + + if (DEC_IS_PREALLOC(mode)) { + s->dict.buf = vmalloc(dict_max); + if (s->dict.buf == NULL) { + kfree(s); + return NULL; + } + } else if (DEC_IS_DYNALLOC(mode)) { + s->dict.buf = NULL; + s->dict.allocated = 0; + } + + return s; +} + +XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props) +{ + /* This limits dictionary size to 3 GiB to keep parsing simpler. */ + if (props > 39) + return XZ_OPTIONS_ERROR; + + s->dict.size = 2 + (props & 1); + s->dict.size <<= (props >> 1) + 11; + + if (DEC_IS_MULTI(s->dict.mode)) { + if (s->dict.size > s->dict.size_max) + return XZ_MEMLIMIT_ERROR; + + s->dict.end = s->dict.size; + + if (DEC_IS_DYNALLOC(s->dict.mode)) { + if (s->dict.allocated < s->dict.size) { + s->dict.allocated = s->dict.size; + vfree(s->dict.buf); + s->dict.buf = vmalloc(s->dict.size); + if (s->dict.buf == NULL) { + s->dict.allocated = 0; + return XZ_MEM_ERROR; + } + } + } + } + + s->lzma2.sequence = SEQ_CONTROL; + s->lzma2.need_dict_reset = true; + + s->temp.size = 0; + + return XZ_OK; +} + +XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s) +{ + if (DEC_IS_MULTI(s->dict.mode)) + vfree(s->dict.buf); + + kfree(s); +} + +#ifdef XZ_DEC_MICROLZMA +/* This is a wrapper struct to have a nice struct name in the public API. */ +struct xz_dec_microlzma { + struct xz_dec_lzma2 s; +}; + +XZ_EXTERN enum xz_ret xz_dec_microlzma_run(struct xz_dec_microlzma *s_ptr, + struct xz_buf *b) +{ + struct xz_dec_lzma2 *s = &s_ptr->s; + + /* + * sequence is SEQ_PROPERTIES before the first input byte, + * SEQ_LZMA_PREPARE until a total of five bytes have been read, + * and SEQ_LZMA_RUN for the rest of the input stream. + */ + if (s->lzma2.sequence != SEQ_LZMA_RUN) { + if (s->lzma2.sequence == SEQ_PROPERTIES) { + /* One byte is needed for the props. */ + if (b->in_pos >= b->in_size) + return XZ_OK; + + /* + * Don't increment b->in_pos here. The same byte is + * also passed to rc_read_init() which will ignore it. + */ + if (!lzma_props(s, ~b->in[b->in_pos])) + return XZ_DATA_ERROR; + + s->lzma2.sequence = SEQ_LZMA_PREPARE; + } + + /* + * xz_dec_microlzma_reset() doesn't validate the compressed + * size so we do it here. We have to limit the maximum size + * to avoid integer overflows in lzma2_lzma(). 3 GiB is a nice + * round number and much more than users of this code should + * ever need. + */ + if (s->lzma2.compressed < RC_INIT_BYTES + || s->lzma2.compressed > (3U << 30)) + return XZ_DATA_ERROR; + + if (!rc_read_init(&s->rc, b)) + return XZ_OK; + + s->lzma2.compressed -= RC_INIT_BYTES; + s->lzma2.sequence = SEQ_LZMA_RUN; + + dict_reset(&s->dict, b); + } + + /* This is to allow increasing b->out_size between calls. */ + if (DEC_IS_SINGLE(s->dict.mode)) + s->dict.end = b->out_size - b->out_pos; + + while (true) { + dict_limit(&s->dict, min_t(size_t, b->out_size - b->out_pos, + s->lzma2.uncompressed)); + + if (!lzma2_lzma(s, b)) + return XZ_DATA_ERROR; + + s->lzma2.uncompressed -= dict_flush(&s->dict, b); + + if (s->lzma2.uncompressed == 0) { + if (s->lzma2.pedantic_microlzma) { + if (s->lzma2.compressed > 0 || s->lzma.len > 0 + || !rc_is_finished(&s->rc)) + return XZ_DATA_ERROR; + } + + return XZ_STREAM_END; + } + + if (b->out_pos == b->out_size) + return XZ_OK; + + if (b->in_pos == b->in_size + && s->temp.size < s->lzma2.compressed) + return XZ_OK; + } +} + +XZ_EXTERN struct xz_dec_microlzma *xz_dec_microlzma_alloc(enum xz_mode mode, + uint32_t dict_size) +{ + struct xz_dec_microlzma *s; + + /* Restrict dict_size to the same range as in the LZMA2 code. */ + if (dict_size < 4096 || dict_size > (3U << 30)) + return NULL; + + s = kmalloc(sizeof(*s), GFP_KERNEL); + if (s == NULL) + return NULL; + + s->s.dict.mode = mode; + s->s.dict.size = dict_size; + + if (DEC_IS_MULTI(mode)) { + s->s.dict.end = dict_size; + + s->s.dict.buf = vmalloc(dict_size); + if (s->s.dict.buf == NULL) { + kfree(s); + return NULL; + } + } + + return s; +} + +XZ_EXTERN void xz_dec_microlzma_reset(struct xz_dec_microlzma *s, + uint32_t comp_size, + uint32_t uncomp_size, + int uncomp_size_is_exact) +{ + /* + * comp_size is validated in xz_dec_microlzma_run(). + * uncomp_size can safely be anything. + */ + s->s.lzma2.compressed = comp_size; + s->s.lzma2.uncompressed = uncomp_size; + s->s.lzma2.pedantic_microlzma = uncomp_size_is_exact; + + s->s.lzma2.sequence = SEQ_PROPERTIES; + s->s.temp.size = 0; +} + +XZ_EXTERN void xz_dec_microlzma_end(struct xz_dec_microlzma *s) +{ + if (DEC_IS_MULTI(s->s.dict.mode)) + vfree(s->s.dict.buf); + + kfree(s); +} +#endif diff --git a/android/app/src/main/cpp/xz/xz_dec_stream.c b/android/app/src/main/cpp/xz/xz_dec_stream.c new file mode 100644 index 000000000..33927e8e8 --- /dev/null +++ b/android/app/src/main/cpp/xz/xz_dec_stream.c @@ -0,0 +1,984 @@ +// SPDX-License-Identifier: 0BSD + +/* + * .xz Stream decoder + * + * Author: Lasse Collin + */ + +#include "xz_private.h" +#include "xz_stream.h" + +#ifdef XZ_USE_CRC64 +# define IS_CRC64(check_type) ((check_type) == XZ_CHECK_CRC64) +#else +# define IS_CRC64(check_type) false +#endif + +#ifdef XZ_USE_SHA256 +# define IS_SHA256(check_type) ((check_type) == XZ_CHECK_SHA256) +#else +# define IS_SHA256(check_type) false +#endif + +/* Hash used to validate the Index field */ +struct xz_dec_hash { + vli_type unpadded; + vli_type uncompressed; + uint32_t crc32; +}; + +struct xz_dec { + /* Position in dec_main() */ + enum { + SEQ_STREAM_HEADER, + SEQ_BLOCK_START, + SEQ_BLOCK_HEADER, + SEQ_BLOCK_UNCOMPRESS, + SEQ_BLOCK_PADDING, + SEQ_BLOCK_CHECK, + SEQ_INDEX, + SEQ_INDEX_PADDING, + SEQ_INDEX_CRC32, + SEQ_STREAM_FOOTER, + SEQ_STREAM_PADDING + } sequence; + + /* Position in variable-length integers and Check fields */ + uint32_t pos; + + /* Variable-length integer decoded by dec_vli() */ + vli_type vli; + + /* Saved in_pos and out_pos */ + size_t in_start; + size_t out_start; + +#ifdef XZ_USE_CRC64 + /* CRC32 or CRC64 value in Block or CRC32 value in Index */ + uint64_t crc; +#else + /* CRC32 value in Block or Index */ + uint32_t crc; +#endif + + /* Type of the integrity check calculated from uncompressed data */ + enum xz_check check_type; + + /* Operation mode */ + enum xz_mode mode; + + /* + * True if the next call to xz_dec_run() is allowed to return + * XZ_BUF_ERROR. + */ + bool allow_buf_error; + + /* Information stored in Block Header */ + struct { + /* + * Value stored in the Compressed Size field, or + * VLI_UNKNOWN if Compressed Size is not present. + */ + vli_type compressed; + + /* + * Value stored in the Uncompressed Size field, or + * VLI_UNKNOWN if Uncompressed Size is not present. + */ + vli_type uncompressed; + + /* Size of the Block Header field */ + uint32_t size; + } block_header; + + /* Information collected when decoding Blocks */ + struct { + /* Observed compressed size of the current Block */ + vli_type compressed; + + /* Observed uncompressed size of the current Block */ + vli_type uncompressed; + + /* Number of Blocks decoded so far */ + vli_type count; + + /* + * Hash calculated from the Block sizes. This is used to + * validate the Index field. + */ + struct xz_dec_hash hash; + } block; + + /* Variables needed when verifying the Index field */ + struct { + /* Position in dec_index() */ + enum { + SEQ_INDEX_COUNT, + SEQ_INDEX_UNPADDED, + SEQ_INDEX_UNCOMPRESSED + } sequence; + + /* Size of the Index in bytes */ + vli_type size; + + /* Number of Records (matches block.count in valid files) */ + vli_type count; + + /* + * Hash calculated from the Records (matches block.hash in + * valid files). + */ + struct xz_dec_hash hash; + } index; + + /* + * Temporary buffer needed to hold Stream Header, Block Header, + * and Stream Footer. The Block Header is the biggest (1 KiB) + * so we reserve space according to that. buf[] has to be aligned + * to a multiple of four bytes; the size_t variables before it + * should guarantee this. + */ + struct { + size_t pos; + size_t size; + uint8_t buf[1024]; + } temp; + + struct xz_dec_lzma2 *lzma2; + +#ifdef XZ_DEC_BCJ + struct xz_dec_bcj *bcj; + bool bcj_active; +#endif + +#ifdef XZ_USE_SHA256 + /* + * SHA-256 value in Block + * + * struct xz_sha256 is over a hundred bytes and it's only accessed + * from a few places. By putting the SHA-256 state near the end + * of struct xz_dec (somewhere after the "index" member) reduces + * code size at least on x86 and RISC-V. It's because the first bytes + * of the struct can be accessed with smaller instructions; the + * members that are accessed from many places should be at the top. + */ + struct xz_sha256 sha256; +#endif +}; + +#if defined(XZ_DEC_ANY_CHECK) || defined(XZ_USE_SHA256) +/* Sizes of the Check field with different Check IDs */ +static const uint8_t check_sizes[16] = { + 0, + 4, 4, 4, + 8, 8, 8, + 16, 16, 16, + 32, 32, 32, + 64, 64, 64 +}; +#endif + +/* + * Fill s->temp by copying data starting from b->in[b->in_pos]. Caller + * must have set s->temp.pos and s->temp.size to indicate how much data + * we are supposed to copy into s->temp.buf. Return true once s->temp.pos + * has reached s->temp.size. + */ +static bool fill_temp(struct xz_dec *s, struct xz_buf *b) +{ + size_t copy_size = min_t(size_t, + b->in_size - b->in_pos, s->temp.size - s->temp.pos); + + memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size); + b->in_pos += copy_size; + s->temp.pos += copy_size; + + if (s->temp.pos == s->temp.size) { + s->temp.pos = 0; + return true; + } + + return false; +} + +/* Decode a variable-length integer (little-endian base-128 encoding) */ +static enum xz_ret dec_vli(struct xz_dec *s, const uint8_t *in, + size_t *in_pos, size_t in_size) +{ + uint8_t byte; + + if (s->pos == 0) + s->vli = 0; + + while (*in_pos < in_size) { + byte = in[*in_pos]; + ++*in_pos; + + s->vli |= (vli_type)(byte & 0x7F) << s->pos; + + if ((byte & 0x80) == 0) { + /* Don't allow non-minimal encodings. */ + if (byte == 0 && s->pos != 0) + return XZ_DATA_ERROR; + + s->pos = 0; + return XZ_STREAM_END; + } + + s->pos += 7; + if (s->pos == 7 * VLI_BYTES_MAX) + return XZ_DATA_ERROR; + } + + return XZ_OK; +} + +/* + * Decode the Compressed Data field from a Block. Update and validate + * the observed compressed and uncompressed sizes of the Block so that + * they don't exceed the values possibly stored in the Block Header + * (validation assumes that no integer overflow occurs, since vli_type + * is normally uint64_t). Update the CRC32 or CRC64 value if presence of + * the CRC32 or CRC64 field was indicated in Stream Header. + * + * Once the decoding is finished, validate that the observed sizes match + * the sizes possibly stored in the Block Header. Update the hash and + * Block count, which are later used to validate the Index field. + */ +static enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b) +{ + enum xz_ret ret; + + s->in_start = b->in_pos; + s->out_start = b->out_pos; + +#ifdef XZ_DEC_BCJ + if (s->bcj_active) + ret = xz_dec_bcj_run(s->bcj, s->lzma2, b); + else +#endif + ret = xz_dec_lzma2_run(s->lzma2, b); + + s->block.compressed += b->in_pos - s->in_start; + s->block.uncompressed += b->out_pos - s->out_start; + + /* + * There is no need to separately check for VLI_UNKNOWN, since + * the observed sizes are always smaller than VLI_UNKNOWN. + */ + if (s->block.compressed > s->block_header.compressed + || s->block.uncompressed + > s->block_header.uncompressed) + return XZ_DATA_ERROR; + + if (s->check_type == XZ_CHECK_CRC32) + s->crc = xz_crc32(b->out + s->out_start, + b->out_pos - s->out_start, s->crc); +#ifdef XZ_USE_CRC64 + else if (s->check_type == XZ_CHECK_CRC64) + s->crc = xz_crc64(b->out + s->out_start, + b->out_pos - s->out_start, s->crc); +#endif +#ifdef XZ_USE_SHA256 + else if (s->check_type == XZ_CHECK_SHA256) + xz_sha256_update(b->out + s->out_start, + b->out_pos - s->out_start, &s->sha256); +#endif + + if (ret == XZ_STREAM_END) { + if (s->block_header.compressed != VLI_UNKNOWN + && s->block_header.compressed + != s->block.compressed) + return XZ_DATA_ERROR; + + if (s->block_header.uncompressed != VLI_UNKNOWN + && s->block_header.uncompressed + != s->block.uncompressed) + return XZ_DATA_ERROR; + + s->block.hash.unpadded += s->block_header.size + + s->block.compressed; + +#if defined(XZ_DEC_ANY_CHECK) || defined(XZ_USE_SHA256) + s->block.hash.unpadded += check_sizes[s->check_type]; +#else + if (s->check_type == XZ_CHECK_CRC32) + s->block.hash.unpadded += 4; + else if (IS_CRC64(s->check_type)) + s->block.hash.unpadded += 8; +#endif + + s->block.hash.uncompressed += s->block.uncompressed; + s->block.hash.crc32 = xz_crc32( + (const uint8_t *)&s->block.hash, + sizeof(s->block.hash), s->block.hash.crc32); + + ++s->block.count; + } + + return ret; +} + +/* Update the Index size and the CRC32 value. */ +static void index_update(struct xz_dec *s, const struct xz_buf *b) +{ + size_t in_used = b->in_pos - s->in_start; + s->index.size += in_used; + s->crc = xz_crc32(b->in + s->in_start, in_used, s->crc); +} + +/* + * Decode the Number of Records, Unpadded Size, and Uncompressed Size + * fields from the Index field. That is, Index Padding and CRC32 are not + * decoded by this function. + * + * This can return XZ_OK (more input needed), XZ_STREAM_END (everything + * successfully decoded), or XZ_DATA_ERROR (input is corrupt). + */ +static enum xz_ret dec_index(struct xz_dec *s, struct xz_buf *b) +{ + enum xz_ret ret; + + do { + ret = dec_vli(s, b->in, &b->in_pos, b->in_size); + if (ret != XZ_STREAM_END) { + index_update(s, b); + return ret; + } + + switch (s->index.sequence) { + case SEQ_INDEX_COUNT: + s->index.count = s->vli; + + /* + * Validate that the Number of Records field + * indicates the same number of Records as + * there were Blocks in the Stream. + */ + if (s->index.count != s->block.count) + return XZ_DATA_ERROR; + + s->index.sequence = SEQ_INDEX_UNPADDED; + break; + + case SEQ_INDEX_UNPADDED: + s->index.hash.unpadded += s->vli; + s->index.sequence = SEQ_INDEX_UNCOMPRESSED; + break; + + case SEQ_INDEX_UNCOMPRESSED: + s->index.hash.uncompressed += s->vli; + s->index.hash.crc32 = xz_crc32( + (const uint8_t *)&s->index.hash, + sizeof(s->index.hash), + s->index.hash.crc32); + --s->index.count; + s->index.sequence = SEQ_INDEX_UNPADDED; + break; + } + } while (s->index.count > 0); + + return XZ_STREAM_END; +} + +/* + * Validate that the next four or eight input bytes match the value + * of s->crc. s->pos must be zero when starting to validate the first byte. + * The "bits" argument allows using the same code for both CRC32 and CRC64. + */ +static enum xz_ret crc_validate(struct xz_dec *s, struct xz_buf *b, + uint32_t bits) +{ + do { + if (b->in_pos == b->in_size) + return XZ_OK; + + if (((s->crc >> s->pos) & 0xFF) != b->in[b->in_pos++]) + return XZ_DATA_ERROR; + + s->pos += 8; + + } while (s->pos < bits); + + s->crc = 0; + s->pos = 0; + + return XZ_STREAM_END; +} + +#ifdef XZ_DEC_ANY_CHECK +/* + * Skip over the Check field when the Check ID is not supported. + * Returns true once the whole Check field has been skipped over. + */ +static bool check_skip(struct xz_dec *s, struct xz_buf *b) +{ + while (s->pos < check_sizes[s->check_type]) { + if (b->in_pos == b->in_size) + return false; + + ++b->in_pos; + ++s->pos; + } + + s->pos = 0; + + return true; +} +#endif + +/* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */ +static enum xz_ret dec_stream_header(struct xz_dec *s) +{ + if (!memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE)) + return XZ_FORMAT_ERROR; + + if (xz_crc32(s->temp.buf + HEADER_MAGIC_SIZE, 2, 0) + != get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2)) + return XZ_DATA_ERROR; + + if (s->temp.buf[HEADER_MAGIC_SIZE] != 0) + return XZ_OPTIONS_ERROR; + + /* + * Of integrity checks, we support none (Check ID = 0), + * CRC32 (Check ID = 1), and optionally CRC64 (Check ID = 4). + * However, if XZ_DEC_ANY_CHECK is defined, we will accept other + * check types too, but then the check won't be verified and + * a warning (XZ_UNSUPPORTED_CHECK) will be given. + */ + if (s->temp.buf[HEADER_MAGIC_SIZE + 1] > XZ_CHECK_MAX) + return XZ_OPTIONS_ERROR; + + s->check_type = s->temp.buf[HEADER_MAGIC_SIZE + 1]; + + if (s->check_type > XZ_CHECK_CRC32 && !IS_CRC64(s->check_type) + && !IS_SHA256(s->check_type)) { +#ifdef XZ_DEC_ANY_CHECK + return XZ_UNSUPPORTED_CHECK; +#else + return XZ_OPTIONS_ERROR; +#endif + } + + return XZ_OK; +} + +/* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */ +static enum xz_ret dec_stream_footer(struct xz_dec *s) +{ + if (!memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE)) + return XZ_DATA_ERROR; + + if (xz_crc32(s->temp.buf + 4, 6, 0) != get_le32(s->temp.buf)) + return XZ_DATA_ERROR; + + /* + * Validate Backward Size. Note that we never added the size of the + * Index CRC32 field to s->index.size, thus we use s->index.size / 4 + * instead of s->index.size / 4 - 1. + */ + if ((s->index.size >> 2) != get_le32(s->temp.buf + 4)) + return XZ_DATA_ERROR; + + if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->check_type) + return XZ_DATA_ERROR; + + /* + * Use XZ_STREAM_END instead of XZ_OK to be more convenient + * for the caller. + */ + return XZ_STREAM_END; +} + +/* Decode the Block Header and initialize the filter chain. */ +static enum xz_ret dec_block_header(struct xz_dec *s) +{ + enum xz_ret ret; + + /* + * Validate the CRC32. We know that the temp buffer is at least + * eight bytes so this is safe. + */ + s->temp.size -= 4; + if (xz_crc32(s->temp.buf, s->temp.size, 0) + != get_le32(s->temp.buf + s->temp.size)) + return XZ_DATA_ERROR; + + s->temp.pos = 2; + + /* + * Catch unsupported Block Flags. We support only one or two filters + * in the chain, so we catch that with the same test. + */ +#ifdef XZ_DEC_BCJ + if (s->temp.buf[1] & 0x3E) +#else + if (s->temp.buf[1] & 0x3F) +#endif + return XZ_OPTIONS_ERROR; + + /* Compressed Size */ + if (s->temp.buf[1] & 0x40) { + if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size) + != XZ_STREAM_END) + return XZ_DATA_ERROR; + + s->block_header.compressed = s->vli; + } else { + s->block_header.compressed = VLI_UNKNOWN; + } + + /* Uncompressed Size */ + if (s->temp.buf[1] & 0x80) { + if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size) + != XZ_STREAM_END) + return XZ_DATA_ERROR; + + s->block_header.uncompressed = s->vli; + } else { + s->block_header.uncompressed = VLI_UNKNOWN; + } + +#ifdef XZ_DEC_BCJ + /* If there are two filters, the first one must be a BCJ filter. */ + s->bcj_active = s->temp.buf[1] & 0x01; + if (s->bcj_active) { + if (s->temp.size - s->temp.pos < 2) + return XZ_OPTIONS_ERROR; + + ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]); + if (ret != XZ_OK) + return ret; + + /* + * We don't support custom start offset, + * so Size of Properties must be zero. + */ + if (s->temp.buf[s->temp.pos++] != 0x00) + return XZ_OPTIONS_ERROR; + } +#endif + + /* Valid Filter Flags always take at least two bytes. */ + if (s->temp.size - s->temp.pos < 2) + return XZ_DATA_ERROR; + + /* Filter ID = LZMA2 */ + if (s->temp.buf[s->temp.pos++] != 0x21) + return XZ_OPTIONS_ERROR; + + /* Size of Properties = 1-byte Filter Properties */ + if (s->temp.buf[s->temp.pos++] != 0x01) + return XZ_OPTIONS_ERROR; + + /* Filter Properties contains LZMA2 dictionary size. */ + if (s->temp.size - s->temp.pos < 1) + return XZ_DATA_ERROR; + + ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]); + if (ret != XZ_OK) + return ret; + + /* The rest must be Header Padding. */ + while (s->temp.pos < s->temp.size) + if (s->temp.buf[s->temp.pos++] != 0x00) + return XZ_OPTIONS_ERROR; + + s->temp.pos = 0; + s->block.compressed = 0; + s->block.uncompressed = 0; + + return XZ_OK; +} + +static enum xz_ret dec_main(struct xz_dec *s, struct xz_buf *b) +{ + enum xz_ret ret; + + /* + * Store the start position for the case when we are in the middle + * of the Index field. + */ + s->in_start = b->in_pos; + + while (true) { + switch (s->sequence) { + case SEQ_STREAM_HEADER: + /* + * Stream Header is copied to s->temp, and then + * decoded from there. This way if the caller + * gives us only little input at a time, we can + * still keep the Stream Header decoding code + * simple. Similar approach is used in many places + * in this file. + */ + if (!fill_temp(s, b)) + return XZ_OK; + + /* + * If dec_stream_header() returns + * XZ_UNSUPPORTED_CHECK, it is still possible + * to continue decoding if working in multi-call + * mode. Thus, update s->sequence before calling + * dec_stream_header(). + */ + s->sequence = SEQ_BLOCK_START; + + ret = dec_stream_header(s); + if (ret != XZ_OK) + return ret; + + fallthrough; + + case SEQ_BLOCK_START: + /* We need one byte of input to continue. */ + if (b->in_pos == b->in_size) + return XZ_OK; + + /* See if this is the beginning of the Index field. */ + if (b->in[b->in_pos] == 0) { + s->in_start = b->in_pos++; + s->sequence = SEQ_INDEX; + break; + } + + /* + * Calculate the size of the Block Header and + * prepare to decode it. + */ + s->block_header.size + = ((uint32_t)b->in[b->in_pos] + 1) * 4; + + s->temp.size = s->block_header.size; + s->temp.pos = 0; + s->sequence = SEQ_BLOCK_HEADER; + + fallthrough; + + case SEQ_BLOCK_HEADER: + if (!fill_temp(s, b)) + return XZ_OK; + + ret = dec_block_header(s); + if (ret != XZ_OK) + return ret; + +#ifdef XZ_USE_SHA256 + if (s->check_type == XZ_CHECK_SHA256) + xz_sha256_reset(&s->sha256); +#endif + + s->sequence = SEQ_BLOCK_UNCOMPRESS; + + fallthrough; + + case SEQ_BLOCK_UNCOMPRESS: + ret = dec_block(s, b); + if (ret != XZ_STREAM_END) + return ret; + + s->sequence = SEQ_BLOCK_PADDING; + + fallthrough; + + case SEQ_BLOCK_PADDING: + /* + * Size of Compressed Data + Block Padding + * must be a multiple of four. We don't need + * s->block.compressed for anything else + * anymore, so we use it here to test the size + * of the Block Padding field. + */ + while (s->block.compressed & 3) { + if (b->in_pos == b->in_size) + return XZ_OK; + + if (b->in[b->in_pos++] != 0) + return XZ_DATA_ERROR; + + ++s->block.compressed; + } + + s->sequence = SEQ_BLOCK_CHECK; + + fallthrough; + + case SEQ_BLOCK_CHECK: + if (s->check_type == XZ_CHECK_CRC32) { + ret = crc_validate(s, b, 32); + if (ret != XZ_STREAM_END) + return ret; + } + else if (IS_CRC64(s->check_type)) { + ret = crc_validate(s, b, 64); + if (ret != XZ_STREAM_END) + return ret; + } +#ifdef XZ_USE_SHA256 + else if (s->check_type == XZ_CHECK_SHA256) { + s->temp.size = 32; + if (!fill_temp(s, b)) + return XZ_OK; + + if (!xz_sha256_validate(s->temp.buf, + &s->sha256)) + return XZ_DATA_ERROR; + + s->pos = 0; + } +#endif +#ifdef XZ_DEC_ANY_CHECK + else if (!check_skip(s, b)) { + return XZ_OK; + } +#endif + + s->sequence = SEQ_BLOCK_START; + break; + + case SEQ_INDEX: + ret = dec_index(s, b); + if (ret != XZ_STREAM_END) + return ret; + + s->sequence = SEQ_INDEX_PADDING; + + fallthrough; + + case SEQ_INDEX_PADDING: + while ((s->index.size + (b->in_pos - s->in_start)) + & 3) { + if (b->in_pos == b->in_size) { + index_update(s, b); + return XZ_OK; + } + + if (b->in[b->in_pos++] != 0) + return XZ_DATA_ERROR; + } + + /* Finish the CRC32 value and Index size. */ + index_update(s, b); + + /* Compare the hashes to validate the Index field. */ + if (!memeq(&s->block.hash, &s->index.hash, + sizeof(s->block.hash))) + return XZ_DATA_ERROR; + + s->sequence = SEQ_INDEX_CRC32; + + fallthrough; + + case SEQ_INDEX_CRC32: + ret = crc_validate(s, b, 32); + if (ret != XZ_STREAM_END) + return ret; + + s->temp.size = STREAM_HEADER_SIZE; + s->sequence = SEQ_STREAM_FOOTER; + + fallthrough; + + case SEQ_STREAM_FOOTER: + if (!fill_temp(s, b)) + return XZ_OK; + + return dec_stream_footer(s); + + case SEQ_STREAM_PADDING: + /* Never reached, only silencing a warning */ + break; + } + } + + /* Never reached */ +} + +/* + * xz_dec_run() is a wrapper for dec_main() to handle some special cases in + * multi-call and single-call decoding. + * + * In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we + * are not going to make any progress anymore. This is to prevent the caller + * from calling us infinitely when the input file is truncated or otherwise + * corrupt. Since zlib-style API allows that the caller fills the input buffer + * only when the decoder doesn't produce any new output, we have to be careful + * to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only + * after the second consecutive call to xz_dec_run() that makes no progress. + * + * In single-call mode, if we couldn't decode everything and no error + * occurred, either the input is truncated or the output buffer is too small. + * Since we know that the last input byte never produces any output, we know + * that if all the input was consumed and decoding wasn't finished, the file + * must be corrupt. Otherwise the output buffer has to be too small or the + * file is corrupt in a way that decoding it produces too big output. + * + * If single-call decoding fails, we reset b->in_pos and b->out_pos back to + * their original values. This is because with some filter chains there won't + * be any valid uncompressed data in the output buffer unless the decoding + * actually succeeds (that's the price to pay of using the output buffer as + * the workspace). + */ +XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b) +{ + size_t in_start; + size_t out_start; + enum xz_ret ret; + + if (DEC_IS_SINGLE(s->mode)) + xz_dec_reset(s); + + in_start = b->in_pos; + out_start = b->out_pos; + ret = dec_main(s, b); + + if (DEC_IS_SINGLE(s->mode)) { + if (ret == XZ_OK) + ret = b->in_pos == b->in_size + ? XZ_DATA_ERROR : XZ_BUF_ERROR; + + if (ret != XZ_STREAM_END) { + b->in_pos = in_start; + b->out_pos = out_start; + } + + } else if (ret == XZ_OK && in_start == b->in_pos + && out_start == b->out_pos) { + if (s->allow_buf_error) + ret = XZ_BUF_ERROR; + + s->allow_buf_error = true; + } else { + s->allow_buf_error = false; + } + + return ret; +} + +#ifdef XZ_DEC_CONCATENATED +XZ_EXTERN enum xz_ret xz_dec_catrun(struct xz_dec *s, struct xz_buf *b, + int finish) +{ + enum xz_ret ret; + + if (DEC_IS_SINGLE(s->mode)) { + xz_dec_reset(s); + finish = true; + } + + while (true) { + if (s->sequence == SEQ_STREAM_PADDING) { + /* + * Skip Stream Padding. Its size must be a multiple + * of four bytes which is tracked with s->pos. + */ + while (true) { + if (b->in_pos == b->in_size) { + /* + * Note that if we are repeatedly + * given no input and finish is false, + * we will keep returning XZ_OK even + * though no progress is being made. + * The lack of XZ_BUF_ERROR support + * isn't a problem here because a + * reasonable caller will eventually + * provide more input or set finish + * to true. + */ + if (!finish) + return XZ_OK; + + if (s->pos != 0) + return XZ_DATA_ERROR; + + return XZ_STREAM_END; + } + + if (b->in[b->in_pos] != 0x00) { + if (s->pos != 0) + return XZ_DATA_ERROR; + + break; + } + + ++b->in_pos; + s->pos = (s->pos + 1) & 3; + } + + /* + * More input remains. It should be a new Stream. + * + * In single-call mode xz_dec_run() will always call + * xz_dec_reset(). Thus, we need to do it here only + * in multi-call mode. + */ + if (DEC_IS_MULTI(s->mode)) + xz_dec_reset(s); + } + + ret = xz_dec_run(s, b); + + if (ret != XZ_STREAM_END) + break; + + s->sequence = SEQ_STREAM_PADDING; + } + + return ret; +} +#endif + +XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max) +{ + struct xz_dec *s = kmalloc(sizeof(*s), GFP_KERNEL); + if (s == NULL) + return NULL; + + s->mode = mode; + +#ifdef XZ_DEC_BCJ + s->bcj = xz_dec_bcj_create(DEC_IS_SINGLE(mode)); + if (s->bcj == NULL) + goto error_bcj; +#endif + + s->lzma2 = xz_dec_lzma2_create(mode, dict_max); + if (s->lzma2 == NULL) + goto error_lzma2; + + xz_dec_reset(s); + return s; + +error_lzma2: +#ifdef XZ_DEC_BCJ + xz_dec_bcj_end(s->bcj); +error_bcj: +#endif + kfree(s); + return NULL; +} + +XZ_EXTERN void xz_dec_reset(struct xz_dec *s) +{ + s->sequence = SEQ_STREAM_HEADER; + s->allow_buf_error = false; + s->pos = 0; + s->crc = 0; + memzero(&s->block, sizeof(s->block)); + memzero(&s->index, sizeof(s->index)); + s->temp.pos = 0; + s->temp.size = STREAM_HEADER_SIZE; +} + +XZ_EXTERN void xz_dec_end(struct xz_dec *s) +{ + if (s != NULL) { + xz_dec_lzma2_end(s->lzma2); +#ifdef XZ_DEC_BCJ + xz_dec_bcj_end(s->bcj); +#endif + kfree(s); + } +} diff --git a/android/app/src/main/cpp/xz/xz_lzma2.h b/android/app/src/main/cpp/xz/xz_lzma2.h new file mode 100644 index 000000000..d2632b7df --- /dev/null +++ b/android/app/src/main/cpp/xz/xz_lzma2.h @@ -0,0 +1,203 @@ +/* SPDX-License-Identifier: 0BSD */ + +/* + * LZMA2 definitions + * + * Authors: Lasse Collin + * Igor Pavlov + */ + +#ifndef XZ_LZMA2_H +#define XZ_LZMA2_H + +/* Range coder constants */ +#define RC_SHIFT_BITS 8 +#define RC_TOP_BITS 24 +#define RC_TOP_VALUE (1 << RC_TOP_BITS) +#define RC_BIT_MODEL_TOTAL_BITS 11 +#define RC_BIT_MODEL_TOTAL (1 << RC_BIT_MODEL_TOTAL_BITS) +#define RC_MOVE_BITS 5 + +/* + * Maximum number of position states. A position state is the lowest pb + * number of bits of the current uncompressed offset. In some places there + * are different sets of probabilities for different position states. + */ +#define POS_STATES_MAX (1 << 4) + +/* + * This enum is used to track which LZMA symbols have occurred most recently + * and in which order. This information is used to predict the next symbol. + * + * Symbols: + * - Literal: One 8-bit byte + * - Match: Repeat a chunk of data at some distance + * - Long repeat: Multi-byte match at a recently seen distance + * - Short repeat: One-byte repeat at a recently seen distance + * + * The symbol names are in from STATE_oldest_older_previous. REP means + * either short or long repeated match, and NONLIT means any non-literal. + */ +enum lzma_state { + STATE_LIT_LIT, + STATE_MATCH_LIT_LIT, + STATE_REP_LIT_LIT, + STATE_SHORTREP_LIT_LIT, + STATE_MATCH_LIT, + STATE_REP_LIT, + STATE_SHORTREP_LIT, + STATE_LIT_MATCH, + STATE_LIT_LONGREP, + STATE_LIT_SHORTREP, + STATE_NONLIT_MATCH, + STATE_NONLIT_REP +}; + +/* Total number of states */ +#define STATES 12 + +/* The lowest 7 states indicate that the previous state was a literal. */ +#define LIT_STATES 7 + +/* Indicate that the latest symbol was a literal. */ +static inline void lzma_state_literal(enum lzma_state *state) +{ + if (*state <= STATE_SHORTREP_LIT_LIT) + *state = STATE_LIT_LIT; + else if (*state <= STATE_LIT_SHORTREP) + *state -= 3; + else + *state -= 6; +} + +/* Indicate that the latest symbol was a match. */ +static inline void lzma_state_match(enum lzma_state *state) +{ + *state = *state < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH; +} + +/* Indicate that the latest state was a long repeated match. */ +static inline void lzma_state_long_rep(enum lzma_state *state) +{ + *state = *state < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP; +} + +/* Indicate that the latest symbol was a short match. */ +static inline void lzma_state_short_rep(enum lzma_state *state) +{ + *state = *state < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP; +} + +/* Test if the previous symbol was a literal. */ +static inline bool lzma_state_is_literal(enum lzma_state state) +{ + return state < LIT_STATES; +} + +/* Each literal coder is divided in three sections: + * - 0x001-0x0FF: Without match byte + * - 0x101-0x1FF: With match byte; match bit is 0 + * - 0x201-0x2FF: With match byte; match bit is 1 + * + * Match byte is used when the previous LZMA symbol was something else than + * a literal (that is, it was some kind of match). + */ +#define LITERAL_CODER_SIZE 0x300 + +/* Maximum number of literal coders */ +#define LITERAL_CODERS_MAX (1 << 4) + +/* Minimum length of a match is two bytes. */ +#define MATCH_LEN_MIN 2 + +/* Match length is encoded with 4, 5, or 10 bits. + * + * Length Bits + * 2-9 4 = Choice=0 + 3 bits + * 10-17 5 = Choice=1 + Choice2=0 + 3 bits + * 18-273 10 = Choice=1 + Choice2=1 + 8 bits + */ +#define LEN_LOW_BITS 3 +#define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS) +#define LEN_MID_BITS 3 +#define LEN_MID_SYMBOLS (1 << LEN_MID_BITS) +#define LEN_HIGH_BITS 8 +#define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS) +#define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS) + +/* + * Maximum length of a match is 273 which is a result of the encoding + * described above. + */ +#define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1) + +/* + * Different sets of probabilities are used for match distances that have + * very short match length: Lengths of 2, 3, and 4 bytes have a separate + * set of probabilities for each length. The matches with longer length + * use a shared set of probabilities. + */ +#define DIST_STATES 4 + +/* + * Get the index of the appropriate probability array for decoding + * the distance slot. + */ +static inline uint32_t lzma_get_dist_state(uint32_t len) +{ + return len < DIST_STATES + MATCH_LEN_MIN + ? len - MATCH_LEN_MIN : DIST_STATES - 1; +} + +/* + * The highest two bits of a 32-bit match distance are encoded using six bits. + * This six-bit value is called a distance slot. This way encoding a 32-bit + * value takes 6-36 bits, larger values taking more bits. + */ +#define DIST_SLOT_BITS 6 +#define DIST_SLOTS (1 << DIST_SLOT_BITS) + +/* Match distances up to 127 are fully encoded using probabilities. Since + * the highest two bits (distance slot) are always encoded using six bits, + * the distances 0-3 don't need any additional bits to encode, since the + * distance slot itself is the same as the actual distance. DIST_MODEL_START + * indicates the first distance slot where at least one additional bit is + * needed. + */ +#define DIST_MODEL_START 4 + +/* + * Match distances greater than 127 are encoded in three pieces: + * - distance slot: the highest two bits + * - direct bits: 2-26 bits below the highest two bits + * - alignment bits: four lowest bits + * + * Direct bits don't use any probabilities. + * + * The distance slot value of 14 is for distances 128-191. + */ +#define DIST_MODEL_END 14 + +/* Distance slots that indicate a distance <= 127. */ +#define FULL_DISTANCES_BITS (DIST_MODEL_END / 2) +#define FULL_DISTANCES (1 << FULL_DISTANCES_BITS) + +/* + * For match distances greater than 127, only the highest two bits and the + * lowest four bits (alignment) is encoded using probabilities. + */ +#define ALIGN_BITS 4 +#define ALIGN_SIZE (1 << ALIGN_BITS) +#define ALIGN_MASK (ALIGN_SIZE - 1) + +/* Total number of all probability variables */ +#define PROBS_TOTAL (1846 + LITERAL_CODERS_MAX * LITERAL_CODER_SIZE) + +/* + * LZMA remembers the four most recent match distances. Reusing these + * distances tends to take less space than re-encoding the actual + * distance value. + */ +#define REPS 4 + +#endif diff --git a/android/app/src/main/cpp/xz/xz_private.h b/android/app/src/main/cpp/xz/xz_private.h new file mode 100644 index 000000000..7387401da --- /dev/null +++ b/android/app/src/main/cpp/xz/xz_private.h @@ -0,0 +1,189 @@ +/* SPDX-License-Identifier: 0BSD */ + +/* + * Private includes and definitions + * + * Author: Lasse Collin + */ + +#ifndef XZ_PRIVATE_H +#define XZ_PRIVATE_H + +#ifdef __KERNEL__ +# include +# include +# include + /* XZ_PREBOOT may be defined only via decompress_unxz.c. */ +# ifndef XZ_PREBOOT +# include +# include +# include +# ifdef CONFIG_XZ_DEC_X86 +# define XZ_DEC_X86 +# endif +# ifdef CONFIG_XZ_DEC_POWERPC +# define XZ_DEC_POWERPC +# endif +# ifdef CONFIG_XZ_DEC_IA64 +# define XZ_DEC_IA64 +# endif +# ifdef CONFIG_XZ_DEC_ARM +# define XZ_DEC_ARM +# endif +# ifdef CONFIG_XZ_DEC_ARMTHUMB +# define XZ_DEC_ARMTHUMB +# endif +# ifdef CONFIG_XZ_DEC_SPARC +# define XZ_DEC_SPARC +# endif +# ifdef CONFIG_XZ_DEC_ARM64 +# define XZ_DEC_ARM64 +# endif +# ifdef CONFIG_XZ_DEC_RISCV +# define XZ_DEC_RISCV +# endif +# ifdef CONFIG_XZ_DEC_MICROLZMA +# define XZ_DEC_MICROLZMA +# endif +# define memeq(a, b, size) (memcmp(a, b, size) == 0) +# define memzero(buf, size) memset(buf, 0, size) +# endif +# define get_le32(p) le32_to_cpup((const uint32_t *)(p)) +#else + /* + * For userspace builds, use a separate header to define the required + * macros and functions. This makes it easier to adapt the code into + * different environments and avoids clutter in the Linux kernel tree. + */ +# include "xz_config.h" +#endif + +/* If no specific decoding mode is requested, enable support for all modes. */ +#if !defined(XZ_DEC_SINGLE) && !defined(XZ_DEC_PREALLOC) \ + && !defined(XZ_DEC_DYNALLOC) +# define XZ_DEC_SINGLE +# define XZ_DEC_PREALLOC +# define XZ_DEC_DYNALLOC +#endif + +/* + * The DEC_IS_foo(mode) macros are used in "if" statements. If only some + * of the supported modes are enabled, these macros will evaluate to true or + * false at compile time and thus allow the compiler to omit unneeded code. + */ +#ifdef XZ_DEC_SINGLE +# define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE) +#else +# define DEC_IS_SINGLE(mode) (false) +#endif + +#ifdef XZ_DEC_PREALLOC +# define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC) +#else +# define DEC_IS_PREALLOC(mode) (false) +#endif + +#ifdef XZ_DEC_DYNALLOC +# define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC) +#else +# define DEC_IS_DYNALLOC(mode) (false) +#endif + +#if !defined(XZ_DEC_SINGLE) +# define DEC_IS_MULTI(mode) (true) +#elif defined(XZ_DEC_PREALLOC) || defined(XZ_DEC_DYNALLOC) +# define DEC_IS_MULTI(mode) ((mode) != XZ_SINGLE) +#else +# define DEC_IS_MULTI(mode) (false) +#endif + +/* + * If any of the BCJ filter decoders are wanted, define XZ_DEC_BCJ. + * XZ_DEC_BCJ is used to enable generic support for BCJ decoders. + */ +#ifndef XZ_DEC_BCJ +# if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \ + || defined(XZ_DEC_IA64) \ + || defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \ + || defined(XZ_DEC_SPARC) || defined(XZ_DEC_ARM64) \ + || defined(XZ_DEC_RISCV) +# define XZ_DEC_BCJ +# endif +#endif + +struct xz_sha256 { + /* Buffered input data */ + uint8_t data[64]; + + /* Internal state and the final hash value */ + uint32_t state[8]; + + /* Size of the input data */ + uint64_t size; +}; + +/* Reset the SHA-256 state to prepare for a new calculation. */ +XZ_EXTERN void xz_sha256_reset(struct xz_sha256 *s); + +/* Update the SHA-256 state with new data. */ +XZ_EXTERN void xz_sha256_update(const uint8_t *buf, size_t size, + struct xz_sha256 *s); + +/* + * Finish the SHA-256 calculation. Compare the result with the first 32 bytes + * from buf. Return true if the values are equal and false if they aren't. + */ +XZ_EXTERN bool xz_sha256_validate(const uint8_t *buf, struct xz_sha256 *s); + +/* + * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used + * before calling xz_dec_lzma2_run(). + */ +XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode, + uint32_t dict_max); + +/* + * Decode the LZMA2 properties (one byte) and reset the decoder. Return + * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not + * big enough, and XZ_OPTIONS_ERROR if props indicates something that this + * decoder doesn't support. + */ +XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, + uint8_t props); + +/* Decode raw LZMA2 stream from b->in to b->out. */ +XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, + struct xz_buf *b); + +/* Free the memory allocated for the LZMA2 decoder. */ +XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s); + +#ifdef XZ_DEC_BCJ +/* + * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before + * calling xz_dec_bcj_run(). + */ +XZ_EXTERN struct xz_dec_bcj *xz_dec_bcj_create(bool single_call); + +/* + * Decode the Filter ID of a BCJ filter. This implementation doesn't + * support custom start offsets, so no decoding of Filter Properties + * is needed. Returns XZ_OK if the given Filter ID is supported. + * Otherwise XZ_OPTIONS_ERROR is returned. + */ +XZ_EXTERN enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id); + +/* + * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is + * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run() + * must be called directly. + */ +XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s, + struct xz_dec_lzma2 *lzma2, + struct xz_buf *b); + +/* Free the memory allocated for the BCJ filters. */ +#define xz_dec_bcj_end(s) kfree(s) +#endif + +#endif diff --git a/android/app/src/main/cpp/xz/xz_sha256.c b/android/app/src/main/cpp/xz/xz_sha256.c new file mode 100644 index 000000000..078cad2c8 --- /dev/null +++ b/android/app/src/main/cpp/xz/xz_sha256.c @@ -0,0 +1,182 @@ +// SPDX-License-Identifier: 0BSD + +/* + * SHA-256 + * + * This is based on the XZ Utils version which is based public domain code + * from Crypto++ Library 5.5.1 released in 2007: https://www.cryptopp.com/ + * + * Authors: Wei Dai + * Lasse Collin + */ + +#include "xz_private.h" + +static inline uint32_t +rotr_32(uint32_t num, unsigned amount) +{ + return (num >> amount) | (num << (32 - amount)); +} + +#define blk0(i) (W[i] = get_be32(&data[4 * i])) +#define blk2(i) (W[i & 15] += s1(W[(i - 2) & 15]) + W[(i - 7) & 15] \ + + s0(W[(i - 15) & 15])) + +#define Ch(x, y, z) (z ^ (x & (y ^ z))) +#define Maj(x, y, z) ((x & (y ^ z)) + (y & z)) + +#define a(i) T[(0 - i) & 7] +#define b(i) T[(1 - i) & 7] +#define c(i) T[(2 - i) & 7] +#define d(i) T[(3 - i) & 7] +#define e(i) T[(4 - i) & 7] +#define f(i) T[(5 - i) & 7] +#define g(i) T[(6 - i) & 7] +#define h(i) T[(7 - i) & 7] + +#define R(i, j, blk) \ + h(i) += S1(e(i)) + Ch(e(i), f(i), g(i)) + SHA256_K[i + j] + blk; \ + d(i) += h(i); \ + h(i) += S0(a(i)) + Maj(a(i), b(i), c(i)) +#define R0(i) R(i, 0, blk0(i)) +#define R2(i) R(i, j, blk2(i)) + +#define S0(x) rotr_32(x ^ rotr_32(x ^ rotr_32(x, 9), 11), 2) +#define S1(x) rotr_32(x ^ rotr_32(x ^ rotr_32(x, 14), 5), 6) +#define s0(x) (rotr_32(x ^ rotr_32(x, 11), 7) ^ (x >> 3)) +#define s1(x) (rotr_32(x ^ rotr_32(x, 2), 17) ^ (x >> 10)) + +static const uint32_t SHA256_K[64] = { + 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, + 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, + 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, + 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, + 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, + 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, + 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, + 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, + 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, + 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, + 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, + 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, + 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, + 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, + 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, + 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2 +}; + +static void +transform(uint32_t state[8], const uint8_t data[64]) +{ + uint32_t W[16]; + uint32_t T[8]; + unsigned int j; + + /* Copy state[] to working vars. */ + memcpy(T, state, sizeof(T)); + + /* The first 16 operations unrolled */ + R0( 0); R0( 1); R0( 2); R0( 3); + R0( 4); R0( 5); R0( 6); R0( 7); + R0( 8); R0( 9); R0(10); R0(11); + R0(12); R0(13); R0(14); R0(15); + + /* The remaining 48 operations partially unrolled */ + for (j = 16; j < 64; j += 16) { + R2( 0); R2( 1); R2( 2); R2( 3); + R2( 4); R2( 5); R2( 6); R2( 7); + R2( 8); R2( 9); R2(10); R2(11); + R2(12); R2(13); R2(14); R2(15); + } + + /* Add the working vars back into state[]. */ + state[0] += a(0); + state[1] += b(0); + state[2] += c(0); + state[3] += d(0); + state[4] += e(0); + state[5] += f(0); + state[6] += g(0); + state[7] += h(0); +} + +XZ_EXTERN void xz_sha256_reset(struct xz_sha256 *s) +{ + static const uint32_t initial_state[8] = { + 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, + 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 + }; + + memcpy(s->state, initial_state, sizeof(initial_state)); + s->size = 0; +} + +XZ_EXTERN void xz_sha256_update(const uint8_t *buf, size_t size, + struct xz_sha256 *s) +{ + size_t copy_start; + size_t copy_size; + + /* + * Copy the input data into a properly aligned temporary buffer. + * This way we can be called with arbitrarily sized buffers + * (no need to be a multiple of 64 bytes). + * + * Full 64-byte chunks could be processed directly from buf with + * unaligned access. It seemed to make very little difference in + * speed on x86-64 though. Thus it was omitted. + */ + while (size > 0) { + copy_start = s->size & 0x3F; + copy_size = 64 - copy_start; + if (copy_size > size) + copy_size = size; + + memcpy(s->data + copy_start, buf, copy_size); + + buf += copy_size; + size -= copy_size; + s->size += copy_size; + + if ((s->size & 0x3F) == 0) + transform(s->state, s->data); + } +} + +XZ_EXTERN bool xz_sha256_validate(const uint8_t *buf, struct xz_sha256 *s) +{ + /* + * Add padding as described in RFC 3174 (it describes SHA-1 but + * the same padding style is used for SHA-256 too). + */ + size_t i = s->size & 0x3F; + s->data[i++] = 0x80; + + while (i != 64 - 8) { + if (i == 64) { + transform(s->state, s->data); + i = 0; + } + + s->data[i++] = 0x00; + } + + /* Convert the message size from bytes to bits. */ + s->size *= 8; + + /* + * Store the message size in big endian byte order and + * calculate the final hash value. + */ + for (i = 0; i < 8; ++i) + s->data[64 - 8 + i] = (uint8_t)(s->size >> ((7 - i) * 8)); + + transform(s->state, s->data); + + /* Compare if the hash value matches the first 32 bytes in buf. */ + for (i = 0; i < 8; ++i) + if (get_unaligned_be32(buf + 4 * i) != s->state[i]) + return false; + + return true; +} diff --git a/android/app/src/main/cpp/xz/xz_stream.h b/android/app/src/main/cpp/xz/xz_stream.h new file mode 100644 index 000000000..55f9f6f94 --- /dev/null +++ b/android/app/src/main/cpp/xz/xz_stream.h @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: 0BSD */ + +/* + * Definitions for handling the .xz file format + * + * Author: Lasse Collin + */ + +#ifndef XZ_STREAM_H +#define XZ_STREAM_H + +#if defined(__KERNEL__) && !XZ_INTERNAL_CRC32 +# include +# undef crc32 +# define xz_crc32(buf, size, crc) \ + (~crc32_le(~(uint32_t)(crc), buf, size)) +#endif + +/* + * See the .xz file format specification at + * https://tukaani.org/xz/xz-file-format.txt + * to understand the container format. + */ + +#define STREAM_HEADER_SIZE 12 + +#define HEADER_MAGIC "\3757zXZ" +#define HEADER_MAGIC_SIZE 6 + +#define FOOTER_MAGIC "YZ" +#define FOOTER_MAGIC_SIZE 2 + +/* + * Variable-length integer can hold a 63-bit unsigned integer or a special + * value indicating that the value is unknown. + * + * Experimental: vli_type can be defined to uint32_t to save a few bytes + * in code size (no effect on speed). Doing so limits the uncompressed and + * compressed size of the file to less than 256 MiB and may also weaken + * error detection slightly. + */ +typedef uint64_t vli_type; + +#define VLI_MAX ((vli_type)-1 / 2) +#define VLI_UNKNOWN ((vli_type)-1) + +/* Maximum encoded size of a VLI */ +#define VLI_BYTES_MAX (sizeof(vli_type) * 8 / 7) + +/* Integrity Check types */ +enum xz_check { + XZ_CHECK_NONE = 0, + XZ_CHECK_CRC32 = 1, + XZ_CHECK_CRC64 = 4, + XZ_CHECK_SHA256 = 10 +}; + +/* Maximum possible Check ID */ +#define XZ_CHECK_MAX 15 + +#endif diff --git a/android/app/src/main/java/me/kavishdevar/librepods/LibrePodsApplication.kt b/android/app/src/main/java/me/kavishdevar/librepods/LibrePodsApplication.kt new file mode 100644 index 000000000..d6968038c --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/LibrePodsApplication.kt @@ -0,0 +1,41 @@ +package me.kavishdevar.librepods + +import android.app.Application +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.ProcessLifecycleOwner +import io.github.libxposed.service.XposedService +import io.github.libxposed.service.XposedServiceHelper +import me.kavishdevar.librepods.billing.BillingManager +import me.kavishdevar.librepods.billing.BillingProviderFactory +import me.kavishdevar.librepods.utils.XposedServiceHolder +import me.kavishdevar.librepods.utils.XposedState + +class LibrePodsApplication: Application(), XposedServiceHelper.OnServiceListener, DefaultLifecycleObserver { + + override fun onCreate() { + XposedServiceHelper.registerListener(this) + BillingManager.provider = BillingProviderFactory.create(this) + ProcessLifecycleOwner.get().lifecycle.addObserver(this) + + super.onCreate() + + } + + override fun onResume(owner: LifecycleOwner) { + BillingManager.provider.queryPurchases() + XposedState.isAvailable = XposedServiceHolder.service != null + XposedState.bluetoothScopeEnabled = XposedServiceHolder.service?.scope?.contains("com.google.android.bluetooth") == true || XposedServiceHolder.service?.scope?.contains("com.android.bluetooth") == true + } + + override fun onServiceBind(service: XposedService) { + XposedServiceHolder.service = service + XposedState.isAvailable = true + XposedState.bluetoothScopeEnabled = XposedServiceHolder.service?.scope?.contains("com.google.android.bluetooth") == true || XposedServiceHolder.service?.scope?.contains("com.android.bluetooth") == true + } + + override fun onServiceDied(p0: XposedService) { + XposedServiceHolder.service = null + XposedState.isAvailable = false + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/MainActivity.kt b/android/app/src/main/java/me/kavishdevar/librepods/MainActivity.kt index 3e589b6a6..f9924eaec 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/MainActivity.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/MainActivity.kt @@ -1,39 +1,41 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) package me.kavishdevar.librepods +// import me.kavishdevar.librepods.screens.Onboarding +// import me.kavishdevar.librepods.utils.RadareOffsetFinder +//import dagger.hilt.android.AndroidEntryPoint import android.annotation.SuppressLint +import android.app.Activity import android.content.BroadcastReceiver import android.content.ComponentName import android.content.Context import android.content.Context.MODE_PRIVATE import android.content.Intent import android.content.ServiceConnection -import android.net.Uri import android.os.Build import android.os.Bundle import android.os.IBinder import android.provider.Settings import android.util.Log -import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge @@ -52,6 +54,7 @@ import androidx.compose.animation.slideOutHorizontally import androidx.compose.foundation.Canvas import androidx.compose.foundation.background import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row @@ -73,7 +76,6 @@ import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon -import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -88,7 +90,6 @@ import androidx.compose.ui.draw.scale import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.drawscope.rotate -import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalWindowInfo @@ -103,6 +104,7 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.core.content.edit import androidx.core.net.toUri +import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController @@ -110,61 +112,71 @@ import com.google.accompanist.permissions.ExperimentalPermissionsApi import com.google.accompanist.permissions.MultiplePermissionsState import com.google.accompanist.permissions.isGranted import com.google.accompanist.permissions.rememberMultiplePermissionsState +import com.google.android.play.core.review.ReviewManagerFactory import com.kyant.backdrop.backdrops.layerBackdrop import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import dev.chrisbanes.haze.hazeSource import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.constants.AirPodsNotifications -import me.kavishdevar.librepods.screens.AccessibilitySettingsScreen -import me.kavishdevar.librepods.screens.AdaptiveStrengthScreen -import me.kavishdevar.librepods.screens.AirPodsSettingsScreen -import me.kavishdevar.librepods.screens.AppSettingsScreen -import me.kavishdevar.librepods.screens.CameraControlScreen -import me.kavishdevar.librepods.screens.DebugScreen -import me.kavishdevar.librepods.screens.HeadTrackingScreen -import me.kavishdevar.librepods.screens.HearingAidAdjustmentsScreen -import me.kavishdevar.librepods.screens.HearingAidScreen -import me.kavishdevar.librepods.screens.HearingProtectionScreen -import me.kavishdevar.librepods.screens.LongPress -import me.kavishdevar.librepods.screens.Onboarding -import me.kavishdevar.librepods.screens.OpenSourceLicensesScreen -import me.kavishdevar.librepods.screens.RenameScreen -import me.kavishdevar.librepods.screens.TransparencySettingsScreen -import me.kavishdevar.librepods.screens.TroubleshootingScreen -import me.kavishdevar.librepods.screens.UpdateHearingTestScreen -import me.kavishdevar.librepods.screens.VersionScreen +import dev.chrisbanes.haze.rememberHazeState +import me.kavishdevar.librepods.bluetooth.BluetoothConnectionManager +import me.kavishdevar.librepods.data.AirPodsNotifications +import me.kavishdevar.librepods.data.ControlCommandRepository +import me.kavishdevar.librepods.presentation.components.AppInfoCard +import me.kavishdevar.librepods.presentation.components.DeviceInfoCard +import me.kavishdevar.librepods.presentation.components.StyledIconButton +import me.kavishdevar.librepods.presentation.screens.AccessibilitySettingsScreen +import me.kavishdevar.librepods.presentation.screens.AdaptiveStrengthScreen +import me.kavishdevar.librepods.presentation.screens.AirPodsSettingsScreen +import me.kavishdevar.librepods.presentation.screens.AppSettingsScreen +import me.kavishdevar.librepods.presentation.screens.CameraControlScreen +import me.kavishdevar.librepods.presentation.screens.DebugScreen +import me.kavishdevar.librepods.presentation.screens.EqualizerScreen +import me.kavishdevar.librepods.presentation.screens.HeadTrackingScreen +import me.kavishdevar.librepods.presentation.screens.HearingAidAdjustmentsScreen +import me.kavishdevar.librepods.presentation.screens.HearingAidScreen +import me.kavishdevar.librepods.presentation.screens.HearingProtectionScreen +import me.kavishdevar.librepods.presentation.screens.LongPress +import me.kavishdevar.librepods.presentation.screens.OpenSourceLicensesScreen +import me.kavishdevar.librepods.presentation.screens.PurchaseScreen +import me.kavishdevar.librepods.presentation.screens.RenameScreen +import me.kavishdevar.librepods.presentation.screens.TransparencySettingsScreen +import me.kavishdevar.librepods.presentation.screens.TroubleshootingScreen +import me.kavishdevar.librepods.presentation.screens.UpdateHearingTestScreen +import me.kavishdevar.librepods.presentation.screens.VersionScreen +import me.kavishdevar.librepods.presentation.theme.LibrePodsTheme +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel +import me.kavishdevar.librepods.presentation.viewmodel.AppSettingsViewModel +import me.kavishdevar.librepods.presentation.viewmodel.PurchaseViewModel import me.kavishdevar.librepods.services.AirPodsService -import me.kavishdevar.librepods.ui.theme.LibrePodsTheme -import me.kavishdevar.librepods.utils.RadareOffsetFinder -import kotlin.io.encoding.Base64 +import me.kavishdevar.librepods.utils.XposedState +import me.kavishdevar.librepods.utils.isSupported import kotlin.io.encoding.ExperimentalEncodingApi lateinit var serviceConnection: ServiceConnection lateinit var connectionStatusReceiver: BroadcastReceiver +lateinit var testReviewReceiver: BroadcastReceiver +//@AndroidEntryPoint @ExperimentalMaterial3Api class MainActivity : ComponentActivity() { companion object { init { - System.loadLibrary("l2c_fcr_hook") + if (XposedState.isAvailable && XposedState.bluetoothScopeEnabled) { + System.loadLibrary("l2c_fcr_hook") + } } } + @ExperimentalHazeMaterialsApi override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { LibrePodsTheme { - getSharedPreferences("settings", MODE_PRIVATE).edit { - putLong( - "textColor", - MaterialTheme.colorScheme.onSurface.toArgb().toLong())} Main() } } - - handleIncomingIntent(intent) } override fun onDestroy() { @@ -199,82 +211,108 @@ class MainActivity : ComponentActivity() { } super.onStop() } +} - override fun onNewIntent(intent: Intent) { - super.onNewIntent(intent) - setIntent(intent) - handleIncomingIntent(intent) - } +@ExperimentalHazeMaterialsApi +@SuppressLint("MissingPermission", "InlinedApi", "UnspecifiedRegisterReceiverFlag") +@OptIn(ExperimentalPermissionsApi::class, ExperimentalMaterial3Api::class) +@Composable +fun Main() { + val context = LocalContext.current + val sharedPreferences = context.getSharedPreferences("settings", MODE_PRIVATE) + if (!isSupported(sharedPreferences) && !XposedState.bluetoothScopeEnabled) { + val hazeState = rememberHazeState() + val backdrop = rememberLayerBackdrop() + val isDarkTheme = isSystemInDarkTheme() + val textColor = if (isDarkTheme) Color.White else Color.Black + val backgroundColor = if (isSystemInDarkTheme()) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - private fun handleIncomingIntent(intent: Intent) { - val data: Uri? = intent.data + val scrollState = rememberScrollState() - if (data != null && data.scheme == "librepods") { - when (data.host) { - "add-magic-keys" -> { - val queryParams = data.queryParameterNames - queryParams.forEach { param -> - val value = data.getQueryParameter(param) - Log.d("LibrePods", "Parameter: $param = $value") - } + Box( + modifier = Modifier + .fillMaxSize() + .hazeSource(hazeState) + .layerBackdrop(backdrop) + .background(if (isDarkTheme) Color.Black else Color(0xFFF2F2F7)), + contentAlignment = Alignment.Center + ) { + Column( + modifier = Modifier + .padding(horizontal = 16.dp) + .verticalScroll(scrollState), + verticalArrangement = Arrangement + .spacedBy(16.dp) + ) { + Spacer(modifier = Modifier.height(48.dp)) + Column( + modifier = Modifier, + verticalArrangement = Arrangement + .spacedBy(16.dp) + ) { + Spacer(modifier = Modifier.height(16.dp)) + Text( + text = stringResource(R.string.not_supported), + style = TextStyle( + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.SemiBold, + color = textColor, + fontSize = 28.sp, + textAlign = TextAlign.Center + ), + modifier = Modifier.fillMaxWidth() + ) - handleAddMagicKeys(data) + Box( + modifier = Modifier + .fillMaxWidth() + .background(backgroundColor, RoundedCornerShape(28.dp)) + .clip(RoundedCornerShape(28.dp)) + ) { + Text( + text = stringResource(R.string.check_the_repository_for_more_info), + style = TextStyle( + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Medium, + color = if (isDarkTheme) Color.White else Color.Black, + fontSize = 16.sp + ), + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 16.dp) + ) + } + Spacer(modifier = Modifier.height(4.dp)) + Text( + text = stringResource(R.string.enable_app_in_xposed_or_update_device), + style = TextStyle( + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Light, + color = if (isDarkTheme) Color.White else Color.Black, + fontSize = 14.sp + ), + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 16.dp, vertical = 16.dp) + ) + DeviceInfoCard() + AppInfoCard() } + Spacer(modifier = Modifier.height(48.dp)) } } + return } - private fun handleAddMagicKeys(uri: Uri) { - val sharedPreferences = getSharedPreferences("settings", MODE_PRIVATE) - - val irkHex = uri.getQueryParameter("irk") - val encKeyHex = uri.getQueryParameter("enc_key") - - try { - if (irkHex != null && validateHexInput(irkHex)) { - val irkBytes = hexStringToByteArray(irkHex) - val irkBase64 = Base64.encode(irkBytes) - sharedPreferences.edit {putString("IRK", irkBase64)} - } - - if (encKeyHex != null && validateHexInput(encKeyHex)) { - val encKeyBytes = hexStringToByteArray(encKeyHex) - val encKeyBase64 = Base64.encode(encKeyBytes) - sharedPreferences.edit { putString("ENC_KEY", encKeyBase64)} - } - - Toast.makeText(this, "Magic keys added successfully!", Toast.LENGTH_SHORT).show() - } catch (e: Exception) { - Toast.makeText(this, "Error processing magic keys: ${e.message}", Toast.LENGTH_LONG).show() - } - } - - private fun validateHexInput(input: String): Boolean { - val hexPattern = Regex("^[0-9a-fA-F]{32}$") - return hexPattern.matches(input) - } - - private fun hexStringToByteArray(hex: String): ByteArray { - val result = ByteArray(16) - for (i in 0 until 16) { - val hexByte = hex.substring(i * 2, i * 2 + 2) - result[i] = hexByte.toInt(16).toByte() - } - return result - } -} - -@ExperimentalHazeMaterialsApi -@SuppressLint("MissingPermission", "InlinedApi", "UnspecifiedRegisterReceiverFlag") -@OptIn(ExperimentalPermissionsApi::class) -@Composable -fun Main() { val isConnected = remember { mutableStateOf(false) } - val isRemotelyConnected = remember { mutableStateOf(false) } - val hookAvailable = RadareOffsetFinder(LocalContext.current).isHookOffsetAvailable() - val context = LocalContext.current + var canDrawOverlays by remember { mutableStateOf(Settings.canDrawOverlays(context)) } - val overlaySkipped = remember { mutableStateOf(context.getSharedPreferences("settings", MODE_PRIVATE).getBoolean("overlay_permission_skipped", false)) } + val overlaySkipped = remember { + mutableStateOf( + context.getSharedPreferences("settings", MODE_PRIVATE) + .getBoolean("overlay_permission_skipped", false) + ) + } val bluetoothPermissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { listOf( @@ -301,23 +339,58 @@ fun Main() { val permissionState = rememberMultiplePermissionsState( permissions = allPermissions ) + val airPodsService = remember { mutableStateOf(null) } + val airPodsViewModel = remember(airPodsService.value) { + airPodsService.value?.let { service -> + AirPodsViewModel( + service = service, + sharedPreferences = context.getSharedPreferences("settings", MODE_PRIVATE), + controlRepo = ControlCommandRepository(service.aacpManager), + appContext = context.applicationContext + ) + } + } + LaunchedEffect(Unit) { canDrawOverlays = Settings.canDrawOverlays(context) } if (permissionState.allPermissionsGranted && (canDrawOverlays || overlaySkipped.value)) { - val context = LocalContext.current val navController = rememberNavController() - Box ( - modifier = Modifier - .fillMaxSize() - ){ + LaunchedEffect(Unit) { + if (BuildConfig.PLAY_BUILD) { + val now = System.currentTimeMillis() + val firstConn = + sharedPreferences.getLong("first_connection_successful_time", 0L) + + val alreadyPrompted = + sharedPreferences.getBoolean("review_prompted", false) + + val oneDay = 24 * 60 * 60 * 1000L + + if ( + firstConn != 0L && + !alreadyPrompted && + (now - firstConn) > oneDay + ) { + triggerReviewFlow(context as? Activity ?: return@LaunchedEffect) + + sharedPreferences.edit { + putBoolean("review_prompted", true) + } + } + } + } + + Box( + modifier = Modifier.fillMaxSize() + ) { val backButtonBackdrop = rememberLayerBackdrop() - Box ( + Box( modifier = Modifier .fillMaxSize() .background(if (isSystemInDarkTheme()) Color.Black else Color(0xFFF2F2F7)) @@ -325,134 +398,140 @@ fun Main() { ) { NavHost( navController = navController, - startDestination = if (hookAvailable) "settings" else "onboarding", + startDestination = "settings", enterTransition = { slideInHorizontally( - initialOffsetX = { it }, - animationSpec = tween(durationMillis = 300) - ) // + fadeIn(animationSpec = tween(durationMillis = 300)) + initialOffsetX = { it }, animationSpec = tween(durationMillis = 300) + ) }, exitTransition = { slideOutHorizontally( - targetOffsetX = { -it/4 }, - animationSpec = tween(durationMillis = 300) - ) // + fadeOut(animationSpec = tween(durationMillis = 150)) + targetOffsetX = { -it / 4 }, animationSpec = tween(durationMillis = 300) + ) }, popEnterTransition = { slideInHorizontally( - initialOffsetX = { -it/4 }, + initialOffsetX = { -it / 4 }, animationSpec = tween(durationMillis = 300) - ) // + fadeIn(animationSpec = tween(durationMillis = 300)) + ) }, popExitTransition = { slideOutHorizontally( - targetOffsetX = { it }, - animationSpec = tween(durationMillis = 300) - ) // + fadeOut(animationSpec = tween(durationMillis = 150)) - } - ) { + targetOffsetX = { it }, animationSpec = tween(durationMillis = 300) + ) + }) { composable("settings") { - if (airPodsService.value != null) { - AirPodsSettingsScreen( - dev = airPodsService.value?.device, - service = airPodsService.value!!, - navController = navController, - isConnected = isConnected.value, - isRemotelyConnected = isRemotelyConnected.value - ) - } + if (airPodsViewModel != null) AirPodsSettingsScreen(airPodsViewModel, navController) } composable("debug") { DebugScreen(navController = navController) } composable("long_press/{bud}") { navBackStackEntry -> - LongPress( - navController = navController, - name = navBackStackEntry.arguments?.getString("bud")!! + if (airPodsViewModel != null) LongPress( + viewModel = airPodsViewModel, + name = navBackStackEntry.arguments?.getString("bud")!!, + navController = navController ) } composable("rename") { - RenameScreen(navController) + if (airPodsViewModel != null) RenameScreen(airPodsViewModel) } composable("app_settings") { - AppSettingsScreen(navController) + val appSettingsViewModel: AppSettingsViewModel = viewModel() + AppSettingsScreen(navController, appSettingsViewModel) } composable("troubleshooting") { TroubleshootingScreen(navController) } composable("head_tracking") { - HeadTrackingScreen(navController) - } - composable("onboarding") { - Onboarding(navController, context) + if (airPodsViewModel != null) HeadTrackingScreen(airPodsViewModel, navController) } composable("accessibility") { - AccessibilitySettingsScreen(navController) + if (airPodsViewModel != null) AccessibilitySettingsScreen(airPodsViewModel, navController) } composable("transparency_customization") { - TransparencySettingsScreen(navController) + if (airPodsViewModel != null) TransparencySettingsScreen(airPodsViewModel) } composable("hearing_aid") { - HearingAidScreen(navController) + if (airPodsViewModel != null) HearingAidScreen(airPodsViewModel, navController) } composable("hearing_aid_adjustments") { - HearingAidAdjustmentsScreen(navController) + if (airPodsViewModel != null) HearingAidAdjustmentsScreen(airPodsViewModel) } composable("adaptive_strength") { - AdaptiveStrengthScreen(navController) + if (airPodsViewModel != null) AdaptiveStrengthScreen(airPodsViewModel, navController) } composable("camera_control") { - CameraControlScreen(navController) + if (airPodsViewModel != null) CameraControlScreen(airPodsViewModel) } composable("open_source_licenses") { OpenSourceLicensesScreen(navController) } composable("update_hearing_test") { - UpdateHearingTestScreen(navController) + if (airPodsViewModel != null) UpdateHearingTestScreen(airPodsViewModel) } composable("version_info") { - VersionScreen(navController) + if (airPodsViewModel != null) VersionScreen(airPodsViewModel) } composable("hearing_protection") { - HearingProtectionScreen(navController) + if (airPodsViewModel != null) HearingProtectionScreen(airPodsViewModel, navController) + } + composable("purchase_screen") { + val purchaseViewModel: PurchaseViewModel = viewModel() + PurchaseScreen(purchaseViewModel, navController) + } + composable("equalizer_screen") { + if (airPodsViewModel != null) EqualizerScreen(airPodsViewModel) } } } - val showBackButton = remember{ mutableStateOf(false) } + val showBackButton = remember { mutableStateOf(false) } LaunchedEffect(navController) { navController.addOnDestinationChangedListener { _, destination, _ -> - showBackButton.value = destination.route != "settings" && destination.route != "onboarding" - Log.d("MainActivity", "Navigated to ${destination.route}, showBackButton: ${showBackButton.value}") + showBackButton.value = + destination.route != "settings" // && destination.route != "onboarding" } } AnimatedVisibility( visible = showBackButton.value, - enter = fadeIn(animationSpec = tween()) + scaleIn(initialScale = 0f, animationSpec = tween()), - exit = fadeOut(animationSpec = tween()) + scaleOut(targetScale = 0.5f, animationSpec = tween(100)), + enter = fadeIn(animationSpec = tween()) + scaleIn( + initialScale = 0f, + animationSpec = tween() + ), + exit = fadeOut(animationSpec = tween()) + scaleOut( + targetScale = 0.5f, + animationSpec = tween(100) + ), modifier = Modifier .align(Alignment.TopStart) .padding( - start = 8.dp, - top = (LocalWindowInfo.current.containerSize.width * 0.05f).dp + start = 8.dp, top = (LocalWindowInfo.current.containerSize.width * 0.05f).dp ) ) { StyledIconButton( - onClick = { navController.popBackStack() }, - icon = "􀯶", - darkMode = isSystemInDarkTheme(), - backdrop = backButtonBackdrop - ) + onClick = { navController.popBackStack() }, + icon = "􀯶", + backdrop = backButtonBackdrop + ) } } + context.startForegroundService(Intent(context, AirPodsService::class.java)) + serviceConnection = remember { object : ServiceConnection { override fun onServiceConnected(name: ComponentName?, service: IBinder?) { val binder = service as AirPodsService.LocalBinder airPodsService.value = binder.getService() + + if (!sharedPreferences.contains("first_connection_successful_time")) { + sharedPreferences.edit { + putLong("first_connection_successful_time", System.currentTimeMillis()) + } + } } override fun onServiceDisconnected(name: ComponentName?) { @@ -461,17 +540,31 @@ fun Main() { } } - context.bindService(Intent(context, AirPodsService::class.java), serviceConnection, Context.BIND_AUTO_CREATE) + context.bindService( + Intent(context, AirPodsService::class.java), + serviceConnection, + Context.BIND_AUTO_CREATE + ) - if (airPodsService.value?.isConnectedLocally == true) { + if (BluetoothConnectionManager.getAACPSocket()?.isConnected == true) { isConnected.value = true } } else { PermissionsScreen( permissionState = permissionState, canDrawOverlays = canDrawOverlays, - onOverlaySettingsReturn = { canDrawOverlays = Settings.canDrawOverlays(context) } - ) + onOverlaySettingsReturn = { canDrawOverlays = Settings.canDrawOverlays(context) }) + } +} + +private fun triggerReviewFlow(activity: Activity) { + val manager = ReviewManagerFactory.create(activity) + val request = manager.requestReviewFlow() + request.addOnCompleteListener { task -> + if (task.isSuccessful) { + val reviewInfo = task.result + manager.launchReviewFlow(activity, reviewInfo) + } } } @@ -494,13 +587,9 @@ fun PermissionsScreen( val infiniteTransition = rememberInfiniteTransition(label = "pulse") val pulseScale by infiniteTransition.animateFloat( - initialValue = 1f, - targetValue = 1.05f, - animationSpec = infiniteRepeatable( - animation = tween(1000), - repeatMode = RepeatMode.Reverse - ), - label = "pulse scale" + initialValue = 1f, targetValue = 1.05f, animationSpec = infiniteRepeatable( + animation = tween(1000), repeatMode = RepeatMode.Reverse + ), label = "pulse scale" ) Column( @@ -508,18 +597,15 @@ fun PermissionsScreen( .fillMaxSize() .background(if (isDarkTheme) Color.Black else Color(0xFFF2F2F7)) .padding(16.dp) - .verticalScroll(scrollState), - horizontalAlignment = Alignment.CenterHorizontally + .verticalScroll(scrollState), horizontalAlignment = Alignment.CenterHorizontally ) { Box( modifier = Modifier .fillMaxWidth() - .height(180.dp), - contentAlignment = Alignment.Center + .height(180.dp), contentAlignment = Alignment.Center ) { Text( - text = "\uDBC2\uDEB7", - style = TextStyle( + text = "\uDBC2\uDEB7", style = TextStyle( fontSize = 48.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily(Font(R.font.sf_pro)), @@ -555,29 +641,25 @@ fun PermissionsScreen( Spacer(modifier = Modifier.height(16.dp)) Text( - text = "Permission Required", - style = TextStyle( + text = "Permission Required", style = TextStyle( fontSize = 24.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily(Font(R.font.sf_pro)), color = textColor, textAlign = TextAlign.Center - ), - modifier = Modifier.fillMaxWidth() + ), modifier = Modifier.fillMaxWidth() ) Spacer(modifier = Modifier.height(8.dp)) Text( - text = stringResource(R.string.permissions_required), - style = TextStyle( + text = stringResource(R.string.permissions_required), style = TextStyle( fontSize = 16.sp, fontWeight = FontWeight.Normal, fontFamily = FontFamily(Font(R.font.sf_pro)), color = textColor.copy(alpha = 0.7f), textAlign = TextAlign.Center - ), - modifier = Modifier.fillMaxWidth() + ), modifier = Modifier.fillMaxWidth() ) Spacer(modifier = Modifier.height(32.dp)) @@ -750,8 +832,7 @@ fun PermissionCard( if (isGranted) accentColor.copy(alpha = 0.15f) else Color.Gray.copy( alpha = 0.15f ) - ), - contentAlignment = Alignment.Center + ), contentAlignment = Alignment.Center ) { Icon( imageVector = icon, @@ -767,8 +848,7 @@ fun PermissionCard( .padding(start = 16.dp) ) { Text( - text = title, - style = TextStyle( + text = title, style = TextStyle( fontSize = 16.sp, fontWeight = FontWeight.Medium, fontFamily = FontFamily(Font(R.font.sf_pro)), @@ -777,8 +857,7 @@ fun PermissionCard( ) Text( - text = description, - style = TextStyle( + text = description, style = TextStyle( fontSize = 14.sp, fontWeight = FontWeight.Normal, fontFamily = FontFamily(Font(R.font.sf_pro)), @@ -795,11 +874,8 @@ fun PermissionCard( contentAlignment = Alignment.Center ) { Text( - text = if (isGranted) "✓" else "!", - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = Color.White + text = if (isGranted) "✓" else "!", style = TextStyle( + fontSize = 14.sp, fontWeight = FontWeight.Bold, color = Color.White ) ) } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/QuickSettingsDialogActivity.kt b/android/app/src/main/java/me/kavishdevar/librepods/QuickSettingsDialogActivity.kt index bd46412b5..2aa9158c3 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/QuickSettingsDialogActivity.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/QuickSettingsDialogActivity.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) @@ -85,15 +85,15 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import kotlinx.coroutines.launch -import me.kavishdevar.librepods.composables.AdaptiveRainbowBrush -import me.kavishdevar.librepods.composables.ControlCenterNoiseControlSegmentedButton -import me.kavishdevar.librepods.composables.IconAreaSize -import me.kavishdevar.librepods.composables.VerticalVolumeSlider -import me.kavishdevar.librepods.constants.AirPodsNotifications -import me.kavishdevar.librepods.constants.NoiseControlMode +import me.kavishdevar.librepods.presentation.components.AdaptiveRainbowBrush +import me.kavishdevar.librepods.presentation.components.ControlCenterNoiseControlSegmentedButton +import me.kavishdevar.librepods.presentation.components.IconAreaSize +import me.kavishdevar.librepods.presentation.components.VerticalVolumeSlider +import me.kavishdevar.librepods.data.AirPodsNotifications +import me.kavishdevar.librepods.data.NoiseControlMode import me.kavishdevar.librepods.services.AirPodsService -import me.kavishdevar.librepods.ui.theme.LibrePodsTheme -import me.kavishdevar.librepods.utils.AACPManager +import me.kavishdevar.librepods.presentation.theme.LibrePodsTheme +import me.kavishdevar.librepods.bluetooth.AACPManager import kotlin.io.encoding.ExperimentalEncodingApi import kotlin.math.abs diff --git a/android/app/src/main/java/me/kavishdevar/librepods/billing/BillingManager.kt b/android/app/src/main/java/me/kavishdevar/librepods/billing/BillingManager.kt new file mode 100644 index 000000000..cfea382a2 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/billing/BillingManager.kt @@ -0,0 +1,5 @@ +package me.kavishdevar.librepods.billing + +object BillingManager { + lateinit var provider: BillingProvider +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/billing/BillingProvider.kt b/android/app/src/main/java/me/kavishdevar/librepods/billing/BillingProvider.kt new file mode 100644 index 000000000..52a670512 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/billing/BillingProvider.kt @@ -0,0 +1,30 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.billing + +import android.app.Activity +import kotlinx.coroutines.flow.StateFlow + +interface BillingProvider { + val isPremium: StateFlow + val price: StateFlow + fun purchase(activity: Activity) + fun queryPurchases() + fun restorePurchases() +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/billing/BillingProviderFactory.kt b/android/app/src/main/java/me/kavishdevar/librepods/billing/BillingProviderFactory.kt new file mode 100644 index 000000000..33f9b41f2 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/billing/BillingProviderFactory.kt @@ -0,0 +1,33 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.billing + +import android.content.Context +import me.kavishdevar.librepods.BuildConfig + +object BillingProviderFactory { + + fun create(context: Context): BillingProvider { + return if (BuildConfig.PLAY_BUILD) { + PlayBillingProvider(context) + } else { + FOSSBillingProvider(context) + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/billing/FOSSBillingProvider.kt b/android/app/src/main/java/me/kavishdevar/librepods/billing/FOSSBillingProvider.kt new file mode 100644 index 000000000..4f07db669 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/billing/FOSSBillingProvider.kt @@ -0,0 +1,77 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.billing + +import android.app.Activity +import android.content.Context +import android.content.Intent +import androidx.core.content.edit +import androidx.core.net.toUri +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch +import me.kavishdevar.librepods.R + +class FOSSBillingProvider(context: Context): BillingProvider { + private val _isPremium = MutableStateFlow(false) + override val isPremium: StateFlow = _isPremium + + private val _price = MutableStateFlow(context.getString(R.string.name_your_own_price)) + override val price: StateFlow = _price + + private val sharedPreferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE) + + private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) + private var purchaseJob: Job? = null + + init { + queryPurchases() + } + + override fun purchase(activity: Activity) { + activity.startActivity( + Intent(Intent.ACTION_VIEW, "https://github.com/sponsors/kavishdevar".toUri()) + ) + + purchaseJob?.cancel() + + purchaseJob = scope.launch { + delay(5_000) + _isPremium.value = true + sharedPreferences.edit { putBoolean("foss_upgraded", true) } + } + } + + override fun queryPurchases() { + val stored = sharedPreferences.getBoolean("foss_upgraded", false) + if (stored != _isPremium.value) { + _isPremium.value = stored + } + } + + override fun restorePurchases() { + _isPremium.value = true + sharedPreferences.edit { putBoolean("foss_upgraded", true) } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/billing/PlayBillingProvider.kt b/android/app/src/main/java/me/kavishdevar/librepods/billing/PlayBillingProvider.kt new file mode 100644 index 000000000..02f17b95e --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/billing/PlayBillingProvider.kt @@ -0,0 +1,206 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.billing + +import android.app.Activity +import android.content.Context +import android.util.Log +import com.android.billingclient.api.AcknowledgePurchaseParams +import com.android.billingclient.api.BillingClient +import com.android.billingclient.api.BillingClientStateListener +import com.android.billingclient.api.BillingFlowParams +import com.android.billingclient.api.BillingResult +import com.android.billingclient.api.PendingPurchasesParams +import com.android.billingclient.api.ProductDetails +import com.android.billingclient.api.Purchase +import com.android.billingclient.api.PurchasesUpdatedListener +import com.android.billingclient.api.QueryProductDetailsParams +import com.android.billingclient.api.QueryPurchasesParams +import com.android.billingclient.api.acknowledgePurchase +import com.android.billingclient.api.queryProductDetails +import com.android.billingclient.api.queryPurchasesAsync +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch + +const val TAG = "PlayBillingProvider" + +private const val PREMIUM_PRODUCT_ID = "librepods.advanced_features.v2" + +class PlayBillingProvider( + context: Context +) : BillingProvider, PurchasesUpdatedListener { + + private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) + + private val _isPremium = MutableStateFlow(false) + override val isPremium: StateFlow = _isPremium + + private val _price = MutableStateFlow("unknown") + override val price: StateFlow = _price + + + private var productDetails: ProductDetails? = null + + private val billingClient = BillingClient.newBuilder(context) + .setListener(this) + .enablePendingPurchases( + PendingPurchasesParams.newBuilder().enableOneTimeProducts().build() + ) + .build() + + init { + connect() + } + + private fun connect() { + billingClient.startConnection(object : BillingClientStateListener { + override fun onBillingSetupFinished(result: BillingResult) { + if (result.responseCode == BillingClient.BillingResponseCode.OK) { + scope.launch { + queryProductDetails() + queryExistingPurchases() + } + } else { + Log.w(TAG, "Billing setup failed: ${result.debugMessage}") + } + } + + override fun onBillingServiceDisconnected() { + connect() + } + }) + } + + private suspend fun queryProductDetails() { + val params = QueryProductDetailsParams.newBuilder() + .setProductList( + listOf( + QueryProductDetailsParams.Product.newBuilder() + .setProductId(PREMIUM_PRODUCT_ID) + .setProductType(BillingClient.ProductType.INAPP) + .build() + ) + ).build() + + val result = billingClient.queryProductDetails(params) + if (result.billingResult.responseCode == BillingClient.BillingResponseCode.OK) { + productDetails = result.productDetailsList?.firstOrNull() + Log.d(TAG, "Product loaded: ${productDetails?.name}") + val priceString = productDetails + ?.oneTimePurchaseOfferDetails + ?.formattedPrice + + if (priceString != null) { + _price.value = priceString + } + } else { + Log.w(TAG, "queryProductDetails failed: ${result.billingResult.debugMessage}") + } + } + + private suspend fun queryExistingPurchases() { + val result = billingClient.queryPurchasesAsync( + QueryPurchasesParams.newBuilder() + .setProductType(BillingClient.ProductType.INAPP) + .build() + ) + processPurchases(result.purchasesList) + } + + override fun purchase(activity: Activity) { + val details = productDetails ?: run { + Log.e(TAG, "Product details not loaded yet") + return + } + + val billingFlowParams = BillingFlowParams.newBuilder() + .setProductDetailsParamsList( + listOf( + BillingFlowParams.ProductDetailsParams.newBuilder() + .setProductDetails(details) + .build() + ) + ).build() + + val result = billingClient.launchBillingFlow(activity, billingFlowParams) + if (result.responseCode != BillingClient.BillingResponseCode.OK) { + Log.e(TAG, "launchBillingFlow failed: ${result.debugMessage}") + } + } + + override fun onPurchasesUpdated(result: BillingResult, purchases: List?) { + when (result.responseCode) { + BillingClient.BillingResponseCode.OK -> purchases?.let { processPurchases(it) } + BillingClient.BillingResponseCode.USER_CANCELED -> Log.d(TAG, "User cancelled") + else -> Log.w(TAG, "Purchase error ${result.responseCode}: ${result.debugMessage}") + } + } + + private fun processPurchases(purchases: List) { + val hasPremium = purchases.any { + it.products.contains(PREMIUM_PRODUCT_ID) && + it.purchaseState == Purchase.PurchaseState.PURCHASED + } + +// val purchase = purchases.find { +// it.products.contains(PREMIUM_PRODUCT_ID) && it.purchaseState == Purchase.PurchaseState.PURCHASED +// } +// +// if (purchase != null) { +// val consumeParams = ConsumeParams.newBuilder() +// .setPurchaseToken(purchase.purchaseToken) +// .build() +// scope.launch { +// billingClient.consumeAsync(consumeParams) { _, _ ->} +// } +// } + + _isPremium.value = hasPremium + + scope.launch { + purchases + .filter { it.purchaseState == Purchase.PurchaseState.PURCHASED && !it.isAcknowledged } + .forEach { acknowledge(it) } + } + } + + private suspend fun acknowledge(purchase: Purchase) { + val params = AcknowledgePurchaseParams.newBuilder() + .setPurchaseToken(purchase.purchaseToken) + .build() + val result = billingClient.acknowledgePurchase(params) + if (result.responseCode != BillingClient.BillingResponseCode.OK) { + Log.e(TAG, "Acknowledgement failed: ${result.debugMessage}") + } + } + + override fun queryPurchases() { + scope.launch { + queryExistingPurchases() + } + } + + override fun restorePurchases() { + queryPurchases() + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/AACPManager.kt b/android/app/src/main/java/me/kavishdevar/librepods/bluetooth/AACPManager.kt similarity index 76% rename from android/app/src/main/java/me/kavishdevar/librepods/utils/AACPManager.kt rename to android/app/src/main/java/me/kavishdevar/librepods/bluetooth/AACPManager.kt index cd5392ddc..0467ef84d 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/AACPManager.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/bluetooth/AACPManager.kt @@ -1,26 +1,28 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) -package me.kavishdevar.librepods.utils +package me.kavishdevar.librepods.bluetooth import android.util.Log +import me.kavishdevar.librepods.data.Capability +import me.kavishdevar.librepods.data.CustomEq import java.nio.ByteBuffer import java.nio.ByteOrder import kotlin.io.encoding.ExperimentalEncodingApi @@ -31,9 +33,8 @@ import kotlin.io.encoding.ExperimentalEncodingApi * constructing and parsing packets for communication with AirPods. */ class AACPManager { + private val TAG = "AACPManager[${System.identityHashCode(this)}]" companion object { - private const val TAG = "AACPManager" - @Suppress("unused") object Opcodes { const val SET_FEATURE_FLAGS: Byte = 0x4D @@ -43,25 +44,26 @@ class AACPManager { const val EAR_DETECTION: Byte = 0x06 const val CONVERSATION_AWARENESS: Byte = 0x4B const val INFORMATION: Byte = 0x1D - const val RENAME: Byte = 0x1E + const val RENAME: Byte = 0x1A const val HEADTRACKING: Byte = 0x17 const val PROXIMITY_KEYS_REQ: Byte = 0x30 const val PROXIMITY_KEYS_RSP: Byte = 0x31 const val STEM_PRESS: Byte = 0x19 - const val EQ_DATA: Byte = 0x53 + const val HEADPHONE_ACCOMMODATION: Byte = 0x53 const val CONNECTED_DEVICES: Byte = 0x2E // TiPi 1 const val AUDIO_SOURCE: Byte = 0x0E // TiPi 2 const val SMART_ROUTING: Byte = 0x10 const val TIPI_3: Byte = 0x0C // Don't know this one const val SMART_ROUTING_RESP: Byte = 0x11 const val SEND_CONNECTED_MAC: Byte = 0x14 + const val AUDIO_SOURCE_2: Byte = 0x0C // seems redundant? + const val CUSTOM_EQ: Byte = 0x63 } private val HEADER_BYTES = byteArrayOf(0x04, 0x00, 0x04, 0x00) data class ControlCommandStatus( - val identifier: ControlCommandIdentifiers, - val value: ByteArray + val identifier: ControlCommandIdentifiers, val value: ByteArray ) { override fun equals(other: Any?): Boolean { if (this === other) return true @@ -84,43 +86,33 @@ class AACPManager { // @Suppress("unused") enum class ControlCommandIdentifiers(val value: Byte) { - MIC_MODE(0x01), - BUTTON_SEND_MODE(0x05), - VOICE_TRIGGER(0x12), - SINGLE_CLICK_MODE(0x14), - DOUBLE_CLICK_MODE(0x15), - CLICK_HOLD_MODE(0x16), - DOUBLE_CLICK_INTERVAL(0x17), - CLICK_HOLD_INTERVAL(0x18), - LISTENING_MODE_CONFIGS(0x1A), - ONE_BUD_ANC_MODE(0x1B), - CROWN_ROTATION_DIRECTION(0x1C), - LISTENING_MODE(0x0D), - AUTO_ANSWER_MODE(0x1E), - CHIME_VOLUME(0x1F), - VOLUME_SWIPE_INTERVAL(0x23), - CALL_MANAGEMENT_CONFIG(0x24), - VOLUME_SWIPE_MODE(0x25), - ADAPTIVE_VOLUME_CONFIG(0x26), - SOFTWARE_MUTE_CONFIG(0x27), - CONVERSATION_DETECT_CONFIG(0x28), - SSL(0x29), - HEARING_AID(0x2C), - AUTO_ANC_STRENGTH(0x2E), - HPS_GAIN_SWIPE(0x2F), - HRM_STATE(0x30), - IN_CASE_TONE_CONFIG(0x31), - SIRI_MULTITONE_CONFIG(0x32), - HEARING_ASSIST_CONFIG(0x33), - ALLOW_OFF_OPTION(0x34), - STEM_CONFIG(0x39), - SLEEP_DETECTION_CONFIG(0x35), - ALLOW_AUTO_CONNECT(0x36), // not sure what this does, AUTOMATIC_CONNECTION is the only one used, but this is newer... so ¯\_(ツ)_/¯ - EAR_DETECTION_CONFIG(0x0A), - AUTOMATIC_CONNECTION_CONFIG(0x20), - OWNS_CONNECTION(0x06), - PPE_TOGGLE_CONFIG(0x37), - PPE_CAP_LEVEL_CONFIG(0x38); + MIC_MODE(0x01), BUTTON_SEND_MODE(0x05), VOICE_TRIGGER(0x12), SINGLE_CLICK_MODE(0x14), DOUBLE_CLICK_MODE( + 0x15 + ), + CLICK_HOLD_MODE(0x16), DOUBLE_CLICK_INTERVAL(0x17), CLICK_HOLD_INTERVAL(0x18), LISTENING_MODE_CONFIGS( + 0x1A + ), + ONE_BUD_ANC_MODE(0x1B), CROWN_ROTATION_DIRECTION(0x1C), LISTENING_MODE(0x0D), AUTO_ANSWER_MODE( + 0x1E + ), + CHIME_VOLUME(0x1F), VOLUME_SWIPE_INTERVAL(0x23), CALL_MANAGEMENT_CONFIG(0x24), VOLUME_SWIPE_MODE( + 0x25 + ), + ADAPTIVE_VOLUME_CONFIG(0x26), SOFTWARE_MUTE_CONFIG(0x27), CONVERSATION_DETECT_CONFIG( + 0x28 + ), + SSL(0x29), HEARING_AID(0x2C), AUTO_ANC_STRENGTH(0x2E), HPS_GAIN_SWIPE(0x2F), HRM_STATE( + 0x30 + ), + IN_CASE_TONE_CONFIG(0x31), SIRI_MULTITONE_CONFIG(0x32), HEARING_ASSIST_CONFIG(0x33), ALLOW_OFF_OPTION( + 0x34 + ), + STEM_CONFIG(0x39), SLEEP_DETECTION_CONFIG(0x35), ALLOW_AUTO_CONNECT(0x36), // not sure what this does, AUTOMATIC_CONNECTION is the only one used, but this is newer... so ¯\_(ツ)_/¯ + EAR_DETECTION_CONFIG(0x0A), AUTOMATIC_CONNECTION_CONFIG(0x20), OWNS_CONNECTION(0x06), PPE_TOGGLE_CONFIG( + 0x37 + ), + PPE_CAP_LEVEL_CONFIG(0x38), + DYNAMIC_END_OF_CHARGE(0x3B); companion object { fun fromByte(byte: Byte): ControlCommandIdentifiers? = @@ -129,59 +121,44 @@ class AACPManager { } enum class ProximityKeyType(val value: Byte) { - IRK(0x01), - ENC_KEY(0x04); + IRK(0x01), ENC_KEY(0x04); companion object { - fun fromByte(byte: Byte): ProximityKeyType = - ProximityKeyType.entries.find { it.value == byte } - ?: throw IllegalArgumentException("Unknown ProximityKeyType: $byte") + fun fromByte(byte: Byte): ProximityKeyType = entries.find { it.value == byte } + ?: throw IllegalArgumentException("Unknown ProximityKeyType: $byte") } } enum class StemPressType(val value: Byte) { - SINGLE_PRESS(0x05), - DOUBLE_PRESS(0x06), - TRIPLE_PRESS(0x07), - LONG_PRESS(0x08); + SINGLE_PRESS(0x05), DOUBLE_PRESS(0x06), TRIPLE_PRESS(0x07), LONG_PRESS(0x08); companion object { - fun fromByte(byte: Byte): StemPressType? = - entries.find { it.value == byte } + fun fromByte(byte: Byte): StemPressType? = entries.find { it.value == byte } } } enum class StemPressBudType(val value: Byte) { - LEFT(0x01), - RIGHT(0x02); + LEFT(0x01), RIGHT(0x02); companion object { - fun fromByte(byte: Byte): StemPressBudType? = - entries.find { it.value == byte } + fun fromByte(byte: Byte): StemPressBudType? = entries.find { it.value == byte } } } enum class AudioSourceType(val value: Byte) { - NONE(0x00), - CALL(0x01), - MEDIA(0x02); + NONE(0x00), CALL(0x01), MEDIA(0x02); companion object { - fun fromByte(byte: Byte): AudioSourceType? = - entries.find { it.value == byte } + fun fromByte(byte: Byte): AudioSourceType? = entries.find { it.value == byte } } } data class AudioSource( - val mac: String, - val type: AudioSourceType + val mac: String, val type: AudioSourceType ) data class ConnectedDevice( - val mac: String, - val info1: Byte, - val info2: Byte, - var type: String? + val mac: String, val info1: Byte, val info2: Byte, var type: String? ) data class AirPodsInformation( @@ -216,7 +193,7 @@ class AACPManager { var audioSource: AudioSource? = null private set - var eqData = FloatArray(8) { 0.0f } + var eqData = FloatArray(8) private set var eqOnPhone: Boolean = false @@ -225,19 +202,20 @@ class AACPManager { var eqOnMedia: Boolean = false private set + var customEq: CustomEq = CustomEq(state = 1, low = 50, mid = 50, high = 50) + private set + + var customEqCallback: ((CustomEq) -> Unit)? = null + fun getControlCommandStatus(identifier: ControlCommandIdentifiers): ControlCommandStatus? { return controlCommandStatusList.find { it.identifier == identifier } } private fun setControlCommandStatusValue( - identifier: ControlCommandIdentifiers, - value: ByteArray + identifier: ControlCommandIdentifiers, value: ByteArray ) { val existingStatus = getControlCommandStatus(identifier) - if (existingStatus == value) { - controlCommandStatusList.remove(existingStatus) - } - if (existingStatus != null) { + if (existingStatus?.value.contentEquals(value)) { controlCommandStatusList.remove(existingStatus) } controlCommandListeners[identifier]?.forEach { listener -> @@ -265,6 +243,9 @@ class AACPManager { fun onConnectedDevicesReceived(connectedDevices: List) fun onOwnershipToFalseRequest(sender: String, reasonReverseTapped: Boolean) fun onShowNearbyUI(sender: String) + fun onHeadphoneAccommodationReceived(eqData: FloatArray) + fun onCustomEqReceived(customEq: CustomEq) + fun onCapabilitiesReceived(capabilities: List) } fun parseStemPressResponse(data: ByteArray): Pair { @@ -287,15 +268,13 @@ class AACPManager { } fun registerControlCommandListener( - identifier: ControlCommandIdentifiers, - callback: ControlCommandListener + identifier: ControlCommandIdentifiers, callback: ControlCommandListener ) { controlCommandListeners.getOrPut(identifier) { mutableListOf() }.add(callback) } fun unregisterControlCommandListener( - identifier: ControlCommandIdentifiers, - callback: ControlCommandListener + identifier: ControlCommandIdentifiers, callback: ControlCommandListener ) { controlCommandListeners[identifier]?.remove(callback) } @@ -330,8 +309,7 @@ class AACPManager { fun sendControlCommand(identifier: Byte, value: ByteArray): Boolean { val controlPacket = createControlCommandPacket(identifier, value) setControlCommandStatusValue( - ControlCommandIdentifiers.fromByte(identifier) ?: return false, - value + ControlCommandIdentifiers.fromByte(identifier) ?: return false, value ) return sendDataPacket(controlPacket) } @@ -340,16 +318,14 @@ class AACPManager { fun sendControlCommand(identifier: Byte, value: Byte): Boolean { val controlPacket = createControlCommandPacket(identifier, byteArrayOf(value)) setControlCommandStatusValue( - ControlCommandIdentifiers.fromByte(identifier) ?: return false, - byteArrayOf(value) + ControlCommandIdentifiers.fromByte(identifier) ?: return false, byteArrayOf(value) ) return sendDataPacket(controlPacket) } fun sendControlCommand(identifier: Byte, value: Boolean): Boolean { val controlPacket = createControlCommandPacket( - identifier, - if (value) byteArrayOf(0x01) else byteArrayOf(0x02) + identifier, if (value) byteArrayOf(0x01) else byteArrayOf(0x02) ) setControlCommandStatusValue( ControlCommandIdentifiers.fromByte(identifier) ?: return false, @@ -369,8 +345,7 @@ class AACPManager { fun parseProximityKeysResponse(data: ByteArray): Map { Log.d( - TAG, - "Parsing Proximity Keys Response: ${data.joinToString(" ") { "%02X".format(it) }}" + TAG, "Parsing Proximity Keys Response: ${data.joinToString(" ") { "%02X".format(it) }}" ) if (data.size < 4) { throw IllegalArgumentException("Data array too short to parse Proximity Keys Response") @@ -395,14 +370,18 @@ class AACPManager { } val key = ByteArray(keyLength) System.arraycopy(data, offset, key, 0, keyLength) - keys[ProximityKeyType.fromByte(keyType)] = key + try { + keys[ProximityKeyType.fromByte(keyType)] = key + } catch (e: Exception) { + Log.e( + TAG, "incorrect key type received: $keyType, ${key.toHexString()}" + ) + } offset += keyLength Log.d( - TAG, - "Parsed Proximity Key: Type: ${keyType}, Length: $keyLength, Key: ${ - key.joinToString(" ") { "%02X".format(it) } - }" - ) + TAG, "Parsed Proximity Key: Type: ${keyType}, Length: $keyLength, Key: ${ + key.joinToString(" ") { "%02X".format(it) } + }") } return keys } @@ -422,32 +401,33 @@ class AACPManager { fun receivePacket(packet: ByteArray) { if (!packet.toHexString().startsWith("04000400")) { Log.w( - TAG, - "Received packet does not start with expected header: ${ - packet.joinToString(" ") { - "%02X".format(it) - } - }" - ) + TAG, "Received packet does not start with expected header: ${ + packet.joinToString(" ") { + "%02X".format(it) + } + }") return } if (packet.size < 6) { Log.w( - TAG, - "Received packet too short: ${packet.joinToString(" ") { "%02X".format(it) }}" + TAG, "Received packet too short: ${packet.joinToString(" ") { "%02X".format(it) }}" ) return } - val opcode = packet[4] - - when (opcode) { + when (val opcode = packet[4]) { Opcodes.BATTERY_INFO -> { callback?.onBatteryInfoReceived(packet) } Opcodes.CONTROL_COMMAND -> { - val controlCommand = ControlCommand.fromByteArray(packet) + val controlCommand = try { + ControlCommand.fromByteArray(packet) + } catch (e: Exception) { + Log.w(TAG, "Failed to parse control command: ${e.message}") + callback?.onUnknownPacketReceived(packet) + return + } setControlCommandStatusValue( ControlCommandIdentifiers.fromByte(controlCommand.identifier) ?: return, controlCommand.value @@ -456,10 +436,9 @@ class AACPManager { TAG, "Control command received: ${controlCommand.identifier.toHexString()} - ${ controlCommand.value.joinToString(" ") { "%02X".format(it) } - }" - ) - Log.d( - TAG, "Control command list is now: ${ + }") + + val controlCommandListText = try { controlCommandStatusList.joinToString(", ") { it -> "${it.identifier.name} (${it.identifier.value.toHexString()}) - ${ it.value.joinToString( @@ -467,12 +446,19 @@ class AACPManager { ) { "%02X".format(it) } }" } - }") + } catch (e: Exception) { + e.message + } + + Log.d( + TAG, "Control command list is now: $controlCommandListText" + ) val controlCommandIdentifier = ControlCommandIdentifiers.fromByte(controlCommand.identifier) if (controlCommandIdentifier != null) { controlCommandListeners[controlCommandIdentifier]?.forEach { listener -> + Log.d(TAG, "calling listener for ${controlCommandIdentifier.name}") listener.onControlCommandReceived(controlCommand) } } else { @@ -500,13 +486,11 @@ class AACPManager { Opcodes.HEADTRACKING -> { if (packet.size < 70) { Log.w( - TAG, - "Received HEADTRACKING packet too short: ${ - packet.joinToString(" ") { - "%02X".format(it) - } - }" - ) + TAG, "Received HEADTRACKING packet too short: ${ + packet.joinToString(" ") { + "%02X".format(it) + } + }") return } callback?.onHeadTrackingReceived(packet) @@ -538,7 +522,8 @@ class AACPManager { Opcodes.SMART_ROUTING_RESP -> { val packetString = packet.decodeToString() - val sender = packet.sliceArray(6..11).reversedArray().joinToString(":") { "%02X".format(it) } + val sender = + packet.sliceArray(6..11).reversedArray().joinToString(":") { "%02X".format(it) } // if (connectedDevices.find { it.mac == sender }?.type == null && packetString.contains("btName")) { // val nameStartIndex = packetString.indexOf("btName") + 8 @@ -558,52 +543,73 @@ class AACPManager { } else if ("Android" in packetString) { connectedDevices.find { it.mac == sender }?.type = "Android" } - Log.d(TAG, "Smart Routing Response from $sender: $packetString, type: ${connectedDevices.find { it.mac == sender }?.type}") + Log.d( + TAG, + "Smart Routing Response from $sender: $packetString, type: ${connectedDevices.find { it.mac == sender }?.type}" + ) if (packetString.contains("SetOwnershipToFalse")) { - callback?.onOwnershipToFalseRequest(sender, packetString.contains("ReverseBannerTapped")) + callback?.onOwnershipToFalseRequest( + sender, + packetString.contains("ReverseBannerTapped") + ) } if (packetString.contains("ShowNearbyUI")) { callback?.onShowNearbyUI(sender) } } - Opcodes.EQ_DATA -> { + Opcodes.HEADPHONE_ACCOMMODATION -> { if (packet.size != 140) { Log.w( TAG, - "Received EQ_DATA packet of unexpected size: ${packet.size}, expected 140" + "Received HEADPHONE_ACCOMMODATION packet of unexpected size: ${packet.size}, expected 140" ) return } if (packet[6] != 0x84.toByte()) { Log.w( TAG, - "Received EQ_DATA packet with unexpected identifier: ${packet[6].toHexString()}, expected 0x84" + "Received HEADPHONE_ACCOMMODATION packet with unexpected identifier: ${packet[6].toHexString()}, expected 0x84" ) return } eqOnMedia = (packet[10] == 0x01.toByte()) eqOnPhone = (packet[11] == 0x01.toByte()) - // there are 4 eqs. i am not sure what those are for, maybe all 4 listening modes, or maybe phone+media left+right, but then there shouldn't be another flag for phone/media enabled. just directly the EQ... weird. + // there are 4 eqs. i am not sure what those are for, maybe all 4 listening modes, or maybe phone+media left+right, but then there shouldn't be another flag for phone/media visible. just directly the EQ... weird. // the EQs are little endian floats - val eq1 = ByteBuffer.wrap(packet, 12, 32).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer() - val eq2 = ByteBuffer.wrap(packet, 44, 32).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer() - val eq3 = ByteBuffer.wrap(packet, 76, 32).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer() - val eq4 = ByteBuffer.wrap(packet, 108, 32).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer() + val eq1 = + ByteBuffer.wrap(packet, 12, 32).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer() + ByteBuffer.wrap(packet, 44, 32).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer() + ByteBuffer.wrap(packet, 76, 32).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer() + ByteBuffer.wrap(packet, 108, 32).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer() // for now, taking just the first EQ eqData = FloatArray(8) { i -> eq1.get(i) } - Log.d(TAG, "EQ Data set to: ${eqData.toList()}, eqOnPhone: $eqOnPhone, eqOnMedia: $eqOnMedia") + + Log.d( + TAG, + "EQ Data set to: ${eqData.toList()}, eqOnPhone: $eqOnPhone, eqOnMedia: $eqOnMedia" + ) + + callback?.onHeadphoneAccommodationReceived(eqData) } - + Opcodes.INFORMATION -> { - Log.e(TAG, "Parsing Information Packet") + Log.d(TAG, "Parsing Information Packet") val information = parseInformationPacket(packet) callback?.onDeviceInformationReceived(information) } + + Opcodes.CUSTOM_EQ -> { + Log.d(TAG, "Parsing CUSTOM_EQ: ${packet.toHexString()}") + customEq = parseCustomEqPacket(packet) + customEqCallback?.invoke(customEq) + callback?.onCustomEqReceived(customEq) + } + else -> { - Log.d(TAG, "Unknown opcode received: ${opcode.toHexString()}") + Log.d(TAG, "Unhandled opcode received: ${opcode.toHexString()}") callback?.onUnknownPacketReceived(packet) } } @@ -633,10 +639,22 @@ class AACPManager { fun createHandshakePacket(): ByteArray { return byteArrayOf( - 0x00, 0x00, 0x04, 0x00, - 0x01, 0x00, 0x02, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00 + 0x00, + 0x00, + 0x04, + 0x00, + 0x01, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00 ) } @@ -773,25 +791,40 @@ class AACPManager { val packet = ByteArray(5 + size) packet[0] = Opcodes.RENAME packet[1] = 0x00 - packet[2] = size.toByte() - packet[3] = 0x00 - System.arraycopy(nameBytes, 0, packet, 4, size) + packet[2] = 0x01 + packet[3] = size.toByte() + packet[4] = 0x00 + System.arraycopy(nameBytes, 0, packet, 5, size) return packet } fun sendMediaInformationNewDevice(selfMacAddress: String, targetMacAddress: String): Boolean { - if (selfMacAddress.length != 17 || !selfMacAddress.matches(Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")) || targetMacAddress.length != 17 || !targetMacAddress.matches(Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}"))) { + if (selfMacAddress.length != 17 || !selfMacAddress.matches(Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")) || targetMacAddress.length != 17 || !targetMacAddress.matches( + Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}") + ) + ) { // throw IllegalArgumentException("MAC address must be 6 bytes") - Log.w(TAG, "Invalid MAC address format, got: selfMacAddress=$selfMacAddress, targetMacAddress=$targetMacAddress") + Log.w( + TAG, + "Invalid MAC address format, got: selfMacAddress=$selfMacAddress, targetMacAddress=$targetMacAddress" + ) return false } Log.d(TAG, "SELFMAC: ${selfMacAddress}, TARGETMAC: $targetMacAddress") Log.d(TAG, "Sending Media Information packet to $targetMacAddress") - return sendDataPacket(createMediaInformationNewDevicePacket(selfMacAddress, targetMacAddress)) + return sendDataPacket( + createMediaInformationNewDevicePacket( + selfMacAddress, + targetMacAddress + ) + ) } - fun createMediaInformationNewDevicePacket(selfMacAddress: String, targetMacAddress: String): ByteArray { + fun createMediaInformationNewDevicePacket( + selfMacAddress: String, + targetMacAddress: String + ): ByteArray { val opcode = byteArrayOf(Opcodes.SMART_ROUTING, 0x00) val buffer = ByteBuffer.allocate(116) buffer.put( @@ -880,17 +913,13 @@ class AACPManager { Log.d(TAG, "Sending Media Information packet to $targetMac") return sendDataPacket( createMediaInformationPacket( - selfMacAddress, - targetMac, - streamingState + selfMacAddress, targetMac, streamingState ) ) } fun createMediaInformationPacket( - selfMacAddress: String, - targetMacAddress: String, - streamingState: Boolean = true + selfMacAddress: String, targetMacAddress: String, streamingState: Boolean = true ): ByteArray { val opcode = byteArrayOf(Opcodes.SMART_ROUTING, 0x00) val buffer = ByteBuffer.allocate(138) @@ -905,7 +934,7 @@ class AACPManager { ) buffer.put(byteArrayOf(0x01, 0xE5.toByte(), 0x4A)) // unknown, constant buffer.put("PlayingApp".toByteArray()) - buffer.put(byteArrayOf(0x56)) // 'V', seems like a identifier or a separator + buffer.put(byteArrayOf(0x56)) // 'V', seems like an identifier or a separator buffer.put("com.google.ios.youtube".toByteArray()) // package name, hardcoding for now, aforementioned reason buffer.put(byteArrayOf(0x52)) // 'R' buffer.put("HostStreamingState".toByteArray()) @@ -923,7 +952,7 @@ class AACPManager { buffer.put("AudioCategory".toByteArray()) buffer.put(byteArrayOf(0x31, 0x2D, 0x01)) - return opcode+buffer.array() + return opcode + buffer.array() } fun sendSmartRoutingShowUI(selfMacAddress: String): Boolean { @@ -1005,9 +1034,15 @@ class AACPManager { fun sendAddTiPiDevice(selfMacAddress: String, targetMacAddress: String): Boolean { - if (selfMacAddress.length != 17 || !selfMacAddress.matches(Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")) || targetMacAddress.length != 17 || !targetMacAddress.matches(Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}"))) { + if (selfMacAddress.length != 17 || !selfMacAddress.matches(Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")) || targetMacAddress.length != 17 || !targetMacAddress.matches( + Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}") + ) + ) { // throw IllegalArgumentException("MAC address must be 6 bytes") - Log.w(TAG, "Invalid MAC address format, got: selfMacAddress=$selfMacAddress, targetMacAddress=$targetMacAddress") + Log.w( + TAG, + "Invalid MAC address format, got: selfMacAddress=$selfMacAddress, targetMacAddress=$targetMacAddress" + ) return false } Log.d(TAG, "Sending Add TiPi Device packet to $targetMacAddress") @@ -1041,8 +1076,7 @@ class AACPManager { } data class ControlCommand( - val identifier: Byte, - val value: ByteArray + val identifier: Byte, val value: ByteArray ) { override fun equals(other: Any?): Boolean { if (this === other) return true @@ -1064,25 +1098,25 @@ class AACPManager { companion object { fun fromByteArray(data: ByteArray): ControlCommand { - if (data.size < 4) { - throw IllegalArgumentException("Data array too short to parse ControlCommand") + var offset = 0 + while (data.size - offset >= 4 && + data[offset] == 0x04.toByte() && + data[offset + 1] == 0x00.toByte() && + data[offset + 2] == 0x04.toByte() && + data[offset + 3] == 0x00.toByte() + ) { + offset += 4 } - if (data[0] == 0x04.toByte() && data[1] == 0x00.toByte() && data[2] == 0x04.toByte() && data[3] == 0x00.toByte()) { - val newData = ByteArray(data.size - 4) - System.arraycopy(data, 4, newData, 0, data.size - 4) - return fromByteArray(newData) + if (data.size - offset < 7) { + throw IllegalArgumentException("Too short for ControlCommand") } - if (data[0] != Opcodes.CONTROL_COMMAND) { - throw IllegalArgumentException("Data array does not start with CONTROL_COMMAND opcode") + if (data[offset] != Opcodes.CONTROL_COMMAND) { + throw IllegalArgumentException("Invalid opcode") } - val identifier = data[2] - - val value = ByteArray(4) - System.arraycopy(data, 3, value, 0, 4) - - val trimmedValue = value.dropLastWhile { it == 0x00.toByte() }.toByteArray() - val finalValue = if (trimmedValue.isEmpty()) byteArrayOf(0x00) else trimmedValue - return ControlCommand(identifier, finalValue) + val identifier = data[offset + 2] + val value = data.copyOfRange(offset + 3, offset + 7) + val trimmed = value.dropLastWhile { it == 0x00.toByte() }.toByteArray() + return ControlCommand(identifier, if (trimmed.isEmpty()) byteArrayOf(0x00) else trimmed) } } } @@ -1094,10 +1128,8 @@ class AACPManager { triplePressCustomized: Boolean = false, longPressCustomized: Boolean = false ): Boolean { - val value = ((if (singlePressCustomized) 0x01 else 0) or - (if (doublePressCustomized) 0x02 else 0) or - (if (triplePressCustomized) 0x04 else 0) or - (if (longPressCustomized) 0x08 else 0)).toByte() + val value = + ((if (singlePressCustomized) 0x01 else 0) or (if (doublePressCustomized) 0x02 else 0) or (if (triplePressCustomized) 0x04 else 0) or (if (longPressCustomized) 0x08 else 0)).toByte() Log.d(TAG, "Sending Stem Config Packet with value: ${value.toHexString()}") return sendControlCommand( ControlCommandIdentifiers.STEM_CONFIG.value, value @@ -1110,21 +1142,26 @@ class AACPManager { Log.d(TAG, "Sending packet: ${packet.joinToString(" ") { "%02X".format(it) }}") if (packet[4] == Opcodes.CONTROL_COMMAND) { - val controlCommand = ControlCommand.fromByteArray(packet) + val controlCommand = try { + ControlCommand.fromByteArray(packet) + } catch (e: Exception) { + Log.w(TAG, "Invalid control command: ${e.message}") + callback?.onUnknownPacketReceived(packet) + return false + } Log.d( - TAG, - "Control command: ${controlCommand.identifier.toHexString()} - ${ - controlCommand.value.joinToString(" ") { "%02X".format(it) } - }" - ) + TAG, "Control command: ${controlCommand.identifier.toHexString()} - ${ + controlCommand.value.joinToString(" ") { "%02X".format(it) } + }") setControlCommandStatusValue( ControlCommandIdentifiers.fromByte(controlCommand.identifier) ?: return false, controlCommand.value ) } - val socket = BluetoothConnectionManager.getCurrentSocket() - if (socket?.isConnected == true) { + val socket = BluetoothConnectionManager.getAACPSocket() ?: return false + + if (socket.isConnected) { socket.outputStream?.write(packet) socket.outputStream?.flush() return true @@ -1201,7 +1238,11 @@ class AACPManager { var offset = 9 for (i in 0 until deviceCount) { if (offset + 8 > data.size) { - throw IllegalArgumentException("Data array too short to parse all connected devices") + Log.w( + TAG, + "Data array too short to parse all connected devices, returning what we have" + ) + break } val macBytes = data.sliceArray(offset until offset + 6) val mac = macBytes.joinToString(":") { "%02X".format(it) } @@ -1214,6 +1255,7 @@ class AACPManager { return devices } + fun sendSomePacketIDontKnowWhatItIs() { // 2900 00ff ffff ffff ffff -- enables setting EQ sendDataPacket( @@ -1271,4 +1313,38 @@ class AACPManager { version3 = strings.getOrNull(10) ?: "", ) } + + fun sendCustomEqPacket(customEq: CustomEq): Boolean { + return sendDataPacket(customEq.toPacket()) + } + + fun parseCustomEqPacket(packet: ByteArray): CustomEq { + val data = packet.sliceArray(6 until packet.size) + + if (data.size < 7) { + Log.e(TAG, "custom EQ packet length less than 7, returning default") + return CustomEq(1, 50, 50, 50) + } + + val lengthLow = data[0].toInt() and 0xFF + val lengthHigh = data[1].toInt() and 0xFF + + val length = (lengthHigh shl 8) or lengthLow + + if (length != 5) { + Log.w(TAG, "parseCustomEqPacket: unexpected length ($length). parsing normally") + } + + val state = data[3].toInt() + val low = data[4].toInt() + val mid = data[5].toInt() + val high = data[6].toInt() + + return CustomEq( + state, + low, + mid, + high + ) + } } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/bluetooth/ATTManager.kt b/android/app/src/main/java/me/kavishdevar/librepods/bluetooth/ATTManager.kt new file mode 100644 index 000000000..4dac27fa7 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/bluetooth/ATTManager.kt @@ -0,0 +1,211 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.bluetooth + +import android.util.Log +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean + +private const val TAG = "ATTManager" + +enum class ATTHandles(val value: Int) { + TRANSPARENCY(0x18), + LOUD_SOUND_REDUCTION(0x1B), + HEARING_AID(0x2A) +} + +enum class ATTCCCDHandles(val value: Int) { + TRANSPARENCY(ATTHandles.TRANSPARENCY.value + 1), +// LOUD_SOUND_REDUCTION(ATTHandles.LOUD_SOUND_REDUCTION.value + 1), // doesn't work + HEARING_AID(ATTHandles.HEARING_AID.value + 1) +} + +class ATTManagerv2 { + val characteristicList = mutableMapOf() + + private val responseQueues = ConcurrentHashMap>() + + private val readerRunning = AtomicBoolean(false) + private var readerThread: Thread? = null + + private var onNotificationReceived: ((handle: Byte, value: ByteArray) -> Unit)? = null + + fun startReader() { + if (readerRunning.getAndSet(true)) return + + readerThread = Thread { + try { + runReaderLoop() + } catch (t: Throwable) { + Log.e(TAG, "reader thread crashed: ${t.message}", t) + } finally { + readerRunning.set(false) + Log.d(TAG, "reader thread stopped") + } + }.also { it.name = "ATT-Reader"; it.isDaemon = true; it.start() } + Log.d(TAG, "reader started") + } + + fun stopReader() { + readerRunning.set(false) + readerThread?.interrupt() + readerThread = null + } + + fun setOnNotificationReceived(listener: ((handle: Byte, value: ByteArray) -> Unit)?) { + onNotificationReceived = listener + } + + fun enableNotification(handle: ATTCCCDHandles) { + writeCharacteristic(handle.value.toByte(), byteArrayOf(0x01)) + } + + fun getCharacteristic(handle: ATTHandles): ByteArray? { + val storedValue = characteristicList[handle] + return if (storedValue?.isNotEmpty() != true) { + readCharacteristic(handle) + } else storedValue + } + + fun readCharacteristic(handle: ATTHandles, timeoutMillis: Long = 2000): ByteArray? { + val socket = BluetoothConnectionManager.getATTSocket() ?: return null + try { + val output = socket.outputStream + val pdu = byteArrayOf(0x0A, handle.value.toByte(), 0x00) + synchronized(output) { + output.write(pdu) + output.flush() + } + Log.d(TAG, "sending read request: ${pdu.joinToString(" ") { String.format("%02X", it) }}") + + val resp = waitForResponse(0x0B, timeoutMillis) ?: run { + Log.e(TAG, "Timeout waiting for Read Response (0x0B) for handle ${handle.value}") + return null + } + + Log.d(TAG, "read response: ${resp.joinToString(" ") { String.format("%02X", it) }}") + val value = resp.copyOfRange(1, resp.size) + characteristicList[handle] = value + return value + } catch (e: Exception) { + Log.e(TAG, "error reading characteristic: ${e.message}") + return null + } + } + + fun writeCharacteristic(handle: ATTHandles, data: ByteArray, timeoutMillis: Long = 2000) { + characteristicList[handle] = data + writeCharacteristic(handle.value.toByte(), data, timeoutMillis) + } + + fun writeCharacteristic(handle: Byte, data: ByteArray, timeoutMillis: Long = 2000) { + val socket = BluetoothConnectionManager.getATTSocket() ?: return + try { + val output = socket.outputStream + val pdu = byteArrayOf(0x12, handle, 0x00) + data // 0x00 for LE + synchronized(output) { + output.write(pdu) + output.flush() + } + Log.d(TAG, "sending write request: ${pdu.joinToString(" ") { String.format("%02X", it) }}") + + val resp = waitForResponse(0x13, timeoutMillis) ?: run { + Log.e(TAG, "timeout waiting for response (0x13) for handle ${String.format("%02X", handle)}") + return + } + + Log.d(TAG, "write respose: ${resp.joinToString(" ") { String.format("%02X", it) }}") + } catch (e: Exception) { + Log.e(TAG, "error writing characteristic: ${e.message}") + } + } + + fun disconnected() { + characteristicList.clear() + stopReader() + val socket = BluetoothConnectionManager.getATTSocket() ?: return + try { + socket.close() + } catch (e: Exception) { + Log.w(TAG, "error closing socket: ${e.message}") + } + Log.d(TAG, "ATT disconnected") + } + + private fun runReaderLoop() { + val socket = BluetoothConnectionManager.getATTSocket() ?: run { + Log.w(TAG, "ATT socket not available. stopping reader") + readerRunning.set(false) + return + } + + val input = socket.inputStream + val buffer = ByteArray(512) + + while (readerRunning.get()) { + try { + val len = input.read(buffer) + if (len == -1) { + Log.w(TAG, "ATT input stream ended") + break + } + val data = buffer.copyOfRange(0, len) + if (data.isEmpty()) continue + + val opcode = data[0] + Log.d(TAG, "pdu received ${data.joinToString(" ") { String.format("%02X", it) }}") + + val queue = responseQueues.computeIfAbsent(opcode) { LinkedBlockingQueue() } + queue.offer(data) + + if (opcode == 0x1B.toByte()) { + if (data.size >= 3) { + val handle = data[1] + val value = if (data.size > 3) data.copyOfRange(3, data.size) else ByteArray(0) + Log.d(TAG, "notification/indication handle=0x${String.format("%02X", handle)} value=${value.toHexString()}") + try { + onNotificationReceived?.invoke(handle, value) + } catch (t: Throwable) { + Log.e(TAG, "onNotificationReceived threw: ${t.message}", t) + } + } else { + Log.w(TAG, "notification PDU too short: ${data.joinToString(" ") { String.format("%02X", it) }}") + } + } + } catch (e: Exception) { + Log.e(TAG, "error in reader loop: ${e.message}", e) + break + } + } + + readerRunning.set(false) + } + + private fun waitForResponse(opcode: Byte, timeoutMillis: Long): ByteArray? { + val queue = responseQueues.computeIfAbsent(opcode) { LinkedBlockingQueue() } + return try { + queue.poll(timeoutMillis, TimeUnit.MILLISECONDS) + } catch (e: Exception) { + e.printStackTrace() + null + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/BLEManager.kt b/android/app/src/main/java/me/kavishdevar/librepods/bluetooth/BLEManager.kt similarity index 95% rename from android/app/src/main/java/me/kavishdevar/librepods/utils/BLEManager.kt rename to android/app/src/main/java/me/kavishdevar/librepods/bluetooth/BLEManager.kt index 5553e217c..52fa05512 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/BLEManager.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/bluetooth/BLEManager.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple's ecosystem - * - * Copyright (C) 2025 LibrePods Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ -package me.kavishdevar.librepods.utils +package me.kavishdevar.librepods.bluetooth import android.annotation.SuppressLint import android.bluetooth.BluetoothManager @@ -30,8 +30,10 @@ import android.content.SharedPreferences import android.os.Handler import android.os.Looper import android.util.Log +import me.kavishdevar.librepods.utils.BluetoothCryptography import javax.crypto.Cipher import javax.crypto.spec.SecretKeySpec +import kotlin.collections.iterator import kotlin.io.encoding.Base64 import kotlin.io.encoding.ExperimentalEncodingApi diff --git a/android/app/src/main/java/me/kavishdevar/librepods/bluetooth/BluetoothConnectionManager.kt b/android/app/src/main/java/me/kavishdevar/librepods/bluetooth/BluetoothConnectionManager.kt new file mode 100644 index 000000000..012b587d2 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/bluetooth/BluetoothConnectionManager.kt @@ -0,0 +1,39 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.bluetooth + +import android.bluetooth.BluetoothSocket + +object BluetoothConnectionManager { + private var aacpSocket: BluetoothSocket? = null + private var attSocket: BluetoothSocket? = null + + fun setCurrentConnection(aacpSocket: BluetoothSocket?, attSocket: BluetoothSocket?) { + BluetoothConnectionManager.aacpSocket = aacpSocket + BluetoothConnectionManager.attSocket = attSocket + } + + fun getAACPSocket(): BluetoothSocket? { + return aacpSocket + } + + fun getATTSocket(): BluetoothSocket? { + return attSocket + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/AudioSettings.kt b/android/app/src/main/java/me/kavishdevar/librepods/composables/AudioSettings.kt deleted file mode 100644 index 8a0da0c67..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/AudioSettings.kt +++ /dev/null @@ -1,152 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -@file:OptIn(ExperimentalEncodingApi::class) - -package me.kavishdevar.librepods.composables - -import androidx.compose.foundation.background -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.HorizontalDivider -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.navigation.NavController -import androidx.navigation.compose.rememberNavController -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.ATTHandles -import me.kavishdevar.librepods.utils.Capability -import kotlin.io.encoding.ExperimentalEncodingApi - -@Composable -fun AudioSettings(navController: NavController) { - val isDarkTheme = isSystemInDarkTheme() - val textColor = if (isDarkTheme) Color.White else Color.Black - val service = ServiceManager.getService() - if (service == null) return - val airpodsInstance = service.airpodsInstance - if (airpodsInstance == null) return - if (!airpodsInstance.model.capabilities.contains(Capability.ADAPTIVE_VOLUME) && - !airpodsInstance.model.capabilities.contains(Capability.CONVERSATION_AWARENESS) && - !airpodsInstance.model.capabilities.contains(Capability.LOUD_SOUND_REDUCTION) && - !airpodsInstance.model.capabilities.contains(Capability.ADAPTIVE_AUDIO) - ) { - return - } - Box( - modifier = Modifier - .background(if (isDarkTheme) Color(0xFF000000) else Color(0xFFF2F2F7)) - .padding(horizontal = 16.dp, vertical = 4.dp) - ){ - Text( - text = stringResource(R.string.audio), - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f) - ) - ) - } - - val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - - Column( - modifier = Modifier - .clip(RoundedCornerShape(28.dp)) - .fillMaxWidth() - .background(backgroundColor, RoundedCornerShape(28.dp)) - .padding(top = 2.dp) - ) { - - if (airpodsInstance.model.capabilities.contains(Capability.ADAPTIVE_VOLUME)) { - StyledToggle( - label = stringResource(R.string.personalized_volume), - description = stringResource(R.string.personalized_volume_description), - controlCommandIdentifier = AACPManager.Companion.ControlCommandIdentifiers.ADAPTIVE_VOLUME_CONFIG, - independent = false - ) - - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(horizontal = 12.dp) - ) - } - - if (airpodsInstance.model.capabilities.contains(Capability.CONVERSATION_AWARENESS)) { - StyledToggle( - label = stringResource(R.string.conversational_awareness), - description = stringResource(R.string.conversational_awareness_description), - controlCommandIdentifier = AACPManager.Companion.ControlCommandIdentifiers.CONVERSATION_DETECT_CONFIG, - independent = false - ) - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(horizontal = 12.dp) - ) - } - - if (airpodsInstance.model.capabilities.contains(Capability.LOUD_SOUND_REDUCTION)){ - StyledToggle( - label = stringResource(R.string.loud_sound_reduction), - description = stringResource(R.string.loud_sound_reduction_description), - attHandle = ATTHandles.LOUD_SOUND_REDUCTION, - independent = false - ) - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(horizontal = 12.dp) - ) - } - - if (airpodsInstance.model.capabilities.contains(Capability.ADAPTIVE_AUDIO)) { - NavigationButton( - to = "adaptive_strength", - name = stringResource(R.string.adaptive_audio), - navController = navController, - independent = false - ) - } - } -} - -@Preview -@Composable -fun AudioSettingsPreview() { - AudioSettings(rememberNavController()) -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/BatteryIndicator.kt b/android/app/src/main/java/me/kavishdevar/librepods/composables/BatteryIndicator.kt deleted file mode 100644 index 3beef1c00..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/BatteryIndicator.kt +++ /dev/null @@ -1,128 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.composables - - -import android.content.res.Configuration -import androidx.compose.animation.core.Animatable -import androidx.compose.animation.core.tween -import androidx.compose.foundation.background -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.height -import androidx.compose.material3.CircularProgressIndicator -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.remember -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.scale -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.StrokeCap -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.Font -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import me.kavishdevar.librepods.R - -@Composable -fun BatteryIndicator( - batteryPercentage: Int, - charging: Boolean = false, - prefix: String = "", - previousCharging: Boolean = false, -) { - val isDarkTheme = isSystemInDarkTheme() - val backgroundColor = if (isDarkTheme) Color.Black else Color(0xFFF2F2F7) - val batteryTextColor = if (isDarkTheme) Color.White else Color.Black - val batteryFillColor = if (batteryPercentage > 25) - if (isDarkTheme) Color(0xFF2ED158) else Color(0xFF35C759) - else if (isDarkTheme) Color(0xFFFC4244) else Color(0xFFfe373C) - - val initialScale = if (previousCharging) 1f else 0f - val scaleAnim = remember { Animatable(initialScale) } - val targetScale = if (charging) 1f else 0f - - LaunchedEffect(previousCharging, charging) { - scaleAnim.animateTo(targetScale, animationSpec = tween(durationMillis = 250)) - } - - Column( - modifier = Modifier - .background(backgroundColor), // just for haze to work - horizontalAlignment = Alignment.CenterHorizontally - ) { - Box( - modifier = Modifier.padding(bottom = 4.dp), - contentAlignment = Alignment.Center - ) { - CircularProgressIndicator( - progress = { batteryPercentage / 100f }, - modifier = Modifier.size(40.dp), - color = batteryFillColor, - gapSize = 0.dp, - strokeCap = StrokeCap.Round, - strokeWidth = 4.dp, - trackColor = if (isDarkTheme) Color(0xFF0E0E0F) else Color(0xFFE3E3E8) - ) - - Text( - text = "\uDBC0\uDEE6", - style = TextStyle( - fontSize = 12.sp, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = batteryFillColor, - textAlign = TextAlign.Center - ), - modifier = Modifier.scale(scaleAnim.value) - ) - } - - Spacer(modifier = Modifier.height(4.dp)) - - Text( - text = "$prefix $batteryPercentage%", - color = batteryTextColor, - style = TextStyle( - fontSize = 16.sp, - fontFamily = FontFamily(Font(R.font.sf_pro)), - textAlign = TextAlign.Center - ), - ) - } -} - -@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) -@Composable -fun BatteryIndicatorPreview() { - val bg = if (isSystemInDarkTheme()) Color.Black else Color(0xFFF2F2F7) - Box( - modifier = Modifier.background(bg) - ) { - BatteryIndicator(batteryPercentage = 24, charging = true, prefix = "\uDBC6\uDCE5", previousCharging = false) - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/BatteryView.kt b/android/app/src/main/java/me/kavishdevar/librepods/composables/BatteryView.kt deleted file mode 100644 index 62893f700..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/BatteryView.kt +++ /dev/null @@ -1,235 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -@file:OptIn(ExperimentalEncodingApi::class) - -package me.kavishdevar.librepods.composables - -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.content.IntentFilter -import android.content.res.Configuration -import android.os.Build -import android.util.Log -import androidx.compose.foundation.Image -import androidx.compose.foundation.background -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.width -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.ImageBitmap -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.imageResource -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.constants.AirPodsNotifications -import me.kavishdevar.librepods.constants.Battery -import me.kavishdevar.librepods.constants.BatteryComponent -import me.kavishdevar.librepods.constants.BatteryStatus -import me.kavishdevar.librepods.services.AirPodsService -import kotlin.io.encoding.ExperimentalEncodingApi - -@Composable -fun BatteryView(service: AirPodsService, preview: Boolean = false) { - val batteryStatus = remember { mutableStateOf>(listOf()) } - - val previousBatteryStatus = remember { mutableStateOf>(listOf()) } - - @Suppress("DEPRECATION") val batteryReceiver = remember { - object : BroadcastReceiver() { - override fun onReceive(context: Context, intent: Intent) { - if (intent.action == AirPodsNotifications.BATTERY_DATA) { - batteryStatus.value = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableArrayListExtra("data", Battery::class.java) - } else { - intent.getParcelableArrayListExtra("data") - }?.toList() ?: listOf() - } - else if (intent.action == AirPodsNotifications.DISCONNECT_RECEIVERS) { - try { - context.unregisterReceiver(this) - } - catch (_: IllegalArgumentException) { - Log.wtf("BatteryReceiver", "Receiver already unregistered") - } - } - } - } - } - val context = LocalContext.current - - LaunchedEffect(context) { - val batteryIntentFilter = IntentFilter() - .apply { - addAction(AirPodsNotifications.BATTERY_DATA) - addAction(AirPodsNotifications.DISCONNECT_RECEIVERS) - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - context.registerReceiver( - batteryReceiver, - batteryIntentFilter, - Context.RECEIVER_EXPORTED - ) - } - } - - previousBatteryStatus.value = batteryStatus.value - batteryStatus.value = service.getBattery() - - if (preview) { - batteryStatus.value = listOf( - Battery(BatteryComponent.LEFT, 100, BatteryStatus.NOT_CHARGING), - Battery(BatteryComponent.RIGHT, 94, BatteryStatus.CHARGING), - Battery(BatteryComponent.CASE, 40, BatteryStatus.CHARGING) - ) - previousBatteryStatus.value = batteryStatus.value - } - - val left = batteryStatus.value.find { it.component == BatteryComponent.LEFT } - val right = batteryStatus.value.find { it.component == BatteryComponent.RIGHT } - val case = batteryStatus.value.find { it.component == BatteryComponent.CASE } - val leftLevel = left?.level ?: 0 - val rightLevel = right?.level ?: 0 - val caseLevel = case?.level ?: 0 - val leftCharging = left?.status == BatteryStatus.CHARGING - val rightCharging = right?.status == BatteryStatus.CHARGING - val caseCharging = case?.status == BatteryStatus.CHARGING - - val prevLeft = previousBatteryStatus.value.find { it.component == BatteryComponent.LEFT } - val prevRight = previousBatteryStatus.value.find { it.component == BatteryComponent.RIGHT } - val prevCase = previousBatteryStatus.value.find { it.component == BatteryComponent.CASE } - val prevLeftCharging = prevLeft?.status == BatteryStatus.CHARGING - val prevRightCharging = prevRight?.status == BatteryStatus.CHARGING - val prevCaseCharging = prevCase?.status == BatteryStatus.CHARGING - - val singleDisplayed = remember { mutableStateOf(false) } - - val airpodsInstance = service.airpodsInstance - if (airpodsInstance == null) { - return - } - val budsRes = airpodsInstance.model.budsRes - val caseRes = airpodsInstance.model.caseRes - - Row { - Column ( - modifier = Modifier - .fillMaxWidth(0.5f), - horizontalAlignment = Alignment.CenterHorizontally - ) { - Image ( - bitmap = ImageBitmap.imageResource(budsRes), - contentDescription = stringResource(R.string.buds), - modifier = Modifier - .fillMaxWidth() - .padding(8.dp) - ) - if ( - leftCharging == rightCharging && - (leftLevel - rightLevel) in -3..3 - ) - { - BatteryIndicator( - leftLevel.coerceAtMost(rightLevel), - leftCharging, - previousCharging = (prevLeftCharging && prevRightCharging) - ) - singleDisplayed.value = true - } - else { - singleDisplayed.value = false - Row ( - modifier = Modifier - .fillMaxWidth(), - horizontalArrangement = Arrangement.Center - ) { - if (leftLevel > 0 || left?.status != BatteryStatus.DISCONNECTED) { - BatteryIndicator( - leftLevel, - leftCharging, - "\uDBC6\uDCE5", - previousCharging = prevLeftCharging - ) - } - if (leftLevel > 0 && rightLevel > 0) - { - Spacer(modifier = Modifier.width(16.dp)) - } - if (rightLevel > 0 || right?.status != BatteryStatus.DISCONNECTED) - { - BatteryIndicator( - rightLevel, - rightCharging, - "\uDBC6\uDCE8", - previousCharging = prevRightCharging - ) - } - } - } - } - - Column ( - modifier = Modifier - .fillMaxWidth(), - horizontalAlignment = Alignment.CenterHorizontally - ) { - Image( - bitmap = ImageBitmap.imageResource(caseRes), - contentDescription = stringResource(R.string.case_alt), - modifier = Modifier - .fillMaxWidth() - .padding(8.dp) - ) - if (caseLevel > 0 || case?.status != BatteryStatus.DISCONNECTED) { - BatteryIndicator( - caseLevel, - caseCharging, - prefix = if (!singleDisplayed.value) "\uDBC3\uDE6C" else "", - previousCharging = prevCaseCharging - ) - } - } - } -} - -@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) -@Composable -fun BatteryViewPreview() { - val bg = if (isSystemInDarkTheme()) Color.Black else Color(0xFFF2F2F7) - Box( - modifier = Modifier.background(bg) - ) { - BatteryView(AirPodsService(), preview = true) - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/ConfirmationDialog.kt b/android/app/src/main/java/me/kavishdevar/librepods/composables/ConfirmationDialog.kt deleted file mode 100644 index 7a40f3d59..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/ConfirmationDialog.kt +++ /dev/null @@ -1,217 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.composables - -import androidx.compose.foundation.background -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxHeight -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.requiredWidthIn -import androidx.compose.foundation.layout.width -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.HorizontalDivider -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.MutableState -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.input.pointer.PointerEventType -import androidx.compose.ui.input.pointer.pointerInput -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.Font -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.compose.ui.window.Dialog -import dev.chrisbanes.haze.HazeState -import dev.chrisbanes.haze.hazeEffect -import dev.chrisbanes.haze.materials.CupertinoMaterials -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import me.kavishdevar.librepods.R - -@ExperimentalHazeMaterialsApi -@Composable -fun ConfirmationDialog( - showDialog: MutableState, - title: String, - message: String, - confirmText: String = "Enable", - dismissText: String = "Cancel", - onConfirm: () -> Unit, - onDismiss: () -> Unit = { showDialog.value = false }, - hazeState: HazeState, -) { - val isDarkTheme = isSystemInDarkTheme() - val textColor = if (isDarkTheme) Color.White else Color.Black - val accentColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5) - if (showDialog.value) { - Dialog(onDismissRequest = { showDialog.value = false }) { - Box( - modifier = Modifier - // .fillMaxWidth(0.75f) - .requiredWidthIn(min = 200.dp, max = 360.dp) - .background(Color.Transparent, RoundedCornerShape(14.dp)) - .clip(RoundedCornerShape(14.dp)) - .hazeEffect( - hazeState, - style = CupertinoMaterials.regular( - containerColor = if (isDarkTheme) Color(0xFF1C1C1E).copy(alpha = 0.95f) else Color.White.copy(alpha = 0.95f) - ) - ) - ) { - Column(horizontalAlignment = Alignment.CenterHorizontally) { - androidx.compose.foundation.layout.Spacer(modifier = Modifier.height(24.dp)) - Text( - title, - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Bold, - color = textColor, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - textAlign = TextAlign.Center, - modifier = Modifier.padding(horizontal = 16.dp) - ) - androidx.compose.foundation.layout.Spacer(modifier = Modifier.height(12.dp)) - Text( - message, - style = TextStyle( - fontSize = 14.sp, - color = textColor.copy(alpha = 0.8f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - textAlign = TextAlign.Center, - modifier = Modifier.padding(horizontal = 16.dp) - ) - androidx.compose.foundation.layout.Spacer(modifier = Modifier.height(16.dp)) - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier.fillMaxWidth() - ) - var leftPressed by remember { mutableStateOf(false) } - var rightPressed by remember { mutableStateOf(false) } - val pressedColor = if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9) - Row( - modifier = Modifier - .fillMaxWidth() - .height(48.dp) - .pointerInput(Unit) { - awaitPointerEventScope { - while (true) { - val event = awaitPointerEvent() - val position = event.changes.first().position - val width = size.width.toFloat() - val height = size.height.toFloat() - val isWithinBounds = position.y >= 0 && position.y <= height - val isLeft = position.x < width / 2 - event.changes.first().consume() - when (event.type) { - PointerEventType.Press -> { - if (isWithinBounds) { - leftPressed = isLeft - rightPressed = !isLeft - } else { - leftPressed = false - rightPressed = false - } - } - PointerEventType.Move -> { - if (isWithinBounds) { - leftPressed = isLeft - rightPressed = !isLeft - } else { - leftPressed = false - rightPressed = false - } - } - PointerEventType.Release -> { - if (isWithinBounds) { - if (leftPressed) { - onDismiss() - } else if (rightPressed) { - onConfirm() - } - } - leftPressed = false - rightPressed = false - } - } - } - } - }, - horizontalArrangement = Arrangement.Start, - verticalAlignment = Alignment.CenterVertically - ) { - Box( - modifier = Modifier - .weight(1f) - .fillMaxHeight() - .background(if (leftPressed) pressedColor else Color.Transparent), - contentAlignment = Alignment.Center - ) { - Text( - text = dismissText, - style = TextStyle( - color = accentColor, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - ) - } - Box( - modifier = Modifier - .width(1.dp) - .fillMaxHeight() - .background(Color(0x40888888)) - ) - Box( - modifier = Modifier - .weight(1f) - .fillMaxHeight() - .background(if (rightPressed) pressedColor else Color.Transparent), - contentAlignment = Alignment.Center - ) { - Text( - text = confirmText, - style = TextStyle( - color = accentColor, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - ) - } - } - } - } - } - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/ConnectionSettings.kt b/android/app/src/main/java/me/kavishdevar/librepods/composables/ConnectionSettings.kt deleted file mode 100644 index 4d07eeaae..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/ConnectionSettings.kt +++ /dev/null @@ -1,82 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -@file:OptIn(ExperimentalEncodingApi::class) - -package me.kavishdevar.librepods.composables - -import android.content.Context.MODE_PRIVATE -import androidx.compose.foundation.background -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.HorizontalDivider -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.utils.AACPManager -import kotlin.io.encoding.ExperimentalEncodingApi - -@Composable -fun ConnectionSettings() { - val isDarkTheme = isSystemInDarkTheme() - val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - - Column( - modifier = Modifier - .fillMaxWidth() - .background(backgroundColor, RoundedCornerShape(28.dp)) - .padding(top = 2.dp) - ) { - StyledToggle( - label = stringResource(R.string.ear_detection), - controlCommandIdentifier = AACPManager.Companion.ControlCommandIdentifiers.EAR_DETECTION_CONFIG, - sharedPreferenceKey = "automatic_ear_detection", - sharedPreferences = LocalContext.current.getSharedPreferences("settings", MODE_PRIVATE), - independent = false - ) - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(horizontal = 12.dp) - ) - - StyledToggle( - label = stringResource(R.string.automatically_connect), - description = stringResource(R.string.automatically_connect_description), - controlCommandIdentifier = AACPManager.Companion.ControlCommandIdentifiers.AUTOMATIC_CONNECTION_CONFIG, - sharedPreferenceKey = "automatic_connection_ctrl_cmd", - sharedPreferences = LocalContext.current.getSharedPreferences("settings", MODE_PRIVATE), - independent = false - ) - } -} - -@Preview -@Composable -fun ConnectionSettingsPreview() { - ConnectionSettings() -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/HearingHealthSettings.kt b/android/app/src/main/java/me/kavishdevar/librepods/composables/HearingHealthSettings.kt deleted file mode 100644 index 725acad05..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/HearingHealthSettings.kt +++ /dev/null @@ -1,109 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -@file:OptIn(ExperimentalEncodingApi::class) - -package me.kavishdevar.librepods.composables - -import androidx.compose.foundation.background -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.HorizontalDivider -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.navigation.NavController -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.NavigationButton -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.Capability -import kotlin.io.encoding.ExperimentalEncodingApi - -@Composable -fun HearingHealthSettings(navController: NavController) { - val service = ServiceManager.getService() - if (service == null) return - val airpodsInstance = service.airpodsInstance - if (airpodsInstance == null) return - if (airpodsInstance.model.capabilities.contains(Capability.HEARING_AID)) { - val isDarkTheme = isSystemInDarkTheme() - val textColor = if (isDarkTheme) Color.White else Color.Black - val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - - if (airpodsInstance.model.capabilities.contains(Capability.PPE)) { - Box( - modifier = Modifier - .background(if (isDarkTheme) Color(0xFF000000) else Color(0xFFF2F2F7)) - .padding(horizontal = 16.dp, vertical = 4.dp) - ){ - Text( - text = stringResource(R.string.hearing_health), - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f) - ) - ) - } - Column( - modifier = Modifier - .clip(RoundedCornerShape(28.dp)) - .fillMaxWidth() - .background(backgroundColor, RoundedCornerShape(28.dp)) - .padding(top = 2.dp) - ) { - NavigationButton( - to = "hearing_protection", - name = stringResource(R.string.hearing_protection), - navController = navController, - independent = false - ) - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(horizontal = 12.dp) - ) - - NavigationButton( - to = "hearing_aid", - name = stringResource(R.string.hearing_aid), - navController = navController, - independent = false - ) - } - } else { - NavigationButton( - to = "hearing_aid", - name = stringResource(R.string.hearing_aid), - navController = navController - ) - } - } -} \ No newline at end of file diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledToggle.kt b/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledToggle.kt deleted file mode 100644 index 2afd64b05..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledToggle.kt +++ /dev/null @@ -1,682 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -@file:OptIn(ExperimentalEncodingApi::class) - -package me.kavishdevar.librepods.composables - -import android.content.SharedPreferences -import android.util.Log -import androidx.compose.animation.animateColorAsState -import androidx.compose.animation.core.tween -import androidx.compose.foundation.background -import androidx.compose.foundation.clickable -import androidx.compose.foundation.gestures.detectTapGestures -import androidx.compose.foundation.interaction.MutableInteractionSource -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.MutableState -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.input.pointer.pointerInput -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.Font -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.core.content.edit -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.ATTHandles -import kotlin.io.encoding.ExperimentalEncodingApi - -@Composable -fun StyledToggle( - title: String? = null, - label: String, - description: String? = null, - checkedState: MutableState = remember { mutableStateOf(false) } , - sharedPreferenceKey: String? = null, - sharedPreferences: SharedPreferences? = null, - independent: Boolean = true, - enabled: Boolean = true, - onCheckedChange: ((Boolean) -> Unit)? = null, -) { - val isDarkTheme = isSystemInDarkTheme() - val textColor = if (isDarkTheme) Color.White else Color.Black - var checked by checkedState - var backgroundColor by remember { mutableStateOf(if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF)) } - val animatedBackgroundColor by animateColorAsState(targetValue = backgroundColor, animationSpec = tween(durationMillis = 500)) - if (sharedPreferenceKey != null && sharedPreferences != null) { - checked = sharedPreferences.getBoolean(sharedPreferenceKey, checked) - } - fun cb() { - if (sharedPreferences != null) { - if (sharedPreferenceKey == null) { - Log.e("StyledToggle", "SharedPreferenceKey is null but SharedPreferences is provided.") - return - } - sharedPreferences.edit { putBoolean(sharedPreferenceKey, checked) } - } - onCheckedChange?.invoke(checked) - } - - if (independent) { - Column(modifier = Modifier.padding(vertical = 8.dp)) { - if (title != null) { - Text( - text = title, - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f) - ), - modifier = Modifier.padding(start = 16.dp, end = 16.dp, top = 8.dp, bottom = 4.dp) - ) - } - Box( - modifier = Modifier - .background(animatedBackgroundColor, RoundedCornerShape(28.dp)) - .padding(4.dp) - .pointerInput(Unit) { - detectTapGestures( - onPress = { - backgroundColor = - if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9) - tryAwaitRelease() - backgroundColor = - if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - }, - onTap = { - if (enabled) { - checked = !checked - cb() - } - } - ) - } - ) { - Row( - modifier = Modifier - .fillMaxWidth() - .height(55.dp) - .padding(horizontal = 12.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Text( - text = label, - modifier = Modifier.weight(1f), - style = TextStyle( - fontSize = 16.sp, - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Normal, - color = textColor - ) - ) - StyledSwitch( - checked = checked, - enabled = enabled, - onCheckedChange = { - if (enabled) { - checked = it - cb() - } - } - ) - } - } - if (description != null) { - Spacer(modifier = Modifier.height(8.dp)) - Box( - modifier = Modifier - .padding(horizontal = 16.dp) - .background(if (isDarkTheme) Color(0xFF000000) else Color(0xFFF2F2F7)) - ) { - Text( - text = description, - style = TextStyle( - fontSize = 12.sp, - fontWeight = FontWeight.Light, - color = textColor.copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - ) - } - } - } - } else { - val isPressed = remember { mutableStateOf(false) } - Row( - modifier = Modifier - .fillMaxWidth() - .background( - shape = RoundedCornerShape(28.dp), - color = if (isPressed.value) Color(0xFFE0E0E0) else Color.Transparent - ) - .padding(16.dp) - .pointerInput(Unit) { - detectTapGestures( - onPress = { - isPressed.value = true - tryAwaitRelease() - isPressed.value = false - } - ) - } - .clickable( - indication = null, - interactionSource = remember { MutableInteractionSource() } - ) { - if (enabled) { - checked = !checked - cb() - } - }, - verticalAlignment = Alignment.CenterVertically - ) { - Column( - modifier = Modifier - .weight(1f) - .padding(end = 4.dp) - ) { - Text( - text = label, - style = TextStyle( - fontSize = 16.sp, - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Normal, - color = textColor - ) - ) - Spacer(modifier = Modifier.height(4.dp)) - if (description != null) { - Text( - text = description, - style = TextStyle( - fontSize = 12.sp, - color = textColor.copy(0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)), - ) - ) - } - } - StyledSwitch( - checked = checked, - enabled = enabled, - onCheckedChange = { - if (enabled) { - checked = it - cb() - } - } - ) - } - } -} - -@Composable -fun StyledToggle( - title: String? = null, - label: String, - description: String? = null, - controlCommandIdentifier: AACPManager.Companion.ControlCommandIdentifiers, - independent: Boolean = true, - enabled: Boolean = true, - sharedPreferenceKey: String? = null, - sharedPreferences: SharedPreferences? = null, - onCheckedChange: ((Boolean) -> Unit)? = null, -) { - val service = ServiceManager.getService() ?: return - val isDarkTheme = isSystemInDarkTheme() - val textColor = if (isDarkTheme) Color.White else Color.Black - val checkedValue = service.aacpManager.controlCommandStatusList.find { - it.identifier == controlCommandIdentifier - }?.value?.takeIf { it.isNotEmpty() }?.get(0) - var checked by remember { mutableStateOf(checkedValue == 1.toByte()) } - var backgroundColor by remember { mutableStateOf(if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF)) } - val animatedBackgroundColor by animateColorAsState(targetValue = backgroundColor, animationSpec = tween(durationMillis = 500)) - if (sharedPreferenceKey != null && sharedPreferences != null) { - checked = sharedPreferences.getBoolean(sharedPreferenceKey, checked) - } - fun cb() { - service.aacpManager.sendControlCommand(identifier = controlCommandIdentifier.value, value = checked) - if (sharedPreferences != null) { - if (sharedPreferenceKey == null) { - Log.e("StyledToggle", "SharedPreferenceKey is null but SharedPreferences is provided.") - return - } - sharedPreferences.edit { putBoolean(sharedPreferenceKey, checked) } - } - onCheckedChange?.invoke(checked) - } - - val listener = remember { - object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (controlCommand.identifier == controlCommandIdentifier.value) { - Log.d("StyledToggle", "Received control command for $label: ${controlCommand.value}") - checked = controlCommand.value.takeIf { it.isNotEmpty() }?.get(0) == 1.toByte() - } - } - } - } - LaunchedEffect(Unit) { - service.aacpManager.registerControlCommandListener(controlCommandIdentifier, listener) - } - DisposableEffect(Unit) { - onDispose { - service.aacpManager.unregisterControlCommandListener(controlCommandIdentifier, listener) - } - } - - if (independent) { - Column(modifier = Modifier.padding(vertical = 8.dp)) { - if (title != null) { - Text( - text = title, - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f) - ), - modifier = Modifier.padding(start = 16.dp, end = 16.dp, top = 8.dp, bottom = 4.dp) - ) - } - Box( - modifier = Modifier - .background(animatedBackgroundColor, RoundedCornerShape(28.dp)) - .padding(4.dp) - .pointerInput(Unit) { - detectTapGestures( - onPress = { - backgroundColor = - if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9) - tryAwaitRelease() - backgroundColor = - if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - }, - onTap = { - if (enabled) { - checked = !checked - cb() - } - } - ) - } - ) { - Row( - modifier = Modifier - .fillMaxWidth() - .height(55.dp) - .padding(horizontal = 12.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Text( - text = label, - modifier = Modifier.weight(1f), - style = TextStyle( - fontSize = 16.sp, - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Normal, - color = textColor - ) - ) - StyledSwitch( - checked = checked, - enabled = enabled, - onCheckedChange = { - if (enabled) { - checked = it - cb() - } - } - ) - } - } - if (description != null) { - Spacer(modifier = Modifier.height(8.dp)) - Box( - modifier = Modifier - .padding(horizontal = 16.dp) - .background(if (isDarkTheme) Color(0xFF000000) else Color(0xFFF2F2F7)) - ) { - Text( - text = description, - style = TextStyle( - fontSize = 12.sp, - fontWeight = FontWeight.Light, - color = textColor.copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - ) - } - } - } - } else { - val isPressed = remember { mutableStateOf(false) } - Row( - modifier = Modifier - .fillMaxWidth() - .background( - shape = RoundedCornerShape(28.dp), - color = if (isPressed.value) Color(0xFFE0E0E0) else Color.Transparent - ) - .padding(16.dp) - .pointerInput(Unit) { - detectTapGestures( - onPress = { - isPressed.value = true - tryAwaitRelease() - isPressed.value = false - } - ) - } - .clickable( - indication = null, - interactionSource = remember { MutableInteractionSource() } - ) { - if (enabled) { - checked = !checked - cb() - } - }, - verticalAlignment = Alignment.CenterVertically - ) { - Column( - modifier = Modifier - .weight(1f) - .padding(end = 4.dp) - ) { - Text( - text = label, - style = TextStyle( - fontSize = 16.sp, - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Normal, - color = textColor - ) - ) - Spacer(modifier = Modifier.height(4.dp)) - if (description != null) { - Text( - text = description, - style = TextStyle( - fontSize = 12.sp, - color = textColor.copy(0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)), - ) - ) - } - } - StyledSwitch( - checked = checked, - enabled = enabled, - onCheckedChange = { - if (enabled) { - checked = it - cb() - } - } - ) - } - } -} - -@Composable -fun StyledToggle( - title: String? = null, - label: String, - description: String? = null, - attHandle: ATTHandles, - independent: Boolean = true, - enabled: Boolean = true, - sharedPreferenceKey: String? = null, - sharedPreferences: SharedPreferences? = null, - onCheckedChange: ((Boolean) -> Unit)? = null, -) { - val attManager = ServiceManager.getService()?.attManager ?: return - val isDarkTheme = isSystemInDarkTheme() - val textColor = if (isDarkTheme) Color.White else Color.Black - val checkedValue = attManager.read(attHandle).getOrNull(0)?.toInt() - var checked by remember { mutableStateOf(checkedValue !=0) } - var backgroundColor by remember { mutableStateOf(if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF)) } - val animatedBackgroundColor by animateColorAsState(targetValue = backgroundColor, animationSpec = tween(durationMillis = 500)) - - attManager.enableNotifications(attHandle) - - if (sharedPreferenceKey != null && sharedPreferences != null) { - checked = sharedPreferences.getBoolean(sharedPreferenceKey, checked) - } - - fun cb() { - if (sharedPreferences != null) { - if (sharedPreferenceKey == null) { - Log.e("StyledToggle", "SharedPreferenceKey is null but SharedPreferences is provided.") - return - } - sharedPreferences.edit { putBoolean(sharedPreferenceKey, checked) } - } - onCheckedChange?.invoke(checked) - } - - LaunchedEffect(checked) { - if (attManager.socket?.isConnected != true) return@LaunchedEffect - attManager.write(attHandle, if (checked) byteArrayOf(1) else byteArrayOf(0)) - } - - val listener = remember { - object : (ByteArray) -> Unit { - override fun invoke(value: ByteArray) { - if (value.isNotEmpty()) { - checked = value[0].toInt() != 0 - Log.d("StyledToggle", "Updated from notification for $label: enabled=$checked") - } else { - Log.w("StyledToggle", "Empty value in notification for $label") - } - } - } - } - - LaunchedEffect(Unit) { - attManager.registerListener(attHandle, listener) - } - - DisposableEffect(Unit) { - onDispose { - attManager.unregisterListener(attHandle, listener) - } - } - - if (independent) { - Column(modifier = Modifier.padding(vertical = 8.dp)) { - if (title != null) { - Text( - text = title, - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f) - ), - modifier = Modifier.padding(start = 16.dp, end = 16.dp, top = 8.dp, bottom = 4.dp) - ) - } - Box( - modifier = Modifier - .background(animatedBackgroundColor, RoundedCornerShape(28.dp)) - .padding(4.dp) - .pointerInput(Unit) { - detectTapGestures( - onPress = { - backgroundColor = - if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9) - tryAwaitRelease() - backgroundColor = - if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - }, - onTap = { - if (enabled) { - checked = !checked - cb() - } - } - ) - } - ) { - Row( - modifier = Modifier - .fillMaxWidth() - .height(55.dp) - .padding(horizontal = 12.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Text( - text = label, - modifier = Modifier.weight(1f), - style = TextStyle( - fontSize = 16.sp, - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Normal, - color = textColor - ) - ) - StyledSwitch( - checked = checked, - enabled = enabled, - onCheckedChange = { - if (enabled) { - checked = it - cb() - } - } - ) - } - } - if (description != null) { - Spacer(modifier = Modifier.height(8.dp)) - Box( - modifier = Modifier - .padding(horizontal = 16.dp) - .background(if (isDarkTheme) Color(0xFF000000) else Color(0xFFF2F2F7)) - ) { - Text( - text = description, - style = TextStyle( - fontSize = 12.sp, - fontWeight = FontWeight.Light, - color = textColor.copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - ) - } - } - } - } else { - val isPressed = remember { mutableStateOf(false) } - Row( - modifier = Modifier - .fillMaxWidth() - .background( - shape = RoundedCornerShape(28.dp), - color = if (isPressed.value) Color(0xFFE0E0E0) else Color.Transparent - ) - .padding(16.dp) - .pointerInput(Unit) { - detectTapGestures( - onPress = { - isPressed.value = true - tryAwaitRelease() - isPressed.value = false - } - ) - } - .clickable( - indication = null, - interactionSource = remember { MutableInteractionSource() } - ) { - if (enabled) { - checked = !checked - cb() - } - }, - verticalAlignment = Alignment.CenterVertically - ) { - Column( - modifier = Modifier - .weight(1f) - .padding(end = 4.dp) - ) { - Text( - text = label, - fontSize = 16.sp, - color = textColor - ) - Spacer(modifier = Modifier.height(4.dp)) - if (description != null) { - Text( - text = description, - fontSize = 12.sp, - color = textColor.copy(0.6f), - lineHeight = 14.sp, - ) - } - } - StyledSwitch( - checked = checked, - enabled = enabled, - onCheckedChange = { - if (enabled) { - checked = it - cb() - } - } - ) - } - } -} - -@Preview -@Composable -fun StyledTogglePreview() { - val context = LocalContext.current - val sharedPrefs = context.getSharedPreferences("preview", 0) - StyledToggle( - label = "Example Toggle", - description = "This is an example description for the styled toggle.", - sharedPreferences = sharedPrefs - ) -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/constants/StemAction.kt b/android/app/src/main/java/me/kavishdevar/librepods/constants/StemAction.kt deleted file mode 100644 index 206fc3269..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/constants/StemAction.kt +++ /dev/null @@ -1,40 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.constants - -import me.kavishdevar.librepods.utils.AACPManager - -enum class StemAction { - PLAY_PAUSE, - PREVIOUS_TRACK, - NEXT_TRACK, - DIGITAL_ASSISTANT, - CYCLE_NOISE_CONTROL_MODES; - companion object { - fun fromString(action: String): StemAction? { - return entries.find { it.name == action } - } - val defaultActions: Map = mapOf( - AACPManager.Companion.StemPressType.SINGLE_PRESS to PLAY_PAUSE, - AACPManager.Companion.StemPressType.DOUBLE_PRESS to NEXT_TRACK, - AACPManager.Companion.StemPressType.TRIPLE_PRESS to PREVIOUS_TRACK, - AACPManager.Companion.StemPressType.LONG_PRESS to CYCLE_NOISE_CONTROL_MODES, - ) - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/data/AirPods.kt b/android/app/src/main/java/me/kavishdevar/librepods/data/AirPods.kt new file mode 100644 index 000000000..9d83f05f5 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/data/AirPods.kt @@ -0,0 +1,277 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.data + +import me.kavishdevar.librepods.R + +open class AirPodsBase( + val modelNumber: List, + val name: String, + val displayName: String = "AirPods", + val manufacturer: String = "Apple Inc.", + val budCaseRes: Int, + val budsRes: Int, + val leftBudsRes: Int, + val rightBudsRes: Int, + val caseRes: Int, + val capabilities: Set +) +enum class Capability { + LISTENING_MODE, + CONVERSATION_AWARENESS, + STEM_CONFIG, + HEAD_GESTURES, + LOUD_SOUND_REDUCTION, + PPE, + SLEEP_DETECTION, + HEARING_AID, + ADAPTIVE_AUDIO, + ADAPTIVE_VOLUME, + SWIPE_FOR_VOLUME, + HRM +} + +class AirPods: AirPodsBase( + modelNumber = listOf("A1523", "A1722"), + name = "AirPods 1", + // budCaseRes = R.drawable.airpods_1 + budCaseRes = R.drawable.airpods_pro_2, + // budsRes = R.drawable.airpods_1_buds + budsRes = R.drawable.airpods_pro_2_buds, + // leftBudsRes = R.drawable.airpods_1_left + leftBudsRes = R.drawable.airpods_pro_2_left, + // rightBudsRes = R.drawable.airpods_1_right + rightBudsRes = R.drawable.airpods_pro_2_right, + // caseRes = R.drawable.airpods_1_case + caseRes = R.drawable.airpods_pro_2_case, + capabilities = emptySet() +) + +class AirPods2: AirPodsBase( + modelNumber = listOf("A2032", "A2031"), + name = "AirPods 2", + // budCaseRes = R.drawable.airpods_2 + budCaseRes = R.drawable.airpods_pro_2, + // budsRes = R.drawable.airpods_2_buds + budsRes = R.drawable.airpods_pro_2_buds, + // leftBudsRes = R.drawable.airpods_2_left + leftBudsRes = R.drawable.airpods_pro_2_left, + // rightBudsRes = R.drawable.airpods_2_right + rightBudsRes = R.drawable.airpods_pro_2_right, + // caseRes = R.drawable.airpods_2_case + caseRes = R.drawable.airpods_pro_2_case, + capabilities = emptySet() +) + +class AirPods3: AirPodsBase( + modelNumber = listOf("A2565", "A2564"), + name = "AirPods 3", + // budCaseRes = R.drawable.airpods_3 + budCaseRes = R.drawable.airpods_pro_2, + // budsRes = R.drawable.airpods_3_buds + budsRes = R.drawable.airpods_pro_2_buds, + // leftBudsRes = R.drawable.airpods_3_left + leftBudsRes = R.drawable.airpods_pro_2_left, + // rightBudsRes = R.drawable.airpods_3_right + rightBudsRes = R.drawable.airpods_pro_2_right, + // caseRes = R.drawable.airpods_3_case + caseRes = R.drawable.airpods_pro_2_case, + capabilities = setOf( + Capability.HEAD_GESTURES + ) +) + +class AirPods4: AirPodsBase( + modelNumber = listOf("A3053", "A3050", "A3054"), + name = "AirPods 4", + // budCaseRes = R.drawable.airpods_4 + budCaseRes = R.drawable.airpods_pro_2, + // budsRes = R.drawable.airpods_4_buds + budsRes = R.drawable.airpods_pro_2_buds, + // leftBudsRes = R.drawable.airpods_4_left + leftBudsRes = R.drawable.airpods_pro_2_left, + // rightBudsRes = R.drawable.airpods_4_right + rightBudsRes = R.drawable.airpods_pro_2_right, + // caseRes = R.drawable.airpods_4_case + caseRes = R.drawable.airpods_pro_2_case, + capabilities = setOf( + Capability.HEAD_GESTURES, + Capability.SLEEP_DETECTION, + Capability.ADAPTIVE_VOLUME + ) +) + +class AirPods4ANC: AirPodsBase( + modelNumber = listOf("A3056", "A3055", "A3057"), + name = "AirPods 4 (ANC)", + // budCaseRes = R.drawable.airpods_4 + budCaseRes = R.drawable.airpods_pro_2, + // budsRes = R.drawable.airpods_4_buds + budsRes = R.drawable.airpods_pro_2_buds, + // leftBudsRes = R.drawable.airpods_4_left + leftBudsRes = R.drawable.airpods_pro_2_left, + // rightBudsRes = R.drawable.airpods_4_right + rightBudsRes = R.drawable.airpods_pro_2_right, + // caseRes = R.drawable.airpods_4_case + caseRes = R.drawable.airpods_pro_2_case, + capabilities = setOf( + Capability.LISTENING_MODE, + Capability.CONVERSATION_AWARENESS, + Capability.HEAD_GESTURES, + Capability.ADAPTIVE_AUDIO, + Capability.SLEEP_DETECTION, + Capability.ADAPTIVE_VOLUME, + Capability.STEM_CONFIG + ) +) + +class AirPodsPro1: AirPodsBase( + modelNumber = listOf("A2084", "A2083"), + name = "AirPods Pro 1", + displayName = "AirPods Pro", + // budCaseRes = R.drawable.airpods_pro_1 + budCaseRes = R.drawable.airpods_pro_2, + // budsRes = R.drawable.airpods_pro_1_buds + budsRes = R.drawable.airpods_pro_2_buds, + // leftBudsRes = R.drawable.airpods_pro_1_left + leftBudsRes = R.drawable.airpods_pro_2_left, + // rightBudsRes = R.drawable.airpods_pro_1_right + rightBudsRes = R.drawable.airpods_pro_2_right, + // caseRes = R.drawable.airpods_pro_1_case + caseRes = R.drawable.airpods_pro_2_case, + capabilities = setOf( + Capability.LISTENING_MODE + ) +) + +class AirPodsPro2Lightning: AirPodsBase( + modelNumber = listOf("A2931", "A2699", "A2698"), + name = "AirPods Pro 2 with Magsafe Charging Case (Lightning)", + displayName = "AirPods Pro", + // budCaseRes = R.drawable.airpods_pro_2 + budCaseRes = R.drawable.airpods_pro_2, + // budsRes = R.drawable.airpods_pro_2_buds + budsRes = R.drawable.airpods_pro_2_buds, + // leftBudsRes = R.drawable.airpods_pro_2_left + leftBudsRes = R.drawable.airpods_pro_2_left, + // rightBudsRes = R.drawable.airpods_pro_2_right + rightBudsRes = R.drawable.airpods_pro_2_right, + // caseRes = R.drawable.airpods_pro_2_case + caseRes = R.drawable.airpods_pro_2_case, + capabilities = setOf( + Capability.LISTENING_MODE, + Capability.CONVERSATION_AWARENESS, + Capability.STEM_CONFIG, + Capability.LOUD_SOUND_REDUCTION, + Capability.SLEEP_DETECTION, + Capability.HEARING_AID, + Capability.ADAPTIVE_AUDIO, + Capability.ADAPTIVE_VOLUME, + Capability.SWIPE_FOR_VOLUME, + Capability.HEAD_GESTURES + ) +) + +class AirPodsPro2USBC: AirPodsBase( + modelNumber = listOf("A3047", "A3048", "A3049"), + name = "AirPods Pro 2 with Magsafe Charging Case (USB-C)", + displayName = "AirPods Pro", + // budCaseRes = R.drawable.airpods_pro_2 + budCaseRes = R.drawable.airpods_pro_2, + // budsRes = R.drawable.airpods_pro_2_buds + budsRes = R.drawable.airpods_pro_2_buds, + // leftBudsRes = R.drawable.airpods_pro_2_left + leftBudsRes = R.drawable.airpods_pro_2_left, + // rightBudsRes = R.drawable.airpods_pro_2_right + rightBudsRes = R.drawable.airpods_pro_2_right, + // caseRes = R.drawable.airpods_pro_2_case + caseRes = R.drawable.airpods_pro_2_case, + capabilities = setOf( + Capability.LISTENING_MODE, + Capability.CONVERSATION_AWARENESS, + Capability.STEM_CONFIG, + Capability.LOUD_SOUND_REDUCTION, + Capability.SLEEP_DETECTION, + Capability.HEARING_AID, + Capability.ADAPTIVE_AUDIO, + Capability.ADAPTIVE_VOLUME, + Capability.SWIPE_FOR_VOLUME, + Capability.HEAD_GESTURES + ) +) + +class AirPodsPro3: AirPodsBase( + modelNumber = listOf("A3063", "A3064", "A3065"), + name = "AirPods Pro 3", + displayName = "AirPods Pro", + // budCaseRes = R.drawable.airpods_pro_3 + budCaseRes = R.drawable.airpods_pro_2, + // budsRes = R.drawable.airpods_pro_3_buds + budsRes = R.drawable.airpods_pro_2_buds, + // leftBudsRes = R.drawable.airpods_pro_3_left + leftBudsRes = R.drawable.airpods_pro_2_left, + // rightBudsRes = R.drawable.airpods_pro_3_right + rightBudsRes = R.drawable.airpods_pro_2_right, + // caseRes = R.drawable.airpods_pro_3_case + caseRes = R.drawable.airpods_pro_2_case, + capabilities = setOf( + Capability.LISTENING_MODE, + Capability.CONVERSATION_AWARENESS, + Capability.HEAD_GESTURES, + Capability.STEM_CONFIG, + Capability.LOUD_SOUND_REDUCTION, + Capability.PPE, + Capability.SLEEP_DETECTION, + Capability.HEARING_AID, + Capability.ADAPTIVE_AUDIO, + Capability.ADAPTIVE_VOLUME, + Capability.SWIPE_FOR_VOLUME, + Capability.HRM + ) +) + +data class AirPodsInstance( + val name: String, + val model: AirPodsBase, + val actualModelNumber: String, + val serialNumber: String?, + val leftSerialNumber: String?, + val rightSerialNumber: String?, + val version1: String?, + val version2: String?, + val version3: String?, +) + +object AirPodsModels { + val models: List = listOf( + AirPods(), + AirPods2(), + AirPods3(), + AirPods4(), + AirPods4ANC(), + AirPodsPro1(), + AirPodsPro2Lightning(), + AirPodsPro2USBC(), + AirPodsPro3() + ) + + fun getModelByModelNumber(modelNumber: String): AirPodsBase? { + return models.find { modelNumber in it.modelNumber } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/data/ControlCommandRepository.kt b/android/app/src/main/java/me/kavishdevar/librepods/data/ControlCommandRepository.kt new file mode 100644 index 000000000..9097a2eec --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/data/ControlCommandRepository.kt @@ -0,0 +1,70 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.data + +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.bluetooth.AACPManager.Companion.ControlCommandIdentifiers + +class ControlCommandRepository( + private val aacpManager: AACPManager +) { + fun getValue( + identifier: ControlCommandIdentifiers + ): ByteArray? { + return aacpManager.controlCommandStatusList + .find { it.identifier == identifier } + ?.value + } + + fun setValue( + id: ControlCommandIdentifiers, + value: ByteArray + ) { + aacpManager.sendControlCommand(id.value, value) + } + + + fun observe( + identifier: ControlCommandIdentifiers, + onChange: (ByteArray) -> Unit + ): AACPManager.ControlCommandListener { + + val listener = object : AACPManager.ControlCommandListener { + override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { + onChange(controlCommand.value) + } + } + + aacpManager.registerControlCommandListener(identifier, listener) + return listener + } + + fun remove( + identifier: ControlCommandIdentifiers, + listener: AACPManager.ControlCommandListener + ) { + aacpManager.unregisterControlCommandListener(identifier, listener) + } + + fun getMap(): Map { + return aacpManager.controlCommandStatusList.associate { + it.identifier to it.value + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/data/CustomEq.kt b/android/app/src/main/java/me/kavishdevar/librepods/data/CustomEq.kt new file mode 100644 index 000000000..38fe3b794 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/data/CustomEq.kt @@ -0,0 +1,27 @@ +package me.kavishdevar.librepods.data + +import me.kavishdevar.librepods.bluetooth.AACPManager + +enum class CustomEqBand { LOW, MID, HIGH } + +data class CustomEq(val state: Int, val low: Int, val mid: Int, val high: Int) { + + fun isEnabled(): Boolean { + return state == 2 + } + + fun toPacket(): ByteArray { + return byteArrayOf( + AACPManager.Companion.Opcodes.CUSTOM_EQ, 0x00, + 0x05, 0x00, // length (LE) + 0x01, state.toByte(), + low.toByte(), mid.toByte(), high.toByte() + ) + } + + init { + require(low in 0..100) { "low must be between 0 and 100, was $low" } + require(mid in 0..100) { "mid must be between 0 and 100, was $mid" } + require(high in 0..100) { "high must be between 0 and 100, was $high" } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/HearingAidEnums.kt b/android/app/src/main/java/me/kavishdevar/librepods/data/HearingAid.kt similarity index 86% rename from android/app/src/main/java/me/kavishdevar/librepods/utils/HearingAidEnums.kt rename to android/app/src/main/java/me/kavishdevar/librepods/data/HearingAid.kt index b405f8432..2e416f5c1 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/HearingAidEnums.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/data/HearingAid.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.utils + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.data import android.util.Log import androidx.compose.runtime.MutableState @@ -25,6 +25,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import me.kavishdevar.librepods.bluetooth.ATTHandles import java.io.IOException import java.nio.ByteBuffer import java.nio.ByteOrder @@ -136,15 +137,15 @@ fun parseHearingAidSettingsResponse(data: ByteArray): HearingAidSettings? { } fun sendHearingAidSettings( - attManager: ATTManager, + currentData: ByteArray, hearingAidSettings: HearingAidSettings, - debounceJob: MutableState + debounceJob: MutableState, + sender: (ATTHandles, ByteArray) -> Unit ) { debounceJob.value?.cancel() debounceJob.value = CoroutineScope(Dispatchers.IO).launch { delay(100) try { - val currentData = attManager.read(ATTHandles.HEARING_AID) Log.d(TAG, "Current data before update: ${currentData.joinToString(" ") { String.format("%02X", it) }}") if (currentData.size < 104) { Log.w(TAG, "Current data size ${currentData.size} too small, cannot send settings") @@ -182,7 +183,7 @@ fun sendHearingAidSettings( Log.d(TAG, "Sending updated settings: ${currentData.joinToString(" ") { String.format("%02X", it) }}") - attManager.write(ATTHandles.HEARING_AID, currentData) + sender(ATTHandles.HEARING_AID, currentData) } catch (e: IOException) { e.printStackTrace() } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/constants/Packets.kt b/android/app/src/main/java/me/kavishdevar/librepods/data/Packets.kt similarity index 86% rename from android/app/src/main/java/me/kavishdevar/librepods/constants/Packets.kt rename to android/app/src/main/java/me/kavishdevar/librepods/data/Packets.kt index 943f52b85..fe0232f3a 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/constants/Packets.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/data/Packets.kt @@ -1,29 +1,31 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.constants + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.data import android.os.Parcelable import android.util.Log import kotlinx.parcelize.Parcelize +// TODO: Remove everything but Battery-related stuff + enum class Enums(val value: ByteArray) { - NOISE_CANCELLATION(Capabilities.NOISE_CANCELLATION), + NOISE_CANCELLATION(byteArrayOf(0x0d)), PREFIX(byteArrayOf(0x04, 0x00, 0x04, 0x00)), SETTINGS(byteArrayOf(0x09, 0x00)), NOISE_CANCELLATION_PREFIX(PREFIX.value + SETTINGS.value + NOISE_CANCELLATION.value), @@ -40,6 +42,7 @@ object BatteryStatus { const val CHARGING = 1 const val NOT_CHARGING = 2 const val DISCONNECTED = 4 + const val OPTIMIZED_CHARGING = 5 } @Parcelize @@ -58,6 +61,7 @@ data class Battery(val component: Int, val level: Int, val status: Int) : Parcel BatteryStatus.CHARGING -> "CHARGING" BatteryStatus.NOT_CHARGING -> "NOT_CHARGING" BatteryStatus.DISCONNECTED -> "DISCONNECTED" + BatteryStatus.OPTIMIZED_CHARGING -> "OPTIMIZED_CHARGING" else -> null } } @@ -70,6 +74,7 @@ enum class NoiseControlMode { class AirPodsNotifications { companion object { const val AIRPODS_CONNECTED = "me.kavishdevar.librepods.AIRPODS_CONNECTED" + const val AIRPODS_L2CAP_CONNECTED = "me.kavishdevar.librepods.AIRPODS_CONNECTED" const val AIRPODS_DATA = "me.kavishdevar.librepods.AIRPODS_DATA" const val EAR_DETECTION_DATA = "me.kavishdevar.librepods.EAR_DETECTION_DATA" const val ANC_DATA = "me.kavishdevar.librepods.ANC_DATA" @@ -78,10 +83,12 @@ class AirPodsNotifications { const val AIRPODS_DISCONNECTED = "me.kavishdevar.librepods.AIRPODS_DISCONNECTED" const val AIRPODS_CONNECTION_DETECTED = "me.kavishdevar.librepods.AIRPODS_CONNECTION_DETECTED" const val DISCONNECT_RECEIVERS = "me.kavishdevar.librepods.DISCONNECT_RECEIVERS" + const val EQ_DATA = "me.kavishdevar.librepods.HEADPHONE_ACCOMMODATION" + const val AIRPODS_INFORMATION_UPDATED = "me.kavishdevar.librepods.AIRPODS_INFORMATION_UPDATED" } class EarDetection { - private val notificationBit = Capabilities.EAR_DETECTION + private val notificationBit = 6.toByte() private val notificationPrefix = Enums.PREFIX.value + notificationBit var status: List = listOf(0x01, 0x01) @@ -238,13 +245,6 @@ class AirPodsNotifications { } } -class Capabilities { - companion object { - val NOISE_CANCELLATION = byteArrayOf(0x0d) - val EAR_DETECTION = byteArrayOf(0x06) - } -} - fun isHeadTrackingData(data: ByteArray): Boolean { if (data.size <= 60) return false diff --git a/android/app/src/main/java/me/kavishdevar/librepods/data/StemAction.kt b/android/app/src/main/java/me/kavishdevar/librepods/data/StemAction.kt new file mode 100644 index 000000000..5bd9e6c86 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/data/StemAction.kt @@ -0,0 +1,40 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.data + +import me.kavishdevar.librepods.bluetooth.AACPManager + +enum class StemAction { + PLAY_PAUSE, + PREVIOUS_TRACK, + NEXT_TRACK, + DIGITAL_ASSISTANT, + CYCLE_NOISE_CONTROL_MODES; + companion object { + fun fromString(action: String): StemAction? { + return entries.find { it.name == action } + } + val defaultActions: Map = mapOf( + AACPManager.Companion.StemPressType.SINGLE_PRESS to PLAY_PAUSE, + AACPManager.Companion.StemPressType.DOUBLE_PRESS to NEXT_TRACK, + AACPManager.Companion.StemPressType.TRIPLE_PRESS to PREVIOUS_TRACK, + AACPManager.Companion.StemPressType.LONG_PRESS to CYCLE_NOISE_CONTROL_MODES, + ) + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/TransparencyUtils.kt b/android/app/src/main/java/me/kavishdevar/librepods/data/Transparency.kt similarity index 84% rename from android/app/src/main/java/me/kavishdevar/librepods/utils/TransparencyUtils.kt rename to android/app/src/main/java/me/kavishdevar/librepods/data/Transparency.kt index 0ceaa9ea6..c43e1cff0 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/TransparencyUtils.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/data/Transparency.kt @@ -1,28 +1,29 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.utils + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.data import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import me.kavishdevar.librepods.bluetooth.ATTHandles import java.io.IOException import java.nio.ByteBuffer import java.nio.ByteOrder @@ -82,7 +83,8 @@ data class TransparencySettings( } } -fun parseTransparencySettingsResponse(data: ByteArray): TransparencySettings { +fun parseTransparencySettingsResponse(data: ByteArray): TransparencySettings? { + if (data.size < 50) return null // 50 is arbitrary, too lazy to count val buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN) val enabled = buffer.float @@ -139,7 +141,7 @@ fun parseTransparencySettingsResponse(data: ByteArray): TransparencySettings { private var debounceJob: Job? = null -fun sendTransparencySettings(attManager: ATTManager, transparencySettings: TransparencySettings) { +fun sendTransparencySettings(writer: (ATTHandles, ByteArray) -> Unit, transparencySettings: TransparencySettings) { debounceJob?.cancel() debounceJob = CoroutineScope(Dispatchers.IO).launch { delay(100) @@ -171,7 +173,7 @@ fun sendTransparencySettings(attManager: ATTManager, transparencySettings: Trans } val data = buffer.array() - attManager.write(ATTHandles.TRANSPARENCY, value = data) + writer(ATTHandles.TRANSPARENCY, data) } catch (e: IOException) { e.printStackTrace() } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/data/XposedRemotePref.kt b/android/app/src/main/java/me/kavishdevar/librepods/data/XposedRemotePref.kt new file mode 100644 index 000000000..1977b0436 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/data/XposedRemotePref.kt @@ -0,0 +1,8 @@ +package me.kavishdevar.librepods.data + +interface XposedRemotePref { + fun isAvailable(): Boolean + + fun getBoolean(key: String, def: Boolean): Boolean + fun putBoolean(key: String, value: Boolean) +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/data/XposedRemotePrefImpl.kt b/android/app/src/main/java/me/kavishdevar/librepods/data/XposedRemotePrefImpl.kt new file mode 100644 index 000000000..112e7527c --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/data/XposedRemotePrefImpl.kt @@ -0,0 +1,21 @@ +package me.kavishdevar.librepods.data + +import androidx.core.content.edit +import me.kavishdevar.librepods.utils.XposedServiceHolder + +class XposedRemotePrefImpl: XposedRemotePref { + override fun isAvailable(): Boolean { + return XposedServiceHolder.service != null + } + + override fun getBoolean(key: String, def: Boolean): Boolean { + val s = XposedServiceHolder.service ?: return def + return s.getRemotePreferences("me.kavishdevar.librepods").getBoolean(key, def) + } + + override fun putBoolean(key: String, value: Boolean) { + val s = XposedServiceHolder.service ?: return + s.getRemotePreferences("me.kavishdevar.librepods") + .edit { putBoolean(key, value) } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/data/XposedRemotePrefProvider.kt b/android/app/src/main/java/me/kavishdevar/librepods/data/XposedRemotePrefProvider.kt new file mode 100644 index 000000000..9f18e8ca0 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/data/XposedRemotePrefProvider.kt @@ -0,0 +1,5 @@ +package me.kavishdevar.librepods.data + +object XposedRemotePrefProvider { + fun create(): XposedRemotePref = XposedRemotePrefImpl() +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/AboutCard.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/AboutCard.kt similarity index 77% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/AboutCard.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/AboutCard.kt index 264f941b2..f0669bada 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/AboutCard.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/AboutCard.kt @@ -1,29 +1,29 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) -package me.kavishdevar.librepods.composables +package me.kavishdevar.librepods.presentation.components import androidx.compose.foundation.background import androidx.compose.foundation.clickable -import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column @@ -34,35 +34,35 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color -import androidx.compose.ui.res.stringResource import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.navigation.NavController import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.NavigationButton -import me.kavishdevar.librepods.services.ServiceManager import kotlin.io.encoding.ExperimentalEncodingApi @Composable -fun AboutCard(navController: NavController) { +fun AboutCard( + navController: NavController, + modelName: String, + actualModel: String, + serialNumbers: List, + version: String? +) { val isDarkTheme = isSystemInDarkTheme() val textColor = if (isDarkTheme) Color.White else Color.Black - val service = ServiceManager.getService() - if (service == null) return - val airpodsInstance = service.airpodsInstance - if (airpodsInstance == null) return val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) Box( @@ -75,7 +75,8 @@ fun AboutCard(navController: NavController) { style = TextStyle( fontSize = 14.sp, fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f) + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) ) ) } @@ -108,7 +109,7 @@ fun AboutCard(navController: NavController) { ) ) Text( - text = airpodsInstance.model.displayName, + text = modelName, style = TextStyle( fontSize = 16.sp, color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy(alpha = 0.8f), @@ -137,7 +138,7 @@ fun AboutCard(navController: NavController) { ) ) Text( - text = airpodsInstance.actualModelNumber, + text = actualModel, style = TextStyle( fontSize = 16.sp, color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy(alpha = 0.8f), @@ -152,11 +153,11 @@ fun AboutCard(navController: NavController) { .padding(horizontal = 12.dp) ) val serialNumbers = listOf( - airpodsInstance.serialNumber?: "", - "􀀛 ${airpodsInstance.leftSerialNumber}", - "􀀧 ${airpodsInstance.rightSerialNumber}" + serialNumbers[0], + "􀀛 ${serialNumbers[1]}", + "􀀧 ${serialNumbers[2]}" ) - val serialNumber = remember { mutableStateOf(0) } + val serialNumber = remember { mutableIntStateOf(0) } Row( modifier = Modifier .fillMaxWidth() @@ -172,7 +173,7 @@ fun AboutCard(navController: NavController) { ), ) Text( - text = serialNumbers[serialNumber.value], + text = serialNumbers[serialNumber.intValue], style = TextStyle( fontSize = 16.sp, color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy(alpha = 0.8f), @@ -183,7 +184,7 @@ fun AboutCard(navController: NavController) { interactionSource = remember { MutableInteractionSource() }, indication = null ) { - serialNumber.value = (serialNumber.value + 1) % serialNumbers.size + serialNumber.intValue = (serialNumber.intValue + 1) % serialNumbers.size } ) } @@ -197,9 +198,9 @@ fun AboutCard(navController: NavController) { to = "version_info", navController = navController, name = stringResource(R.string.version), - currentState = airpodsInstance.version3, + currentState = version, independent = false, height = rowHeight.value + 32.dp ) } -} \ No newline at end of file +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/AppInfoCard.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/AppInfoCard.kt new file mode 100644 index 000000000..bc4d08712 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/AppInfoCard.kt @@ -0,0 +1,193 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components + +import androidx.compose.foundation.background +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.onGloballyPositioned +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import me.kavishdevar.librepods.BuildConfig +import me.kavishdevar.librepods.R + +@Composable +fun AppInfoCard() { + val rowHeight = remember { mutableStateOf(0.dp) } + val density = LocalDensity.current + val isDarkTheme = isSystemInDarkTheme() + val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + val textColor = if (isDarkTheme) Color.White else Color.Black + + Column { + Box( + modifier = Modifier + .background(if (isDarkTheme) Color.Black else Color(0xFFF2F2F7)) + .padding(start = 16.dp, bottom = 8.dp, end = 4.dp) + ) { + Text( + text = stringResource(R.string.about), style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + + Column( + modifier = Modifier + .clip(RoundedCornerShape(28.dp)) + .fillMaxWidth() + .background(backgroundColor, RoundedCornerShape(28.dp)) + ) { + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp) + .onGloballyPositioned { coordinates -> + rowHeight.value = with(density) { coordinates.size.height.toDp() } + }, + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text( + text = stringResource(R.string.version), style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = BuildConfig.VERSION_NAME, style = TextStyle( + fontSize = 16.sp, + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.8f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp), + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text( + text = stringResource(R.string.version_code), style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = BuildConfig.VERSION_CODE.toString(), style = TextStyle( + fontSize = 16.sp, + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.8f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp), + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text( + text = stringResource(R.string.flavor), style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = BuildConfig.FLAVOR, style = TextStyle( + fontSize = 16.sp, + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.8f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp), + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text( + text = stringResource(R.string.build_type), style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = BuildConfig.BUILD_TYPE, + style = TextStyle( + fontSize = 16.sp, + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.8f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/AudioSettings.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/AudioSettings.kt new file mode 100644 index 000000000..6d8b91ac1 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/AudioSettings.kt @@ -0,0 +1,198 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +@file:OptIn(ExperimentalEncodingApi::class) + +package me.kavishdevar.librepods.presentation.components + +import androidx.compose.foundation.background +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.navigation.NavController +import androidx.navigation.compose.rememberNavController +import me.kavishdevar.librepods.R +import kotlin.io.encoding.ExperimentalEncodingApi + +@Composable +fun AudioSettings( + navController: NavController, + adaptiveVolumeCapability: Boolean, + conversationalAwarenessCapability: Boolean, + loudSoundReductionCapability: Boolean, + adaptiveAudioCapability: Boolean, + customEqCapability: Boolean, + + adaptiveVolumeChecked: Boolean, + onAdaptiveVolumeCheckedChange: (Boolean) -> Unit, + + conversationalAwarenessChecked: Boolean, + onConversationalAwarenessCheckedChange: (Boolean) -> Unit, + + loudSoundReductionChecked: Boolean, + onLoudSoundReductionCheckedChange: (Boolean) -> Unit, + + vendorIdHook: Boolean, + isPremium: Boolean +) { + val isDarkTheme = isSystemInDarkTheme() + val textColor = if (isDarkTheme) Color.White else Color.Black + + if (!adaptiveVolumeCapability && !conversationalAwarenessCapability && !loudSoundReductionCapability && !adaptiveAudioCapability) { + return + } + Box( + modifier = Modifier + .background(if (isDarkTheme) Color(0xFF000000) else Color(0xFFF2F2F7)) + .padding(horizontal = 16.dp, vertical = 4.dp) + ){ + Text( + text = stringResource(R.string.audio), + style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + + val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + + Column( + modifier = Modifier + .clip(RoundedCornerShape(28.dp)) + .fillMaxWidth() + .background(backgroundColor, RoundedCornerShape(28.dp)) + .padding(top = 2.dp) + ) { + + if (adaptiveVolumeCapability) { + StyledToggle( + label = stringResource(R.string.personalized_volume), + description = stringResource(R.string.personalized_volume_description), + independent = false, + checked = adaptiveVolumeChecked, + onCheckedChange = onAdaptiveVolumeCheckedChange, + enabled = isPremium + ) + + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + } + + if (conversationalAwarenessCapability) { + StyledToggle( + label = stringResource(R.string.conversational_awareness), + description = stringResource(R.string.conversational_awareness_description), + independent = false, + checked = conversationalAwarenessChecked, + onCheckedChange = onConversationalAwarenessCheckedChange, + enabled = isPremium + ) + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + } + + if (loudSoundReductionCapability && vendorIdHook){ + StyledToggle( + label = stringResource(R.string.loud_sound_reduction), + description = stringResource(R.string.loud_sound_reduction_description), + independent = false, + checked = loudSoundReductionChecked, + onCheckedChange = onLoudSoundReductionCheckedChange, + enabled = isPremium + ) + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + } + + if (adaptiveAudioCapability) { + NavigationButton( + to = "adaptive_strength", + name = stringResource(R.string.adaptive_audio), + navController = navController, + independent = false + ) + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + } + if (customEqCapability) { + NavigationButton( + to = "equalizer_screen", + name = stringResource(R.string.equalizer), + navController = navController, + independent = false + ) + } + } +} + +@Preview +@Composable +fun AudioSettingsPreview() { + AudioSettings( + navController = rememberNavController(), + adaptiveVolumeCapability = true, + conversationalAwarenessCapability = true, + loudSoundReductionCapability = true, + adaptiveAudioCapability = true, + customEqCapability = true, + adaptiveVolumeChecked = true, + onAdaptiveVolumeCheckedChange = { }, + conversationalAwarenessChecked = true, + onConversationalAwarenessCheckedChange = { }, + loudSoundReductionChecked = true, + onLoudSoundReductionCheckedChange = { }, + vendorIdHook = true, + isPremium = true + ) +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/BatteryIndicator.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/BatteryIndicator.kt new file mode 100644 index 000000000..a2a5804e0 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/BatteryIndicator.kt @@ -0,0 +1,214 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components + + +import android.content.res.Configuration +import androidx.compose.animation.core.Animatable +import androidx.compose.animation.core.tween +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.background +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.scale +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Rect +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.StrokeCap +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.data.BatteryStatus +import kotlin.math.cos +import kotlin.math.min +import kotlin.math.sin +import kotlin.math.sqrt + +@Composable +fun BatteryIndicator( + batteryPercentage: Int, + status: Int, + prefix: String = "", + previousCharging: Boolean = false, +) { + val isDarkTheme = isSystemInDarkTheme() + val backgroundColor = if (isDarkTheme) Color.Black else Color(0xFFF2F2F7) + val batteryTextColor = if (isDarkTheme) Color.White else Color.Black + val batteryFillColor = + if (batteryPercentage > 25) if (isDarkTheme) Color(0xFF2ED158) else Color(0xFF35C759) + else if (isDarkTheme) Color(0xFFFC4244) else Color(0xFFfe373C) + + val initialScale = if (previousCharging) 1f else 0f + val scaleAnim = remember { Animatable(initialScale) } + val charging = status == BatteryStatus.CHARGING || status == BatteryStatus.OPTIMIZED_CHARGING + val targetScale = if (charging) 1f else 0f + + LaunchedEffect(previousCharging, charging) { + scaleAnim.animateTo(targetScale, animationSpec = tween(durationMillis = 250)) + } + + Column( + modifier = Modifier.background(backgroundColor).padding(4.dp), // just for haze to work + horizontalAlignment = Alignment.CenterHorizontally + ) { + Box( + modifier = Modifier.padding(bottom = 4.dp), contentAlignment = Alignment.Center + ) { + val strokeWidthPx = with(LocalDensity.current) { 4.dp.toPx() } + val gapFromCenterPx = with(LocalDensity.current) { 8.sp.toPx() } + + val trackColor = if (isDarkTheme) Color(0xFF272728) else Color(0xFFE3E3E8) + val optimizedLimit = 0.8f + val progress = batteryPercentage / 100f + + Canvas(modifier = Modifier.size(34.dp)) { + val startAngle = -90f + val stroke = Stroke(width = strokeWidthPx, cap = StrokeCap.Round) + val inset = strokeWidthPx / 2 + Rect( + left = inset, + top = inset, + right = size.width - inset, + bottom = size.height - inset + ) + val radius = size.minDimension / 2 + + if (status == BatteryStatus.OPTIMIZED_CHARGING) { + drawArc( + color = trackColor, + startAngle = startAngle, + sweepAngle = 360f * optimizedLimit, + useCenter = false, + style = stroke + ) + + val sweep = 360f * min(progress, optimizedLimit) + drawArc( + color = batteryFillColor, + startAngle = startAngle, + sweepAngle = sweep, + useCenter = false, + style = stroke + ) + + // ---- PILL MARKER AT 80% ---- + val angleDeg = startAngle + 360f * optimizedLimit + val angleRad = Math.toRadians(angleDeg.toDouble()) + + val arcRadius = radius - strokeWidthPx + + val outerX = center.x + arcRadius * cos(angleRad).toFloat() + val outerY = center.y + arcRadius * sin(angleRad).toFloat() + + val dirX = center.x - outerX + val dirY = center.y - outerY + val length = sqrt(dirX * dirX + dirY * dirY) + + val normX = dirX / length + val normY = dirY / length + + val startX = outerX - normX * strokeWidthPx / 2 + val startY = outerY - normY * strokeWidthPx / 2 + + val endX = center.x - normX * gapFromCenterPx + val endY = center.y - normY * gapFromCenterPx + + drawLine( + color = if (batteryPercentage >= 80) batteryFillColor else trackColor, + start = Offset(startX, startY), + end = Offset(endX, endY), + strokeWidth = strokeWidthPx, + cap = StrokeCap.Round + ) + } else { + drawArc( + color = trackColor, + startAngle = 0f, + sweepAngle = 360f, + useCenter = false, + style = stroke + ) + + drawArc( + color = batteryFillColor, + startAngle = startAngle, + sweepAngle = 360f * progress, + useCenter = false, + style = stroke + ) + } + } + + Text( + text = "\uDBC0\uDEE6", style = TextStyle( + fontSize = 14.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = batteryFillColor, + textAlign = TextAlign.Center + ), modifier = Modifier.scale(scaleAnim.value) + ) + } + + Spacer(modifier = Modifier.height(4.dp)) + + Text( + text = "$prefix $batteryPercentage%", + color = batteryTextColor, + style = TextStyle( + fontSize = 14.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + textAlign = TextAlign.Center + ), + ) + } +} + +@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) +@Composable +fun BatteryIndicatorPreview() { + val bg = if (isSystemInDarkTheme()) Color.Black else Color(0xFFF2F2F7) + Box( + modifier = Modifier.background(bg) + ) { + BatteryIndicator( + batteryPercentage = 50, + status = BatteryStatus.OPTIMIZED_CHARGING, + prefix = "\uDBC6\uDCE5", + previousCharging = false + ) + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/BatteryView.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/BatteryView.kt new file mode 100644 index 000000000..7accabee7 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/BatteryView.kt @@ -0,0 +1,177 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +@file:OptIn(ExperimentalEncodingApi::class) + +package me.kavishdevar.librepods.presentation.components + +import android.content.res.Configuration +import androidx.compose.foundation.Image +import androidx.compose.foundation.background +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.layout.widthIn +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.res.imageResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.data.Battery +import me.kavishdevar.librepods.data.BatteryComponent +import me.kavishdevar.librepods.data.BatteryStatus +import kotlin.io.encoding.ExperimentalEncodingApi + +@Composable +fun BatteryView( + batteryList: List, + budsRes: Int, + caseRes: Int +) { + val left = batteryList.find { it.component == BatteryComponent.LEFT } + val right = batteryList.find { it.component == BatteryComponent.RIGHT } + val case = batteryList.find { it.component == BatteryComponent.CASE } + + val leftLevel = left?.level ?: 0 + val rightLevel = right?.level ?: 0 + val caseLevel = case?.level ?: 0 + + val caseCharging = case?.status == BatteryStatus.CHARGING || + case?.status == BatteryStatus.OPTIMIZED_CHARGING + + val singleDisplayed = remember { mutableStateOf(false) } + + Box( + modifier = Modifier.fillMaxWidth(), + contentAlignment = Alignment.Center + ) { + Row( + modifier = Modifier.widthIn(max = 500.dp), + horizontalArrangement = Arrangement.Center + ) { + Column( + modifier = Modifier.weight(1f), + horizontalAlignment = Alignment.CenterHorizontally + ) { + Image( + bitmap = ImageBitmap.imageResource(budsRes), + contentDescription = stringResource(R.string.buds), + modifier = Modifier + .fillMaxWidth() + .padding(8.dp) + ) + + if ( + left?.status == right?.status && + (leftLevel - rightLevel) in -3..3 + ) { + BatteryIndicator( + leftLevel.coerceAtMost(rightLevel), + left?.status ?: BatteryStatus.NOT_CHARGING + ) + singleDisplayed.value = true + } else { + singleDisplayed.value = false + + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.Center + ) { + if (leftLevel > 0 || left?.status != BatteryStatus.DISCONNECTED) { + BatteryIndicator( + leftLevel, + left?.status ?: BatteryStatus.NOT_CHARGING, + "\uDBC6\uDCE5" + ) + } + + if (leftLevel > 0 && rightLevel > 0) { + Spacer(modifier = Modifier.width(16.dp)) + } + + if (rightLevel > 0 || right?.status != BatteryStatus.DISCONNECTED) { + BatteryIndicator( + rightLevel, + right?.status ?: BatteryStatus.NOT_CHARGING, + "\uDBC6\uDCE8" + ) + } + } + } + } + + Column( + modifier = Modifier.weight(1f), + horizontalAlignment = Alignment.CenterHorizontally + ) { + Image( + bitmap = ImageBitmap.imageResource(caseRes), + contentDescription = stringResource(R.string.case_alt), + modifier = Modifier + .fillMaxWidth() + .padding(8.dp) + ) + + if (caseLevel > 0 || case?.status != BatteryStatus.DISCONNECTED) { + BatteryIndicator( + caseLevel, + case?.status ?: BatteryStatus.NOT_CHARGING, + prefix = if (!singleDisplayed.value) "\uDBC3\uDE6C" else "" + ) + } + } + } + } +} + +@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) +@Composable +fun BatteryViewPreview() { + val fakeBattery = listOf( + Battery(BatteryComponent.LEFT, 85, BatteryStatus.CHARGING), + Battery(BatteryComponent.RIGHT, 40, BatteryStatus.OPTIMIZED_CHARGING), + Battery(BatteryComponent.CASE, 60, BatteryStatus.NOT_CHARGING) + ) + + val bg = if (isSystemInDarkTheme()) Color.Black else Color(0xFFF2F2F7) + + Box( + modifier = Modifier + .background(bg) + .padding(16.dp) + ) { + BatteryView( + batteryList = fakeBattery, + budsRes = R.drawable.airpods_pro_2_buds, + caseRes = R.drawable.airpods_pro_2_case + ) + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/CallControlSettings.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/CallControlSettings.kt similarity index 79% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/CallControlSettings.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/CallControlSettings.kt index 616e8ac14..2b00c06ed 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/CallControlSettings.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/CallControlSettings.kt @@ -1,24 +1,24 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) -package me.kavishdevar.librepods.composables +package me.kavishdevar.librepods.presentation.components import android.util.Log import androidx.compose.foundation.background @@ -36,39 +36,43 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color +import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.layout.positionInParent import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import dev.chrisbanes.haze.HazeState import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi +import kotlinx.coroutines.launch import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager import kotlin.io.encoding.ExperimentalEncodingApi @ExperimentalHazeMaterialsApi @Composable -fun CallControlSettings(hazeState: HazeState) { +fun CallControlSettings( + hazeState: HazeState, + flipped: Boolean, + onCallControlValueChanged: (Boolean) -> Unit +) { val isDarkTheme = isSystemInDarkTheme() val textColor = if (isDarkTheme) Color.White else Color.Black val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) @@ -82,7 +86,8 @@ fun CallControlSettings(hazeState: HazeState) { style = TextStyle( fontSize = 14.sp, fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f) + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) ) ) } @@ -93,24 +98,13 @@ fun CallControlSettings(hazeState: HazeState) { .background(backgroundColor, RoundedCornerShape(28.dp)) .padding(top = 2.dp) ) { - val service = ServiceManager.getService()!! - val callControlEnabledValue = service.aacpManager.controlCommandStatusList.find { - it.identifier == AACPManager.Companion.ControlCommandIdentifiers.CALL_MANAGEMENT_CONFIG - }?.value ?: byteArrayOf(0x00, 0x03) + + val scope = rememberCoroutineScope() + val haptics = LocalHapticFeedback.current val pressOnceText = stringResource(R.string.press_once) val pressTwiceText = stringResource(R.string.press_twice) - var flipped by remember { - mutableStateOf( - callControlEnabledValue.contentEquals( - byteArrayOf( - 0x00, - 0x02 - ) - ) - ) - } var singlePressAction by remember { mutableStateOf(if (flipped) pressTwiceText else pressOnceText) } var doublePressAction by remember { mutableStateOf(if (flipped) pressOnceText else pressTwiceText) } @@ -120,6 +114,7 @@ fun CallControlSettings(hazeState: HazeState) { var lastDismissTimeSingle by remember { mutableLongStateOf(0L) } var parentHoveredIndexSingle by remember { mutableStateOf(null) } var parentDragActiveSingle by remember { mutableStateOf(false) } + var previousIdxSingle by remember { mutableStateOf(null) } var showDoublePressDropdown by remember { mutableStateOf(false) } var touchOffsetDouble by remember { mutableStateOf(null) } @@ -127,36 +122,8 @@ fun CallControlSettings(hazeState: HazeState) { var lastDismissTimeDouble by remember { mutableLongStateOf(0L) } var parentHoveredIndexDouble by remember { mutableStateOf(null) } var parentDragActiveDouble by remember { mutableStateOf(false) } + var previousIdxDouble by remember { mutableStateOf(null) } - LaunchedEffect(Unit) { - val listener = object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (AACPManager.Companion.ControlCommandIdentifiers.fromByte(controlCommand.identifier) == - AACPManager.Companion.ControlCommandIdentifiers.CALL_MANAGEMENT_CONFIG - ) { - val newFlipped = controlCommand.value.contentEquals(byteArrayOf(0x00, 0x02)) - flipped = newFlipped - singlePressAction = if (newFlipped) pressTwiceText else pressOnceText - doublePressAction = if (newFlipped) pressOnceText else pressTwiceText - Log.d( - "CallControlSettings", - "Control command received, flipped: $newFlipped" - ) - } - } - } - - service.aacpManager.registerControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.CALL_MANAGEMENT_CONFIG, - listener - ) - } - - DisposableEffect(Unit) { - onDispose { - service.aacpManager.controlCommandListeners[AACPManager.Companion.ControlCommandIdentifiers.CALL_MANAGEMENT_CONFIG]?.clear() - } - } LaunchedEffect(flipped) { Log.d("CallControlSettings", "Call control flipped: $flipped") } @@ -231,7 +198,11 @@ fun CallControlSettings(hazeState: HazeState) { val touch = touchOffsetSingle ?: current val posInPopupY = current.y - touch.y val idx = (posInPopupY / itemHeightPx).toInt() + if (idx != previousIdxSingle) { + scope.launch { haptics.performHapticFeedback(HapticFeedbackType.SegmentTick) } + } parentHoveredIndexSingle = idx + previousIdxSingle = idx }, onDragEnd = { parentDragActiveSingle = false @@ -244,13 +215,13 @@ fun CallControlSettings(hazeState: HazeState) { if (option == pressOnceText) pressTwiceText else pressOnceText showSinglePressDropdown = false lastDismissTimeSingle = System.currentTimeMillis() - val bytes = if (option == pressOnceText) byteArrayOf( - 0x00, - 0x03 - ) else byteArrayOf(0x00, 0x02) - service.aacpManager.sendControlCommand(0x24, bytes) + onCallControlValueChanged(option != pressOnceText) + } } + if (parentHoveredIndexSingle != null && parentHoveredIndexSingle in 0..1) { + scope.launch { haptics.performHapticFeedback(HapticFeedbackType.GestureEnd) } + } parentHoveredIndexSingle = null }, onDragCancel = { @@ -313,11 +284,8 @@ fun CallControlSettings(hazeState: HazeState) { doublePressAction = if (option == pressOnceText) pressTwiceText else pressOnceText showSinglePressDropdown = false - val bytes = if (option == pressOnceText) byteArrayOf( - 0x00, - 0x03 - ) else byteArrayOf(0x00, 0x02) - service.aacpManager.sendControlCommand(0x24, bytes) + val flipped = option != pressOnceText + onCallControlValueChanged(flipped) }, hazeState = hazeState ) @@ -366,7 +334,11 @@ fun CallControlSettings(hazeState: HazeState) { val touch = touchOffsetDouble ?: current val posInPopupY = current.y - touch.y val idx = (posInPopupY / itemHeightPx).toInt() + if (idx != previousIdxDouble) { + scope.launch { haptics.performHapticFeedback(HapticFeedbackType.SegmentTick) } + } parentHoveredIndexDouble = idx + previousIdxDouble = idx }, onDragEnd = { parentDragActiveDouble = false @@ -379,13 +351,13 @@ fun CallControlSettings(hazeState: HazeState) { if (option == pressOnceText) pressTwiceText else pressOnceText showDoublePressDropdown = false lastDismissTimeDouble = System.currentTimeMillis() - val bytes = if (option == pressOnceText) byteArrayOf( - 0x00, - 0x02 - ) else byteArrayOf(0x00, 0x03) - service.aacpManager.sendControlCommand(0x24, bytes) + val flipped = option == pressOnceText + onCallControlValueChanged(flipped) } } + if (parentHoveredIndexDouble != null && parentHoveredIndexDouble in 0..1) { + scope.launch { haptics.performHapticFeedback(HapticFeedbackType.GestureEnd) } + } parentHoveredIndexDouble = null }, onDragCancel = { @@ -448,11 +420,8 @@ fun CallControlSettings(hazeState: HazeState) { singlePressAction = if (option == pressOnceText) pressTwiceText else pressOnceText showDoublePressDropdown = false - val bytes = if (option == pressOnceText) byteArrayOf( - 0x00, - 0x02 - ) else byteArrayOf(0x00, 0x03) - service.aacpManager.sendControlCommand(0x24, bytes) + val flipped = option == pressOnceText + onCallControlValueChanged(flipped) }, hazeState = hazeState ) @@ -461,10 +430,3 @@ fun CallControlSettings(hazeState: HazeState) { } } } - -@ExperimentalHazeMaterialsApi -@Preview -@Composable -fun CallControlSettingsPreview() { - CallControlSettings(HazeState()) -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ConfirmationDialog.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ConfirmationDialog.kt new file mode 100644 index 000000000..35e538ad9 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ConfirmationDialog.kt @@ -0,0 +1,179 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components + +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.animation.scaleIn +import androidx.compose.animation.scaleOut +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.requiredWidthIn +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.MutableState +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.kyant.backdrop.backdrops.LayerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import com.kyant.backdrop.drawBackdrop +import com.kyant.backdrop.effects.blur +import com.kyant.backdrop.effects.lens +import com.kyant.backdrop.effects.vibrancy +import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi +import me.kavishdevar.librepods.R + +@ExperimentalHazeMaterialsApi +@Composable +fun ConfirmationDialog( + showDialog: MutableState, + title: String, + message: String, + confirmText: String = "Enable", + dismissText: String = "Cancel", + onConfirm: () -> Unit, + onDismiss: () -> Unit = { showDialog.value = false }, + backdrop: LayerBackdrop, +) { + val isDarkTheme = isSystemInDarkTheme() + val textColor = if (isDarkTheme) Color.White else Color.Black + val accentColor = if (isDarkTheme) Color(0xFF0091FF) else Color(0xFF0088FF) + + AnimatedVisibility( + visible = showDialog.value, + enter = scaleIn(initialScale = 1.05f) + fadeIn(), + exit = scaleOut(targetScale = 1.05f) + fadeOut() + ) { + Box( + modifier = Modifier.fillMaxSize(), + contentAlignment = Alignment.Center + ) { + val innerBackdrop = rememberLayerBackdrop() + Box( + modifier = Modifier + .fillMaxSize() + .background(Color.Black.copy(alpha = 0.4f)) + .clickable(enabled = false, onClick = {}), + contentAlignment = Alignment.Center + ) { + Box( + modifier = Modifier + .requiredWidthIn(min = 200.dp, max = 360.dp) + .clip(RoundedCornerShape(48.dp)) + .drawBackdrop( + backdrop = backdrop, + exportedBackdrop = innerBackdrop, + shape = { RoundedCornerShape(48.dp) }, + effects = { + vibrancy() + blur(4f.dp.toPx()) + lens(12f.dp.toPx(), 48f.dp.toPx(), true) + }, + onDrawSurface = { + drawRect( + if (isDarkTheme) Color(0xFF1F1F1F).copy(alpha = 0.35f) else Color(0xFFE0E0E0).copy(alpha = 0.7f) + ) + })) { + Column(horizontalAlignment = Alignment.CenterHorizontally) { + Spacer(modifier = Modifier.height(24.dp)) + Text( + title, + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Bold, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), + textAlign = TextAlign.Center, + modifier = Modifier.padding(horizontal = 16.dp) + ) + Spacer(modifier = Modifier.height(12.dp)) + Text( + message, + style = TextStyle( + fontSize = 14.sp, + color = textColor.copy(alpha = 0.8f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), + textAlign = TextAlign.Center, + modifier = Modifier.padding(horizontal = 16.dp) + ) + Spacer(modifier = Modifier.height(16.dp)) + Row( + modifier = Modifier.fillMaxWidth(0.9f), + horizontalArrangement = Arrangement.spacedBy(24.dp) + ) { + StyledButton( + onClick = onDismiss, + backdrop = innerBackdrop, + modifier = Modifier.weight(1f), + ) { + Text( + text = dismissText, style = TextStyle( + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Medium, + fontSize = 14.sp, + color = textColor + ) + ) + } + StyledButton( + onClick = onConfirm, + backdrop = innerBackdrop, + modifier = Modifier.weight(1f), + surfaceColor = accentColor + ) { + Text( + text = confirmText, style = TextStyle( + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Medium, + fontSize = 14.sp, + color = Color.White + ) + ) + } + } + Spacer(modifier = Modifier.height(24.dp)) + } + } + } + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ConnectionSettings.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ConnectionSettings.kt new file mode 100644 index 000000000..ba5481852 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ConnectionSettings.kt @@ -0,0 +1,75 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +@file:OptIn(ExperimentalEncodingApi::class) + +package me.kavishdevar.librepods.presentation.components + +import androidx.compose.foundation.background +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.HorizontalDivider +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import me.kavishdevar.librepods.R +import kotlin.io.encoding.ExperimentalEncodingApi + +@Composable +fun ConnectionSettings( + automaticEarDetectionEnabled: Boolean, + onAutomaticEarDetectionChanged: (Boolean) -> Unit, + automaticConnectionEnabled: Boolean, + onAutomaticConnectionChanged: (Boolean) -> Unit, +) { + val isDarkTheme = isSystemInDarkTheme() + val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + + Column( + modifier = Modifier + .fillMaxWidth() + .background(backgroundColor, RoundedCornerShape(28.dp)) + .padding(top = 2.dp) + ) { + StyledToggle( + label = stringResource(R.string.ear_detection), + independent = false, + checked = automaticEarDetectionEnabled, + onCheckedChange = onAutomaticEarDetectionChanged + ) + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + + StyledToggle( + label = stringResource(R.string.automatically_connect), + description = stringResource(R.string.automatically_connect_description), + independent = false, + checked = automaticConnectionEnabled, + onCheckedChange = onAutomaticConnectionChanged + ) + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/ControlCenterButton.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ControlCenterButton.kt similarity index 78% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/ControlCenterButton.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ControlCenterButton.kt index 6de28766f..5340b0cae 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/ControlCenterButton.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ControlCenterButton.kt @@ -1,24 +1,24 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:Suppress("unused") -package me.kavishdevar.librepods.composables +package me.kavishdevar.librepods.presentation.components import androidx.compose.animation.animateColorAsState import androidx.compose.foundation.background diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/ControlCenterNoiseControlSegmentedButton.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ControlCenterNoiseControlSegmentedButton.kt similarity index 90% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/ControlCenterNoiseControlSegmentedButton.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ControlCenterNoiseControlSegmentedButton.kt index 743e918ad..ca0d1f4c8 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/ControlCenterNoiseControlSegmentedButton.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/ControlCenterNoiseControlSegmentedButton.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.composables + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components import androidx.compose.animation.core.Spring import androidx.compose.animation.core.animateDpAsState @@ -56,7 +56,7 @@ import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.constants.NoiseControlMode +import me.kavishdevar.librepods.data.NoiseControlMode private val ContainerColor = Color(0x593C3C3E) private val SelectedIndicatorColorGray = Color(0xFF6C6C6E) diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/DeviceInfoCard.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/DeviceInfoCard.kt new file mode 100644 index 000000000..18ab494d3 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/DeviceInfoCard.kt @@ -0,0 +1,235 @@ +package me.kavishdevar.librepods.presentation.components + +import android.os.Build +import androidx.compose.foundation.background +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.onGloballyPositioned +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.utils.XposedState + +@Composable +fun DeviceInfoCard() { + val isDarkTheme = isSystemInDarkTheme() + + val textColor = if (isDarkTheme) Color.White else Color.Black + val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + + val rowHeight = remember { mutableStateOf(0.dp) } + val density = LocalDensity.current + + Column ( + verticalArrangement = Arrangement.spacedBy(8.dp) + ) { + Box( + modifier = Modifier + .background(if (isDarkTheme) Color.Black else Color(0xFFF2F2F7)) + .padding(start = 16.dp, top = 24.dp, end = 4.dp) + ) { + Text( + text = stringResource(R.string.device_info), style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + Column( + modifier = Modifier + .clip(RoundedCornerShape(28.dp)) + .fillMaxWidth() + .background(backgroundColor, RoundedCornerShape(28.dp)) + ) { + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp) + .onGloballyPositioned { coordinates -> + rowHeight.value = with(density) { coordinates.size.height.toDp() } + }, + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text( + text = stringResource(R.string.manufacturer), style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = Build.MANUFACTURER, style = TextStyle( + fontSize = 16.sp, + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.8f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp), + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text( + text = stringResource(R.string.model_number), style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = Build.MODEL, style = TextStyle( + fontSize = 16.sp, + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.8f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp), + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text( + text = stringResource(R.string.build_id), style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = Build.DISPLAY, style = TextStyle( + fontSize = 16.sp, + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.8f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp), + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text( + text = stringResource(R.string.version), style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = Build.ID + " (${Build.VERSION.SDK_INT_FULL})", + style = TextStyle( + fontSize = 16.sp, + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.8f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp), + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text( + text = stringResource(R.string.xposed_available), style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = if (XposedState.isAvailable) stringResource(R.string.yes) else stringResource(R.string.no), style = TextStyle( + fontSize = 16.sp, + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.8f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp), + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text( + text = stringResource(R.string.app_enabled_in_xposed), style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = if (XposedState.bluetoothScopeEnabled) stringResource(R.string.yes) else stringResource(R.string.no), style = TextStyle( + fontSize = 16.sp, + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.8f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/HearingHealthSettings.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/HearingHealthSettings.kt new file mode 100644 index 000000000..a444ddae8 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/HearingHealthSettings.kt @@ -0,0 +1,118 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +@file:OptIn(ExperimentalEncodingApi::class) + +package me.kavishdevar.librepods.presentation.components + +import androidx.compose.foundation.background +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.navigation.NavController +import me.kavishdevar.librepods.R +import kotlin.io.encoding.ExperimentalEncodingApi + +@Composable +fun HearingHealthSettings( + navController: NavController, + hasPPECapability: Boolean, + hasHearingAidCapability: Boolean, + vendorIdHook: Boolean +) { + val isDarkTheme = isSystemInDarkTheme() + val textColor = if (isDarkTheme) Color.White else Color.Black + val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + val shouldShowHearingAid = hasHearingAidCapability && vendorIdHook + + if (hasPPECapability && shouldShowHearingAid) { + Box( + modifier = Modifier + .background(if (isDarkTheme) Color(0xFF000000) else Color(0xFFF2F2F7)) + .padding(horizontal = 16.dp, vertical = 4.dp) + ){ + Text( + text = stringResource(R.string.hearing_health), + style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + Column( + modifier = Modifier + .clip(RoundedCornerShape(28.dp)) + .fillMaxWidth() + .background(backgroundColor, RoundedCornerShape(28.dp)) + .padding(top = 2.dp) + ) { + NavigationButton( + to = "hearing_protection", + name = stringResource(R.string.hearing_protection), + navController = navController, + independent = false + ) + + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + + + NavigationButton( + to = "hearing_aid", + name = stringResource(R.string.hearing_aid), + navController = navController, + independent = false + ) + } + } else if (shouldShowHearingAid) { + NavigationButton( + to = "hearing_aid", + name = stringResource(R.string.hearing_aid), + navController = navController + ) + } else if (hasPPECapability) { + NavigationButton( + to = "hearing_protection", + name = stringResource(R.string.hearing_protection), + title = stringResource(R.string.hearing_health), + navController = navController + ) + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/MicrophoneSettings.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/MicrophoneSettings.kt similarity index 74% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/MicrophoneSettings.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/MicrophoneSettings.kt index 2c1b4a086..5f693e9b9 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/MicrophoneSettings.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/MicrophoneSettings.kt @@ -1,26 +1,25 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) -package me.kavishdevar.librepods.composables +package me.kavishdevar.librepods.presentation.components -import android.util.Log import androidx.compose.foundation.background import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress import androidx.compose.foundation.gestures.detectTapGestures @@ -35,38 +34,41 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color +import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.layout.positionInParent import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import dev.chrisbanes.haze.HazeState import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi +import kotlinx.coroutines.launch import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager import kotlin.io.encoding.ExperimentalEncodingApi @ExperimentalHazeMaterialsApi @Composable -fun MicrophoneSettings(hazeState: HazeState) { +fun MicrophoneSettings( + hazeState: HazeState, + micModeValue: Byte, + onMicModeValueChanged: (Byte) -> Unit +) { val isDarkTheme = isSystemInDarkTheme() val textColor = if (isDarkTheme) Color.White else Color.Black val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) @@ -77,11 +79,6 @@ fun MicrophoneSettings(hazeState: HazeState) { .background(backgroundColor, RoundedCornerShape(28.dp)) .padding(top = 2.dp) ) { - val service = ServiceManager.getService()!! - val micModeValue = service.aacpManager.controlCommandStatusList.find { - it.identifier == AACPManager.Companion.ControlCommandIdentifiers.MIC_MODE - }?.value?.get(0) ?: 0x00.toByte() - var selectedMode by remember { mutableStateOf( when (micModeValue) { @@ -98,42 +95,13 @@ fun MicrophoneSettings(hazeState: HazeState) { var lastDismissTime by remember { mutableLongStateOf(0L) } val reopenThresholdMs = 250L - val listener = object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (AACPManager.Companion.ControlCommandIdentifiers.fromByte(controlCommand.identifier) == - AACPManager.Companion.ControlCommandIdentifiers.MIC_MODE - ) { - selectedMode = when (controlCommand.value[0]) { - 0x00.toByte() -> "Automatic" - 0x01.toByte() -> "Always Right" - 0x02.toByte() -> "Always Left" - else -> "Automatic" - } - Log.d("MicrophoneSettings", "Microphone mode received: $selectedMode") - } - } - } - - LaunchedEffect(Unit) { - service.aacpManager.registerControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.MIC_MODE, - listener - ) - } - - DisposableEffect(Unit) { - onDispose { - service.aacpManager.unregisterControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.MIC_MODE, - listener - ) - } - } - val density = LocalDensity.current val itemHeightPx = with(density) { 48.dp.toPx() } var parentHoveredIndex by remember { mutableStateOf(null) } var parentDragActive by remember { mutableStateOf(false) } + var previousIdx by remember { mutableStateOf(null) } + val haptics = LocalHapticFeedback.current + val scope = rememberCoroutineScope() val microphoneAutomaticText = stringResource(R.string.microphone_automatic) val microphoneAlwaysRightText = stringResource(R.string.microphone_always_right) val microphoneAlwaysLeftText = stringResource(R.string.microphone_always_left) @@ -173,7 +141,11 @@ fun MicrophoneSettings(hazeState: HazeState) { val touch = touchOffset ?: current val posInPopupY = current.y - touch.y val idx = (posInPopupY / itemHeightPx).toInt() + if (idx != previousIdx) { + scope.launch { haptics.performHapticFeedback(HapticFeedbackType.SegmentTick) } + } parentHoveredIndex = idx + previousIdx = idx }, onDragEnd = { parentDragActive = false @@ -194,12 +166,16 @@ fun MicrophoneSettings(hazeState: HazeState) { options[2] -> 0x02 else -> 0x00 } - service.aacpManager.sendControlCommand( - AACPManager.Companion.ControlCommandIdentifiers.MIC_MODE.value, - byteArrayOf(byteValue.toByte()) - ) +// service.aacpManager.sendControlCommand( +// AACPManager.Companion.ControlCommandIdentifiers.MIC_MODE.value, +// byteArrayOf(byteValue.toByte()) +// ) + onMicModeValueChanged(byteValue.toByte()) } } + if (parentHoveredIndex != null && parentHoveredIndex in 0..2) { + scope.launch { haptics.performHapticFeedback(HapticFeedbackType.GestureEnd) } + } parentHoveredIndex = null }, onDragCancel = { @@ -277,10 +253,7 @@ fun MicrophoneSettings(hazeState: HazeState) { microphoneAlwaysLeftText -> 0x02 else -> 0x00 } - service.aacpManager.sendControlCommand( - AACPManager.Companion.ControlCommandIdentifiers.MIC_MODE.value, - byteArrayOf(byteValue.toByte()) - ) + onMicModeValueChanged(byteValue.toByte()) }, hazeState = hazeState ) @@ -288,10 +261,3 @@ fun MicrophoneSettings(hazeState: HazeState) { } } } - -@ExperimentalHazeMaterialsApi -@Preview -@Composable -fun MicrophoneSettingsPreview() { - MicrophoneSettings(HazeState()) -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/NavigationButton.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/NavigationButton.kt similarity index 72% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/NavigationButton.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/NavigationButton.kt index 8d96a54d0..ceff731a2 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/NavigationButton.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/NavigationButton.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.composables + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components import androidx.compose.animation.animateColorAsState import androidx.compose.animation.core.tween @@ -35,21 +35,25 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.navigation.NavController +import kotlinx.coroutines.launch import me.kavishdevar.librepods.R @Composable @@ -62,10 +66,14 @@ fun NavigationButton( description: String? = null, currentState: String? = null, height: Dp = 58.dp, + enabled: Boolean = true ) { val isDarkTheme = isSystemInDarkTheme() var backgroundColor by remember { mutableStateOf(if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF)) } val animatedBackgroundColor by animateColorAsState(targetValue = backgroundColor, animationSpec = tween(durationMillis = 500)) + val haptics = LocalHapticFeedback.current + val scope = rememberCoroutineScope() + Column { if (title != null) { Box( @@ -79,23 +87,34 @@ fun NavigationButton( fontSize = 14.sp, fontWeight = FontWeight.Bold, color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) ) ) } } Row( modifier = Modifier - .background(animatedBackgroundColor, RoundedCornerShape(if (independent) 28.dp else 0.dp)) + .background( + animatedBackgroundColor, + RoundedCornerShape(if (independent) 28.dp else 0.dp) + ) .height(height) .pointerInput(Unit) { detectTapGestures( onPress = { - backgroundColor = if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9) - tryAwaitRelease() - backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + if (enabled) { + backgroundColor = + if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9) + tryAwaitRelease() + backgroundColor = + if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + } }, onTap = { - if (onClick != null) onClick() else navController.navigate(to) + if (enabled) { + scope.launch { haptics.performHapticFeedback(HapticFeedbackType.ContextClick) } + if (onClick != null) onClick() else navController.navigate(to) + } } ) } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/NoiseControlButton.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/NoiseControlButton.kt similarity index 69% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/NoiseControlButton.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/NoiseControlButton.kt index 504c9d053..a5b880f52 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/NoiseControlButton.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/NoiseControlButton.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.composables + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components import androidx.compose.foundation.clickable import androidx.compose.foundation.interaction.MutableInteractionSource @@ -74,4 +74,4 @@ fun NoiseControlButtonPreview() { onClick = {}, textColor = Color.White, ) -} \ No newline at end of file +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/NoiseControlSettings.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/NoiseControlSettings.kt similarity index 74% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/NoiseControlSettings.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/NoiseControlSettings.kt index 1dd01b4d2..453c35c76 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/NoiseControlSettings.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/NoiseControlSettings.kt @@ -1,31 +1,26 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) -package me.kavishdevar.librepods.composables +package me.kavishdevar.librepods.presentation.components import android.annotation.SuppressLint -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.content.IntentFilter -import android.os.Build import androidx.compose.animation.core.AnimationSpec import androidx.compose.animation.core.Spring import androidx.compose.animation.core.SpringSpec @@ -60,64 +55,49 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ImageBitmap -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.res.imageResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.zIndex import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.constants.AirPodsNotifications -import me.kavishdevar.librepods.constants.NoiseControlMode -import me.kavishdevar.librepods.services.AirPodsService -import me.kavishdevar.librepods.utils.AACPManager +import me.kavishdevar.librepods.data.NoiseControlMode import kotlin.io.encoding.ExperimentalEncodingApi import kotlin.math.roundToInt @SuppressLint("UnspecifiedRegisterReceiverFlag", "UnusedBoxWithConstraintsScope") @Composable fun NoiseControlSettings( - service: AirPodsService, + showOffListeningMode: Boolean, + noiseControlModeValue: Int, + onNoiseControlModeChanged: (Int) -> Unit ) { - val context = LocalContext.current - val offListeningModeConfigValue = service.aacpManager.controlCommandStatusList.find { - it.identifier == AACPManager.Companion.ControlCommandIdentifiers.ALLOW_OFF_OPTION - }?.value?.takeIf { it.isNotEmpty() }?.get(0) == 1.toByte() - val offListeningMode = remember { mutableStateOf(offListeningModeConfigValue) } - - val offListeningModeListener = object: AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - offListeningMode.value = controlCommand.value[0] == 1.toByte() - } - } - - service.aacpManager.registerControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.ALLOW_OFF_OPTION, - offListeningModeListener - ) - val isDarkTheme = isSystemInDarkTheme() val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFE3E3E8) val textColor = if (isDarkTheme) Color.White else Color.Black val textColorSelected = if (isDarkTheme) Color.White else Color.Black val selectedBackground = if (isDarkTheme) Color(0xBF5C5A5F) else Color(0xFFFFFFFF) + + val noiseControlMode = remember { mutableStateOf(NoiseControlMode.OFF) } val d1a = remember { mutableFloatStateOf(0f) } val d2a = remember { mutableFloatStateOf(0f) } val d3a = remember { mutableFloatStateOf(0f) } + // this function exists solely for the dividers, should get rid of it fun onModeSelected(mode: NoiseControlMode, received: Boolean = false) { val previousMode = noiseControlMode.value - val targetMode = if (!offListeningMode.value && mode == NoiseControlMode.OFF) { + val targetMode = if (!showOffListeningMode && mode == NoiseControlMode.OFF) { NoiseControlMode.TRANSPARENCY } else { mode @@ -125,9 +105,8 @@ fun NoiseControlSettings( noiseControlMode.value = targetMode - if (!received && targetMode != previousMode) { - service.aacpManager.sendControlCommand(identifier = AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE.value, value = targetMode.ordinal + 1) - } + if (!received && targetMode != previousMode) onNoiseControlModeChanged(targetMode.ordinal + 1) + when (noiseControlMode.value) { NoiseControlMode.NOISE_CANCELLATION -> { @@ -153,32 +132,12 @@ fun NoiseControlSettings( } } - val noiseControlReceiver = remember { - object : BroadcastReceiver() { - override fun onReceive(context: Context, intent: Intent) { - if (intent.action == AirPodsNotifications.ANC_DATA) { - noiseControlMode.value = NoiseControlMode.entries.toTypedArray()[intent.getIntExtra("data", 3) - 1] - onModeSelected(noiseControlMode.value, true) - } else if (intent.action == AirPodsNotifications.DISCONNECT_RECEIVERS) { - try { - context.unregisterReceiver(this) - } catch (e: IllegalArgumentException) { - e.printStackTrace() - } - } - } - } - } - val noiseControlIntentFilter = IntentFilter().apply { - addAction(AirPodsNotifications.ANC_DATA) - addAction(AirPodsNotifications.DISCONNECT_RECEIVERS) - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - context.registerReceiver(noiseControlReceiver, noiseControlIntentFilter, Context.RECEIVER_EXPORTED) - } else { - context.registerReceiver(noiseControlReceiver, noiseControlIntentFilter) - } + val index = (noiseControlModeValue - 1).coerceIn(0, NoiseControlMode.entries.size - 1) + noiseControlMode.value = NoiseControlMode.entries[index] + + onModeSelected(noiseControlMode.value, received = true) + Box( modifier = Modifier .background(if (isDarkTheme) Color(0xFF000000) else Color(0xFFF2F2F7)) @@ -190,17 +149,18 @@ fun NoiseControlSettings( fontSize = 14.sp, fontWeight = FontWeight.Bold, color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) ) ) } - @Suppress("COMPOSE_APPLIER_CALL_MISMATCH") + BoxWithConstraints( modifier = Modifier .fillMaxWidth() .padding(vertical = 8.dp) ) { val density = LocalDensity.current - val buttonCount = if (offListeningMode.value) 4 else 3 + val buttonCount = if (showOffListeningMode) 4 else 3 val buttonWidth = maxWidth / buttonCount val isDragging = remember { mutableStateOf(false) } @@ -208,10 +168,10 @@ fun NoiseControlSettings( mutableFloatStateOf( with(density) { when(noiseControlMode.value) { - NoiseControlMode.OFF -> if (offListeningMode.value) 0f else buttonWidth.toPx() - NoiseControlMode.TRANSPARENCY -> if (offListeningMode.value) buttonWidth.toPx() else 0f - NoiseControlMode.ADAPTIVE -> if (offListeningMode.value) (buttonWidth * 2).toPx() else buttonWidth.toPx() - NoiseControlMode.NOISE_CANCELLATION -> if (offListeningMode.value) (buttonWidth * 3).toPx() else (buttonWidth * 2).toPx() + NoiseControlMode.OFF -> if (showOffListeningMode) 0f else buttonWidth.toPx() + NoiseControlMode.TRANSPARENCY -> if (showOffListeningMode) buttonWidth.toPx() else 0f + NoiseControlMode.ADAPTIVE -> if (showOffListeningMode) (buttonWidth * 2).toPx() else buttonWidth.toPx() + NoiseControlMode.NOISE_CANCELLATION -> if (showOffListeningMode) (buttonWidth * 3).toPx() else (buttonWidth * 2).toPx() } } ) @@ -224,10 +184,10 @@ fun NoiseControlSettings( ) val targetOffset = buttonWidth * when(noiseControlMode.value) { - NoiseControlMode.OFF -> if (offListeningMode.value) 0 else 1 - NoiseControlMode.TRANSPARENCY -> if (offListeningMode.value) 1 else 0 - NoiseControlMode.ADAPTIVE -> if (offListeningMode.value) 2 else 1 - NoiseControlMode.NOISE_CANCELLATION -> if (offListeningMode.value) 3 else 2 + NoiseControlMode.OFF -> if (showOffListeningMode) 0 else 1 + NoiseControlMode.TRANSPARENCY -> if (showOffListeningMode) 1 else 0 + NoiseControlMode.ADAPTIVE -> if (showOffListeningMode) 2 else 1 + NoiseControlMode.NOISE_CANCELLATION -> if (showOffListeningMode) 3 else 2 } val animatedOffset by animateFloatAsState( @@ -250,7 +210,7 @@ fun NoiseControlSettings( Row( modifier = Modifier.fillMaxWidth() ) { - if (offListeningMode.value) { + if (showOffListeningMode) { NoiseControlButton( icon = ImageBitmap.imageResource(R.drawable.noise_cancellation), onClick = { onModeSelected(NoiseControlMode.OFF) }, @@ -323,13 +283,12 @@ fun NoiseControlSettings( val position = dragOffset / with(density) { buttonWidth.toPx() } val newIndex = position.roundToInt() val newMode = when(newIndex) { - 0 -> if (offListeningMode.value) NoiseControlMode.OFF else NoiseControlMode.TRANSPARENCY - 1 -> if (offListeningMode.value) NoiseControlMode.TRANSPARENCY else NoiseControlMode.ADAPTIVE - 2 -> if (offListeningMode.value) NoiseControlMode.ADAPTIVE else NoiseControlMode.NOISE_CANCELLATION + 0 -> if (showOffListeningMode) NoiseControlMode.OFF else NoiseControlMode.TRANSPARENCY + 1 -> if (showOffListeningMode) NoiseControlMode.TRANSPARENCY else NoiseControlMode.ADAPTIVE + 2 -> if (showOffListeningMode) NoiseControlMode.ADAPTIVE else NoiseControlMode.NOISE_CANCELLATION 3 -> NoiseControlMode.NOISE_CANCELLATION else -> noiseControlMode.value // Keep current if index is invalid } - // Call onModeSelected which now handles service call but not callback onModeSelected(newMode) } ) @@ -347,7 +306,7 @@ fun NoiseControlSettings( .fillMaxWidth() .zIndex(1f) ) { - if (offListeningMode.value) { + if (showOffListeningMode) { NoiseControlButton( icon = ImageBitmap.imageResource(R.drawable.noise_cancellation), onClick = { onModeSelected(NoiseControlMode.OFF) }, @@ -406,7 +365,7 @@ fun NoiseControlSettings( .fillMaxWidth() .padding(top = 4.dp) ) { - if (offListeningMode.value) { + if (showOffListeningMode) { Text( text = stringResource(R.string.off), style = TextStyle(fontSize = 12.sp, color = textColor), @@ -436,9 +395,3 @@ fun NoiseControlSettings( } } } - -@Preview -@Composable -fun NoiseControlSettingsPreview() { - NoiseControlSettings(AirPodsService()) -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/PressAndHoldSettings.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/PressAndHoldSettings.kt similarity index 60% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/PressAndHoldSettings.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/PressAndHoldSettings.kt index 4c5deeae7..a29ea725c 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/PressAndHoldSettings.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/PressAndHoldSettings.kt @@ -1,32 +1,28 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.composables + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components -import android.content.Context -import android.content.res.Configuration import androidx.compose.foundation.background import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.HorizontalDivider @@ -35,38 +31,34 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.navigation.NavController import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.constants.StemAction +import me.kavishdevar.librepods.data.StemAction @Composable -fun PressAndHoldSettings(navController: NavController) { +fun PressAndHoldSettings( + navController: NavController, + leftAction: StemAction, + rightAction: StemAction +) { val isDarkTheme = isSystemInDarkTheme() val textColor = if (isDarkTheme) Color.White else Color.Black val dividerColor = Color(0x40888888) - val context = LocalContext.current - val sharedPreferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE) - - val leftAction = sharedPreferences.getString("left_long_press_action", StemAction.CYCLE_NOISE_CONTROL_MODES.name) - val rightAction = sharedPreferences.getString("right_long_press_action", StemAction.CYCLE_NOISE_CONTROL_MODES.name) - - val leftActionText = when (StemAction.valueOf(leftAction ?: StemAction.CYCLE_NOISE_CONTROL_MODES.name)) { + val leftActionText = when (leftAction) { StemAction.CYCLE_NOISE_CONTROL_MODES -> stringResource(R.string.noise_control) StemAction.DIGITAL_ASSISTANT -> "Digital Assistant" else -> "INVALID!!" } - val rightActionText = when (StemAction.valueOf(rightAction ?: StemAction.CYCLE_NOISE_CONTROL_MODES.name)) { + val rightActionText = when (rightAction) { StemAction.CYCLE_NOISE_CONTROL_MODES -> stringResource(R.string.noise_control) StemAction.DIGITAL_ASSISTANT -> "Digital Assistant" else -> "INVALID!!" @@ -114,9 +106,3 @@ fun PressAndHoldSettings(navController: NavController) { ) } } - -@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) -@Composable -fun PressAndHoldSettingsPreview() { - PressAndHoldSettings(navController = NavController(LocalContext.current)) -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledBottomSheet.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledBottomSheet.kt new file mode 100644 index 000000000..b98139810 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledBottomSheet.kt @@ -0,0 +1,87 @@ +package me.kavishdevar.librepods.presentation.components + +import androidx.compose.animation.core.animateFloatAsState +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.ModalBottomSheet +import androidx.compose.material3.SheetValue +import androidx.compose.material3.rememberModalBottomSheetState +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.lerp +import com.kyant.backdrop.backdrops.LayerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import com.kyant.backdrop.drawBackdrop +import com.kyant.backdrop.effects.blur +import com.kyant.backdrop.effects.lens +import com.kyant.backdrop.effects.vibrancy + + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun StyledBottomSheet( + visible: Boolean, + onDismiss: () -> Unit, + backdrop: LayerBackdrop, + content: @Composable (innerBackdrop: LayerBackdrop, progress: Float) -> Unit +) { + if (!visible) return + + val isDarkTheme = isSystemInDarkTheme() + val sheetState = rememberModalBottomSheetState(false) // move this to parent composable + + val isExpanded = sheetState.targetValue == SheetValue.Expanded + + val progress by animateFloatAsState( + targetValue = if (isExpanded) 1f else 0f, + label = "sheetProgress" + ) + + val animatedCorner = lerp(48.dp, 42.dp, progress) + + ModalBottomSheet( + onDismissRequest = onDismiss, + sheetState = sheetState, + containerColor = Color.Transparent, + dragHandle = { }, + shape = RoundedCornerShape(animatedCorner), + scrimColor = Color.Transparent, + modifier = Modifier.padding(4.dp) + ) { + val innerBackdrop = rememberLayerBackdrop() + Box( + modifier = Modifier + .fillMaxWidth() + .clip(RoundedCornerShape(animatedCorner)) + .drawBackdrop( + backdrop = backdrop, + exportedBackdrop = innerBackdrop, + shape = { RoundedCornerShape(animatedCorner) }, + effects = { + vibrancy() + blur(4f.dp.toPx()) + lens(12f.dp.toPx(), 48f.dp.toPx(), true) + }, + onDrawSurface = { + drawRect( + if (isDarkTheme) Color.DarkGray.copy(alpha = 0.3f) else Color( + 0xFFE0E0E0 + ).copy(alpha = 0.45f) + ) + } + ) + .padding(top = 24.dp) + .padding(horizontal = 16.dp) + ) { + content(innerBackdrop, progress) + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledButton.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledButton.kt similarity index 65% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/StyledButton.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledButton.kt index 01438bdfc..14b30f3f2 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledButton.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledButton.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.composables + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components import android.graphics.RuntimeShader import android.os.Build @@ -46,7 +46,9 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ShaderBrush import androidx.compose.ui.graphics.isSpecified import androidx.compose.ui.graphics.toArgb +import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.semantics.Role import androidx.compose.ui.unit.dp import androidx.compose.ui.util.fastCoerceAtMost @@ -55,7 +57,7 @@ import androidx.compose.ui.util.lerp import com.kyant.backdrop.Backdrop import com.kyant.backdrop.drawBackdrop import com.kyant.backdrop.effects.blur -import com.kyant.backdrop.effects.refraction +import com.kyant.backdrop.effects.lens import com.kyant.backdrop.effects.vibrancy import com.kyant.backdrop.highlight.Highlight import kotlinx.coroutines.launch @@ -75,9 +77,12 @@ fun StyledButton( tint: Color = Color.Unspecified, surfaceColor: Color = Color.Unspecified, maxScale: Float = 0.1f, + enabled: Boolean = true, content: @Composable RowScope.() -> Unit, ) { - val animationScope = rememberCoroutineScope() + val isInteractive = enabled && isInteractive + val scope = rememberCoroutineScope() + val haptics = LocalHapticFeedback.current val progressAnimation = remember { Animatable(0f) } var pressStartPosition by remember { mutableStateOf(Offset.Zero) } val offsetAnimation = remember { Animatable(Offset.Zero, Offset.VectorConverter) } @@ -122,8 +127,8 @@ half4 main(float2 coord) { } else { drawRect(Color.White.copy(0.1f)) } - if (surfaceColor.isSpecified) { - val color = if (!isInteractive && isPressed) { + if (surfaceColor.isSpecified && enabled) { + val color = if (isPressed) { Color( red = surfaceColor.red * 0.5f, green = surfaceColor.green * 0.5f, @@ -134,6 +139,11 @@ half4 main(float2 coord) { surfaceColor } drawRect(color) + } else { + if (isPressed && enabled) { + drawRect(Color.Black.copy(alpha = 0.4f)) + drawRect(Color.White.copy(alpha = 0.2f)) + } } }, onDrawFront = null, @@ -146,7 +156,12 @@ half4 main(float2 coord) { effects = { vibrancy() blur(2f.dp.toPx()) - refraction(12f.dp.toPx(), 24f.dp.toPx()) + lens( + refractionHeight = 12f.dp.toPx(), + refractionAmount = 24f.dp.toPx(), + depthEffect = true, + chromaticAberration = true + ) }, layerBlock = { val width = size.width @@ -158,19 +173,21 @@ half4 main(float2 coord) { val maxOffset = size.minDimension val initialDerivative = 0.05f val offset = offsetAnimation.value - translationX = maxOffset * tanh(initialDerivative * offset.x / maxOffset) - translationY = maxOffset * tanh(initialDerivative * offset.y / maxOffset) + translationX = + maxOffset * tanh(initialDerivative * offset.x / maxOffset) + translationY = + maxOffset * tanh(initialDerivative * offset.y / maxOffset) val maxDragScale = 0.1f val offsetAngle = atan2(offset.y, offset.x) scaleX = scale + - maxDragScale * abs(cos(offsetAngle) * offset.x / size.maxDimension) * - (width / height).fastCoerceAtMost(1f) + maxDragScale * abs(cos(offsetAngle) * offset.x / size.maxDimension) * + (width / height).fastCoerceAtMost(1f) scaleY = scale + - maxDragScale * abs(sin(offsetAngle) * offset.y / size.maxDimension) * - (height / width).fastCoerceAtMost(1f) + maxDragScale * abs(sin(offsetAngle) * offset.y / size.maxDimension) * + (height / width).fastCoerceAtMost(1f) }, onDrawSurface = { if (tint.isSpecified) { @@ -204,7 +221,10 @@ half4 main(float2 coord) { interactiveHighlightShader.apply { val offset = pressStartPosition + offsetAnimation.value setFloatUniform("size", size.width, size.height) - setColorUniform("color", Color.White.copy(0.15f * progress).toArgb()) + setColorUniform( + "color", + Color.White.copy(0.15f * progress).toArgb() + ) setFloatUniform("radius", size.maxDimension) setFloatUniform( "offset", @@ -231,32 +251,65 @@ half4 main(float2 coord) { interactionSource = null, indication = null, role = Role.Button, - onClick = onClick + onClick = { + if (enabled) { + haptics.performHapticFeedback(HapticFeedbackType.ContextClick) + onClick() + } + } ) .then( if (isInteractive) { - Modifier.pointerInput(animationScope) { + Modifier.pointerInput(scope) { val progressAnimationSpec = spring(0.5f, 300f, 0.001f) val offsetAnimationSpec = spring(1f, 300f, Offset.VisibilityThreshold) val onDragStop: () -> Unit = { - animationScope.launch { - launch { progressAnimation.animateTo(0f, progressAnimationSpec) } - launch { offsetAnimation.animateTo(Offset.Zero, offsetAnimationSpec) } + if (enabled) { + scope.launch { + launch { haptics.performHapticFeedback(HapticFeedbackType.Reject) } + launch { + progressAnimation.animateTo( + 0f, + progressAnimationSpec + ) + } + launch { + offsetAnimation.animateTo( + Offset.Zero, + offsetAnimationSpec + ) + } + } } } inspectDragGestures( onDragStart = { down -> pressStartPosition = down.position - animationScope.launch { - launch { progressAnimation.animateTo(1f, progressAnimationSpec) } - launch { offsetAnimation.snapTo(Offset.Zero) } + if (enabled) { + scope.launch { + launch { haptics.performHapticFeedback(HapticFeedbackType.SegmentFrequentTick) } + launch { + progressAnimation.animateTo( + 1f, + progressAnimationSpec + ) + } + launch { offsetAnimation.snapTo(Offset.Zero) } + } } }, - onDragEnd = { onDragStop() }, + onDragEnd = { + onDragStop() + }, onDragCancel = onDragStop ) { _, dragAmount -> - animationScope.launch { - offsetAnimation.snapTo(offsetAnimation.value + dragAmount) + if (enabled) { + scope.launch { + if (dragAmount.getDistanceSquared() > 350) haptics.performHapticFeedback( + HapticFeedbackType.SegmentFrequentTick + ) + offsetAnimation.snapTo(offsetAnimation.value + dragAmount) + } } } } @@ -269,7 +322,10 @@ half4 main(float2 coord) { isPressed = false }, onTap = { - onClick() + if (enabled) { + haptics.performHapticFeedback(HapticFeedbackType.ContextClick) + onClick() + } } ) } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledDropdown.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledDropdown.kt similarity index 85% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/StyledDropdown.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledDropdown.kt index 73cf74493..4446f0856 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledDropdown.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledDropdown.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.composables + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components import android.annotation.SuppressLint import androidx.compose.animation.AnimatedVisibility @@ -49,14 +49,17 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color +import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.layout.onGloballyPositioned +import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily @@ -71,6 +74,7 @@ import dev.chrisbanes.haze.HazeTint import dev.chrisbanes.haze.hazeEffect import dev.chrisbanes.haze.materials.CupertinoMaterials import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi +import kotlinx.coroutines.launch import me.kavishdevar.librepods.R @ExperimentalHazeMaterialsApi @@ -110,6 +114,9 @@ fun StyledDropdown( var hoveredIndex by remember { mutableStateOf(null) } val itemHeight = 48.dp + val scope = rememberCoroutineScope() + val haptics = LocalHapticFeedback.current + var popupSize by remember { mutableStateOf(IntSize(0, 0)) } var lastDragPosition by remember { mutableStateOf(null) } @@ -132,7 +139,12 @@ fun StyledDropdown( }, onDrag = { change, _ -> val y = change.position.y - hoveredIndex = (y / itemHeight.toPx()).toInt() + val newHoveredIndex = (y / itemHeight.toPx()).toInt() + if (newHoveredIndex != hoveredIndex) { + scope.launch { haptics.performHapticFeedback( + HapticFeedbackType.SegmentTick) } + } + hoveredIndex = newHoveredIndex lastDragPosition = change.position }, onDragEnd = { @@ -144,6 +156,8 @@ fun StyledDropdown( if (withinBounds) { hoveredIndex?.let { idx -> if (idx in options.indices) { + scope.launch { haptics.performHapticFeedback( + HapticFeedbackType.GestureEnd) } onOptionSelected(options[idx]) } } @@ -174,6 +188,7 @@ fun StyledDropdown( interactionSource = remember { MutableInteractionSource() }, indication = null ) { + scope.launch { haptics.performHapticFeedback(HapticFeedbackType.ContextClick) } onOptionSelected(text) onDismissRequest() } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledIconButton.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledIconButton.kt similarity index 66% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/StyledIconButton.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledIconButton.kt index 5f7071879..6937587a2 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledIconButton.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledIconButton.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.composables + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components import android.graphics.RuntimeShader import android.os.Build @@ -50,7 +50,10 @@ import androidx.compose.ui.graphics.layer.CompositingStrategy import androidx.compose.ui.graphics.layer.drawLayer import androidx.compose.ui.graphics.rememberGraphicsLayer import androidx.compose.ui.graphics.toArgb +import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily @@ -63,8 +66,7 @@ import androidx.compose.ui.util.lerp import com.kyant.backdrop.backdrops.LayerBackdrop import com.kyant.backdrop.backdrops.rememberLayerBackdrop import com.kyant.backdrop.drawBackdrop -import com.kyant.backdrop.effects.blur -import com.kyant.backdrop.effects.refractionWithDispersion +import com.kyant.backdrop.effects.lens import com.kyant.backdrop.highlight.Highlight import com.kyant.backdrop.shadow.Shadow import kotlinx.coroutines.launch @@ -78,14 +80,17 @@ import kotlin.math.tanh @Composable fun StyledIconButton( - onClick: () -> Unit, + modifier: Modifier = Modifier, icon: String, - darkMode: Boolean, - tint: Color = Color.Unspecified, + iconTint: Color = Color.Unspecified, + surfaceColor: Color = Color.Unspecified, backdrop: LayerBackdrop = rememberLayerBackdrop(), - modifier: Modifier = Modifier, + onClick: () -> Unit, + enabled: Boolean = true ) { - val animationScope = rememberCoroutineScope() + val haptics = LocalHapticFeedback.current + val darkMode = isSystemInDarkTheme() + val scope = rememberCoroutineScope() val progressAnimationSpec = spring(0.5f, 300f, 0.001f) val offsetAnimationSpec = spring(1f, 300f, Offset.VisibilityThreshold) val progressAnimation = remember { Animatable(0f) } @@ -94,6 +99,7 @@ fun StyledIconButton( val innerShadowLayer = rememberGraphicsLayer().apply { compositingStrategy = CompositingStrategy.Offscreen } + val density = LocalDensity.current val interactiveHighlightShader = remember { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { @@ -117,7 +123,12 @@ half4 main(float2 coord) { } val isDarkTheme = isSystemInDarkTheme() TextButton( - onClick = onClick, + onClick = { + if (enabled) { + scope.launch { haptics.performHapticFeedback(HapticFeedbackType.ContextClick) } + onClick() + } + }, shape = RoundedCornerShape(56.dp), modifier = modifier .padding(horizontal = 12.dp) @@ -132,6 +143,7 @@ half4 main(float2 coord) { ) }, layerBlock = { + if (!enabled) return@drawBackdrop val width = size.width val height = size.height @@ -148,14 +160,20 @@ half4 main(float2 coord) { val offsetAngle = atan2(offset.y, offset.x) scaleX = scale + - maxDragScale * abs(cos(offsetAngle) * offset.x / size.maxDimension) * - (width / height).fastCoerceAtMost(1f) + maxDragScale * abs(cos(offsetAngle) * offset.x / size.maxDimension) * + (width / height).fastCoerceAtMost(1f) scaleY = scale + - maxDragScale * abs(sin(offsetAngle) * offset.y / size.maxDimension) * - (height / width).fastCoerceAtMost(1f) + maxDragScale * abs(sin(offsetAngle) * offset.y / size.maxDimension) * + (height / width).fastCoerceAtMost(1f) }, onDrawSurface = { + if (!enabled) { + drawRect( + (if (isDarkTheme) Color(0xFFAFAFAF) else Color.White).copy(0.5f) + ) + return@drawBackdrop + } val progress = progressAnimation.value.coerceIn(0f, 1f) val shape = RoundedCornerShape(56.dp) @@ -182,11 +200,21 @@ half4 main(float2 coord) { } drawLayer(innerShadowLayer) + if (surfaceColor.isSpecified) { + drawRect(surfaceColor) + } + drawRect( - (if (isDarkTheme) Color(0xFFAFAFAF) else Color.White).copy(progress.coerceIn(0.15f, 0.35f)) + (if (isDarkTheme) Color(0xFFAFAFAF) else Color.White).copy( + progress.coerceIn( + 0.15f, + 0.35f + ) + ) ) }, onDrawFront = { + if (!enabled) return@drawBackdrop val progress = progressAnimation.value.fastCoerceIn(0f, 1f) if (progress > 0f) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && interactiveHighlightShader != null) { @@ -197,7 +225,10 @@ half4 main(float2 coord) { interactiveHighlightShader.apply { val offset = pressStartPosition + offsetAnimation.value setFloatUniform("size", size.width, size.height) - setColorUniform("color", Color.White.copy(0.15f * progress).toArgb()) + setColorUniform( + "color", + Color.White.copy(0.15f * progress).toArgb() + ) setFloatUniform("radius", size.maxDimension) setFloatUniform( "offset", @@ -218,41 +249,56 @@ half4 main(float2 coord) { } }, effects = { - refractionWithDispersion(6f.dp.toPx(), size.height / 2f) - // blur(24f, TileMode.Decal) + lens( + refractionHeight = 6f.dp.toPx(), + refractionAmount = size.height / 2f, + depthEffect = true, + chromaticAberration = true + ) }, ) - .pointerInput(animationScope) { + .pointerInput(scope) { val onDragStop: () -> Unit = { - animationScope.launch { - launch { progressAnimation.animateTo(0f, progressAnimationSpec) } - launch { offsetAnimation.animateTo(Offset.Zero, offsetAnimationSpec) } + if (enabled) { + scope.launch { + launch { haptics.performHapticFeedback(HapticFeedbackType.Reject) } + launch { progressAnimation.animateTo(0f, progressAnimationSpec) } + launch { offsetAnimation.animateTo(Offset.Zero, offsetAnimationSpec) } + } } } inspectDragGestures( onDragStart = { down -> - pressStartPosition = down.position - animationScope.launch { - launch { progressAnimation.animateTo(1f, progressAnimationSpec) } - launch { offsetAnimation.snapTo(Offset.Zero) } + if (enabled) { + pressStartPosition = down.position + scope.launch { + launch { haptics.performHapticFeedback(HapticFeedbackType.SegmentFrequentTick) } + launch { progressAnimation.animateTo(1f, progressAnimationSpec) } + launch { offsetAnimation.snapTo(Offset.Zero) } + } } }, onDragEnd = { onDragStop() }, onDragCancel = onDragStop ) { _, dragAmount -> - animationScope.launch { - offsetAnimation.snapTo(offsetAnimation.value + dragAmount) + scope.launch { + if (enabled) { + if (dragAmount.getDistanceSquared() > 350) haptics.performHapticFeedback( + HapticFeedbackType.SegmentFrequentTick + ) + offsetAnimation.snapTo(offsetAnimation.value + dragAmount) + } } } } - .size(48.dp), + .size(with(density) { 48.sp.toDp() }), ) { Text( text = icon, style = TextStyle( - fontSize = 16.sp, + fontSize = 20.sp, fontWeight = FontWeight.Normal, - color = if (tint.isSpecified) tint else if (darkMode) Color.White else Color.Black, + color = if (iconTint.isSpecified) iconTint else if (darkMode) Color.White else Color.Black, fontFamily = FontFamily(Font(R.font.sf_pro)) ) ) diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledInputField.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledInputField.kt new file mode 100644 index 000000000..86eae6a68 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledInputField.kt @@ -0,0 +1,154 @@ +package me.kavishdevar.librepods.presentation.components + +import android.R.attr.singleLine +import androidx.compose.animation.core.animateDp +import androidx.compose.animation.core.animateDpAsState +import androidx.compose.animation.core.updateTransition +import androidx.compose.foundation.background +import androidx.compose.foundation.gestures.detectTapGestures +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.text.BasicTextField +import androidx.compose.foundation.text.input.TextFieldLineLimits +import androidx.compose.foundation.text.input.TextFieldState +import androidx.compose.foundation.text.input.clearText +import androidx.compose.material3.IconButton +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.SolidColor +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import me.kavishdevar.librepods.R + + +@Composable +fun StyledInputField( + inputState: TextFieldState, + focusRequester: FocusRequester, + placeholder: String = "", + singleLine: Boolean = true +){ + val isDarkTheme = isSystemInDarkTheme() + val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + val textColor = if (isDarkTheme) Color.White else Color.Black + val minHeight = if (singleLine) 58.dp else 120.dp + val verticalAlignment = if (singleLine) Alignment.CenterVertically else Alignment.Top + val hasText = inputState.text.isNotEmpty() + val density = LocalDensity.current + val spacerHeight by animateDpAsState( + targetValue = if (hasText) with(density) { 32.sp.toDp() } else 0.dp, + label = "labelSpacer" + ) + + val transition = updateTransition(hasText, label = "floating") + val yOffset by transition.animateDp(label = "y") { + if (it) with (density) { (-48).sp.toDp() } else 0.dp + } + + Spacer(modifier = Modifier.height(spacerHeight)) + + Box( + modifier = Modifier.fillMaxWidth() + ) { + Row( + verticalAlignment = verticalAlignment, + modifier = Modifier + .fillMaxWidth() + .heightIn(min = minHeight) + .background( + backgroundColor, + RoundedCornerShape(28.dp) + ) + .padding(horizontal = 16.dp, vertical = 8.dp) + .pointerInput(Unit) { + detectTapGestures { + focusRequester.requestFocus() + } + } + ) { + BasicTextField( + state = inputState, + lineLimits = if (singleLine) TextFieldLineLimits.SingleLine else TextFieldLineLimits.Default, + textStyle = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), + cursorBrush = SolidColor(textColor), + decorator = { innerTextField -> + Row( + modifier = Modifier.padding(top = if (singleLine) 0.dp else 16.dp), + verticalAlignment = verticalAlignment, + ) { + Row( + modifier = Modifier + .weight(1f) + ) { + Box( + modifier = Modifier + .weight(1f), + contentAlignment = if (singleLine) Alignment.CenterStart else Alignment.TopStart + ) { + Text( + text = placeholder, + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Light, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor.copy(alpha = 0.8f) + ), + modifier = Modifier + .offset(y = yOffset) + ) + + innerTextField() + } + } + if (singleLine && !inputState.text.isEmpty()) { + IconButton( + onClick = { + inputState.clearText() + } + ) { + Text( + text = "􀁡", + style = TextStyle( + fontSize = 16.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy( + alpha = 0.6f + ) + ), + ) + } + } + } + }, + modifier = Modifier + .fillMaxWidth() + .padding(start = 8.dp) + .focusRequester(focusRequester) + ) + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledScaffold.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledScaffold.kt similarity index 81% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/StyledScaffold.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledScaffold.kt index 6c034f90f..6376188ca 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledScaffold.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledScaffold.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.composables + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.layout.Box @@ -49,7 +49,6 @@ import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.zIndex -import androidx.navigation.NavController import com.kyant.backdrop.backdrops.LayerBackdrop import com.kyant.backdrop.backdrops.layerBackdrop import com.kyant.backdrop.backdrops.rememberLayerBackdrop @@ -57,17 +56,15 @@ import dev.chrisbanes.haze.HazeProgressive import dev.chrisbanes.haze.HazeState import dev.chrisbanes.haze.HazeTint import dev.chrisbanes.haze.hazeEffect -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi import dev.chrisbanes.haze.rememberHazeState import me.kavishdevar.librepods.R -@ExperimentalHazeMaterialsApi @Composable fun StyledScaffold( title: String, actionButtons: List<@Composable (backdrop: LayerBackdrop) -> Unit> = emptyList(), snackbarHostState: SnackbarHostState = remember { SnackbarHostState() }, - content: @Composable (spacerValue: Dp, hazeState: HazeState) -> Unit + content: @Composable (spacerValue: Dp, hazeState: HazeState, bottomPadding: Dp) -> Unit ) { val isDarkTheme = isSystemInDarkTheme() val hazeState = rememberHazeState(blurEnabled = true) @@ -80,14 +77,14 @@ fun StyledScaffold( .clip(RoundedCornerShape(52.dp)) ) { paddingValues -> val topPadding = paddingValues.calculateTopPadding() - val bottomPadding = paddingValues.calculateBottomPadding() + val bottomPadding = paddingValues.calculateBottomPadding() + 16.dp val startPadding = paddingValues.calculateLeftPadding(LocalLayoutDirection.current) val endPadding = paddingValues.calculateRightPadding(LocalLayoutDirection.current) Box( modifier = Modifier .fillMaxSize() - .padding(start = startPadding, end = endPadding, bottom = bottomPadding) + .padding(start = startPadding, end = endPadding) ) { val backdrop = rememberLayerBackdrop() Box( @@ -127,13 +124,12 @@ fun StyledScaffold( } } - content(topPadding + 64.dp, hazeState) + content(topPadding + 64.dp, hazeState, bottomPadding + 12.dp) } } } -@ExperimentalHazeMaterialsApi @Composable fun StyledScaffold( title: String, @@ -145,12 +141,11 @@ fun StyledScaffold( title = title, actionButtons = actionButtons, snackbarHostState = snackbarHostState, - ) { _, _ -> + ) { _, _, _-> content() } } -@ExperimentalHazeMaterialsApi @Composable fun StyledScaffold( title: String, @@ -162,7 +157,7 @@ fun StyledScaffold( title = title, actionButtons = actionButtons, snackbarHostState = snackbarHostState, - ) { spacerValue, _ -> + ) { spacerValue, _, _ -> content(spacerValue) } } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledSelectList.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledSelectList.kt similarity index 73% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/StyledSelectList.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledSelectList.kt index 58e196c86..b9f6c2f70 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledSelectList.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledSelectList.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.composables + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components import androidx.compose.animation.animateColorAsState import androidx.compose.animation.core.animateFloatAsState @@ -29,6 +29,7 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.wrapContentWidth import androidx.compose.foundation.shape.RoundedCornerShape @@ -43,12 +44,13 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import me.kavishdevar.librepods.R @@ -59,19 +61,10 @@ data class SelectItem( val iconRes: Int? = null, val selected: Boolean, val onClick: () -> Unit, + val visible: Boolean = true, val enabled: Boolean = true ) -data class SelectItem2( - val name: String, - val description: String? = null, - val iconRes: Int? = null, - val selected: () -> Boolean, - val onClick: () -> Unit, - val enabled: Boolean = true -) - - @Composable fun StyledSelectList( items: List, @@ -81,37 +74,48 @@ fun StyledSelectList( val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) val textColor = if (isDarkTheme) Color.White else Color.Black + val haptics = LocalHapticFeedback.current + Column( modifier = modifier .fillMaxWidth() .background(backgroundColor, RoundedCornerShape(28.dp)), horizontalAlignment = Alignment.CenterHorizontally ) { - val visibleItems = items.filter { it.enabled } + val visibleItems = items.filter { it.visible } visibleItems.forEachIndexed { index, item -> val isFirst = index == 0 val isLast = index == visibleItems.size - 1 val hasIcon = item.iconRes != null val shape = when { + isFirst && isLast -> RoundedCornerShape(28.dp) isFirst -> RoundedCornerShape(topStart = 28.dp, topEnd = 28.dp) isLast -> RoundedCornerShape(bottomStart = 28.dp, bottomEnd = 28.dp) else -> RoundedCornerShape(0.dp) } - var itemBackgroundColor by remember { mutableStateOf(backgroundColor) } + var itemBackgroundColor by remember { mutableStateOf(if (item.enabled) backgroundColor else if (isDarkTheme) Color(0x40050505) else Color(0x40D9D9D9)) } val animatedBackgroundColor by animateColorAsState(targetValue = itemBackgroundColor, animationSpec = tween(durationMillis = 500)) Row( modifier = Modifier - .height(if (hasIcon) 72.dp else 55.dp) + .heightIn(min = if (hasIcon) 72.dp else 55.dp) .background(animatedBackgroundColor, shape) .pointerInput(Unit) { detectTapGestures( onPress = { - itemBackgroundColor = if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9) - tryAwaitRelease() - itemBackgroundColor = backgroundColor - item.onClick() + if (item.enabled) { + itemBackgroundColor = + if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9) + tryAwaitRelease() + itemBackgroundColor = backgroundColor + } + }, + onTap = { + if (item.enabled) { + haptics.performHapticFeedback(HapticFeedbackType.ContextClick) + item.onClick() + } } ) } @@ -121,7 +125,7 @@ fun StyledSelectList( ) { if (hasIcon) { Icon( - painter = painterResource(item.iconRes!!), + painter = painterResource(item.iconRes), contentDescription = "Icon", tint = Color(0xFF007AFF), modifier = Modifier @@ -181,4 +185,4 @@ fun StyledSelectList( } } } -} \ No newline at end of file +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledSlider.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledSlider.kt similarity index 73% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/StyledSlider.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledSlider.kt index 3aba12635..58c9b571d 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/StyledSlider.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledSlider.kt @@ -1,23 +1,24 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.composables + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components + +import android.annotation.SuppressLint import android.content.res.Configuration import android.util.Log import androidx.compose.animation.core.Animatable @@ -43,7 +44,6 @@ import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.MutableFloatState import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableFloatStateOf @@ -56,6 +56,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.input.pointer.util.VelocityTracker import androidx.compose.ui.input.pointer.util.addPointerInputChange @@ -64,6 +65,7 @@ import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.layout.positionInParent import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily @@ -81,7 +83,7 @@ import com.kyant.backdrop.backdrops.rememberCombinedBackdrop import com.kyant.backdrop.backdrops.rememberLayerBackdrop import com.kyant.backdrop.drawBackdrop import com.kyant.backdrop.effects.blur -import com.kyant.backdrop.effects.refractionWithDispersion +import com.kyant.backdrop.effects.lens import com.kyant.backdrop.highlight.Highlight import com.kyant.backdrop.shadow.InnerShadow import com.kyant.backdrop.shadow.Shadow @@ -203,10 +205,11 @@ class MomentumAnimation( } } +@SuppressLint("UnrememberedMutableState") @Composable fun StyledSlider( label: String? = null, - mutableFloatState: MutableFloatState, + value: Float, onValueChange: (Float) -> Unit, valueRange: ClosedFloatingPointRange, backdrop: Backdrop = rememberLayerBackdrop(), @@ -217,23 +220,26 @@ fun StyledSlider( startLabel: String? = null, endLabel: String? = null, independent: Boolean = false, - description: String? = null + description: String? = null, + enabled: Boolean = true ) { val backgroundColor = if (isSystemInDarkTheme()) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) val isLightTheme = !isSystemInDarkTheme() - val accentColor = - if (isLightTheme) Color(0xFF0088FF) - else Color(0xFF0091FF) val trackColor = if (isLightTheme) Color(0xFF787878).copy(0.2f) else Color(0xFF787880).copy(0.36f) + val accentColor = + if (enabled) { + if (isLightTheme) Color(0xFF0088FF) + else Color(0xFF0091FF) + } else { + trackColor + } val labelTextColor = if (isLightTheme) Color.Black else Color.White - val fraction by remember { - derivedStateOf { - ((mutableFloatState.floatValue - valueRange.start) / (valueRange.endInclusive - valueRange.start)) - .fastCoerceIn(0f, 1f) - } + val fraction by derivedStateOf { + ((value - valueRange.start) / (valueRange.endInclusive - valueRange.start)) + .fastCoerceIn(0f, 1f) } val sliderBackdrop = rememberLayerBackdrop() @@ -242,6 +248,8 @@ fun StyledSlider( val startIconWidthState = remember { mutableFloatStateOf(0f) } val endIconWidthState = remember { mutableFloatStateOf(0f) } val density = LocalDensity.current + val haptics = LocalHapticFeedback.current + var lastDragValue by remember { mutableFloatStateOf(value) } val momentumAnimation = rememberMomentumAnimation(maxScale = 1.5f) @@ -427,71 +435,93 @@ fun StyledSlider( ) translationY = if (startLabel != null || endLabel != null) trackPositionState.floatValue + with(density) { 26.dp.toPx() } + size.height / 2f else trackPositionState.floatValue + with(density) { 8.dp.toPx() } } - .draggable( - rememberDraggableState { delta -> - val trackWidth = trackWidthState.floatValue - if (trackWidth > 0f) { - val targetFraction = fraction + delta / trackWidth - val targetValue = - lerp(valueRange.start, valueRange.endInclusive, targetFraction) - .fastCoerceIn(valueRange.start, valueRange.endInclusive) - val snappedValue = if (snapPoints.isNotEmpty()) snapIfClose( - targetValue, - snapPoints, - snapThreshold - ) else targetValue - onValueChange(snappedValue) - } - }, - Orientation.Horizontal, - startDragImmediately = true, - onDragStarted = { - // Remove this block as momentumAnimation handles pressing - }, - onDragStopped = { - // Remove this block as momentumAnimation handles pressing - onValueChange((mutableFloatState.floatValue * 100).roundToInt() / 100f) - } - ) - .then(momentumAnimation.modifier) - .drawBackdrop( - rememberCombinedBackdrop(backdrop, sliderBackdrop), - { RoundedCornerShape(28.dp) }, - highlight = { - val progress = momentumAnimation.progress - Highlight.Ambient.copy(alpha = progress) - }, - shadow = { - Shadow( - radius = 4f.dp, - color = Color.Black.copy(0.05f) - ) - }, - innerShadow = { - val progress = momentumAnimation.progress - InnerShadow( - radius = 4f.dp * progress, - alpha = progress - ) - }, - layerBlock = { - scaleX = momentumAnimation.scaleX - scaleY = momentumAnimation.scaleY - val velocity = momentumAnimation.velocity / 5000f - scaleX /= 1f - (velocity * 0.75f).fastCoerceIn(-0.15f, 0.15f) - scaleY *= 1f - (velocity * 0.25f).fastCoerceIn(-0.15f, 0.15f) - }, - onDrawSurface = { - val progress = momentumAnimation.progress - drawRect(Color.White.copy(alpha = 1f - progress)) - }, - effects = { - val progress = momentumAnimation.progress - blur(8f.dp.toPx() * (1f - progress)) - refractionWithDispersion( - height = 6f.dp.toPx() * progress, - amount = size.height / 2f * progress - ) + .then( + if (enabled) { + Modifier + .draggable( + rememberDraggableState { delta -> + val trackWidth = trackWidthState.floatValue + if (trackWidth > 0f) { + val targetFraction = fraction + delta / trackWidth + val targetValue = + lerp( + valueRange.start, + valueRange.endInclusive, + targetFraction + ) + .fastCoerceIn( + valueRange.start, + valueRange.endInclusive + ) + snapPoints.forEach { snap -> + if ((lastDragValue < snap && targetValue >= snap) || + (snap in targetValue... - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.composables + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components import android.content.res.Configuration -import androidx.compose.animation.Animatable +import androidx.compose.animation.animateColorAsState import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.FastOutSlowInEasing import androidx.compose.animation.core.spring @@ -58,8 +58,10 @@ import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.graphics.layer.CompositingStrategy import androidx.compose.ui.graphics.layer.drawLayer import androidx.compose.ui.graphics.rememberGraphicsLayer +import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.util.fastCoerceIn @@ -68,11 +70,12 @@ import com.kyant.backdrop.backdrops.layerBackdrop import com.kyant.backdrop.backdrops.rememberCombinedBackdrop import com.kyant.backdrop.backdrops.rememberLayerBackdrop import com.kyant.backdrop.drawBackdrop -import com.kyant.backdrop.effects.refractionWithDispersion +import com.kyant.backdrop.effects.lens import com.kyant.backdrop.highlight.Highlight import com.kyant.backdrop.shadow.Shadow import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch +import kotlin.math.abs @Composable fun StyledSwitch( @@ -81,9 +84,12 @@ fun StyledSwitch( enabled: Boolean = true, ) { val isDarkTheme = isSystemInDarkTheme() + val haptics = LocalHapticFeedback.current val onColor = if (enabled) Color(0xFF34C759) else if (isDarkTheme) Color(0xFF5B5B5E) else Color(0xFFD1D1D6) - val offColor = if (enabled) if (isDarkTheme) Color(0xFF5B5B5E) else Color(0xFFD1D1D6) else if (isDarkTheme) Color(0xFF5B5B5E) else Color(0xFFD1D1D6) + val offColor = if (enabled) if (isDarkTheme) Color(0xFF5B5B5E) else Color(0xFFD1D1D6) else if (isDarkTheme) Color( + 0x805B5B5E + ) else Color(0xFFD1D1D6) val trackWidth = 64.dp val trackHeight = 28.dp @@ -98,24 +104,25 @@ fun StyledSwitch( val animatedFraction = remember { Animatable(fraction) } val trackWidthPx = remember { mutableFloatStateOf(0f) } val density = LocalDensity.current - val animationScope = rememberCoroutineScope() + val scope = rememberCoroutineScope() val progressAnimationSpec = spring(0.5f, 300f, 0.001f) - val colorAnimationSpec = tween(200, easing = FastOutSlowInEasing) val progressAnimation = remember { Animatable(0f) } val innerShadowLayer = rememberGraphicsLayer().apply { compositingStrategy = CompositingStrategy.Offscreen } - val animatedTrackColor = remember { Animatable(if (checked) onColor else offColor) } + val targetColor = if (checked) onColor else offColor + val animatedTrackColor by animateColorAsState(targetColor) val totalDrag = remember { mutableFloatStateOf(0f) } val tapThreshold = 10f val isFirstComposition = remember { mutableStateOf(true) } LaunchedEffect(checked) { if (!isFirstComposition.value) { + if (checked) { + haptics.performHapticFeedback(HapticFeedbackType.ToggleOn) + } else { + haptics.performHapticFeedback(HapticFeedbackType.ToggleOff) + } coroutineScope { - launch { - val targetColor = if (checked) onColor else offColor - animatedTrackColor.animateTo(targetColor, colorAnimationSpec) - } launch { val targetFrac = if (checked) 1f else 0f animatedFraction.animateTo(targetFrac, progressAnimationSpec) @@ -140,7 +147,7 @@ fun StyledSwitch( modifier = Modifier .layerBackdrop(switchBackdrop) .clip(RoundedCornerShape(trackHeight / 2)) - .background(animatedTrackColor.value) + .background(animatedTrackColor) .width(trackWidth) .height(trackHeight) .onSizeChanged { trackWidthPx.floatValue = it.width.toFloat() } @@ -154,27 +161,31 @@ fun StyledSwitch( .then(if (enabled) Modifier.draggable( rememberDraggableState { delta -> if (trackWidthPx.floatValue > 0f) { + val oldFraction = animatedFraction.value val newFraction = (animatedFraction.value + delta / trackWidthPx.floatValue).fastCoerceIn(-0.3f, 1.3f) - animationScope.launch { + scope.launch { animatedFraction.snapTo(newFraction) } - totalDrag.floatValue += kotlin.math.abs(delta) + totalDrag.floatValue += abs(delta) val newChecked = newFraction >= 0.5f if (newChecked != checked) { onCheckedChange(newChecked) } + if ((oldFraction < 0.5f && newFraction >= 0.5f) || (oldFraction >= 0.5f && newFraction < 0.5f)) { + haptics.performHapticFeedback(HapticFeedbackType.SegmentTick) + } } }, Orientation.Horizontal, startDragImmediately = true, onDragStarted = { totalDrag.floatValue = 0f - animationScope.launch { + scope.launch { progressAnimation.animateTo(1f, progressAnimationSpec) } }, onDragStopped = { - animationScope.launch { + scope.launch { if (totalDrag.floatValue < tapThreshold) { val newChecked = !checked onCheckedChange(newChecked) @@ -262,7 +273,12 @@ fun StyledSwitch( drawRect(Color.White.copy(1f - progress)) }, effects = { - refractionWithDispersion(6f.dp.toPx(), size.height / 2f) + lens( + refractionHeight = 6f.dp.toPx(), + refractionAmount = size.height / 2f, + depthEffect = true, + chromaticAberration = true + ) } ) .width(thumbWidth) diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledToggle.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledToggle.kt new file mode 100644 index 000000000..4028dc4a2 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/StyledToggle.kt @@ -0,0 +1,273 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +@file:OptIn(ExperimentalEncodingApi::class) + +package me.kavishdevar.librepods.presentation.components + +import androidx.compose.animation.animateColorAsState +import androidx.compose.animation.core.tween +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.gestures.detectTapGestures +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.rememberUpdatedState +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.hapticfeedback.HapticFeedbackType +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.platform.LocalHapticFeedback +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import kotlinx.coroutines.launch +import me.kavishdevar.librepods.R +import kotlin.io.encoding.ExperimentalEncodingApi + +@Composable +fun StyledToggle( + title: String? = null, + label: String, + description: String? = null, + checked: Boolean = false, + independent: Boolean = true, + enabled: Boolean = true, + onCheckedChange: (Boolean) -> Unit, +) { + val currentChecked by rememberUpdatedState(checked) + + val isDarkTheme = isSystemInDarkTheme() + val textColor = if (isDarkTheme) Color.White else Color.Black + + val haptics = LocalHapticFeedback.current + val scope = rememberCoroutineScope() + + var backgroundColor by remember { + mutableStateOf( + if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + ) + } + + val animatedBackgroundColor by animateColorAsState( + targetValue = backgroundColor, + animationSpec = tween(durationMillis = 500) + ) + + if (independent) { + Column(modifier = Modifier.padding(vertical = 8.dp)) { + if (title != null) { + Text( + text = title, + style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), + modifier = Modifier.padding( + start = 16.dp, + end = 16.dp, + top = 8.dp, + bottom = 4.dp + ) + ) + } + + Box( + modifier = Modifier + .background(animatedBackgroundColor, RoundedCornerShape(28.dp)) + .padding(4.dp) + .pointerInput(Unit) { + detectTapGestures( + onPress = { + if (enabled) { + backgroundColor = + if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9) + tryAwaitRelease() + backgroundColor = + if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + } + }, + onTap = { + if (enabled) { + scope.launch { haptics.performHapticFeedback(if (!currentChecked) HapticFeedbackType.ToggleOn else HapticFeedbackType.ToggleOff) } + onCheckedChange(!currentChecked) + } + } + ) + } + ) { + Row( + modifier = Modifier + .fillMaxWidth() + .height(55.dp) + .padding(horizontal = 12.dp), + verticalAlignment = Alignment.CenterVertically + ) { + Text( + text = label, + modifier = Modifier.weight(1f), + style = TextStyle( + fontSize = 16.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Normal, + color = textColor + ) + ) + + StyledSwitch( + checked = checked, + enabled = enabled, + onCheckedChange = { + if (enabled) { + scope.launch { haptics.performHapticFeedback(if (it) HapticFeedbackType.ToggleOn else HapticFeedbackType.ToggleOff) } + onCheckedChange(it) + } + } + ) + } + } + + if (description != null) { + Spacer(modifier = Modifier.height(8.dp)) + + Box( + modifier = Modifier + .padding(horizontal = 16.dp) + .background( + if (isDarkTheme) Color(0xFF000000) + else Color(0xFFF2F2F7) + ) + ) { + Text( + text = description, + style = TextStyle( + fontSize = 12.sp, + fontWeight = FontWeight.Light, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + } + } + } else { + val isPressed = remember { mutableStateOf(false) } + + Row( + modifier = Modifier + .fillMaxWidth() + .background( + shape = RoundedCornerShape(28.dp), + color = if (isPressed.value) Color(0xFFE0E0E0) else Color.Transparent + ) + .padding(16.dp) + .pointerInput(Unit) { + detectTapGestures( + onPress = { + isPressed.value = true + tryAwaitRelease() + isPressed.value = false + } + ) + } + .clickable( + indication = null, + interactionSource = remember { MutableInteractionSource() } + ) { + if (enabled) { + scope.launch { haptics.performHapticFeedback(if (!currentChecked) HapticFeedbackType.ToggleOn else HapticFeedbackType.ToggleOff) } + onCheckedChange(!currentChecked) + } + }, + verticalAlignment = Alignment.CenterVertically + ) { + Column( + modifier = Modifier + .weight(1f) + .padding(end = 4.dp) + ) { + Text( + text = label, + style = TextStyle( + fontSize = 16.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Normal, + color = textColor + ) + ) + + Spacer(modifier = Modifier.height(4.dp)) + + if (description != null) { + Text( + text = description, + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + } + + StyledSwitch( + checked = checked, + enabled = enabled, + onCheckedChange = { + if (enabled) { + onCheckedChange(it) + } + } + ) + } + } +} + +@Preview +@Composable +fun StyledTogglePreview() { + val checked = remember { mutableStateOf(false) } + StyledToggle( + label = "Example Toggle", + description = "This is an example description for the styled toggle.", + checked = checked.value, + onCheckedChange = { checked.value = !checked.value } + ) +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/composables/VerticalVolumeSlider.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/VerticalVolumeSlider.kt similarity index 89% rename from android/app/src/main/java/me/kavishdevar/librepods/composables/VerticalVolumeSlider.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/components/VerticalVolumeSlider.kt index 8929ca649..37bddd8e6 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/composables/VerticalVolumeSlider.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/components/VerticalVolumeSlider.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.composables + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.components import androidx.compose.animation.core.Spring import androidx.compose.animation.core.animateFloatAsState diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/IslandWindow.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/overlays/IslandWindow.kt similarity index 94% rename from android/app/src/main/java/me/kavishdevar/librepods/utils/IslandWindow.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/overlays/IslandWindow.kt index 0d143a7e6..bf5eff89e 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/IslandWindow.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/overlays/IslandWindow.kt @@ -1,24 +1,24 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) -package me.kavishdevar.librepods.utils +package me.kavishdevar.librepods.presentation.overlays import android.animation.Animator import android.animation.AnimatorListenerAdapter @@ -33,6 +33,7 @@ import android.content.IntentFilter import android.content.res.Resources import android.graphics.PixelFormat import android.graphics.drawable.GradientDrawable +import android.media.AudioManager import android.os.Build import android.os.Handler import android.os.Looper @@ -58,10 +59,10 @@ import androidx.dynamicanimation.animation.DynamicAnimation import androidx.dynamicanimation.animation.SpringAnimation import androidx.dynamicanimation.animation.SpringForce import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.constants.AirPodsNotifications -import me.kavishdevar.librepods.constants.Battery -import me.kavishdevar.librepods.constants.BatteryComponent -import me.kavishdevar.librepods.constants.BatteryStatus +import me.kavishdevar.librepods.data.AirPodsNotifications +import me.kavishdevar.librepods.data.Battery +import me.kavishdevar.librepods.data.BatteryComponent +import me.kavishdevar.librepods.data.BatteryStatus import me.kavishdevar.librepods.services.ServiceManager import kotlin.io.encoding.ExperimentalEncodingApi import kotlin.math.abs @@ -240,6 +241,7 @@ class IslandWindow(private val context: Context) { FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT ) + containerView.addView(islandView, containerParams) params = WindowManager.LayoutParams( @@ -373,13 +375,18 @@ class IslandWindow(private val context: Context) { val videoView = islandView.findViewById(R.id.island_video_view) val videoUri = "android.resource://me.kavishdevar.librepods/${R.raw.island}".toUri() + videoView.setAudioFocusRequest(AudioManager.AUDIOFOCUS_NONE) videoView.setVideoURI(videoUri) videoView.setOnPreparedListener { mediaPlayer -> mediaPlayer.isLooping = true videoView.start() } - windowManager.addView(containerView, params) + try { + windowManager.addView(containerView, params) + } catch (e: Exception) { + e.printStackTrace() + } islandView.post { initialHeight = islandView.height @@ -707,8 +714,16 @@ class IslandWindow(private val context: Context) { } isClosing = false // Make sure all animations are canceled - springAnimation.cancel() - flingAnimator.cancel() + try { + springAnimation.cancel() + } catch (e: Exception) { + e("IslandWindow", "Error cancelling spring animation $e") + } + try { + flingAnimator.cancel() + } catch (e: Exception) { + e("IslandWindow", "Error cancelling fling animation $e") + } } fun forceClose() { diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/PopupWindow.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/overlays/PopupWindow.kt similarity index 84% rename from android/app/src/main/java/me/kavishdevar/librepods/utils/PopupWindow.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/overlays/PopupWindow.kt index 1d54aa951..4247ea47a 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/PopupWindow.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/overlays/PopupWindow.kt @@ -1,23 +1,23 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - - -package me.kavishdevar.librepods.utils + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + + +package me.kavishdevar.librepods.presentation.overlays import android.animation.Animator import android.animation.AnimatorListenerAdapter @@ -29,6 +29,7 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.graphics.PixelFormat +import android.media.AudioManager import android.os.Build import android.os.Handler import android.os.Looper @@ -45,10 +46,10 @@ import android.widget.LinearLayout import android.widget.TextView import android.widget.VideoView import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.constants.AirPodsNotifications -import me.kavishdevar.librepods.constants.Battery -import me.kavishdevar.librepods.constants.BatteryComponent -import me.kavishdevar.librepods.constants.BatteryStatus +import me.kavishdevar.librepods.data.AirPodsNotifications +import me.kavishdevar.librepods.data.Battery +import me.kavishdevar.librepods.data.BatteryComponent +import me.kavishdevar.librepods.data.BatteryStatus @SuppressLint("InflateParams", "ClickableViewAccessibility") class PopupWindow( @@ -64,10 +65,16 @@ class PopupWindow( @Suppress("DEPRECATION") private val mParams: WindowManager.LayoutParams = WindowManager.LayoutParams().apply { height = WindowManager.LayoutParams.WRAP_CONTENT - width = WindowManager.LayoutParams.MATCH_PARENT + val displayMetrics = context.resources.displayMetrics + val screenWidthDp = displayMetrics.widthPixels / displayMetrics.density + width = if (screenWidthDp >= 600) { + (400 * displayMetrics.density).toInt() + } else { + WindowManager.LayoutParams.MATCH_PARENT + } type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY format = PixelFormat.TRANSLUCENT - gravity = Gravity.BOTTOM + gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL dimAmount = 0.3f flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS or WindowManager.LayoutParams.FLAG_FULLSCREEN or @@ -84,7 +91,6 @@ class PopupWindow( mParams.x = 0 mParams.y = 0 - mParams.gravity = Gravity.BOTTOM mView.setOnClickListener { close() } @@ -132,6 +138,7 @@ class PopupWindow( updateBatteryStatus(batteryNotification) val vid = mView.findViewById(R.id.video) + vid.setAudioFocusRequest(AudioManager.AUDIOFOCUS_NONE) vid.setVideoPath("android.resource://me.kavishdevar.librepods/" + R.raw.connected) vid.resolveAdjustedSize(vid.width, vid.height) vid.start() @@ -139,7 +146,11 @@ class PopupWindow( vid.start() } - mWindowManager.addView(mView, mParams) + try { + mWindowManager.addView(mView, mParams) + } catch (e: Exception) { + e.printStackTrace() + } val displayMetrics = mView.context.resources.displayMetrics val screenHeight = displayMetrics.heightPixels diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AccessibilitySettingsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AccessibilitySettingsScreen.kt new file mode 100644 index 000000000..7988d910b --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AccessibilitySettingsScreen.kt @@ -0,0 +1,844 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens + +// import me.kavishdevar.librepods.utils.RadareOffsetFinder +import android.annotation.SuppressLint +import android.util.Log +import androidx.compose.foundation.background +import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress +import androidx.compose.foundation.gestures.detectTapGestures +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableLongStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.hapticfeedback.HapticFeedbackType +import androidx.compose.ui.input.pointer.PointerEventPass +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.layout.onGloballyPositioned +import androidx.compose.ui.layout.positionInParent +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalHapticFeedback +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.navigation.NavController +import com.kyant.backdrop.backdrops.layerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import dev.chrisbanes.haze.HazeState +import dev.chrisbanes.haze.hazeSource +import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.bluetooth.ATTHandles +import me.kavishdevar.librepods.data.Capability +import me.kavishdevar.librepods.presentation.components.NavigationButton +import me.kavishdevar.librepods.presentation.components.StyledButton +import me.kavishdevar.librepods.presentation.components.StyledDropdown +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledSlider +import me.kavishdevar.librepods.presentation.components.StyledToggle +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel +import kotlin.io.encoding.ExperimentalEncodingApi + +private var phoneMediaDebounceJob: Job? = null +private var toneVolumeDebounceJob: Job? = null +//private const val TAG = "AccessibilitySettings" + +@SuppressLint("DefaultLocale") +@ExperimentalHazeMaterialsApi +@OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) +@Composable +fun AccessibilitySettingsScreen(viewModel: AirPodsViewModel, navController: NavController) { + val state by viewModel.uiState.collectAsState() + + val isDarkTheme = isSystemInDarkTheme() + val textColor = if (isDarkTheme) Color.White else Color.Black + + val hearingAidEnabled = + state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID]?.getOrNull( + 1 + ) + ?.toInt() == 1 && state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID]?.getOrNull( + 0 + )?.toInt() == 1 + + val backdrop = rememberLayerBackdrop() + + StyledScaffold( + title = stringResource(R.string.accessibility) + ) { topPadding, hazeState, bottomPadding -> + Column( + modifier = Modifier + .fillMaxSize() + .hazeSource(hazeState) + .layerBackdrop(backdrop) + .verticalScroll(rememberScrollState()) + .padding(horizontal = 16.dp), + verticalArrangement = Arrangement.spacedBy(16.dp) + ) { + Spacer(modifier = Modifier.height(topPadding)) + + if (!state.isPremium) { + StyledButton( + onClick = { + navController.navigate("purchase_screen") + }, + backdrop = rememberLayerBackdrop(), + modifier = Modifier.fillMaxWidth(), + maxScale = 0.05f, + surfaceColor = if (isSystemInDarkTheme()) Color(0xFF916100) else Color(0xFFE59900) + ) { + Text( + stringResource(R.string.unlock_advanced_features), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = Color.White + ), + ) + } + } + +// val phoneMediaEQ = remember { mutableStateOf(FloatArray(8) { 0.5f }) } +// val phoneEQEnabled = remember { mutableStateOf(false) } +// val mediaEQEnabled = remember { mutableStateOf(false) } + + val pressSpeedOptions = mapOf( + 0.toByte() to stringResource(R.string.default_option), + 1.toByte() to stringResource(R.string.slower), + 2.toByte() to stringResource(R.string.slowest) + ) + + val selectedPressSpeedValue = + state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.DOUBLE_CLICK_INTERVAL]?.getOrNull( + 0 + ) + var selectedPressSpeed by remember { + mutableStateOf( + pressSpeedOptions[selectedPressSpeedValue] ?: pressSpeedOptions[0] + ) + } + + val pressAndHoldDurationOptions = mapOf( + 0.toByte() to stringResource(R.string.default_option), + 1.toByte() to stringResource(R.string.slower), + 2.toByte() to stringResource(R.string.slowest) + ) + + val selectedPressAndHoldDurationValue = + state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.CLICK_HOLD_INTERVAL]?.getOrNull( + 0 + ) + var selectedPressAndHoldDuration by remember { + mutableStateOf( + pressAndHoldDurationOptions[selectedPressAndHoldDurationValue] + ?: pressAndHoldDurationOptions[0] + ) + } + + val volumeSwipeSpeedOptions = mapOf( + 1.toByte() to stringResource(R.string.default_option), + 2.toByte() to stringResource(R.string.longer), + 3.toByte() to stringResource(R.string.longest) + ) + val selectedVolumeSwipeSpeedValue = + state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.VOLUME_SWIPE_INTERVAL]?.getOrNull( + 0 + ) + var selectedVolumeSwipeSpeed by remember { + mutableStateOf( + volumeSwipeSpeedOptions[selectedVolumeSwipeSpeedValue] + ?: volumeSwipeSpeedOptions[1] + ) + } + + val phoneMediaEQ = remember { mutableStateOf(FloatArray(8) { 0.5f }) } + val phoneEQEnabled = remember { mutableStateOf(false) } + val mediaEQEnabled = remember { mutableStateOf(false) } + + LaunchedEffect(phoneMediaEQ.value, phoneEQEnabled.value, mediaEQEnabled.value) { + phoneMediaDebounceJob?.cancel() + phoneMediaDebounceJob = CoroutineScope(Dispatchers.IO).launch { + delay(150) + try { + val phoneByte = if (phoneEQEnabled.value) 0x01.toByte() else 0x02.toByte() + val mediaByte = if (mediaEQEnabled.value) 0x01.toByte() else 0x02.toByte() + Log.d( + "AccessibilitySettingsScreen", + "Sending phone/media EQ (phoneEnabled=${phoneEQEnabled.value}, mediaEnabled=${mediaEQEnabled.value})" + ) + viewModel.sendPhoneMediaEQ(phoneMediaEQ.value, phoneByte, mediaByte) + } catch (e: Exception) { + Log.w( + "AccessibilitySettingsScreen", + "Error sending phone/media EQ: ${e.message}" + ) + } + } + } + Box( + modifier = Modifier.then( + if (!state.isPremium) { + Modifier.pointerInput(Unit) { + awaitPointerEventScope { + while (true) { + val event = awaitPointerEvent(PointerEventPass.Initial) + event.changes.forEach { it.consume() } + } + } + } + } else Modifier)) { + DropdownMenuComponent( + label = stringResource(R.string.press_speed), + description = stringResource(R.string.press_speed_description), + options = pressSpeedOptions.values.toList(), + selectedOption = selectedPressSpeed ?: stringResource(R.string.default_option), + onOptionSelected = { newValue -> + selectedPressSpeed = newValue + viewModel.setControlCommandByte( + identifier = AACPManager.Companion.ControlCommandIdentifiers.DOUBLE_CLICK_INTERVAL, + value = pressSpeedOptions.filterValues { it == newValue }.keys.firstOrNull() + ?: 0.toByte() + ) + }, + textColor = textColor, + hazeState = hazeState, + independent = true + ) + } + + Box( + modifier = Modifier.then( + if (!state.isPremium) { + Modifier.pointerInput(Unit) { + awaitPointerEventScope { + while (true) { + val event = awaitPointerEvent(PointerEventPass.Initial) + event.changes.forEach { it.consume() } + } + } + } + } else Modifier)) { + DropdownMenuComponent( + label = stringResource(R.string.press_and_hold_duration), + description = stringResource(R.string.press_and_hold_duration_description), + options = pressAndHoldDurationOptions.values.toList(), + selectedOption = selectedPressAndHoldDuration + ?: stringResource(R.string.default_option), + onOptionSelected = { newValue -> + selectedPressAndHoldDuration = newValue + viewModel.setControlCommandByte( + identifier = AACPManager.Companion.ControlCommandIdentifiers.CLICK_HOLD_INTERVAL, + value = pressAndHoldDurationOptions.filterValues { it == newValue }.keys.firstOrNull() + ?: 0.toByte() + ) + }, + textColor = textColor, + hazeState = hazeState, + independent = true + ) + } + StyledToggle( + title = stringResource(R.string.noise_control), + label = stringResource(R.string.noise_cancellation_single_airpod), + description = stringResource(R.string.noise_cancellation_single_airpod_description), + independent = true, + checked = state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.ONE_BUD_ANC_MODE]?.getOrNull( + 0 + ) == 0x01.toByte(), + onCheckedChange = { + viewModel.setControlCommandBoolean( + AACPManager.Companion.ControlCommandIdentifiers.ONE_BUD_ANC_MODE, it + ) + }, + enabled = state.isPremium + ) + + if (state.capabilities.contains(Capability.LOUD_SOUND_REDUCTION) && state.vendorIdHook) { + StyledToggle( + label = stringResource(R.string.loud_sound_reduction), + description = stringResource(R.string.loud_sound_reduction_description), + checked = state.loudSoundReductionEnabled, + onCheckedChange = { + viewModel.setATTCharacteristicValue( + ATTHandles.LOUD_SOUND_REDUCTION, + if (it) byteArrayOf(0x01) else byteArrayOf(0x00) + ) + }, + enabled = state.isPremium + ) + } + + if (!hearingAidEnabled && state.vendorIdHook) { + NavigationButton( + to = "transparency_customization", + name = stringResource(R.string.customize_transparency_mode), + navController = navController, + enabled = state.isPremium + ) + } + + val toneVolumeValue = + state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.CHIME_VOLUME]?.getOrNull( + 0 + )?.toFloat() ?: 75f + StyledSlider( + label = stringResource(R.string.tone_volume), + description = stringResource(R.string.tone_volume_description), + value = toneVolumeValue, + onValueChange = { + viewModel.setControlCommandValue( + AACPManager.Companion.ControlCommandIdentifiers.CHIME_VOLUME, + byteArrayOf(it.toInt().toByte(), 0x50) + ) + }, + valueRange = 0f..100f, + snapPoints = listOf(75f), + startIcon = "\uDBC0\uDEA1", + endIcon = "\uDBC0\uDEA9", + independent = true, + enabled = state.isPremium + ) + + if (state.capabilities.contains(Capability.SWIPE_FOR_VOLUME)) { + val volumeSwipeEnabled = + state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.VOLUME_SWIPE_MODE]?.getOrNull( + 0 + )?.toInt() == 0x01 + StyledToggle( + label = stringResource(R.string.volume_control), + description = stringResource(R.string.volume_control_description), + checked = volumeSwipeEnabled, + onCheckedChange = { + viewModel.setControlCommandBoolean( + AACPManager.Companion.ControlCommandIdentifiers.VOLUME_SWIPE_MODE, it + ) + }, + enabled = state.isPremium + ) + + Box( + modifier = Modifier.then( + if (!state.isPremium) { + Modifier.pointerInput(Unit) { + awaitPointerEventScope { + while (true) { + val event = awaitPointerEvent(PointerEventPass.Initial) + event.changes.forEach { it.consume() } + } + } + } + } else Modifier)) { + DropdownMenuComponent( + label = stringResource(R.string.volume_swipe_speed), + description = stringResource(R.string.volume_swipe_speed_description), + options = volumeSwipeSpeedOptions.values.toList(), + selectedOption = selectedVolumeSwipeSpeed + ?: stringResource(R.string.default_option), + onOptionSelected = { newValue -> + selectedVolumeSwipeSpeed = newValue + viewModel.setControlCommandByte( + identifier = AACPManager.Companion.ControlCommandIdentifiers.VOLUME_SWIPE_INTERVAL, + value = volumeSwipeSpeedOptions.filterValues { it == newValue }.keys.firstOrNull() + ?: 1.toByte() + ) + }, + textColor = textColor, + hazeState = hazeState, + independent = true + ) + } + } + +// if (!hearingAidEnabled && XposedState.isAvailable) { +// Text( +// text = stringResource(R.string.apply_eq_to), style = TextStyle( +// fontSize = 14.sp, +// fontWeight = FontWeight.Bold, +// color = textColor.copy(alpha = 0.6f), +// fontFamily = FontFamily(Font(R.font.sf_pro)) +// ), modifier = Modifier.padding(8.dp, bottom = 0.dp) +// ) +// Column( +// modifier = Modifier +// .fillMaxWidth() +// .background( +// if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF), +// RoundedCornerShape(28.dp) +// ) +// .padding(vertical = 0.dp) +// ) { +// val darkModeLocal = isSystemInDarkTheme() +// +// val phoneShape = RoundedCornerShape(topStart = 28.dp, topEnd = 28.dp) +// var phoneBackgroundColor by remember { +// mutableStateOf( +// if (darkModeLocal) Color( +// 0xFF1C1C1E +// ) else Color(0xFFFFFFFF) +// ) +// } +// val phoneAnimatedBackgroundColor by animateColorAsState( +// targetValue = phoneBackgroundColor, +// animationSpec = tween(durationMillis = 500) +// ) +// +// Row( +// modifier = Modifier +// .height(48.dp) +// .fillMaxWidth() +// .background(phoneAnimatedBackgroundColor, phoneShape) +// .pointerInput(Unit) { +// detectTapGestures( +// onPress = { +// phoneBackgroundColor = +// if (darkModeLocal) Color(0x40888888) else Color( +// 0x40D9D9D9 +// ) +// tryAwaitRelease() +// phoneBackgroundColor = +// if (darkModeLocal) Color(0xFF1C1C1E) else Color( +// 0xFFFFFFFF +// ) +// phoneEQEnabled.value = !phoneEQEnabled.value +// }) +// } +// .padding(horizontal = 16.dp), +// verticalAlignment = Alignment.CenterVertically) { +// Text( +// stringResource(R.string.phone), +// fontSize = 16.sp, +// color = textColor, +// fontFamily = FontFamily(Font(R.font.sf_pro)), +// modifier = Modifier.weight(1f) +// ) +// Checkbox( +// checked = phoneEQEnabled.value, +// onCheckedChange = { phoneEQEnabled.value = it }, +// colors = CheckboxDefaults.colors().copy( +// checkedCheckmarkColor = Color(0xFF007AFF), +// uncheckedCheckmarkColor = Color.Transparent, +// checkedBoxColor = Color.Transparent, +// uncheckedBoxColor = Color.Transparent, +// checkedBorderColor = Color.Transparent, +// uncheckedBorderColor = Color.Transparent +// ), +// modifier = Modifier +// .height(24.dp) +// .scale(1.5f) +// ) +// } +// +// HorizontalDivider( +// thickness = 1.dp, color = Color(0x40888888) +// ) +// +// val mediaShape = RoundedCornerShape(bottomStart = 28.dp, bottomEnd = 28.dp) +// var mediaBackgroundColor by remember { +// mutableStateOf( +// if (darkModeLocal) Color( +// 0xFF1C1C1E +// ) else Color(0xFFFFFFFF) +// ) +// } +// val mediaAnimatedBackgroundColor by animateColorAsState( +// targetValue = mediaBackgroundColor, +// animationSpec = tween(durationMillis = 500) +// ) +// +// Row( +// modifier = Modifier +// .height(48.dp) +// .fillMaxWidth() +// .background(mediaAnimatedBackgroundColor, mediaShape) +// .pointerInput(Unit) { +// detectTapGestures( +// onPress = { +// mediaBackgroundColor = +// if (darkModeLocal) Color(0x40888888) else Color( +// 0x40D9D9D9 +// ) +// tryAwaitRelease() +// mediaBackgroundColor = +// if (darkModeLocal) Color(0xFF1C1C1E) else Color( +// 0xFFFFFFFF +// ) +// mediaEQEnabled.value = !mediaEQEnabled.value +// }) +// } +// .padding(horizontal = 16.dp), +// verticalAlignment = Alignment.CenterVertically) { +// Text( +// stringResource(R.string.media), +// fontSize = 16.sp, +// color = textColor, +// fontFamily = FontFamily(Font(R.font.sf_pro)), +// modifier = Modifier.weight(1f) +// ) +// Checkbox( +// checked = mediaEQEnabled.value, +// onCheckedChange = { mediaEQEnabled.value = it }, +// colors = CheckboxDefaults.colors().copy( +// checkedCheckmarkColor = Color(0xFF007AFF), +// uncheckedCheckmarkColor = Color.Transparent, +// checkedBoxColor = Color.Transparent, +// uncheckedBoxColor = Color.Transparent, +// checkedBorderColor = Color.Transparent, +// uncheckedBorderColor = Color.Transparent +// ), +// modifier = Modifier +// .height(24.dp) +// .scale(1.5f) +// ) +// } +// } +// +//// EQ Settings. Don't seem to have an effect? +// Column( +// modifier = Modifier +// .fillMaxWidth() +// .background( +// if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF), +// RoundedCornerShape(28.dp) +// ) +// .padding(12.dp), horizontalAlignment = Alignment.CenterHorizontally +// ) { +// val trackColor = if (isDarkTheme) Color(0xFFB3B3B3) else Color(0xFF929491) +// val activeTrackColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5) +// val thumbColor = if (isDarkTheme) Color(0xFFFFFFFF) else Color(0xFFFFFFFF) +// +// for (i in 0 until 8) { +// val eqPhoneValue = +// remember(phoneMediaEQ.value[i]) { mutableFloatStateOf(phoneMediaEQ.value[i]) } +// Row( +// horizontalArrangement = Arrangement.SpaceBetween, +// verticalAlignment = Alignment.CenterVertically, +// modifier = Modifier +// .fillMaxWidth() +// .height(38.dp) +// ) { +// Text( +// text = String.format("%.2f", eqPhoneValue.floatValue), +// fontSize = 12.sp, +// color = textColor, +// modifier = Modifier.padding(bottom = 4.dp) +// ) +// +// Slider( +// value = eqPhoneValue.floatValue, +// onValueChange = { newVal -> +// eqPhoneValue.floatValue = newVal +// val newEQ = phoneMediaEQ.value.copyOf() +// newEQ[i] = eqPhoneValue.floatValue +// phoneMediaEQ.value = newEQ +// }, +// valueRange = 0f..100f, +// modifier = Modifier +// .fillMaxWidth(0.9f) +// .height(36.dp), +// colors = SliderDefaults.colors( +// thumbColor = thumbColor, +// activeTrackColor = activeTrackColor, +// inactiveTrackColor = trackColor +// ), +// thumb = { +// Box( +// modifier = Modifier +// .size(24.dp) +// .shadow(4.dp, CircleShape) +// .background(thumbColor, CircleShape) +// ) +// }, +// track = { +// Box( +// modifier = Modifier +// .fillMaxWidth() +// .height(12.dp), +// contentAlignment = Alignment.CenterStart +// ) { +// Box( +// modifier = Modifier +// .fillMaxWidth() +// .height(4.dp) +// .background(trackColor, RoundedCornerShape(4.dp)) +// ) +// Box( +// modifier = Modifier +// .fillMaxWidth(eqPhoneValue.floatValue / 100f) +// .height(4.dp) +// .background( +// activeTrackColor, RoundedCornerShape(4.dp) +// ) +// ) +// } +// }) +// +// Text( +// text = stringResource(R.string.band_label, i + 1), +// fontSize = 12.sp, +// color = textColor, +// modifier = Modifier.padding(top = 4.dp) +// ) +// } +// } +// } +// } + Spacer(modifier = Modifier.height(bottomPadding)) + } + } +} + +@ExperimentalHazeMaterialsApi +@Composable +private fun DropdownMenuComponent( + label: String, + options: List, + selectedOption: String, + onOptionSelected: (String) -> Unit, + textColor: Color, + hazeState: HazeState, + description: String? = null, + independent: Boolean = true +) { + val density = LocalDensity.current + val itemHeightPx = with(density) { 48.dp.toPx() } + + var expanded by remember { mutableStateOf(false) } + var touchOffset by remember { mutableStateOf(null) } + var boxPosition by remember { mutableStateOf(Offset.Zero) } + var lastDismissTime by remember { mutableLongStateOf(0L) } + var parentHoveredIndex by remember { mutableStateOf(null) } + var parentDragActive by remember { mutableStateOf(false) } + var previousIdx by remember { mutableStateOf(null) } + val haptics = LocalHapticFeedback.current + val scope = rememberCoroutineScope() + + Column(modifier = Modifier.fillMaxWidth()) { + Column( + modifier = Modifier + .fillMaxWidth() + .then( + if (independent) { + if (description != null) { + Modifier.padding(top = 8.dp, bottom = 4.dp) + } else { + Modifier.padding(vertical = 8.dp) + } + } else Modifier + ) + .background( + if (independent) (if (isSystemInDarkTheme()) Color(0xFF1C1C1E) else Color( + 0xFFFFFFFF + )) else Color.Transparent, + if (independent) RoundedCornerShape(28.dp) else RoundedCornerShape(0.dp) + ) then (if (independent) Modifier.padding(horizontal = 4.dp) else Modifier).clip( + if (independent) RoundedCornerShape(28.dp) else RoundedCornerShape(0.dp) + ) + ) { + Row( + modifier = Modifier + .fillMaxWidth() + .padding(start = 12.dp, end = 12.dp) + .height(58.dp) + .pointerInput(Unit) { + detectTapGestures { offset -> + val now = System.currentTimeMillis() + if (expanded) { + expanded = false + lastDismissTime = now + } else { + if (now - lastDismissTime > 250L) { + touchOffset = offset + expanded = true + } + } + } + } + .pointerInput(Unit) { + detectDragGesturesAfterLongPress(onDragStart = { offset -> + val now = System.currentTimeMillis() + touchOffset = offset + if (!expanded && now - lastDismissTime > 250L) { + expanded = true + } + lastDismissTime = now + parentDragActive = true + parentHoveredIndex = 0 + }, onDrag = { change, _ -> + val current = change.position + val touch = touchOffset ?: current + val posInPopupY = current.y - touch.y + val idx = (posInPopupY / itemHeightPx).toInt() + if (idx != previousIdx) { + scope.launch { + haptics.performHapticFeedback( + HapticFeedbackType.SegmentTick + ) + } + } + parentHoveredIndex = idx + previousIdx = idx + }, onDragEnd = { + parentDragActive = false + parentHoveredIndex?.let { idx -> + if (idx in options.indices) { + onOptionSelected(options[idx]) + expanded = false + lastDismissTime = System.currentTimeMillis() + } + } + if (parentHoveredIndex != null && parentHoveredIndex in options.indices) { + scope.launch { + haptics.performHapticFeedback( + HapticFeedbackType.GestureEnd + ) + } + } + parentHoveredIndex = null + }, onDragCancel = { + parentDragActive = false + parentHoveredIndex = null + }) + }, + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically + ) { + Column( + modifier = Modifier.weight(1f) + ) { + Text( + text = label, + fontSize = 16.sp, + color = textColor, + modifier = Modifier.padding(bottom = 4.dp) + ) + if (!independent && description != null) { + Text( + text = description, style = TextStyle( + fontSize = 12.sp, + fontWeight = FontWeight.Light, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), modifier = Modifier.padding(16.dp, top = 0.dp, bottom = 2.dp) + ) + } + } + Box( + modifier = Modifier.onGloballyPositioned { coordinates -> + boxPosition = coordinates.positionInParent() + }) { + Row( + verticalAlignment = Alignment.CenterVertically + ) { + Text( + text = selectedOption, style = TextStyle( + fontSize = 16.sp, + color = textColor.copy(alpha = 0.8f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + Text( + text = "􀆏", style = TextStyle( + fontSize = 16.sp, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), modifier = Modifier.padding(start = 6.dp) + ) + } + + StyledDropdown( + expanded = expanded, + onDismissRequest = { + expanded = false + lastDismissTime = System.currentTimeMillis() + }, + options = options, + selectedOption = selectedOption, + touchOffset = touchOffset, + boxPosition = boxPosition, + externalHoveredIndex = parentHoveredIndex, + externalDragActive = parentDragActive, + onOptionSelected = { option -> + onOptionSelected(option) + expanded = false + }, + hazeState = hazeState + ) + } + } + } + if (independent && description != null) { + Box( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 16.dp) + .background( + if (isSystemInDarkTheme()) Color(0xFF000000) else Color(0xFFF2F2F7) + ) + ) { + Text( + text = description, style = TextStyle( + fontSize = 12.sp, + fontWeight = FontWeight.Light, + color = (if (isSystemInDarkTheme()) Color.White else Color.Black).copy( + alpha = 0.6f + ), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AdaptiveStrengthScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AdaptiveStrengthScreen.kt new file mode 100644 index 000000000..466a790da --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AdaptiveStrengthScreen.kt @@ -0,0 +1,126 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens + +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableFloatStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.navigation.NavController +import com.kyant.backdrop.backdrops.layerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.presentation.components.StyledButton +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledSlider +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel + +@Composable +fun AdaptiveStrengthScreen(viewModel: AirPodsViewModel, navController: NavController) { + val state by viewModel.uiState.collectAsState() + val backdrop = rememberLayerBackdrop() + + StyledScaffold( + title = stringResource(R.string.customize_adaptive_audio) + ) { spacerHeight -> + Column( + modifier = Modifier + .fillMaxSize() + .layerBackdrop(backdrop) + .padding(horizontal = 16.dp), + verticalArrangement = Arrangement.spacedBy(16.dp) + ) { + Spacer(modifier = Modifier.height(spacerHeight)) + if (!state.isPremium) { + StyledButton( + onClick = { + navController.navigate("purchase_screen") + }, + backdrop = rememberLayerBackdrop(), + modifier = Modifier.fillMaxWidth(), + maxScale = 0.05f, + surfaceColor = if (isSystemInDarkTheme()) Color(0xFF916100) else Color(0xFFE59900) + ) { + Text( + stringResource(R.string.unlock_advanced_features), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = Color.White + ), + ) + } + } + val sliderValue = remember { + mutableFloatStateOf(100f - (state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.AUTO_ANC_STRENGTH]?.getOrNull(0)?.toFloat() ?: 50f)) + } + var job by remember { mutableStateOf(null) } + val scope = rememberCoroutineScope() + StyledSlider( + label = stringResource(R.string.customize_adaptive_audio), + value = sliderValue.floatValue, + onValueChange = { + sliderValue.floatValue = it + job?.cancel() + job = scope.launch { + delay(150) + viewModel.setControlCommandValue( + AACPManager.Companion.ControlCommandIdentifiers.AUTO_ANC_STRENGTH, + byteArrayOf((100 - it).toInt().toByte()) + ) + } + }, + valueRange = 0f..100f, + snapPoints = listOf(0f, 50f, 100f), + startIcon = "􀊥", + endIcon = "􀊩", + independent = true, + description = stringResource(R.string.adaptive_audio_description), + enabled = state.isPremium + ) + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AirPodsSettingsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AirPodsSettingsScreen.kt new file mode 100644 index 000000000..aeb11e483 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AirPodsSettingsScreen.kt @@ -0,0 +1,620 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +@file:OptIn(ExperimentalEncodingApi::class) + +package me.kavishdevar.librepods.presentation.screens + +// import me.kavishdevar.librepods.utils.RadareOffsetFinder +import android.annotation.SuppressLint +import android.content.Context.MODE_PRIVATE +import android.content.Intent +import android.content.SharedPreferences +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.gestures.detectTapGestures +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.SnackbarHostState +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableLongStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.input.pointer.PointerEventPass +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.input.TextFieldValue +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.core.net.toUri +import androidx.navigation.NavController +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import com.kyant.backdrop.drawBackdrop +import com.kyant.backdrop.highlight.Highlight +import dev.chrisbanes.haze.HazeState +import dev.chrisbanes.haze.hazeSource +import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi +import kotlinx.coroutines.delay +import me.kavishdevar.librepods.BuildConfig +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.bluetooth.ATTHandles +import me.kavishdevar.librepods.data.AirPodsPro3 +import me.kavishdevar.librepods.data.Capability +import me.kavishdevar.librepods.presentation.components.AboutCard +import me.kavishdevar.librepods.presentation.components.AudioSettings +import me.kavishdevar.librepods.presentation.components.BatteryView +import me.kavishdevar.librepods.presentation.components.CallControlSettings +import me.kavishdevar.librepods.presentation.components.ConnectionSettings +import me.kavishdevar.librepods.presentation.components.HearingHealthSettings +import me.kavishdevar.librepods.presentation.components.MicrophoneSettings +import me.kavishdevar.librepods.presentation.components.NavigationButton +import me.kavishdevar.librepods.presentation.components.NoiseControlSettings +import me.kavishdevar.librepods.presentation.components.PressAndHoldSettings +import me.kavishdevar.librepods.presentation.components.StyledButton +import me.kavishdevar.librepods.presentation.components.StyledIconButton +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledToggle +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel +import java.util.concurrent.TimeUnit +import kotlin.io.encoding.ExperimentalEncodingApi + +@OptIn(ExperimentalMaterial3Api::class, ExperimentalHazeMaterialsApi::class) +@SuppressLint("MissingPermission", "UnspecifiedRegisterReceiverFlag") +@Composable +fun AirPodsSettingsScreen(viewModel: AirPodsViewModel, navController: NavController) { + val state by viewModel.uiState.collectAsState() + val sharedPreferences = LocalContext.current.getSharedPreferences("settings", MODE_PRIVATE) + var deviceName by remember { + mutableStateOf( + TextFieldValue( + sharedPreferences.getString("name", state.deviceName).toString() + ) + ) + } + + val nameChangeListener = remember { + SharedPreferences.OnSharedPreferenceChangeListener { _, key -> + if (key == "name") { + deviceName = + TextFieldValue(sharedPreferences.getString("name", "AirPods Pro").toString()) + } + } + } + + DisposableEffect(Unit) { + sharedPreferences.registerOnSharedPreferenceChangeListener(nameChangeListener) + onDispose { + sharedPreferences.unregisterOnSharedPreferenceChangeListener(nameChangeListener) + } + } + + val snackbarHostState = remember { SnackbarHostState() } + + LaunchedEffect(Unit) { + viewModel.refreshInitialData() + } + + val hazeStateS = remember { mutableStateOf(HazeState()) } + + StyledScaffold( + title = deviceName.text, actionButtons = listOf( + { scaffoldBackdrop -> + StyledIconButton( + onClick = { navController.navigate("app_settings") }, + icon = "􀍟", + backdrop = scaffoldBackdrop + ) + }), snackbarHostState = snackbarHostState + ) { topPadding, hazeState, bottomPadding -> + hazeStateS.value = hazeState + var blockTouches by remember { mutableStateOf(false) } + + LaunchedEffect(Unit) { + viewModel.demoActivated.collect { + blockTouches = true + delay(1000) + blockTouches = false + } + } + + if (state.isLocallyConnected) { + val capabilities = state.capabilities + LazyColumn( + modifier = Modifier + .fillMaxSize() + .hazeSource(hazeState) + .padding(horizontal = 16.dp) + .then(if (blockTouches) Modifier.pointerInput(Unit) { + awaitPointerEventScope { + while (true) { + val event = awaitPointerEvent(PointerEventPass.Initial) + event.changes.forEach { it.consume() } + } + } + } else Modifier)) { + item(key = "spacer_top") { Spacer(modifier = Modifier.height(topPadding)) } + + item(key = "play_update_banner") { + if (state.timeUntilFOSSPremiumExpiry > 0L) { + val context = LocalContext.current + Box( + modifier = Modifier + .background(Color(0xFF32829B), RoundedCornerShape(28.dp)) + .clip(RoundedCornerShape(28.dp)) + .clickable { + val emailIntent = Intent(Intent.ACTION_SENDTO).apply { + data = "mailto:".toUri() + putExtra(Intent.EXTRA_EMAIL, arrayOf("billing@kavish.xyz")) + putExtra(Intent.EXTRA_SUBJECT, "LibrePods Play billing error") + putExtra( + Intent.EXTRA_TEXT, + "Please enter your GitHub username to restore your premium access:\n\nGitHub username: " + ) + } + context.startActivity(emailIntent) + } + ) { + Text( + text = stringResource( + R.string.play_foss_premium_banner, maxOf(1, TimeUnit.MILLISECONDS.toDays(state.timeUntilFOSSPremiumExpiry).toInt()) + ), + modifier = Modifier + .padding(16.dp), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Bold, + color = Color.White, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + } + } + + item(key = "battery") { + BatteryView( + batteryList = state.battery, + budsRes = state.instance?.model?.budsRes ?: R.drawable.airpods_pro_2_case, + caseRes = state.instance?.model?.caseRes ?: R.drawable.airpods_pro_2_case + ) + } + item(key = "spacer_battery") { Spacer(modifier = Modifier.height(32.dp)) } + + item(key = "name") { + NavigationButton( + to = "rename", + name = stringResource(R.string.name), + currentState = deviceName.text, + navController = navController, + independent = true + ) + } + + val hasHearingAidCapability = + state.instance?.model?.capabilities?.contains(Capability.HEARING_AID) == true + val hasPPECapability = + state.instance?.model?.capabilities?.contains(Capability.PPE) == true + + if (hasHearingAidCapability || hasPPECapability) { + if (hasPPECapability || (state.vendorIdHook && hasHearingAidCapability)) item( + key = "spacer_hearing_health" + ) { Spacer(modifier = Modifier.height(24.dp)) } + item(key = "hearing_health") { + HearingHealthSettings( + navController = navController, + hasPPECapability = hasPPECapability, + hasHearingAidCapability = hasHearingAidCapability, + vendorIdHook = state.vendorIdHook + ) + } + } + + if (capabilities.contains(Capability.LISTENING_MODE)) { + item(key = "spacer_noise") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "noise_control") { + NoiseControlSettings( + showOffListeningMode = state.offListeningMode, + noiseControlModeValue = state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE]?.getOrNull( + 0 + )?.toInt() ?: 3, + onNoiseControlModeChanged = { + viewModel.setControlCommandInt( + AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE, + it + ) + }, + ) + } + } + + if (capabilities.contains(Capability.STEM_CONFIG)) { + item(key = "spacer_press_hold") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "press_hold") { + PressAndHoldSettings( + navController = navController, + leftAction = state.leftAction, + rightAction = state.rightAction + ) + } + } + + item(key = "spacer_call") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "call_control") { + val bytes = state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.CALL_MANAGEMENT_CONFIG]?.take(2)?.toByteArray() ?: byteArrayOf(0x00, 0x00) + val flipped = try { bytes[1] == 0x02.toByte() } catch (e: Exception) { false } + CallControlSettings( + hazeState = hazeState, + flipped = flipped, + onCallControlValueChanged = { + viewModel.setControlCommandValue( + AACPManager.Companion.ControlCommandIdentifiers.CALL_MANAGEMENT_CONFIG, + if (it) byteArrayOf(0x00, 0x02) else byteArrayOf(0x00, 0x03) + ) + }) + } + +// if (capabilities.contains(Capability.STEM_CONFIG) && !BuildConfig.PLAY_BUILD) { +// item(key = "spacer_camera") { Spacer(modifier = Modifier.height(16.dp)) } +// item(key = "camera_control") { +// NavigationButton( +// to = "camera_control", +// name = stringResource(R.string.camera_remote), +// description = stringResource(R.string.camera_control_description), +// title = stringResource(R.string.camera_control), +// navController = navController +// ) +// } +// } + + item(key = "upgrade_button") { + if (!state.isPremium) { + Spacer(modifier = Modifier.height(28.dp)) + StyledButton( + onClick = { + navController.navigate("purchase_screen") + }, + backdrop = rememberLayerBackdrop(), + modifier = Modifier.fillMaxWidth(), + maxScale = 0.05f, + surfaceColor = if (isSystemInDarkTheme()) Color(0xFF916100) else Color( + 0xFFE59900 + ) + ) { + Text( + stringResource(R.string.unlock_advanced_features), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = Color.White + ), + ) + } + Spacer(modifier = Modifier.height(16.dp)) + } + } + + item(key = "spacer_audio") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "audio") { + val model = state.instance?.model ?: AirPodsPro3() + val adaptiveVolumeCapability = + model.capabilities.contains(Capability.ADAPTIVE_VOLUME) + val conversationalAwarenessCapability = + model.capabilities.contains(Capability.CONVERSATION_AWARENESS) + val loudSoundReductionCapability = + model.capabilities.contains(Capability.LOUD_SOUND_REDUCTION) + val adaptiveAudioCapability = + model.capabilities.contains(Capability.ADAPTIVE_VOLUME) + + val adaptiveVolumeChecked = + state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.ADAPTIVE_VOLUME_CONFIG]?.getOrNull( + 0 + ) == 0x01.toByte() + val conversationalAwarenessChecked = + state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.CONVERSATION_DETECT_CONFIG]?.getOrNull( + 0 + ) == 0x01.toByte() + + AudioSettings( + navController = navController, + adaptiveVolumeCapability = adaptiveVolumeCapability, + conversationalAwarenessCapability = conversationalAwarenessCapability, + loudSoundReductionCapability = loudSoundReductionCapability, + adaptiveAudioCapability = adaptiveAudioCapability, + customEqCapability = true, + adaptiveVolumeChecked = adaptiveVolumeChecked, + onAdaptiveVolumeCheckedChange = { checked -> + viewModel.setControlCommandBoolean( + AACPManager.Companion.ControlCommandIdentifiers.ADAPTIVE_VOLUME_CONFIG, + checked + ) + }, + conversationalAwarenessChecked = conversationalAwarenessChecked && state.isPremium, + onConversationalAwarenessCheckedChange = { checked -> + viewModel.setControlCommandBoolean( + AACPManager.Companion.ControlCommandIdentifiers.CONVERSATION_DETECT_CONFIG, + checked + ) + }, + loudSoundReductionChecked = state.loudSoundReductionEnabled, + onLoudSoundReductionCheckedChange = { + viewModel.setATTCharacteristicValue( + ATTHandles.LOUD_SOUND_REDUCTION, + byteArrayOf(if (it) 0x01.toByte() else 0x00.toByte()) + ) + }, + vendorIdHook = state.vendorIdHook, + isPremium = state.isPremium + ) + } + + item(key = "spacer_connection") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "connection") { + ConnectionSettings( + automaticEarDetectionEnabled = state.automaticEarDetectionEnabled, + onAutomaticEarDetectionChanged = { + viewModel.setAutomaticEarDetectionEnabled(it) + }, + automaticConnectionEnabled = state.automaticConnectionEnabled, + onAutomaticConnectionChanged = { viewModel.setAutomaticConnectionEnabled(it) }) + } + + item(key = "spacer_microphone") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "microphone") { + val id = AACPManager.Companion.ControlCommandIdentifiers.MIC_MODE + MicrophoneSettings( + hazeState = hazeState, + micModeValue = state.controlStates[id]?.getOrNull(0) ?: 0x00.toByte(), + onMicModeValueChanged = { viewModel.setControlCommandByte(id, it) }) + } + + if (capabilities.contains(Capability.SLEEP_DETECTION)) { + item(key = "spacer_sleep") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "sleep_detection") { + val id = + AACPManager.Companion.ControlCommandIdentifiers.SLEEP_DETECTION_CONFIG + StyledToggle( + label = stringResource(R.string.sleep_detection), + checked = state.controlStates[id]?.getOrNull(0) == 0x01.toByte(), + onCheckedChange = { + viewModel.setControlCommandBoolean(id, it) + }, + enabled = state.isPremium + ) + } + } + + if (capabilities.contains(Capability.HEAD_GESTURES)) { + item(key = "spacer_head_tracking") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "head_tracking") { + NavigationButton( + to = "head_tracking", + name = stringResource(R.string.head_gestures), + navController = navController, + currentState = if (sharedPreferences.getBoolean( + "head_gestures", false + ) + ) stringResource(R.string.on) else stringResource(R.string.off) + ) + } + } + + item(key = "spacer_dynamic_end_of_charge") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "dynamic_end_of_charge") { + StyledToggle( + label = stringResource(R.string.optimized_charging), + description = stringResource(R.string.optimized_charging_description), + checked = state.dynamicEndOfCharge, + onCheckedChange = viewModel::setDynamicEndOfCharge + ) + } + + item(key = "spacer_accessibility") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "accessibility") { + NavigationButton( + to = "accessibility", + name = stringResource(R.string.accessibility), + navController = navController + ) + } + + if (capabilities.contains(Capability.LOUD_SOUND_REDUCTION)) { + item(key = "spacer_off_listening") { Spacer(modifier = Modifier.height(16.dp)) } + item(key = "off_listening") { + val id = AACPManager.Companion.ControlCommandIdentifiers.ALLOW_OFF_OPTION + StyledToggle( + label = stringResource(R.string.off_listening_mode), + description = stringResource(R.string.off_listening_mode_description), + checked = state.controlStates[id]?.getOrNull(0) == 0x01.toByte(), + onCheckedChange = viewModel::setOffListeningMode + ) + } + } + + item(key = "spacer_about") { Spacer(modifier = Modifier.height(32.dp)) } + item(key = "about") { + AboutCard( + navController = navController, + modelName = state.modelName, + actualModel = state.actualModel, + serialNumbers = state.serialNumbers, + version = state.version3, + ) + } + + item(key = "spacer_disconnect") { Spacer(modifier = Modifier.height(28.dp)) } + item(key = "disconnect_button") { + StyledButton( + onClick = viewModel::disconnect, + backdrop = rememberLayerBackdrop(), + isInteractive = false, + modifier = Modifier + .fillMaxWidth() + .heightIn(min = 56.dp) + ) { + Text( + text = stringResource(R.string.disconnect), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + color = if (isSystemInDarkTheme()) Color(0xFF0091FF) else Color(0xFF0088FF), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), + textAlign = TextAlign.Start, + modifier = Modifier.fillMaxWidth() + ) + } + } + +// item(key = "spacer_debug") { Spacer(modifier = Modifier.height(16.dp)) } +// item(key = "debug") { NavigationButton("debug", "Debug", navController) } + item(key = "spacer_bottom") { Spacer(Modifier.height(bottomPadding)) } + } + } else { + val backdrop = rememberLayerBackdrop() + Column( + modifier = Modifier + .fillMaxSize() + .drawBackdrop( + backdrop = rememberLayerBackdrop(), + exportedBackdrop = backdrop, + shape = { RoundedCornerShape(0.dp) }, + highlight = { + Highlight.Ambient.copy(alpha = 0f) + }, + effects = {}) + .hazeSource(hazeState) + .padding(horizontal = 8.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center + ) { + val tapCount = remember { mutableIntStateOf(0) } + val lastTapTime = remember { mutableLongStateOf(0L) } + Column( + modifier = Modifier + .fillMaxWidth() + .pointerInput(Unit) { + detectTapGestures( + onTap = { + val now = System.currentTimeMillis() + + if (now - lastTapTime.longValue > 400) { + tapCount.intValue = 0 + } + + tapCount.intValue++ + lastTapTime.longValue = now + + if (tapCount.intValue >= 5) { + tapCount.intValue = 0 + viewModel.activateDemoMode() + } + }) + }) { + Text( + text = stringResource(R.string.airpods_not_connected), style = TextStyle( + fontSize = 24.sp, + fontWeight = FontWeight.Medium, + color = if (isSystemInDarkTheme()) Color.White else Color.Black, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), textAlign = TextAlign.Center, modifier = Modifier.fillMaxWidth() + ) + Spacer(Modifier.height(24.dp)) + Text( + text = stringResource(R.string.airpods_not_connected_description), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Light, + color = if (isSystemInDarkTheme()) Color.White else Color.Black, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), + textAlign = TextAlign.Center, + modifier = Modifier.fillMaxWidth(), + ) + } + + Spacer(Modifier.height(32.dp)) + if (!BuildConfig.PLAY_BUILD) { + StyledButton( + onClick = { navController.navigate("troubleshooting") }, + backdrop = backdrop, + modifier = Modifier + .fillMaxWidth(0.9f) + ) { + Text( + text = stringResource(R.string.troubleshooting), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = if (isSystemInDarkTheme()) Color.White else Color.Black + ) + ) + } + Spacer(Modifier.height(16.dp)) + } + if (state.connectionSuccessful) { + StyledButton( + onClick = { + viewModel.reconnectFromSavedMac() + }, backdrop = backdrop, modifier = Modifier.fillMaxWidth(0.9f) + ) { + Text( + text = stringResource(R.string.reconnect_to_last_device), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = if (isSystemInDarkTheme()) Color.White else Color.Black + ) + ) + } + } + } + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AppSettingsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AppSettingsScreen.kt new file mode 100644 index 000000000..af1973d11 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/AppSettingsScreen.kt @@ -0,0 +1,777 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens + +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.os.Build +import android.widget.Toast +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.foundation.text.input.TextFieldState +import androidx.compose.foundation.text.input.clearText +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.OutlinedTextFieldDefaults +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.input.KeyboardCapitalization +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.lerp +import androidx.compose.ui.unit.sp +import androidx.core.net.toUri +import androidx.lifecycle.viewmodel.compose.viewModel +import androidx.navigation.NavController +import com.kyant.backdrop.backdrops.layerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import dev.chrisbanes.haze.hazeSource +import me.kavishdevar.librepods.BuildConfig +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.presentation.components.AppInfoCard +import me.kavishdevar.librepods.presentation.components.DeviceInfoCard +import me.kavishdevar.librepods.presentation.components.NavigationButton +import me.kavishdevar.librepods.presentation.components.StyledBottomSheet +import me.kavishdevar.librepods.presentation.components.StyledButton +import me.kavishdevar.librepods.presentation.components.StyledIconButton +import me.kavishdevar.librepods.presentation.components.StyledInputField +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledSlider +import me.kavishdevar.librepods.presentation.components.StyledToggle +import me.kavishdevar.librepods.presentation.viewmodel.AppSettingsViewModel +import me.kavishdevar.librepods.utils.XposedState +import java.util.concurrent.TimeUnit + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun AppSettingsScreen( + navController: NavController, viewModel: AppSettingsViewModel = viewModel() +) { + val context = LocalContext.current + val scrollState = rememberScrollState() + val state by viewModel.uiState.collectAsState() + + val backdrop = rememberLayerBackdrop() + + val contactBottomSheet = remember { mutableStateOf(false) } + val subjectState = remember { TextFieldState() } + val descriptionState = remember { TextFieldState() } + val subjectFocusRequester = remember { FocusRequester() } + val descriptionFocusRequester = remember { FocusRequester() } + + StyledScaffold( + title = stringResource(R.string.settings) + ) { topPadding, hazeState, bottomPadding -> + Column( + modifier = Modifier + .fillMaxSize() + .layerBackdrop(backdrop) + .hazeSource(state = hazeState) + .verticalScroll(scrollState) + .padding(horizontal = 16.dp) + ) { + Spacer(modifier = Modifier.height(topPadding)) + + val isDarkTheme = isSystemInDarkTheme() + val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + val textColor = if (isDarkTheme) Color.White else Color.Black + + if (!state.isPremium && state.connectionSuccessful) { + StyledButton( + onClick = { + navController.navigate("purchase_screen") + }, + backdrop = rememberLayerBackdrop(), + modifier = Modifier.fillMaxWidth(), + maxScale = 0.05f, + surfaceColor = if (isSystemInDarkTheme()) Color(0xFF916100) else Color(0xFFE59900) + ) { + Text( + stringResource(R.string.unlock_advanced_features), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = Color.White + ), + ) + } + } + if (state.timeUntilFOSSPremiumExpiry > 0L) { + Box( + modifier = Modifier + .background(Color(0xFF32829B), RoundedCornerShape(28.dp)) + .clip(RoundedCornerShape(28.dp)) + .clickable { + val emailIntent = Intent(Intent.ACTION_SENDTO).apply { + data = "mailto:".toUri() + putExtra(Intent.EXTRA_EMAIL, arrayOf("billing@kavish.xyz")) + putExtra(Intent.EXTRA_SUBJECT, "LibrePods Play billing error") + putExtra( + Intent.EXTRA_TEXT, + "Please enter your GitHub username to restore your premium access:\n\nGitHub username: " + ) + } + context.startActivity(emailIntent) + } + ) { + Text( + text = stringResource( + R.string.play_foss_premium_banner, maxOf(1, TimeUnit.MILLISECONDS.toDays(state.timeUntilFOSSPremiumExpiry).toInt()) + ), + modifier = Modifier + .padding(16.dp), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Bold, + color = Color.White, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + } + if (state.connectionSuccessful) { + StyledToggle( + title = stringResource(R.string.widget), + label = stringResource(R.string.show_phone_battery_in_widget), + description = stringResource(R.string.show_phone_battery_in_widget_description), + checked = state.showPhoneBatteryInWidget, + onCheckedChange = viewModel::setShowPhoneBatteryInWidget, + enabled = state.isPremium + ) + + Text( + text = stringResource(R.string.popup_animations), style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), modifier = Modifier.padding(16.dp, bottom = 2.dp, top = 24.dp) + ) + + Spacer(modifier = Modifier.height(2.dp)) + + Column( + modifier = Modifier + .fillMaxWidth() + .background( + backgroundColor, RoundedCornerShape(28.dp) + ) + .padding(vertical = 4.dp) + ) { + StyledToggle( + label = stringResource(R.string.show_bottom_sheet_popup), + description = stringResource(R.string.show_bottom_sheet_popup_description), + checked = state.showBottomSheetPopup, + onCheckedChange = viewModel::setShowBottomSheetPopup, + independent = false + ) + + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + + StyledToggle( + label = stringResource(R.string.show_island_popup), + description = stringResource(R.string.show_island_popup_description), + checked = state.showIslandPopup, + onCheckedChange = viewModel::setShowIslandPopup, + independent = false + ) + } + + Text( + text = stringResource(R.string.conversational_awareness), style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), modifier = Modifier.padding(16.dp, bottom = 2.dp, top = 24.dp) + ) + + Spacer(modifier = Modifier.height(2.dp)) + + Column( + modifier = Modifier + .fillMaxWidth() + .background( + backgroundColor, RoundedCornerShape(28.dp) + ) + .padding(vertical = 4.dp) + ) { + StyledToggle( + label = stringResource(R.string.conversational_awareness_pause_music), + description = stringResource(R.string.conversational_awareness_pause_music_description), + checked = state.conversationalAwarenessPauseMusicEnabled, + onCheckedChange = viewModel::setConversationalAwarenessPauseMusicEnabled, + independent = false, + enabled = state.isPremium + ) + + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + + StyledToggle( + label = stringResource(R.string.relative_conversational_awareness_volume), + description = stringResource(R.string.relative_conversational_awareness_volume_description), + checked = state.relativeConversationalAwarenessVolumeEnabled, + onCheckedChange = viewModel::setRelativeConversationalAwarenessVolumeEnabled, + independent = false, + enabled = state.isPremium, + ) + } + + Spacer(modifier = Modifier.height(16.dp)) + + val conversationalAwarenessVolume = state.conversationalAwarenessVolume + LaunchedEffect(conversationalAwarenessVolume) { + viewModel.setConversationalAwarenessVolume(conversationalAwarenessVolume) + } + + StyledSlider( + label = stringResource(R.string.conversational_awareness_volume), + value = conversationalAwarenessVolume, + valueRange = 10f..85f, + snapPoints = listOf(44f), + startLabel = "10%", + endLabel = "85%", + onValueChange = { newValue -> + viewModel.setConversationalAwarenessVolume( + newValue + ) + }, + independent = true, + enabled = state.isPremium + ) + +// if (!BuildConfig.PLAY_BUILD) { +// Spacer(modifier = Modifier.height(16.dp)) +// +// NavigationButton( +// to = "", +// title = stringResource(R.string.camera_control), +// name = stringResource(R.string.set_custom_camera_package), +// navController = navController, +// onClick = { +// if (state.isPremium) viewModel.setShowCameraDialog(true) +// }, +// independent = true, +// description = stringResource(R.string.camera_control_app_description) +// ) +// } + + Spacer(modifier = Modifier.height(16.dp)) + if (context.checkSelfPermission("android.permission.BLUETOOTH_PRIVILEGED") == PackageManager.PERMISSION_GRANTED) { + StyledToggle( + title = stringResource(R.string.ear_detection), + label = stringResource(R.string.disconnect_when_not_wearing), + description = stringResource(R.string.disconnect_when_not_wearing_description), + checked = state.disconnectWhenNotWearing, + onCheckedChange = viewModel::setDisconnectWhenNotWearing, + enabled = state.isPremium + ) + } + + Text( + text = stringResource(R.string.takeover_airpods_state), style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), modifier = Modifier.padding(16.dp, bottom = 2.dp, top = 24.dp) + ) + + Spacer(modifier = Modifier.height(4.dp)) + + Column( + modifier = Modifier + .fillMaxWidth() + .background( + backgroundColor, RoundedCornerShape(28.dp) + ) + .padding(vertical = 4.dp) + ) { + StyledToggle( + label = stringResource(R.string.takeover_disconnected), + description = stringResource(R.string.takeover_disconnected_desc), + checked = state.takeoverWhenDisconnected, + onCheckedChange = viewModel::setTakeoverWhenDisconnected, + independent = false, + enabled = state.isPremium + ) + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + + StyledToggle( + label = stringResource(R.string.takeover_idle), + description = stringResource(R.string.takeover_idle_desc), + checked = state.takeoverWhenIdle, + onCheckedChange = viewModel::setTakeoverWhenIdle, + independent = false, + enabled = state.isPremium + ) + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + + StyledToggle( + label = stringResource(R.string.takeover_music), + description = stringResource(R.string.takeover_music_desc), + checked = state.takeoverWhenMusic, + onCheckedChange = viewModel::setTakeoverWhenMusic, + independent = false, + enabled = state.isPremium + ) + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + + StyledToggle( + label = stringResource(R.string.takeover_call), + description = stringResource(R.string.takeover_call_desc), + checked = state.takeoverWhenCall, + onCheckedChange = viewModel::setTakeoverWhenCall, + independent = false, + enabled = state.isPremium + ) + } + + Spacer(modifier = Modifier.height(16.dp)) + + Text( + text = stringResource(R.string.takeover_phone_state), style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), modifier = Modifier.padding(horizontal = 16.dp) + ) + Spacer(modifier = Modifier.height(4.dp)) + Column( + modifier = Modifier + .fillMaxWidth() + .background( + backgroundColor, RoundedCornerShape(28.dp) + ) + .padding(vertical = 4.dp) + ) { + StyledToggle( + label = stringResource(R.string.takeover_ringing_call), + description = stringResource(R.string.takeover_ringing_call_desc), + checked = state.takeoverWhenRingingCall, + onCheckedChange = viewModel::setTakeoverWhenRingingCall, + independent = false, + enabled = state.isPremium + ) + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + + StyledToggle( + label = stringResource(R.string.takeover_media_start), + description = stringResource(R.string.takeover_media_start_desc), + checked = state.takeoverWhenMediaStart, + onCheckedChange = viewModel::setTakeoverWhenMediaStart, + independent = false, + enabled = state.isPremium + ) + } + + Text( + text = stringResource(R.string.advanced_options), style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), modifier = Modifier.padding(16.dp, bottom = 2.dp, top = 24.dp) + ) + + Spacer(modifier = Modifier.height(2.dp)) + + StyledToggle( + label = stringResource(R.string.use_alternate_head_tracking_packets), + description = stringResource(R.string.use_alternate_head_tracking_packets_description), + checked = state.useAlternateHeadTrackingPackets, + onCheckedChange = viewModel::setUseAlternateHeadTrackingPackets, + independent = true, + enabled = state.isPremium + ) + Spacer(modifier = Modifier.height(16.dp)) + } else { + Box( + modifier = Modifier + .background(if (isDarkTheme) Color.Black else Color(0xFFF2F2F7)) + .padding(horizontal = 16.dp) + .padding(top = 16.dp, bottom = 2.dp) + ) { + Text( + text = stringResource(R.string.customizations_unavailable), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor.copy(alpha = 0.6f), + ), + modifier = Modifier + ) + } + } + + if (XposedState.isAvailable && XposedState.bluetoothScopeEnabled) { + val restartBluetoothText = + stringResource(R.string.found_offset_restart_bluetooth) + StyledToggle( + label = stringResource(R.string.act_as_an_apple_device) + " (${ + stringResource( + R.string.requires_xposed + ) + })", + description = stringResource(R.string.act_as_an_apple_device_description), + checked = state.vendorIdHook, + onCheckedChange = { enabled -> + Toast.makeText(context, restartBluetoothText, Toast.LENGTH_SHORT).show() + viewModel.setVendorIdHook(enabled) + }, + independent = true, + enabled = state.isPremium + ) + } + + if (!BuildConfig.PLAY_BUILD) { + Spacer(modifier = Modifier.height(16.dp)) + NavigationButton( + to = "troubleshooting", + name = stringResource(R.string.troubleshooting), + navController = navController, + independent = true, + description = stringResource(R.string.troubleshooting_description) + ) + } + + Spacer(modifier = Modifier.height(8.dp)) + + Box( + modifier = Modifier + .background(if (isDarkTheme) Color.Black else Color(0xFFF2F2F7)) + .padding(start = 16.dp, bottom = 2.dp, top = 24.dp, end = 4.dp) + ) { + Text( + text = stringResource(R.string.contact), style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + + Spacer(modifier = Modifier.height(4.dp)) + Column( + modifier = Modifier + .fillMaxWidth() + .background( + backgroundColor, RoundedCornerShape(28.dp) + ) + .clip(RoundedCornerShape(28.dp)) + ) { + NavigationButton( + to = "", + name = stringResource(R.string.email), + navController = navController, + onClick = { contactBottomSheet.value = true }, + independent = false + ) + + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + + NavigationButton( + to = "", + name = stringResource(R.string.discord), + navController = navController, + onClick = { + val intent = + Intent(Intent.ACTION_VIEW, "https://discord.gg/Ts4wupXcmc".toUri()) + context.startActivity(intent) + }, + independent = false + ) + + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier.padding(horizontal = 12.dp) + ) + + NavigationButton( + to = "", + name = stringResource(R.string.github_issues), + navController = navController, + onClick = { + val appVersion = Uri.encode("v${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})") + val device = Uri.encode("${Build.MANUFACTURER} ${Build.MODEL}") + val androidVersion = Uri.encode("${Build.ID} (${Build.DISPLAY})") + val appSource = Uri.encode( + when { + BuildConfig.PLAY_BUILD -> "Play" + else -> "GitHub" + } + ) + val url = "https://github.com/kavishdevar/librepods/issues/new" + + "?template=01-bug-report-android.yml" + + "&app-source=$appSource" + + "&app-version=$appVersion" + + "&device=$device" + + "&android-version=$androidVersion" + + val intent = Intent(Intent.ACTION_VIEW, url.toUri()) + context.startActivity(intent) + }, + independent = false + ) + } + + Spacer(modifier = Modifier.height(20.dp)) + DeviceInfoCard() + Spacer(modifier = Modifier.height(16.dp)) + AppInfoCard() + + Spacer(modifier = Modifier.height(16.dp)) + + NavigationButton( + to = "open_source_licenses", + name = stringResource(R.string.open_source_licenses), + navController = navController, + independent = true + ) + + Spacer(modifier = Modifier.height(bottomPadding)) + + if (state.showCameraDialog) { + AlertDialog(onDismissRequest = { viewModel.setShowCameraDialog(false) }, title = { + Text( + stringResource(R.string.set_custom_camera_package), + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Medium + ) + }, text = { + Column { + Text( + stringResource(R.string.enter_custom_camera_package), + fontFamily = FontFamily(Font(R.font.sf_pro)), + modifier = Modifier.padding(bottom = 8.dp) + ) + + OutlinedTextField( + value = state.cameraPackageValue, + onValueChange = { + viewModel.setCameraPackageValue(it) + viewModel.setCameraPackageError(null) + }, + modifier = Modifier.fillMaxWidth(), + isError = state.cameraPackageError != null, + keyboardOptions = KeyboardOptions( + keyboardType = KeyboardType.Ascii, + capitalization = KeyboardCapitalization.None + ), + colors = OutlinedTextFieldDefaults.colors( + focusedBorderColor = if (isDarkTheme) Color(0xFF007AFF) else Color( + 0xFF3C6DF5 + ), + unfocusedBorderColor = if (isDarkTheme) Color.Gray else Color.LightGray + ), + supportingText = { + if (state.cameraPackageError != null) { + Text( + state.cameraPackageError ?: "", + color = MaterialTheme.colorScheme.error + ) + } + }, + label = { Text(stringResource(R.string.custom_camera_package)) }) + } + }, confirmButton = { + val successText = stringResource(R.string.custom_camera_package_set_success) + TextButton( + onClick = { + viewModel.saveCameraPackage() + Toast.makeText(context, successText, Toast.LENGTH_SHORT).show() + }) { + Text( + "Save", + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Medium + ) + } + }, dismissButton = { + TextButton( + onClick = { viewModel.setShowCameraDialog(false) }) { + Text( + "Cancel", + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Medium + ) + } + }) + } + } + StyledBottomSheet( + visible = contactBottomSheet.value, + onDismiss = { contactBottomSheet.value = false }, + backdrop = backdrop + ) { innerBackdrop, progress -> + val animatedPadding = lerp(16.dp, 2.dp, progress) + + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = animatedPadding) + .padding(bottom = 16.dp), + ) { + Row( + modifier = Modifier + .fillMaxWidth() + .padding(bottom = 16.dp), + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically + ) { + StyledIconButton( + icon = "\uDBC0\uDD84", + backdrop = innerBackdrop, + onClick = { contactBottomSheet.value = false } + ) + Text ( + text = stringResource(R.string.describe_your_issue), + style = TextStyle( + fontSize = 18.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Bold, + textAlign = TextAlign.Center, + color = if (isSystemInDarkTheme()) Color.White else Color.Black + ) + ) + StyledIconButton( + icon = "\uDBC0\uDE1F", + backdrop = innerBackdrop, + surfaceColor = if (isSystemInDarkTheme()) Color(0xFF0091FF) else Color(0xFF0088FF), + iconTint = if (subjectState.text.isNotEmpty() && descriptionState.text.isNotEmpty()) Color.White else Color.Gray, + enabled = subjectState.text.isNotEmpty() && descriptionState.text.isNotEmpty(), + onClick = { + contactBottomSheet.value = false + val intent = Intent(Intent.ACTION_SENDTO).apply { + data = "mailto:".toUri() + putExtra(Intent.EXTRA_EMAIL, arrayOf("contact@kavish.xyz")) + putExtra(Intent.EXTRA_SUBJECT, "LibrePods: ${subjectState.text}") + putExtra( + Intent.EXTRA_TEXT, + "${descriptionState.text}" + + "\n\n----------" + + "\nPhone details:" + + "\nMANUFACTURER: ${Build.MANUFACTURER}" + + "\nMODEL: ${Build.MODEL} (${Build.PRODUCT})" + + "\nDISPLAY_VERSION: ${Build.DISPLAY}" + + "\nID: ${Build.ID} (SDK ${Build.VERSION.SDK_INT_FULL})" + + "\nXposed enabled/active: ${XposedState.isAvailable}/${XposedState.bluetoothScopeEnabled}" + + "\n\nApp details:" + + "\nVERSION: ${BuildConfig.VERSION_NAME}" + + "\nVERSION_CODE: ${BuildConfig.VERSION_CODE}" + + "\nFLAVOR: ${BuildConfig.FLAVOR}" + + "\nBUILD_TYPE: ${BuildConfig.BUILD_TYPE}" + ) + } + context.startActivity(intent) + subjectState.clearText() + descriptionState.clearText() + } + ) + } + + Spacer(modifier = Modifier.height(8.dp)) + + StyledInputField( + inputState = subjectState, + focusRequester = subjectFocusRequester, + placeholder = stringResource(R.string.subject), + ) + + Spacer(modifier = Modifier.height(12.dp)) + + StyledInputField( + inputState = descriptionState, + focusRequester = descriptionFocusRequester, + placeholder = stringResource(R.string.describe_your_issue), + singleLine = false + ) + } + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/CameraControlScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/CameraControlScreen.kt new file mode 100644 index 000000000..433e7b324 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/CameraControlScreen.kt @@ -0,0 +1,111 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens + +import android.accessibilityservice.AccessibilityServiceInfo +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.provider.Settings +import android.view.accessibility.AccessibilityManager +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import com.kyant.backdrop.backdrops.layerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.presentation.components.SelectItem +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledSelectList +import me.kavishdevar.librepods.services.AppListenerService +import me.kavishdevar.librepods.bluetooth.AACPManager.Companion.StemPressType +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel + +@Composable +fun CameraControlScreen(viewModel: AirPodsViewModel) { + val context = LocalContext.current + val currentCameraAction by viewModel.cameraAction.collectAsState() + + fun isAppListenerServiceEnabled(context: Context): Boolean { + val am = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager + val enabledServices = + am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK) + val serviceComponent = ComponentName(context, AppListenerService::class.java) + return enabledServices.any { + it.resolveInfo.serviceInfo.packageName == serviceComponent.packageName && + it.resolveInfo.serviceInfo.name == serviceComponent.className + } + } + + fun handleSelection(action: StemPressType?) { + if (action != null && !isAppListenerServiceEnabled(context)) { + context.startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)) + } else { + viewModel.setCameraAction(action) + } + } + + val cameraOptions = remember(currentCameraAction) { + listOf( + SelectItem( + name = "Off", + selected = currentCameraAction == null, + onClick = { handleSelection(null) } + ), + SelectItem( + name = "Press once", + selected = currentCameraAction == StemPressType.SINGLE_PRESS, + onClick = { handleSelection(StemPressType.SINGLE_PRESS) } + ), + SelectItem( + name = "Press and hold AirPods", + selected = currentCameraAction == StemPressType.LONG_PRESS, + onClick = { handleSelection(StemPressType.LONG_PRESS) } + ) + ) + } + + val backdrop = rememberLayerBackdrop() + + StyledScaffold( + title = stringResource(R.string.camera_control) + ) { spacerHeight -> + Column( + modifier = Modifier + .fillMaxSize() + .layerBackdrop(backdrop) + .padding(horizontal = 16.dp), + verticalArrangement = Arrangement.spacedBy(16.dp) + ) { + Spacer(modifier = Modifier.height(spacerHeight)) + StyledSelectList(items = cameraOptions) + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/DebugScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/DebugScreen.kt similarity index 95% rename from android/app/src/main/java/me/kavishdevar/librepods/screens/DebugScreen.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/DebugScreen.kt index 27db1f8ea..75c4bc2d6 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/DebugScreen.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/DebugScreen.kt @@ -1,24 +1,24 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalHazeMaterialsApi::class, ExperimentalEncodingApi::class) -package me.kavishdevar.librepods.screens +package me.kavishdevar.librepods.presentation.screens import android.annotation.SuppressLint import android.content.ClipData @@ -82,10 +82,10 @@ import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi import kotlinx.coroutines.delay import kotlinx.coroutines.launch import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.constants.BatteryStatus -import me.kavishdevar.librepods.constants.isHeadTrackingData +import me.kavishdevar.librepods.presentation.components.StyledIconButton +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.data.BatteryStatus +import me.kavishdevar.librepods.data.isHeadTrackingData import me.kavishdevar.librepods.services.ServiceManager import kotlin.io.encoding.ExperimentalEncodingApi @@ -335,12 +335,11 @@ fun DebugScreen(navController: NavController) { expandedItems.value = emptySet() }, icon = "􀈑", - darkMode = isDarkTheme, backdrop = scaffoldBackdrop ) } ), - ) { spacerHeight, hazeState -> + ) { topPadding, hazeState, bottomPadding -> Column( modifier = Modifier .fillMaxSize() @@ -349,7 +348,7 @@ fun DebugScreen(navController: NavController) { .layerBackdrop(backdrop) .padding(horizontal = 16.dp) ) { - Spacer(modifier = Modifier.height(spacerHeight)) + Spacer(modifier = Modifier.height(topPadding)) LazyColumn( state = listState, modifier = Modifier diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/EqualizerScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/EqualizerScreen.kt new file mode 100644 index 000000000..d6ecd76f2 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/EqualizerScreen.kt @@ -0,0 +1,658 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens + +import androidx.compose.animation.Crossfade +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.gestures.Orientation +import androidx.compose.foundation.gestures.draggable +import androidx.compose.foundation.gestures.rememberDraggableState +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.visible +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.derivedStateOf +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableFloatStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.snapshotFlow +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.graphics.Brush +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Path +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.graphics.lerp +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.IntOffset +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.compose.ui.util.lerp +import com.kyant.backdrop.backdrops.layerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import com.kyant.backdrop.drawBackdrop +import com.kyant.backdrop.effects.lens +import com.kyant.backdrop.highlight.Highlight +import kotlinx.coroutines.FlowPreview +import kotlinx.coroutines.flow.debounce +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.presentation.components.SelectItem +import me.kavishdevar.librepods.presentation.components.StyledButton +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledSelectList +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel +import kotlin.math.abs +import kotlin.math.roundToInt +import kotlin.time.Duration.Companion.milliseconds + +@OptIn(FlowPreview::class) +@Composable +fun EqualizerScreen(viewModel: AirPodsViewModel) { + val state by viewModel.uiState.collectAsState() + + val customEq = state.customEq + val enabled = customEq.isEnabled() + + val recommendedString = stringResource(R.string.recommended) + val customString = stringResource(R.string.custom) + + val eqStateOptions = remember(state.customEq) { + listOf( + SelectItem( + name = recommendedString, + selected = !enabled, + onClick = { viewModel.setCustomEqEnabled(false) } + ), + SelectItem( + name = customString, + selected = enabled, + onClick = { viewModel.setCustomEqEnabled(true) } + ), + ) + } + + StyledScaffold( + title = stringResource(R.string.equalizer) + ) { spacerHeight -> + val scrollState = rememberScrollState() + Column( + modifier = Modifier + .fillMaxSize() + .padding(horizontal = 16.dp) + .verticalScroll(scrollState), + verticalArrangement = Arrangement.spacedBy(16.dp) + ) { + + val height = 200.dp + val maxOffset = with(LocalDensity.current) { height.toPx() } / 2 + + val offsets = remember(state.customEq) { + listOf( + mutableFloatStateOf(lerp(maxOffset, -maxOffset, customEq.low.toFloat() / 100)), + mutableFloatStateOf(lerp(maxOffset, -maxOffset, customEq.mid.toFloat() / 100)), + mutableFloatStateOf(lerp(maxOffset, -maxOffset, customEq.high.toFloat() / 100)) + ) + } + + Spacer(modifier = Modifier.height(spacerHeight)) + StyledSelectList(items = eqStateOptions) + Spacer(modifier = Modifier.height(12.dp)) + val backgroundColor = if (isSystemInDarkTheme()) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + + Crossfade ( + customEq.isEnabled() + ) { visible -> + Column( + modifier = Modifier + .fillMaxWidth() + .visible(visible), + verticalArrangement = Arrangement.spacedBy(16.dp) + ) { + Column( + modifier = Modifier + .fillMaxWidth() + .background(backgroundColor, RoundedCornerShape(28.dp)) + ) { + val dashColor = + if (isSystemInDarkTheme()) Color(0x80AAAAAA) else Color(0x809D9D9D) + // LaunchedEffect(offsets[0].floatValue, offsets[1].floatValue, offsets[2].floatValue) { + // val low = ((offsets[0].floatValue / (2 * maxOffset) + 0.5f) * 100).roundToInt() + // val mid = ((offsets[1].floatValue / (2 * maxOffset) + 0.5f) * 100).roundToInt() + // val high = ((offsets[2].floatValue / (2 * maxOffset) + 0.5f) * 100).roundToInt() + // Log.d("EqualizerScreen", "$low, $mid, $high") + // viewModel.setCustomEq( + // low = low, + // mid = mid, + // high = high + // ) + // } + + LaunchedEffect(offsets) { + snapshotFlow { + Triple( + offsets[0].floatValue, + offsets[1].floatValue, + offsets[2].floatValue + ) + } + .debounce(100.milliseconds) // cool, should've been using this since the very beginning + .collect { (lowF, midF, highF) -> + val low = + 100 - ((lowF / (2 * maxOffset) + 0.5f) * 100).roundToInt() + val mid = + 100 - ((midF / (2 * maxOffset) + 0.5f) * 100).roundToInt() + val high = + 100 - ((highF / (2 * maxOffset) + 0.5f) * 100).roundToInt() + + viewModel.setCustomEq(low, mid, high) + } + } + + val backdrop = rememberLayerBackdrop() + Column( + modifier = Modifier + .fillMaxWidth() + .background(backgroundColor, RoundedCornerShape(28.dp)) + ) { + Spacer(modifier = Modifier.height(42.dp)) + // Row( + // modifier = Modifier + // .fillMaxWidth() + // .padding(18.dp), + // verticalAlignment = Alignment.CenterVertically, + // horizontalArrangement = Arrangement.spacedBy(12.dp) + // ) { + // Box( + // modifier = Modifier + // .size(64.dp) + // .background(if (isSystemInDarkTheme()) Color.DarkGray else Color.LightGray, RoundedCornerShape(12.dp)) + // ) + // Column( + // modifier = Modifier + // .weight(1f), + // verticalArrangement = Arrangement.Center + // ) { + // Text( + // text = "Written into Changes", + // style = TextStyle( + // fontSize = 16.sp, + // fontFamily = FontFamily(Font(R.font.sf_pro)), + // fontWeight = FontWeight.Bold, + // color = if (isSystemInDarkTheme()) Color.White else Color.Black + // ) + // ) + // Spacer(modifier = Modifier.height(4.dp)) + // Text( + // text = "Avalon Emerson", + // style = TextStyle( + // fontSize = 14.sp, + // fontFamily = FontFamily(Font(R.font.sf_pro)), + // fontWeight = FontWeight.Normal, + // color = if (isSystemInDarkTheme()) Color.White else Color.Black + // ) + // ) + // } + // val paused = remember { mutableStateOf(false) } + // Box( + // modifier = Modifier + // .size(48.dp) + // .background(Color(0x600091FF), CircleShape) + // .clickable( + // interactionSource = remember { MutableInteractionSource() }, + // indication = null, + // ) { + // paused.value = !paused.value + // }, + // contentAlignment = Alignment.Center + // ) { + // Crossfade( + // targetState = paused.value, + // label = "media_icon" + // ) { p -> + // Text( + // text = if (p) "􀊄" else "􀊆", + // style = TextStyle( + // fontSize = 24.sp, + // fontFamily = FontFamily(Font(R.font.sf_pro)), + // fontWeight = FontWeight.Normal, + // color = Color(0xFF0091FF), + // textAlign = TextAlign.Center + // ) + // ) + // } + // } + // } + // + // HorizontalDivider( + // thickness = 1.dp, + // color = Color(0x40888888), + // modifier = Modifier + // .padding(horizontal = 20.dp) + // .padding(bottom = 16.dp) + // ) + + Box( + modifier = Modifier.fillMaxWidth() + ) { + fun colorFromY(y: Float): Color { + val f = ((y + maxOffset) / (2f * maxOffset)).coerceIn(0f, 1f) + val stops = listOf( + 0.0f to Color(0xFFFFA300), + 0.25f to Color(0xFFFCE600), + 0.5f to Color(0xFF00FAAF), + 0.75f to Color(0xFF00FAFF), + 1.0f to Color(0xFF00B5FF) + ) + val (start, end) = stops.zipWithNext() + .first { f <= it.second.first } + val c = (f - start.first) / (end.first - start.first) + return lerp(start.second, end.second, c) + } + + fun pathBrush( + startY: Float, + endY: Float, + ): Brush { + val stops = (0..20).map { i -> + val t = i / 20f + val y = lerp(startY, endY, t) + t to colorFromY(y) + } + + return Brush.linearGradient( + colorStops = stops.toTypedArray() + ) + } + + Column( + modifier = Modifier.fillMaxWidth().layerBackdrop(backdrop) + ) { + Box( + modifier = Modifier + .fillMaxWidth() + .height(height) + .padding(horizontal = 20.dp) + ) { + Row( + modifier = Modifier + .fillMaxSize() + ) { + val dashCount = (height / 10.dp).toInt() + repeat(3) { + Box( + modifier = Modifier + .fillMaxSize() + .weight(1f), + contentAlignment = Alignment.Center + ) { + Column( + modifier = Modifier + .fillMaxHeight(), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + for (i in 1..(dashCount)) { + val t = i.toFloat() / dashCount + val centerDistance = abs(0.5f - t) + val alpha = 1f - (centerDistance * 2f) + Box( + modifier = Modifier + .height(9.dp) + .width(0.75.dp) + .background( + dashColor.copy(alpha), + RoundedCornerShape(28.dp) + ) + ) + } + } + } + } + } + + Canvas( + modifier = Modifier + .fillMaxSize() + ) { + val canvasWidth = size.width + + drawLine( + color = backgroundColor, + start = Offset( + x = 0f, + y = offsets[0].floatValue + maxOffset + ), + end = Offset( + x = 1 / 6f * canvasWidth, + y = offsets[0].floatValue + maxOffset + ), + strokeWidth = 10f + ) + drawLine( + color = colorFromY(offsets[0].floatValue), + start = Offset( + x = 0f, + y = offsets[0].floatValue + maxOffset + ), + end = Offset( + x = 1 / 6f * canvasWidth, + y = offsets[0].floatValue + maxOffset + ), + strokeWidth = 8f + ) + + val lowToMidPath = Path() + lowToMidPath.moveTo( + x = 1 / 6f * canvasWidth, + y = offsets[0].floatValue + maxOffset + ) + lowToMidPath.cubicTo( + x1 = canvasWidth * 1 / 6f + 108.dp.value, + y1 = offsets[0].floatValue + maxOffset, + x2 = canvasWidth * 0.5f - 108.dp.value, + y2 = offsets[1].floatValue + maxOffset, + x3 = canvasWidth * 0.5f, + y3 = offsets[1].floatValue + maxOffset + ) + drawPath( + color = backgroundColor, + path = lowToMidPath, + style = Stroke(width = 10f) + ) + drawPath( + brush = pathBrush( + offsets[0].floatValue, + offsets[1].floatValue + ), + path = lowToMidPath, + style = Stroke(width = 8f) + ) + + val midToHighPath = Path() + midToHighPath.moveTo( + x = 0.5f * canvasWidth, + y = offsets[1].floatValue + maxOffset + ) + midToHighPath.cubicTo( + x1 = canvasWidth * 0.5f + 108.dp.value, + y1 = offsets[1].floatValue + maxOffset, + x2 = canvasWidth * 5 / 6f - 108.dp.value, + y2 = offsets[2].floatValue + maxOffset, + x3 = canvasWidth * 5 / 6f, + y3 = offsets[2].floatValue + maxOffset + ) + drawPath( + color = backgroundColor, + path = midToHighPath, + style = Stroke(width = 10f) + ) + drawPath( + brush = pathBrush( + offsets[1].floatValue, + offsets[2].floatValue + ), + path = midToHighPath, + style = Stroke(width = 8f) + ) + drawLine( + color = backgroundColor, + start = Offset( + x = 5 / 6f * canvasWidth, + y = offsets[2].floatValue + maxOffset + ), + end = Offset( + x = 1f * canvasWidth, + y = offsets[2].floatValue + maxOffset + ), + strokeWidth = 10f + ) + drawLine( + color = colorFromY(offsets[2].floatValue), + start = Offset( + x = 5 / 6f * canvasWidth, + y = offsets[2].floatValue + maxOffset + ), + end = Offset( + x = 1f * canvasWidth, + y = offsets[2].floatValue + maxOffset + ), + strokeWidth = 8f + ) + } + } + Row( + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 16.dp, horizontal = 20.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + Box( + modifier = Modifier.weight(1f) + ) { + Text( + text = "Low".uppercase(), + style = TextStyle( + fontSize = 14.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Bold, + color = (if (isSystemInDarkTheme()) Color.White else Color.Black).copy( + 0.2f + ), + textAlign = TextAlign.Center + ), + modifier = Modifier.fillMaxWidth() + ) + } + Box( + modifier = Modifier.weight(1f) + ) { + Text( + text = "Mid".uppercase(), + style = TextStyle( + fontSize = 14.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Bold, + color = (if (isSystemInDarkTheme()) Color.White else Color.Black).copy( + 0.2f + ), + textAlign = TextAlign.Center + ), + modifier = Modifier.fillMaxWidth() + ) + } + Box( + modifier = Modifier.weight(1f) + ) { + Text( + text = "High".uppercase(), + style = TextStyle( + fontSize = 14.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Bold, + color = (if (isSystemInDarkTheme()) Color.White else Color.Black).copy( + 0.2f + ), + textAlign = TextAlign.Center + ), + modifier = Modifier.fillMaxWidth() + ) + } + } + Spacer(modifier = Modifier.height(24.dp)) + } + Row( + modifier = Modifier + .fillMaxWidth() + .height(height) + .padding(horizontal = 20.dp), + + verticalAlignment = Alignment.CenterVertically + ) { + for (i in 0..2) { + Row( + modifier = Modifier + .weight(1f), + horizontalArrangement = Arrangement.Center + ) { + val pressed = remember { mutableStateOf(false) } + Box( + modifier = Modifier + .offset { + IntOffset( + x = 0, + y = offsets[i].floatValue.roundToInt() + ) + }, + contentAlignment = Alignment.Center + ) { + Crossfade( + pressed.value + ) { + Box( + modifier = Modifier + .size(96.dp) + .then( + if (it) { + Modifier.drawBackdrop( + backdrop = backdrop, + shape = { CircleShape }, + highlight = { + Highlight.Ambient + }, + onDrawSurface = { + drawCircle( + color = Color.White.copy( + 0.2f + ), + radius = size.height + ) + drawCircle( + color = colorFromY( + offsets[i].floatValue + ), + style = Stroke(2.dp.value), + radius = size.height / 2 + ) + }, + effects = { + lens( + refractionHeight = 32f.dp.value, + refractionAmount = size.height + ) + } + ) + } else Modifier + ) + ) + } + Box( + modifier = Modifier + .size(18.dp) + .background( + colorFromY(offsets[i].floatValue), + CircleShape + ) + .border( + 2.5.dp, + backgroundColor, + CircleShape + ) + .draggable( + orientation = Orientation.Vertical, + state = rememberDraggableState { delta -> + offsets[i].floatValue = + (offsets[i].floatValue + delta).coerceIn( + -maxOffset, + maxOffset + ) + }, + onDragStarted = { + pressed.value = true + }, + onDragStopped = { + pressed.value = false + } + ) + ) + } + } + } + } + } + } + } + + val resetButtonEnabled = remember { derivedStateOf { !offsets.all { it.floatValue == 0f } } } + + StyledButton( + onClick = { + offsets[0].floatValue = 0f + offsets[1].floatValue = 0f + offsets[2].floatValue = 0f + }, + backdrop = rememberLayerBackdrop(), + modifier = Modifier.fillMaxWidth(), + isInteractive = false, + surfaceColor = backgroundColor, + enabled = resetButtonEnabled.value + ) { + Text( + text = stringResource(R.string.reset), + style = TextStyle( + fontSize = 14.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontWeight = FontWeight.Normal, + color = if (!offsets.all { it.floatValue == 0f }) Color(0xFF0093FF) else Color.Gray + ) + ) + } + } + } + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/HeadTrackingScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HeadTrackingScreen.kt similarity index 82% rename from android/app/src/main/java/me/kavishdevar/librepods/screens/HeadTrackingScreen.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HeadTrackingScreen.kt index 6dcf5214f..ce21253c4 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/HeadTrackingScreen.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HeadTrackingScreen.kt @@ -1,29 +1,33 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + + +// this is absolutely unnecessary, why did I make this. a simple toggle would've sufficed @file:OptIn(ExperimentalEncodingApi::class) -package me.kavishdevar.librepods.screens +package me.kavishdevar.librepods.presentation.screens -import android.content.Context -import android.os.Build +import android.graphics.Paint +import android.graphics.RadialGradient +import android.graphics.Shader +import android.graphics.Typeface import android.util.Log -import androidx.annotation.RequiresApi import androidx.compose.animation.AnimatedContent import androidx.compose.animation.ExperimentalAnimationApi import androidx.compose.animation.core.tween @@ -42,6 +46,7 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults @@ -71,7 +76,6 @@ import androidx.compose.ui.graphics.asAndroidPath import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.graphics.nativeCanvas import androidx.compose.ui.graphics.toArgb -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.drawText @@ -80,7 +84,6 @@ import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.rememberTextMeasurer import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.navigation.NavController @@ -92,10 +95,11 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledButton -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledToggle +import me.kavishdevar.librepods.presentation.components.StyledButton +import me.kavishdevar.librepods.presentation.components.StyledIconButton +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledToggle +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel import me.kavishdevar.librepods.services.ServiceManager import me.kavishdevar.librepods.utils.HeadTracking import kotlin.io.encoding.ExperimentalEncodingApi @@ -105,14 +109,14 @@ import kotlin.math.sin import kotlin.random.Random @ExperimentalHazeMaterialsApi -@RequiresApi(Build.VERSION_CODES.Q) @OptIn(ExperimentalMaterial3Api::class, ExperimentalAnimationApi::class) @Composable -fun HeadTrackingScreen(navController: NavController) { +fun HeadTrackingScreen(viewModel: AirPodsViewModel, navController: NavController) { + val state by viewModel.uiState.collectAsState() DisposableEffect(Unit) { - ServiceManager.getService()?.startHeadTracking() + viewModel.startHeadTracking() onDispose { - ServiceManager.getService()?.stopHeadTracking() + viewModel.stopHeadTracking() } } val isDarkTheme = isSystemInDarkTheme() @@ -125,34 +129,35 @@ fun HeadTrackingScreen(navController: NavController) { title = stringResource(R.string.head_tracking), actionButtons = listOf( { scaffoldBackdrop -> - var isActive by remember { mutableStateOf(ServiceManager.getService()?.isHeadTrackingActive == true) } StyledIconButton( onClick = { - if (ServiceManager.getService()?.isHeadTrackingActive == false) { - ServiceManager.getService()?.startHeadTracking() + if (!state.headTrackingActive) { + viewModel.startHeadTracking() Log.d("HeadTrackingScreen", "Head tracking started") } else { - ServiceManager.getService()?.stopHeadTracking() + viewModel.stopHeadTracking() Log.d("HeadTrackingScreen", "Head tracking stopped") } }, - icon = if (isActive) "􀊅" else "􀊃", - darkMode = isDarkTheme, + icon = if (state.headTrackingActive) "􀊅" else "􀊃", backdrop = scaffoldBackdrop ) } ), - ) { spacerHeight, hazeState -> - val sharedPreferences = LocalContext.current.getSharedPreferences("settings", Context.MODE_PRIVATE) + ) { topPadding, hazeState, _ -> var gestureText by remember { mutableStateOf("") } val coroutineScope = rememberCoroutineScope() var lastClickTime by remember { mutableLongStateOf(0L) } var shouldExplode by remember { mutableStateOf(false) } + + val scrollState = rememberScrollState() + Column( modifier = Modifier - .fillMaxWidth(), + .fillMaxWidth() + .verticalScroll(scrollState), horizontalAlignment = Alignment.CenterHorizontally ) { Column ( @@ -162,37 +167,50 @@ fun HeadTrackingScreen(navController: NavController) { .layerBackdrop(backdrop) .padding(top = 8.dp) .padding(horizontal = 16.dp) - .verticalScroll(scrollState) ) { - Spacer(modifier = Modifier.height(spacerHeight)) + Spacer(modifier = Modifier.height(topPadding)) + + if (!state.isPremium) { + StyledButton( + onClick = { + navController.navigate("purchase_screen") + }, + backdrop = rememberLayerBackdrop(), + modifier = Modifier.fillMaxWidth(), + maxScale = 0.05f, + surfaceColor = if (isSystemInDarkTheme()) Color(0xFF916100) else Color(0xFFE59900) + ) { + Text( + stringResource(R.string.unlock_advanced_features), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = Color.White + ), + ) + } + Spacer(modifier = Modifier.height(8.dp)) + } + StyledToggle( label = "Head Gestures", - sharedPreferences = sharedPreferences, - sharedPreferenceKey = "head_gestures", - ) - - Spacer(modifier = Modifier.height(2.dp)) - Text( - stringResource(R.string.head_gestures_details), - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Normal, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = textColor.copy(0.6f) - ), - modifier = Modifier.padding(start = 4.dp) + checked = state.headGesturesEnabled, + onCheckedChange = { viewModel.setHeadGesturesEnabled(it) }, + enabled = state.isPremium || state.headGesturesEnabled, + description = stringResource(R.string.head_gestures_details) ) Spacer(modifier = Modifier.height(16.dp)) Text( "Head Orientation", style = TextStyle( - fontSize = 18.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = textColor + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) ), - modifier = Modifier.padding(start = 4.dp, bottom = 8.dp, top = 8.dp) + modifier = Modifier.padding(start = 16.dp, bottom = 8.dp, top = 8.dp) ) HeadVisualization() @@ -200,12 +218,12 @@ fun HeadTrackingScreen(navController: NavController) { Text( "Velocity", style = TextStyle( - fontSize = 18.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = textColor + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) ), - modifier = Modifier.padding(start = 4.dp, bottom = 8.dp, top = 8.dp) + modifier = Modifier.padding(start = 16.dp, bottom = 8.dp, top = 8.dp) ) AccelerationPlot() @@ -381,7 +399,8 @@ private fun HeadVisualization() { .aspectRatio(2f), colors = CardDefaults.cardColors( containerColor = backgroundColor - ) + ), + shape = RoundedCornerShape(28.dp) ) { Box( modifier = Modifier.fillMaxSize(), @@ -456,9 +475,9 @@ private fun HeadVisualization() { spherePath.close() drawContext.canvas.nativeCanvas.apply { - val paint = android.graphics.Paint().apply { - style = android.graphics.Paint.Style.FILL - shader = android.graphics.RadialGradient( + val paint = Paint().apply { + style = Paint.Style.FILL + shader = RadialGradient( center.x + sinY * faceRadius * 0.3f, center.y - sinP * faceRadius * 0.3f, faceRadius * 1.4f, @@ -470,14 +489,14 @@ private fun HeadVisualization() { backgroundColor.copy(alpha = 0.7f).toArgb() ), floatArrayOf(0.3f, 0.5f, 0.7f, 0.8f, 1f), - android.graphics.Shader.TileMode.CLAMP + Shader.TileMode.CLAMP ) } drawPath(spherePath.asAndroidPath(), paint) - val highlightPaint = android.graphics.Paint().apply { - style = android.graphics.Paint.Style.FILL - shader = android.graphics.RadialGradient( + val highlightPaint = Paint().apply { + style = Paint.Style.FILL + shader = RadialGradient( center.x - faceRadius * 0.4f - sinY * faceRadius * 0.5f, center.y - faceRadius * 0.4f - sinP * faceRadius * 0.5f, faceRadius * 0.9f, @@ -487,15 +506,15 @@ private fun HeadVisualization() { android.graphics.Color.TRANSPARENT ), floatArrayOf(0f, 0.3f, 1f), - android.graphics.Shader.TileMode.CLAMP + Shader.TileMode.CLAMP ) alpha = if (darkTheme) 30 else 60 } drawPath(spherePath.asAndroidPath(), highlightPaint) - val secondaryHighlightPaint = android.graphics.Paint().apply { - style = android.graphics.Paint.Style.FILL - shader = android.graphics.RadialGradient( + val secondaryHighlightPaint = Paint().apply { + style = Paint.Style.FILL + shader = RadialGradient( center.x + faceRadius * 0.3f + sinY * faceRadius * 0.3f, center.y + faceRadius * 0.3f - sinP * faceRadius * 0.3f, faceRadius * 0.7f, @@ -504,15 +523,15 @@ private fun HeadVisualization() { android.graphics.Color.TRANSPARENT ), floatArrayOf(0f, 1f), - android.graphics.Shader.TileMode.CLAMP + Shader.TileMode.CLAMP ) alpha = if (darkTheme) 15 else 30 } drawPath(spherePath.asAndroidPath(), secondaryHighlightPaint) - val shadowPaint = android.graphics.Paint().apply { - style = android.graphics.Paint.Style.FILL - shader = android.graphics.RadialGradient( + val shadowPaint = Paint().apply { + style = Paint.Style.FILL + shader = RadialGradient( center.x + sinY * faceRadius * 0.5f, center.y - sinP * faceRadius * 0.5f, faceRadius * 1.1f, @@ -521,7 +540,7 @@ private fun HeadVisualization() { android.graphics.Color.BLACK ), floatArrayOf(0.7f, 1f), - android.graphics.Shader.TileMode.CLAMP + Shader.TileMode.CLAMP ) alpha = if (darkTheme) 40 else 20 } @@ -581,13 +600,13 @@ private fun HeadVisualization() { } drawContext.canvas.nativeCanvas.apply { - val paint = android.graphics.Paint().apply { + val paint = Paint().apply { color = if (darkTheme) android.graphics.Color.WHITE else android.graphics.Color.BLACK textSize = 12.sp.toPx() - textAlign = android.graphics.Paint.Align.RIGHT - typeface = android.graphics.Typeface.create( + textAlign = Paint.Align.RIGHT + typeface = Typeface.create( "SF Pro", - android.graphics.Typeface.NORMAL + Typeface.NORMAL ) } @@ -632,7 +651,8 @@ private fun AccelerationPlot() { .height(300.dp), colors = CardDefaults.cardColors( containerColor = if (darkTheme) Color(0xFF1C1C1E) else Color.White - ) + ), + shape = RoundedCornerShape(28.dp) ) { Box( modifier = Modifier @@ -700,10 +720,10 @@ private fun AccelerationPlot() { } drawContext.canvas.nativeCanvas.apply { - val paint = android.graphics.Paint().apply { + val paint = Paint().apply { color = if (darkTheme) android.graphics.Color.WHITE else android.graphics.Color.BLACK textSize = 12.sp.toPx() - textAlign = android.graphics.Paint.Align.RIGHT + textAlign = Paint.Align.RIGHT } drawText("${maxAbs.toInt()}", 30.dp.toPx(), 20.dp.toPx(), paint) @@ -716,20 +736,20 @@ private fun AccelerationPlot() { drawCircle(Color(0xFF007AFF), 5.dp.toPx(), Offset(width - 150.dp.toPx(), legendY)) drawContext.canvas.nativeCanvas.apply { - val paint = android.graphics.Paint().apply { + val paint = Paint().apply { color = if (darkTheme) android.graphics.Color.WHITE else android.graphics.Color.BLACK textSize = 12.sp.toPx() - textAlign = android.graphics.Paint.Align.LEFT + textAlign = Paint.Align.LEFT } drawText("Horizontal", width - 140.dp.toPx(), textOffsetY, paint) } drawCircle(Color(0xFFFF3B30), 5.dp.toPx(), Offset(width - 70.dp.toPx(), legendY)) drawContext.canvas.nativeCanvas.apply { - val paint = android.graphics.Paint().apply { + val paint = Paint().apply { color = if (darkTheme) android.graphics.Color.WHITE else android.graphics.Color.BLACK textSize = 12.sp.toPx() - textAlign = android.graphics.Paint.Align.LEFT + textAlign = Paint.Align.LEFT } drawText("Vertical", width - 60.dp.toPx(), textOffsetY, paint) } @@ -737,11 +757,3 @@ private fun AccelerationPlot() { } } } - -@ExperimentalHazeMaterialsApi -@RequiresApi(Build.VERSION_CODES.Q) -@Preview -@Composable -fun HeadTrackingScreenPreview() { - HeadTrackingScreen(navController = NavController(LocalContext.current)) -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HearingAidAdjustmentsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HearingAidAdjustmentsScreen.kt new file mode 100644 index 000000000..108e19c0d --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HearingAidAdjustmentsScreen.kt @@ -0,0 +1,224 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens + +import android.annotation.SuppressLint +import android.util.Log +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableFloatStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import com.kyant.backdrop.backdrops.layerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import dev.chrisbanes.haze.HazeState +import dev.chrisbanes.haze.hazeSource +import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi +import kotlinx.coroutines.Job +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.data.HearingAidSettings +import me.kavishdevar.librepods.data.parseHearingAidSettingsResponse +import me.kavishdevar.librepods.data.sendHearingAidSettings +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledSlider +import me.kavishdevar.librepods.presentation.components.StyledToggle +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel +import kotlin.io.encoding.ExperimentalEncodingApi + +private const val TAG = "HearingAidAdjustments" + +@SuppressLint("DefaultLocale") +@ExperimentalHazeMaterialsApi +@OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) +@Composable +fun HearingAidAdjustmentsScreen(viewModel: AirPodsViewModel) { + isSystemInDarkTheme() + val verticalScrollState = rememberScrollState() + val hazeState = remember { HazeState() } + val state by viewModel.uiState.collectAsState() + val backdrop = rememberLayerBackdrop() + + val debounceJob = remember { mutableStateOf(null) } + + val amplificationSliderValue = rememberSaveable { mutableFloatStateOf(0.5f) } + val balanceSliderValue = rememberSaveable { mutableFloatStateOf(0.5f) } + val toneSliderValue = rememberSaveable { mutableFloatStateOf(0.5f) } + val ambientNoiseReductionSliderValue = rememberSaveable { mutableFloatStateOf(0.0f) } + val conversationBoostEnabled = rememberSaveable { mutableStateOf(false) } + val leftEQ = rememberSaveable { mutableStateOf(FloatArray(8)) } + val rightEQ = rememberSaveable { mutableStateOf(FloatArray(8)) } + val ownVoiceAmplification = rememberSaveable { mutableFloatStateOf(0.5f) } + + val initialized = rememberSaveable { mutableStateOf(false) } + + val hearingAidSettings = remember { mutableStateOf( + HearingAidSettings( + leftEQ = leftEQ.value, + rightEQ = rightEQ.value, + leftAmplification = 0f, + rightAmplification = 0f, + leftTone = 0f, + rightTone = 0f, + leftConversationBoost = false, + rightConversationBoost = false, + leftAmbientNoiseReduction = 0f, + rightAmbientNoiseReduction = 0f, + netAmplification = 0f, + balance = 0f, + ownVoiceAmplification = 0f + ) + ) } + + LaunchedEffect(state.hearingAidData) { + parseHearingAidSettingsResponse(state.hearingAidData)?.let { parsed -> + amplificationSliderValue.floatValue = parsed.netAmplification + balanceSliderValue.floatValue = parsed.balance + toneSliderValue.floatValue = parsed.leftTone + ambientNoiseReductionSliderValue.floatValue = parsed.leftAmbientNoiseReduction + conversationBoostEnabled.value = parsed.leftConversationBoost + leftEQ.value = parsed.leftEQ.copyOf() + rightEQ.value = parsed.rightEQ.copyOf() + ownVoiceAmplification.floatValue = parsed.ownVoiceAmplification + initialized.value = true + } + } + + LaunchedEffect( + amplificationSliderValue.floatValue, + balanceSliderValue.floatValue, + toneSliderValue.floatValue, + conversationBoostEnabled.value, + ambientNoiseReductionSliderValue.floatValue, + ownVoiceAmplification.floatValue + ) { + if (!initialized.value) return@LaunchedEffect + hearingAidSettings.value = HearingAidSettings( + leftEQ = leftEQ.value, + rightEQ = rightEQ.value, + leftAmplification = amplificationSliderValue.floatValue + if (balanceSliderValue.floatValue < 0) -balanceSliderValue.floatValue else 0f, + rightAmplification = amplificationSliderValue.floatValue + if (balanceSliderValue.floatValue > 0) balanceSliderValue.floatValue else 0f, + leftTone = toneSliderValue.floatValue, + rightTone = toneSliderValue.floatValue, + leftConversationBoost = conversationBoostEnabled.value, + rightConversationBoost = conversationBoostEnabled.value, + leftAmbientNoiseReduction = ambientNoiseReductionSliderValue.floatValue, + rightAmbientNoiseReduction = ambientNoiseReductionSliderValue.floatValue, + netAmplification = amplificationSliderValue.floatValue, + balance = balanceSliderValue.floatValue, + ownVoiceAmplification = ownVoiceAmplification.floatValue + ) + Log.d(TAG, "Updated settings: ${hearingAidSettings.value}") + sendHearingAidSettings(state.hearingAidData, hearingAidSettings.value, debounceJob, viewModel::setATTCharacteristicValue) + } + + StyledScaffold(title = stringResource(R.string.adjustments)) { spacerHeight -> + Column( + modifier = Modifier + .hazeSource(hazeState) + .fillMaxSize() + .layerBackdrop(backdrop) + .verticalScroll(verticalScrollState) + .padding(horizontal = 16.dp), + verticalArrangement = Arrangement.spacedBy(16.dp) + ) { + Spacer(modifier = Modifier.height(spacerHeight)) + + StyledSlider( + label = stringResource(R.string.amplification), + valueRange = -1f..1f, + value = amplificationSliderValue.floatValue, + onValueChange = { + amplificationSliderValue.floatValue = it + }, + startIcon = "􀊥", + endIcon = "􀊩", + independent = true, + ) + + StyledToggle( + label = stringResource(R.string.swipe_to_control_amplification), + checked = state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.HPS_GAIN_SWIPE]?.getOrNull(0) == 0x01.toByte(), + onCheckedChange = { viewModel.setControlCommandBoolean(AACPManager.Companion.ControlCommandIdentifiers.HPS_GAIN_SWIPE, it) }, + description = stringResource(R.string.swipe_amplification_description) + ) + + StyledSlider( + label = stringResource(R.string.balance), + valueRange = -1f..1f, + value = balanceSliderValue.floatValue, + onValueChange = { + balanceSliderValue.floatValue = it + }, + snapPoints = listOf(-1f, 0f, 1f), + startLabel = stringResource(R.string.left), + endLabel = stringResource(R.string.right), + independent = true, + ) + + StyledSlider( + label = stringResource(R.string.tone), + valueRange = -1f..1f, + value = toneSliderValue.floatValue, + onValueChange = { + toneSliderValue.floatValue = it + }, + startLabel = stringResource(R.string.darker), + endLabel = stringResource(R.string.brighter), + independent = true, + ) + + StyledSlider( + label = stringResource(R.string.ambient_noise_reduction), + valueRange = 0f..1f, + value = ambientNoiseReductionSliderValue.floatValue, + onValueChange = { + ambientNoiseReductionSliderValue.floatValue = it + }, + startLabel = stringResource(R.string.less), + endLabel = stringResource(R.string.more), + independent = true, + ) + + StyledToggle( + label = stringResource(R.string.conversation_boost), + checked = conversationBoostEnabled.value, + onCheckedChange = { conversationBoostEnabled.value = it }, + independent = true, + description = stringResource(R.string.conversation_boost_description) + ) + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/HearingAidScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HearingAidScreen.kt similarity index 62% rename from android/app/src/main/java/me/kavishdevar/librepods/screens/HearingAidScreen.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HearingAidScreen.kt index 8e067c000..3cb72ce40 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/HearingAidScreen.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HearingAidScreen.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.screens + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens import android.annotation.SuppressLint import android.util.Log @@ -37,8 +37,9 @@ import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier @@ -61,15 +62,14 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.ConfirmationDialog -import me.kavishdevar.librepods.composables.NavigationButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledToggle -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.ATTHandles -import me.kavishdevar.librepods.utils.parseTransparencySettingsResponse -import me.kavishdevar.librepods.utils.sendTransparencySettings +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.data.parseTransparencySettingsResponse +import me.kavishdevar.librepods.data.sendTransparencySettings +import me.kavishdevar.librepods.presentation.components.ConfirmationDialog +import me.kavishdevar.librepods.presentation.components.NavigationButton +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledToggle +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel import kotlin.io.encoding.ExperimentalEncodingApi private const val TAG = "AccessibilitySettings" @@ -78,23 +78,22 @@ private const val TAG = "AccessibilitySettings" @ExperimentalHazeMaterialsApi @OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) @Composable -fun HearingAidScreen(navController: NavController) { +fun HearingAidScreen(viewModel: AirPodsViewModel, navController: NavController) { val isDarkTheme = isSystemInDarkTheme() val textColor = if (isDarkTheme) Color.White else Color.Black val verticalScrollState = rememberScrollState() val snackbarHostState = remember { SnackbarHostState() } - val attManager = ServiceManager.getService()?.attManager ?: return - - val aacpManager = remember { ServiceManager.getService()?.aacpManager } val showDialog = remember { mutableStateOf(false) } val backdrop = rememberLayerBackdrop() val initialLoad = remember { mutableStateOf(true) } + val state by viewModel.uiState.collectAsState() + val hearingAidEnabled = remember { - val aidStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID } - val assistStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG } - mutableStateOf((aidStatus?.value?.getOrNull(1) == 0x01.toByte()) && (assistStatus?.value?.getOrNull(0) == 0x01.toByte())) + val aidStatus = state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID] + val assistStatus = state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG] + mutableStateOf((aidStatus?.getOrNull(1) == 0x01.toByte()) && (assistStatus?.getOrNull(0) == 0x01.toByte())) } val hazeStateS = remember { mutableStateOf(HazeState()) } // dont question this. i could possibly use something other than initializing it with an empty state and then replacing it with the the one provided by the scaffold @@ -102,7 +101,7 @@ fun HearingAidScreen(navController: NavController) { StyledScaffold( title = stringResource(R.string.hearing_aid), snackbarHostState = snackbarHostState, - ) { spacerHeight, hazeState -> + ) { topPadding, hazeState, bottomPadding -> Column( modifier = Modifier .layerBackdrop(backdrop) @@ -113,43 +112,18 @@ fun HearingAidScreen(navController: NavController) { verticalArrangement = Arrangement.spacedBy(8.dp) ) { hazeStateS.value = hazeState - Spacer(modifier = Modifier.height(spacerHeight)) - - val hearingAidListener = remember { - object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID.value || - controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG.value) { - val aidStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID } - val assistStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG } - hearingAidEnabled.value = (aidStatus?.value?.getOrNull(1) == 0x01.toByte()) && (assistStatus?.value?.getOrNull(0) == 0x01.toByte()) - } - } - } - } + Spacer(modifier = Modifier.height(topPadding)) // val mediaAssistEnabled = remember { mutableStateOf(false) } // val adjustMediaEnabled = remember { mutableStateOf(false) } // val adjustPhoneEnabled = remember { mutableStateOf(false) } - LaunchedEffect(Unit) { - aacpManager?.registerControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, hearingAidListener) - aacpManager?.registerControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, hearingAidListener) - } - - DisposableEffect(Unit) { - onDispose { - aacpManager?.unregisterControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, hearingAidListener) - aacpManager?.unregisterControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, hearingAidListener) - } - } - LaunchedEffect(hearingAidEnabled.value) { if (hearingAidEnabled.value && !initialLoad.value) { showDialog.value = true } else if (!hearingAidEnabled.value && !initialLoad.value) { - aacpManager?.sendControlCommand(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID.value, byteArrayOf(0x01, 0x02)) - aacpManager?.sendControlCommand(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG.value, 0x02.toByte()) + viewModel.setControlCommandValue(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, byteArrayOf(0x01, 0x02)) + viewModel.setControlCommandByte(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, 0x02.toByte()) hearingAidEnabled.value = false } initialLoad.value = false @@ -186,7 +160,8 @@ fun HearingAidScreen(navController: NavController) { ) { StyledToggle( label = stringResource(R.string.hearing_aid), - checkedState = hearingAidEnabled, + checked = hearingAidEnabled.value, + onCheckedChange = { hearingAidEnabled.value = it }, independent = false ) HorizontalDivider( @@ -198,7 +173,7 @@ fun HearingAidScreen(navController: NavController) { NavigationButton( to = "hearing_aid_adjustments", name = stringResource(R.string.adjustments), - navController, + navController = navController, independent = false ) } @@ -217,7 +192,7 @@ fun HearingAidScreen(navController: NavController) { NavigationButton( to = "update_hearing_test", name = stringResource(R.string.update_hearing_test), - navController, + navController = navController, independent = true ) @@ -258,9 +233,9 @@ fun HearingAidScreen(navController: NavController) { // independent = false // ) // } + Spacer(modifier = Modifier.height(bottomPadding)) } } - ConfirmationDialog( showDialog = showDialog, title = "Enable Hearing Aid", @@ -269,26 +244,37 @@ fun HearingAidScreen(navController: NavController) { dismissText = "Cancel", onConfirm = { showDialog.value = false - val enrolled = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID }?.value?.getOrNull(0) == 0x01.toByte() + val enrolled = state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID]?.getOrNull(0) == 0x01.toByte() if (!enrolled) { - aacpManager?.sendControlCommand(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID.value, byteArrayOf(0x01, 0x01)) + viewModel.setControlCommandValue(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, byteArrayOf(0x01, 0x01)) } else { - aacpManager.sendControlCommand(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID.value, byteArrayOf(0x01, 0x01)) + viewModel.setControlCommandValue(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, byteArrayOf(0x01, 0x01)) } - aacpManager?.sendControlCommand(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG.value, 0x01.toByte()) + viewModel.setControlCommandByte(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, 0x01.toByte()) hearingAidEnabled.value = true CoroutineScope(Dispatchers.IO).launch { try { - val data = attManager.read(ATTHandles.TRANSPARENCY) - val parsed = parseTransparencySettingsResponse(data) + if (state.hearingAidData.isEmpty()) { + Log.w(TAG, "read failed") + return@launch + } + val parsed = parseTransparencySettingsResponse(state.hearingAidData) + if (parsed == null) { + Log.w(TAG, "transparency parse failed") + return@launch + } val disabledSettings = parsed.copy(enabled = false) - sendTransparencySettings(attManager, disabledSettings) + sendTransparencySettings(viewModel::setATTCharacteristicValue, disabledSettings) } catch (e: Exception) { Log.e(TAG, "Error disabling transparency: ${e.message}") } } }, - hazeState = hazeStateS.value, - // backdrop = backdrop + onDismiss = { + hearingAidEnabled.value = false + showDialog.value = false + }, +// hazeState = hazeStateS.value, + backdrop = backdrop ) } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HearingProtectionScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HearingProtectionScreen.kt new file mode 100644 index 000000000..cf484b863 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/HearingProtectionScreen.kt @@ -0,0 +1,120 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens + +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.navigation.NavController +import com.kyant.backdrop.backdrops.layerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.bluetooth.ATTHandles +import me.kavishdevar.librepods.presentation.components.StyledButton +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledToggle +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel + +@Composable +fun HearingProtectionScreen(viewModel: AirPodsViewModel, navController: NavController) { + val backdrop = rememberLayerBackdrop() + val state by viewModel.uiState.collectAsState() + StyledScaffold( + title = stringResource(R.string.hearing_protection), + ) { spacerHeight -> + Column( + modifier = Modifier + .fillMaxSize() + .layerBackdrop(backdrop) + .padding(horizontal = 16.dp) + ) { + Spacer(modifier = Modifier.height(spacerHeight)) + if (!state.isPremium) { + StyledButton( + onClick = { + navController.navigate("purchase_screen") + }, + backdrop = rememberLayerBackdrop(), + modifier = Modifier.fillMaxWidth(), + maxScale = 0.05f, + surfaceColor = if (isSystemInDarkTheme()) Color(0xFF916100) else Color(0xFFE59900) + ) { + Text( + stringResource(R.string.unlock_advanced_features), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = Color.White + ), + ) + } + } + if (state.vendorIdHook) { + StyledToggle( + title = stringResource(R.string.environmental_noise), + label = stringResource(R.string.loud_sound_reduction), + description = stringResource(R.string.loud_sound_reduction_description), + checked = state.loudSoundReductionEnabled, + onCheckedChange = { + viewModel.setATTCharacteristicValue( + ATTHandles.LOUD_SOUND_REDUCTION, + byteArrayOf(if (it) 1.toByte() else 0.toByte()) + ) + }, + enabled = state.isPremium + ) + + Spacer(modifier = Modifier.height(12.dp)) + } + StyledToggle( + title = stringResource(R.string.workspace_use), + label = stringResource(R.string.ppe), + description = stringResource(R.string.workspace_use_description), + checked = state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.PPE_TOGGLE_CONFIG]?.getOrNull( + 0 + )?.toInt() == 1, + onCheckedChange = { + viewModel.setControlCommandBoolean( + AACPManager.Companion.ControlCommandIdentifiers.PPE_TOGGLE_CONFIG, it + ) + }, + enabled = state.isPremium + ) + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/OpenSourceLicensesScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/OpenSourceLicensesScreen.kt similarity index 60% rename from android/app/src/main/java/me/kavishdevar/librepods/screens/OpenSourceLicensesScreen.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/OpenSourceLicensesScreen.kt index 34f255b1c..8f57cdb56 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/OpenSourceLicensesScreen.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/OpenSourceLicensesScreen.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.screens + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens import android.annotation.SuppressLint import androidx.compose.foundation.isSystemInDarkTheme @@ -28,12 +28,9 @@ import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.mutableFloatStateOf -import androidx.compose.runtime.remember import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.navigation.NavController @@ -42,17 +39,9 @@ import com.kyant.backdrop.backdrops.rememberLayerBackdrop import com.mikepenz.aboutlibraries.ui.compose.m3.LibrariesContainer import com.mikepenz.aboutlibraries.ui.compose.produceLibraries import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledSlider -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager +import me.kavishdevar.librepods.presentation.components.StyledScaffold import kotlin.io.encoding.ExperimentalEncodingApi private var debounceJob: Job? = null @@ -76,7 +65,7 @@ fun OpenSourceLicensesScreen(navController: NavController) { verticalArrangement = Arrangement.spacedBy(16.dp) ) { Spacer(modifier = Modifier.height(spacerHeight)) - val context = androidx.compose.ui.platform.LocalContext.current + val context = LocalContext.current val libraries by produceLibraries { context.resources.openRawResource(R.raw.aboutlibraries) .bufferedReader() @@ -90,4 +79,4 @@ fun OpenSourceLicensesScreen(navController: NavController) { ) } } -} \ No newline at end of file +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/PressAndHoldSettingsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/PressAndHoldSettingsScreen.kt new file mode 100644 index 000000000..ef8a073e8 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/PressAndHoldSettingsScreen.kt @@ -0,0 +1,211 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +@file:OptIn(ExperimentalStdlibApi::class, ExperimentalEncodingApi::class) + +package me.kavishdevar.librepods.presentation.screens + +import android.util.Log +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.navigation.NavController +import com.kyant.backdrop.backdrops.layerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.data.StemAction +import me.kavishdevar.librepods.presentation.components.SelectItem +import me.kavishdevar.librepods.presentation.components.StyledButton +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledSelectList +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel +import kotlin.experimental.and +import kotlin.io.encoding.ExperimentalEncodingApi + +@ExperimentalHazeMaterialsApi +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun LongPress(viewModel: AirPodsViewModel, name: String, navController: NavController) { + val isDarkTheme = isSystemInDarkTheme() + val textColor = if (isDarkTheme) Color.White else Color.Black + + val state by viewModel.uiState.collectAsState() + + val modesByte = state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE_CONFIGS]?.get(0) ?: 0 + + Log.d("PressAndHoldSettingsScreen", "Current modes state: ${modesByte.toString(2)}") + Log.d("PressAndHoldSettingsScreen", "Off mode: ${(modesByte and 0x01) != 0.toByte()}") + Log.d("PressAndHoldSettingsScreen", "Transparency mode: ${(modesByte and 0x04) != 0.toByte()}") + Log.d("PressAndHoldSettingsScreen", "Noise Cancellation mode: ${(modesByte and 0x02) != 0.toByte()}") + Log.d("PressAndHoldSettingsScreen", "Adaptive mode: ${(modesByte and 0x08) != 0.toByte()}") + + val longPressAction = if (name.lowercase() == "left") state.leftAction else state.rightAction + val backdrop = rememberLayerBackdrop() + StyledScaffold( + title = name + ) { spacerHeight -> + Column ( + modifier = Modifier + .layerBackdrop(backdrop) + .fillMaxSize() + .padding(top = 8.dp) + .padding(horizontal = 16.dp) + ) { + Spacer(modifier = Modifier.height(spacerHeight)) + val actionItems = listOf( + SelectItem( + name = stringResource(R.string.noise_control), + selected = longPressAction == StemAction.CYCLE_NOISE_CONTROL_MODES, + onClick = { + viewModel.setLongPressAction(name, StemAction.CYCLE_NOISE_CONTROL_MODES) + } + ), + SelectItem( + name = stringResource(R.string.digital_assistant), + selected = longPressAction == StemAction.DIGITAL_ASSISTANT, + onClick = { + viewModel.setLongPressAction(name, StemAction.DIGITAL_ASSISTANT) + }, + enabled = state.isPremium + ) + ) + StyledSelectList(items = actionItems) + + if (!state.isPremium) { + Spacer(modifier = Modifier.height(24.dp)) + StyledButton( + onClick = { + navController.navigate("purchase_screen") + }, + backdrop = rememberLayerBackdrop(), + modifier = Modifier.fillMaxWidth(), + maxScale = 0.05f, + surfaceColor = if (isSystemInDarkTheme()) Color(0xFF916100) else Color(0xFFE59900) + ) { + Text( + stringResource(R.string.unlock_advanced_features), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = Color.White + ), + ) + } + Spacer(modifier = Modifier.height(8.dp)) + } + + if (longPressAction == StemAction.CYCLE_NOISE_CONTROL_MODES) { + Spacer(modifier = Modifier.height(32.dp)) + Text( + text = stringResource(R.string.noise_control), + style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), + fontFamily = FontFamily(Font(R.font.sf_pro)), + modifier = Modifier + .padding(horizontal = 18.dp) + ) + + Spacer(modifier = Modifier.height(8.dp)) + + val currentByte = state.controlStates[AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE_CONFIGS]?.get(0)?.toInt() ?: 0 + + val listeningModeItems = mutableListOf() + if (state.offListeningMode) { + listeningModeItems.add( + SelectItem( + name = stringResource(R.string.off), + description = stringResource(R.string.listening_mode_off_description), + iconRes = R.drawable.noise_cancellation, + selected = (currentByte and 0x01) != 0, + onClick = { + viewModel.toggleListeningMode(0x01) + } + ) + ) + } + listeningModeItems.addAll(listOf( + SelectItem( + name = stringResource(R.string.transparency), + description = stringResource(R.string.listening_mode_transparency_description), + iconRes = R.drawable.transparency, + selected = (currentByte and 0x04) != 0, + onClick = { + viewModel.toggleListeningMode(0x04) + } + ), + SelectItem( + name = stringResource(R.string.adaptive), + description = stringResource(R.string.listening_mode_adaptive_description), + iconRes = R.drawable.adaptive, + selected = (currentByte and 0x08) != 0, + onClick = { + viewModel.toggleListeningMode(0x08) + } + ), + SelectItem( + name = stringResource(R.string.noise_cancellation), + description = stringResource(R.string.listening_mode_noise_cancellation_description), + iconRes = R.drawable.noise_cancellation, + selected = (currentByte and 0x02) != 0, + onClick = { + viewModel.toggleListeningMode(0x02) + } + ) + )) + StyledSelectList(items = listeningModeItems) + Spacer(modifier = Modifier.height(8.dp)) + Text( + text = stringResource(R.string.press_and_hold_noise_control_description), + style = TextStyle( + fontSize = 12.sp, + fontWeight = FontWeight.Light, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), + modifier = Modifier + .padding(horizontal = 18.dp) + ) + } + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/PurchaseScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/PurchaseScreen.kt new file mode 100644 index 000000000..ae5cefe90 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/PurchaseScreen.kt @@ -0,0 +1,528 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens + +import androidx.compose.foundation.background +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.lifecycle.viewmodel.compose.viewModel +import androidx.navigation.NavController +import com.kyant.backdrop.backdrops.layerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import dev.chrisbanes.haze.hazeSource +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.presentation.components.StyledButton +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.viewmodel.PurchaseViewModel +import me.kavishdevar.librepods.utils.XposedState + +@Composable +fun PurchaseScreen( + viewModel: PurchaseViewModel = viewModel(), + navController: NavController +) { + val context = LocalContext.current + val scrollState = rememberScrollState() + val state by viewModel.uiState.collectAsState() + + val backdrop = rememberLayerBackdrop() + + StyledScaffold( + title = stringResource(R.string.unlock_advanced_features) + ) { topPadding, hazeState, bottomPadding -> + Column( + modifier = Modifier + .fillMaxSize() + .layerBackdrop(backdrop) + .hazeSource(state = hazeState) + .verticalScroll(scrollState) + .padding(horizontal = 16.dp) + ) { + Spacer(modifier = Modifier.height(topPadding)) + + val isDarkTheme = isSystemInDarkTheme() + val backgroundColor = if (isDarkTheme) Color(0xFF000000) else Color(0xFFF2F2F7) + val cardBackgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) + val textColor = if (isDarkTheme) Color.White else Color.Black + LaunchedEffect(state.isPremium) { + if (state.isPremium) { + navController.popBackStack() + } + } + if (!state.isPremium) { + Box( + modifier = Modifier + .background(backgroundColor) + .padding(horizontal = 16.dp, vertical = 4.dp) + ) { + Text( + text = stringResource(R.string.free_features), + style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + Column( + modifier = Modifier + .fillMaxWidth() + .background(cardBackgroundColor, RoundedCornerShape(28.dp)) + .padding(horizontal = 8.dp, vertical = 16.dp), + verticalArrangement = Arrangement.spacedBy(8.dp) + ) { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.ear_detection), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.ear_detection_description), + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.battery), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.battery_description), + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.noise_control), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.noise_control_description), + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + if (XposedState.isAvailable) { + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.hearing_aid) + " (" + stringResource( + R.string.requires_xposed + ) + ")", + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.hearing_aid_description).split("\n\n")[0], + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + } + } + + Spacer(modifier = Modifier.height(24.dp)) + Box( + modifier = Modifier + .background(backgroundColor) + .padding(horizontal = 16.dp, vertical = 4.dp) + ) { + Text( + text = stringResource(R.string.advanced_features), + style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Bold, + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) + ) + ) + } + Column( + modifier = Modifier + .fillMaxWidth() + .background(cardBackgroundColor, RoundedCornerShape(28.dp)) + .padding(horizontal = 8.dp, vertical = 16.dp), + verticalArrangement = Arrangement.spacedBy(8.dp) + ) { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.conversational_awareness), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.conversational_awareness_description), + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.digital_assistant_on_long_press), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.digital_assistant_on_long_press_description), + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.head_gestures), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.head_gestures_details), + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.advanced_device_settings), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.advanced_device_settings_description), + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.automatic_connection), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.automatic_connection_description), + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.customizations), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.customizations_description), + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + HorizontalDivider( + thickness = 1.dp, + color = Color(0x40888888), + modifier = Modifier + .padding(horizontal = 12.dp) + ) + Column( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(2.dp) + ) { + Text( + text = stringResource(R.string.support_the_development), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.support_development_description), + style = TextStyle( + fontSize = 12.sp, + color = textColor.copy(0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)), + ) + ) + } + } + Spacer(modifier = Modifier.height(8.dp)) + Text( + text = stringResource(R.string.feature_availability_disclaimer), + modifier = Modifier.fillMaxWidth(), + style = TextStyle( + fontSize = 14.sp, + fontWeight = FontWeight.Light, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor.copy(alpha = 0.6f), + textAlign = TextAlign.Center + ), + ) + + + Spacer(modifier = Modifier.height(24.dp)) + + StyledButton( + onClick = { + viewModel.purchase(context) + }, + backdrop = rememberLayerBackdrop(), + modifier = Modifier.fillMaxWidth(), + maxScale = 0.05f, + surfaceColor = if (isSystemInDarkTheme()) Color(0xFF0091FF) + else Color(0xFF0088FF) // if (isSystemInDarkTheme()) Color(0xFF916100) else Color(0xFFE59900) + ) { + Text( + stringResource(R.string.buy_price, state.price), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = Color.White + ), + ) + } + + Spacer(modifier = Modifier.height(8.dp)) + + StyledButton( + onClick = { + viewModel.restorePurchases() + }, + backdrop = rememberLayerBackdrop(), + modifier = Modifier.fillMaxWidth(), + maxScale = 0.05f, + isInteractive = false + ) { + Text( + stringResource(R.string.restore_purchases), + style = TextStyle( + fontSize = 16.sp, + fontWeight = FontWeight.Medium, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ), + ) + } + } + Spacer(modifier = Modifier.height(bottomPadding)) + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/RenameScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/RenameScreen.kt new file mode 100644 index 000000000..d06177b6d --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/RenameScreen.kt @@ -0,0 +1,85 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +@file:OptIn(ExperimentalEncodingApi::class) + +package me.kavishdevar.librepods.presentation.screens + +import android.content.Context +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.text.input.rememberTextFieldState +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalSoftwareKeyboardController +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.core.content.edit +import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.presentation.components.StyledInputField +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel +import kotlin.io.encoding.ExperimentalEncodingApi + + +@OptIn(ExperimentalMaterial3Api::class, ExperimentalHazeMaterialsApi::class) +@Composable +fun RenameScreen(viewModel: AirPodsViewModel) { + val sharedPreferences = LocalContext.current.getSharedPreferences("settings", Context.MODE_PRIVATE) + val focusRequester = remember { FocusRequester() } + val keyboardController = LocalSoftwareKeyboardController.current + + LaunchedEffect(Unit) { + focusRequester.requestFocus() + keyboardController?.show() + } + + StyledScaffold( + title = stringResource(R.string.name), + ) { spacerHeight -> + Column( + modifier = Modifier + .fillMaxSize() + .padding(horizontal = 16.dp) + ) { + Spacer(modifier = Modifier.height(spacerHeight)) + + val name = sharedPreferences.getString("name", "")?: "" + val textFieldState = rememberTextFieldState(initialText = name) + + LaunchedEffect(textFieldState.text) { + sharedPreferences.edit {putString("name", textFieldState.text as String?)} + viewModel.setName(textFieldState.text.toString()) + } + + StyledInputField( + textFieldState, + focusRequester + ) + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/TransparencySettingsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/TransparencySettingsScreen.kt similarity index 61% rename from android/app/src/main/java/me/kavishdevar/librepods/screens/TransparencySettingsScreen.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/TransparencySettingsScreen.kt index bc1d48e97..526771212 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/TransparencySettingsScreen.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/TransparencySettingsScreen.kt @@ -1,23 +1,24 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.screens + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens + +// import me.kavishdevar.librepods.utils.RadareOffsetFinder import android.annotation.SuppressLint import android.util.Log import androidx.compose.foundation.background @@ -41,12 +42,14 @@ import androidx.compose.material3.Slider import androidx.compose.material3.SliderDefaults import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableFloatStateOf -import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.Saver +import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.shadow @@ -58,24 +61,18 @@ import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import androidx.navigation.NavController import com.kyant.backdrop.backdrops.layerBackdrop import com.kyant.backdrop.backdrops.rememberLayerBackdrop import dev.chrisbanes.haze.hazeSource import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.delay import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledSlider -import me.kavishdevar.librepods.composables.StyledToggle -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.ATTHandles -import me.kavishdevar.librepods.utils.RadareOffsetFinder -import me.kavishdevar.librepods.utils.TransparencySettings -import me.kavishdevar.librepods.utils.parseTransparencySettingsResponse -import me.kavishdevar.librepods.utils.sendTransparencySettings -import java.io.IOException +import me.kavishdevar.librepods.data.TransparencySettings +import me.kavishdevar.librepods.data.parseTransparencySettingsResponse +import me.kavishdevar.librepods.data.sendTransparencySettings +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledSlider +import me.kavishdevar.librepods.presentation.components.StyledToggle +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel import kotlin.io.encoding.ExperimentalEncodingApi private const val TAG = "TransparencySettings" @@ -84,14 +81,10 @@ private const val TAG = "TransparencySettings" @ExperimentalHazeMaterialsApi @OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) @Composable -fun TransparencySettingsScreen(navController: NavController) { +fun TransparencySettingsScreen(viewModel: AirPodsViewModel) { val isDarkTheme = isSystemInDarkTheme() val textColor = if (isDarkTheme) Color.White else Color.Black val verticalScrollState = rememberScrollState() - val attManager = ServiceManager.getService()?.attManager ?: return - val aacpManager = remember { ServiceManager.getService()?.aacpManager } - val isSdpOffsetAvailable = - remember { mutableStateOf(RadareOffsetFinder.isSdpOffsetAvailable()) } val trackColor = if (isDarkTheme) Color(0xFFB3B3B3) else Color(0xFF929491) val activeTrackColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5) @@ -99,9 +92,12 @@ fun TransparencySettingsScreen(navController: NavController) { val backdrop = rememberLayerBackdrop() + + val state by viewModel.uiState.collectAsState() + StyledScaffold( title = stringResource(R.string.customize_transparency_mode) - ){ spacerHeight, hazeState -> + ){ topPadding, hazeState, bottomPadding -> Column( modifier = Modifier .hazeSource(hazeState) @@ -111,22 +107,29 @@ fun TransparencySettingsScreen(navController: NavController) { .padding(horizontal = 16.dp), verticalArrangement = Arrangement.spacedBy(16.dp) ) { - Spacer(modifier = Modifier.height(spacerHeight)) + Spacer(modifier = Modifier.height(topPadding)) val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - val enabled = remember { mutableStateOf(false) } - val amplificationSliderValue = remember { mutableFloatStateOf(0.5f) } - val balanceSliderValue = remember { mutableFloatStateOf(0.5f) } - val toneSliderValue = remember { mutableFloatStateOf(0.5f) } - val ambientNoiseReductionSliderValue = remember { mutableFloatStateOf(0.0f) } - val conversationBoostEnabled = remember { mutableStateOf(false) } - val eq = remember { mutableStateOf(FloatArray(8)) } - val phoneMediaEQ = remember { mutableStateOf(FloatArray(8) { 0.5f }) } - - val initialLoadComplete = remember { mutableStateOf(false) } + val enabled = rememberSaveable { mutableStateOf(false) } + val amplificationSliderValue = rememberSaveable { mutableFloatStateOf(0.5f) } + val balanceSliderValue = rememberSaveable { mutableFloatStateOf(0.5f) } + val toneSliderValue = rememberSaveable { mutableFloatStateOf(0.5f) } + val ambientNoiseReductionSliderValue = rememberSaveable { mutableFloatStateOf(0.0f) } + val conversationBoostEnabled = rememberSaveable { mutableStateOf(false) } + val eq = rememberSaveable( + saver = Saver( + save = { it.value.toList() }, + restore = { mutableStateOf(it.toFloatArray()) } + ) + ) { mutableStateOf(FloatArray(8)) } + val phoneMediaEQ = rememberSaveable( + saver = Saver( + save = { it.value.toList() }, + restore = { mutableStateOf(it.toFloatArray()) } + ) + ) { mutableStateOf(FloatArray(8) { 0.5f }) } - val initialReadSucceeded = remember { mutableStateOf(false) } - val initialReadAttempts = remember { mutableIntStateOf(0) } + val initialized = rememberSaveable { mutableStateOf(false) } val transparencySettings = remember { mutableStateOf( @@ -148,23 +151,6 @@ fun TransparencySettingsScreen(navController: NavController) { ) } - val transparencyListener = remember { - object : (ByteArray) -> Unit { - override fun invoke(value: ByteArray) { - val parsed = parseTransparencySettingsResponse(value) - enabled.value = parsed.enabled - amplificationSliderValue.floatValue = parsed.netAmplification - balanceSliderValue.floatValue = parsed.balance - toneSliderValue.floatValue = parsed.leftTone - ambientNoiseReductionSliderValue.floatValue = - parsed.leftAmbientNoiseReduction - conversationBoostEnabled.value = parsed.leftConversationBoost - eq.value = parsed.leftEQ.copyOf() - Log.d(TAG, "Updated transparency settings from notification") - } - } - } - LaunchedEffect( enabled.value, amplificationSliderValue.floatValue, @@ -172,23 +158,9 @@ fun TransparencySettingsScreen(navController: NavController) { toneSliderValue.floatValue, conversationBoostEnabled.value, ambientNoiseReductionSliderValue.floatValue, - eq.value, - initialLoadComplete.value, - initialReadSucceeded.value + eq.value ) { - if (!initialLoadComplete.value) { - Log.d(TAG, "Initial device load not complete - skipping send") - return@LaunchedEffect - } - - if (!initialReadSucceeded.value) { - Log.d( - TAG, - "Initial device read not successful yet - skipping send until read succeeds" - ) - return@LaunchedEffect - } - + if (!initialized.value) return@LaunchedEffect transparencySettings.value = TransparencySettings( enabled = enabled.value, leftEQ = eq.value, @@ -205,90 +177,38 @@ fun TransparencySettingsScreen(navController: NavController) { balance = balanceSliderValue.floatValue ) Log.d("TransparencySettings", "Updated settings: ${transparencySettings.value}") - sendTransparencySettings(attManager, transparencySettings.value) + sendTransparencySettings(viewModel::setATTCharacteristicValue, transparencySettings.value) } - DisposableEffect(Unit) { - onDispose { - attManager.unregisterListener(ATTHandles.TRANSPARENCY, transparencyListener) + LaunchedEffect(state.transparencyData) { + val parsedSettings = parseTransparencySettingsResponse(data = state.transparencyData) ?: return@LaunchedEffect + Log.d(TAG, "Initial transparency settings: $parsedSettings") + enabled.value = parsedSettings.enabled + amplificationSliderValue.floatValue = parsedSettings.netAmplification + balanceSliderValue.floatValue = parsedSettings.balance + toneSliderValue.floatValue = parsedSettings.leftTone + ambientNoiseReductionSliderValue.floatValue = + parsedSettings.leftAmbientNoiseReduction + conversationBoostEnabled.value = parsedSettings.leftConversationBoost + if (!eq.value.contentEquals(parsedSettings.leftEQ)) { + eq.value = parsedSettings.leftEQ.copyOf() } + initialized.value = true } - LaunchedEffect(Unit) { - Log.d(TAG, "Connecting to ATT...") - try { - attManager.enableNotifications(ATTHandles.TRANSPARENCY) - attManager.registerListener(ATTHandles.TRANSPARENCY, transparencyListener) - - // If we have an AACP manager, prefer its EQ data to populate EQ controls first - try { - if (aacpManager != null) { - Log.d(TAG, "Found AACPManager, reading cached EQ data") - val aacpEQ = aacpManager.eqData - if (aacpEQ.isNotEmpty()) { - eq.value = aacpEQ.copyOf() - phoneMediaEQ.value = aacpEQ.copyOf() - Log.d(TAG, "Populated EQ from AACPManager: ${aacpEQ.toList()}") - } else { - Log.d(TAG, "AACPManager EQ data empty") - } - } else { - Log.d(TAG, "No AACPManager available") - } - } catch (e: Exception) { - Log.w(TAG, "Error reading EQ from AACPManager: ${e.message}") - } - - var parsedSettings: TransparencySettings? = null - for (attempt in 1..3) { - initialReadAttempts.intValue = attempt - try { - val data = attManager.read(ATTHandles.TRANSPARENCY) - parsedSettings = parseTransparencySettingsResponse(data = data) - Log.d(TAG, "Parsed settings on attempt $attempt") - } catch (e: Exception) { - Log.w(TAG, "Read attempt $attempt failed: ${e.message}") - } - delay(200) - } - - if (parsedSettings != null) { - Log.d(TAG, "Initial transparency settings: $parsedSettings") - enabled.value = parsedSettings.enabled - amplificationSliderValue.floatValue = parsedSettings.netAmplification - balanceSliderValue.floatValue = parsedSettings.balance - toneSliderValue.floatValue = parsedSettings.leftTone - ambientNoiseReductionSliderValue.floatValue = - parsedSettings.leftAmbientNoiseReduction - conversationBoostEnabled.value = parsedSettings.leftConversationBoost - eq.value = parsedSettings.leftEQ.copyOf() - initialReadSucceeded.value = true - } else { - Log.d( - TAG, - "Failed to read/parse initial transparency settings after ${initialReadAttempts.intValue} attempts" - ) - } - } catch (e: IOException) { - e.printStackTrace() - } finally { - initialLoadComplete.value = true - } - } - - // Only show transparency mode section if SDP offset is available - if (isSdpOffsetAvailable.value) { + if (state.vendorIdHook) { StyledToggle( label = stringResource(R.string.transparency_mode), - checkedState = enabled, + checked = enabled.value, independent = true, - description = stringResource(R.string.customize_transparency_mode_description) + description = stringResource(R.string.customize_transparency_mode_description), + onCheckedChange = { enabled.value = it } ) Spacer(modifier = Modifier.height(4.dp)) StyledSlider( label = stringResource(R.string.amplification), valueRange = -1f..1f, - mutableFloatState = amplificationSliderValue, + value = amplificationSliderValue.floatValue, onValueChange = { amplificationSliderValue.floatValue = it }, @@ -300,7 +220,7 @@ fun TransparencySettingsScreen(navController: NavController) { StyledSlider( label = stringResource(R.string.balance), valueRange = -1f..1f, - mutableFloatState = balanceSliderValue, + value = balanceSliderValue.floatValue, onValueChange = { balanceSliderValue.floatValue = it }, @@ -313,7 +233,7 @@ fun TransparencySettingsScreen(navController: NavController) { StyledSlider( label = stringResource(R.string.tone), valueRange = -1f..1f, - mutableFloatState = toneSliderValue, + value = toneSliderValue.floatValue, onValueChange = { toneSliderValue.floatValue = it }, @@ -325,7 +245,7 @@ fun TransparencySettingsScreen(navController: NavController) { StyledSlider( label = stringResource(R.string.ambient_noise_reduction), valueRange = 0f..1f, - mutableFloatState = ambientNoiseReductionSliderValue, + value = ambientNoiseReductionSliderValue.floatValue, onValueChange = { ambientNoiseReductionSliderValue.floatValue = it }, @@ -336,14 +256,12 @@ fun TransparencySettingsScreen(navController: NavController) { StyledToggle( label = stringResource(R.string.conversation_boost), - checkedState = conversationBoostEnabled, + checked = conversationBoostEnabled.value, independent = true, - description = stringResource(R.string.conversation_boost_description) + description = stringResource(R.string.conversation_boost_description), + onCheckedChange = { conversationBoostEnabled.value = it } ) - } - // Only show transparency mode EQ section if SDP offset is available - if (isSdpOffsetAvailable.value) { Text( text = stringResource(R.string.equalizer), style = TextStyle( @@ -443,6 +361,8 @@ fun TransparencySettingsScreen(navController: NavController) { Spacer(modifier = Modifier.height(16.dp)) } + + Spacer(modifier = Modifier.height(bottomPadding)) } } } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/TroubleshootingScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/TroubleshootingScreen.kt similarity index 97% rename from android/app/src/main/java/me/kavishdevar/librepods/screens/TroubleshootingScreen.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/TroubleshootingScreen.kt index e1598efd4..d9195718e 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/TroubleshootingScreen.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/TroubleshootingScreen.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.screens + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens import android.content.Intent import android.widget.Toast @@ -94,7 +94,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledScaffold +import me.kavishdevar.librepods.presentation.components.StyledScaffold import me.kavishdevar.librepods.utils.LogCollector import java.io.File import java.text.SimpleDateFormat @@ -216,7 +216,7 @@ fun TroubleshootingScreen(navController: NavController) { ) { StyledScaffold( title = stringResource(R.string.troubleshooting) - ){ spacerHeight, hazeState -> + ){ topPadding, hazeState, bottomPadding -> Column( modifier = Modifier .fillMaxSize() @@ -225,7 +225,7 @@ fun TroubleshootingScreen(navController: NavController) { .verticalScroll(scrollState) .padding(horizontal = 16.dp) ) { - Spacer(modifier = Modifier.height(spacerHeight)) + Spacer(modifier = Modifier.height(topPadding)) Text( text = stringResource(R.string.saved_logs), diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/UpdateHearingTestScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/UpdateHearingTestScreen.kt new file mode 100644 index 000000000..175a830cc --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/UpdateHearingTestScreen.kt @@ -0,0 +1,285 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens + +import android.util.Log +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableFloatStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.Saver +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.kyant.backdrop.backdrops.layerBackdrop +import com.kyant.backdrop.backdrops.rememberLayerBackdrop +import dev.chrisbanes.haze.hazeSource +import kotlinx.coroutines.Job +import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.data.HearingAidSettings +import me.kavishdevar.librepods.data.parseHearingAidSettingsResponse +import me.kavishdevar.librepods.data.sendHearingAidSettings +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel + +private const val TAG = "HearingAidAdjustments" + +@Composable +fun UpdateHearingTestScreen(viewModel: AirPodsViewModel) { + val verticalScrollState = rememberScrollState() + val state by viewModel.uiState.collectAsState() + val backdrop = rememberLayerBackdrop() + StyledScaffold( + title = stringResource(R.string.hearing_test) + ) { topPadding, hazeState, bottomPadding -> + Column( + modifier = Modifier + .hazeSource(hazeState) + .fillMaxSize() + .layerBackdrop(backdrop) + .verticalScroll(verticalScrollState) + .padding(horizontal = 16.dp), + verticalArrangement = Arrangement.spacedBy(16.dp) + ) { + val textColor = if (isSystemInDarkTheme()) Color.White else Color.Black + + Spacer(modifier = Modifier.height(topPadding)) + + Text( + text = stringResource(R.string.hearing_test_value_instruction), + modifier = Modifier.fillMaxWidth(), + style = TextStyle( + fontSize = 16.sp, + color = textColor, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), + textAlign = TextAlign.Center, + ) + val tone = rememberSaveable { mutableFloatStateOf(0.5f) } + val ambientNoiseReduction = rememberSaveable { mutableFloatStateOf(0.0f) } + val ownVoiceAmplification = rememberSaveable { mutableFloatStateOf(0.5f) } + val leftAmplification = rememberSaveable { mutableFloatStateOf(0.5f) } + val rightAmplification = rememberSaveable { mutableFloatStateOf(0.5f) } + val conversationBoostEnabled = rememberSaveable { mutableStateOf(false) } + val leftEQ = rememberSaveable( + saver = Saver( + save = { it.value.toList() }, + restore = { mutableStateOf(it.toFloatArray()) } + ) + ) { + mutableStateOf(FloatArray(8)) + } + val rightEQ = rememberSaveable( + saver = Saver( + save = { it.value.toList() }, + restore = { mutableStateOf(it.toFloatArray()) } + ) + ) { + mutableStateOf(FloatArray(8)) + } + + val debounceJob = remember { mutableStateOf(null) } + val initialized = rememberSaveable { mutableStateOf(false) } + + val hearingAidSettings = remember { + mutableStateOf( + HearingAidSettings( + leftEQ = leftEQ.value, + rightEQ = rightEQ.value, + leftAmplification = leftAmplification.floatValue, + rightAmplification = rightAmplification.floatValue, + leftTone = tone.floatValue, + rightTone = tone.floatValue, + leftConversationBoost = conversationBoostEnabled.value, + rightConversationBoost = conversationBoostEnabled.value, + leftAmbientNoiseReduction = ambientNoiseReduction.floatValue, + rightAmbientNoiseReduction = ambientNoiseReduction.floatValue, + netAmplification = leftAmplification.floatValue + rightAmplification.floatValue / 2, + balance = 0.5f + (rightAmplification.floatValue - leftAmplification.floatValue) / 2, + ownVoiceAmplification = ownVoiceAmplification.floatValue + ) + ) + } + + LaunchedEffect(state.hearingAidData) { + val parsed = parseHearingAidSettingsResponse(state.hearingAidData) + if (parsed != null) { + leftEQ.value = parsed.leftEQ.copyOf() + rightEQ.value = parsed.rightEQ.copyOf() + conversationBoostEnabled.value = parsed.leftConversationBoost + tone.floatValue = parsed.leftTone + ambientNoiseReduction.floatValue = parsed.leftAmbientNoiseReduction + ownVoiceAmplification.floatValue = parsed.ownVoiceAmplification + leftAmplification.floatValue = parsed.leftAmplification + rightAmplification.floatValue = parsed.rightAmplification + initialized.value = true + Log.d(TAG, "Updated hearing aid settings from notification") + } else { + Log.w(TAG, "Failed to parse hearing aid settings from notification") + } + } + + LaunchedEffect( + leftEQ.value, + rightEQ.value, + conversationBoostEnabled.value, + leftAmplification.floatValue, + rightAmplification.floatValue, + tone.floatValue, + ambientNoiseReduction.floatValue, + ownVoiceAmplification.floatValue + ) { + if (!initialized.value) return@LaunchedEffect + hearingAidSettings.value = HearingAidSettings( + leftEQ = leftEQ.value, + rightEQ = rightEQ.value, + leftAmplification = leftAmplification.floatValue, + rightAmplification = rightAmplification.floatValue, + leftTone = tone.floatValue, + rightTone = tone.floatValue, + leftConversationBoost = conversationBoostEnabled.value, + rightConversationBoost = conversationBoostEnabled.value, + leftAmbientNoiseReduction = ambientNoiseReduction.floatValue, + rightAmbientNoiseReduction = ambientNoiseReduction.floatValue, + netAmplification = leftAmplification.floatValue + rightAmplification.floatValue / 2, + balance = 0.5f + (rightAmplification.floatValue - leftAmplification.floatValue) / 2, + ownVoiceAmplification = ownVoiceAmplification.floatValue + ) + Log.d(TAG, "Updated settings: ${hearingAidSettings.value}") + sendHearingAidSettings(state.hearingAidData, hearingAidSettings.value, debounceJob, viewModel::setATTCharacteristicValue) + } + + val frequencies = + listOf("250Hz", "500Hz", "1kHz", "2kHz", "3kHz", "4kHz", "6kHz", "8kHz") + + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy(8.dp) + ) { + Spacer(modifier = Modifier.width(60.dp)) + Text( + text = stringResource(R.string.left), + modifier = Modifier.weight(1f), + textAlign = TextAlign.Center, + style = TextStyle( + fontSize = 18.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + Text( + text = stringResource(R.string.right), + modifier = Modifier.weight(1f), + textAlign = TextAlign.Center, + style = TextStyle( + fontSize = 18.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)), + color = textColor + ) + ) + } + + frequencies.forEachIndexed { index, freq -> + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy(8.dp) + ) { + Text( + text = freq, + modifier = Modifier + .width(60.dp) + .align(Alignment.CenterVertically), + textAlign = TextAlign.End, + style = TextStyle( + color = textColor, + fontSize = 16.sp, + fontFamily = FontFamily(Font(R.font.sf_pro)) + ), + ) + OutlinedTextField( + value = leftEQ.value[index].toString(), + onValueChange = { newValue -> + val parsed = newValue.toFloatOrNull() + if (parsed != null) { + val newArray = leftEQ.value.copyOf() + newArray[index] = parsed + leftEQ.value = newArray + Log.d(TAG, "Left EQ updated at index $index to $parsed") + } + }, +// label = { Text("Value", fontSize = 14.sp, fontFamily = FontFamily(Font(R.font.sf_pro))) }, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal), + textStyle = TextStyle( + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontSize = 14.sp + ), + modifier = Modifier.weight(1f) + ) + OutlinedTextField( + value = rightEQ.value[index].toString(), + onValueChange = { newValue -> + val parsed = newValue.toFloatOrNull() + if (parsed != null) { + val newArray = rightEQ.value.copyOf() + newArray[index] = parsed + rightEQ.value = newArray + Log.d(TAG, "Right EQ updated at index $index to $parsed") + } + }, +// label = { Text("Value", fontSize = 14.sp, fontFamily = FontFamily(Font(R.font.sf_pro))) }, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal), + textStyle = TextStyle( + fontFamily = FontFamily(Font(R.font.sf_pro)), + fontSize = 14.sp + ), + modifier = Modifier.weight(1f) + ) + } + } + Spacer(modifier = Modifier.height(bottomPadding)) + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/VersionInfoScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/VersionInfoScreen.kt similarity index 75% rename from android/app/src/main/java/me/kavishdevar/librepods/screens/VersionInfoScreen.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/VersionInfoScreen.kt index 73f7fa6cd..266958762 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/VersionInfoScreen.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/screens/VersionInfoScreen.kt @@ -1,40 +1,40 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.screens + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.screens import androidx.compose.foundation.background -import android.annotation.SuppressLint import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color @@ -45,36 +45,23 @@ import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import androidx.navigation.NavController import com.kyant.backdrop.backdrops.layerBackdrop import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.Job import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.services.ServiceManager -import kotlin.io.encoding.ExperimentalEncodingApi +import me.kavishdevar.librepods.presentation.components.StyledScaffold +import me.kavishdevar.librepods.presentation.viewmodel.AirPodsViewModel -private var debounceJob: Job? = null - -@SuppressLint("DefaultLocale") -@ExperimentalHazeMaterialsApi -@OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) @Composable -fun VersionScreen(navController: NavController) { +fun VersionScreen(viewModel: AirPodsViewModel) { + val state by viewModel.uiState.collectAsState() val isDarkTheme = isSystemInDarkTheme() - val service = ServiceManager.getService() - if (service == null) return - val airpodsInstance = service.airpodsInstance - if (airpodsInstance == null) return - val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) val textColor = if (isDarkTheme) Color.White else Color.Black val backdrop = rememberLayerBackdrop() StyledScaffold( - title = stringResource(R.string.customize_adaptive_audio) + title = stringResource(R.string.version) ) { spacerHeight -> Column( modifier = Modifier @@ -93,7 +80,8 @@ fun VersionScreen(navController: NavController) { style = TextStyle( fontSize = 14.sp, fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f) + color = textColor.copy(alpha = 0.6f), + fontFamily = FontFamily(Font(R.font.sf_pro)) ) ) } @@ -120,7 +108,7 @@ fun VersionScreen(navController: NavController) { ) ) Text( - text = airpodsInstance.version1 ?: "N/A", + text = state.version1, style = TextStyle( fontSize = 16.sp, color = textColor.copy(0.8f), @@ -149,7 +137,7 @@ fun VersionScreen(navController: NavController) { ) ) Text( - text = airpodsInstance.version2 ?: "N/A", + text = state.version2, style = TextStyle( fontSize = 16.sp, color = textColor.copy(0.8f), @@ -178,7 +166,7 @@ fun VersionScreen(navController: NavController) { ) ) Text( - text = airpodsInstance.version3 ?: "N/A", + text = state.version3, style = TextStyle( fontSize = 16.sp, color = textColor.copy(0.8f), @@ -189,4 +177,4 @@ fun VersionScreen(navController: NavController) { } } } -} \ No newline at end of file +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/theme/Color.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/theme/Color.kt new file mode 100644 index 000000000..f2e44c1d3 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/theme/Color.kt @@ -0,0 +1,30 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + + +package me.kavishdevar.librepods.presentation.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) diff --git a/android/app/src/main/java/me/kavishdevar/librepods/ui/theme/Theme.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/theme/Theme.kt similarity index 62% rename from android/app/src/main/java/me/kavishdevar/librepods/ui/theme/Theme.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/theme/Theme.kt index 31e4f12b3..04225ccdd 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/ui/theme/Theme.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/theme/Theme.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.ui.theme + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.theme import android.os.Build import androidx.compose.foundation.isSystemInDarkTheme @@ -61,4 +61,4 @@ fun LibrePodsTheme( typography = Typography, content = content ) -} \ No newline at end of file +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/ui/theme/Type.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/theme/Type.kt similarity index 54% rename from android/app/src/main/java/me/kavishdevar/librepods/ui/theme/Type.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/theme/Type.kt index 79a52189c..72a44242a 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/ui/theme/Type.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/theme/Type.kt @@ -1,22 +1,22 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors -package me.kavishdevar.librepods.ui.theme + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.theme import androidx.compose.material3.Typography import androidx.compose.ui.text.TextStyle @@ -49,4 +49,4 @@ val Typography = Typography( letterSpacing = 0.5.sp ) */ -) \ No newline at end of file +) diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/viewmodel/AirPodsViewModel.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/viewmodel/AirPodsViewModel.kt new file mode 100644 index 000000000..939588e9f --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/viewmodel/AirPodsViewModel.kt @@ -0,0 +1,734 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.presentation.viewmodel + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.content.SharedPreferences +import android.content.pm.PackageManager +import android.util.Log +import android.widget.Toast +import androidx.core.content.edit +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import me.kavishdevar.librepods.BuildConfig +import me.kavishdevar.librepods.billing.BillingManager +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.bluetooth.AACPManager.Companion.ControlCommandIdentifiers +import me.kavishdevar.librepods.bluetooth.ATTCCCDHandles +import me.kavishdevar.librepods.bluetooth.ATTHandles +import me.kavishdevar.librepods.bluetooth.BluetoothConnectionManager +import me.kavishdevar.librepods.data.AirPodsInstance +import me.kavishdevar.librepods.data.AirPodsModels +import me.kavishdevar.librepods.data.AirPodsNotifications +import me.kavishdevar.librepods.data.Battery +import me.kavishdevar.librepods.data.BatteryComponent +import me.kavishdevar.librepods.data.BatteryStatus +import me.kavishdevar.librepods.data.Capability +import me.kavishdevar.librepods.data.ControlCommandRepository +import me.kavishdevar.librepods.data.CustomEq +import me.kavishdevar.librepods.data.StemAction +import me.kavishdevar.librepods.data.XposedRemotePrefProvider +import me.kavishdevar.librepods.services.AirPodsService + +@Suppress("ArrayInDataClass") +data class AirPodsUiState( + val deviceName: String, + + val isLocallyConnected: Boolean = false, + + val instance: AirPodsInstance? = null, + val capabilities: Set = emptySet(), + + val controlStates: Map = emptyMap(), + val offListeningMode: Boolean = true, + + val battery: List = emptyList(), + val ancMode: Int = 3, + + val modelName: String = "", + val actualModel: String = "", + val serialNumbers: List = emptyList(), + val version1: String = "", + val version2: String = "", + val version3: String = "", + + val headTrackingActive: Boolean = false, + val headGesturesEnabled: Boolean = true, + + val eqData: FloatArray = floatArrayOf(), + + val automaticEarDetectionEnabled: Boolean = true, + val automaticConnectionEnabled: Boolean = true, + + val leftAction: StemAction = StemAction.CYCLE_NOISE_CONTROL_MODES, + val rightAction: StemAction = StemAction.CYCLE_NOISE_CONTROL_MODES, + + val loudSoundReductionEnabled: Boolean = false, + val transparencyData: ByteArray = byteArrayOf(), + val hearingAidData: ByteArray = byteArrayOf(), + + val isPremium: Boolean = false, + val vendorIdHook: Boolean = false, + + val dynamicEndOfCharge: Boolean = false, + + val connectionSuccessful: Boolean = false, + val timeUntilFOSSPremiumExpiry: Long = 0L, + + val customEq: CustomEq = CustomEq(1, 50, 50, 50) // disabled +) + +class AirPodsViewModel( + private val service: AirPodsService, + private val sharedPreferences: SharedPreferences, + private val controlRepo: ControlCommandRepository, + private val appContext: Context +) : ViewModel() { + private val _uiState = MutableStateFlow( + AirPodsUiState( + deviceName = sharedPreferences.getString( + "name", + "AirPods Pro" + ) ?: "AirPods Pro" + ) + ) + val uiState: StateFlow = _uiState + + private var isDemoMode = false + val demoActivated = MutableSharedFlow() + + private val listeners = + mutableMapOf() + + private val xposedRemotePref = XposedRemotePrefProvider.create() + + private lateinit var broadcastReceiver: BroadcastReceiver + + private val _cameraAction = MutableStateFlow( + sharedPreferences.getString("camera_action", null) + ?.let { value -> AACPManager.Companion.StemPressType.entries.find { it.name == value } }) + + val cameraAction: StateFlow = _cameraAction + + fun setCameraAction(action: AACPManager.Companion.StemPressType?) { + sharedPreferences.edit { + if (action == null) remove("camera_action") + else putString("camera_action", action.name) + } + _cameraAction.value = action + } + + fun setCustomEq(low: Int, mid: Int, high: Int) { + require(low in 0..100) + require(mid in 0..100) + require(high in 0..100) + val updatedEq = _uiState.value.customEq.copy(low = low, mid = mid, high = high) + service.aacpManager.sendCustomEqPacket(updatedEq) + _uiState.update { + it.copy( + customEq = updatedEq + ) + } + } + + fun setCustomEqEnabled(enabled: Boolean) { + service.aacpManager.sendCustomEqPacket(_uiState.value.customEq.copy(state = if (enabled) 2 else 1)) + _uiState.update { + it.copy( + customEq = it.customEq.copy(state = if (enabled) 2 else 1) + ) + } + } + + init { + observeBroadcasts() + loadName() + loadInstance() + loadSharedPreferences() + observeAACP() + loadControlList() + loadEq() + loadATT() + observeATT() + observeSharedPreferences() + observeBilling() + if (isDemoMode) activateDemoMode() + } + + override fun onCleared() { + listeners.forEach { (id, listener) -> + controlRepo.remove(id, listener) + } + service.aacpManager.customEqCallback = null + appContext.unregisterReceiver(broadcastReceiver) + + super.onCleared() + } + + private fun loadName() { + val name = sharedPreferences.getString("name", "AirPods Pro")!! + _uiState.update { it.copy(deviceName = name) } + } + + private fun observeBilling() { + if (isDemoMode) return + viewModelScope.launch { +// if (!BuildConfig.PLAY_BUILD) billingFirstCollectDone = true // FOSS doesn't send multiple events + BillingManager.provider.isPremium.collect { premium -> +// if (!billingFirstCollectDone) { +// billingFirstCollectDone = true +// return@collect +// } + if (premium) { + sharedPreferences.edit { + remove("premium_expiry_time") + if (BuildConfig.PLAY_BUILD) remove("foss_upgraded") + } + _uiState.update { it.copy(isPremium = true, timeUntilFOSSPremiumExpiry = 0L) } + } else { + if (_uiState.value.timeUntilFOSSPremiumExpiry <= 0L) { + setControlCommandBoolean( + ControlCommandIdentifiers.CONVERSATION_DETECT_CONFIG, + false + ) + setHeadGesturesEnabled(false) + _uiState.update { it.copy(isPremium = false) } + } + } + } + } + } + + private fun observeSharedPreferences() { + val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key -> + when (key) { + "name" -> loadName() + "off_listening_mode", "automatic_ear_detection", "automatic_connection_ctrl_cmd", + "head_gestures", "left_long_press_action", "right_long_press_action", + "dynamic_end_of_charge", "foss_upgraded", "premium_expiry_time" -> loadSharedPreferences() + } + } + sharedPreferences.registerOnSharedPreferenceChangeListener(listener) + } + + private fun observeBroadcasts() { + broadcastReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + val action = intent?.action ?: return + if (!isDemoMode) when (action) { + AirPodsNotifications.AIRPODS_L2CAP_CONNECTED -> { + _uiState.update { + it.copy(isLocallyConnected = true) + } + } + + AirPodsNotifications.AIRPODS_DISCONNECTED -> { + _uiState.update { + it.copy(isLocallyConnected = false) + } + } + + AirPodsNotifications.BATTERY_DATA -> { + _uiState.update { + it.copy(battery = service.getBattery()) + } + } + + AirPodsNotifications.EQ_DATA -> { + val data = intent.getFloatArrayExtra("eqData") ?: floatArrayOf() + + _uiState.update { + it.copy(eqData = data) + } + } + + AirPodsNotifications.AIRPODS_INFORMATION_UPDATED -> { + loadInstance() + } + } + } + } + + val filter = IntentFilter().apply { + addAction(AirPodsNotifications.AIRPODS_CONNECTED) + addAction(AirPodsNotifications.AIRPODS_DISCONNECTED) + addAction(AirPodsNotifications.BATTERY_DATA) + addAction(AirPodsNotifications.EQ_DATA) + addAction(AirPodsNotifications.AIRPODS_INFORMATION_UPDATED) + } + + appContext.registerReceiver( + broadcastReceiver, filter, Context.RECEIVER_NOT_EXPORTED + ) + } + + fun setControlCommandValue( + identifier: ControlCommandIdentifiers, value: ByteArray + ) { + if (!isDemoMode) controlRepo.setValue(identifier, value) + _uiState.update { + it.copy( + controlStates = it.controlStates + (identifier to value) + ) + } + } + + fun setControlCommandBoolean( + identifier: ControlCommandIdentifiers, enabled: Boolean + ) { + setControlCommandValue( + identifier, if (enabled) byteArrayOf(0x01) else byteArrayOf(0x02) + ) + } + + fun setControlCommandInt( + identifier: ControlCommandIdentifiers, value: Int + ) { + setControlCommandValue(identifier, byteArrayOf(value.toByte())) + } + + fun setControlCommandByte( + identifier: ControlCommandIdentifiers, value: Byte + ) { + setControlCommandValue(identifier, byteArrayOf(value)) + } + + fun observeControl(identifier: ControlCommandIdentifiers) { + val listener = controlRepo.observe(identifier) { value -> + _uiState.update { state -> + val current = state.controlStates[identifier] + if (current?.contentEquals(value) == true) return@update state + + if (identifier == ControlCommandIdentifiers.DYNAMIC_END_OF_CHARGE) { + state.copy( + dynamicEndOfCharge = value[0] == 0x01.toByte(), + controlStates = state.controlStates + (identifier to value) + ) + } else { + state.copy( + controlStates = state.controlStates + (identifier to value) + ) + } + } + } + + listeners[identifier] = listener + } + + // I'm lazy, sorry. + fun observeAACP() { + val identifiersList = listOf( + ControlCommandIdentifiers.MIC_MODE, + ControlCommandIdentifiers.DOUBLE_CLICK_INTERVAL, + ControlCommandIdentifiers.CLICK_HOLD_INTERVAL, + ControlCommandIdentifiers.LISTENING_MODE_CONFIGS, + ControlCommandIdentifiers.ONE_BUD_ANC_MODE, + ControlCommandIdentifiers.LISTENING_MODE, + ControlCommandIdentifiers.AUTO_ANSWER_MODE, + ControlCommandIdentifiers.CHIME_VOLUME, + ControlCommandIdentifiers.VOLUME_SWIPE_INTERVAL, + ControlCommandIdentifiers.CALL_MANAGEMENT_CONFIG, + ControlCommandIdentifiers.VOLUME_SWIPE_MODE, + ControlCommandIdentifiers.ADAPTIVE_VOLUME_CONFIG, + ControlCommandIdentifiers.CONVERSATION_DETECT_CONFIG, + ControlCommandIdentifiers.HEARING_AID, + ControlCommandIdentifiers.AUTO_ANC_STRENGTH, + ControlCommandIdentifiers.HPS_GAIN_SWIPE, + ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, + ControlCommandIdentifiers.ALLOW_OFF_OPTION, + ControlCommandIdentifiers.STEM_CONFIG, + ControlCommandIdentifiers.SLEEP_DETECTION_CONFIG, + ControlCommandIdentifiers.ALLOW_AUTO_CONNECT, + ControlCommandIdentifiers.EAR_DETECTION_CONFIG, + ControlCommandIdentifiers.AUTOMATIC_CONNECTION_CONFIG, + ControlCommandIdentifiers.OWNS_CONNECTION, + ControlCommandIdentifiers.PPE_TOGGLE_CONFIG, + ControlCommandIdentifiers.DYNAMIC_END_OF_CHARGE + ) + for (identifier in identifiersList) { + observeControl(identifier) + } + service.aacpManager.customEqCallback = { customEq -> + _uiState.update { it.copy(customEq = customEq) } + } + } + + fun refreshInitialData() { + if (isDemoMode) return + service.let { service -> + _uiState.update { + it.copy( + isLocallyConnected = BluetoothConnectionManager.getAACPSocket()?.isConnected == true, battery = service.getBattery() + ) + } + } + } + + private fun loadSharedPreferences() { + val offListeningModeEnabled = sharedPreferences.getBoolean("off_listening_mode", true) + val automaticEarDetectionEnabled = + sharedPreferences.getBoolean("automatic_ear_detection", true) + val automaticConnectionEnabled = + sharedPreferences.getBoolean("automatic_connection_ctrl_cmd", true) + val headGesturesEnabled = sharedPreferences.getBoolean("head_gestures", true) + val leftAction = StemAction.valueOf( + sharedPreferences.getString( + "left_long_press_action", + "CYCLE_NOISE_CONTROL_MODES" + ) ?: "CYCLE_NOISE_CONTROL_MODES" + ) + val rightAction = StemAction.valueOf( + sharedPreferences.getString( + "right_long_press_action", + "CYCLE_NOISE_CONTROL_MODES" + ) ?: "CYCLE_NOISE_CONTROL_MODES" + ) + val vendorIdHook = xposedRemotePref.getBoolean("vendor_id_hook", false) + val dynamicEndOfCharge = sharedPreferences.getBoolean("dynamic_end_of_charge", false) + + val connectionSuccessful = sharedPreferences.getBoolean("connection_successful", false) + + _uiState.update { + it.copy( + offListeningMode = offListeningModeEnabled, + automaticEarDetectionEnabled = automaticEarDetectionEnabled, + automaticConnectionEnabled = automaticConnectionEnabled, + headGesturesEnabled = headGesturesEnabled, + leftAction = leftAction, + rightAction = rightAction, + vendorIdHook = vendorIdHook, + dynamicEndOfCharge = dynamicEndOfCharge, + connectionSuccessful = connectionSuccessful, + ) + } + + // faulty update on Play caused PLAY_BUILD to be false and resulted in use of FOSS billing in Play. since FOSS is not verified, we need to give 2 weeks to verify the purchase + if (BuildConfig.PLAY_BUILD) { + val fossUpgraded = sharedPreferences.getBoolean("foss_upgraded", false) + val expiryTime = sharedPreferences.getLong("premium_expiry_time", 0L) + val now = System.currentTimeMillis() + + when { + // existing temporary premium + expiryTime > 0L -> { + if (expiryTime <= now) { + sharedPreferences.edit { + remove("premium_expiry_time") + remove("foss_upgraded") + } + + _uiState.update { + it.copy( + timeUntilFOSSPremiumExpiry = 0L, + isPremium = false + ) + } + } else { + _uiState.update { + it.copy( + timeUntilFOSSPremiumExpiry = expiryTime - now, + isPremium = true + ) + } + } + } + + // First migration from accidental FOSS Play build + fossUpgraded && !_uiState.value.isPremium -> { + val newExpiry = now + 28L * 24 * 60 * 60 * 1000 + + sharedPreferences.edit { + putLong("premium_expiry_time", newExpiry) + } + + _uiState.update { + it.copy( + timeUntilFOSSPremiumExpiry = newExpiry - now, + isPremium = true + ) + } + } + } + } + } + + fun setOffListeningMode(enabled: Boolean) { + sharedPreferences.edit { putBoolean("off_listening_mode", enabled) } + setControlCommandBoolean(ControlCommandIdentifiers.ALLOW_OFF_OPTION, enabled) + _uiState.update { + it.copy(offListeningMode = enabled) + } + } + + fun setHeadGesturesEnabled(enabled: Boolean) { + sharedPreferences.edit { putBoolean("head_gestures", enabled) } + _uiState.update { + it.copy(headGesturesEnabled = enabled) + } + } + + fun setDynamicEndOfCharge(enabled: Boolean) { + service.aacpManager.sendControlCommand(ControlCommandIdentifiers.DYNAMIC_END_OF_CHARGE.value, enabled) + sharedPreferences.edit { putBoolean("dynamic_end_of_charge", enabled) } + _uiState.update { + it.copy(dynamicEndOfCharge = enabled) + } + } + + private fun loadControlList() { + _uiState.update { + it.copy( + controlStates = controlRepo.getMap() + ) + } + } + + private fun loadEq() { + _uiState.update { + it.copy( + customEq = service.aacpManager.customEq + ) + } + } + + private fun loadInstance() { + val instance = service.airpodsInstance ?: AirPodsInstance( + name = "AirPods", + model = AirPodsModels.getModelByModelNumber("A3049")!!, + actualModelNumber = "A3049", + serialNumber = null, + leftSerialNumber = null, + rightSerialNumber = null, + version1 = null, + version2 = null, + version3 = null, + ) + + _uiState.update { + it.copy( + capabilities = instance.model.capabilities, + instance = instance, + modelName = instance.model.displayName, + actualModel = instance.actualModelNumber, + serialNumbers = listOf( + instance.serialNumber ?: "", + instance.leftSerialNumber ?: "", + instance.rightSerialNumber ?: "" + ), + version1 = instance.version1 ?: "", + version2 = instance.version2 ?: "", + version3 = instance.version3 ?: "" + ) + } + } + + fun reconnectFromSavedMac() { + service.reconnectFromSavedMac() + } + + fun setName(name: String) { + service.setName(name) + } + + fun startHeadTracking() { + service.startHeadTracking() + _uiState.update { it.copy(headTrackingActive = true) } + } + + fun stopHeadTracking() { + service.stopHeadTracking() + _uiState.update { it.copy(headTrackingActive = false) } + } + + fun setATTCharacteristicValue(handle: ATTHandles, value: ByteArray) { + when (handle) { + // ideally should be using a different viewmodel for ATT based things because there are a lot of values, and I am not going to add all to this state, but there's loudsoundreduction. + ATTHandles.LOUD_SOUND_REDUCTION -> { + _uiState.value = _uiState.value.copy(loudSoundReductionEnabled = value[0].toInt() == 0x01) + } + ATTHandles.HEARING_AID -> { + _uiState.value = _uiState.value.copy(hearingAidData = value) + } + ATTHandles.TRANSPARENCY -> { + _uiState.value = _uiState.value.copy(transparencyData = value) + } + } + viewModelScope.launch(Dispatchers.IO) { + try { + service.attManager.writeCharacteristic(handle, value) + } catch (e: Exception) { + e.printStackTrace() + } + } + } + + fun loadATT() { + val loudSoundReduction = service.attManager.getCharacteristic(ATTHandles.LOUD_SOUND_REDUCTION) ?: byteArrayOf() + val loudSoundReductionEnabled = if (loudSoundReduction.isNotEmpty()) { + loudSoundReduction[0].toInt() == 1 + } else false + val hearingAidData = service.attManager.getCharacteristic(ATTHandles.HEARING_AID) ?: byteArrayOf() + val transparencyData = service.attManager.getCharacteristic(ATTHandles.TRANSPARENCY) ?: byteArrayOf() + _uiState.update { + it.copy( + loudSoundReductionEnabled = loudSoundReductionEnabled, + transparencyData = transparencyData, + hearingAidData = hearingAidData + ) + } + } + + fun observeATT() { + viewModelScope.launch(Dispatchers.IO) { + service.attManager.enableNotification(ATTCCCDHandles.HEARING_AID) + service.attManager.enableNotification(ATTCCCDHandles.TRANSPARENCY) +// service.attManager.enableNotification(ATTCCCDHandles.LOUD_SOUND_REDUCTION) + } + service.attManager.setOnNotificationReceived { handle, value -> + when (handle) { + ATTHandles.LOUD_SOUND_REDUCTION.value.toByte() -> { + val loudSoundReductionEnabled = if (value.isNotEmpty()) { + value[0].toInt() == 1 + } else false + _uiState.update { + it.copy(loudSoundReductionEnabled = loudSoundReductionEnabled) + } + } + ATTHandles.HEARING_AID.value.toByte() -> { + _uiState.update { + it.copy(hearingAidData = value) + } + } + ATTHandles.TRANSPARENCY.value.toByte() -> { + _uiState.update { + it.copy(transparencyData = value) + } + } + } + } + } + + fun setAutomaticEarDetectionEnabled(enabled: Boolean) { + sharedPreferences.edit { putBoolean("automatic_ear_detection", enabled) } + setControlCommandBoolean(ControlCommandIdentifiers.EAR_DETECTION_CONFIG, enabled) + _uiState.update { + it.copy( + automaticEarDetectionEnabled = enabled + ) + } + } + + fun setAutomaticConnectionEnabled(enabled: Boolean) { + sharedPreferences.edit { putBoolean("automatic_connection_ctrl_cmd", enabled) } + setControlCommandBoolean(ControlCommandIdentifiers.AUTOMATIC_CONNECTION_CONFIG, enabled) + _uiState.update { + it.copy( + automaticConnectionEnabled = enabled + ) + } + } + + fun activateDemoMode() { + isDemoMode = true + viewModelScope.launch { + demoActivated.emit(Unit) + } + val fakeInstance = AirPodsInstance( + name = "AirPods Pro (Demo)", + model = AirPodsModels.getModelByModelNumber("A3049")!!, + actualModelNumber = "A3049", + serialNumber = "DEMO123", + leftSerialNumber = "L-DEMO", + rightSerialNumber = "R-DEMO", + version1 = "1.0", + version2 = "1.0", + version3 = "1.0", + ) + + _uiState.update { + it.copy( + isLocallyConnected = true, + instance = fakeInstance, + capabilities = fakeInstance.model.capabilities, + + battery = listOf( + Battery(BatteryComponent.LEFT, 85, BatteryStatus.CHARGING), + Battery(BatteryComponent.RIGHT, 25, BatteryStatus.NOT_CHARGING), + Battery(BatteryComponent.CASE, 85, BatteryStatus.CHARGING), + ), + + modelName = fakeInstance.model.displayName, + actualModel = fakeInstance.actualModelNumber, + serialNumbers = listOf("DEMO", "DEMO", "DEMO"), + version3 = "Demo Firmware", + isPremium = true + ) + } + } + + fun sendPhoneMediaEQ(eq: FloatArray, phoneByte: Byte, mediaByte: Byte) { + service.aacpManager.sendPhoneMediaEQ(eq, phoneByte, mediaByte) + } + + fun setLongPressAction(side: String, action: StemAction) { + val prefKey = if (side.lowercase() == "left") "left_long_press_action" else "right_long_press_action" + sharedPreferences.edit { putString(prefKey, action.name) } + _uiState.update { + if (side.lowercase() == "left") it.copy(leftAction = action) else it.copy(rightAction = action) + } + } + + private fun countEnabledModes(byteValue: Int): Int { + var count = 0 + if ((byteValue and 0x01) != 0) count++ + if ((byteValue and 0x02) != 0) count++ + if ((byteValue and 0x04) != 0) count++ + if ((byteValue and 0x08) != 0) count++ + return count + } + + fun toggleListeningMode(modeBit: Int) { + val currentByte = uiState.value.controlStates[ControlCommandIdentifiers.LISTENING_MODE_CONFIGS]?.get(0)?.toInt() ?: 0 + val newValue = if ((currentByte and modeBit) != 0) { + val temp = currentByte and modeBit.inv() + if (countEnabledModes(temp) >= 2) temp else currentByte + } else { + currentByte or modeBit + } + setControlCommandByte(ControlCommandIdentifiers.LISTENING_MODE_CONFIGS, newValue.toByte()) + sharedPreferences.edit { putInt("long_press_byte", newValue) } + } + + fun disconnect() { + service.disconnectAirPods() + if (appContext.checkSelfPermission("android.permission.BLUETOOTH_PRIVILEGED") != PackageManager.PERMISSION_GRANTED) { + Toast.makeText(appContext, "App has disconnected, disconnect from Android Settings.", + Toast.LENGTH_LONG).show() + } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/viewmodel/AppSettingsViewModel.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/viewmodel/AppSettingsViewModel.kt new file mode 100644 index 000000000..168e98b24 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/viewmodel/AppSettingsViewModel.kt @@ -0,0 +1,254 @@ +package me.kavishdevar.librepods.presentation.viewmodel + +import android.app.Application +import android.content.Context +import android.content.SharedPreferences +import androidx.core.content.edit +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import me.kavishdevar.librepods.BuildConfig +import me.kavishdevar.librepods.billing.BillingManager +import me.kavishdevar.librepods.data.XposedRemotePrefProvider +import kotlin.math.roundToInt + +data class AppSettingsUiState( + val showPhoneBatteryInWidget: Boolean = false, + val conversationalAwarenessPauseMusicEnabled: Boolean = false, + val relativeConversationalAwarenessVolumeEnabled: Boolean = true, + val disconnectWhenNotWearing: Boolean = false, + val takeoverWhenDisconnected: Boolean = false, + val takeoverWhenIdle: Boolean = false, + val takeoverWhenMusic: Boolean = false, + val takeoverWhenCall: Boolean = false, + val takeoverWhenRingingCall: Boolean = false, + val takeoverWhenMediaStart: Boolean = false, + val useAlternateHeadTrackingPackets: Boolean = true, + val conversationalAwarenessVolume: Float = 43f, + val showCameraDialog: Boolean = false, + val cameraPackageValue: String = "", + val cameraPackageError: String? = null, + val vendorIdHook: Boolean = false, + val isPremium: Boolean = false, + val connectionSuccessful: Boolean = false, + val showBottomSheetPopup: Boolean = true, + val showIslandPopup: Boolean = true, + val timeUntilFOSSPremiumExpiry: Long = 0L +) + +class AppSettingsViewModel(application: Application) : AndroidViewModel(application) { + private val sharedPreferences = application.getSharedPreferences("settings", Context.MODE_PRIVATE) + + private val _uiState = MutableStateFlow(AppSettingsUiState()) + val uiState = _uiState.asStateFlow() + + private val xposedRemotePref = XposedRemotePrefProvider.create() + + val sharedPrefListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPref, key -> + if (key == "connection_successful") { + _uiState.update { it.copy(connectionSuccessful = sharedPref.getBoolean(key, false)) } + } + } + + + init { + loadSettings() + observeBilling() + sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPrefListener) + } + + override fun onCleared() { + sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPrefListener) + super.onCleared() + } + + private fun observeBilling() { + viewModelScope.launch { + BillingManager.provider.isPremium.collect { premium -> + if (premium) { + sharedPreferences.edit { + remove("premium_expiry_time") + remove("foss_upgraded") + } + _uiState.update { it.copy(isPremium = true, timeUntilFOSSPremiumExpiry = 0L) } + } else { + // No billing premium, only update if no temporary premium is active + if (_uiState.value.timeUntilFOSSPremiumExpiry <= 0L) { + _uiState.update { it.copy(isPremium = false) } + } + } + } + } + } + + private fun loadSettings() { + // faulty update on Play caused PLAY_BUILD to be false and resulted in use of FOSS billing in Play. since FOSS is not verified, we need to give 2 weeks to verify the purchase + + val fossUpgraded = sharedPreferences.getBoolean("foss_upgraded", false) + val expiryTime = sharedPreferences.getLong("premium_expiry_time", 0L) + val now = System.currentTimeMillis() + + when { + // existing temporary premium + expiryTime > 0L -> { + if (expiryTime <= now) { + sharedPreferences.edit { + remove("premium_expiry_time") + remove("foss_upgraded") + } + + _uiState.update { + it.copy( + timeUntilFOSSPremiumExpiry = 0L, + isPremium = false + ) + } + } else { + _uiState.update { + it.copy( + timeUntilFOSSPremiumExpiry = expiryTime - now, + isPremium = true + ) + } + } + } + + // First migration from accidental FOSS Play build + fossUpgraded && !_uiState.value.isPremium && BuildConfig.PLAY_BUILD -> { + val newExpiry = now + 28L * 24 * 60 * 60 * 1000 + + sharedPreferences.edit { + putLong("premium_expiry_time", newExpiry) + } + + _uiState.update { + it.copy( + timeUntilFOSSPremiumExpiry = newExpiry - now, + isPremium = true + ) + } + } + } + + _uiState.update { currentState -> + currentState.copy( + showPhoneBatteryInWidget = sharedPreferences.getBoolean("show_phone_battery_in_widget", false), + conversationalAwarenessPauseMusicEnabled = sharedPreferences.getBoolean("conversational_awareness_pause_music", false), + relativeConversationalAwarenessVolumeEnabled = sharedPreferences.getBoolean("relative_conversational_awareness_volume", true), + disconnectWhenNotWearing = sharedPreferences.getBoolean("disconnect_when_not_wearing", false), + takeoverWhenDisconnected = sharedPreferences.getBoolean("takeover_when_disconnected", false), + takeoverWhenIdle = sharedPreferences.getBoolean("takeover_when_idle", false), + takeoverWhenMusic = sharedPreferences.getBoolean("takeover_when_music", false), + takeoverWhenCall = sharedPreferences.getBoolean("takeover_when_call", false), + takeoverWhenRingingCall = sharedPreferences.getBoolean("takeover_when_ringing_call", false), + takeoverWhenMediaStart = sharedPreferences.getBoolean("takeover_when_media_start", false), + useAlternateHeadTrackingPackets = sharedPreferences.getBoolean("use_alternate_head_tracking_packets", true), + conversationalAwarenessVolume = sharedPreferences.getInt("conversational_awareness_volume", 43).toFloat(), + cameraPackageValue = sharedPreferences.getString("custom_camera_package", "") ?: "", + vendorIdHook = xposedRemotePref.getBoolean("vendor_id_hook", false), + connectionSuccessful = sharedPreferences.getBoolean("connection_successful", false), + showBottomSheetPopup = sharedPreferences.getBoolean("show_bottom_sheet_popup", true), + showIslandPopup = sharedPreferences.getBoolean("show_island_popup", true) + ) + } + } + + fun setShowPhoneBatteryInWidget(enabled: Boolean) { + sharedPreferences.edit { putBoolean("show_phone_battery_in_widget", enabled) } + _uiState.update { it.copy(showPhoneBatteryInWidget = enabled) } + } + + fun setConversationalAwarenessPauseMusicEnabled(enabled: Boolean) { + sharedPreferences.edit { putBoolean("conversational_awareness_pause_music", enabled) } + _uiState.update { it.copy(conversationalAwarenessPauseMusicEnabled = enabled) } + } + + fun setRelativeConversationalAwarenessVolumeEnabled(enabled: Boolean) { + sharedPreferences.edit { putBoolean("relative_conversational_awareness_volume", enabled) } + _uiState.update { it.copy(relativeConversationalAwarenessVolumeEnabled = enabled) } + } + + fun setDisconnectWhenNotWearing(enabled: Boolean) { + sharedPreferences.edit { putBoolean("disconnect_when_not_wearing", enabled) } + _uiState.update { it.copy(disconnectWhenNotWearing = enabled) } + } + + fun setTakeoverWhenDisconnected(enabled: Boolean) { + sharedPreferences.edit { putBoolean("takeover_when_disconnected", enabled) } + _uiState.update { it.copy(takeoverWhenDisconnected = enabled) } + } + + fun setTakeoverWhenIdle(enabled: Boolean) { + sharedPreferences.edit { putBoolean("takeover_when_idle", enabled) } + _uiState.update { it.copy(takeoverWhenIdle = enabled) } + } + + fun setTakeoverWhenMusic(enabled: Boolean) { + sharedPreferences.edit { putBoolean("takeover_when_music", enabled) } + _uiState.update { it.copy(takeoverWhenMusic = enabled) } + } + + fun setTakeoverWhenCall(enabled: Boolean) { + sharedPreferences.edit { putBoolean("takeover_when_call", enabled) } + _uiState.update { it.copy(takeoverWhenCall = enabled) } + } + + fun setTakeoverWhenRingingCall(enabled: Boolean) { + sharedPreferences.edit { putBoolean("takeover_when_ringing_call", enabled) } + _uiState.update { it.copy(takeoverWhenRingingCall = enabled) } + } + + fun setTakeoverWhenMediaStart(enabled: Boolean) { + sharedPreferences.edit { putBoolean("takeover_when_media_start", enabled) } + _uiState.update { it.copy(takeoverWhenMediaStart = enabled) } + } + + fun setUseAlternateHeadTrackingPackets(enabled: Boolean) { + sharedPreferences.edit { putBoolean("use_alternate_head_tracking_packets", enabled) } + _uiState.update { it.copy(useAlternateHeadTrackingPackets = enabled) } + } + + fun setConversationalAwarenessVolume(volume: Float) { + sharedPreferences.edit { putInt("conversational_awareness_volume", volume.roundToInt()) } + _uiState.update { it.copy(conversationalAwarenessVolume = volume) } + } + + fun setShowCameraDialog(show: Boolean) { + _uiState.update { it.copy(showCameraDialog = show) } + } + + fun setCameraPackageValue(value: String) { + _uiState.update { it.copy(cameraPackageValue = value) } + } + + fun setCameraPackageError(error: String?) { + _uiState.update { it.copy(cameraPackageError = error) } + } + + fun saveCameraPackage() { + if (_uiState.value.cameraPackageValue.isBlank()) { + sharedPreferences.edit { remove("custom_camera_package") } + } else { + sharedPreferences.edit { putString("custom_camera_package", _uiState.value.cameraPackageValue) } + } + setShowCameraDialog(false) + } + + fun setVendorIdHook(enabled: Boolean) { + xposedRemotePref.putBoolean("vendor_id_hook", enabled) + _uiState.update { it.copy(vendorIdHook = enabled) } + } + + fun setShowBottomSheetPopup(enabled: Boolean) { + sharedPreferences.edit { putBoolean("show_bottom_sheet_popup", enabled) } + _uiState.update { it.copy(showBottomSheetPopup = enabled) } + } + + fun setShowIslandPopup(enabled: Boolean) { + sharedPreferences.edit { putBoolean("show_island_popup", enabled) } + _uiState.update { it.copy(showIslandPopup = enabled) } + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/viewmodel/PurchaseViewModel.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/viewmodel/PurchaseViewModel.kt new file mode 100644 index 000000000..b9e5235ec --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/viewmodel/PurchaseViewModel.kt @@ -0,0 +1,47 @@ +package me.kavishdevar.librepods.presentation.viewmodel + +import android.app.Activity +import android.app.Application +import android.content.Context +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import me.kavishdevar.librepods.billing.BillingManager + +data class PurchaseUiState( + val isPremium: Boolean = false, + val price: String = "" +) + +class PurchaseViewModel(application: Application) : AndroidViewModel(application) { + private val _uiState = MutableStateFlow(PurchaseUiState()) + val uiState = _uiState.asStateFlow() + + init { + observeBilling() + } + + private fun observeBilling() { + viewModelScope.launch { + BillingManager.provider.isPremium.collect { premium -> + _uiState.update { it.copy(isPremium = premium) } + } + } + viewModelScope.launch { + BillingManager.provider.price.collect { price -> + _uiState.update { it.copy(price = price) } + } + } + } + + fun purchase(context: Context) { + BillingManager.provider.purchase(context as Activity) + } + + fun restorePurchases() { + BillingManager.provider.restorePurchases() + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/presentation/widgets/BatteryWidget.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/widgets/BatteryWidget.kt new file mode 100644 index 000000000..20a12d549 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/widgets/BatteryWidget.kt @@ -0,0 +1,37 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +@file:OptIn(ExperimentalEncodingApi::class) + +package me.kavishdevar.librepods.presentation.widgets + +import android.appwidget.AppWidgetManager +import android.appwidget.AppWidgetProvider +import android.content.Context +import me.kavishdevar.librepods.services.ServiceManager +import kotlin.io.encoding.ExperimentalEncodingApi + +class BatteryWidget : AppWidgetProvider() { + override fun onUpdate( + context: Context, + appWidgetManager: AppWidgetManager, + appWidgetIds: IntArray + ) { + ServiceManager.getService()?.updateBattery() + } +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/widgets/NoiseControlWidget.kt b/android/app/src/main/java/me/kavishdevar/librepods/presentation/widgets/NoiseControlWidget.kt similarity index 72% rename from android/app/src/main/java/me/kavishdevar/librepods/widgets/NoiseControlWidget.kt rename to android/app/src/main/java/me/kavishdevar/librepods/presentation/widgets/NoiseControlWidget.kt index 3f5af9d1c..6253ccdf6 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/widgets/NoiseControlWidget.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/presentation/widgets/NoiseControlWidget.kt @@ -1,24 +1,24 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) -package me.kavishdevar.librepods.widgets +package me.kavishdevar.librepods.presentation.widgets import android.app.PendingIntent import android.appwidget.AppWidgetManager @@ -28,8 +28,8 @@ import android.content.Intent import android.util.Log import android.widget.RemoteViews import me.kavishdevar.librepods.R +import me.kavishdevar.librepods.bluetooth.AACPManager import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager import kotlin.io.encoding.ExperimentalEncodingApi class NoiseControlWidget : AppWidgetProvider() { @@ -82,8 +82,14 @@ class NoiseControlWidget : AppWidgetProvider() { if (intent.action == "ACTION_SET_ANC_MODE") { val mode = intent.getIntExtra("ANC_MODE", 1) Log.d("NoiseControlWidget", "Setting ANC mode to $mode") - ServiceManager.getService()!! - .aacpManager + val service = ServiceManager.getService() + + if (service == null) { + Log.w("NoiseControlWidget", "Service unavailable") + return + } + + service.aacpManager .sendControlCommand( AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE.value, mode.toByte() diff --git a/android/app/src/main/java/me/kavishdevar/librepods/receivers/BootReceiver.kt b/android/app/src/main/java/me/kavishdevar/librepods/receivers/BootReceiver.kt index 7a240c6be..180a7e953 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/receivers/BootReceiver.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/receivers/BootReceiver.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/AccessibilitySettingsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/AccessibilitySettingsScreen.kt deleted file mode 100644 index 0a37dfd32..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/AccessibilitySettingsScreen.kt +++ /dev/null @@ -1,840 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.screens - -import android.annotation.SuppressLint -import android.util.Log -import androidx.compose.foundation.background -import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress -import androidx.compose.foundation.gestures.detectTapGestures -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.foundation.verticalScroll -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableFloatStateOf -import androidx.compose.runtime.mutableLongStateOf -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip -import androidx.compose.ui.geometry.Offset -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.input.pointer.pointerInput -import androidx.compose.ui.layout.onGloballyPositioned -import androidx.compose.ui.layout.positionInParent -import androidx.compose.ui.platform.LocalDensity -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.Font -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.navigation.NavController -import com.kyant.backdrop.backdrops.layerBackdrop -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.HazeState -import dev.chrisbanes.haze.hazeSource -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.NavigationButton -import me.kavishdevar.librepods.composables.StyledDropdown -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledSlider -import me.kavishdevar.librepods.composables.StyledToggle -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.ATTHandles -import me.kavishdevar.librepods.utils.Capability -import me.kavishdevar.librepods.utils.RadareOffsetFinder -import kotlin.io.encoding.ExperimentalEncodingApi - -private var phoneMediaDebounceJob: Job? = null -private var toneVolumeDebounceJob: Job? = null -private const val TAG = "AccessibilitySettings" - -@SuppressLint("DefaultLocale") -@ExperimentalHazeMaterialsApi -@OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) -@Composable -fun AccessibilitySettingsScreen(navController: NavController) { - val isDarkTheme = isSystemInDarkTheme() - val textColor = if (isDarkTheme) Color.White else Color.Black - val aacpManager = remember { ServiceManager.getService()?.aacpManager } - val isSdpOffsetAvailable = - remember { mutableStateOf(RadareOffsetFinder.isSdpOffsetAvailable()) } - - val trackColor = if (isDarkTheme) Color(0xFFB3B3B3) else Color(0xFF929491) - val activeTrackColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5) - val thumbColor = if (isDarkTheme) Color(0xFFFFFFFF) else Color(0xFFFFFFFF) - - val capabilities = remember { ServiceManager.getService()?.airpodsInstance?.model?.capabilities ?: emptySet() } - - val hearingAidEnabled = remember { mutableStateOf( - aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID }?.value?.getOrNull(1) == 0x01.toByte() && - aacpManager.controlCommandStatusList.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG }?.value?.getOrNull(0) == 0x01.toByte() - ) } - - val hearingAidListener = remember { - object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID.value || - controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG.value) { - val aidStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID } - val assistStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG } - hearingAidEnabled.value = (aidStatus?.value?.getOrNull(1) == 0x01.toByte()) && (assistStatus?.value?.getOrNull(0) == 0x01.toByte()) - } - } - } - } - - LaunchedEffect(Unit) { - aacpManager?.registerControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, hearingAidListener) - aacpManager?.registerControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, hearingAidListener) - } - - DisposableEffect(Unit) { - onDispose { - aacpManager?.unregisterControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, hearingAidListener) - aacpManager?.unregisterControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, hearingAidListener) - } - } - - val backdrop = rememberLayerBackdrop() - - StyledScaffold( - title = stringResource(R.string.accessibility) - ) { spacerHeight, hazeState -> - Column( - modifier = Modifier - .fillMaxSize() - .hazeSource(hazeState) - .layerBackdrop(backdrop) - .verticalScroll(rememberScrollState()) - .padding(horizontal = 16.dp), - verticalArrangement = Arrangement.spacedBy(16.dp) - ) { - Spacer(modifier = Modifier.height(spacerHeight)) - val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - - val phoneMediaEQ = remember { mutableStateOf(FloatArray(8) { 0.5f }) } - val phoneEQEnabled = remember { mutableStateOf(false) } - val mediaEQEnabled = remember { mutableStateOf(false) } - - val pressSpeedOptions = mapOf( - 0.toByte() to "Default", - 1.toByte() to "Slower", - 2.toByte() to "Slowest" - ) - val selectedPressSpeedValue = - aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.DOUBLE_CLICK_INTERVAL }?.value?.takeIf { it.isNotEmpty() } - ?.get(0) - var selectedPressSpeed by remember { - mutableStateOf( - pressSpeedOptions[selectedPressSpeedValue] ?: pressSpeedOptions[0] - ) - } - val selectedPressSpeedListener = object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.DOUBLE_CLICK_INTERVAL.value) { - val newValue = controlCommand.value.takeIf { it.isNotEmpty() }?.get(0) - selectedPressSpeed = pressSpeedOptions[newValue] ?: pressSpeedOptions[0] - } - } - } - LaunchedEffect(Unit) { - aacpManager?.registerControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.DOUBLE_CLICK_INTERVAL, - selectedPressSpeedListener - ) - } - DisposableEffect(Unit) { - onDispose { - aacpManager?.unregisterControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.DOUBLE_CLICK_INTERVAL, - selectedPressSpeedListener - ) - } - } - - val pressAndHoldDurationOptions = mapOf( - 0.toByte() to "Default", - 1.toByte() to "Slower", - 2.toByte() to "Slowest" - ) - val selectedPressAndHoldDurationValue = - aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.CLICK_HOLD_INTERVAL }?.value?.takeIf { it.isNotEmpty() } - ?.get(0) - var selectedPressAndHoldDuration by remember { - mutableStateOf( - pressAndHoldDurationOptions[selectedPressAndHoldDurationValue] - ?: pressAndHoldDurationOptions[0] - ) - } - val selectedPressAndHoldDurationListener = object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.CLICK_HOLD_INTERVAL.value) { - val newValue = controlCommand.value.takeIf { it.isNotEmpty() }?.get(0) - selectedPressAndHoldDuration = - pressAndHoldDurationOptions[newValue] ?: pressAndHoldDurationOptions[0] - } - } - } - LaunchedEffect(Unit) { - aacpManager?.registerControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.CLICK_HOLD_INTERVAL, - selectedPressAndHoldDurationListener - ) - } - DisposableEffect(Unit) { - onDispose { - aacpManager?.unregisterControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.CLICK_HOLD_INTERVAL, - selectedPressAndHoldDurationListener - ) - } - } - - val volumeSwipeSpeedOptions = mapOf( - 1.toByte() to "Default", - 2.toByte() to "Longer", - 3.toByte() to "Longest" - ) - val selectedVolumeSwipeSpeedValue = - aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.VOLUME_SWIPE_INTERVAL }?.value?.takeIf { it.isNotEmpty() } - ?.get(0) - var selectedVolumeSwipeSpeed by remember { - mutableStateOf( - volumeSwipeSpeedOptions[selectedVolumeSwipeSpeedValue] - ?: volumeSwipeSpeedOptions[1] - ) - } - val selectedVolumeSwipeSpeedListener = object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.VOLUME_SWIPE_INTERVAL.value) { - val newValue = controlCommand.value.takeIf { it.isNotEmpty() }?.get(0) - selectedVolumeSwipeSpeed = - volumeSwipeSpeedOptions[newValue] ?: volumeSwipeSpeedOptions[1] - } - } - } - LaunchedEffect(Unit) { - aacpManager?.registerControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.VOLUME_SWIPE_INTERVAL, - selectedVolumeSwipeSpeedListener - ) - } - DisposableEffect(Unit) { - onDispose { - aacpManager?.unregisterControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.VOLUME_SWIPE_INTERVAL, - selectedVolumeSwipeSpeedListener - ) - } - } - - LaunchedEffect(phoneMediaEQ.value, phoneEQEnabled.value, mediaEQEnabled.value) { - phoneMediaDebounceJob?.cancel() - phoneMediaDebounceJob = CoroutineScope(Dispatchers.IO).launch { - delay(150) - val manager = ServiceManager.getService()?.aacpManager - if (manager == null) { - Log.w(TAG, "Cannot write EQ: AACPManager not available") - return@launch - } - try { - val phoneByte = if (phoneEQEnabled.value) 0x01.toByte() else 0x02.toByte() - val mediaByte = if (mediaEQEnabled.value) 0x01.toByte() else 0x02.toByte() - Log.d( - TAG, - "Sending phone/media EQ (phoneEnabled=${phoneEQEnabled.value}, mediaEnabled=${mediaEQEnabled.value})" - ) - manager.sendPhoneMediaEQ(phoneMediaEQ.value, phoneByte, mediaByte) - } catch (e: Exception) { - Log.w(TAG, "Error sending phone/media EQ: ${e.message}") - } - } - } - val toneVolumeValue = remember { mutableFloatStateOf( - aacpManager?.controlCommandStatusList?.find { - it.identifier == AACPManager.Companion.ControlCommandIdentifiers.CHIME_VOLUME - }?.value?.takeIf { it.isNotEmpty() }?.get(0)?.toFloat() ?: 75f - ) } - LaunchedEffect(toneVolumeValue.floatValue) { - toneVolumeDebounceJob?.cancel() - toneVolumeDebounceJob = CoroutineScope(Dispatchers.IO).launch { - delay(150) - val manager = ServiceManager.getService()?.aacpManager - if (manager == null) { - Log.w(TAG, "Cannot write tone volume: AACPManager not available") - return@launch - } - try { - manager.sendControlCommand( - identifier = AACPManager.Companion.ControlCommandIdentifiers.CHIME_VOLUME.value, - value = byteArrayOf(toneVolumeValue.floatValue.toInt().toByte(), 0x50.toByte()) - ) - } catch (e: Exception) { - Log.w(TAG, "Error sending tone volume: ${e.message}") - } - } - } - - DropdownMenuComponent( - label = stringResource(R.string.press_speed), - description = stringResource(R.string.press_speed_description), - options = pressSpeedOptions.values.toList(), - selectedOption = selectedPressSpeed?: "Default", - onOptionSelected = { newValue -> - selectedPressSpeed = newValue - aacpManager?.sendControlCommand( - identifier = AACPManager.Companion.ControlCommandIdentifiers.DOUBLE_CLICK_INTERVAL.value, - value = pressSpeedOptions.filterValues { it == newValue }.keys.firstOrNull() - ?: 0.toByte() - ) - }, - textColor = textColor, - hazeState = hazeState, - independent = true - ) - - DropdownMenuComponent( - label = stringResource(R.string.press_and_hold_duration), - description = stringResource(R.string.press_and_hold_duration_description), - options = pressAndHoldDurationOptions.values.toList(), - selectedOption = selectedPressAndHoldDuration?: "Default", - onOptionSelected = { newValue -> - selectedPressAndHoldDuration = newValue - aacpManager?.sendControlCommand( - identifier = AACPManager.Companion.ControlCommandIdentifiers.CLICK_HOLD_INTERVAL.value, - value = pressAndHoldDurationOptions.filterValues { it == newValue }.keys.firstOrNull() - ?: 0.toByte() - ) - }, - textColor = textColor, - hazeState = hazeState, - independent = true - ) - - StyledToggle( - title = stringResource(R.string.noise_control), - label = stringResource(R.string.noise_cancellation_single_airpod), - description = stringResource(R.string.noise_cancellation_single_airpod_description), - controlCommandIdentifier = AACPManager.Companion.ControlCommandIdentifiers.ONE_BUD_ANC_MODE, - independent = true, - ) - - if (capabilities.contains(Capability.LOUD_SOUND_REDUCTION)) { - StyledToggle( - label = stringResource(R.string.loud_sound_reduction), - description = stringResource(R.string.loud_sound_reduction_description), - attHandle = ATTHandles.LOUD_SOUND_REDUCTION - ) - } - - if (!hearingAidEnabled.value&& isSdpOffsetAvailable.value) { - NavigationButton( - to = "transparency_customization", - name = stringResource(R.string.customize_transparency_mode), - navController = navController - ) - } - - StyledSlider( - label = stringResource(R.string.tone_volume), - description = stringResource(R.string.tone_volume_description), - mutableFloatState = toneVolumeValue, - onValueChange = { - toneVolumeValue.floatValue = it - }, - valueRange = 0f..100f, - snapPoints = listOf(75f), - startIcon = "\uDBC0\uDEA1", - endIcon = "\uDBC0\uDEA9", - independent = true - ) - - if (capabilities.contains(Capability.SWIPE_FOR_VOLUME)) { - StyledToggle( - label = stringResource(R.string.volume_control), - description = stringResource(R.string.volume_control_description), - controlCommandIdentifier = AACPManager.Companion.ControlCommandIdentifiers.VOLUME_SWIPE_MODE, - ) - - DropdownMenuComponent( - label = stringResource(R.string.volume_swipe_speed), - description = stringResource(R.string.volume_swipe_speed_description), - options = volumeSwipeSpeedOptions.values.toList(), - selectedOption = selectedVolumeSwipeSpeed?: "Default", - onOptionSelected = { newValue -> - selectedVolumeSwipeSpeed = newValue - aacpManager?.sendControlCommand( - identifier = AACPManager.Companion.ControlCommandIdentifiers.VOLUME_SWIPE_INTERVAL.value, - value = volumeSwipeSpeedOptions.filterValues { it == newValue }.keys.firstOrNull() - ?: 1.toByte() - ) - }, - textColor = textColor, - hazeState = hazeState, - independent = true - ) - } - - if (!hearingAidEnabled.value&& isSdpOffsetAvailable.value) { -// Text( -// text = stringResource(R.string.apply_eq_to), -// style = TextStyle( -// fontSize = 14.sp, -// fontWeight = FontWeight.Bold, -// color = textColor.copy(alpha = 0.6f), -// fontFamily = FontFamily(Font(R.font.sf_pro)) -// ), -// modifier = Modifier.padding(8.dp, bottom = 0.dp) -// ) -// Column( -// modifier = Modifier -// .fillMaxWidth() -// .background(backgroundColor, RoundedCornerShape(28.dp)) -// .padding(vertical = 0.dp) -// ) { -// val darkModeLocal = isSystemInDarkTheme() -// -// val phoneShape = RoundedCornerShape(topStart = 28.dp, topEnd = 28.dp) -// var phoneBackgroundColor by remember { -// mutableStateOf( -// if (darkModeLocal) Color( -// 0xFF1C1C1E -// ) else Color(0xFFFFFFFF) -// ) -// } -// val phoneAnimatedBackgroundColor by animateColorAsState( -// targetValue = phoneBackgroundColor, -// animationSpec = tween(durationMillis = 500) -// ) -// -// Row( -// modifier = Modifier -// .height(48.dp) -// .fillMaxWidth() -// .background(phoneAnimatedBackgroundColor, phoneShape) -// .pointerInput(Unit) { -// detectTapGestures( -// onPress = { -// phoneBackgroundColor = -// if (darkModeLocal) Color(0x40888888) else Color(0x40D9D9D9) -// tryAwaitRelease() -// phoneBackgroundColor = -// if (darkModeLocal) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) -// phoneEQEnabled.value = !phoneEQEnabled.value -// } -// ) -// } -// .padding(horizontal = 16.dp), -// verticalAlignment = Alignment.CenterVertically -// ) { -// Text( -// stringResource(R.string.phone), -// fontSize = 16.sp, -// color = textColor, -// fontFamily = FontFamily(Font(R.font.sf_pro)), -// modifier = Modifier.weight(1f) -// ) -// Checkbox( -// checked = phoneEQEnabled.value, -// onCheckedChange = { phoneEQEnabled.value = it }, -// colors = CheckboxDefaults.colors().copy( -// checkedCheckmarkColor = Color(0xFF007AFF), -// uncheckedCheckmarkColor = Color.Transparent, -// checkedBoxColor = Color.Transparent, -// uncheckedBoxColor = Color.Transparent, -// checkedBorderColor = Color.Transparent, -// uncheckedBorderColor = Color.Transparent -// ), -// modifier = Modifier -// .height(24.dp) -// .scale(1.5f) -// ) -// } -// -// HorizontalDivider( -// thickness = 1.dp, -// color = Color(0x40888888) -// ) -// -// val mediaShape = RoundedCornerShape(bottomStart = 28.dp, bottomEnd = 28.dp) -// var mediaBackgroundColor by remember { -// mutableStateOf( -// if (darkModeLocal) Color( -// 0xFF1C1C1E -// ) else Color(0xFFFFFFFF) -// ) -// } -// val mediaAnimatedBackgroundColor by animateColorAsState( -// targetValue = mediaBackgroundColor, -// animationSpec = tween(durationMillis = 500) -// ) -// -// Row( -// modifier = Modifier -// .height(48.dp) -// .fillMaxWidth() -// .background(mediaAnimatedBackgroundColor, mediaShape) -// .pointerInput(Unit) { -// detectTapGestures( -// onPress = { -// mediaBackgroundColor = -// if (darkModeLocal) Color(0x40888888) else Color(0x40D9D9D9) -// tryAwaitRelease() -// mediaBackgroundColor = -// if (darkModeLocal) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) -// mediaEQEnabled.value = !mediaEQEnabled.value -// } -// ) -// } -// .padding(horizontal = 16.dp), -// verticalAlignment = Alignment.CenterVertically -// ) { -// Text( -// stringResource(R.string.media), -// fontSize = 16.sp, -// color = textColor, -// fontFamily = FontFamily(Font(R.font.sf_pro)), -// modifier = Modifier.weight(1f) -// ) -// Checkbox( -// checked = mediaEQEnabled.value, -// onCheckedChange = { mediaEQEnabled.value = it }, -// colors = CheckboxDefaults.colors().copy( -// checkedCheckmarkColor = Color(0xFF007AFF), -// uncheckedCheckmarkColor = Color.Transparent, -// checkedBoxColor = Color.Transparent, -// uncheckedBoxColor = Color.Transparent, -// checkedBorderColor = Color.Transparent, -// uncheckedBorderColor = Color.Transparent -// ), -// modifier = Modifier -// .height(24.dp) -// .scale(1.5f) -// ) -// } -// } - - // EQ Settings. Don't seem to have an effect? - // Column( - // modifier = Modifier - // .fillMaxWidth() - // .background(backgroundColor, RoundedCornerShape(28.dp)) - // .padding(12.dp), - // horizontalAlignment = Alignment.CenterHorizontally - // ) { - // for (i in 0 until 8) { - // val eqPhoneValue = - // remember(phoneMediaEQ.value[i]) { mutableFloatStateOf(phoneMediaEQ.value[i]) } - // Row( - // horizontalArrangement = Arrangement.SpaceBetween, - // verticalAlignment = Alignment.CenterVertically, - // modifier = Modifier - // .fillMaxWidth() - // .height(38.dp) - // ) { - // Text( - // text = String.format("%.2f", eqPhoneValue.floatValue), - // fontSize = 12.sp, - // color = textColor, - // modifier = Modifier.padding(bottom = 4.dp) - // ) - - // Slider( - // value = eqPhoneValue.floatValue, - // onValueChange = { newVal -> - // eqPhoneValue.floatValue = newVal - // val newEQ = phoneMediaEQ.value.copyOf() - // newEQ[i] = eqPhoneValue.floatValue - // phoneMediaEQ.value = newEQ - // }, - // valueRange = 0f..100f, - // modifier = Modifier - // .fillMaxWidth(0.9f) - // .height(36.dp), - // colors = SliderDefaults.colors( - // thumbColor = thumbColor, - // activeTrackColor = activeTrackColor, - // inactiveTrackColor = trackColor - // ), - // thumb = { - // Box( - // modifier = Modifier - // .size(24.dp) - // .shadow(4.dp, CircleShape) - // .background(thumbColor, CircleShape) - // ) - // }, - // track = { - // Box( - // modifier = Modifier - // .fillMaxWidth() - // .height(12.dp), - // contentAlignment = Alignment.CenterStart - // ) - // { - // Box( - // modifier = Modifier - // .fillMaxWidth() - // .height(4.dp) - // .background(trackColor, RoundedCornerShape(4.dp)) - // ) - // Box( - // modifier = Modifier - // .fillMaxWidth(eqPhoneValue.floatValue / 100f) - // .height(4.dp) - // .background(activeTrackColor, RoundedCornerShape(4.dp)) - // ) - // } - // } - // ) - - // Text( - // text = stringResource(R.string.band_label, i + 1), - // fontSize = 12.sp, - // color = textColor, - // modifier = Modifier.padding(top = 4.dp) - // ) - // } - // } - // } - } - } - } -} - -@ExperimentalHazeMaterialsApi -@Composable -private fun DropdownMenuComponent( - label: String, - options: List, - selectedOption: String, - onOptionSelected: (String) -> Unit, - textColor: Color, - hazeState: HazeState, - description: String? = null, - independent: Boolean = true -) { - val density = LocalDensity.current - val itemHeightPx = with(density) { 48.dp.toPx() } - - var expanded by remember { mutableStateOf(false) } - var touchOffset by remember { mutableStateOf(null) } - var boxPosition by remember { mutableStateOf(Offset.Zero) } - var lastDismissTime by remember { mutableLongStateOf(0L) } - var parentHoveredIndex by remember { mutableStateOf(null) } - var parentDragActive by remember { mutableStateOf(false) } - - Column(modifier = Modifier.fillMaxWidth()){ - Column( - modifier = Modifier - .fillMaxWidth() - .then( - if (independent) { - if (description != null) { - Modifier.padding(top = 8.dp, bottom = 4.dp) - } else { - Modifier.padding(vertical = 8.dp) - } - } else Modifier - ) - .background( - if (independent) (if (isSystemInDarkTheme()) Color(0xFF1C1C1E) else Color(0xFFFFFFFF)) else Color.Transparent, - if (independent) RoundedCornerShape(28.dp) else RoundedCornerShape(0.dp) - ) - then( - if (independent) Modifier.padding(horizontal = 4.dp) else Modifier - ) - .clip(if (independent) RoundedCornerShape(28.dp) else RoundedCornerShape(0.dp)) - ){ - Row( - modifier = Modifier - .fillMaxWidth() - .padding(start = 12.dp, end = 12.dp) - .height(58.dp) - .pointerInput(Unit) { - detectTapGestures { offset -> - val now = System.currentTimeMillis() - if (expanded) { - expanded = false - lastDismissTime = now - } else { - if (now - lastDismissTime > 250L) { - touchOffset = offset - expanded = true - } - } - } - } - .pointerInput(Unit) { - detectDragGesturesAfterLongPress( - onDragStart = { offset -> - val now = System.currentTimeMillis() - touchOffset = offset - if (!expanded && now - lastDismissTime > 250L) { - expanded = true - } - lastDismissTime = now - parentDragActive = true - parentHoveredIndex = 0 - }, - onDrag = { change, _ -> - val current = change.position - val touch = touchOffset ?: current - val posInPopupY = current.y - touch.y - val idx = (posInPopupY / itemHeightPx).toInt() - parentHoveredIndex = idx - }, - onDragEnd = { - parentDragActive = false - parentHoveredIndex?.let { idx -> - if (idx in options.indices) { - onOptionSelected(options[idx]) - expanded = false - lastDismissTime = System.currentTimeMillis() - } - } - parentHoveredIndex = null - }, - onDragCancel = { - parentDragActive = false - parentHoveredIndex = null - } - ) - }, - horizontalArrangement = Arrangement.SpaceBetween, - verticalAlignment = Alignment.CenterVertically - ) { - Column( - modifier = Modifier.weight(1f) - ){ - Text( - text = label, - fontSize = 16.sp, - color = textColor, - modifier = Modifier.padding(bottom = 4.dp) - ) - if (!independent && description != null){ - Text( - text = description, - style = TextStyle( - fontSize = 12.sp, - fontWeight = FontWeight.Light, - color = textColor.copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - modifier = Modifier.padding(16.dp, top = 0.dp, bottom = 2.dp) - ) - } - } - Box( - modifier = Modifier.onGloballyPositioned { coordinates -> - boxPosition = coordinates.positionInParent() - } - ) { - Row( - verticalAlignment = Alignment.CenterVertically - ) { - Text( - text = selectedOption, - style = TextStyle( - fontSize = 16.sp, - color = textColor.copy(alpha = 0.8f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - ) - Text( - text = "􀆏", - style = TextStyle( - fontSize = 16.sp, - color = textColor.copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - modifier = Modifier - .padding(start = 6.dp) - ) - } - - StyledDropdown( - expanded = expanded, - onDismissRequest = { - expanded = false - lastDismissTime = System.currentTimeMillis() - }, - options = options, - selectedOption = selectedOption, - touchOffset = touchOffset, - boxPosition = boxPosition, - externalHoveredIndex = parentHoveredIndex, - externalDragActive = parentDragActive, - onOptionSelected = { option -> - onOptionSelected(option) - expanded = false - }, - hazeState = hazeState - ) - } - } - } - if (independent && description != null){ - Box( - modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 16.dp) - .background(if (isSystemInDarkTheme()) Color(0xFF000000) else Color(0xFFF2F2F7)) - ){ - Text( - text = description, - style = TextStyle( - fontSize = 12.sp, - fontWeight = FontWeight.Light, - color = (if (isSystemInDarkTheme()) Color.White else Color.Black).copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - ) - } - } - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/AdaptiveStrengthScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/AdaptiveStrengthScreen.kt deleted file mode 100644 index e6e537b8a..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/AdaptiveStrengthScreen.kt +++ /dev/null @@ -1,135 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.screens - -import android.annotation.SuppressLint -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.mutableFloatStateOf -import androidx.compose.runtime.remember -import androidx.compose.ui.Modifier -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.unit.dp -import androidx.navigation.NavController -import com.kyant.backdrop.backdrops.layerBackdrop -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledSlider -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager -import kotlin.io.encoding.ExperimentalEncodingApi - -private var debounceJob: Job? = null - -@SuppressLint("DefaultLocale") -@ExperimentalHazeMaterialsApi -@OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) -@Composable -fun AdaptiveStrengthScreen(navController: NavController) { - val isDarkTheme = isSystemInDarkTheme() - - val sliderValue = remember { mutableFloatStateOf(0f) } - val service = ServiceManager.getService()!! - - LaunchedEffect(sliderValue) { - val sliderValueFromAACP = service.aacpManager.controlCommandStatusList.find { - it.identifier == AACPManager.Companion.ControlCommandIdentifiers.AUTO_ANC_STRENGTH - }?.value?.takeIf { it.isNotEmpty() }?.get(0) - sliderValueFromAACP?.toFloat()?.let { sliderValue.floatValue = (100 - it) } - } - - val listener = remember { - object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.AUTO_ANC_STRENGTH.value) { - controlCommand.value.takeIf { it.isNotEmpty() }?.get(0)?.toFloat()?.let { - sliderValue.floatValue = (100 - it) - } - } - } - } - } - - DisposableEffect(Unit) { - service.aacpManager.registerControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.AUTO_ANC_STRENGTH, - listener - ) - onDispose { - service.aacpManager.unregisterControlCommandListener( - AACPManager.Companion.ControlCommandIdentifiers.AUTO_ANC_STRENGTH, - listener - ) - } - } - - val backdrop = rememberLayerBackdrop() - - StyledScaffold( - title = stringResource(R.string.customize_adaptive_audio) - ) { spacerHeight -> - Column( - modifier = Modifier - .fillMaxSize() - .layerBackdrop(backdrop) - .padding(horizontal = 16.dp), - verticalArrangement = Arrangement.spacedBy(16.dp) - ) { - Spacer(modifier = Modifier.height(spacerHeight)) - StyledSlider( - label = stringResource(R.string.customize_adaptive_audio), - mutableFloatState = sliderValue, - onValueChange = { - sliderValue.floatValue = it - debounceJob?.cancel() - debounceJob = CoroutineScope(Dispatchers.Default).launch { - delay(300) - service.aacpManager.sendControlCommand( - AACPManager.Companion.ControlCommandIdentifiers.AUTO_ANC_STRENGTH.value, - (100 - it).toInt() - ) - } - }, - valueRange = 0f..100f, - snapPoints = listOf(0f, 50f, 100f), - startIcon = "􀊥", - endIcon = "􀊩", - independent = true, - description = stringResource(R.string.adaptive_audio_description) - ) - } - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/AirPodsSettingsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/AirPodsSettingsScreen.kt deleted file mode 100644 index 90ef913a3..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/AirPodsSettingsScreen.kt +++ /dev/null @@ -1,451 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -@file:OptIn(ExperimentalEncodingApi::class) - -package me.kavishdevar.librepods.screens - -import android.annotation.SuppressLint -import android.bluetooth.BluetoothDevice -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Context.MODE_PRIVATE -import android.content.Context.RECEIVER_EXPORTED -import android.content.Intent -import android.content.IntentFilter -import android.content.SharedPreferences -import android.os.Build -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.lazy.LazyColumn -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.SnackbarHostState -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberCoroutineScope -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.Font -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.input.TextFieldValue -import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.core.content.edit -import androidx.core.net.toUri -import androidx.navigation.NavController -import androidx.navigation.compose.rememberNavController -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import com.kyant.backdrop.drawBackdrop -import com.kyant.backdrop.highlight.Highlight -import dev.chrisbanes.haze.HazeState -import dev.chrisbanes.haze.hazeSource -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.launch -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.AboutCard -import me.kavishdevar.librepods.composables.AudioSettings -import me.kavishdevar.librepods.composables.BatteryView -import me.kavishdevar.librepods.composables.CallControlSettings -import me.kavishdevar.librepods.composables.ConfirmationDialog -import me.kavishdevar.librepods.composables.ConnectionSettings -import me.kavishdevar.librepods.composables.HearingHealthSettings -import me.kavishdevar.librepods.composables.MicrophoneSettings -import me.kavishdevar.librepods.composables.NavigationButton -import me.kavishdevar.librepods.composables.NoiseControlSettings -import me.kavishdevar.librepods.composables.PressAndHoldSettings -import me.kavishdevar.librepods.composables.StyledButton -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledToggle -import me.kavishdevar.librepods.constants.AirPodsNotifications -import me.kavishdevar.librepods.services.AirPodsService -import me.kavishdevar.librepods.ui.theme.LibrePodsTheme -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.Capability -import me.kavishdevar.librepods.utils.RadareOffsetFinder -import kotlin.io.encoding.ExperimentalEncodingApi - -@OptIn(ExperimentalMaterial3Api::class, ExperimentalHazeMaterialsApi::class) -@SuppressLint("MissingPermission", "UnspecifiedRegisterReceiverFlag") -@Composable -fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService, - navController: NavController, isConnected: Boolean, isRemotelyConnected: Boolean) { - var isLocallyConnected by remember { mutableStateOf(isConnected) } - var isRemotelyConnected by remember { mutableStateOf(isRemotelyConnected) } - val sharedPreferences = LocalContext.current.getSharedPreferences("settings", MODE_PRIVATE) - var device by remember { mutableStateOf(dev) } - var deviceName by remember { - mutableStateOf( - TextFieldValue( - sharedPreferences.getString("name", device?.name ?: "AirPods Pro").toString() - ) - ) - } - - LaunchedEffect(service) { - isLocallyConnected = service.isConnectedLocally - } - - val nameChangeListener = remember { - SharedPreferences.OnSharedPreferenceChangeListener { _, key -> - if (key == "name") { - deviceName = TextFieldValue(sharedPreferences.getString("name", "AirPods Pro").toString()) - } - } - } - - DisposableEffect(Unit) { - sharedPreferences.registerOnSharedPreferenceChangeListener(nameChangeListener) - onDispose { - sharedPreferences.unregisterOnSharedPreferenceChangeListener(nameChangeListener) - } - } - - val snackbarHostState = remember { SnackbarHostState() } - val coroutineScope = rememberCoroutineScope() - - fun handleRemoteConnection(connected: Boolean) { - isRemotelyConnected = connected - } - - val context = LocalContext.current - - val connectionReceiver = remember { - object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - when (intent?.action) { - "me.kavishdevar.librepods.AIRPODS_CONNECTED_REMOTELY" -> { - coroutineScope.launch { - handleRemoteConnection(true) - } - } - "me.kavishdevar.librepods.AIRPODS_DISCONNECTED_REMOTELY" -> { - coroutineScope.launch { - handleRemoteConnection(false) - } - } - AirPodsNotifications.AIRPODS_CONNECTED -> { - coroutineScope.launch { - isLocallyConnected = true - } - } - AirPodsNotifications.AIRPODS_DISCONNECTED -> { - coroutineScope.launch { - isLocallyConnected = false - } - } - AirPodsNotifications.DISCONNECT_RECEIVERS -> { - try { - context?.unregisterReceiver(this) - } catch (e: IllegalArgumentException) { - e.printStackTrace() - } - } - } - } - } - } - - DisposableEffect(Unit) { - val filter = IntentFilter().apply { - addAction("me.kavishdevar.librepods.AIRPODS_CONNECTED_REMOTELY") - addAction("me.kavishdevar.librepods.AIRPODS_DISCONNECTED_REMOTELY") - addAction(AirPodsNotifications.AIRPODS_CONNECTED) - addAction(AirPodsNotifications.AIRPODS_DISCONNECTED) - addAction(AirPodsNotifications.DISCONNECT_RECEIVERS) - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - context.registerReceiver(connectionReceiver, filter, RECEIVER_EXPORTED) - } else { - context.registerReceiver(connectionReceiver, filter) - } - onDispose { - try { - context.unregisterReceiver(connectionReceiver) - } catch (e: Exception) { - e.printStackTrace() - } - } - } - - LaunchedEffect(service) { - service.let { - it.sendBroadcast(Intent(AirPodsNotifications.BATTERY_DATA).apply { - putParcelableArrayListExtra("data", ArrayList(it.getBattery())) - }) - it.sendBroadcast(Intent(AirPodsNotifications.ANC_DATA).apply { - putExtra("data", it.getANC()) - }) - } - } - - val darkMode = isSystemInDarkTheme() - val hazeStateS = remember { mutableStateOf(HazeState()) } - - val showDialog = remember { mutableStateOf(!sharedPreferences.getBoolean("donationDialogShown", false)) } - - StyledScaffold( - title = deviceName.text, - actionButtons = listOf( - {scaffoldBackdrop -> - StyledIconButton( - onClick = { navController.navigate("app_settings") }, - icon = "􀍟", - darkMode = darkMode, - backdrop = scaffoldBackdrop - ) - } - ), - snackbarHostState = snackbarHostState - ) { spacerHeight, hazeState -> - hazeStateS.value = hazeState - if (isLocallyConnected || isRemotelyConnected) { - val instance = service.airpodsInstance - if (instance == null) { - Text("Error: AirPods instance is null") - return@StyledScaffold - } - val capabilities = instance.model.capabilities - LazyColumn( - modifier = Modifier - .fillMaxSize() - .hazeSource(hazeState) - .padding(horizontal = 16.dp) - ) { - item(key = "spacer_top") { Spacer(modifier = Modifier.height(spacerHeight)) } - item(key = "battery") { - BatteryView(service = service) - } - item(key = "spacer_battery") { Spacer(modifier = Modifier.height(32.dp)) } - - item(key = "name") { - NavigationButton( - to = "rename", - name = stringResource(R.string.name), - currentState = deviceName.text, - navController = navController, - independent = true - ) - } - val actAsAppleDeviceHookEnabled = RadareOffsetFinder.isSdpOffsetAvailable() - if (actAsAppleDeviceHookEnabled) { - item(key = "spacer_hearing_health") { Spacer(modifier = Modifier.height(32.dp)) } - item(key = "hearing_health") { - HearingHealthSettings(navController = navController) - } - } - - if (capabilities.contains(Capability.LISTENING_MODE)) { - item(key = "spacer_noise") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "noise_control") { NoiseControlSettings(service = service) } - } - - if (capabilities.contains(Capability.STEM_CONFIG)) { - item(key = "spacer_press_hold") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "press_hold") { PressAndHoldSettings(navController = navController) } - } - - item(key = "spacer_call") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "call_control") { CallControlSettings(hazeState = hazeState) } - - if (capabilities.contains(Capability.STEM_CONFIG)) { - item(key = "spacer_camera") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "camera_control") { NavigationButton(to = "camera_control", name = stringResource(R.string.camera_remote), description = stringResource(R.string.camera_control_description), title = stringResource(R.string.camera_control), navController = navController) } - } - - item(key = "spacer_audio") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "audio") { AudioSettings(navController = navController) } - - item(key = "spacer_connection") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "connection") { ConnectionSettings() } - - item(key = "spacer_microphone") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "microphone") { MicrophoneSettings(hazeState) } - - if (capabilities.contains(Capability.SLEEP_DETECTION)) { - item(key = "spacer_sleep") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "sleep_detection") { - StyledToggle( - label = stringResource(R.string.sleep_detection), - controlCommandIdentifier = AACPManager.Companion.ControlCommandIdentifiers.SLEEP_DETECTION_CONFIG - ) - } - } - - if (capabilities.contains(Capability.HEAD_GESTURES)) { - item(key = "spacer_head_tracking") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "head_tracking") { NavigationButton(to = "head_tracking", name = stringResource(R.string.head_gestures), navController = navController, currentState = if (sharedPreferences.getBoolean("head_gestures", false)) stringResource(R.string.on) else stringResource(R.string.off)) } - } - - item(key = "spacer_accessibility") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "accessibility") { NavigationButton(to = "accessibility", name = stringResource(R.string.accessibility), navController = navController) } - - if (capabilities.contains(Capability.LOUD_SOUND_REDUCTION)){ - item(key = "spacer_off_listening") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "off_listening") { - StyledToggle( - label = stringResource(R.string.off_listening_mode), - controlCommandIdentifier = AACPManager.Companion.ControlCommandIdentifiers.ALLOW_OFF_OPTION, - description = stringResource(R.string.off_listening_mode_description) - ) - } - } - - item(key = "spacer_about") { Spacer(modifier = Modifier.height(32.dp)) } - item(key = "about") { AboutCard(navController = navController) } - - item(key = "spacer_debug") { Spacer(modifier = Modifier.height(16.dp)) } - item(key = "debug") { NavigationButton("debug", "Debug", navController) } - item(key = "spacer_bottom") { Spacer(Modifier.height(24.dp)) } - } - } - else { - val backdrop = rememberLayerBackdrop() - Column( - modifier = Modifier - .fillMaxSize() - .drawBackdrop( - backdrop = rememberLayerBackdrop(), - exportedBackdrop = backdrop, - shape = { RoundedCornerShape(0.dp) }, - highlight = { - Highlight.Ambient.copy(alpha = 0f) - } - ) - .hazeSource(hazeState) - .padding(horizontal = 8.dp), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.Center - ) { - Text( - text = stringResource(R.string.airpods_not_connected), - style = TextStyle( - fontSize = 24.sp, - fontWeight = FontWeight.Medium, - color = if (isSystemInDarkTheme()) Color.White else Color.Black, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - textAlign = TextAlign.Center, - modifier = Modifier.fillMaxWidth() - ) - Spacer(Modifier.height(24.dp)) - Text( - text = stringResource(R.string.airpods_not_connected_description), - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Light, - color = if (isSystemInDarkTheme()) Color.White else Color.Black, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - textAlign = TextAlign.Center, - modifier = Modifier.fillMaxWidth() - ) - Spacer(Modifier.height(32.dp)) - StyledButton( - onClick = { navController.navigate("troubleshooting") }, - backdrop = backdrop, - modifier = Modifier - .fillMaxWidth(0.9f) - ) { - Text( - text = "Troubleshoot Connection", - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = if (isSystemInDarkTheme()) Color.White else Color.Black - ) - ) - } - Spacer(Modifier.height(16.dp)) - StyledButton( - onClick = { - service.reconnectFromSavedMac() - }, - backdrop = backdrop, - modifier = Modifier - .fillMaxWidth(0.9f) - ) { - Text( - text = stringResource(R.string.reconnect_to_last_device), - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = if (isSystemInDarkTheme()) Color.White else Color.Black - ) - ) - } - } - } - } - ConfirmationDialog( - showDialog = showDialog, - title = stringResource(R.string.support_librepods), - message = stringResource(R.string.support_dialog_description), - confirmText = stringResource(R.string.support_me) + " \uDBC0\uDEB5", - dismissText = stringResource(R.string.never_show_again), - onConfirm = { - val browserIntent = Intent( - Intent.ACTION_VIEW, - "https://github.com/sponsors/kavishdevar".toUri() - ) - context.startActivity(browserIntent) - sharedPreferences.edit { putBoolean("donationDialogShown", true) } - }, - onDismiss = { - sharedPreferences.edit { putBoolean("donationDialogShown", true) } - }, - hazeState = hazeStateS.value, - ) -} - -@Preview -@Composable -fun AirPodsSettingsScreenPreview() { - Column ( - modifier = Modifier.height(2000.dp) - ) { - LibrePodsTheme ( - darkTheme = true - ) { - AirPodsSettingsScreen(dev = null, service = AirPodsService(), navController = rememberNavController(), isConnected = true, isRemotelyConnected = false) - } - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/AppSettingsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/AppSettingsScreen.kt deleted file mode 100644 index 5dc271457..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/AppSettingsScreen.kt +++ /dev/null @@ -1,981 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.screens - -import android.content.Context -import android.widget.Toast -import androidx.activity.compose.BackHandler -import androidx.compose.foundation.background -import androidx.compose.foundation.clickable -import androidx.compose.foundation.interaction.MutableInteractionSource -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.width -import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.foundation.text.KeyboardOptions -import androidx.compose.foundation.verticalScroll -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Refresh -import androidx.compose.material3.AlertDialog -import androidx.compose.material3.Button -import androidx.compose.material3.ButtonDefaults -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.HorizontalDivider -import androidx.compose.material3.Icon -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.OutlinedTextField -import androidx.compose.material3.OutlinedTextFieldDefaults -import androidx.compose.material3.Text -import androidx.compose.material3.TextButton -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.mutableFloatStateOf -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberCoroutineScope -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.Font -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.input.KeyboardCapitalization -import androidx.compose.ui.text.input.KeyboardType -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.core.content.edit -import androidx.navigation.NavController -import com.kyant.backdrop.backdrops.layerBackdrop -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.hazeSource -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.launch -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.NavigationButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledSlider -import me.kavishdevar.librepods.composables.StyledToggle -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.RadareOffsetFinder -import kotlin.io.encoding.Base64 -import kotlin.io.encoding.ExperimentalEncodingApi -import kotlin.math.roundToInt - -@OptIn(ExperimentalMaterial3Api::class, ExperimentalHazeMaterialsApi::class, ExperimentalEncodingApi::class) -@Composable -fun AppSettingsScreen(navController: NavController) { - val sharedPreferences = LocalContext.current.getSharedPreferences("settings", Context.MODE_PRIVATE) - - val isDarkTheme = isSystemInDarkTheme() - val context = LocalContext.current - val coroutineScope = rememberCoroutineScope() - val scrollState = rememberScrollState() - - val showResetDialog = remember { mutableStateOf(false) } - val showIrkDialog = remember { mutableStateOf(false) } - val showEncKeyDialog = remember { mutableStateOf(false) } - val showCameraDialog = remember { mutableStateOf(false) } - val irkValue = remember { mutableStateOf("") } - val encKeyValue = remember { mutableStateOf("") } - val cameraPackageValue = remember { mutableStateOf("") } - val irkError = remember { mutableStateOf(null) } - val encKeyError = remember { mutableStateOf(null) } - val cameraPackageError = remember { mutableStateOf(null) } - - LaunchedEffect(Unit) { - val savedIrk = sharedPreferences.getString(AACPManager.Companion.ProximityKeyType.IRK.name, null) - val savedEncKey = sharedPreferences.getString(AACPManager.Companion.ProximityKeyType.ENC_KEY.name, null) - val savedCameraPackage = sharedPreferences.getString("custom_camera_package", null) - - if (savedIrk != null) { - try { - val decoded = Base64.decode(savedIrk) - irkValue.value = decoded.joinToString("") { "%02x".format(it) } - } catch (e: Exception) { - irkValue.value = "" - e.printStackTrace() - } - } - - if (savedEncKey != null) { - try { - val decoded = Base64.decode(savedEncKey) - encKeyValue.value = decoded.joinToString("") { "%02x".format(it) } - } catch (e: Exception) { - encKeyValue.value = "" - e.printStackTrace() - } - } - if (savedCameraPackage != null) { - cameraPackageValue.value = savedCameraPackage - } - } - - val showPhoneBatteryInWidget = remember { - mutableStateOf(sharedPreferences.getBoolean("show_phone_battery_in_widget", true)) - } - val conversationalAwarenessPauseMusicEnabled = remember { - mutableStateOf(sharedPreferences.getBoolean("conversational_awareness_pause_music", false)) - } - val relativeConversationalAwarenessVolumeEnabled = remember { - mutableStateOf(sharedPreferences.getBoolean("relative_conversational_awareness_volume", true)) - } - val openDialogForControlling = remember { - mutableStateOf(sharedPreferences.getString("qs_click_behavior", "dialog") == "dialog") - } - val disconnectWhenNotWearing = remember { - mutableStateOf(sharedPreferences.getBoolean("disconnect_when_not_wearing", false)) - } - - val takeoverWhenDisconnected = remember { - mutableStateOf(sharedPreferences.getBoolean("takeover_when_disconnected", true)) - } - val takeoverWhenIdle = remember { - mutableStateOf(sharedPreferences.getBoolean("takeover_when_idle", true)) - } - val takeoverWhenMusic = remember { - mutableStateOf(sharedPreferences.getBoolean("takeover_when_music", false)) - } - val takeoverWhenCall = remember { - mutableStateOf(sharedPreferences.getBoolean("takeover_when_call", true)) - } - - val takeoverWhenRingingCall = remember { - mutableStateOf(sharedPreferences.getBoolean("takeover_when_ringing_call", true)) - } - val takeoverWhenMediaStart = remember { - mutableStateOf(sharedPreferences.getBoolean("takeover_when_media_start", true)) - } - - val useAlternateHeadTrackingPackets = remember { - mutableStateOf(sharedPreferences.getBoolean("use_alternate_head_tracking_packets", false)) - } - - fun validateHexInput(input: String): Boolean { - val hexPattern = Regex("^[0-9a-fA-F]{32}$") - return hexPattern.matches(input) - } - - val isProcessingSdp = remember { mutableStateOf(false) } - val actAsAppleDevice = remember { mutableStateOf(false) } - - BackHandler(enabled = isProcessingSdp.value) {} - - val backdrop = rememberLayerBackdrop() - - StyledScaffold( - title = stringResource(R.string.app_settings) - ) { spacerHeight, hazeState -> - Column( - modifier = Modifier - .fillMaxSize() - .layerBackdrop(backdrop) - .hazeSource(state = hazeState) - .verticalScroll(scrollState) - .padding(horizontal = 16.dp) - ) { - Spacer(modifier = Modifier.height(spacerHeight)) - - val isDarkTheme = isSystemInDarkTheme() - val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - val textColor = if (isDarkTheme) Color.White else Color.Black - - StyledToggle( - title = stringResource(R.string.widget), - label = stringResource(R.string.show_phone_battery_in_widget), - description = stringResource(R.string.show_phone_battery_in_widget_description), - checkedState = showPhoneBatteryInWidget, - sharedPreferenceKey = "show_phone_battery_in_widget", - sharedPreferences = sharedPreferences, - ) - - Text( - text = stringResource(R.string.conversational_awareness), - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - modifier = Modifier.padding(16.dp, bottom = 2.dp, top = 24.dp) - ) - - Spacer(modifier = Modifier.height(2.dp)) - - Column ( - modifier = Modifier - .fillMaxWidth() - .background( - backgroundColor, - RoundedCornerShape(28.dp) - ) - .padding(vertical = 4.dp) - ) { - fun updateConversationalAwarenessPauseMusic(enabled: Boolean) { - conversationalAwarenessPauseMusicEnabled.value = enabled - sharedPreferences.edit { putBoolean("conversational_awareness_pause_music", enabled)} - } - - fun updateRelativeConversationalAwarenessVolume(enabled: Boolean) { - relativeConversationalAwarenessVolumeEnabled.value = enabled - sharedPreferences.edit { putBoolean("relative_conversational_awareness_volume", enabled)} - } - - StyledToggle( - label = stringResource(R.string.conversational_awareness_pause_music), - description = stringResource(R.string.conversational_awareness_pause_music_description), - checkedState = conversationalAwarenessPauseMusicEnabled, - onCheckedChange = { updateConversationalAwarenessPauseMusic(it) }, - independent = false - ) - - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(horizontal = 12.dp) - ) - - StyledToggle( - label = stringResource(R.string.relative_conversational_awareness_volume), - description = stringResource(R.string.relative_conversational_awareness_volume_description), - checkedState = relativeConversationalAwarenessVolumeEnabled, - onCheckedChange = { updateRelativeConversationalAwarenessVolume(it) }, - independent = false - ) - } - - Spacer(modifier = Modifier.height(16.dp)) - - val conversationalAwarenessVolume = remember { mutableFloatStateOf(sharedPreferences.getInt("conversational_awareness_volume", 43).toFloat()) } - LaunchedEffect(conversationalAwarenessVolume.floatValue) { - sharedPreferences.edit { putInt("conversational_awareness_volume", conversationalAwarenessVolume.floatValue.roundToInt()) } - } - - StyledSlider( - label = stringResource(R.string.conversational_awareness_volume), - mutableFloatState = conversationalAwarenessVolume, - valueRange = 10f..85f, - startLabel = "10%", - endLabel = "85%", - onValueChange = { newValue -> conversationalAwarenessVolume.floatValue = newValue }, - independent = true - ) - - Spacer(modifier = Modifier.height(16.dp)) - - NavigationButton( - to = "", - title = stringResource(R.string.camera_control), - name = stringResource(R.string.set_custom_camera_package), - navController = navController, - onClick = { showCameraDialog.value = true }, - independent = true, - description = stringResource(R.string.camera_control_app_description) - ) - - Spacer(modifier = Modifier.height(16.dp)) - - StyledToggle( - title = stringResource(R.string.quick_settings_tile), - label = stringResource(R.string.open_dialog_for_controlling), - description = stringResource(R.string.open_dialog_for_controlling_description), - checkedState = openDialogForControlling, - onCheckedChange = { - openDialogForControlling.value = it - sharedPreferences.edit { putString("qs_click_behavior", if (it) "dialog" else "activity") } - }, - ) - - Spacer(modifier = Modifier.height(16.dp)) - - StyledToggle( - title = stringResource(R.string.ear_detection), - label = stringResource(R.string.disconnect_when_not_wearing), - description = stringResource(R.string.disconnect_when_not_wearing_description), - checkedState = disconnectWhenNotWearing, - sharedPreferenceKey = "disconnect_when_not_wearing", - sharedPreferences = sharedPreferences, - ) - - Text( - text = stringResource(R.string.takeover_airpods_state), - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - modifier = Modifier.padding(16.dp, bottom = 2.dp, top = 24.dp) - ) - - Spacer(modifier = Modifier.height(4.dp)) - - Column( - modifier = Modifier - .fillMaxWidth() - .background( - backgroundColor, - RoundedCornerShape(28.dp) - ) - .padding(vertical = 4.dp) - ) { - StyledToggle( - label = stringResource(R.string.takeover_disconnected), - description = stringResource(R.string.takeover_disconnected_desc), - checkedState = takeoverWhenDisconnected, - onCheckedChange = { - takeoverWhenDisconnected.value = it - sharedPreferences.edit { putBoolean("takeover_when_disconnected", it)} - }, - independent = false - ) - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(horizontal = 12.dp) - ) - - StyledToggle( - label = stringResource(R.string.takeover_idle), - description = stringResource(R.string.takeover_idle_desc), - checkedState = takeoverWhenIdle, - onCheckedChange = { - takeoverWhenIdle.value = it - sharedPreferences.edit { putBoolean("takeover_when_idle", it)} - }, - independent = false - ) - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(horizontal = 12.dp) - ) - - StyledToggle( - label = stringResource(R.string.takeover_music), - description = stringResource(R.string.takeover_music_desc), - checkedState = takeoverWhenMusic, - onCheckedChange = { - takeoverWhenMusic.value = it - sharedPreferences.edit { putBoolean("takeover_when_music", it)} - }, - independent = false - ) - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(horizontal = 12.dp) - ) - - StyledToggle( - label = stringResource(R.string.takeover_call), - description = stringResource(R.string.takeover_call_desc), - checkedState = takeoverWhenCall, - onCheckedChange = { - takeoverWhenCall.value = it - sharedPreferences.edit { putBoolean("takeover_when_call", it)} - }, - independent = false - ) - } - - Spacer(modifier = Modifier.height(16.dp)) - - Text( - text = stringResource(R.string.takeover_phone_state), - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - modifier = Modifier.padding(horizontal = 16.dp) - ) - Spacer(modifier = Modifier.height(4.dp)) - Column( - modifier = Modifier - .fillMaxWidth() - .background( - backgroundColor, - RoundedCornerShape(28.dp) - ) - .padding(vertical = 4.dp) - ){ - StyledToggle( - label = stringResource(R.string.takeover_ringing_call), - description = stringResource(R.string.takeover_ringing_call_desc), - checkedState = takeoverWhenRingingCall, - onCheckedChange = { - takeoverWhenRingingCall.value = it - sharedPreferences.edit { putBoolean("takeover_when_ringing_call", it)} - }, - independent = false - ) - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(horizontal = 12.dp) - ) - - StyledToggle( - label = stringResource(R.string.takeover_media_start), - description = stringResource(R.string.takeover_media_start_desc), - checkedState = takeoverWhenMediaStart, - onCheckedChange = { - takeoverWhenMediaStart.value = it - sharedPreferences.edit { putBoolean("takeover_when_media_start", it)} - }, - independent = false - ) - } - - Text( - text = stringResource(R.string.advanced_options), - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - modifier = Modifier.padding(16.dp, bottom = 2.dp, top = 24.dp) - ) - - Spacer(modifier = Modifier.height(2.dp)) - - Column( - modifier = Modifier - .fillMaxWidth() - .background( - backgroundColor, - RoundedCornerShape(28.dp) - ) - .padding(horizontal = 16.dp, vertical = 4.dp) - ) { - Row( - modifier = Modifier - .fillMaxWidth() - .clickable ( - onClick = { showIrkDialog.value = true }, - indication = null, - interactionSource = remember { MutableInteractionSource() } - ), - verticalAlignment = Alignment.CenterVertically - ) { - Column( - modifier = Modifier - .weight(1f) - .padding(vertical = 8.dp) - .padding(end = 4.dp) - ) { - Text( - text = stringResource(R.string.set_identity_resolving_key), - fontSize = 16.sp, - color = textColor - ) - Spacer(modifier = Modifier.height(4.dp)) - Text( - text = stringResource(R.string.set_identity_resolving_key_description), - fontSize = 14.sp, - color = textColor.copy(0.6f), - lineHeight = 16.sp, - ) - } - } - - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - ) - - Row( - modifier = Modifier - .fillMaxWidth() - .clickable ( - onClick = { showEncKeyDialog.value = true }, - indication = null, - interactionSource = remember { MutableInteractionSource() } - ), - verticalAlignment = Alignment.CenterVertically - ) { - Column( - modifier = Modifier - .weight(1f) - .padding(vertical = 8.dp) - .padding(end = 4.dp) - ) { - Text( - text = stringResource(R.string.set_encryption_key), - fontSize = 16.sp, - color = textColor - ) - Spacer(modifier = Modifier.height(4.dp)) - Text( - text = stringResource(R.string.set_encryption_key_description), - fontSize = 14.sp, - color = textColor.copy(0.6f), - lineHeight = 16.sp, - ) - } - } - } - - Spacer(modifier = Modifier.height(16.dp)) - - StyledToggle( - label = stringResource(R.string.use_alternate_head_tracking_packets), - description = stringResource(R.string.use_alternate_head_tracking_packets_description), - checkedState = useAlternateHeadTrackingPackets, - onCheckedChange = { - useAlternateHeadTrackingPackets.value = it - sharedPreferences.edit { putBoolean("use_alternate_head_tracking_packets", it)} - }, - independent = true - ) - - Spacer(modifier = Modifier.height(16.dp)) - - NavigationButton( - to = "troubleshooting", - name = stringResource(R.string.troubleshooting), - navController = navController, - independent = true, - description = stringResource(R.string.troubleshooting_description) - ) - - LaunchedEffect(Unit) { - actAsAppleDevice.value = RadareOffsetFinder.isSdpOffsetAvailable() - } - val restartBluetoothText = stringResource(R.string.found_offset_restart_bluetooth) - - StyledToggle( - label = stringResource(R.string.act_as_an_apple_device), - description = stringResource(R.string.act_as_an_apple_device_description), - checkedState = actAsAppleDevice, - onCheckedChange = { - actAsAppleDevice.value = it - isProcessingSdp.value = true - coroutineScope.launch { - if (it) { - val radareOffsetFinder = RadareOffsetFinder(context) - val success = radareOffsetFinder.findSdpOffset() - if (success) { - Toast.makeText(context, restartBluetoothText, Toast.LENGTH_LONG).show() - } - } else { - RadareOffsetFinder.clearSdpOffset() - } - isProcessingSdp.value = false - } - }, - independent = true, - enabled = !isProcessingSdp.value - ) - - Spacer(modifier = Modifier.height(16.dp)) - - Button( - onClick = { showResetDialog.value = true }, - modifier = Modifier - .fillMaxWidth() - .height(50.dp), - colors = ButtonDefaults.buttonColors( - containerColor = MaterialTheme.colorScheme.errorContainer - ), - shape = RoundedCornerShape(28.dp) - ) { - Row( - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.Center - ) { - Icon( - imageVector = Icons.Default.Refresh, - contentDescription = "Reset", - tint = MaterialTheme.colorScheme.onErrorContainer, - modifier = Modifier.size(18.dp) - ) - Spacer(modifier = Modifier.width(8.dp)) - Text( - text = stringResource(R.string.reset_hook_offset), - color = MaterialTheme.colorScheme.onErrorContainer, - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - ) - } - } - - Spacer(modifier = Modifier.height(16.dp)) - - NavigationButton( - to = "open_source_licenses", - name = stringResource(R.string.open_source_licenses), - navController = navController, - independent = true - ) - - Spacer(modifier = Modifier.height(32.dp)) - - if (showResetDialog.value) { - AlertDialog( - onDismissRequest = { showResetDialog.value = false }, - title = { - Text( - "Reset Hook Offset", - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - }, - text = { - Text( - stringResource(R.string.reset_hook_offset_description), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - }, - confirmButton = { - val successText = stringResource(R.string.hook_offset_reset_success) - val failureText = stringResource(R.string.hook_offset_reset_failure) - TextButton( - onClick = { - if (RadareOffsetFinder.clearHookOffsets()) { - Toast.makeText( - context, - successText, - Toast.LENGTH_LONG - ).show() - - navController.navigate("onboarding") { - popUpTo("settings") { inclusive = true } - } - } else { - Toast.makeText( - context, - failureText, - Toast.LENGTH_SHORT - ).show() - } - showResetDialog.value = false - }, - colors = ButtonDefaults.textButtonColors( - contentColor = MaterialTheme.colorScheme.error - ) - ) { - Text( - stringResource(R.string.reset), - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - } - }, - dismissButton = { - TextButton( - onClick = { showResetDialog.value = false } - ) { - Text( - "Cancel", - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - } - } - ) - } - - if (showIrkDialog.value) { - AlertDialog( - onDismissRequest = { showIrkDialog.value = false }, - title = { - Text( - stringResource(R.string.set_identity_resolving_key), - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - }, - text = { - Column { - Text( - stringResource(R.string.enter_irk_hex), - fontFamily = FontFamily(Font(R.font.sf_pro)), - modifier = Modifier.padding(bottom = 8.dp) - ) - - OutlinedTextField( - value = irkValue.value, - onValueChange = { - irkValue.value = it.lowercase().filter { char -> char.isDigit() || char in 'a'..'f' } - irkError.value = null - }, - modifier = Modifier.fillMaxWidth(), - isError = irkError.value != null, - keyboardOptions = KeyboardOptions( - keyboardType = KeyboardType.Ascii, - capitalization = KeyboardCapitalization.None - ), - colors = OutlinedTextFieldDefaults.colors( - focusedBorderColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5), - unfocusedBorderColor = if (isDarkTheme) Color.Gray else Color.LightGray - ), - supportingText = { - if (irkError.value != null) { - Text(stringResource(R.string.must_be_32_hex_chars), color = MaterialTheme.colorScheme.error) - } - }, - label = { Text(stringResource(R.string.irk_hex_value)) } - ) - } - }, - confirmButton = { - val successText = stringResource(R.string.irk_set_success) - val errorText = stringResource(R.string.error_converting_hex) - TextButton( - onClick = { - if (!validateHexInput(irkValue.value)) { - irkError.value = "Must be exactly 32 hex characters" - return@TextButton - } - - try { - val hexBytes = ByteArray(16) - for (i in 0 until 16) { - val hexByte = irkValue.value.substring(i * 2, i * 2 + 2) - hexBytes[i] = hexByte.toInt(16).toByte() - } - - val base64Value = Base64.encode(hexBytes) - sharedPreferences.edit { putString(AACPManager.Companion.ProximityKeyType.IRK.name, base64Value)} - - Toast.makeText(context, successText, Toast.LENGTH_SHORT).show() - showIrkDialog.value = false - } catch (e: Exception) { - irkError.value = errorText + " " + (e.message ?: "Unknown error") - } - } - ) { - Text( - "Save", - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - } - }, - dismissButton = { - TextButton( - onClick = { showIrkDialog.value = false } - ) { - Text( - "Cancel", - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - } - } - ) - } - - if (showEncKeyDialog.value) { - AlertDialog( - onDismissRequest = { showEncKeyDialog.value = false }, - title = { - Text( - stringResource(R.string.set_encryption_key), - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - }, - text = { - Column { - Text( - stringResource(R.string.enter_enc_key_hex), - fontFamily = FontFamily(Font(R.font.sf_pro)), - modifier = Modifier.padding(bottom = 8.dp) - ) - - OutlinedTextField( - value = encKeyValue.value, - onValueChange = { - encKeyValue.value = it.lowercase().filter { char -> char.isDigit() || char in 'a'..'f' } - encKeyError.value = null - }, - modifier = Modifier.fillMaxWidth(), - isError = encKeyError.value != null, - keyboardOptions = KeyboardOptions( - keyboardType = KeyboardType.Ascii, - capitalization = KeyboardCapitalization.None - ), - colors = OutlinedTextFieldDefaults.colors( - focusedBorderColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5), - unfocusedBorderColor = if (isDarkTheme) Color.Gray else Color.LightGray - ), - supportingText = { - if (encKeyError.value != null) { - Text(stringResource(R.string.must_be_32_hex_chars), color = MaterialTheme.colorScheme.error) - } - }, - label = { Text(stringResource(R.string.enc_key_hex_value)) } - ) - } - }, - confirmButton = { - val successText = stringResource(R.string.encryption_key_set_success) - val errorText = stringResource(R.string.error_converting_hex) - TextButton( - onClick = { - if (!validateHexInput(encKeyValue.value)) { - encKeyError.value = "Must be exactly 32 hex characters" - return@TextButton - } - - try { - val hexBytes = ByteArray(16) - for (i in 0 until 16) { - val hexByte = encKeyValue.value.substring(i * 2, i * 2 + 2) - hexBytes[i] = hexByte.toInt(16).toByte() - } - - val base64Value = Base64.encode(hexBytes) - sharedPreferences.edit { putString(AACPManager.Companion.ProximityKeyType.ENC_KEY.name, base64Value)} - - Toast.makeText(context, successText, Toast.LENGTH_SHORT).show() - showEncKeyDialog.value = false - } catch (e: Exception) { - encKeyError.value = errorText + " " + (e.message ?: "Unknown error") - } - } - ) { - Text( - "Save", - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - } - }, - dismissButton = { - TextButton( - onClick = { showEncKeyDialog.value = false } - ) { - Text( - "Cancel", - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - } - } - ) - } - - if (showCameraDialog.value) { - AlertDialog( - onDismissRequest = { showCameraDialog.value = false }, - title = { - Text( - stringResource(R.string.set_custom_camera_package), - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - }, - text = { - Column { - Text( - stringResource(R.string.enter_custom_camera_package), - fontFamily = FontFamily(Font(R.font.sf_pro)), - modifier = Modifier.padding(bottom = 8.dp) - ) - - OutlinedTextField( - value = cameraPackageValue.value, - onValueChange = { - cameraPackageValue.value = it - cameraPackageError.value = null - }, - modifier = Modifier.fillMaxWidth(), - isError = cameraPackageError.value != null, - keyboardOptions = KeyboardOptions( - keyboardType = KeyboardType.Ascii, - capitalization = KeyboardCapitalization.None - ), - colors = OutlinedTextFieldDefaults.colors( - focusedBorderColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5), - unfocusedBorderColor = if (isDarkTheme) Color.Gray else Color.LightGray - ), - supportingText = { - if (cameraPackageError.value != null) { - Text(cameraPackageError.value!!, color = MaterialTheme.colorScheme.error) - } - }, - label = { Text(stringResource(R.string.custom_camera_package)) } - ) - } - }, - confirmButton = { - val successText = stringResource(R.string.custom_camera_package_set_success) - TextButton( - onClick = { - if (cameraPackageValue.value.isBlank()) { - sharedPreferences.edit { remove("custom_camera_package") } - Toast.makeText(context, successText, Toast.LENGTH_SHORT).show() - showCameraDialog.value = false - return@TextButton - } - - sharedPreferences.edit { putString("custom_camera_package", cameraPackageValue.value) } - Toast.makeText(context, successText, Toast.LENGTH_SHORT).show() - showCameraDialog.value = false - } - ) { - Text( - "Save", - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - } - }, - dismissButton = { - TextButton( - onClick = { showCameraDialog.value = false } - ) { - Text( - "Cancel", - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontWeight = FontWeight.Medium - ) - } - } - ) - } - } - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/CameraControlScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/CameraControlScreen.kt deleted file mode 100644 index 8f5c5295b..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/CameraControlScreen.kt +++ /dev/null @@ -1,146 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.screens - -import android.annotation.SuppressLint -import android.content.ComponentName -import android.content.Context -import android.content.Intent -import android.provider.Settings -import android.view.accessibility.AccessibilityManager -import android.accessibilityservice.AccessibilityServiceInfo -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.mutableFloatStateOf -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.unit.dp -import androidx.navigation.NavController -import androidx.core.content.edit -import com.kyant.backdrop.backdrops.layerBackdrop -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.SelectItem -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledSelectList -import me.kavishdevar.librepods.composables.StyledSlider -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.services.AppListenerService -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.AACPManager.Companion.StemPressType -import kotlin.io.encoding.ExperimentalEncodingApi - -private var debounceJob: Job? = null - -@SuppressLint("DefaultLocale") -@ExperimentalHazeMaterialsApi -@OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) -@Composable -fun CameraControlScreen(navController: NavController) { - val isDarkTheme = isSystemInDarkTheme() - val context = LocalContext.current - val sharedPreferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE) - - val service = ServiceManager.getService()!! - var currentCameraAction by remember { - mutableStateOf( - sharedPreferences.getString("camera_action", null)?.let { StemPressType.valueOf(it) } - ) - } - - fun isAppListenerServiceEnabled(context: Context): Boolean { - val am = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager - val enabledServices = am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK) - val serviceComponent = ComponentName(context, AppListenerService::class.java) - return enabledServices.any { it.resolveInfo.serviceInfo.packageName == serviceComponent.packageName && it.resolveInfo.serviceInfo.name == serviceComponent.className } - } - - val cameraOptions = listOf( - SelectItem( - name = stringResource(R.string.off), - selected = currentCameraAction == null, - onClick = { - sharedPreferences.edit { remove("camera_action") } - currentCameraAction = null - } - ), - SelectItem( - name = stringResource(R.string.press_once), - selected = currentCameraAction == StemPressType.SINGLE_PRESS, - onClick = { - if (!isAppListenerServiceEnabled(context)) { - context.startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)) - } else { - sharedPreferences.edit { putString("camera_action", StemPressType.SINGLE_PRESS.name) } - currentCameraAction = StemPressType.SINGLE_PRESS - } - } - ), - SelectItem( - name = stringResource(R.string.press_and_hold_airpods), - selected = currentCameraAction == StemPressType.LONG_PRESS, - onClick = { - if (!isAppListenerServiceEnabled(context)) { - context.startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)) - } else { - sharedPreferences.edit { putString("camera_action", StemPressType.LONG_PRESS.name) } - currentCameraAction = StemPressType.LONG_PRESS - } - } - ) - ) - - val backdrop = rememberLayerBackdrop() - - StyledScaffold( - title = stringResource(R.string.camera_control) - ) { spacerHeight -> - Column( - modifier = Modifier - .fillMaxSize() - .layerBackdrop(backdrop) - .padding(horizontal = 16.dp), - verticalArrangement = Arrangement.spacedBy(16.dp) - ) { - Spacer(modifier = Modifier.height(spacerHeight)) - StyledSelectList(items = cameraOptions) - } - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/HearingAidAdjustmentsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/HearingAidAdjustmentsScreen.kt deleted file mode 100644 index 34cb87577..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/HearingAidAdjustmentsScreen.kt +++ /dev/null @@ -1,341 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.screens - -import android.annotation.SuppressLint -import android.util.Log -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.verticalScroll -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.MutableState -import androidx.compose.runtime.mutableFloatStateOf -import androidx.compose.runtime.mutableIntStateOf -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.ui.Modifier -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.unit.dp -import androidx.navigation.NavController -import com.kyant.backdrop.backdrops.layerBackdrop -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.HazeState -import dev.chrisbanes.haze.hazeSource -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.Job -import kotlinx.coroutines.delay -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledSlider -import me.kavishdevar.librepods.composables.StyledToggle -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.ATTHandles -import me.kavishdevar.librepods.utils.HearingAidSettings -import me.kavishdevar.librepods.utils.parseHearingAidSettingsResponse -import me.kavishdevar.librepods.utils.sendHearingAidSettings -import java.io.IOException -import kotlin.io.encoding.ExperimentalEncodingApi - -private var debounceJob: MutableState = mutableStateOf(null) -private const val TAG = "HearingAidAdjustments" - -@SuppressLint("DefaultLocale") -@ExperimentalHazeMaterialsApi -@OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) -@Composable -fun HearingAidAdjustmentsScreen(@Suppress("unused") navController: NavController) { - isSystemInDarkTheme() - val verticalScrollState = rememberScrollState() - val hazeState = remember { HazeState() } - val attManager = ServiceManager.getService()?.attManager ?: throw IllegalStateException("ATTManager not available") - - val aacpManager = remember { ServiceManager.getService()?.aacpManager } - val backdrop = rememberLayerBackdrop() - StyledScaffold( - title = stringResource(R.string.adjustments) - ) { spacerHeight -> - Column( - modifier = Modifier - .hazeSource(hazeState) - .fillMaxSize() - .layerBackdrop(backdrop) - .verticalScroll(verticalScrollState) - .padding(horizontal = 16.dp), - verticalArrangement = Arrangement.spacedBy(16.dp) - ) { - Spacer(modifier = Modifier.height(spacerHeight)) - - val amplificationSliderValue = remember { mutableFloatStateOf(0.5f) } - val balanceSliderValue = remember { mutableFloatStateOf(0.5f) } - val toneSliderValue = remember { mutableFloatStateOf(0.5f) } - val ambientNoiseReductionSliderValue = remember { mutableFloatStateOf(0.0f) } - val conversationBoostEnabled = remember { mutableStateOf(false) } - val eq = remember { mutableStateOf(FloatArray(8)) } - val ownVoiceAmplification = remember { mutableFloatStateOf(0.5f) } - - val phoneMediaEQ = remember { mutableStateOf(FloatArray(8) { 0.5f }) } - val phoneEQEnabled = remember { mutableStateOf(false) } - val mediaEQEnabled = remember { mutableStateOf(false) } - - val initialLoadComplete = remember { mutableStateOf(false) } - - val initialReadSucceeded = remember { mutableStateOf(false) } - val initialReadAttempts = remember { mutableIntStateOf(0) } - - val hearingAidSettings = remember { - mutableStateOf( - HearingAidSettings( - leftEQ = eq.value, - rightEQ = eq.value, - leftAmplification = amplificationSliderValue.floatValue + (0.5f - balanceSliderValue.floatValue) * amplificationSliderValue.floatValue * 2, - rightAmplification = amplificationSliderValue.floatValue + (balanceSliderValue.floatValue - 0.5f) * amplificationSliderValue.floatValue * 2, - leftTone = toneSliderValue.floatValue, - rightTone = toneSliderValue.floatValue, - leftConversationBoost = conversationBoostEnabled.value, - rightConversationBoost = conversationBoostEnabled.value, - leftAmbientNoiseReduction = ambientNoiseReductionSliderValue.floatValue, - rightAmbientNoiseReduction = ambientNoiseReductionSliderValue.floatValue, - netAmplification = amplificationSliderValue.floatValue, - balance = balanceSliderValue.floatValue, - ownVoiceAmplification = ownVoiceAmplification.floatValue - ) - ) - } - - val hearingAidEnabled = remember { - val aidStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID } - val assistStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG } - mutableStateOf((aidStatus?.value?.getOrNull(1) == 0x01.toByte()) && (assistStatus?.value?.getOrNull(0) == 0x01.toByte())) - } - - val hearingAidListener = remember { - object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID.value || - controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG.value) { - val aidStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID } - val assistStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG } - hearingAidEnabled.value = (aidStatus?.value?.getOrNull(1) == 0x01.toByte()) && (assistStatus?.value?.getOrNull(0) == 0x01.toByte()) - } - } - } - } - - val hearingAidATTListener = remember { - object : (ByteArray) -> Unit { - override fun invoke(value: ByteArray) { - val parsed = parseHearingAidSettingsResponse(value) - if (parsed != null) { - amplificationSliderValue.floatValue = parsed.netAmplification - balanceSliderValue.floatValue = parsed.balance - toneSliderValue.floatValue = parsed.leftTone - ambientNoiseReductionSliderValue.floatValue = parsed.leftAmbientNoiseReduction - conversationBoostEnabled.value = parsed.leftConversationBoost - eq.value = parsed.leftEQ.copyOf() - ownVoiceAmplification.floatValue = parsed.ownVoiceAmplification - Log.d(TAG, "Updated hearing aid settings from notification") - } else { - Log.w(TAG, "Failed to parse hearing aid settings from notification") - } - } - } - } - - LaunchedEffect(Unit) { - aacpManager?.registerControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, hearingAidListener) - aacpManager?.registerControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, hearingAidListener) - } - - DisposableEffect(Unit) { - onDispose { - aacpManager?.unregisterControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, hearingAidListener) - aacpManager?.unregisterControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, hearingAidListener) - attManager.unregisterListener(ATTHandles.HEARING_AID, hearingAidATTListener) - } - } - - LaunchedEffect(amplificationSliderValue.floatValue, balanceSliderValue.floatValue, toneSliderValue.floatValue, conversationBoostEnabled.value, ambientNoiseReductionSliderValue.floatValue, ownVoiceAmplification.floatValue, initialLoadComplete.value, initialReadSucceeded.value) { - if (!initialLoadComplete.value) { - Log.d(TAG, "Initial device load not complete - skipping send") - return@LaunchedEffect - } - - if (!initialReadSucceeded.value) { - Log.d(TAG, "Initial device read not successful yet - skipping send until read succeeds") - return@LaunchedEffect - } - - hearingAidSettings.value = HearingAidSettings( - leftEQ = eq.value, - rightEQ = eq.value, - leftAmplification = amplificationSliderValue.floatValue + if (balanceSliderValue.floatValue < 0) -balanceSliderValue.floatValue else 0f, - rightAmplification = amplificationSliderValue.floatValue + if (balanceSliderValue.floatValue > 0) balanceSliderValue.floatValue else 0f, - leftTone = toneSliderValue.floatValue, - rightTone = toneSliderValue.floatValue, - leftConversationBoost = conversationBoostEnabled.value, - rightConversationBoost = conversationBoostEnabled.value, - leftAmbientNoiseReduction = ambientNoiseReductionSliderValue.floatValue, - rightAmbientNoiseReduction = ambientNoiseReductionSliderValue.floatValue, - netAmplification = amplificationSliderValue.floatValue, - balance = balanceSliderValue.floatValue, - ownVoiceAmplification = ownVoiceAmplification.floatValue - ) - Log.d(TAG, "Updated settings: ${hearingAidSettings.value}") - sendHearingAidSettings(attManager, hearingAidSettings.value, debounceJob) - } - - LaunchedEffect(Unit) { - Log.d(TAG, "Connecting to ATT...") - try { - attManager.enableNotifications(ATTHandles.HEARING_AID) - attManager.registerListener(ATTHandles.HEARING_AID, hearingAidATTListener) - - try { - if (aacpManager != null) { - Log.d(TAG, "Found AACPManager, reading cached EQ data") - val aacpEQ = aacpManager.eqData - if (aacpEQ.isNotEmpty()) { - eq.value = aacpEQ.copyOf() - phoneMediaEQ.value = aacpEQ.copyOf() - phoneEQEnabled.value = aacpManager.eqOnPhone - mediaEQEnabled.value = aacpManager.eqOnMedia - Log.d(TAG, "Populated EQ from AACPManager: ${aacpEQ.toList()}") - } else { - Log.d(TAG, "AACPManager EQ data empty") - } - } else { - Log.d(TAG, "No AACPManager available") - } - } catch (e: Exception) { - Log.w(TAG, "Error reading EQ from AACPManager: ${e.message}") - } - - var parsedSettings: HearingAidSettings? = null - for (attempt in 1..3) { - initialReadAttempts.intValue = attempt - try { - val data = attManager.read(ATTHandles.HEARING_AID) - parsedSettings = parseHearingAidSettingsResponse(data = data) - if (parsedSettings != null) { - Log.d(TAG, "Parsed settings on attempt $attempt") - break - } else { - Log.d(TAG, "Parsing returned null on attempt $attempt") - } - } catch (e: Exception) { - Log.w(TAG, "Read attempt $attempt failed: ${e.message}") - } - delay(200) - } - - if (parsedSettings != null) { - Log.d(TAG, "Initial hearing aid settings: $parsedSettings") - amplificationSliderValue.floatValue = parsedSettings.netAmplification - balanceSliderValue.floatValue = parsedSettings.balance - toneSliderValue.floatValue = parsedSettings.leftTone - ambientNoiseReductionSliderValue.floatValue = parsedSettings.leftAmbientNoiseReduction - conversationBoostEnabled.value = parsedSettings.leftConversationBoost - eq.value = parsedSettings.leftEQ.copyOf() - ownVoiceAmplification.floatValue = parsedSettings.ownVoiceAmplification - initialReadSucceeded.value = true - } else { - Log.d(TAG, "Failed to read/parse initial hearing aid settings after ${initialReadAttempts.intValue} attempts") - } - } catch (e: IOException) { - e.printStackTrace() - } finally { - initialLoadComplete.value = true - } - } - - StyledSlider( - label = stringResource(R.string.amplification), - valueRange = -1f..1f, - mutableFloatState = amplificationSliderValue, - onValueChange = { - amplificationSliderValue.floatValue = it - }, - startIcon = "􀊥", - endIcon = "􀊩", - independent = true, - ) - - - StyledToggle( - label = stringResource(R.string.swipe_to_control_amplification), - controlCommandIdentifier = AACPManager.Companion.ControlCommandIdentifiers.HPS_GAIN_SWIPE, - description = stringResource(R.string.swipe_amplification_description) - ) - - StyledSlider( - label = stringResource(R.string.balance), - valueRange = -1f..1f, - mutableFloatState = balanceSliderValue, - onValueChange = { - balanceSliderValue.floatValue = it - }, - snapPoints = listOf(-1f, 0f, 1f), - startLabel = stringResource(R.string.left), - endLabel = stringResource(R.string.right), - independent = true, - ) - - StyledSlider( - label = stringResource(R.string.tone), - valueRange = -1f..1f, - mutableFloatState = toneSliderValue, - onValueChange = { - toneSliderValue.floatValue = it - }, - startLabel = stringResource(R.string.darker), - endLabel = stringResource(R.string.brighter), - independent = true, - ) - - StyledSlider( - label = stringResource(R.string.ambient_noise_reduction), - valueRange = 0f..1f, - mutableFloatState = ambientNoiseReductionSliderValue, - onValueChange = { - ambientNoiseReductionSliderValue.floatValue = it - }, - startLabel = stringResource(R.string.less), - endLabel = stringResource(R.string.more), - independent = true, - ) - - StyledToggle( - label = stringResource(R.string.conversation_boost), - checkedState = conversationBoostEnabled, - independent = true, - description = stringResource(R.string.conversation_boost_description) - ) - } - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/HearingProtectionScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/HearingProtectionScreen.kt deleted file mode 100644 index 432f38259..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/HearingProtectionScreen.kt +++ /dev/null @@ -1,90 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.screens - -import android.annotation.SuppressLint -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.unit.dp -import androidx.navigation.NavController -import com.kyant.backdrop.backdrops.layerBackdrop -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.Job -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledToggle -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.ATTHandles -import kotlin.io.encoding.ExperimentalEncodingApi - -private var debounceJob: Job? = null - -@SuppressLint("DefaultLocale") -@ExperimentalHazeMaterialsApi -@OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) -@Composable -fun HearingProtectionScreen(navController: NavController) { - val isDarkTheme = isSystemInDarkTheme() - val service = ServiceManager.getService() - if (service == null) return - - val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - val textColor = if (isDarkTheme) Color.White else Color.Black - - val backdrop = rememberLayerBackdrop() - - StyledScaffold( - title = stringResource(R.string.hearing_protection), - ) { spacerHeight -> - Column( - modifier = Modifier - .fillMaxSize() - .layerBackdrop(backdrop) - .padding(horizontal = 16.dp) - ) { - Spacer(modifier = Modifier.height(spacerHeight)) - - StyledToggle( - title = stringResource(R.string.environmental_noise), - label = stringResource(R.string.loud_sound_reduction), - description = stringResource(R.string.loud_sound_reduction_description), - attHandle = ATTHandles.LOUD_SOUND_REDUCTION - ) - - Spacer(modifier = Modifier.height(12.dp)) - StyledToggle( - title = stringResource(R.string.workspace_use), - label = stringResource(R.string.ppe), - description = stringResource(R.string.workspace_use_description), - controlCommandIdentifier = AACPManager.Companion.ControlCommandIdentifiers.PPE_TOGGLE_CONFIG - ) - } - } -} \ No newline at end of file diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/Onboarding.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/Onboarding.kt deleted file mode 100644 index b8365ce97..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/Onboarding.kt +++ /dev/null @@ -1,643 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.screens - -import android.content.Context -import android.util.Log -import androidx.compose.animation.AnimatedContent -import androidx.compose.animation.core.animateFloatAsState -import androidx.compose.animation.fadeIn -import androidx.compose.animation.fadeOut -import androidx.compose.animation.togetherWith -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Check -import androidx.compose.material.icons.filled.Clear -import androidx.compose.material.icons.filled.Settings -import androidx.compose.material3.AlertDialog -import androidx.compose.material3.Button -import androidx.compose.material3.ButtonDefaults -import androidx.compose.material3.Card -import androidx.compose.material3.CardDefaults -import androidx.compose.material3.CircularProgressIndicator -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.Icon -import androidx.compose.material3.LinearProgressIndicator -import androidx.compose.material3.Text -import androidx.compose.material3.TextButton -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.collectAsState -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.StrokeCap -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.Font -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.core.content.edit -import androidx.navigation.NavController -import com.kyant.backdrop.backdrops.layerBackdrop -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.utils.RadareOffsetFinder - -@ExperimentalHazeMaterialsApi -@OptIn(ExperimentalMaterial3Api::class) -@Composable -fun Onboarding(navController: NavController, activityContext: Context) { - val isDarkTheme = isSystemInDarkTheme() - val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color.White - val textColor = if (isDarkTheme) Color.White else Color.Black - val accentColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5) - - val radareOffsetFinder = remember { RadareOffsetFinder(activityContext) } - val progressState by radareOffsetFinder.progressState.collectAsState() - var isComplete by remember { mutableStateOf(false) } - var hasStarted by remember { mutableStateOf(false) } - var rootCheckPassed by remember { mutableStateOf(false) } - var checkingRoot by remember { mutableStateOf(false) } - var rootCheckFailed by remember { mutableStateOf(false) } - var moduleEnabled by remember { mutableStateOf(false) } - var bluetoothToggled by remember { mutableStateOf(false) } - - var showSkipDialog by remember { mutableStateOf(false) } - - fun checkRootAccess() { - checkingRoot = true - rootCheckFailed = false - kotlinx.coroutines.MainScope().launch { - withContext(Dispatchers.IO) { - try { - val process = Runtime.getRuntime().exec("su -c id") - val exitValue = process.waitFor() // no idea why i have this, probably don't need to do this - withContext(Dispatchers.Main) { - rootCheckPassed = (exitValue == 0) - rootCheckFailed = (exitValue != 0) - checkingRoot = false - } - } catch (e: Exception) { - Log.e("Onboarding", "Root check failed", e) - withContext(Dispatchers.Main) { - rootCheckPassed = false - rootCheckFailed = true - checkingRoot = false - } - } - } - } - } - - LaunchedEffect(hasStarted) { - if (hasStarted && rootCheckPassed) { - Log.d("Onboarding", "Checking if hook offset is available...") - val isHookReady = radareOffsetFinder.isHookOffsetAvailable() - Log.d("Onboarding", "Hook offset ready: $isHookReady") - - if (isHookReady) { - Log.d("Onboarding", "Hook is ready") - isComplete = true - } else { - Log.d("Onboarding", "Hook not ready, starting setup process...") - withContext(Dispatchers.IO) { - radareOffsetFinder.setupAndFindOffset() - } - } - } - } - - LaunchedEffect(progressState) { - if (progressState is RadareOffsetFinder.ProgressState.Success) { - isComplete = true - } - } - val backdrop = rememberLayerBackdrop() - StyledScaffold( - title = "Setting Up", - actionButtons = listOf( - {scaffoldBackdrop -> - StyledIconButton( - onClick = { - showSkipDialog = true - }, - icon = "􀊋", - darkMode = isDarkTheme, - backdrop = scaffoldBackdrop - ) - } - ) - ) { spacerHeight -> - Column( - modifier = Modifier - .fillMaxSize() - .layerBackdrop(backdrop) - .padding(horizontal = 16.dp), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.spacedBy(16.dp) - ) { - Spacer(modifier = Modifier.height(spacerHeight)) - - Card( - modifier = Modifier.fillMaxWidth(), - colors = CardDefaults.cardColors(containerColor = backgroundColor), - shape = RoundedCornerShape(12.dp) - ) { - Column( - modifier = Modifier - .fillMaxWidth() - .padding(24.dp), - horizontalAlignment = Alignment.CenterHorizontally - ) { - if (!rootCheckPassed && !hasStarted) { - Icon( - imageVector = Icons.Default.Settings, - contentDescription = "Root Access", - tint = accentColor, - modifier = Modifier.size(50.dp) - ) - - Spacer(modifier = Modifier.height(24.dp)) - - Text( - text = stringResource(R.string.root_access_required), - style = TextStyle( - fontSize = 22.sp, - fontWeight = FontWeight.Bold, - textAlign = TextAlign.Center, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = textColor - ) - ) - - Spacer(modifier = Modifier.height(8.dp)) - - Text( - text = stringResource(R.string.this_app_needs_root_access_to_hook_onto_the_bluetooth_library), - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Normal, - textAlign = TextAlign.Center, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = textColor.copy(alpha = 0.7f) - ) - ) - - if (rootCheckFailed) { - Spacer(modifier = Modifier.height(8.dp)) - Text( - text = stringResource(R.string.root_access_denied), - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Normal, - textAlign = TextAlign.Center, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = Color(0xFFFF453A) - ) - ) - } - - Spacer(modifier = Modifier.height(24.dp)) - - Button( - onClick = { checkRootAccess() }, - modifier = Modifier - .fillMaxWidth() - .height(50.dp), - colors = ButtonDefaults.buttonColors( - containerColor = accentColor - ), - shape = RoundedCornerShape(8.dp), - enabled = !checkingRoot - ) { - if (checkingRoot) { - CircularProgressIndicator( - modifier = Modifier.size(24.dp), - color = Color.White, - strokeWidth = 2.dp - ) - } else { - Text( - "Check Root Access", - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - ) - } - } - } else { - StatusIcon(if (hasStarted) progressState else RadareOffsetFinder.ProgressState.Idle, isDarkTheme) - - Spacer(modifier = Modifier.height(24.dp)) - - AnimatedContent( - targetState = if (hasStarted) getStatusTitle(progressState, - moduleEnabled, bluetoothToggled) else "Setup Required", - transitionSpec = { fadeIn() togetherWith fadeOut() } - ) { text -> - Text( - text = text, - style = TextStyle( - fontSize = 22.sp, - fontWeight = FontWeight.Bold, - textAlign = TextAlign.Center, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = textColor - ) - ) - } - - Spacer(modifier = Modifier.height(8.dp)) - - AnimatedContent( - targetState = if (hasStarted) - getStatusDescription(progressState, moduleEnabled, bluetoothToggled) - else - "AirPods functionality requires one-time setup for hooking into Bluetooth library", - transitionSpec = { fadeIn() togetherWith fadeOut() } - ) { text -> - Text( - text = text, - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Normal, - textAlign = TextAlign.Center, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = textColor.copy(alpha = 0.7f) - ) - ) - } - - Spacer(modifier = Modifier.height(24.dp)) - - if (!hasStarted) { - Button( - onClick = { hasStarted = true }, - modifier = Modifier - .fillMaxWidth() - .height(50.dp), - colors = ButtonDefaults.buttonColors( - containerColor = accentColor - ), - shape = RoundedCornerShape(8.dp) - ) { - Text( - "Start Setup", - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - ) - } - } else { - when (progressState) { - is RadareOffsetFinder.ProgressState.DownloadProgress -> { - val progress = (progressState as RadareOffsetFinder.ProgressState.DownloadProgress).progress - val animatedProgress by animateFloatAsState( - targetValue = progress, - label = "Download Progress" - ) - Column( - modifier = Modifier.fillMaxWidth(), - horizontalAlignment = Alignment.CenterHorizontally - ) { - LinearProgressIndicator( - progress = { animatedProgress }, - modifier = Modifier - .fillMaxWidth() - .height(8.dp), - strokeCap = StrokeCap.Round, - color = accentColor - ) - - Spacer(modifier = Modifier.height(8.dp)) - - Text( - text = "${(progress * 100).toInt()}%", - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = textColor.copy(alpha = 0.6f) - ) - ) - } - } - is RadareOffsetFinder.ProgressState.Success -> { - if (!moduleEnabled) { - Button( - onClick = { moduleEnabled = true }, - modifier = Modifier - .fillMaxWidth() - .height(50.dp), - colors = ButtonDefaults.buttonColors( - containerColor = accentColor - ), - shape = RoundedCornerShape(8.dp) - ) { - Text( - "I've Enabled/Reactivated the Module", - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - ) - } - } else if (!bluetoothToggled) { - Button( - onClick = { bluetoothToggled = true }, - modifier = Modifier - .fillMaxWidth() - .height(50.dp), - colors = ButtonDefaults.buttonColors( - containerColor = accentColor - ), - shape = RoundedCornerShape(8.dp) - ) { - Text( - "I've Toggled Bluetooth", - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - ) - } - } else { - Button( - onClick = { - navController.navigate("settings") { - popUpTo("onboarding") { inclusive = true } - } - }, - modifier = Modifier - .fillMaxWidth() - .height(50.dp), - colors = ButtonDefaults.buttonColors( - containerColor = accentColor - ), - shape = RoundedCornerShape(8.dp) - ) { - Text( - "Continue to Settings", - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - ) - } - } - } - is RadareOffsetFinder.ProgressState.Idle, - is RadareOffsetFinder.ProgressState.Error -> { - // No specific UI for these states - } - else -> { - LinearProgressIndicator( - modifier = Modifier - .fillMaxWidth() - .height(8.dp), - strokeCap = StrokeCap.Round, - color = accentColor - ) - } - } - } - } - } - } - - Spacer(modifier = Modifier.weight(1f)) - - if (progressState is RadareOffsetFinder.ProgressState.Error && !isComplete && hasStarted) { - Button( - onClick = { - Log.d("Onboarding", "Trying to find offset again...") - kotlinx.coroutines.MainScope().launch { - withContext(Dispatchers.IO) { - radareOffsetFinder.setupAndFindOffset() - } - } - }, - modifier = Modifier - .fillMaxWidth() - .height(55.dp), - colors = ButtonDefaults.buttonColors( - containerColor = accentColor - ), - shape = RoundedCornerShape(8.dp) - ) { - Text( - "Try Again", - style = TextStyle( - fontSize = 16.sp, - fontWeight = FontWeight.Medium, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - ) - } - } - } - - if (showSkipDialog) { - AlertDialog( - onDismissRequest = { showSkipDialog = false }, - title = { Text("Skip Setup") }, - text = { - Text( - "Have you installed the root module that patches the Bluetooth library directly? This option is for users who have manually patched their system instead of using the dynamic hook.", - style = TextStyle( - fontSize = 16.sp, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - ) - }, - confirmButton = { - val sharedPreferences = activityContext.getSharedPreferences("settings", Context.MODE_PRIVATE) - TextButton( - onClick = { - showSkipDialog = false - RadareOffsetFinder.clearHookOffsets() - sharedPreferences.edit { putBoolean("skip_setup", true) } - navController.navigate("settings") { - popUpTo("onboarding") { inclusive = true } - } - } - ) { - Text( - "Yes, Skip Setup", - color = accentColor, - fontWeight = FontWeight.Bold - ) - } - }, - dismissButton = { - TextButton( - onClick = { showSkipDialog = false } - ) { - Text("Cancel") - } - }, - containerColor = backgroundColor, - textContentColor = textColor, - titleContentColor = textColor - ) - } - } -} - -@Composable -private fun StatusIcon( - progressState: RadareOffsetFinder.ProgressState, - isDarkTheme: Boolean -) { - val accentColor = if (isDarkTheme) Color(0xFF007AFF) else Color(0xFF3C6DF5) - val errorColor = if (isDarkTheme) Color(0xFFFF453A) else Color(0xFFFF3B30) - val successColor = if (isDarkTheme) Color(0xFF30D158) else Color(0xFF34C759) - - Box( - modifier = Modifier.size(80.dp), - contentAlignment = Alignment.Center - ) { - when (progressState) { - is RadareOffsetFinder.ProgressState.Error -> { - Icon( - imageVector = Icons.Default.Clear, - contentDescription = "Error", - tint = errorColor, - modifier = Modifier.size(50.dp) - ) - } - is RadareOffsetFinder.ProgressState.Success -> { - Icon( - imageVector = Icons.Default.Check, - contentDescription = "Success", - tint = successColor, - modifier = Modifier.size(50.dp) - ) - } - is RadareOffsetFinder.ProgressState.Idle -> { - Icon( - imageVector = Icons.Default.Settings, - contentDescription = "Settings", - tint = accentColor, - modifier = Modifier.size(50.dp) - ) - } - else -> { - CircularProgressIndicator( - modifier = Modifier.size(50.dp), - color = accentColor, - strokeWidth = 4.dp - ) - } - } - } -} - -private fun getStatusTitle( - state: RadareOffsetFinder.ProgressState, - moduleEnabled: Boolean, - bluetoothToggled: Boolean -): String { - return when (state) { - is RadareOffsetFinder.ProgressState.Success -> { - when { - !moduleEnabled -> "Enable Xposed Module" - !bluetoothToggled -> "Toggle Bluetooth" - else -> "Setup Complete" - } - } - is RadareOffsetFinder.ProgressState.Idle -> "Getting Ready" - is RadareOffsetFinder.ProgressState.CheckingExisting -> "Checking if radare2 already downloaded" - is RadareOffsetFinder.ProgressState.Downloading -> "Downloading radare2" - is RadareOffsetFinder.ProgressState.DownloadProgress -> "Downloading radare2" - is RadareOffsetFinder.ProgressState.Extracting -> "Extracting radare2" - is RadareOffsetFinder.ProgressState.MakingExecutable -> "Setting executable permissions" - is RadareOffsetFinder.ProgressState.FindingOffset -> "Finding function offset" - is RadareOffsetFinder.ProgressState.SavingOffset -> "Saving offset" - is RadareOffsetFinder.ProgressState.Cleaning -> "Cleaning Up" - is RadareOffsetFinder.ProgressState.Error -> "Setup Failed" - } -} - -private fun getStatusDescription( - state: RadareOffsetFinder.ProgressState, - moduleEnabled: Boolean, - bluetoothToggled: Boolean -): String { - return when (state) { - is RadareOffsetFinder.ProgressState.Success -> { - when { - !moduleEnabled -> "Please enable the LibrePods Xposed module in your Xposed manager (e.g. LSPosed). If already enabled, disable and re-enable it." - !bluetoothToggled -> "Please turn off and then turn on Bluetooth to apply the changes." - else -> "All set! You can now use your AirPods with enhanced functionality." - } - } - is RadareOffsetFinder.ProgressState.Idle -> "Preparing" - is RadareOffsetFinder.ProgressState.CheckingExisting -> "Checking if radare2 are already installed" - is RadareOffsetFinder.ProgressState.Downloading -> "Starting radare2 download" - is RadareOffsetFinder.ProgressState.DownloadProgress -> "Downloading radare2" - is RadareOffsetFinder.ProgressState.Extracting -> "Extracting radare2" - is RadareOffsetFinder.ProgressState.MakingExecutable -> "Setting executable permissions on radare2 binaries" - is RadareOffsetFinder.ProgressState.FindingOffset -> "Looking for the required Bluetooth function in system libraries" - is RadareOffsetFinder.ProgressState.SavingOffset -> "Saving the function offset" - is RadareOffsetFinder.ProgressState.Cleaning -> "Removing temporary extracted files" - is RadareOffsetFinder.ProgressState.Error -> state.message - } -} - -@ExperimentalHazeMaterialsApi -@Preview -@Composable -fun OnboardingPreview() { - Onboarding(navController = NavController(LocalContext.current), activityContext = LocalContext.current) -} - diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/PressAndHoldSettingsScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/PressAndHoldSettingsScreen.kt deleted file mode 100644 index c6b2e4091..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/PressAndHoldSettingsScreen.kt +++ /dev/null @@ -1,311 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -@file:OptIn(ExperimentalStdlibApi::class, ExperimentalEncodingApi::class) - -package me.kavishdevar.librepods.screens - -import android.content.Context -import android.util.Log -import androidx.compose.animation.animateColorAsState -import androidx.compose.animation.core.animateFloatAsState -import androidx.compose.animation.core.tween -import androidx.compose.foundation.background -import androidx.compose.foundation.gestures.detectTapGestures -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.wrapContentWidth -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.HorizontalDivider -import androidx.compose.material3.Icon -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.input.pointer.pointerInput -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.painterResource -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.Font -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.core.content.edit -import androidx.navigation.NavController -import com.kyant.backdrop.backdrops.layerBackdrop -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.SelectItem -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.composables.StyledSelectList -import me.kavishdevar.librepods.constants.StemAction -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager -import kotlin.experimental.and -import kotlin.io.encoding.ExperimentalEncodingApi - -@Composable -fun RightDivider() { - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(start = 72.dp, end = 20.dp) - ) -} - -@Composable -fun RightDividerNoIcon() { - HorizontalDivider( - thickness = 1.dp, - color = Color(0x40888888), - modifier = Modifier - .padding(start = 20.dp, end = 20.dp) - ) -} - -@ExperimentalHazeMaterialsApi -@OptIn(ExperimentalMaterial3Api::class) -@Composable -fun LongPress(navController: NavController, name: String) { - val isDarkTheme = isSystemInDarkTheme() - val textColor = if (isDarkTheme) Color.White else Color.Black - - val modesByte = ServiceManager.getService()!!.aacpManager.controlCommandStatusList.find { - it.identifier == AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE_CONFIGS - }?.value?.takeIf { it.isNotEmpty() }?.get(0) - - if (modesByte != null) { - Log.d("PressAndHoldSettingsScreen", "Current modes state: ${modesByte.toString(2)}") - Log.d("PressAndHoldSettingsScreen", "Off mode: ${(modesByte and 0x01) != 0.toByte()}") - Log.d("PressAndHoldSettingsScreen", "Transparency mode: ${(modesByte and 0x04) != 0.toByte()}") - Log.d("PressAndHoldSettingsScreen", "Noise Cancellation mode: ${(modesByte and 0x02) != 0.toByte()}") - Log.d("PressAndHoldSettingsScreen", "Adaptive mode: ${(modesByte and 0x08) != 0.toByte()}") - } - val context = LocalContext.current - val sharedPreferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE) - val prefKey = if (name.lowercase() == "left") "left_long_press_action" else "right_long_press_action" - val longPressActionPref = sharedPreferences.getString(prefKey, StemAction.CYCLE_NOISE_CONTROL_MODES.name) - Log.d("PressAndHoldSettingsScreen", "Long press action preference ($prefKey): $longPressActionPref") - var longPressAction by remember { mutableStateOf(StemAction.valueOf(longPressActionPref ?: StemAction.CYCLE_NOISE_CONTROL_MODES.name)) } - val backdrop = rememberLayerBackdrop() - StyledScaffold( - title = name - ) { spacerHeight -> - val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - Column ( - modifier = Modifier - .layerBackdrop(backdrop) - .fillMaxSize() - .padding(top = 8.dp) - .padding(horizontal = 16.dp) - ) { - Spacer(modifier = Modifier.height(spacerHeight)) - val actionItems = listOf( - SelectItem( - name = stringResource(R.string.noise_control), - selected = longPressAction == StemAction.CYCLE_NOISE_CONTROL_MODES, - onClick = { - longPressAction = StemAction.CYCLE_NOISE_CONTROL_MODES - sharedPreferences.edit { putString(prefKey, StemAction.CYCLE_NOISE_CONTROL_MODES.name) } - } - ), - SelectItem( - name = stringResource(R.string.digital_assistant), - selected = longPressAction == StemAction.DIGITAL_ASSISTANT, - onClick = { - longPressAction = StemAction.DIGITAL_ASSISTANT - sharedPreferences.edit { putString(prefKey, StemAction.DIGITAL_ASSISTANT.name) } - } - ) - ) - StyledSelectList(items = actionItems) - - if (longPressAction == StemAction.CYCLE_NOISE_CONTROL_MODES) { - Spacer(modifier = Modifier.height(32.dp)) - Text( - text = stringResource(R.string.noise_control), - style = TextStyle( - fontSize = 14.sp, - fontWeight = FontWeight.Bold, - color = textColor.copy(alpha = 0.6f), - ), - fontFamily = FontFamily(Font(R.font.sf_pro)), - modifier = Modifier - .padding(horizontal = 18.dp) - ) - - Spacer(modifier = Modifier.height(8.dp)) - - val offListeningModeValue = ServiceManager.getService()!!.aacpManager.controlCommandStatusList.find { - it.identifier == AACPManager.Companion.ControlCommandIdentifiers.ALLOW_OFF_OPTION - }?.value?.takeIf { it.isNotEmpty() }?.get(0) - Log.d("PressAndHoldSettingsScreen", "Allow Off state: $offListeningModeValue") - val allowOff = offListeningModeValue == 1.toByte() - Log.d("PressAndHoldSettingsScreen", "Allow Off option: $allowOff") - - val initialByte = ServiceManager.getService()!!.aacpManager.controlCommandStatusList.find { - it.identifier == AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE_CONFIGS - }?.value?.takeIf { it.isNotEmpty() }?.get(0)?.toInt() ?: sharedPreferences.getInt("long_press_byte", 0b0101) - var currentByte by remember { mutableStateOf(initialByte) } - - val listeningModeItems = mutableListOf() - if (allowOff) { - listeningModeItems.add( - SelectItem( - name = stringResource(R.string.off), - description = "Turns off noise management", - iconRes = R.drawable.noise_cancellation, - selected = (currentByte and 0x01) != 0, - onClick = { - val bit = 0x01 - val newValue = if ((currentByte and bit) != 0) { - val temp = currentByte and bit.inv() - if (countEnabledModes(temp) >= 2) temp else currentByte - } else { - currentByte or bit - } - ServiceManager.getService()!!.aacpManager.sendControlCommand( - AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE_CONFIGS.value, - newValue.toByte() - ) - sharedPreferences.edit { - putInt("long_press_byte", newValue) - } - currentByte = newValue - } - ) - ) - } - listeningModeItems.addAll(listOf( - SelectItem( - name = stringResource(R.string.transparency), - description = "Lets in external sounds", - iconRes = R.drawable.transparency, - selected = (currentByte and 0x04) != 0, - onClick = { - val bit = 0x04 - val newValue = if ((currentByte and bit) != 0) { - val temp = currentByte and bit.inv() - if (countEnabledModes(temp) >= 2) temp else currentByte - } else { - currentByte or bit - } - ServiceManager.getService()!!.aacpManager.sendControlCommand( - AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE_CONFIGS.value, - newValue.toByte() - ) - sharedPreferences.edit { - putInt("long_press_byte", newValue) - } - currentByte = newValue - } - ), - SelectItem( - name = stringResource(R.string.adaptive), - description = "Dynamically adjust external noise", - iconRes = R.drawable.adaptive, - selected = (currentByte and 0x08) != 0, - onClick = { - val bit = 0x08 - val newValue = if ((currentByte and bit) != 0) { - val temp = currentByte and bit.inv() - if (countEnabledModes(temp) >= 2) temp else currentByte - } else { - currentByte or bit - } - ServiceManager.getService()!!.aacpManager.sendControlCommand( - AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE_CONFIGS.value, - newValue.toByte() - ) - sharedPreferences.edit { - putInt("long_press_byte", newValue) - } - currentByte = newValue - } - ), - SelectItem( - name = stringResource(R.string.noise_cancellation), - description = "Blocks out external sounds", - iconRes = R.drawable.noise_cancellation, - selected = (currentByte and 0x02) != 0, - onClick = { - val bit = 0x02 - val newValue = if ((currentByte and bit) != 0) { - val temp = currentByte and bit.inv() - if (countEnabledModes(temp) >= 2) temp else currentByte - } else { - currentByte or bit - } - ServiceManager.getService()!!.aacpManager.sendControlCommand( - AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE_CONFIGS.value, - newValue.toByte() - ) - sharedPreferences.edit { - putInt("long_press_byte", newValue) - } - currentByte = newValue - } - ) - )) - StyledSelectList(items = listeningModeItems) - Spacer(modifier = Modifier.height(8.dp)) - Text( - text = stringResource(R.string.press_and_hold_noise_control_description), - style = TextStyle( - fontSize = 12.sp, - fontWeight = FontWeight.Light, - color = textColor.copy(alpha = 0.6f), - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - modifier = Modifier - .padding(horizontal = 18.dp) - ) - } - } - } - Log.d("PressAndHoldSettingsScreen", "Current byte: ${ServiceManager.getService()!!.aacpManager.controlCommandStatusList.find { - it.identifier == AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE_CONFIGS - }?.value?.takeIf { it.isNotEmpty() }?.get(0)?.toString(2)}") -} - -fun countEnabledModes(byteValue: Int): Int { - var count = 0 - if ((byteValue and 0x01) != 0) count++ - if ((byteValue and 0x02) != 0) count++ - if ((byteValue and 0x04) != 0) count++ - if ((byteValue and 0x08) != 0) count++ - return count -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/RenameScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/RenameScreen.kt deleted file mode 100644 index f58d09439..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/RenameScreen.kt +++ /dev/null @@ -1,167 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -@file:OptIn(ExperimentalEncodingApi::class) - -package me.kavishdevar.librepods.screens - -import android.content.Context -import androidx.compose.foundation.background -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.foundation.text.BasicTextField -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.IconButton -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.focus.FocusRequester -import androidx.compose.ui.focus.focusRequester -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.SolidColor -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.platform.LocalSoftwareKeyboardController -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextRange -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.Font -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.input.TextFieldValue -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.core.content.edit -import androidx.navigation.NavController -import com.kyant.backdrop.backdrops.layerBackdrop -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledIconButton -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.services.ServiceManager -import kotlin.io.encoding.ExperimentalEncodingApi - - -@OptIn(ExperimentalMaterial3Api::class, ExperimentalHazeMaterialsApi::class) -@Composable -fun RenameScreen(navController: NavController) { - val sharedPreferences = LocalContext.current.getSharedPreferences("settings", Context.MODE_PRIVATE) - val isDarkTheme = isSystemInDarkTheme() - val name = remember { mutableStateOf(TextFieldValue(sharedPreferences.getString("name", "") ?: "")) } - val focusRequester = remember { FocusRequester() } - val keyboardController = LocalSoftwareKeyboardController.current - - LaunchedEffect(Unit) { - focusRequester.requestFocus() - keyboardController?.show() - name.value = name.value.copy(selection = TextRange(name.value.text.length)) - } - - val backdrop = rememberLayerBackdrop() - - StyledScaffold( - title = stringResource(R.string.name), - ) { spacerHeight -> - Column( - modifier = Modifier - .fillMaxSize() - .layerBackdrop(backdrop) - .padding(horizontal = 16.dp) - ) { - Spacer(modifier = Modifier.height(spacerHeight)) - val isDarkTheme = isSystemInDarkTheme() - val backgroundColor = if (isDarkTheme) Color(0xFF1C1C1E) else Color(0xFFFFFFFF) - val textColor = if (isDarkTheme) Color.White else Color.Black - val cursorColor = if (isDarkTheme) Color.White else Color.Black - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier - .fillMaxWidth() - .height(58.dp) - .background( - backgroundColor, - RoundedCornerShape(28.dp) - ) - .padding(horizontal = 16.dp, vertical = 8.dp) - ) { - BasicTextField( - value = name.value, - onValueChange = { - name.value = it - sharedPreferences.edit {putString("name", it.text)} - ServiceManager.getService()?.setName(it.text) - }, - textStyle = TextStyle( - fontSize = 16.sp, - color = textColor, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ), - singleLine = true, - cursorBrush = SolidColor(cursorColor), - decorationBox = { innerTextField -> - Row( - verticalAlignment = Alignment.CenterVertically, - ) { - Row( - modifier = Modifier - .weight(1f) - ) { - innerTextField() - } - IconButton( - onClick = { - name.value = TextFieldValue("") - } - ) { - Text( - text = "􀁡", - style = TextStyle( - fontSize = 16.sp, - fontFamily = FontFamily(Font(R.font.sf_pro)), - color = if (isDarkTheme) Color.White.copy(alpha = 0.6f) else Color.Black.copy(alpha = 0.6f) - ), - ) - } - } - }, - modifier = Modifier - .fillMaxWidth() - .padding(start = 8.dp) - .focusRequester(focusRequester) - ) - } - } - } -} - -@Preview -@Composable -fun RenameScreenPreview() { - RenameScreen(navController = NavController(LocalContext.current)) -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/screens/UpdateHearingTestScreen.kt b/android/app/src/main/java/me/kavishdevar/librepods/screens/UpdateHearingTestScreen.kt deleted file mode 100644 index 9b1577128..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/screens/UpdateHearingTestScreen.kt +++ /dev/null @@ -1,359 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.screens - -import android.annotation.SuppressLint -import android.util.Log -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.width -import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.text.KeyboardOptions -import androidx.compose.foundation.verticalScroll -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.OutlinedTextField -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.MutableState -import androidx.compose.runtime.mutableIntStateOf -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.Font -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.input.KeyboardType -import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.navigation.NavController -import com.kyant.backdrop.backdrops.layerBackdrop -import com.kyant.backdrop.backdrops.rememberLayerBackdrop -import dev.chrisbanes.haze.hazeSource -import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi -import kotlinx.coroutines.Job -import kotlinx.coroutines.delay -import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.composables.StyledScaffold -import me.kavishdevar.librepods.services.ServiceManager -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.ATTHandles -import me.kavishdevar.librepods.utils.HearingAidSettings -import me.kavishdevar.librepods.utils.parseHearingAidSettingsResponse -import me.kavishdevar.librepods.utils.sendHearingAidSettings -import java.io.IOException -import kotlin.io.encoding.ExperimentalEncodingApi - -private var debounceJob: MutableState = mutableStateOf(null) -private const val TAG = "HearingAidAdjustments" - -@SuppressLint("DefaultLocale") -@ExperimentalHazeMaterialsApi -@OptIn(ExperimentalMaterial3Api::class, ExperimentalEncodingApi::class) -@Composable -fun UpdateHearingTestScreen(@Suppress("unused") navController: NavController) { - val verticalScrollState = rememberScrollState() - val attManager = ServiceManager.getService()?.attManager - if (attManager == null) { - Text( - text = stringResource(R.string.att_manager_is_null_try_reconnecting), - modifier = Modifier - .fillMaxSize() - .padding(16.dp), - textAlign = TextAlign.Center - ) - return - } - - val aacpManager = remember { ServiceManager.getService()?.aacpManager } - val backdrop = rememberLayerBackdrop() - StyledScaffold( - title = stringResource(R.string.hearing_test) - ) { spacerHeight, hazeState -> - Column( - modifier = Modifier - .hazeSource(hazeState) - .fillMaxSize() - .layerBackdrop(backdrop) - .verticalScroll(verticalScrollState) - .padding(horizontal = 16.dp), - verticalArrangement = Arrangement.spacedBy(16.dp) - ) { - Spacer(modifier = Modifier.height(spacerHeight)) - - Text( - text = stringResource(R.string.hearing_test_value_instruction), - fontSize = 16.sp, - modifier = Modifier.fillMaxWidth(), - textAlign = TextAlign.Center, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - - val conversationBoostEnabled = remember { mutableStateOf(false) } - val leftEQ = remember { mutableStateOf(FloatArray(8)) } - val rightEQ = remember { mutableStateOf(FloatArray(8)) } - - val initialLoadComplete = remember { mutableStateOf(false) } - val initialReadSucceeded = remember { mutableStateOf(false) } - val initialReadAttempts = remember { mutableIntStateOf(0) } - - val hearingAidSettings = remember { - mutableStateOf( - HearingAidSettings( - leftEQ = leftEQ.value, - rightEQ = rightEQ.value, - leftAmplification = 0.5f, - rightAmplification = 0.5f, - leftTone = 0.5f, - rightTone = 0.5f, - leftConversationBoost = conversationBoostEnabled.value, - rightConversationBoost = conversationBoostEnabled.value, - leftAmbientNoiseReduction = 0.0f, - rightAmbientNoiseReduction = 0.0f, - netAmplification = 0.5f, - balance = 0.5f, - ownVoiceAmplification = 0.5f - ) - ) - } - - val hearingAidEnabled = remember { - val aidStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID } - val assistStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG } - mutableStateOf((aidStatus?.value?.getOrNull(1) == 0x01.toByte()) && (assistStatus?.value?.getOrNull(0) == 0x01.toByte())) - } - - val hearingAidListener = remember { - object : AACPManager.ControlCommandListener { - override fun onControlCommandReceived(controlCommand: AACPManager.ControlCommand) { - if (controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID.value || - controlCommand.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG.value) { - val aidStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID } - val assistStatus = aacpManager?.controlCommandStatusList?.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG } - hearingAidEnabled.value = (aidStatus?.value?.getOrNull(1) == 0x01.toByte()) && (assistStatus?.value?.getOrNull(0) == 0x01.toByte()) - } - } - } - } - - val hearingAidATTListener = remember { - object : (ByteArray) -> Unit { - override fun invoke(value: ByteArray) { - val parsed = parseHearingAidSettingsResponse(value) - if (parsed != null) { - leftEQ.value = parsed.leftEQ.copyOf() - rightEQ.value = parsed.rightEQ.copyOf() - conversationBoostEnabled.value = parsed.leftConversationBoost - Log.d(TAG, "Updated hearing aid settings from notification") - } else { - Log.w(TAG, "Failed to parse hearing aid settings from notification") - } - } - } - } - - LaunchedEffect(Unit) { - aacpManager?.registerControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, hearingAidListener) - aacpManager?.registerControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, hearingAidListener) - } - - DisposableEffect(Unit) { - onDispose { - aacpManager?.unregisterControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_AID, hearingAidListener) - aacpManager?.unregisterControlCommandListener(AACPManager.Companion.ControlCommandIdentifiers.HEARING_ASSIST_CONFIG, hearingAidListener) - attManager.unregisterListener(ATTHandles.HEARING_AID, hearingAidATTListener) - } - } - - LaunchedEffect(leftEQ.value, rightEQ.value, conversationBoostEnabled.value, initialLoadComplete.value, initialReadSucceeded.value) { - if (!initialLoadComplete.value) { - Log.d(TAG, "Initial device load not complete - skipping send") - return@LaunchedEffect - } - - if (!initialReadSucceeded.value) { - Log.d(TAG, "Initial device read not successful yet - skipping send until read succeeds") - return@LaunchedEffect - } - - hearingAidSettings.value = HearingAidSettings( - leftEQ = leftEQ.value, - rightEQ = rightEQ.value, - leftAmplification = 0.5f, - rightAmplification = 0.5f, - leftTone = 0.5f, - rightTone = 0.5f, - leftConversationBoost = conversationBoostEnabled.value, - rightConversationBoost = conversationBoostEnabled.value, - leftAmbientNoiseReduction = 0.0f, - rightAmbientNoiseReduction = 0.0f, - netAmplification = 0.5f, - balance = 0.5f, - ownVoiceAmplification = 0.5f - ) - Log.d(TAG, "Updated settings: ${hearingAidSettings.value}") - sendHearingAidSettings(attManager, hearingAidSettings.value, debounceJob) - } - - LaunchedEffect(Unit) { - Log.d(TAG, "Connecting to ATT...") - try { - attManager.enableNotifications(ATTHandles.HEARING_AID) - attManager.registerListener(ATTHandles.HEARING_AID, hearingAidATTListener) - - try { - if (aacpManager != null) { - Log.d(TAG, "Found AACPManager, reading cached EQ data") - val aacpEQ = aacpManager.eqData - if (aacpEQ.isNotEmpty()) { - leftEQ.value = aacpEQ.copyOf() - rightEQ.value = aacpEQ.copyOf() - Log.d(TAG, "Populated EQ from AACPManager: ${aacpEQ.toList()}") - } else { - Log.d(TAG, "AACPManager EQ data empty") - } - } else { - Log.d(TAG, "No AACPManager available") - } - } catch (e: Exception) { - Log.w(TAG, "Error reading EQ from AACPManager: ${e.message}") - } - - var parsedSettings: HearingAidSettings? = null - for (attempt in 1..3) { - initialReadAttempts.intValue = attempt - try { - val data = attManager.read(ATTHandles.HEARING_AID) - parsedSettings = parseHearingAidSettingsResponse(data = data) - if (parsedSettings != null) { - Log.d(TAG, "Parsed settings on attempt $attempt") - break - } else { - Log.d(TAG, "Parsing returned null on attempt $attempt") - } - } catch (e: Exception) { - Log.w(TAG, "Read attempt $attempt failed: ${e.message}") - } - delay(200) - } - - if (parsedSettings != null) { - Log.d(TAG, "Initial hearing aid settings: $parsedSettings") - leftEQ.value = parsedSettings.leftEQ.copyOf() - rightEQ.value = parsedSettings.rightEQ.copyOf() - conversationBoostEnabled.value = parsedSettings.leftConversationBoost - initialReadSucceeded.value = true - } else { - Log.d(TAG, "Failed to read/parse initial hearing aid settings after ${initialReadAttempts.intValue} attempts") - } - } catch (e: IOException) { - e.printStackTrace() - } finally { - initialLoadComplete.value = true - } - } - - val frequencies = listOf("250Hz", "500Hz", "1kHz", "2kHz", "3kHz", "4kHz", "6kHz", "8kHz") - - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.spacedBy(8.dp) - ) { - Spacer(modifier = Modifier.width(60.dp)) - Text( - text = stringResource(R.string.left), - fontSize = 18.sp, - modifier = Modifier.weight(1f), - textAlign = TextAlign.Center, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - Text( - text = stringResource(R.string.right), - fontSize = 18.sp, - modifier = Modifier.weight(1f), - textAlign = TextAlign.Center, - fontFamily = FontFamily(Font(R.font.sf_pro)) - ) - } - - frequencies.forEachIndexed { index, freq -> - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.spacedBy(8.dp) - ) { - Text( - text = freq, - modifier = Modifier - .width(60.dp) - .align(Alignment.CenterVertically), - textAlign = TextAlign.End, - fontSize = 16.sp, - fontFamily = FontFamily(Font(R.font.sf_pro)), - ) - OutlinedTextField( - value = leftEQ.value[index].toString(), - onValueChange = { newValue -> - val parsed = newValue.toFloatOrNull() - if (parsed != null) { - val newArray = leftEQ.value.copyOf() - newArray[index] = parsed - leftEQ.value = newArray - } - }, -// label = { Text("Value", fontSize = 14.sp, fontFamily = FontFamily(Font(R.font.sf_pro))) }, - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), - textStyle = TextStyle( - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontSize = 14.sp - ), - modifier = Modifier.weight(1f) - ) - OutlinedTextField( - value = rightEQ.value[index].toString(), - onValueChange = { newValue -> - val parsed = newValue.toFloatOrNull() - if (parsed != null) { - val newArray = rightEQ.value.copyOf() - newArray[index] = parsed - rightEQ.value = newArray - } - }, -// label = { Text("Value", fontSize = 14.sp, fontFamily = FontFamily(Font(R.font.sf_pro))) }, - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), - textStyle = TextStyle( - fontFamily = FontFamily(Font(R.font.sf_pro)), - fontSize = 14.sp - ), - modifier = Modifier.weight(1f) - ) - } - } - } - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsQSService.kt b/android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsQSService.kt index efddf8ad6..eea7c6fc3 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsQSService.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsQSService.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) @@ -35,9 +35,10 @@ import android.util.Log import androidx.annotation.RequiresApi import me.kavishdevar.librepods.QuickSettingsDialogActivity import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.constants.AirPodsNotifications -import me.kavishdevar.librepods.constants.NoiseControlMode -import me.kavishdevar.librepods.utils.AACPManager +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.bluetooth.BluetoothConnectionManager +import me.kavishdevar.librepods.data.AirPodsNotifications +import me.kavishdevar.librepods.data.NoiseControlMode import kotlin.io.encoding.ExperimentalEncodingApi @RequiresApi(Build.VERSION_CODES.Q) @@ -98,7 +99,7 @@ class AirPodsQSService : TileService() { Log.d("AirPodsQSService", "onStartListening") val service = ServiceManager.getService() - isAirPodsConnected = service?.isConnectedLocally == true + isAirPodsConnected = BluetoothConnectionManager.getAACPSocket()?.isConnected == true currentAncMode = service?.getANC() ?: (NoiseControlMode.OFF.ordinal + 1) if (currentAncMode == NoiseControlMode.OFF.ordinal + 1 && !isOffModeEnabled()) { @@ -151,7 +152,7 @@ class AirPodsQSService : TileService() { return } - val clickBehavior = sharedPreferences.getString("qs_click_behavior", "dialog") ?: "dialog" + val clickBehavior = "cycle" // sharedPreferences.getString("qs_click_behavior", "dialog") ?: "dialog" if (clickBehavior == "dialog") { launchDialogActivity() @@ -244,8 +245,10 @@ class AirPodsQSService : TileService() { private fun getNextAncMode(): Int { val availableModes = getAvailableModes() + Log.d("AirPodsQSService", "availableModes: $availableModes, currentAncMode: $currentAncMode") val currentIndex = availableModes.indexOf(currentAncMode) val nextIndex = (currentIndex + 1) % availableModes.size + Log.d("AirPodsQSService", "nextIndex: $nextIndex") return availableModes[nextIndex] } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsService.kt b/android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsService.kt index d8d16d3b5..7dff6f696 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsService.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsService.kt @@ -1,26 +1,27 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) -@file:Suppress("DEPRECATION") package me.kavishdevar.librepods.services +//import me.kavishdevar.librepods.utils.CrossDevice +//import me.kavishdevar.librepods.utils.CrossDevicePackets import android.Manifest import android.annotation.SuppressLint import android.app.Notification @@ -29,6 +30,7 @@ import android.app.NotificationManager import android.app.PendingIntent import android.app.Service import android.appwidget.AppWidgetManager +import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothHeadset import android.bluetooth.BluetoothManager @@ -56,7 +58,7 @@ import android.os.ParcelUuid import android.os.UserHandle import android.provider.Settings import android.telecom.TelecomManager -import android.telephony.PhoneStateListener +import android.telephony.TelephonyCallback import android.telephony.TelephonyManager import android.util.Log import android.util.TypedValue @@ -78,29 +80,35 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withTimeout +import me.kavishdevar.librepods.BuildConfig import me.kavishdevar.librepods.MainActivity import me.kavishdevar.librepods.R -import me.kavishdevar.librepods.constants.AirPodsNotifications -import me.kavishdevar.librepods.constants.Battery -import me.kavishdevar.librepods.constants.BatteryComponent -import me.kavishdevar.librepods.constants.BatteryStatus -import me.kavishdevar.librepods.constants.StemAction -import me.kavishdevar.librepods.constants.isHeadTrackingData -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.AACPManager.Companion.StemPressType -import me.kavishdevar.librepods.utils.ATTManager -import me.kavishdevar.librepods.utils.AirPodsInstance -import me.kavishdevar.librepods.utils.AirPodsModels -import me.kavishdevar.librepods.utils.BLEManager -import me.kavishdevar.librepods.utils.BluetoothConnectionManager -import me.kavishdevar.librepods.utils.CrossDevice -import me.kavishdevar.librepods.utils.CrossDevicePackets +import me.kavishdevar.librepods.bluetooth.AACPManager +import me.kavishdevar.librepods.bluetooth.AACPManager.Companion.StemPressType +import me.kavishdevar.librepods.bluetooth.ATTCCCDHandles +import me.kavishdevar.librepods.bluetooth.ATTHandles +import me.kavishdevar.librepods.bluetooth.ATTManagerv2 +import me.kavishdevar.librepods.bluetooth.BLEManager +import me.kavishdevar.librepods.bluetooth.BluetoothConnectionManager +import me.kavishdevar.librepods.data.AirPodsInstance +import me.kavishdevar.librepods.data.AirPodsModels +import me.kavishdevar.librepods.data.AirPodsNotifications +import me.kavishdevar.librepods.data.Battery +import me.kavishdevar.librepods.data.BatteryComponent +import me.kavishdevar.librepods.data.BatteryStatus +import me.kavishdevar.librepods.data.Capability +import me.kavishdevar.librepods.data.CustomEq +import me.kavishdevar.librepods.data.StemAction +import me.kavishdevar.librepods.data.XposedRemotePrefProvider +import me.kavishdevar.librepods.data.isHeadTrackingData +import me.kavishdevar.librepods.presentation.overlays.IslandType +import me.kavishdevar.librepods.presentation.overlays.IslandWindow +import me.kavishdevar.librepods.presentation.overlays.PopupWindow +import me.kavishdevar.librepods.presentation.widgets.BatteryWidget +import me.kavishdevar.librepods.presentation.widgets.NoiseControlWidget import me.kavishdevar.librepods.utils.GestureDetector import me.kavishdevar.librepods.utils.HeadTracking -import me.kavishdevar.librepods.utils.IslandType -import me.kavishdevar.librepods.utils.IslandWindow import me.kavishdevar.librepods.utils.MediaController -import me.kavishdevar.librepods.utils.PopupWindow import me.kavishdevar.librepods.utils.SystemApisUtils import me.kavishdevar.librepods.utils.SystemApisUtils.DEVICE_TYPE_UNTETHERED_HEADSET import me.kavishdevar.librepods.utils.SystemApisUtils.METADATA_COMPANION_APP @@ -120,27 +128,22 @@ import me.kavishdevar.librepods.utils.SystemApisUtils.METADATA_UNTETHERED_RIGHT_ import me.kavishdevar.librepods.utils.SystemApisUtils.METADATA_UNTETHERED_RIGHT_CHARGING import me.kavishdevar.librepods.utils.SystemApisUtils.METADATA_UNTETHERED_RIGHT_ICON import me.kavishdevar.librepods.utils.SystemApisUtils.METADATA_UNTETHERED_RIGHT_LOW_BATTERY_THRESHOLD -import me.kavishdevar.librepods.widgets.BatteryWidget -import me.kavishdevar.librepods.widgets.NoiseControlWidget -import org.lsposed.hiddenapibypass.HiddenApiBypass import java.nio.ByteBuffer import java.nio.ByteOrder import kotlin.io.encoding.Base64 import kotlin.io.encoding.ExperimentalEncodingApi +import kotlin.time.Duration.Companion.milliseconds private const val TAG = "AirPodsService" object ServiceManager { - @ExperimentalEncodingApi private var service: AirPodsService? = null - @ExperimentalEncodingApi @Synchronized fun getService(): AirPodsService? { return service } - @ExperimentalEncodingApi @Synchronized fun setService(service: AirPodsService?) { this.service = service @@ -148,16 +151,16 @@ object ServiceManager { } // @Suppress("unused") -@ExperimentalEncodingApi class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeListener { var macAddress = "" var localMac = "" lateinit var aacpManager: AACPManager - var attManager: ATTManager? = null + lateinit var attManager: ATTManagerv2 var airpodsInstance: AirPodsInstance? = null var cameraActive = false private var disconnectedBecauseReversed = false private var otherDeviceTookOver = false + data class ServiceConfig( var deviceName: String = "AirPods", var earDetectionEnabled: Boolean = true, @@ -167,7 +170,6 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList var headGestures: Boolean = true, var disconnectWhenNotWearing: Boolean = false, var conversationalAwarenessVolume: Int = 43, - var textColor: Long = -1L, var qsClickBehavior: String = "cycle", var bleOnlyMode: Boolean = false, @@ -193,7 +195,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList var leftLongPressAction: StemAction = StemAction.defaultActions[StemPressType.LONG_PRESS]!!, var rightLongPressAction: StemAction = StemAction.defaultActions[StemPressType.LONG_PRESS]!!, - var cameraAction: AACPManager.Companion.StemPressType? = null, + var cameraAction: StemPressType? = null, // AirPods device information var airpodsName: String = "", @@ -207,6 +209,9 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList var airpodsVersion3: String = "", var airpodsHardwareRevision: String = "", var airpodsUpdaterIdentifier: String = "", + + // phone's mac, needed for tipi + var selfMacAddress: String = "" ) private lateinit var config: ServiceConfig @@ -222,7 +227,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList val packetLogsFlow: StateFlow> get() = _packetLogsFlow private lateinit var telephonyManager: TelephonyManager - private lateinit var phoneStateListener: PhoneStateListener + private lateinit var phoneStateListener: TelephonyCallback private val maxLogEntries = 1000 private val inMemoryLogs = mutableSetOf() @@ -230,35 +235,33 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList lateinit var bleManager: BLEManager - private lateinit var socket: BluetoothSocket + companion object { + init { + System.loadLibrary("bluetooth_socket") + } + } private val bleStatusListener = object : BLEManager.AirPodsStatusListener { @SuppressLint("NewApi") override fun onDeviceStatusChanged( - device: BLEManager.AirPodsStatus, - previousStatus: BLEManager.AirPodsStatus? + device: BLEManager.AirPodsStatus, previousStatus: BLEManager.AirPodsStatus? ) { - // Store MAC address for BLE-only mode if not already stored - if (config.bleOnlyMode && macAddress.isEmpty()) { - macAddress = device.address - sharedPreferences.edit { - putString("mac_address", macAddress) - } - Log.d(TAG, "BLE-only mode: stored MAC address ${device.address}") - } - - if (device.connectionState == "Disconnected" && !config.bleOnlyMode) { + if (device.connectionState == "Disconnected" && BluetoothConnectionManager.getAACPSocket()?.isConnected != true) { // should never happen unless android messes up and sends us a stale broadcast Log.d(TAG, "Seems no device has taken over, we will.") val bluetoothManager = getSystemService(BluetoothManager::class.java) - val bluetoothDevice = bluetoothManager.adapter.getRemoteDevice(sharedPreferences.getString( - "mac_address", "") ?: "") - connectToSocket(bluetoothDevice) + val bluetoothAdapter = bluetoothManager.adapter + val bluetoothDevice = bluetoothAdapter.getRemoteDevice( + sharedPreferences.getString( + "mac_address", "" + ) ?: "" + ) + connectToSocket(bluetoothAdapter, bluetoothDevice) } Log.d(TAG, "Device status changed") - if (isConnectedLocally) return - val leftLevel = bleManager.getMostRecentStatus()?.leftBattery?: 0 - val rightLevel = bleManager.getMostRecentStatus()?.rightBattery?: 0 - val caseLevel = bleManager.getMostRecentStatus()?.caseBattery?: 0 + if (BluetoothConnectionManager.getAACPSocket()?.isConnected == true) return + val leftLevel = bleManager.getMostRecentStatus()?.leftBattery ?: 0 + val rightLevel = bleManager.getMostRecentStatus()?.rightBattery ?: 0 + val caseLevel = bleManager.getMostRecentStatus()?.caseBattery ?: 0 val leftCharging = bleManager.getMostRecentStatus()?.isLeftCharging val rightCharging = bleManager.getMostRecentStatus()?.isRightCharging val caseCharging = bleManager.getMostRecentStatus()?.isCaseCharging @@ -285,12 +288,13 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList Log.d(TAG, "Lid opened") showPopup( this@AirPodsService, - getSharedPreferences("settings", MODE_PRIVATE).getString("name", "AirPods Pro") ?: "AirPods" + getSharedPreferences("settings", MODE_PRIVATE).getString("name", "AirPods Pro") + ?: "AirPods" ) - if (isConnectedLocally) return - val leftLevel = bleManager.getMostRecentStatus()?.leftBattery?: 0 - val rightLevel = bleManager.getMostRecentStatus()?.rightBattery?: 0 - val caseLevel = bleManager.getMostRecentStatus()?.caseBattery?: 0 + if (BluetoothConnectionManager.getAACPSocket()?.isConnected == true) return + val leftLevel = bleManager.getMostRecentStatus()?.leftBattery ?: 0 + val rightLevel = bleManager.getMostRecentStatus()?.rightBattery ?: 0 + val caseLevel = bleManager.getMostRecentStatus()?.caseBattery ?: 0 val leftCharging = bleManager.getMostRecentStatus()?.isLeftCharging val rightCharging = bleManager.getMostRecentStatus()?.isRightCharging val caseCharging = bleManager.getMostRecentStatus()?.isCaseCharging @@ -310,9 +314,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } override fun onEarStateChanged( - device: BLEManager.AirPodsStatus, - leftInEar: Boolean, - rightInEar: Boolean + device: BLEManager.AirPodsStatus, leftInEar: Boolean, rightInEar: Boolean ) { Log.d(TAG, "Ear state changed - Left: $leftInEar, Right: $rightInEar") @@ -323,10 +325,10 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } override fun onBatteryChanged(device: BLEManager.AirPodsStatus) { - if (isConnectedLocally) return - val leftLevel = bleManager.getMostRecentStatus()?.leftBattery?: 0 - val rightLevel = bleManager.getMostRecentStatus()?.rightBattery?: 0 - val caseLevel = bleManager.getMostRecentStatus()?.caseBattery?: 0 + if (BluetoothConnectionManager.getAACPSocket()?.isConnected == true) return + val leftLevel = bleManager.getMostRecentStatus()?.leftBattery ?: 0 + val rightLevel = bleManager.getMostRecentStatus()?.rightBattery ?: 0 + val caseLevel = bleManager.getMostRecentStatus()?.caseBattery ?: 0 val leftCharging = bleManager.getMostRecentStatus()?.isLeftCharging val rightCharging = bleManager.getMostRecentStatus()?.isRightCharging val caseCharging = bleManager.getMostRecentStatus()?.isCaseCharging @@ -351,13 +353,27 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } } - @SuppressLint("MissingPermission", "UnspecifiedRegisterReceiverFlag") + fun isBluetoothSocketExempted(): Boolean { + return try { + BluetoothSocket::class.java.declaredConstructors // will throw if still blocked + true + } catch (e: Exception) { + e.printStackTrace() + false + } + } + + + @SuppressLint("MissingPermission", "UnspecifiedRegisterReceiverFlag", "HardwareIds") override fun onCreate() { super.onCreate() + Log.i(TAG, "lib exempt worked: ${isBluetoothSocketExempted()}") sharedPreferencesLogs = getSharedPreferences("packet_logs", MODE_PRIVATE) - inMemoryLogs.addAll(sharedPreferencesLogs.getStringSet(packetLogKey, emptySet()) ?: emptySet()) + inMemoryLogs.addAll( + sharedPreferencesLogs.getStringSet(packetLogKey, emptySet()) ?: emptySet() + ) _packetLogsFlow.value = inMemoryLogs.toSet() sharedPreferences = getSharedPreferences("settings", MODE_PRIVATE) @@ -366,11 +382,42 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList aacpManager = AACPManager() initializeAACPManagerCallback() + attManager = ATTManagerv2() + sharedPreferences.registerOnSharedPreferenceChangeListener(this) - val process = Runtime.getRuntime().exec(arrayOf("su", "-c", "settings", "get", "secure", "bluetooth_address")) - val output = process.inputStream.bufferedReader().use { it.readLine() } - localMac = output.trim() + localMac = config.selfMacAddress + if (localMac.isEmpty()) { + if (checkSelfPermission("android.permission.LOCAL_MAC_ADDRESS") == PackageManager.PERMISSION_GRANTED) { + val bluetoothManager = getSystemService(BluetoothManager::class.java) + val bluetoothAdapter = bluetoothManager.adapter + localMac = bluetoothAdapter.address + } else { + localMac = try { + val process = Runtime.getRuntime().exec( + arrayOf("su", "-c", "settings get secure bluetooth_address") + ) + + val exitCode = process.waitFor() + + if (exitCode == 0) { + process.inputStream.bufferedReader().use { it.readLine()?.trim().orEmpty() } + } else { + "" + } + } catch (e: Exception) { + Log.e( + TAG, + "Error retrieving local MAC address: ${e.message}. We probably aren't rooted." + ) + "" + } + } + config.selfMacAddress = localMac + sharedPreferences.edit { + putString("self_mac_address", localMac) + } + } ServiceManager.setService(this) startForegroundNotification() @@ -391,31 +438,25 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList with(sharedPreferences) { edit { if (!contains("conversational_awareness_pause_music")) putBoolean( - "conversational_awareness_pause_music", - false + "conversational_awareness_pause_music", false ) if (!contains("personalized_volume")) putBoolean("personalized_volume", false) if (!contains("automatic_ear_detection")) putBoolean( - "automatic_ear_detection", - true + "automatic_ear_detection", true ) if (!contains("long_press_nc")) putBoolean("long_press_nc", true) if (!contains("show_phone_battery_in_widget")) putBoolean( - "show_phone_battery_in_widget", - true + "show_phone_battery_in_widget", true ) if (!contains("single_anc")) putBoolean("single_anc", true) if (!contains("long_press_transparency")) putBoolean( - "long_press_transparency", - true + "long_press_transparency", true ) if (!contains("conversational_awareness")) putBoolean( - "conversational_awareness", - true + "conversational_awareness", true ) if (!contains("relative_conversational_awareness_volume")) putBoolean( - "relative_conversational_awareness_volume", - true + "relative_conversational_awareness_volume", true ) if (!contains("long_press_adaptive")) putBoolean("long_press_adaptive", true) if (!contains("loud_sound_reduction")) putBoolean("loud_sound_reduction", true) @@ -423,38 +464,31 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList if (!contains("volume_control")) putBoolean("volume_control", true) if (!contains("head_gestures")) putBoolean("head_gestures", true) if (!contains("disconnect_when_not_wearing")) putBoolean( - "disconnect_when_not_wearing", - false + "disconnect_when_not_wearing", false ) // AirPods state-based takeover if (!contains("takeover_when_disconnected")) putBoolean( - "takeover_when_disconnected", - true + "takeover_when_disconnected", false ) - if (!contains("takeover_when_idle")) putBoolean("takeover_when_idle", true) + if (!contains("takeover_when_idle")) putBoolean("takeover_when_idle", false) if (!contains("takeover_when_music")) putBoolean("takeover_when_music", false) - if (!contains("takeover_when_call")) putBoolean("takeover_when_call", true) + if (!contains("takeover_when_call")) putBoolean("takeover_when_call", false) // Phone state-based takeover if (!contains("takeover_when_ringing_call")) putBoolean( - "takeover_when_ringing_call", - true + "takeover_when_ringing_call", false ) if (!contains("takeover_when_media_start")) putBoolean( - "takeover_when_media_start", - true + "takeover_when_media_start", false ) if (!contains("adaptive_strength")) putInt("adaptive_strength", 51) if (!contains("tone_volume")) putInt("tone_volume", 75) if (!contains("conversational_awareness_volume")) putInt( - "conversational_awareness_volume", - 43 + "conversational_awareness_volume", 43 ) - if (!contains("textColor")) putLong("textColor", -1L) - if (!contains("qs_click_behavior")) putString("qs_click_behavior", "cycle") if (!contains("name")) putString("name", "AirPods") @@ -497,7 +531,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList initializeConfig() - ancModeReceiver = object : BroadcastReceiver() { + externalBroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (intent?.action == "me.kavishdevar.librepods.SET_ANC_MODE") { if (intent.hasExtra("mode")) { @@ -510,94 +544,94 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } } else { val currentMode = ancNotification.status - val allowOffModeValue = aacpManager.controlCommandStatusList.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.ALLOW_OFF_OPTION } - val allowOffMode = allowOffModeValue?.value?.takeIf { it.isNotEmpty() }?.get(0) == 0x01.toByte() - - val nextMode = if (allowOffMode) { - when (currentMode) { - 1 -> 2 - 2 -> 3 - 3 -> 4 - 4 -> 1 - else -> 1 - } - } else { - when (currentMode) { - 1 -> 2 - 2 -> 3 - 3 -> 4 - 4 -> 2 - else -> 2 - } - } + val configByte = sharedPreferences.getInt("long_press_byte", 0b0111) + val allowOffModeValue = + aacpManager.controlCommandStatusList.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.ALLOW_OFF_OPTION } + val allowOffMode = + allowOffModeValue?.value?.takeIf { it.isNotEmpty() }?.get(0) == 0x01.toByte() || sharedPreferences.getBoolean("off_listening_mode", true) + val nextMode = getNextMode(currentMode = currentMode, configByte = configByte, allowOffMode) aacpManager.sendControlCommand( AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE.value, nextMode ) - Log.d(TAG, "Cycling ANC mode from $currentMode to $nextMode (offListeningMode: $allowOffMode)") + Log.d( + TAG, + "Cycling ANC mode from $currentMode to $nextMode" + ) + } + } else if (intent?.action == "me.kavishdevar.librepods.CONVO_DETECT") { + if (intent.hasExtra("enabled")) { + val enabled = intent.getBooleanExtra("enabled", false) + aacpManager.sendControlCommand( + AACPManager.Companion.ControlCommandIdentifiers.CONVERSATION_DETECT_CONFIG.value, + enabled + ) } } } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - registerReceiver(ancModeReceiver, ancModeFilter, RECEIVER_EXPORTED) + registerReceiver(externalBroadcastReceiver, externalBroadcastFilter, RECEIVER_EXPORTED) } else { - @Suppress("UnspecifiedRegisterReceiverFlag") - registerReceiver(ancModeReceiver, ancModeFilter) + @Suppress("UnspecifiedRegisterReceiverFlag") registerReceiver( + externalBroadcastReceiver, externalBroadcastFilter + ) } - val audioManager = - this@AirPodsService.getSystemService(AUDIO_SERVICE) as AudioManager + val audioManager = this@AirPodsService.getSystemService(AUDIO_SERVICE) as AudioManager MediaController.initialize( - audioManager, - this@AirPodsService.getSharedPreferences( - "settings", - MODE_PRIVATE + audioManager, this@AirPodsService.getSharedPreferences( + "settings", MODE_PRIVATE ) ) - Log.d(TAG, "Initializing CrossDevice") - CoroutineScope(Dispatchers.IO).launch { - CrossDevice.init(this@AirPodsService) - Log.d(TAG, "CrossDevice initialized") - } +// Log.d(TAG, "Initializing CrossDevice") +// CoroutineScope(Dispatchers.IO).launch { +// CrossDevice.init(this@AirPodsService) +// Log.d(TAG, "CrossDevice initialized") +// } sharedPreferences = getSharedPreferences("settings", MODE_PRIVATE) macAddress = sharedPreferences.getString("mac_address", "") ?: "" telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager - phoneStateListener = object : PhoneStateListener() { - @SuppressLint("SwitchIntDef", "NewApi") - override fun onCallStateChanged(state: Int, phoneNumber: String?) { - super.onCallStateChanged(state, phoneNumber) + phoneStateListener = object: TelephonyCallback(), TelephonyCallback.CallStateListener { + override fun onCallStateChanged(state: Int) { when (state) { TelephonyManager.CALL_STATE_RINGING -> { - val leAvailableForAudio = bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true - if ((CrossDevice.isAvailable && !isConnectedLocally && earDetectionNotification.status.contains(0x00)) || leAvailableForAudio) CoroutineScope(Dispatchers.IO).launch { + val leAvailableForAudio = + bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true +// if ((CrossDevice.isAvailable && !isConnectedLocally && earDetectionNotification.status.contains(0x00)) || leAvailableForAudio) CoroutineScope(Dispatchers.IO).launch { + if (leAvailableForAudio) runBlocking { takeOver("call") } if (config.headGestures) { - callNumber = phoneNumber handleIncomingCall() } } + TelephonyManager.CALL_STATE_OFFHOOK -> { - val leAvailableForAudio = bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true - if ((CrossDevice.isAvailable && !isConnectedLocally && earDetectionNotification.status.contains(0x00)) || leAvailableForAudio) CoroutineScope( - Dispatchers.IO).launch { - takeOver("call") + val leAvailableForAudio = + bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true +// if ((CrossDevice.isAvailable && !isConnectedLocally && earDetectionNotification.status.contains(0x00)) || leAvailableForAudio) CoroutineScope( + if (leAvailableForAudio) CoroutineScope( + Dispatchers.IO + ).launch { + takeOver("call") } isInCall = true } + TelephonyManager.CALL_STATE_IDLE -> { isInCall = false - callNumber = null gestureDetector?.stopDetection() } } } } - telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE) + if (checkSelfPermission("android.permission.READ_PHONE_STATE") == PackageManager.PERMISSION_GRANTED) { + telephonyManager.registerTelephonyCallback(mainExecutor, phoneStateListener) + } if (config.showPhoneBatteryInWidget) { widgetMobileBatteryEnabled = true @@ -605,13 +639,12 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList batteryChangedIntentFilter.addAction(AirPodsNotifications.DISCONNECT_RECEIVERS) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { registerReceiver( - BatteryChangedIntentReceiver, - batteryChangedIntentFilter, - RECEIVER_EXPORTED + BatteryChangedIntentReceiver, batteryChangedIntentFilter, RECEIVER_EXPORTED ) } else { - @Suppress("UnspecifiedRegisterReceiverFlag") - registerReceiver(BatteryChangedIntentReceiver, batteryChangedIntentFilter) + @Suppress("UnspecifiedRegisterReceiverFlag") registerReceiver( + BatteryChangedIntentReceiver, batteryChangedIntentFilter + ) } } val serviceIntentFilter = IntentFilter().apply { @@ -625,6 +658,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList addAction("android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT") addAction("android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED") addAction("android.bluetooth.a2dp.profile.action.PLAYING_STATE_CHANGED") + addAction("android.bluetooth.device.action.UUID") } connectionReceiver = object : BroadcastReceiver() { @@ -641,34 +675,44 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList sharedPreferences.edit { putString("name", config.deviceName) } } - Log.d("AirPodsCrossDevice", CrossDevice.isAvailable.toString()) - if (!CrossDevice.isAvailable) { - Log.d(TAG, "${config.deviceName} connected") - CoroutineScope(Dispatchers.IO).launch { - connectToSocket(device!!) - } - Log.d(TAG, "Setting metadata") - setMetadatas(device!!) - isConnectedLocally = true - macAddress = device!!.address - sharedPreferences.edit { - putString("mac_address", macAddress) - } +// Log.d("AirPodsCrossDevice", CrossDevice.isAvailable.toString()) +// if (!CrossDevice.isAvailable) { + Log.d(TAG, "${config.deviceName} connected") + CoroutineScope(Dispatchers.IO).launch { + val bluetoothManager = getSystemService(BluetoothManager::class.java) + connectToSocket(bluetoothManager.adapter, device!!) + } + Log.d(TAG, "Setting metadata") + setMetadatas(device!!) +// isConnectedLocally = true + macAddress = device!!.address + sharedPreferences.edit { + putString("mac_address", macAddress) } +// } + } else if (intent?.action == AirPodsNotifications.AIRPODS_DISCONNECTED) { device = null - isConnectedLocally = false +// isConnectedLocally = false popupShown = false updateNotificationContent(false) - attManager?.disconnect() - attManager = null + aacpManager.disconnected() + attManager.disconnected() + BluetoothConnectionManager.setCurrentConnection(null, null) } } } - val showIslandReceiver = object: BroadcastReceiver() { + val showIslandReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (intent?.action == "me.kavishdevar.librepods.cross_device_island") { - showIsland(this@AirPodsService, batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT}?.level!!.coerceAtMost(batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT}?.level!!)) + showIsland( + this@AirPodsService, + batteryNotification.getBattery() + .find { it.component == BatteryComponent.LEFT }?.level!!.coerceAtMost( + batteryNotification.getBattery() + .find { it.component == BatteryComponent.RIGHT }?.level!! + ) + ) } else if (intent?.action == AirPodsNotifications.DISCONNECT_RECEIVERS) { try { context?.unregisterReceiver(this) @@ -687,8 +731,9 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { registerReceiver(showIslandReceiver, showIslandIntentFilter, RECEIVER_EXPORTED) } else { - @Suppress("UnspecifiedRegisterReceiverFlag") - registerReceiver(showIslandReceiver, showIslandIntentFilter) + @Suppress("UnspecifiedRegisterReceiverFlag") registerReceiver( + showIslandReceiver, showIslandIntentFilter + ) } val deviceIntentFilter = IntentFilter().apply { @@ -700,8 +745,9 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList registerReceiver(connectionReceiver, deviceIntentFilter, RECEIVER_EXPORTED) registerReceiver(bluetoothReceiver, serviceIntentFilter, RECEIVER_EXPORTED) } else { - @Suppress("UnspecifiedRegisterReceiverFlag") - registerReceiver(connectionReceiver, deviceIntentFilter) + @Suppress("UnspecifiedRegisterReceiverFlag") registerReceiver( + connectionReceiver, deviceIntentFilter + ) registerReceiver(bluetoothReceiver, serviceIntentFilter) } @@ -712,42 +758,41 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList if (device.uuids != null) { if (device.uuids.contains(ParcelUuid.fromString("74ec2172-0bad-4d01-8f77-997b2be0722a"))) { bluetoothAdapter.getProfileProxy( - this, - object : BluetoothProfile.ServiceListener { + this, object : BluetoothProfile.ServiceListener { @SuppressLint("NewApi") override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { if (profile == BluetoothProfile.A2DP) { val connectedDevices = proxy.connectedDevices if (connectedDevices.isNotEmpty()) { - if (!CrossDevice.isAvailable) { - CoroutineScope(Dispatchers.IO).launch { - connectToSocket(device) - } - setMetadatas(device) - macAddress = device.address - sharedPreferences.edit { - putString("mac_address", macAddress) - } +// if (!CrossDevice.isAvailable) { + CoroutineScope(Dispatchers.IO).launch { + connectToSocket(bluetoothAdapter, device) } - this@AirPodsService.sendBroadcast( - Intent(AirPodsNotifications.AIRPODS_CONNECTED) - ) + setMetadatas(device) + macAddress = device.address + sharedPreferences.edit { + putString("mac_address", macAddress) + } +// } + sendBroadcast( + Intent(AirPodsNotifications.AIRPODS_CONNECTED).apply { + setPackage(packageName) + }) } } bluetoothAdapter.closeProfileProxy(profile, proxy) } override fun onServiceDisconnected(profile: Int) {} - }, - BluetoothProfile.A2DP + }, BluetoothProfile.A2DP ) } } } - if (!isConnectedLocally && !CrossDevice.isAvailable) { - clearPacketLogs() - } +// if (!isConnectedLocally && !CrossDevice.isAvailable) { +// clearPacketLogs() +// } CoroutineScope(Dispatchers.IO).launch { bleManager.startScanning() @@ -756,7 +801,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList @Suppress("unused") fun cameraOpened() { - Log.d(TAG, "Camera opened, gonna handle stem presses and take action if enabled") + Log.d(TAG, "Camera opened, gonna handle stem presses and take action if visible") cameraActive = true setupStemActions() } @@ -768,8 +813,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } fun isCustomAction( - action: StemAction?, - default: StemAction? + action: StemAction?, default: StemAction? ): Boolean { return action != default } @@ -778,23 +822,29 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList val singlePressDefault = StemAction.defaultActions[StemPressType.SINGLE_PRESS] val doublePressDefault = StemAction.defaultActions[StemPressType.DOUBLE_PRESS] val triplePressDefault = StemAction.defaultActions[StemPressType.TRIPLE_PRESS] - val longPressDefault = StemAction.defaultActions[StemPressType.LONG_PRESS] - - val singlePressCustomized = isCustomAction(config.leftSinglePressAction, singlePressDefault) || - isCustomAction(config.rightSinglePressAction, singlePressDefault) || - (cameraActive && config.cameraAction == StemPressType.SINGLE_PRESS) - val doublePressCustomized = isCustomAction(config.leftDoublePressAction, doublePressDefault) || - isCustomAction(config.rightDoublePressAction, doublePressDefault) - val triplePressCustomized = isCustomAction(config.leftTriplePressAction, triplePressDefault) || - isCustomAction(config.rightTriplePressAction, triplePressDefault) - val longPressCustomized = isCustomAction(config.leftLongPressAction, longPressDefault) || - isCustomAction(config.rightLongPressAction, longPressDefault) || - (cameraActive && config.cameraAction == StemPressType.LONG_PRESS) - Log.d(TAG, "Setting up stem actions: " + - "Single Press Customized: $singlePressCustomized, " + - "Double Press Customized: $doublePressCustomized, " + - "Triple Press Customized: $triplePressCustomized, " + - "Long Press Customized: $longPressCustomized") + val longPressDefault = StemAction.defaultActions[StemPressType.LONG_PRESS] + + val singlePressCustomized = + isCustomAction(config.leftSinglePressAction, singlePressDefault) || isCustomAction( + config.rightSinglePressAction, singlePressDefault + ) || (cameraActive && config.cameraAction == StemPressType.SINGLE_PRESS) + val doublePressCustomized = + isCustomAction(config.leftDoublePressAction, doublePressDefault) || isCustomAction( + config.rightDoublePressAction, doublePressDefault + ) + val triplePressCustomized = + isCustomAction(config.leftTriplePressAction, triplePressDefault) || isCustomAction( + config.rightTriplePressAction, triplePressDefault + ) + val longPressCustomized = isCustomAction( + config.leftLongPressAction, longPressDefault + ) || isCustomAction( + config.rightLongPressAction, longPressDefault + ) || (cameraActive && config.cameraAction == StemPressType.LONG_PRESS) + Log.d( + TAG, + "Setting up stem actions: Single Press Customized: $singlePressCustomized, Double Press Customized: $doublePressCustomized, Triple Press Customized: $triplePressCustomized, Long Press Customized: $longPressCustomized" + ) aacpManager.sendStemConfigPacket( singlePressCustomized, doublePressCustomized, @@ -811,6 +861,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList batteryNotification.setBattery(batteryInfo) sendBroadcast(Intent(AirPodsNotifications.BATTERY_DATA).apply { putParcelableArrayListExtra("data", ArrayList(batteryNotification.getBattery())) + setPackage(packageName) }) updateBattery() updateNotificationContent( @@ -819,8 +870,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList .getString("name", device?.name), batteryNotification.getBattery() ) - CrossDevice.sendRemotePacket(batteryInfo) - CrossDevice.batteryBytes = batteryInfo +// CrossDevice.sendRemotePacket(batteryInfo) +// CrossDevice.batteryBytes = batteryInfo for (battery in batteryNotification.getBattery()) { Log.d( @@ -843,6 +894,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList bytes[0] = list[0] bytes[1] = list[1] putExtra("data", bytes) + }.apply { + setPackage(packageName) }) Log.d( "AirPodsParser", @@ -855,11 +908,13 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList conversationAwarenessNotification.setData(conversationAwareness) sendBroadcast(Intent(AirPodsNotifications.CA_DATA).apply { putExtra("data", conversationAwarenessNotification.status) + }.apply { + setPackage(packageName) }) if (conversationAwarenessNotification.status == 1.toByte() || conversationAwarenessNotification.status == 2.toByte()) { MediaController.startSpeaking() - } else if (conversationAwarenessNotification.status == 8.toByte() || conversationAwarenessNotification.status == 9.toByte()) { + } else if (conversationAwarenessNotification.status == 6.toByte() ||conversationAwarenessNotification.status == 8.toByte() || conversationAwarenessNotification.status == 9.toByte()) { MediaController.stopSpeaking() } @@ -872,7 +927,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList override fun onControlCommandReceived(controlCommand: ByteArray) { val command = AACPManager.ControlCommand.fromByteArray(controlCommand) if (command.identifier == AACPManager.Companion.ControlCommandIdentifiers.LISTENING_MODE.value) { - ancNotification.setStatus(byteArrayOf(command.value.takeIf { it.isNotEmpty() }?.get(0) ?: 0x00.toByte())) + ancNotification.setStatus(byteArrayOf(command.value.takeIf { it.isNotEmpty() } + ?.get(0) ?: 0x00.toByte())) sendANCBroadcast() updateNoiseControlWidget() } @@ -889,8 +945,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList MediaController.pausedForOtherDevice = true otherDeviceTookOver = true disconnectAudio( - this@AirPodsService, - device + this@AirPodsService, device ) } } @@ -899,16 +954,19 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList // TODO: Show a reverse button, but that's a lot of effort -- i'd have to change the UI too, which i hate doing, and handle other device's reverses too, and disconnect audio etc... so for now, just pause the audio and show the island without asking to reverse. // handling reverse is a problem because we'd have to disconnect the audio, but there's no option connect audio again natively, so notification would have to be changed. I wish there was a way to just "change the audio output device". // (20 minutes later) i've done it nonetheless :] - val senderName = aacpManager.connectedDevices.find { it.mac == sender }?.type ?: "Other device" - Log.d(TAG, "other device has hijacked the connection, reasonReverseTapped: $reasonReverseTapped") + val senderName = + aacpManager.connectedDevices.find { it.mac == sender }?.type ?: "Other device" + Log.d( + TAG, + "other device has hijacked the connection, reasonReverseTapped: $reasonReverseTapped" + ) aacpManager.sendControlCommand( AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION.value, byteArrayOf(0x00) ) otherDeviceTookOver = true disconnectAudio( - this@AirPodsService, - device + this@AirPodsService, device ) if (reasonReverseTapped) { Log.d(TAG, "reverse tapped, disconnecting audio") @@ -916,7 +974,12 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList disconnectAudio(this@AirPodsService, device) showIsland( this@AirPodsService, - (batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT}?.level?: 0).coerceAtMost(batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT}?.level?: 0), + (batteryNotification.getBattery() + .find { it.component == BatteryComponent.LEFT }?.level + ?: 0).coerceAtMost( + batteryNotification.getBattery() + .find { it.component == BatteryComponent.RIGHT }?.level ?: 0 + ), IslandType.MOVED_TO_OTHER_DEVICE, reversed = true, otherDeviceName = senderName @@ -925,7 +988,12 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList if (!aacpManager.owns) { showIsland( this@AirPodsService, - (batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT}?.level?: 0).coerceAtMost(batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT}?.level?: 0), + (batteryNotification.getBattery() + .find { it.component == BatteryComponent.LEFT }?.level + ?: 0).coerceAtMost( + batteryNotification.getBattery() + .find { it.component == BatteryComponent.RIGHT }?.level ?: 0 + ), IslandType.MOVED_TO_OTHER_DEVICE, reversed = reasonReverseTapped, otherDeviceName = senderName @@ -935,10 +1003,15 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } override fun onShowNearbyUI(sender: String) { - val senderName = aacpManager.connectedDevices.find { it.mac == sender }?.type ?: "Other device" + val senderName = + aacpManager.connectedDevices.find { it.mac == sender }?.type ?: "Other device" showIsland( this@AirPodsService, - (batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT}?.level?: 0).coerceAtMost(batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT}?.level?: 0), + (batteryNotification.getBattery() + .find { it.component == BatteryComponent.LEFT }?.level ?: 0).coerceAtMost( + batteryNotification.getBattery() + .find { it.component == BatteryComponent.RIGHT }?.level ?: 0 + ), IslandType.MOVED_TO_OTHER_DEVICE, reversed = false, otherDeviceName = senderName @@ -952,7 +1025,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList ) // Store in SharedPreferences sharedPreferences.edit { - putString("airpods_name", deviceInformation.name) + putString("name", deviceInformation.name) putString("airpods_model_number", deviceInformation.modelNumber) putString("airpods_manufacturer", deviceInformation.manufacturer) putString("airpods_serial_number", deviceInformation.serialNumber) @@ -989,10 +1062,14 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList version1 = config.airpodsVersion1, version2 = config.airpodsVersion2, version3 = config.airpodsVersion3, - aacpManager = aacpManager, - attManager = attManager ) + if (device != null) setMetadatas(device!!) } + sendBroadcast( + Intent(AirPodsNotifications.AIRPODS_INFORMATION_UPDATED).setPackage( + packageName + ) + ) } @SuppressLint("NewApi") @@ -1015,21 +1092,32 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } override fun onStemPressReceived(stemPress: ByteArray) { + val (stemPressType, bud) = aacpManager.parseStemPressResponse(stemPress) - Log.d("AirPodsParser", "Stem press received: $stemPressType on $bud, cameraActive: $cameraActive, cameraAction: ${config.cameraAction}") + Log.d( + "AirPodsParser", + "Stem press received: $stemPressType on $bud, cameraActive: $cameraActive, cameraAction: ${config.cameraAction}" + ) if (cameraActive && config.cameraAction != null && stemPressType == config.cameraAction) { - Runtime.getRuntime().exec(arrayOf("su", "-c", "input keyevent 27")) + Runtime.getRuntime().exec(arrayOf("su", "-c", "input keyevent 27")) } else { val action = getActionFor(bud, stemPressType) Log.d("AirPodsParser", "$bud $stemPressType action: $action") action?.let { executeStemAction(it) } } } + override fun onAudioSourceReceived(audioSource: ByteArray) { - Log.d("AirPodsParser", "Audio source changed mac: ${aacpManager.audioSource?.mac}, type: ${aacpManager.audioSource?.type?.name}") - if (aacpManager.audioSource?.type != AACPManager.Companion.AudioSourceType.NONE && aacpManager.audioSource?.mac != localMac) { - Log.d("AirPodsParser", "Audio source is another device, better to give up aacp control") + Log.d( + "AirPodsParser", + "Audio source changed mac: ${aacpManager.audioSource?.mac}, type: ${aacpManager.audioSource?.type?.name}" + ) + if (localMac!="" && (aacpManager.audioSource?.type != AACPManager.Companion.AudioSourceType.NONE && aacpManager.audioSource?.mac != localMac)) { + Log.d( + "AirPodsParser", + "Audio source is another device, better to give up aacp control" + ) aacpManager.sendControlCommand( AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION.value, byteArrayOf(0x00) @@ -1043,28 +1131,63 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList override fun onConnectedDevicesReceived(connectedDevices: List) { for (device in connectedDevices) { - Log.d("AirPodsParser", "Connected device: ${device.mac}, info1: ${device.info1}, info2: ${device.info2})") + Log.d( + "AirPodsParser", + "Connected device: ${device.mac}, info1: ${device.info1}, info2: ${device.info2})" + ) } val newDevices = connectedDevices.filter { newDevice -> - val notInOld = aacpManager.oldConnectedDevices.none { oldDevice -> oldDevice.mac == newDevice.mac } + val notInOld = + aacpManager.oldConnectedDevices.none { oldDevice -> oldDevice.mac == newDevice.mac } val notLocal = newDevice.mac != localMac notInOld && notLocal } for (device in newDevices) { - Log.d("AirPodsParser", "New connected device: ${device.mac}, info1: ${device.info1}, info2: ${device.info2})") - Log.d(TAG, "Sending new Tipi packet for device ${device.mac}, and sending media info to the device") - aacpManager.sendMediaInformationNewDevice(selfMacAddress = localMac, targetMacAddress = device.mac) - aacpManager.sendAddTiPiDevice(selfMacAddress = localMac, targetMacAddress = device.mac) + Log.d( + "AirPodsParser", + "New connected device: ${device.mac}, info1: ${device.info1}, info2: ${device.info2})" + ) + Log.d( + TAG, + "Sending new Tipi packet for device ${device.mac}, and sending media info to the device" + ) + aacpManager.sendMediaInformationNewDevice( + selfMacAddress = localMac, targetMacAddress = device.mac + ) + aacpManager.sendAddTiPiDevice( + selfMacAddress = localMac, targetMacAddress = device.mac + ) } } + + override fun onHeadphoneAccommodationReceived(eqData: FloatArray) { + sendBroadcast( + Intent(AirPodsNotifications.EQ_DATA).putExtra("eqData", eqData).apply { + setPackage(packageName) + }) + } + + override fun onCustomEqReceived(customEq: CustomEq) { + // TODO + } + + override fun onCapabilitiesReceived(capabilities: List) { + // TODO + } + override fun onUnknownPacketReceived(packet: ByteArray) { - Log.d("AACPManager", "Unknown packet received: ${packet.joinToString(" ") { "%02X".format(it) }}") + Log.d( + "AACPManager", + "Unknown packet received: ${packet.joinToString(" ") { "%02X".format(it) }}" + ) } }) } - private fun getActionFor(bud: AACPManager.Companion.StemPressBudType, type: StemPressType): StemAction? { + private fun getActionFor( + bud: AACPManager.Companion.StemPressBudType, type: StemPressType + ): StemAction? { return when (type) { StemPressType.SINGLE_PRESS -> if (bud == AACPManager.Companion.StemPressBudType.LEFT) config.leftSinglePressAction else config.rightSinglePressAction StemPressType.DOUBLE_PRESS -> if (bud == AACPManager.Companion.StemPressBudType.LEFT) config.leftDoublePressAction else config.rightDoublePressAction @@ -1076,8 +1199,11 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList private fun executeStemAction(action: StemAction) { when (action) { StemAction.defaultActions[StemPressType.SINGLE_PRESS] -> { - Log.d("AirPodsParser", "Default single press action: Play/Pause, not taking action.") + Log.d( + "AirPodsParser", "Default single press action: Play/Pause, not taking action." + ) } + StemAction.PLAY_PAUSE -> MediaController.sendPlayPause() StemAction.PREVIOUS_TRACK -> MediaController.sendPreviousTrack() StemAction.NEXT_TRACK -> MediaController.sendNextTrack() @@ -1088,19 +1214,28 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } startActivity(intent) } else { - Log.w("AirPodsParser", "Digital Assistant action is not supported on this Android version.") + Log.w( + "AirPodsParser", + "Digital Assistant action is not supported on this Android version." + ) } } + StemAction.CYCLE_NOISE_CONTROL_MODES -> { Log.d("AirPodsParser", "Cycling noise control modes") - sendBroadcast(Intent("me.kavishdevar.librepods.SET_ANC_MODE")) + sendBroadcast(Intent("me.kavishdevar.librepods.SET_ANC_MODE").apply { + setPackage(packageName) + }) } } } private fun processEarDetectionChange(earDetection: ByteArray) { var inEar: Boolean - val inEarData = listOf(earDetectionNotification.status[0] == 0x00.toByte(), earDetectionNotification.status[1] == 0x00.toByte()) + val inEarData = listOf( + earDetectionNotification.status[0] == 0x00.toByte(), + earDetectionNotification.status[1] == 0x00.toByte() + ) var justEnabledA2dp = false earDetectionNotification.setStatus(earDetection) if (config.earDetectionEnabled) { @@ -1108,14 +1243,21 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList inEar = data[0] == 0x00.toByte() && data[1] == 0x00.toByte() val newInEarData = listOf( - data[0] == 0x00.toByte(), - data[1] == 0x00.toByte() + data[0] == 0x00.toByte(), data[1] == 0x00.toByte() ) - if (inEarData.sorted() == listOf(false, false) && newInEarData.sorted() != listOf(false, false) && islandWindow?.isVisible != true) { + if (inEarData.sorted() == listOf(false, false) && newInEarData.sorted() != listOf( + false, false + ) && islandWindow?.isVisible != true + ) { showIsland( this@AirPodsService, - (batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT}?.level?: 0).coerceAtMost(batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT}?.level?: 0)) + (batteryNotification.getBattery() + .find { it.component == BatteryComponent.LEFT }?.level ?: 0).coerceAtMost( + batteryNotification.getBattery() + .find { it.component == BatteryComponent.RIGHT }?.level ?: 0 + ) + ) } if (newInEarData == listOf(false, false) && islandWindow?.isVisible == true) { @@ -1135,6 +1277,14 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList disconnectAudio(this@AirPodsService, device) } } + val wasNone = inEarData == listOf(false, false) + val nowSingle = newInEarData.count { it } == 1 + + if (wasNone && nowSingle) { + MediaController.sendPlay() + MediaController.iPausedTheMedia = false + return + } if (inEarData.contains(false) && newInEarData == listOf(true, true)) { Log.d("AirPodsParser", "User put in both AirPods from just one.") @@ -1146,7 +1296,10 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList MediaController.userPlayedTheMedia = false } - Log.d("AirPodsParser", "inEarData: ${inEarData.sorted()}, newInEarData: ${newInEarData.sorted()}") + Log.d( + "AirPodsParser", + "inEarData: ${inEarData.sorted()}, newInEarData: ${newInEarData.sorted()}" + ) if (newInEarData.sorted() != inEarData.sorted()) { if (inEar) { @@ -1165,15 +1318,21 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList val a2dpConnectionStateReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (intent.action == "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED") { - val state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_DISCONNECTED) - val previousState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_DISCONNECTED) - val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) + val state = intent.getIntExtra( + BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_DISCONNECTED + ) + val previousState = intent.getIntExtra( + BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_DISCONNECTED + ) + val device = + intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) - Log.d("MediaController", "A2DP state changed: $previousState -> $state for device: ${device?.address}") + Log.d( + "MediaController", + "A2DP state changed: $previousState -> $state for device: ${device?.address}" + ) - if (state == BluetoothProfile.STATE_CONNECTED && - previousState != BluetoothProfile.STATE_CONNECTED && - device?.address == this@AirPodsService.device?.address) { + if (state == BluetoothProfile.STATE_CONNECTED && previousState != BluetoothProfile.STATE_CONNECTED && device?.address == this@AirPodsService.device?.address) { Log.d("MediaController", "A2DP connected, sending play command") MediaController.sendPlay() @@ -1185,7 +1344,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } } - val a2dpIntentFilter = IntentFilter("android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED") + val a2dpIntentFilter = + IntentFilter("android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { registerReceiver(a2dpConnectionStateReceiver, a2dpIntentFilter, RECEIVER_EXPORTED) } else { @@ -1197,83 +1357,155 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList config = ServiceConfig( deviceName = sharedPreferences.getString("name", "AirPods") ?: "AirPods", earDetectionEnabled = sharedPreferences.getBoolean("automatic_ear_detection", true), - conversationalAwarenessPauseMusic = sharedPreferences.getBoolean("conversational_awareness_pause_music", false), - showPhoneBatteryInWidget = sharedPreferences.getBoolean("show_phone_battery_in_widget", true), - relativeConversationalAwarenessVolume = sharedPreferences.getBoolean("relative_conversational_awareness_volume", true), + conversationalAwarenessPauseMusic = sharedPreferences.getBoolean( + "conversational_awareness_pause_music", false + ), + showPhoneBatteryInWidget = sharedPreferences.getBoolean( + "show_phone_battery_in_widget", true + ), + relativeConversationalAwarenessVolume = sharedPreferences.getBoolean( + "relative_conversational_awareness_volume", true + ), headGestures = sharedPreferences.getBoolean("head_gestures", true), - disconnectWhenNotWearing = sharedPreferences.getBoolean("disconnect_when_not_wearing", false), - conversationalAwarenessVolume = sharedPreferences.getInt("conversational_awareness_volume", 43), - textColor = sharedPreferences.getLong("textColor", -1L), + disconnectWhenNotWearing = sharedPreferences.getBoolean( + "disconnect_when_not_wearing", false + ), + conversationalAwarenessVolume = sharedPreferences.getInt( + "conversational_awareness_volume", 43 + ), qsClickBehavior = sharedPreferences.getString("qs_click_behavior", "cycle") ?: "cycle", // AirPods state-based takeover - takeoverWhenDisconnected = sharedPreferences.getBoolean("takeover_when_disconnected", true), - takeoverWhenIdle = sharedPreferences.getBoolean("takeover_when_idle", true), + takeoverWhenDisconnected = sharedPreferences.getBoolean( + "takeover_when_disconnected", false + ), + takeoverWhenIdle = sharedPreferences.getBoolean("takeover_when_idle", false), takeoverWhenMusic = sharedPreferences.getBoolean("takeover_when_music", false), - takeoverWhenCall = sharedPreferences.getBoolean("takeover_when_call", true), + takeoverWhenCall = sharedPreferences.getBoolean("takeover_when_call", false), // Phone state-based takeover - takeoverWhenRingingCall = sharedPreferences.getBoolean("takeover_when_ringing_call", true), - takeoverWhenMediaStart = sharedPreferences.getBoolean("takeover_when_media_start", true), + takeoverWhenRingingCall = sharedPreferences.getBoolean( + "takeover_when_ringing_call", false + ), + takeoverWhenMediaStart = sharedPreferences.getBoolean( + "takeover_when_media_start", false + ), // Stem actions - leftSinglePressAction = StemAction.fromString(sharedPreferences.getString("left_single_press_action", "PLAY_PAUSE") ?: "PLAY_PAUSE")!!, - rightSinglePressAction = StemAction.fromString(sharedPreferences.getString("right_single_press_action", "PLAY_PAUSE") ?: "PLAY_PAUSE")!!, - - leftDoublePressAction = StemAction.fromString(sharedPreferences.getString("left_double_press_action", "PREVIOUS_TRACK") ?: "NEXT_TRACK")!!, - rightDoublePressAction = StemAction.fromString(sharedPreferences.getString("right_double_press_action", "NEXT_TRACK") ?: "NEXT_TRACK")!!, - - leftTriplePressAction = StemAction.fromString(sharedPreferences.getString("left_triple_press_action", "PREVIOUS_TRACK") ?: "PREVIOUS_TRACK")!!, - rightTriplePressAction = StemAction.fromString(sharedPreferences.getString("right_triple_press_action", "PREVIOUS_TRACK") ?: "PREVIOUS_TRACK")!!, - - leftLongPressAction = StemAction.fromString(sharedPreferences.getString("left_long_press_action", "CYCLE_NOISE_CONTROL_MODES") ?: "CYCLE_NOISE_CONTROL_MODES")!!, - rightLongPressAction = StemAction.fromString(sharedPreferences.getString("right_long_press_action", "DIGITAL_ASSISTANT") ?: "DIGITAL_ASSISTANT")!!, - - cameraAction = sharedPreferences.getString("camera_action", null)?.let { AACPManager.Companion.StemPressType.valueOf(it) }, + leftSinglePressAction = StemAction.fromString( + sharedPreferences.getString( + "left_single_press_action", "PLAY_PAUSE" + ) ?: "PLAY_PAUSE" + )!!, + rightSinglePressAction = StemAction.fromString( + sharedPreferences.getString( + "right_single_press_action", "PLAY_PAUSE" + ) ?: "PLAY_PAUSE" + )!!, + + leftDoublePressAction = StemAction.fromString( + sharedPreferences.getString( + "left_double_press_action", "PREVIOUS_TRACK" + ) ?: "NEXT_TRACK" + )!!, + rightDoublePressAction = StemAction.fromString( + sharedPreferences.getString( + "right_double_press_action", "NEXT_TRACK" + ) ?: "NEXT_TRACK" + )!!, + + leftTriplePressAction = StemAction.fromString( + sharedPreferences.getString( + "left_triple_press_action", "PREVIOUS_TRACK" + ) ?: "PREVIOUS_TRACK" + )!!, + rightTriplePressAction = StemAction.fromString( + sharedPreferences.getString( + "right_triple_press_action", "PREVIOUS_TRACK" + ) ?: "PREVIOUS_TRACK" + )!!, + + leftLongPressAction = StemAction.fromString( + sharedPreferences.getString( + "left_long_press_action", "CYCLE_NOISE_CONTROL_MODES" + ) ?: "CYCLE_NOISE_CONTROL_MODES" + )!!, + rightLongPressAction = StemAction.fromString( + sharedPreferences.getString( + "right_long_press_action", "DIGITAL_ASSISTANT" + ) ?: "DIGITAL_ASSISTANT" + )!!, + + cameraAction = sharedPreferences.getString("camera_action", null) + ?.let { StemPressType.valueOf(it) }, // AirPods device information airpodsName = sharedPreferences.getString("airpods_name", "") ?: "", airpodsModelNumber = sharedPreferences.getString("airpods_model_number", "") ?: "", airpodsManufacturer = sharedPreferences.getString("airpods_manufacturer", "") ?: "", airpodsSerialNumber = sharedPreferences.getString("airpods_serial_number", "") ?: "", - airpodsLeftSerialNumber = sharedPreferences.getString("airpods_left_serial_number", "") ?: "", - airpodsRightSerialNumber = sharedPreferences.getString("airpods_right_serial_number", "") ?: "", + airpodsLeftSerialNumber = sharedPreferences.getString("airpods_left_serial_number", "") + ?: "", + airpodsRightSerialNumber = sharedPreferences.getString( + "airpods_right_serial_number", "" + ) ?: "", airpodsVersion1 = sharedPreferences.getString("airpods_version1", "") ?: "", airpodsVersion2 = sharedPreferences.getString("airpods_version2", "") ?: "", airpodsVersion3 = sharedPreferences.getString("airpods_version3", "") ?: "", - airpodsHardwareRevision = sharedPreferences.getString("airpods_hardware_revision", "") ?: "", - airpodsUpdaterIdentifier = sharedPreferences.getString("airpods_updater_identifier", "") ?: "", + airpodsHardwareRevision = sharedPreferences.getString("airpods_hardware_revision", "") + ?: "", + airpodsUpdaterIdentifier = sharedPreferences.getString("airpods_updater_identifier", "") + ?: "", + + selfMacAddress = sharedPreferences.getString("self_mac_address", "") ?: "" ) } override fun onSharedPreferenceChanged(preferences: SharedPreferences?, key: String?) { if (preferences == null || key == null) return - when(key) { + when (key) { "name" -> config.deviceName = preferences.getString(key, "AirPods") ?: "AirPods" - "automatic_ear_detection" -> config.earDetectionEnabled = preferences.getBoolean(key, true) - "conversational_awareness_pause_music" -> config.conversationalAwarenessPauseMusic = preferences.getBoolean(key, false) + "mac_address" -> macAddress = preferences.getString(key, "") ?: "" + "automatic_ear_detection" -> config.earDetectionEnabled = + preferences.getBoolean(key, true) + + "conversational_awareness_pause_music" -> config.conversationalAwarenessPauseMusic = + preferences.getBoolean(key, false) + "show_phone_battery_in_widget" -> { config.showPhoneBatteryInWidget = preferences.getBoolean(key, true) widgetMobileBatteryEnabled = config.showPhoneBatteryInWidget updateBattery() } - "relative_conversational_awareness_volume" -> config.relativeConversationalAwarenessVolume = preferences.getBoolean(key, true) + + "relative_conversational_awareness_volume" -> config.relativeConversationalAwarenessVolume = + preferences.getBoolean(key, true) + "head_gestures" -> config.headGestures = preferences.getBoolean(key, true) - "disconnect_when_not_wearing" -> config.disconnectWhenNotWearing = preferences.getBoolean(key, false) - "conversational_awareness_volume" -> config.conversationalAwarenessVolume = preferences.getInt(key, 43) - "textColor" -> config.textColor = preferences.getLong(key, -1L) - "qs_click_behavior" -> config.qsClickBehavior = preferences.getString(key, "cycle") ?: "cycle" + "disconnect_when_not_wearing" -> config.disconnectWhenNotWearing = + preferences.getBoolean(key, false) + + "conversational_awareness_volume" -> config.conversationalAwarenessVolume = + preferences.getInt(key, 43) + + "qs_click_behavior" -> config.qsClickBehavior = + preferences.getString(key, "cycle") ?: "cycle" // AirPods state-based takeover - "takeover_when_disconnected" -> config.takeoverWhenDisconnected = preferences.getBoolean(key, true) + "takeover_when_disconnected" -> config.takeoverWhenDisconnected = + preferences.getBoolean(key, true) + "takeover_when_idle" -> config.takeoverWhenIdle = preferences.getBoolean(key, true) "takeover_when_music" -> config.takeoverWhenMusic = preferences.getBoolean(key, false) "takeover_when_call" -> config.takeoverWhenCall = preferences.getBoolean(key, true) // Phone state-based takeover - "takeover_when_ringing_call" -> config.takeoverWhenRingingCall = preferences.getBoolean(key, true) - "takeover_when_media_start" -> config.takeoverWhenMediaStart = preferences.getBoolean(key, true) + "takeover_when_ringing_call" -> config.takeoverWhenRingingCall = + preferences.getBoolean(key, true) + + "takeover_when_media_start" -> config.takeoverWhenMediaStart = + preferences.getBoolean(key, true) "left_single_press_action" -> { config.leftSinglePressAction = StemAction.fromString( @@ -1281,66 +1513,87 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList )!! setupStemActions() } + "right_single_press_action" -> { config.rightSinglePressAction = StemAction.fromString( preferences.getString(key, "PLAY_PAUSE") ?: "PLAY_PAUSE" )!! setupStemActions() } + "left_double_press_action" -> { config.leftDoublePressAction = StemAction.fromString( preferences.getString(key, "PREVIOUS_TRACK") ?: "PREVIOUS_TRACK" )!! setupStemActions() } + "right_double_press_action" -> { config.rightDoublePressAction = StemAction.fromString( preferences.getString(key, "NEXT_TRACK") ?: "NEXT_TRACK" )!! setupStemActions() } + "left_triple_press_action" -> { config.leftTriplePressAction = StemAction.fromString( preferences.getString(key, "PREVIOUS_TRACK") ?: "PREVIOUS_TRACK" )!! setupStemActions() } + "right_triple_press_action" -> { config.rightTriplePressAction = StemAction.fromString( preferences.getString(key, "PREVIOUS_TRACK") ?: "PREVIOUS_TRACK" )!! setupStemActions() } + "left_long_press_action" -> { config.leftLongPressAction = StemAction.fromString( - preferences.getString(key, "CYCLE_NOISE_CONTROL_MODES") ?: "CYCLE_NOISE_CONTROL_MODES" + preferences.getString(key, "CYCLE_NOISE_CONTROL_MODES") + ?: "CYCLE_NOISE_CONTROL_MODES" )!! setupStemActions() } + "right_long_press_action" -> { config.rightLongPressAction = StemAction.fromString( preferences.getString(key, "DIGITAL_ASSISTANT") ?: "DIGITAL_ASSISTANT" )!! setupStemActions() } - "camera_action" -> config.cameraAction = preferences.getString(key, null)?.let { AACPManager.Companion.StemPressType.valueOf(it) } + + "camera_action" -> config.cameraAction = + preferences.getString(key, null)?.let { StemPressType.valueOf(it) } // AirPods device information "airpods_name" -> config.airpodsName = preferences.getString(key, "") ?: "" - "airpods_model_number" -> config.airpodsModelNumber = preferences.getString(key, "") ?: "" - "airpods_manufacturer" -> config.airpodsManufacturer = preferences.getString(key, "") ?: "" - "airpods_serial_number" -> config.airpodsSerialNumber = preferences.getString(key, "") ?: "" - "airpods_left_serial_number" -> config.airpodsLeftSerialNumber = preferences.getString(key, "") ?: "" - "airpods_right_serial_number" -> config.airpodsRightSerialNumber = preferences.getString(key, "") ?: "" + "airpods_model_number" -> config.airpodsModelNumber = + preferences.getString(key, "") ?: "" + + "airpods_manufacturer" -> config.airpodsManufacturer = + preferences.getString(key, "") ?: "" + + "airpods_serial_number" -> config.airpodsSerialNumber = + preferences.getString(key, "") ?: "" + + "airpods_left_serial_number" -> config.airpodsLeftSerialNumber = + preferences.getString(key, "") ?: "" + + "airpods_right_serial_number" -> config.airpodsRightSerialNumber = + preferences.getString(key, "") ?: "" + "airpods_version1" -> config.airpodsVersion1 = preferences.getString(key, "") ?: "" "airpods_version2" -> config.airpodsVersion2 = preferences.getString(key, "") ?: "" "airpods_version3" -> config.airpodsVersion3 = preferences.getString(key, "") ?: "" - "airpods_hardware_revision" -> config.airpodsHardwareRevision = preferences.getString(key, "") ?: "" - "airpods_updater_identifier" -> config.airpodsUpdaterIdentifier = preferences.getString(key, "") ?: "" - } + "airpods_hardware_revision" -> config.airpodsHardwareRevision = + preferences.getString(key, "") ?: "" + + "airpods_updater_identifier" -> config.airpodsUpdaterIdentifier = + preferences.getString(key, "") ?: "" - if (key == "mac_address") { - macAddress = preferences.getString(key, "") ?: "" + "self_mac_address" -> config.selfMacAddress = preferences.getString(key, "") ?: "" } } @@ -1360,8 +1613,9 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } CoroutineScope(Dispatchers.IO).launch { - val logs = sharedPreferencesLogs.getStringSet(packetLogKey, mutableSetOf())?.toMutableSet() - ?: mutableSetOf() + val logs = + sharedPreferencesLogs.getStringSet(packetLogKey, mutableSetOf())?.toMutableSet() + ?: mutableSetOf() logs.add(logEntry) if (logs.size > maxLogEntries) { @@ -1403,6 +1657,9 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList var popupShown = false fun showPopup(service: Service, name: String) { + if (!sharedPreferences.getBoolean("show_bottom_sheet_popup", true)) { + return + } if (!Settings.canDrawOverlays(service)) { Log.d(TAG, "No permission for SYSTEM_ALERT_WINDOW") return @@ -1417,16 +1674,33 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList var islandOpen = false var islandWindow: IslandWindow? = null + @SuppressLint("MissingPermission") - fun showIsland(service: Service, batteryPercentage: Int, type: IslandType = IslandType.CONNECTED, reversed: Boolean = false, otherDeviceName: String? = null) { + fun showIsland( + service: Service, + batteryPercentage: Int, + type: IslandType = IslandType.CONNECTED, + reversed: Boolean = false, + otherDeviceName: String? = null + ) { Log.d(TAG, "Showing island window") + if (!sharedPreferences.getBoolean("show_island_popup", true)) { + return + } if (!Settings.canDrawOverlays(service)) { Log.d(TAG, "No permission for SYSTEM_ALERT_WINDOW") return } CoroutineScope(Dispatchers.Main).launch { islandWindow = IslandWindow(service.applicationContext) - islandWindow!!.show(sharedPreferences.getString("name", "AirPods Pro").toString(), batteryPercentage, this@AirPodsService, type, reversed, otherDeviceName) + islandWindow!!.show( + sharedPreferences.getString("name", "AirPods Pro").toString(), + batteryPercentage, + this@AirPodsService, + type, + reversed, + otherDeviceName + ) } } @@ -1437,7 +1711,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList startActivity(intent) } - var isConnectedLocally = false + // var isConnectedLocally = false var device: BluetoothDevice? = null private lateinit var earReceiver: BroadcastReceiver @@ -1462,7 +1736,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList val disconnectedNotificationChannel = NotificationChannel( "background_service_status", "Background Service Status", - NotificationManager.IMPORTANCE_LOW + NotificationManager.IMPORTANCE_NONE ) val connectedNotificationChannel = NotificationChannel( @@ -1473,7 +1747,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList val socketFailureChannel = NotificationChannel( "socket_connection_failure", - "AirPods Socket Connection Issues", + "AirPods BluetoothConnectionManager.getAACPSocket()? Connection Issues", NotificationManager.IMPORTANCE_HIGH ).apply { description = "Notifications about problems connecting to AirPods protocol" @@ -1487,10 +1761,11 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList notificationManager.createNotificationChannel(connectedNotificationChannel) notificationManager.createNotificationChannel(socketFailureChannel) - val notificationSettingsIntent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply { - putExtra(Settings.EXTRA_APP_PACKAGE, packageName) - putExtra(Settings.EXTRA_CHANNEL_ID, "background_service_status") - } + val notificationSettingsIntent = + Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply { + putExtra(Settings.EXTRA_APP_PACKAGE, packageName) + putExtra(Settings.EXTRA_CHANNEL_ID, "background_service_status") + } val pendingIntentNotifDisable = PendingIntent.getActivity( this, 0, @@ -1499,14 +1774,10 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList ) val notification = NotificationCompat.Builder(this, "background_service_status") - .setSmallIcon(R.drawable.airpods) - .setContentTitle("Background Service Running") + .setSmallIcon(R.drawable.airpods).setContentTitle("Background Service Running") .setContentText("Useless notification, disable it by clicking on it.") - .setContentIntent(pendingIntentNotifDisable) - .setCategory(Notification.CATEGORY_SERVICE) - .setPriority(NotificationCompat.PRIORITY_LOW) - .setOngoing(true) - .build() + .setContentIntent(pendingIntentNotifDisable).setCategory(Notification.CATEGORY_SERVICE) + .setPriority(NotificationCompat.PRIORITY_LOW).setOngoing(true).build() try { startForeground(1, notification) @@ -1515,8 +1786,17 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } } + @Suppress("KotlinUnreachableCode") @OptIn(ExperimentalMaterial3Api::class) private fun showSocketConnectionFailureNotification(errorMessage: String) { + return // something causes too many notifications. turning off for now + if (BuildConfig.FLAVOR != "xposed") { + Log.w( + TAG, + "Not showing BluetoothConnectionManager.getAACPSocket()? error notification to user, the service shouldn't be running if it isn't supported." + ) + return + } val notificationManager = getSystemService(NotificationManager::class.java) val notificationIntent = Intent(this, MainActivity::class.java) @@ -1528,17 +1808,13 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList ) val notification = NotificationCompat.Builder(this, "socket_connection_failure") - .setSmallIcon(R.drawable.airpods) - .setContentTitle("AirPods Connection Issue") - .setContentText("Unable to connect to AirPods over L2CAP") - .setStyle(NotificationCompat.BigTextStyle() - .bigText("Your AirPods are connected via Bluetooth, but LibrePods couldn't connect to AirPods using L2CAP. " + - "Error: $errorMessage")) - .setContentIntent(pendingIntent) - .setCategory(Notification.CATEGORY_ERROR) - .setPriority(NotificationCompat.PRIORITY_HIGH) - .setAutoCancel(true) - .build() + .setSmallIcon(R.drawable.airpods).setContentTitle("AirPods Connection Issue") + .setContentText("Unable to connect to AirPods over L2CAP").setStyle( + NotificationCompat.BigTextStyle().bigText( + "Your AirPods are connected via Bluetooth, but LibrePods couldn't connect to AirPods using L2CAP. Error: $errorMessage" + ) + ).setContentIntent(pendingIntent).setCategory(Notification.CATEGORY_ERROR) + .setPriority(NotificationCompat.PRIORITY_HIGH).setAutoCancel(true).build() notificationManager.notify(3, notification) } @@ -1546,12 +1822,15 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList fun sendANCBroadcast() { sendBroadcast(Intent(AirPodsNotifications.ANC_DATA).apply { putExtra("data", ancNotification.status) + setPackage(packageName) }) } fun sendBatteryBroadcast() { + broadcastBatteryInformation() sendBroadcast(Intent(AirPodsNotifications.BATTERY_DATA).apply { putParcelableArrayListExtra("data", ArrayList(batteryNotification.getBattery())) + setPackage(packageName) }) } @@ -1565,37 +1844,51 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } fun setBatteryMetadata() { - device?.let { it -> - SystemApisUtils.setMetadata( - it, - it.METADATA_UNTETHERED_CASE_BATTERY, - batteryNotification.getBattery().find { it.component == BatteryComponent.CASE }?.level.toString().toByteArray() - ) - SystemApisUtils.setMetadata( - it, - it.METADATA_UNTETHERED_CASE_CHARGING, - (if (batteryNotification.getBattery().find { it.component == BatteryComponent.CASE}?.status == BatteryStatus.CHARGING) "1".toByteArray() else "0".toByteArray()) - ) - SystemApisUtils.setMetadata( - it, - it.METADATA_UNTETHERED_LEFT_BATTERY, - batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT }?.level.toString().toByteArray() - ) - SystemApisUtils.setMetadata( - it, - it.METADATA_UNTETHERED_LEFT_CHARGING, - (if (batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT}?.status == BatteryStatus.CHARGING) "1".toByteArray() else "0".toByteArray()) - ) - SystemApisUtils.setMetadata( - it, - it.METADATA_UNTETHERED_RIGHT_BATTERY, - batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT }?.level.toString().toByteArray() - ) - SystemApisUtils.setMetadata( - it, - it.METADATA_UNTETHERED_RIGHT_CHARGING, - (if (batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT}?.status == BatteryStatus.CHARGING) "1".toByteArray() else "0".toByteArray()) - ) + if (checkSelfPermission("android.permission.BLUETOOTH_PRIVILEGED") != PackageManager.PERMISSION_GRANTED) { + device?.let { it -> + SystemApisUtils.setMetadata( + it, + it.METADATA_UNTETHERED_CASE_BATTERY, + batteryNotification.getBattery() + .find { it.component == BatteryComponent.CASE }?.level.toString() + .toByteArray() + ) + SystemApisUtils.setMetadata( + it, + it.METADATA_UNTETHERED_CASE_CHARGING, + (if (batteryNotification.getBattery() + .find { it.component == BatteryComponent.CASE }?.status == BatteryStatus.CHARGING + ) "1".toByteArray() else "0".toByteArray()) + ) + SystemApisUtils.setMetadata( + it, + it.METADATA_UNTETHERED_LEFT_BATTERY, + batteryNotification.getBattery() + .find { it.component == BatteryComponent.LEFT }?.level.toString() + .toByteArray() + ) + SystemApisUtils.setMetadata( + it, + it.METADATA_UNTETHERED_LEFT_CHARGING, + (if (batteryNotification.getBattery() + .find { it.component == BatteryComponent.LEFT }?.status == BatteryStatus.CHARGING + ) "1".toByteArray() else "0".toByteArray()) + ) + SystemApisUtils.setMetadata( + it, + it.METADATA_UNTETHERED_RIGHT_BATTERY, + batteryNotification.getBattery() + .find { it.component == BatteryComponent.RIGHT }?.level.toString() + .toByteArray() + ) + SystemApisUtils.setMetadata( + it, + it.METADATA_UNTETHERED_RIGHT_CHARGING, + (if (batteryNotification.getBattery() + .find { it.component == BatteryComponent.RIGHT }?.status == BatteryStatus.CHARGING + ) "1".toByteArray() else "0".toByteArray()) + ) + } } } @@ -1606,7 +1899,12 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList val widgetIds = appWidgetManager.getAppWidgetIds(componentName) val remoteViews = RemoteViews(packageName, R.layout.battery_widget).also { it -> - val openActivityIntent = PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) + val openActivityIntent = PendingIntent.getActivity( + this, + 0, + Intent(this, MainActivity::class.java), + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE + ) it.setOnClickPendingIntent(R.id.battery_widget, openActivityIntent) val leftBattery = @@ -1616,55 +1914,37 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList val caseBattery = batteryNotification.getBattery().find { it.component == BatteryComponent.CASE } - it.setTextViewText( - R.id.left_battery_widget, - leftBattery?.let { - "${it.level}%" - } ?: "" - ) + it.setTextViewText(R.id.left_battery_widget, leftBattery?.let { + "${it.level}%" + } ?: "") it.setProgressBar( - R.id.left_battery_progress, - 100, - leftBattery?.level ?: 0, - false + R.id.left_battery_progress, 100, leftBattery?.level ?: 0, false ) it.setViewVisibility( R.id.left_charging_icon, - if (leftBattery?.status == BatteryStatus.CHARGING) View.VISIBLE else View.GONE + if (leftBattery?.status == BatteryStatus.CHARGING || leftBattery?.status == BatteryStatus.OPTIMIZED_CHARGING) View.VISIBLE else View.GONE ) - it.setTextViewText( - R.id.right_battery_widget, - rightBattery?.let { - "${it.level}%" - } ?: "" - ) + it.setTextViewText(R.id.right_battery_widget, rightBattery?.let { + "${it.level}%" + } ?: "") it.setProgressBar( - R.id.right_battery_progress, - 100, - rightBattery?.level ?: 0, - false + R.id.right_battery_progress, 100, rightBattery?.level ?: 0, false ) it.setViewVisibility( R.id.right_charging_icon, - if (rightBattery?.status == BatteryStatus.CHARGING) View.VISIBLE else View.GONE + if (rightBattery?.status == BatteryStatus.CHARGING || rightBattery?.status == BatteryStatus.OPTIMIZED_CHARGING ) View.VISIBLE else View.GONE ) - it.setTextViewText( - R.id.case_battery_widget, - caseBattery?.let { - "${it.level}%" - } ?: "" - ) + it.setTextViewText(R.id.case_battery_widget, caseBattery?.let { + "${it.level}%" + } ?: "") it.setProgressBar( - R.id.case_battery_progress, - 100, - caseBattery?.level ?: 0, - false + R.id.case_battery_progress, 100, caseBattery?.level ?: 0, false ) it.setViewVisibility( R.id.case_charging_icon, - if (caseBattery?.status == BatteryStatus.CHARGING) View.VISIBLE else View.GONE + if (caseBattery?.status == BatteryStatus.CHARGING || caseBattery?.status == BatteryStatus.OPTIMIZED_CHARGING ) View.VISIBLE else View.GONE ) it.setViewVisibility( @@ -1678,18 +1958,13 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList val charging = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS) == BatteryManager.BATTERY_STATUS_CHARGING it.setTextViewText( - R.id.phone_battery_widget, - "$batteryLevel%" + R.id.phone_battery_widget, "$batteryLevel%" ) it.setViewVisibility( - R.id.phone_charging_icon, - if (charging) View.VISIBLE else View.GONE + R.id.phone_charging_icon, if (charging) View.VISIBLE else View.GONE ) it.setProgressBar( - R.id.phone_battery_progress, - 100, - batteryLevel, - false + R.id.phone_battery_progress, 100, batteryLevel, false ) } } @@ -1711,8 +1986,10 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList val widgetIds = appWidgetManager.getAppWidgetIds(componentName) val remoteViews = RemoteViews(packageName, R.layout.noise_control_widget).also { it -> val ancStatus = ancNotification.status - val allowOffModeValue = aacpManager.controlCommandStatusList.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.ALLOW_OFF_OPTION } - val allowOffMode = allowOffModeValue?.value?.takeIf { it.isNotEmpty() }?.get(0) == 0x01.toByte() + val allowOffModeValue = + aacpManager.controlCommandStatusList.find { it.identifier == AACPManager.Companion.ControlCommandIdentifiers.ALLOW_OFF_OPTION } + val allowOffMode = + allowOffModeValue?.value?.takeIf { it.isNotEmpty() }?.get(0) == 0x01.toByte() || sharedPreferences.getBoolean("off_listening_mode", true) it.setInt( R.id.widget_off_button, "setBackgroundResource", @@ -1734,8 +2011,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList if (ancStatus == 2) R.drawable.widget_button_checked_shape_end else R.drawable.widget_button_shape_end ) it.setViewVisibility( - R.id.widget_off_button, - if (allowOffMode) View.VISIBLE else View.GONE + R.id.widget_off_button, if (allowOffMode) View.VISIBLE else View.GONE ) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { it.setViewLayoutMargin( @@ -1760,12 +2036,9 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList @OptIn(ExperimentalMaterial3Api::class) fun updateNotificationContent( - connected: Boolean, - airpodsName: String? = null, - batteryList: List? = null + connected: Boolean, airpodsName: String? = null, batteryList: List? = null ) { val notificationManager = getSystemService(NotificationManager::class.java) - var updatedNotification: Notification? val notificationIntent = Intent(this, MainActivity::class.java) val pendingIntent = PendingIntent.getActivity( @@ -1775,15 +2048,15 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) - if (!::socket.isInitialized) { + if (BluetoothConnectionManager.getAACPSocket() == null) { return } - if (connected && (config.bleOnlyMode || socket.isConnected)) { - val updatedNotificationBuilder = NotificationCompat.Builder(this, "airpods_connection_status") - .setSmallIcon(R.drawable.airpods) - .setContentTitle(airpodsName ?: config.deviceName) - .setContentText( - """${ + if (BluetoothConnectionManager.getAACPSocket()?.isConnected == true) { + val updatedNotificationBuilder = + NotificationCompat.Builder(this, "airpods_connection_status") + .setSmallIcon(R.drawable.airpods) + .setContentTitle(airpodsName ?: config.deviceName).setContentText( + """${ batteryList?.find { it.component == BatteryComponent.LEFT }?.let { if (it.status != BatteryStatus.DISCONNECTED) { "L: ${if (it.status == BatteryStatus.CHARGING) "⚡" else ""} ${it.level}%" @@ -1807,23 +2080,15 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList "" } } ?: "" - }""") - .setContentIntent(pendingIntent) - .setCategory(Notification.CATEGORY_STATUS) - .setPriority(NotificationCompat.PRIORITY_LOW) - .setOngoing(true) + }""").setContentIntent(pendingIntent).setCategory(Notification.CATEGORY_STATUS) + .setPriority(NotificationCompat.PRIORITY_LOW).setOngoing(true) if (disconnectedBecauseReversed) { updatedNotificationBuilder.addAction( - R.drawable.ic_bluetooth, - "Reconnect", - PendingIntent.getService( - this, - 0, - Intent(this, AirPodsService::class.java).apply { + R.drawable.ic_bluetooth, "Reconnect", PendingIntent.getService( + this, 0, Intent(this, AirPodsService::class.java).apply { action = "me.kavishdevar.librepods.RECONNECT_AFTER_REVERSE" - }, - PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE + }, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) ) } @@ -1833,20 +2098,9 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList notificationManager.notify(2, updatedNotification) notificationManager.cancel(1) } else if (!connected) { - updatedNotification = NotificationCompat.Builder(this, "background_service_status") - .setSmallIcon(R.drawable.airpods) - .setContentTitle("AirPods not connected") - .setContentText("Tap to open app") - .setContentIntent(pendingIntent) - .setCategory(Notification.CATEGORY_SERVICE) - .setPriority(NotificationCompat.PRIORITY_LOW) - .setOngoing(true) - .build() - - notificationManager.notify(1, updatedNotification) notificationManager.cancel(2) - } else if (!config.bleOnlyMode && !socket.isConnected && isConnectedLocally) { - showSocketConnectionFailureNotification("Socket created, but not connected. Is the Bluetooth process hooked?") + } else if (!config.bleOnlyMode && BluetoothConnectionManager.getAACPSocket()?.isConnected != true) { + showSocketConnectionFailureNotification("BluetoothConnectionManager.getAACPSocket()? created, but not connected. Check logs") } } @@ -1873,19 +2127,20 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList return suspendCancellableCoroutine { continuation -> gestureDetector?.startDetection(doNotStop = true) { accepted -> if (continuation.isActive) { - continuation.resume(accepted) { + continuation.resume(accepted) { _, _, _ -> gestureDetector?.stopDetection() } } } } } + private fun answerCall() { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { val telecomManager = getSystemService(TELECOM_SERVICE) as TelecomManager if (checkSelfPermission(Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED) { - telecomManager.acceptRingingCall() + telecomManager.acceptRingingCall() // TODO: Switch to InCallService (needs CDM association) } } else { val telephonyService = getSystemService(TELEPHONY_SERVICE) as TelephonyManager @@ -1893,7 +2148,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList val method = telephonyClass.getDeclaredMethod("getITelephony") method.isAccessible = true val telephonyInterface = method.invoke(telephonyService) - val answerCallMethod = telephonyInterface.javaClass.getDeclaredMethod("answerRingingCall") + val answerCallMethod = + telephonyInterface.javaClass.getDeclaredMethod("answerRingingCall") answerCallMethod.invoke(telephonyInterface) } @@ -1905,12 +2161,13 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList islandWindow?.close() } } + private fun rejectCall() { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { val telecomManager = getSystemService(TELECOM_SERVICE) as TelecomManager if (checkSelfPermission(Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED) { - telecomManager.endCall() + telecomManager.endCall() // TODO: Switch to InCallService (needs CDM association) } } else { val telephonyService = getSystemService(TELEPHONY_SERVICE) as TelephonyManager @@ -1941,19 +2198,21 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList fun processHeadTrackingData(data: ByteArray) { val horizontal = ByteBuffer.wrap(data, 51, 2).order(ByteOrder.LITTLE_ENDIAN).short.toInt() val vertical = ByteBuffer.wrap(data, 53, 2).order(ByteOrder.LITTLE_ENDIAN).short.toInt() - gestureDetector?.processHeadOrientation(horizontal, vertical) + try { + gestureDetector?.processHeadOrientation(horizontal, vertical) + } catch (e: Exception) { + Log.w(TAG, "gesture detector on ${data.toHexString()}: ${e.message}") + } } private lateinit var connectionReceiver: BroadcastReceiver private fun resToUri(resId: Int): Uri? { return try { - Uri.Builder() - .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) + Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority("me.kavishdevar.librepods") .appendPath(applicationContext.resources.getResourceTypeName(resId)) - .appendPath(applicationContext.resources.getResourceEntryName(resId)) - .build() + .appendPath(applicationContext.resources.getResourceEntryName(resId)).build() } catch (_: Resources.NotFoundException) { null } @@ -1961,22 +2220,29 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList @Suppress("PrivatePropertyName") private val VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV = "+IPHONEACCEV" + @Suppress("PrivatePropertyName") private val VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL = 1 + @Suppress("PrivatePropertyName") private val APPLE = 0x004C + @Suppress("PrivatePropertyName") - private val ACTION_BATTERY_LEVEL_CHANGED = "android.bluetooth.device.action.BATTERY_LEVEL_CHANGED" + private val ACTION_BATTERY_LEVEL_CHANGED = + "android.bluetooth.device.action.BATTERY_LEVEL_CHANGED" + @Suppress("PrivatePropertyName") private val EXTRA_BATTERY_LEVEL = "android.bluetooth.device.extra.BATTERY_LEVEL" + @Suppress("PrivatePropertyName") private val PACKAGE_ASI = "com.google.android.settings.intelligence" + @Suppress("PrivatePropertyName") private val ACTION_ASI_UPDATE_BLUETOOTH_DATA = "batterywidget.impl.action.update_bluetooth_data" - @Suppress("MissingPermission", "unused") + @SuppressLint("MissingPermission") fun broadcastBatteryInformation() { - if (device == null) return + if (device == null || checkSelfPermission("android.permission.INTERACT_ACROSS_USERS") != PackageManager.PERMISSION_GRANTED) return val batteryList = batteryNotification.getBattery() val leftBattery = batteryList.find { it.component == BatteryComponent.LEFT } @@ -1984,8 +2250,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList // Calculate unified battery level (minimum of left and right) val batteryUnified = minOf( - leftBattery?.level ?: 100, - rightBattery?.level ?: 100 + leftBattery?.level ?: 100, rightBattery?.level ?: 100 ) // Check charging status @@ -2002,8 +2267,14 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList // Broadcast vendor-specific event val intent = Intent(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT).apply { - putExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD, VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV) - putExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE, BluetoothHeadset.AT_CMD_TYPE_SET) + putExtra( + BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD, + VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV + ) + putExtra( + BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE, + BluetoothHeadset.AT_CMD_TYPE_SET + ) putExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS, arguments) putExtra(BluetoothDevice.EXTRA_DEVICE, device) putExtra(BluetoothDevice.EXTRA_NAME, device?.name) @@ -2055,67 +2326,61 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } private fun setMetadatas(d: BluetoothDevice) { - d.let{ device -> + if (checkSelfPermission("android.permission.BLUETOOTH_PRIVILEGED") != PackageManager.PERMISSION_GRANTED) { + Log.d(TAG, "no permission BLUETOOTH_PRIVILEGED, returning") + return + } + Log.d(TAG, "has permission BLUETOOTH_PRIVILEGED, proceeding") + d.let { device -> val instance = airpodsInstance if (instance != null) { val metadataSet = SystemApisUtils.setMetadata( device, device.METADATA_MAIN_ICON, resToUri(instance.model.budCaseRes).toString().toByteArray() - ) && - SystemApisUtils.setMetadata( - device, - device.METADATA_MODEL_NAME, - instance.model.name.toByteArray() - ) && - SystemApisUtils.setMetadata( + ) && SystemApisUtils.setMetadata( + device, device.METADATA_MODEL_NAME, instance.model.name.toByteArray() + ) && SystemApisUtils.setMetadata( device, device.METADATA_DEVICE_TYPE, device.DEVICE_TYPE_UNTETHERED_HEADSET.toByteArray() - ) && - SystemApisUtils.setMetadata( + ) && SystemApisUtils.setMetadata( device, device.METADATA_UNTETHERED_CASE_ICON, resToUri(instance.model.caseRes).toString().toByteArray() - ) && - SystemApisUtils.setMetadata( + ) && SystemApisUtils.setMetadata( device, device.METADATA_UNTETHERED_RIGHT_ICON, resToUri(instance.model.rightBudsRes).toString().toByteArray() - ) && - SystemApisUtils.setMetadata( + ) && SystemApisUtils.setMetadata( device, device.METADATA_UNTETHERED_LEFT_ICON, resToUri(instance.model.leftBudsRes).toString().toByteArray() - ) && - SystemApisUtils.setMetadata( + ) && SystemApisUtils.setMetadata( device, device.METADATA_MANUFACTURER_NAME, instance.model.manufacturer.toByteArray() - ) && - SystemApisUtils.setMetadata( - device, - device.METADATA_COMPANION_APP, - "me.kavisdevar.librepods".toByteArray() - ) && - SystemApisUtils.setMetadata( + ) && SystemApisUtils.setMetadata( + device, device.METADATA_COMPANION_APP, "me.kavishdevar.librepods".toByteArray() + ) && SystemApisUtils.setMetadata( device, device.METADATA_UNTETHERED_CASE_LOW_BATTERY_THRESHOLD, "20".toByteArray() - ) && - SystemApisUtils.setMetadata( + ) && SystemApisUtils.setMetadata( device, device.METADATA_UNTETHERED_LEFT_LOW_BATTERY_THRESHOLD, "20".toByteArray() - ) && - SystemApisUtils.setMetadata( + ) && SystemApisUtils.setMetadata( device, device.METADATA_UNTETHERED_RIGHT_LOW_BATTERY_THRESHOLD, "20".toByteArray() ) Log.d(TAG, "Metadata set: $metadataSet") } else { - Log.w(TAG, "AirPods instance is not of type AirPodsInstance, skipping metadata setting") + Log.w( + TAG, + "AirPods instance is not of type AirPodsInstance, skipping metadata setting" + ) } } } @@ -2124,45 +2389,51 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList private object bluetoothReceiver : BroadcastReceiver() { @SuppressLint("MissingPermission") override fun onReceive(context: Context?, intent: Intent) { - val bluetoothDevice = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - "android.bluetooth.device.extra.DEVICE", - BluetoothDevice::class.java - ) - } else { - intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE") as BluetoothDevice? - } + val bluetoothDevice = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + intent.getParcelableExtra( + "android.bluetooth.device.extra.DEVICE", BluetoothDevice::class.java + ) + } else { + intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE") as BluetoothDevice? + } val action = intent.action val context = context?.applicationContext val name = context?.getSharedPreferences("settings", MODE_PRIVATE) ?.getString("name", bluetoothDevice?.name) - if (bluetoothDevice != null && action != null && !action.isEmpty()) { + if (bluetoothDevice != null && !action.isNullOrEmpty()) { Log.d(TAG, "Received bluetooth connection broadcast: action=$action") - if (ServiceManager.getService()?.isConnectedLocally == true) { - Log.d(TAG, "Device is already connected locally, checking if we should keep audio connected") - if (ServiceManager.getService()?.socket?.isConnected == true) ServiceManager.getService()?.manuallyCheckForAudioSource() else Log.d(TAG, "We're not connected, ignoring") - return - } + val uuid = ParcelUuid.fromString("74ec2172-0bad-4d01-8f77-997b2be0722a") + if (BluetoothDevice.ACTION_ACL_CONNECTED == action) { - val uuid = ParcelUuid.fromString("74ec2172-0bad-4d01-8f77-997b2be0722a") - bluetoothDevice.fetchUuidsWithSdp() - if (bluetoothDevice.uuids != null) { - if (bluetoothDevice.uuids.contains(uuid)) { - val intent = - Intent(AirPodsNotifications.AIRPODS_CONNECTION_DETECTED) - intent.putExtra("name", name) - intent.putExtra("device", bluetoothDevice) - context?.sendBroadcast(intent) - } + if (bluetoothDevice.uuids?.contains(uuid) == true) { + val intent = Intent(AirPodsNotifications.AIRPODS_CONNECTION_DETECTED) + intent.putExtra("name", name) + intent.putExtra("device", bluetoothDevice) + context?.sendBroadcast(intent) + } else { + bluetoothDevice.fetchUuidsWithSdp() + } + } else if ("android.bluetooth.device.action.UUID" == action) { + val savedMac = context?.getSharedPreferences("settings", MODE_PRIVATE) + ?.getString("mac_address", "") ?: "" + val matchedByMac = savedMac.isNotEmpty() && bluetoothDevice.address == savedMac + val matchedByUuid = bluetoothDevice.uuids?.contains(uuid) == true + if (matchedByUuid || matchedByMac) { + val intent = Intent(AirPodsNotifications.AIRPODS_CONNECTION_DETECTED) + intent.putExtra("name", name) + intent.putExtra("device", bluetoothDevice) + context?.sendBroadcast(intent) } } } } } - val ancModeFilter = IntentFilter("me.kavishdevar.librepods.SET_ANC_MODE") - var ancModeReceiver: BroadcastReceiver? = null + val externalBroadcastFilter = IntentFilter().apply { + addAction("me.kavishdevar.librepods.SET_ANC_MODE") + addAction("me.kavishdevar.librepods.CONVO_DETECT") + } + var externalBroadcastReceiver: BroadcastReceiver? = null @SuppressLint("InlinedApi", "MissingPermission", "UnspecifiedRegisterReceiverFlag") override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -2178,26 +2449,16 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList return START_STICKY } - fun manuallyCheckForAudioSource() { - val shouldResume = MediaController.getMusicActive() // todo: for some reason we lose this info after disconnecting, probably android dispatches some event. haven't investigated yet. - if (airpodsInstance == null) return - Log.d(TAG, "disconnectedBecauseReversed: $disconnectedBecauseReversed, otherDeviceTookOver: $otherDeviceTookOver") - if ((earDetectionNotification.status[0] != 0.toByte() && earDetectionNotification.status[1] != 0.toByte()) || disconnectedBecauseReversed || otherDeviceTookOver) { - Log.d( - TAG, - "For some reason, Android connected to the audio profile itself even after disconnecting. Disconnecting audio profile again! I will resume: $shouldResume" - ) - disconnectAudio(this, device, shouldResume = shouldResume) - } - } - @RequiresApi(Build.VERSION_CODES.R) @SuppressLint("MissingPermission", "HardwareIds") - fun takeOver(takingOverFor: String, manualTakeOverAfterReversed: Boolean = false, startHeadTrackingAgain: Boolean = false) { + fun takeOver( + takingOverFor: String, + manualTakeOverAfterReversed: Boolean = false, + startHeadTrackingAgain: Boolean = false + ) { if (takingOverFor == "reverse") { aacpManager.sendControlCommand( - AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION.value, - 1 + AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION.value, 1 ) aacpManager.sendMediaInformataion( localMac @@ -2206,28 +2467,36 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList localMac ) connectAudio( - this@AirPodsService, - device + this@AirPodsService, device ) otherDeviceTookOver = false } - Log.d(TAG, "owns connection: ${aacpManager.getControlCommandStatus(AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION)?.value?.get(0)?.toInt()}") - if (isConnectedLocally) { + val ownsConnection = aacpManager.getControlCommandStatus(AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION)?.value?.get(0)?.toInt() + Log.d( + TAG, "owns connection: $ownsConnection" + ) + if (BluetoothConnectionManager.getAACPSocket()?.isConnected == true) { + if (!XposedRemotePrefProvider.create().getBoolean("vendor_id_hook", false) || ownsConnection == 0) { + Log.d(TAG, "not taking over, vendorid is probably not set to apple") + return + } if (aacpManager.getControlCommandStatus(AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION)?.value[0]?.toInt() != 1 || (aacpManager.audioSource?.mac != localMac && aacpManager.audioSource?.type != AACPManager.Companion.AudioSourceType.NONE)) { if (disconnectedBecauseReversed) { if (manualTakeOverAfterReversed) { Log.d(TAG, "forcefully taking over despite reverse as user requested") disconnectedBecauseReversed = false } else { - Log.d(TAG, "connected locally, but can not hijack as other device had reversed") + Log.d( + TAG, + "connected locally, but can not hijack as other device had reversed" + ) return } } Log.d(TAG, "already connected locally, hijacking connection by asking AirPods") aacpManager.sendControlCommand( - AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION.value, - 1 + AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION.value, 1 ) aacpManager.sendMediaInformataion( localMac @@ -2240,8 +2509,15 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList ) otherDeviceTookOver = false connectAudio(this, device) - showIsland(this, batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT}?.level!!.coerceAtMost(batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT}?.level!!), - IslandType.CONNECTED) + showIsland( + this, + batteryNotification.getBattery() + .find { it.component == BatteryComponent.LEFT }?.level!!.coerceAtMost( + batteryNotification.getBattery() + .find { it.component == BatteryComponent.RIGHT }?.level!! + ), + IslandType.CONNECTED + ) CoroutineScope(Dispatchers.IO).launch { delay(500) // a2dp takes time, and so does taking control + AirPods pause it for no reason after connecting @@ -2261,19 +2537,26 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } } } else { - Log.d(TAG, "Already connected locally and already own connection, skipping takeover") + Log.d( + TAG, "Already connected locally and already own connection, skipping takeover" + ) } return } - if (CrossDevice.isAvailable) { - Log.d(TAG, "CrossDevice is available, continuing") - } - else if (bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true) { - Log.d(TAG, "At least one AirPod is in ear, continuing") - } - else { - Log.d(TAG, "CrossDevice not available and AirPods not in ear, skipping") +// if (CrossDevice.isAvailable) { +// Log.d(TAG, "CrossDevice is available, continuing") +// } +// else if (bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true) { +// Log.d(TAG, "At least one AirPod is in ear, continuing") +// } +// else { +// Log.d(TAG, "CrossDevice not available and AirPods not in ear, skipping") +// return +// } + + if (bleManager.getMostRecentStatus()?.isLeftInEar == false && bleManager.getMostRecentStatus()?.isRightInEar == false) { + Log.d(TAG, "Both AirPods are out of ear, not taking over audio") return } @@ -2312,11 +2595,13 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } Log.d(TAG, "Taking over audio") - CrossDevice.sendRemotePacket(CrossDevicePackets.REQUEST_DISCONNECT.packet) +// CrossDevice.sendRemotePacket(CrossDevicePackets.REQUEST_DISCONNECT.packet) Log.d(TAG, macAddress) - sharedPreferences.edit { putBoolean("CrossDeviceIsAvailable", false) } - device = getSystemService(BluetoothManager::class.java).adapter.bondedDevices.find { +// sharedPreferences.edit { putBoolean("CrossDeviceIsAvailable", false) } + val bluetoothManager = getSystemService(BluetoothManager::class.java) + val bluetoothAdapter = bluetoothManager.adapter + device = bluetoothAdapter.bondedDevices.find { it.address == macAddress } @@ -2325,31 +2610,39 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList // In BLE-only mode, just show connecting status without actual L2CAP connection Log.d(TAG, "BLE-only mode: showing connecting status without L2CAP connection") updateNotificationContent( - true, - config.deviceName, - batteryNotification.getBattery() + true, config.deviceName, batteryNotification.getBattery() ) // Set a temporary connecting state - isConnectedLocally = false // Keep as false since we're not actually connecting to L2CAP +// isConnectedLocally = false // Keep as false since we're not actually connecting to L2CAP } else { - connectToSocket(device!!) + connectToSocket(bluetoothAdapter, device!!) connectAudio(this, device) - isConnectedLocally = true +// isConnectedLocally = true } } - showIsland(this, batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT}?.level!!.coerceAtMost(batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT}?.level!!), - IslandType.TAKING_OVER) + showIsland( + this, + batteryNotification.getBattery() + .find { it.component == BatteryComponent.LEFT }?.level!!.coerceAtMost( + batteryNotification.getBattery() + .find { it.component == BatteryComponent.RIGHT }?.level!! + ), + IslandType.TAKING_OVER + ) - CrossDevice.isAvailable = false +// CrossDevice.isAvailable = false } - private fun createBluetoothSocket(device: BluetoothDevice, uuid: ParcelUuid): BluetoothSocket { + private fun createBluetoothSocket( + adapter: BluetoothAdapter, device: BluetoothDevice, uuid: ParcelUuid, psm: Int + ): BluetoothSocket { val type = 3 // L2CAP val constructorSpecs = listOf( - arrayOf(device, type, true, true, 0x1001, uuid), - arrayOf(device, type, 1, true, true, 0x1001, uuid), - arrayOf(type, 1, true, true, device, 0x1001, uuid), - arrayOf(type, true, true, device, 0x1001, uuid) + arrayOf(adapter, device, type, true, true, psm, uuid), // A16QPR3 + arrayOf(device, type, true, true, psm, uuid), + arrayOf(device, type, 1, true, true, psm, uuid), + arrayOf(type, 1, true, true, device, psm, uuid), + arrayOf(type, true, true, device, psm, uuid) ) val constructors = BluetoothSocket::class.java.declaredConstructors @@ -2367,190 +2660,248 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList try { Log.d(TAG, "Trying constructor signature #${index + 1}") attemptedConstructors++ - return HiddenApiBypass.newInstance(BluetoothSocket::class.java, *params) as BluetoothSocket + + val paramTypes = + params.map { it::class.javaPrimitiveType ?: it::class.java }.toTypedArray() + val constructor = BluetoothSocket::class.java.getDeclaredConstructor(*paramTypes) + constructor.isAccessible = true + return constructor.newInstance(*params) as BluetoothSocket + } catch (e: Exception) { Log.e(TAG, "Constructor signature #${index + 1} failed: ${e.message}") lastException = e } } - val errorMessage = "Failed to create BluetoothSocket after trying $attemptedConstructors constructor signatures" + val errorMessage = + "Failed to create BluetoothSocket after trying $attemptedConstructors constructor signatures" Log.e(TAG, errorMessage) showSocketConnectionFailureNotification(errorMessage) throw lastException ?: IllegalStateException(errorMessage) } @SuppressLint("MissingPermission", "UnspecifiedRegisterReceiverFlag") - fun connectToSocket(device: BluetoothDevice, manual: Boolean = false) { + fun connectToSocket( + adapter: BluetoothAdapter, device: BluetoothDevice, manual: Boolean = false + ) { + if (BluetoothConnectionManager.getAACPSocket() != null && BluetoothConnectionManager.getAACPSocket()?.isConnected == true) return Log.d(TAG, " Connecting to socket") - HiddenApiBypass.addHiddenApiExemptions("Landroid/bluetooth/BluetoothSocket;") val uuid: ParcelUuid = ParcelUuid.fromString("74ec2172-0bad-4d01-8f77-997b2be0722a") - if (!isConnectedLocally && !CrossDevice.isAvailable) { - socket = try { - createBluetoothSocket(device, uuid) - } catch (e: Exception) { - Log.e(TAG, "Failed to create BluetoothSocket: ${e.message}") - showSocketConnectionFailureNotification("Failed to create Bluetooth socket: ${e.localizedMessage}") - return - } +// if (!isConnectedLocally) { + val socket = try { + createBluetoothSocket(adapter, device, uuid, 4097) + } catch (e: Exception) { + Log.e(TAG, "Failed to create BluetoothSocket: ${e.message}") + showSocketConnectionFailureNotification("Failed to create Bluetooth socket: ${e.localizedMessage}") + return + } - try { - runBlocking { - withTimeout(5000L) { - try { - socket.connect() - isConnectedLocally = true - this@AirPodsService.device = device - - BluetoothConnectionManager.setCurrentConnection(socket, device) - - attManager = ATTManager(device) - attManager!!.connect() - - // Create AirPodsInstance from stored config if available - if (airpodsInstance == null && config.airpodsModelNumber.isNotEmpty()) { - val model = AirPodsModels.getModelByModelNumber(config.airpodsModelNumber) - if (model != null) { - airpodsInstance = AirPodsInstance( - name = config.airpodsName, - model = model, - actualModelNumber = config.airpodsModelNumber, - serialNumber = config.airpodsSerialNumber, - leftSerialNumber = config.airpodsLeftSerialNumber, - rightSerialNumber = config.airpodsRightSerialNumber, - version1 = config.airpodsVersion1, - version2 = config.airpodsVersion2, - version3 = config.airpodsVersion3, - aacpManager = aacpManager, - attManager = attManager - ) - } + try { + runBlocking { + withTimeout(5000.milliseconds) { + try { + socket.connect() +// isConnectedLocally = true + this@AirPodsService.device = device + val xposedRemotePref = XposedRemotePrefProvider.create() + val attSocket = if (xposedRemotePref.getBoolean("vendor_id_hook", false)) { + createBluetoothSocket( + adapter, + device, + ParcelUuid.fromString("00000000-0000-0000-0000-000000000000"), + 31 + ) + } else null + attSocket?.connect() + BluetoothConnectionManager.setCurrentConnection(socket, attSocket) + if (attSocket != null) { + attManager.startReader() + attManager.readCharacteristic(ATTHandles.LOUD_SOUND_REDUCTION) + attManager.readCharacteristic(ATTHandles.TRANSPARENCY) + attManager.readCharacteristic(ATTHandles.HEARING_AID) + attManager.enableNotification(ATTCCCDHandles.HEARING_AID) +// attManager.enableNotification(ATTCCCDHandles.LOUD_SOUND_REDUCTION) + attManager.enableNotification(ATTCCCDHandles.TRANSPARENCY) + } + + // Create AirPodsInstance from stored config if available + if (airpodsInstance == null && config.airpodsModelNumber.isNotEmpty()) { + val model = + AirPodsModels.getModelByModelNumber(config.airpodsModelNumber) + if (model != null) { + airpodsInstance = AirPodsInstance( + name = config.airpodsName, + model = model, + actualModelNumber = config.airpodsModelNumber, + serialNumber = config.airpodsSerialNumber, + leftSerialNumber = config.airpodsLeftSerialNumber, + rightSerialNumber = config.airpodsRightSerialNumber, + version1 = config.airpodsVersion1, + version2 = config.airpodsVersion2, + version3 = config.airpodsVersion3, + ) + setMetadatas(device) } + } - updateNotificationContent( - true, - config.deviceName, - batteryNotification.getBattery() - ) - Log.d(TAG, " Socket connected") - } catch (e: Exception) { - Log.d(TAG, " Socket not connected, ${e.message}") - if (manual) { - sendToast( - "Couldn't connect to socket: ${e.localizedMessage}" + updateNotificationContent( + true, config.deviceName, batteryNotification.getBattery() + ) + Log.d(TAG, " Socket connected") + sharedPreferences.edit { putBoolean("connection_successful", true) } + if (!sharedPreferences.contains("first_connection_successful_time")) { + sharedPreferences.edit { + putLong( + "first_connection_successful_time", + System.currentTimeMillis() ) - } else { - showSocketConnectionFailureNotification("Couldn't connect to socket: ${e.localizedMessage}") } - return@withTimeout -// throw e // lol how did i not catch this before... gonna comment this line instead of removing to preserve history } - } - } - if (!socket.isConnected) { - Log.d(TAG, " Socket not connected") - if (manual) { - sendToast( - "Couldn't connect to socket: timeout." + sendBroadcast(Intent(AirPodsNotifications.AIRPODS_L2CAP_CONNECTED)) + } catch (e: Exception) { +// sharedPreferences.edit { putBoolean("connection_successful", false) } + Log.d( + TAG, " Socket not connected, ${e.message}" ) - } else { - showSocketConnectionFailureNotification("Couldn't connect to socket: Timeout") + if (manual) { + sendToast( + "Couldn't connect to socket: ${e.localizedMessage}" + ) + } else { + showSocketConnectionFailureNotification("Couldn't connect to socket: ${e.localizedMessage}") + } + return@withTimeout +// throw e // lol how did i not catch this before... gonna comment this line instead of removing to preserve history } - return } - this@AirPodsService.device = device - socket.let { + } + if (!socket.isConnected) { + Log.d(TAG, " socket not connected") + if (manual) { + sendToast( + "Couldn't connect to socket: timeout." + ) + } else { + showSocketConnectionFailureNotification("Couldn't connect to socket: Timeout") + } + return + } + this@AirPodsService.device = device + BluetoothConnectionManager.getAACPSocket()?.let { + aacpManager.sendPacket(aacpManager.createHandshakePacket()) + aacpManager.sendSetFeatureFlagsPacket() + aacpManager.sendNotificationRequest() + Log.d(TAG, "Requesting proximity keys") + aacpManager.sendRequestProximityKeys((AACPManager.Companion.ProximityKeyType.IRK.value + AACPManager.Companion.ProximityKeyType.ENC_KEY.value).toByte()) + CoroutineScope(Dispatchers.IO).launch { + delay(200) aacpManager.sendPacket(aacpManager.createHandshakePacket()) + delay(200) aacpManager.sendSetFeatureFlagsPacket() + delay(200) aacpManager.sendNotificationRequest() - Log.d(TAG, "Requesting proximity keys") + delay(200) + aacpManager.sendSomePacketIDontKnowWhatItIs() + delay(200) aacpManager.sendRequestProximityKeys((AACPManager.Companion.ProximityKeyType.IRK.value + AACPManager.Companion.ProximityKeyType.ENC_KEY.value).toByte()) - CoroutineScope(Dispatchers.IO).launch { + if (!handleIncomingCallOnceConnected) startHeadTracking() else handleIncomingCall() + Handler(Looper.getMainLooper()).postDelayed({ aacpManager.sendPacket(aacpManager.createHandshakePacket()) - delay(200) aacpManager.sendSetFeatureFlagsPacket() - delay(200) aacpManager.sendNotificationRequest() - delay(200) - aacpManager.sendSomePacketIDontKnowWhatItIs() - delay(200) - aacpManager.sendRequestProximityKeys((AACPManager.Companion.ProximityKeyType.IRK.value+AACPManager.Companion.ProximityKeyType.ENC_KEY.value).toByte()) - if (!handleIncomingCallOnceConnected) startHeadTracking() else handleIncomingCall() - Handler(Looper.getMainLooper()).postDelayed({ - aacpManager.sendPacket(aacpManager.createHandshakePacket()) - aacpManager.sendSetFeatureFlagsPacket() - aacpManager.sendNotificationRequest() - aacpManager.sendRequestProximityKeys(AACPManager.Companion.ProximityKeyType.IRK.value) - if (!handleIncomingCallOnceConnected) stopHeadTracking() - }, 5000) - - sendBroadcast( - Intent(AirPodsNotifications.AIRPODS_CONNECTED) - .putExtra("device", device) - ) + aacpManager.sendRequestProximityKeys(AACPManager.Companion.ProximityKeyType.IRK.value) + if (!handleIncomingCallOnceConnected) stopHeadTracking() + }, 5000) - setupStemActions() - - while (socket.isConnected) { - socket.let { it -> - val buffer = ByteArray(1024) - val bytesRead = it.inputStream.read(buffer) - var data: ByteArray - if (bytesRead > 0) { - data = buffer.copyOfRange(0, bytesRead) - sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DATA).apply { - putExtra("data", buffer.copyOfRange(0, bytesRead)) - }) - val bytes = buffer.copyOfRange(0, bytesRead) - val formattedHex = bytes.joinToString(" ") { "%02X".format(it) } - CrossDevice.sendReceivedPacket(bytes) - updateNotificationContent( - true, - sharedPreferences.getString("name", device.name), - batteryNotification.getBattery() - ) - - aacpManager.receivePacket(data) - - if (!isHeadTrackingData(data)) { - Log.d("AirPodsData", "Data received: $formattedHex") - logPacket(data, "AirPods") - } + sendBroadcast( + Intent(AirPodsNotifications.AIRPODS_CONNECTED).putExtra("device", device) + .apply { + setPackage(packageName) + }) + + setupStemActions() - } else if (bytesRead == -1) { - Log.d("AirPods Service", "Socket closed (bytesRead = -1)") - sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED)) - aacpManager.disconnected() - return@launch + while (socket.isConnected) { + try { + val buffer = ByteArray(1024) + val bytesRead = it.inputStream.read(buffer) + var data: ByteArray + if (bytesRead > 0) { + data = buffer.copyOfRange(0, bytesRead) + sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DATA).apply { + putExtra("data", buffer.copyOfRange(0, bytesRead)) + setPackage(packageName) + }) + val bytes = buffer.copyOfRange(0, bytesRead) + val formattedHex = bytes.joinToString(" ") { "%02X".format(it) } +// CrossDevice.sendReceivedPacket(bytes) + updateNotificationContent( + true, + sharedPreferences.getString("name", device.name), + batteryNotification.getBattery() + ) + + aacpManager.receivePacket(data) + + if (!isHeadTrackingData(data)) { + Log.d("AirPodsData", "Data received: $formattedHex") + logPacket(data, "AirPods") } + + } else if (bytesRead == -1) { + Log.d("AirPodsService", "socket closed (bytesRead = -1)") + sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED).apply { + setPackage(packageName) + }) + aacpManager.disconnected() + return@launch } + } catch (e: Exception) { + Log.w(TAG, "Error reading data, we have probably disconnected.") + e.printStackTrace() + sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED).apply { + setPackage(packageName) + }) + aacpManager.disconnected() + return@launch } - Log.d("AirPods Service", "Socket closed") - isConnectedLocally = false - socket.close() - aacpManager.disconnected() - updateNotificationContent(false) - sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED)) + } + Log.d("AirPods Service", "socket closed") +// isConnectedLocally = false + aacpManager.disconnected() + updateNotificationContent(false) + sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED).apply { + setPackage(packageName) + }) } - } catch (e: Exception) { - e.printStackTrace() - Log.d(TAG, "Failed to connect to socket: ${e.message}") - showSocketConnectionFailureNotification("Failed to establish connection: ${e.localizedMessage}") - isConnectedLocally = false - this@AirPodsService.device = device - updateNotificationContent(false) } - } + } catch (e: Exception) { + e.printStackTrace() + Log.d(TAG, "Failed to connect to BluetoothConnectionManager.getAACPSocket()?: ${e.message}") + showSocketConnectionFailureNotification("Failed to establish connection: ${e.localizedMessage}") +// isConnectedLocally = false + this@AirPodsService.device = device + updateNotificationContent(false) + } +// } else { +// Log.d(TAG, "Already connected locally, skipping BluetoothConnectionManager.getAACPSocket()? connection (isConnectedLocally = $isConnectedLocally, BluetoothConnectionManager.getAACPSocket()?.isConnected = ${this::BluetoothConnectionManager.getAACPSocket()?.isInitialized && BluetoothConnectionManager.getAACPSocket()?.isConnected})") +// } } fun disconnectForCD() { - if (!this::socket.isInitialized) return - socket.close() + BluetoothConnectionManager.getAACPSocket()?.close() MediaController.pausedWhileTakingOver = false Log.d(TAG, "Disconnected from AirPods, showing island.") - showIsland(this, batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT}?.level!!.coerceAtMost(batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT}?.level!!), - IslandType.MOVED_TO_REMOTE) + showIsland( + this, + batteryNotification.getBattery() + .find { it.component == BatteryComponent.LEFT }?.level!!.coerceAtMost( + batteryNotification.getBattery() + .find { it.component == BatteryComponent.RIGHT }?.level!! + ), + IslandType.MOVED_TO_REMOTE + ) val bluetoothAdapter = getSystemService(BluetoothManager::class.java).adapter bluetoothAdapter.getProfileProxy(this, object : BluetoothProfile.ServiceListener { override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { @@ -2565,35 +2916,63 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList override fun onServiceDisconnected(profile: Int) {} }, BluetoothProfile.A2DP) - isConnectedLocally = false - CrossDevice.isAvailable = true +// isConnectedLocally = false +// CrossDevice.isAvailable = true } fun disconnectAirPods() { - if (!this::socket.isInitialized) return - socket.close() - isConnectedLocally = false + if (BluetoothConnectionManager.getAACPSocket() == null) return + try { + BluetoothConnectionManager.getAACPSocket()?.close() + } catch(e: Exception) { + Log.e(TAG, "error closing aacp socket ${e.message}") + } +// isConnectedLocally = false aacpManager.disconnected() - attManager?.disconnect() + attManager.disconnected() + BluetoothConnectionManager.setCurrentConnection(null, null) updateNotificationContent(false) - sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED)) + sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED).apply { + setPackage(packageName) + }) val bluetoothAdapter = getSystemService(BluetoothManager::class.java).adapter - bluetoothAdapter.getProfileProxy(this, object : BluetoothProfile.ServiceListener { - override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { - if (profile == BluetoothProfile.A2DP) { - val connectedDevices = proxy.connectedDevices - if (connectedDevices.isNotEmpty()) { - MediaController.sendPause() + if (checkSelfPermission("android.permission.BLUETOOTH_PRIVILEGED") == PackageManager.PERMISSION_GRANTED){ + bluetoothAdapter.getProfileProxy(this, object : BluetoothProfile.ServiceListener { + override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { + if (profile == BluetoothProfile.A2DP) { + val connectedDevices = proxy.connectedDevices + if (connectedDevices.isNotEmpty()) { + MediaController.sendPause() + } } + bluetoothAdapter.closeProfileProxy(profile, proxy) } - bluetoothAdapter.closeProfileProxy(profile, proxy) + + override fun onServiceDisconnected(profile: Int) {} + }, BluetoothProfile.A2DP) + try { + device?.disconnect() + } catch (e: Exception) { + Log.w(TAG, "device.disconnect() failed, $e") } + } + if (checkSelfPermission("android.permission.MODIFY_PHONE_STATE") == PackageManager.PERMISSION_GRANTED){ + bluetoothAdapter.getProfileProxy(this, object : BluetoothProfile.ServiceListener { + override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { + if (profile == BluetoothProfile.HEADSET) { + val connectedDevices = proxy.connectedDevices + if (connectedDevices.isNotEmpty()) { + MediaController.sendPause() + } + } + bluetoothAdapter.closeProfileProxy(profile, proxy) + } - override fun onServiceDisconnected(profile: Int) {} - }, BluetoothProfile.A2DP) + override fun onServiceDisconnected(profile: Int) {} + }, BluetoothProfile.HEADSET) + } Log.d(TAG, "Disconnected AirPods upon user request") - } val earDetectionNotification = AirPodsNotifications.EarDetection() @@ -2611,65 +2990,74 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } fun getBattery(): List { - if (!isConnectedLocally && CrossDevice.isAvailable) { - batteryNotification.setBattery(CrossDevice.batteryBytes) - } +// if (!isConnectedLocally && CrossDevice.isAvailable) { +// batteryNotification.setBattery(CrossDevice.batteryBytes) +// } return batteryNotification.getBattery() } fun getANC(): Int { - if (!isConnectedLocally && CrossDevice.isAvailable) { - ancNotification.setStatus(CrossDevice.ancBytes) - } +// if (!isConnectedLocally && CrossDevice.isAvailable) { +// ancNotification.setStatus(CrossDevice.ancBytes) +// } return ancNotification.status } - fun disconnectAudio(context: Context, device: BluetoothDevice?, shouldResume: Boolean = false) { + fun disconnectAudio(context: Context, device: BluetoothDevice?) { val bluetoothAdapter = context.getSystemService(BluetoothManager::class.java).adapter - bluetoothAdapter?.getProfileProxy(context, object : BluetoothProfile.ServiceListener { - override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { - if (profile == BluetoothProfile.A2DP) { - try { - if (proxy.getConnectionState(device) == BluetoothProfile.STATE_DISCONNECTED) { - Log.d(TAG, "Already disconnected from A2DP") - return - } - val method = - proxy.javaClass.getMethod("disconnect", BluetoothDevice::class.java) - method.invoke(proxy, device) - if (shouldResume) { - Handler(Looper.getMainLooper()).postDelayed({ - MediaController.sendPlay() - }, 150) + if (checkSelfPermission("android.permission.BLUETOOTH_PRIVILEGED") == PackageManager.PERMISSION_GRANTED) { + bluetoothAdapter?.getProfileProxy(context, object : BluetoothProfile.ServiceListener { + override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { + if (profile == BluetoothProfile.A2DP) { + try { + if (proxy.getConnectionState(device) == BluetoothProfile.STATE_DISCONNECTED) { + Log.d(TAG, "Already disconnected from A2DP") + return + } + val method = proxy.javaClass.getMethod( + "setConnectionPolicy", BluetoothDevice::class.java, Int::class.java + ) + Log.d(TAG, "calling A2DP.setConnectionPolicy for ${device?.address} to 0") + method.invoke(proxy, device, 0) + } catch (e: Exception) { + e.printStackTrace() + } finally { + bluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, proxy) } - } catch (e: Exception) { - e.printStackTrace() - } finally { - bluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, proxy) } } - } - override fun onServiceDisconnected(profile: Int) {} - }, BluetoothProfile.A2DP) - - bluetoothAdapter?.getProfileProxy(context, object : BluetoothProfile.ServiceListener { - override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { - if (profile == BluetoothProfile.HEADSET) { - try { - val method = - proxy.javaClass.getMethod("disconnect", BluetoothDevice::class.java) - method.invoke(proxy, device) - } catch (e: Exception) { - e.printStackTrace() - } finally { - bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy) + override fun onServiceDisconnected(profile: Int) {} + }, BluetoothProfile.A2DP) + } else { + Log.d(TAG, "not disconnecting A2DP, no BLUETOOTH_PRIVILEGED permission") + } + if (checkSelfPermission("android.permission.MODIFY_PHONE_STATE") == PackageManager.PERMISSION_GRANTED) { + bluetoothAdapter?.getProfileProxy(context, object : BluetoothProfile.ServiceListener { + override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { + if (profile == BluetoothProfile.HEADSET) { + try { + val method = + proxy.javaClass.getMethod( + "setConnectionPolicy", + BluetoothDevice::class.java, + Int::class.java + ) + Log.d(TAG, "calling HEADSET.setConnectionPolicy for ${device?.address} to 0") + method.invoke(proxy, device, 0) + } catch (e: Exception) { + e.printStackTrace() + } finally { + bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy) + } } } - } - override fun onServiceDisconnected(profile: Int) {} - }, BluetoothProfile.HEADSET) + override fun onServiceDisconnected(profile: Int) {} + }, BluetoothProfile.HEADSET) + } else { + Log.d(TAG, "not disconnecting HEADSET, no MODIFIY_PHONE_STATE permission") + } } fun connectAudio(context: Context, device: BluetoothDevice?) { @@ -2678,18 +3066,38 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList bluetoothAdapter?.getProfileProxy(context, object : BluetoothProfile.ServiceListener { override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { if (profile == BluetoothProfile.A2DP) { - try { - val method = - proxy.javaClass.getMethod("connect", BluetoothDevice::class.java) - method.invoke(proxy, device) - } catch (e: Exception) { - e.printStackTrace() - } finally { - bluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, proxy) - if (MediaController.pausedWhileTakingOver) { - MediaController.sendPlay() + if (context.checkSelfPermission("android.permission.BLUETOOTH_PRIVILEGED") == PackageManager.PERMISSION_GRANTED) { + try { + val policyMethod = proxy.javaClass.getMethod( + "setConnectionPolicy", + BluetoothDevice::class.java, + Int::class.java + ) + Log.d(TAG, "calling A2DP.setConnectionPolicy for ${device?.address} to 100") + policyMethod.invoke(proxy, device, 100) + + val connectMethod = + proxy.javaClass.getMethod("connect", BluetoothDevice::class.java) + connectMethod.invoke( + proxy, device + ) + } catch (e: Exception) { + e.printStackTrace() + } finally { + bluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, proxy) + if (MediaController.pausedWhileTakingOver) { + MediaController.sendPlay() + } } } + else { + val connectMethod = + proxy.javaClass.getMethod("connect", BluetoothDevice::class.java) + connectMethod.invoke( + proxy, device + ) + Log.d(TAG, "not setting connection policy for A2DP, no BLUETOOTH_PRIVILEGED permission. just called connect") + } } } @@ -2699,14 +3107,28 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList bluetoothAdapter?.getProfileProxy(context, object : BluetoothProfile.ServiceListener { override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { if (profile == BluetoothProfile.HEADSET) { - try { - val method = - proxy.javaClass.getMethod("connect", BluetoothDevice::class.java) - method.invoke(proxy, device) - } catch (e: Exception) { - e.printStackTrace() - } finally { - bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy) + if (checkSelfPermission("android.permission.MODIFY_PHONE_STATE") == PackageManager.PERMISSION_GRANTED) { + try { + val policyMethod = proxy.javaClass.getMethod( + "setConnectionPolicy", + BluetoothDevice::class.java, + Int::class.java + ) + Log.d( + TAG, + "calling HEADSET.setConnectionPolicy for ${device?.address} to 100" + ) + policyMethod.invoke(proxy, device, 100) + val connectMethod = + proxy.javaClass.getMethod("connect", BluetoothDevice::class.java) + connectMethod.invoke(proxy, device) + } catch (e: Exception) { + e.printStackTrace() + } finally { + bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy) + } + } else { + Log.d(TAG, "not setting connection policy for HEADSET, no MODIFIY_PHONE_STATE permission") } } } @@ -2720,6 +3142,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList if (config.deviceName != name) { config.deviceName = name + device?.alias = name sharedPreferences.edit { putString("name", name) } } @@ -2740,7 +3163,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList e.printStackTrace() } try { - unregisterReceiver(ancModeReceiver) + unregisterReceiver(externalBroadcastReceiver) } catch (e: Exception) { e.printStackTrace() } @@ -2759,9 +3182,11 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } catch (e: Exception) { e.printStackTrace() } - telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE) - isConnectedLocally = false - CrossDevice.isAvailable = true + if (checkSelfPermission("android.permission.READ_PHONE_STATE") == PackageManager.PERMISSION_GRANTED) { + telephonyManager.unregisterTelephonyCallback(phoneStateListener) + } +// isConnectedLocally = false +// CrossDevice.isAvailable = true super.onDestroy() } @@ -2769,8 +3194,12 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList fun startHeadTracking() { isHeadTrackingActive = true - val useAlternatePackets = sharedPreferences.getBoolean("use_alternate_head_tracking_packets", false) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && aacpManager.getControlCommandStatus(AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION)?.value?.get(0)?.toInt() != 1) { + val useAlternatePackets = + sharedPreferences.getBoolean("use_alternate_head_tracking_packets", true) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && aacpManager.getControlCommandStatus( + AACPManager.Companion.ControlCommandIdentifiers.OWNS_CONNECTION + )?.value?.get(0)?.toInt() != 1 + ) { takeOver("call", startHeadTrackingAgain = true) Log.d(TAG, "Taking over for head tracking") } else { @@ -2785,7 +3214,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } fun stopHeadTracking() { - val useAlternatePackets = sharedPreferences.getBoolean("use_alternate_head_tracking_packets", false) + val useAlternatePackets = + sharedPreferences.getBoolean("use_alternate_head_tracking_packets", true) if (useAlternatePackets) { aacpManager.sendDataPacket(aacpManager.createAlternateStopHeadTrackingPacket()) } else { @@ -2795,21 +3225,39 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList } @SuppressLint("MissingPermission") - fun reconnectFromSavedMac(){ + fun reconnectFromSavedMac() { val bluetoothAdapter = getSystemService(BluetoothManager::class.java).adapter device = bluetoothAdapter.bondedDevices.find { it.address == macAddress } if (device != null) { CoroutineScope(Dispatchers.IO).launch { - connectToSocket(device!!, manual = true) + Log.d(TAG, "connecting to $macAddress") + connectToSocket(bluetoothAdapter, device!!, manual = true) + connectAudio(this@AirPodsService, device!!) } } } - } private fun Int.dpToPx(): Int { val density = Resources.getSystem().displayMetrics.density return (this * density).toInt() } + +fun getNextMode(currentMode: Int, configByte: Int, offmodeEnabled: Boolean): Int { + val enabledModes = buildList { + if ((configByte and 0x01) != 0 && offmodeEnabled) add(1) + if ((configByte and 0x04) != 0) add(3) + if ((configByte and 0x08) != 0) add(4) + if ((configByte and 0x02) != 0) add(2) + } + Log.d(TAG, "currentMode: $currentMode, config: ${configByte.toString(2)}") + + if (enabledModes.isEmpty()) return currentMode + + val currentIndex = enabledModes.indexOf(currentMode) + val nextIndex = if (currentIndex == -1) 0 else (currentIndex + 1) % enabledModes.size + + return enabledModes[nextIndex] +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/services/AppListenerService.kt b/android/app/src/main/java/me/kavishdevar/librepods/services/AppListenerService.kt index a7ad97760..7eeea7c50 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/services/AppListenerService.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/services/AppListenerService.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) @@ -40,13 +40,13 @@ val cameraPackages = mutableSetOf( var cameraOpen = false private var currentCustomPackage: String? = null -class AppListenerService : AccessibilityService() { +class AppListenerService: AccessibilityService() { private lateinit var prefs: android.content.SharedPreferences private val preferenceChangeListener = android.content.SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key -> if (key == "custom_camera_package") { val newPackage = sharedPreferences.getString(key, null) currentCustomPackage?.let { cameraPackages.remove(it) } - if (newPackage != null && newPackage.isNotBlank()) { + if (!newPackage.isNullOrBlank()) { cameraPackages.add(newPackage) } currentCustomPackage = newPackage @@ -57,7 +57,7 @@ class AppListenerService : AccessibilityService() { super.onCreate() prefs = getSharedPreferences("settings", MODE_PRIVATE) val customPackage = prefs.getString("custom_camera_package", null) - if (customPackage != null && customPackage.isNotBlank()) { + if (!customPackage.isNullOrBlank()) { cameraPackages.add(customPackage) currentCustomPackage = customPackage } @@ -95,4 +95,4 @@ class AppListenerService : AccessibilityService() { } override fun onInterrupt() {} -} \ No newline at end of file +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/ui/theme/Color.kt b/android/app/src/main/java/me/kavishdevar/librepods/ui/theme/Color.kt deleted file mode 100644 index 662186ed7..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/ui/theme/Color.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - - -package me.kavishdevar.librepods.ui.theme - -import androidx.compose.ui.graphics.Color - -val Purple80 = Color(0xFFD0BCFF) -val PurpleGrey80 = Color(0xFFCCC2DC) -val Pink80 = Color(0xFFEFB8C8) - -val Purple40 = Color(0xFF6650a4) -val PurpleGrey40 = Color(0xFF625b71) -val Pink40 = Color(0xFF7D5260) \ No newline at end of file diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/ATTManager.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/ATTManager.kt deleted file mode 100644 index 41c6116f8..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/ATTManager.kt +++ /dev/null @@ -1,233 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - - /* This is a very basic ATT (Attribute Protocol) implementation. I have only implemented - * what is necessary for LibrePods to function, i.e. reading and writing characteristics, - * and receiving notifications. It is not a complete implementation of the ATT protocol. - */ - -package me.kavishdevar.librepods.utils - -import android.annotation.SuppressLint -import android.bluetooth.BluetoothDevice -import android.bluetooth.BluetoothSocket -import android.os.ParcelUuid -import android.util.Log -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import org.lsposed.hiddenapibypass.HiddenApiBypass -import java.io.InputStream -import java.io.OutputStream -import java.util.concurrent.LinkedBlockingQueue -import java.util.concurrent.TimeUnit - -enum class ATTHandles(val value: Int) { - TRANSPARENCY(0x18), - LOUD_SOUND_REDUCTION(0x1B), - HEARING_AID(0x2A), -} - -enum class ATTCCCDHandles(val value: Int) { - TRANSPARENCY(ATTHandles.TRANSPARENCY.value + 1), - LOUD_SOUND_REDUCTION(ATTHandles.LOUD_SOUND_REDUCTION.value + 1), - HEARING_AID(ATTHandles.HEARING_AID.value + 1), -} - -class ATTManager(private val device: BluetoothDevice) { - companion object { - private const val TAG = "ATTManager" - - private const val OPCODE_READ_REQUEST: Byte = 0x0A - private const val OPCODE_WRITE_REQUEST: Byte = 0x12 - private const val OPCODE_HANDLE_VALUE_NTF: Byte = 0x1B - } - - var socket: BluetoothSocket? = null - private var input: InputStream? = null - private var output: OutputStream? = null - private val listeners = mutableMapOf Unit>>() - private var notificationJob: kotlinx.coroutines.Job? = null - - // queue for non-notification PDUs (responses to requests) - private val responses = LinkedBlockingQueue() - - @SuppressLint("MissingPermission") - fun connect() { - HiddenApiBypass.addHiddenApiExemptions("Landroid/bluetooth/BluetoothSocket;") - val uuid = ParcelUuid.fromString("00000000-0000-0000-0000-000000000000") - - socket = createBluetoothSocket(device, uuid) - socket!!.connect() - input = socket!!.inputStream - output = socket!!.outputStream - Log.d(TAG, "Connected to ATT") - - notificationJob = CoroutineScope(Dispatchers.IO).launch { - while (socket?.isConnected == true) { - try { - val pdu = readPDU() - if (pdu.isNotEmpty() && pdu[0] == OPCODE_HANDLE_VALUE_NTF) { - // notification -> dispatch to listeners - val handle = (pdu[1].toInt() and 0xFF) or ((pdu[2].toInt() and 0xFF) shl 8) - val value = pdu.copyOfRange(3, pdu.size) - listeners[handle]?.forEach { listener -> - try { - listener(value) - Log.d(TAG, "Dispatched notification for handle $handle to listener, with value ${value.joinToString(" ") { String.format("%02X", it) }}") - } catch (e: Exception) { - Log.w(TAG, "Error in listener for handle $handle: ${e.message}") - } - } - } else { - // not a notification -> treat as a response for pending request(s) - responses.put(pdu) - } - } catch (e: Exception) { - Log.w(TAG, "Error reading notification/response: ${e.message}") - if (socket?.isConnected != true) break - } - } - } - } - - fun disconnect() { - try { - notificationJob?.cancel() - socket?.close() - } catch (e: Exception) { - Log.w(TAG, "Error closing socket: ${e.message}") - } - } - - fun registerListener(handle: ATTHandles, listener: (ByteArray) -> Unit) { - listeners.getOrPut(handle.value) { mutableListOf() }.add(listener) - } - - fun unregisterListener(handle: ATTHandles, listener: (ByteArray) -> Unit) { - listeners[handle.value]?.remove(listener) - } - - fun enableNotifications(handle: ATTHandles) { - write(ATTCCCDHandles.valueOf(handle.name), byteArrayOf(0x01, 0x00)) - } - - fun read(handle: ATTHandles): ByteArray { - val lsb = (handle.value and 0xFF).toByte() - val msb = ((handle.value shr 8) and 0xFF).toByte() - val pdu = byteArrayOf(OPCODE_READ_REQUEST, lsb, msb) - writeRaw(pdu) - // wait for response placed into responses queue by the reader coroutine - return readResponse() - } - - fun write(handle: ATTHandles, value: ByteArray) { - val lsb = (handle.value and 0xFF).toByte() - val msb = ((handle.value shr 8) and 0xFF).toByte() - val pdu = byteArrayOf(OPCODE_WRITE_REQUEST, lsb, msb) + value - writeRaw(pdu) - // usually a Write Response (0x13) will arrive; wait for it (but discard return) - try { - readResponse() - } catch (e: Exception) { - Log.w(TAG, "No write response received: ${e.message}") - } - } - - fun write(handle: ATTCCCDHandles, value: ByteArray) { - val lsb = (handle.value and 0xFF).toByte() - val msb = ((handle.value shr 8) and 0xFF).toByte() - val pdu = byteArrayOf(OPCODE_WRITE_REQUEST, lsb, msb) + value - writeRaw(pdu) - // usually a Write Response (0x13) will arrive; wait for it (but discard return) - try { - readResponse() - } catch (e: Exception) { - Log.w(TAG, "No write response received: ${e.message}") - } - } - - private fun writeRaw(pdu: ByteArray) { - output?.write(pdu) - output?.flush() - Log.d(TAG, "writeRaw: ${pdu.joinToString(" ") { String.format("%02X", it) }}") - } - - // rename / specialize: read raw PDU directly from input stream (blocking) - private fun readPDU(): ByteArray { - val inp = input ?: throw IllegalStateException("Not connected") - val buffer = ByteArray(512) - val len = inp.read(buffer) - if (len == -1) { - disconnect() - throw IllegalStateException("End of stream reached") - } - val data = buffer.copyOfRange(0, len) - Log.d(TAG, "readPDU: ${data.joinToString(" ") { String.format("%02X", it) }}") - return data - } - - // wait for a response PDU produced by the background reader - private fun readResponse(timeoutMs: Long = 2000): ByteArray { - try { - val resp = responses.poll(timeoutMs, TimeUnit.MILLISECONDS) - ?: throw IllegalStateException("No response read from ATT socket within $timeoutMs ms") - Log.d(TAG, "readResponse: ${resp.joinToString(" ") { String.format("%02X", it) }}") - return resp.copyOfRange(1, resp.size) - } catch (e: InterruptedException) { - Thread.currentThread().interrupt() - throw IllegalStateException("Interrupted while waiting for ATT response", e) - } - } - - private fun createBluetoothSocket(device: BluetoothDevice, uuid: ParcelUuid): BluetoothSocket { - val type = 3 // L2CAP - val constructorSpecs = listOf( - arrayOf(device, type, true, true, 31, uuid), - arrayOf(device, type, 1, true, true, 31, uuid), - arrayOf(type, 1, true, true, device, 31, uuid), - arrayOf(type, true, true, device, 31, uuid) - ) - - val constructors = BluetoothSocket::class.java.declaredConstructors - Log.d("ATTManager", "BluetoothSocket has ${constructors.size} constructors:") - - constructors.forEachIndexed { index, constructor -> - val params = constructor.parameterTypes.joinToString(", ") { it.simpleName } - Log.d("ATTManager", "Constructor $index: ($params)") - } - - var lastException: Exception? = null - var attemptedConstructors = 0 - - for ((index, params) in constructorSpecs.withIndex()) { - try { - Log.d("ATTManager", "Trying constructor signature #${index + 1}") - attemptedConstructors++ - return HiddenApiBypass.newInstance(BluetoothSocket::class.java, *params) as BluetoothSocket - } catch (e: Exception) { - Log.e("ATTManager", "Constructor signature #${index + 1} failed: ${e.message}") - lastException = e - } - } - - val errorMessage = "Failed to create BluetoothSocket after trying $attemptedConstructors constructor signatures" - Log.e("ATTManager", errorMessage) - throw lastException ?: IllegalStateException(errorMessage) - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/AirPods.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/AirPods.kt deleted file mode 100644 index a43a5ee17..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/AirPods.kt +++ /dev/null @@ -1,233 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.utils - -import me.kavishdevar.librepods.utils.AACPManager -import me.kavishdevar.librepods.utils.ATTManager -import me.kavishdevar.librepods.R - -open class AirPodsBase( - val modelNumber: List, - val name: String, - val displayName: String = "AirPods", - val manufacturer: String = "Apple Inc.", - val budCaseRes: Int, - val budsRes: Int, - val leftBudsRes: Int, - val rightBudsRes: Int, - val caseRes: Int, - val capabilities: Set -) -enum class Capability { - LISTENING_MODE, - CONVERSATION_AWARENESS, - STEM_CONFIG, - HEAD_GESTURES, - LOUD_SOUND_REDUCTION, - PPE, - SLEEP_DETECTION, - HEARING_AID, - ADAPTIVE_AUDIO, - ADAPTIVE_VOLUME, - SWIPE_FOR_VOLUME, - HRM -} - -class AirPods: AirPodsBase( - modelNumber = listOf("A1523", "A1722"), - name = "AirPods 1", - budCaseRes = R.drawable.airpods_1, - budsRes = R.drawable.airpods_1_buds, - leftBudsRes = R.drawable.airpods_1_left, - rightBudsRes = R.drawable.airpods_1_right, - caseRes = R.drawable.airpods_1_case, - capabilities = emptySet() -) - -class AirPods2: AirPodsBase( - modelNumber = listOf("A2032", "A2031"), - name = "AirPods 2", - budCaseRes = R.drawable.airpods_2, - budsRes = R.drawable.airpods_2_buds, - leftBudsRes = R.drawable.airpods_2_left, - rightBudsRes = R.drawable.airpods_2_right, - caseRes = R.drawable.airpods_2_case, - capabilities = emptySet() -) - -class AirPods3: AirPodsBase( - modelNumber = listOf("A2565", "A2564"), - name = "AirPods 3", - budCaseRes = R.drawable.airpods_3, - budsRes = R.drawable.airpods_3_buds, - leftBudsRes = R.drawable.airpods_3_left, - rightBudsRes = R.drawable.airpods_3_right, - caseRes = R.drawable.airpods_3_case, - capabilities = setOf( - Capability.HEAD_GESTURES - ) -) - -class AirPods4: AirPodsBase( - modelNumber = listOf("A3053", "A3050", "A3054"), - name = "AirPods 4", - budCaseRes = R.drawable.airpods_4, - budsRes = R.drawable.airpods_4_buds, - leftBudsRes = R.drawable.airpods_4_left, - rightBudsRes = R.drawable.airpods_4_right, - caseRes = R.drawable.airpods_4_case, - capabilities = setOf( - Capability.HEAD_GESTURES, - Capability.SLEEP_DETECTION, - Capability.ADAPTIVE_VOLUME - ) -) - -class AirPods4ANC: AirPodsBase( - modelNumber = listOf("A3056", "A3055", "A3057"), - name = "AirPods 4 (ANC)", - budCaseRes = R.drawable.airpods_4, - budsRes = R.drawable.airpods_4_buds, - leftBudsRes = R.drawable.airpods_4_left, - rightBudsRes = R.drawable.airpods_4_right, - caseRes = R.drawable.airpods_4_case, - capabilities = setOf( - Capability.LISTENING_MODE, - Capability.CONVERSATION_AWARENESS, - Capability.HEAD_GESTURES, - Capability.ADAPTIVE_AUDIO, - Capability.SLEEP_DETECTION, - Capability.ADAPTIVE_VOLUME - ) -) - -class AirPodsPro1: AirPodsBase( - modelNumber = listOf("A2084", "A2083"), - name = "AirPods Pro 1", - displayName = "AirPods Pro", - budCaseRes = R.drawable.airpods_pro_1, - budsRes = R.drawable.airpods_pro_1_buds, - leftBudsRes = R.drawable.airpods_pro_1_left, - rightBudsRes = R.drawable.airpods_pro_1_right, - caseRes = R.drawable.airpods_pro_1_case, - capabilities = setOf( - Capability.LISTENING_MODE - ) -) - -class AirPodsPro2Lightning: AirPodsBase( - modelNumber = listOf("A2931", "A2699", "A2698"), - name = "AirPods Pro 2 with Magsafe Charging Case (Lightning)", - displayName = "AirPods Pro", - budCaseRes = R.drawable.airpods_pro_2, - budsRes = R.drawable.airpods_pro_2_buds, - leftBudsRes = R.drawable.airpods_pro_2_left, - rightBudsRes = R.drawable.airpods_pro_2_right, - caseRes = R.drawable.airpods_pro_2_case, - capabilities = setOf( - Capability.LISTENING_MODE, - Capability.CONVERSATION_AWARENESS, - Capability.STEM_CONFIG, - Capability.LOUD_SOUND_REDUCTION, - Capability.SLEEP_DETECTION, - Capability.HEARING_AID, - Capability.ADAPTIVE_AUDIO, - Capability.ADAPTIVE_VOLUME, - Capability.SWIPE_FOR_VOLUME - ) -) - -class AirPodsPro2USBC: AirPodsBase( - modelNumber = listOf("A3047", "A3048", "A3049"), - name = "AirPods Pro 2 with Magsafe Charging Case (USB-C)", - displayName = "AirPods Pro", - budCaseRes = R.drawable.airpods_pro_2, - budsRes = R.drawable.airpods_pro_2_buds, - leftBudsRes = R.drawable.airpods_pro_2_left, - rightBudsRes = R.drawable.airpods_pro_2_right, - caseRes = R.drawable.airpods_pro_2_case, - capabilities = setOf( - Capability.LISTENING_MODE, - Capability.CONVERSATION_AWARENESS, - Capability.STEM_CONFIG, - Capability.LOUD_SOUND_REDUCTION, - Capability.SLEEP_DETECTION, - Capability.HEARING_AID, - Capability.ADAPTIVE_AUDIO, - Capability.ADAPTIVE_VOLUME, - Capability.SWIPE_FOR_VOLUME - ) -) - -class AirPodsPro3: AirPodsBase( - modelNumber = listOf("A3063", "A3064", "A3065"), - name = "AirPods Pro 3", - displayName = "AirPods Pro", - budCaseRes = R.drawable.airpods_pro_3, - budsRes = R.drawable.airpods_pro_3_buds, - leftBudsRes = R.drawable.airpods_pro_3_left, - rightBudsRes = R.drawable.airpods_pro_3_right, - caseRes = R.drawable.airpods_pro_3_case, - capabilities = setOf( - Capability.LISTENING_MODE, - Capability.CONVERSATION_AWARENESS, - Capability.HEAD_GESTURES, - Capability.STEM_CONFIG, - Capability.LOUD_SOUND_REDUCTION, - Capability.PPE, - Capability.SLEEP_DETECTION, - Capability.HEARING_AID, - Capability.ADAPTIVE_AUDIO, - Capability.ADAPTIVE_VOLUME, - Capability.SWIPE_FOR_VOLUME, - Capability.HRM - ) -) - -data class AirPodsInstance( - val name: String, - val model: AirPodsBase, - val actualModelNumber: String, - val serialNumber: String?, - val leftSerialNumber: String?, - val rightSerialNumber: String?, - val version1: String?, - val version2: String?, - val version3: String?, - val aacpManager: AACPManager, - val attManager: ATTManager? -) - -object AirPodsModels { - val models: List = listOf( - AirPods(), - AirPods2(), - AirPods3(), - AirPods4(), - AirPods4ANC(), - AirPodsPro1(), - AirPodsPro2Lightning(), - AirPodsPro2USBC(), - AirPodsPro3() - ) - - fun getModelByModelNumber(modelNumber: String): AirPodsBase? { - return models.find { modelNumber in it.modelNumber } - } -} \ No newline at end of file diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/BluetoothConnectionManager.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/BluetoothConnectionManager.kt deleted file mode 100644 index 5655793e6..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/BluetoothConnectionManager.kt +++ /dev/null @@ -1,40 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple's ecosystem - * - * Copyright (C) 2025 LibrePods Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package me.kavishdevar.librepods.utils - -import android.bluetooth.BluetoothDevice -import android.bluetooth.BluetoothSocket -import android.util.Log - -object BluetoothConnectionManager { - private const val TAG = "BluetoothConnectionManager" - - private var currentSocket: BluetoothSocket? = null - private var currentDevice: BluetoothDevice? = null - - fun setCurrentConnection(socket: BluetoothSocket, device: BluetoothDevice) { - currentSocket = socket - currentDevice = device - Log.d(TAG, "Current connection set to device: ${device.address}") - } - - fun getCurrentSocket(): BluetoothSocket? { - return currentSocket - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/BluetoothCryptography.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/BluetoothCryptography.kt index 633ee4665..80d3a456c 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/BluetoothCryptography.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/BluetoothCryptography.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ package me.kavishdevar.librepods.utils diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/CrossDevice.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/CrossDevice.kt deleted file mode 100644 index 3e91c2838..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/CrossDevice.kt +++ /dev/null @@ -1,289 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -@file:OptIn(ExperimentalEncodingApi::class) - -package me.kavishdevar.librepods.utils - -import android.annotation.SuppressLint -import android.bluetooth.BluetoothAdapter -import android.bluetooth.BluetoothManager -import android.bluetooth.BluetoothServerSocket -import android.bluetooth.BluetoothSocket -import android.bluetooth.le.AdvertiseCallback -import android.bluetooth.le.AdvertiseData -import android.bluetooth.le.AdvertiseSettings -import android.bluetooth.le.BluetoothLeAdvertiser -import android.content.Context -import android.content.Intent -import android.content.SharedPreferences -import android.os.ParcelUuid -import android.util.Log -import androidx.core.content.edit -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import me.kavishdevar.librepods.services.ServiceManager -import java.io.IOException -import java.util.UUID -import kotlin.io.encoding.ExperimentalEncodingApi - -enum class CrossDevicePackets(val packet: ByteArray) { - AIRPODS_CONNECTED(byteArrayOf(0x00, 0x01, 0x00, 0x01)), - AIRPODS_DISCONNECTED(byteArrayOf(0x00, 0x01, 0x00, 0x00)), - REQUEST_DISCONNECT(byteArrayOf(0x00, 0x02, 0x00, 0x00)), - REQUEST_BATTERY_BYTES(byteArrayOf(0x00, 0x02, 0x00, 0x01)), - REQUEST_ANC_BYTES(byteArrayOf(0x00, 0x02, 0x00, 0x02)), - REQUEST_CONNECTION_STATUS(byteArrayOf(0x00, 0x02, 0x00, 0x03)), - AIRPODS_DATA_HEADER(byteArrayOf(0x00, 0x04, 0x00, 0x01)), -} - - -object CrossDevice { - var initialized = false - private val uuid = UUID.fromString("1abbb9a4-10e4-4000-a75c-8953c5471342") - private var serverSocket: BluetoothServerSocket? = null - private var clientSocket: BluetoothSocket? = null - private lateinit var bluetoothAdapter: BluetoothAdapter - private lateinit var bluetoothLeAdvertiser: BluetoothLeAdvertiser - private const val MANUFACTURER_ID = 0x1234 - private const val MANUFACTURER_DATA = "ALN_AirPods" - var isAvailable: Boolean = false // set to true when airpods are connected to another device - var batteryBytes: ByteArray = byteArrayOf() - var ancBytes: ByteArray = byteArrayOf() - private lateinit var sharedPreferences: SharedPreferences - private const val PACKET_LOG_KEY = "packet_log" - private var earDetectionStatus = listOf(false, false) - var disconnectionRequested = false - - @SuppressLint("MissingPermission") - fun init(context: Context) { - CoroutineScope(Dispatchers.IO).launch { - Log.d("CrossDevice", "Initializing CrossDevice") - sharedPreferences = context.getSharedPreferences("packet_logs", Context.MODE_PRIVATE) - sharedPreferences.edit { putBoolean("CrossDeviceIsAvailable", false)} - this@CrossDevice.bluetoothAdapter = context.getSystemService(BluetoothManager::class.java).adapter - this@CrossDevice.bluetoothLeAdvertiser = bluetoothAdapter.bluetoothLeAdvertiser - // startAdvertising() - startServer() - initialized = true - } - } - - @SuppressLint("MissingPermission") - private fun startServer() { - CoroutineScope(Dispatchers.IO).launch { - if (!bluetoothAdapter.isEnabled) return@launch -// serverSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("ALNCrossDevice", uuid) - Log.d("CrossDevice", "Server started") - while (serverSocket != null) { - if (!bluetoothAdapter.isEnabled) { - serverSocket?.close() - break - } - if (clientSocket != null) { - try { - clientSocket!!.close() - } catch (e: IOException) { - e.printStackTrace() - } - } - try { - val socket = serverSocket!!.accept() - handleClientConnection(socket) - } catch (e: IOException) { } - } - } - } - - @SuppressLint("MissingPermission", "unused") - private fun startAdvertising() { - CoroutineScope(Dispatchers.IO).launch { - val settings = AdvertiseSettings.Builder() - .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY) - .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH) - .setConnectable(true) - .build() - - val data = AdvertiseData.Builder() - .setIncludeDeviceName(true) - .addManufacturerData(MANUFACTURER_ID, MANUFACTURER_DATA.toByteArray()) - .addServiceUuid(ParcelUuid(uuid)) - .build() - try { - bluetoothLeAdvertiser.startAdvertising(settings, data, advertiseCallback) - } catch (e: Exception) { - Log.e("CrossDevice", "Failed to start BLE Advertising: ${e.message}") - } - Log.d("CrossDevice", "BLE Advertising started") - } - } - - private val advertiseCallback = object : AdvertiseCallback() { - override fun onStartSuccess(settingsInEffect: AdvertiseSettings) { - Log.d("CrossDevice", "BLE Advertising started successfully") - } - - override fun onStartFailure(errorCode: Int) { - Log.e("CrossDevice", "BLE Advertising failed with error code: $errorCode") - } - } - - fun setAirPodsConnected(connected: Boolean) { - if (connected) { - isAvailable = false - sharedPreferences.edit { putBoolean("CrossDeviceIsAvailable", false)} - clientSocket?.outputStream?.write(CrossDevicePackets.AIRPODS_CONNECTED.packet) - } else { - clientSocket?.outputStream?.write(CrossDevicePackets.AIRPODS_DISCONNECTED.packet) - // Reset state variables - isAvailable = true - } - } - - fun sendReceivedPacket(packet: ByteArray) { - if (clientSocket == null || clientSocket!!.outputStream != null) { - return - } - clientSocket?.outputStream?.write(CrossDevicePackets.AIRPODS_DATA_HEADER.packet + packet) - } - - private fun logPacket(packet: ByteArray, source: String) { - val packetHex = packet.joinToString(" ") { "%02X".format(it) } - val logEntry = "$source: $packetHex" - val logs = sharedPreferences.getStringSet(PACKET_LOG_KEY, mutableSetOf())?.toMutableSet() ?: mutableSetOf() - logs.add(logEntry) - sharedPreferences.edit { putStringSet(PACKET_LOG_KEY, logs)} - } - - @SuppressLint("MissingPermission") - private fun handleClientConnection(socket: BluetoothSocket) { - Log.d("CrossDevice", "Client connected") - notifyAirPodsConnectedRemotely(ServiceManager.getService()?.applicationContext!!) - clientSocket = socket - val inputStream = socket.inputStream - val buffer = ByteArray(1024) - var bytes: Int - setAirPodsConnected(ServiceManager.getService()?.isConnectedLocally == true) - while (true) { - try { - bytes = inputStream.read(buffer) - } catch (e: IOException) { - e.printStackTrace() - notifyAirPodsDisconnectedRemotely(ServiceManager.getService()?.applicationContext!!) - val s = serverSocket?.accept() - if (s != null) { - handleClientConnection(s) - } - break - } - var packet = buffer.copyOf(bytes) - logPacket(packet, "Relay") - Log.d("CrossDevice", "Received packet: ${packet.joinToString("") { "%02x".format(it) }}") - if (bytes == -1) { - notifyAirPodsDisconnectedRemotely(ServiceManager.getService()?.applicationContext!!) - break - } else if (packet.contentEquals(CrossDevicePackets.REQUEST_DISCONNECT.packet) || packet.contentEquals(CrossDevicePackets.REQUEST_DISCONNECT.packet + CrossDevicePackets.AIRPODS_DATA_HEADER.packet)) { - ServiceManager.getService()?.disconnectForCD() - disconnectionRequested = true - CoroutineScope(Dispatchers.IO).launch { - delay(1000) - disconnectionRequested = false - } - } else if (packet.contentEquals(CrossDevicePackets.AIRPODS_CONNECTED.packet)) { - isAvailable = true - sharedPreferences.edit { putBoolean("CrossDeviceIsAvailable", true)} - } else if (packet.contentEquals(CrossDevicePackets.AIRPODS_DISCONNECTED.packet)) { - isAvailable = false - sharedPreferences.edit { putBoolean("CrossDeviceIsAvailable", false)} - } else if (packet.contentEquals(CrossDevicePackets.REQUEST_BATTERY_BYTES.packet)) { - Log.d("CrossDevice", "Received battery request, battery data: ${batteryBytes.joinToString("") { "%02x".format(it) }}") - sendRemotePacket(batteryBytes) - } else if (packet.contentEquals(CrossDevicePackets.REQUEST_ANC_BYTES.packet)) { - Log.d("CrossDevice", "Received ANC request") - sendRemotePacket(ancBytes) - } else if (packet.contentEquals(CrossDevicePackets.REQUEST_CONNECTION_STATUS.packet)) { - Log.d("CrossDevice", "Received connection status request") - sendRemotePacket(if (ServiceManager.getService()?.isConnectedLocally == true) CrossDevicePackets.AIRPODS_CONNECTED.packet else CrossDevicePackets.AIRPODS_DISCONNECTED.packet) - } else { - if (packet.sliceArray(0..3).contentEquals(CrossDevicePackets.AIRPODS_DATA_HEADER.packet)) { - isAvailable = true - sharedPreferences.edit { putBoolean("CrossDeviceIsAvailable", true) } - if (packet.size % 2 == 0) { - val half = packet.size / 2 - if (packet.sliceArray(0 until half).contentEquals(packet.sliceArray(half until packet.size))) { - Log.d("CrossDevice", "Duplicated packet, trimming") - packet = packet.sliceArray(0 until half) - } - } - var trimmedPacket = packet.drop(CrossDevicePackets.AIRPODS_DATA_HEADER.packet.size).toByteArray() - Log.d("CrossDevice", "Received relayed packet: ${trimmedPacket.joinToString("") { "%02x".format(it) }}") - if (ServiceManager.getService()?.isConnectedLocally == true) { - val packetInHex = trimmedPacket.joinToString("") { "%02x".format(it) } -// ServiceManager.getService()?.sendPacket(packetInHex) - } else if (ServiceManager.getService()?.batteryNotification?.isBatteryData(trimmedPacket) == true) { - batteryBytes = trimmedPacket - ServiceManager.getService()?.batteryNotification?.setBattery(trimmedPacket) - Log.d("CrossDevice", "Battery data: ${ServiceManager.getService()?.batteryNotification?.getBattery()[0]?.level}") - ServiceManager.getService()?.updateBattery() - ServiceManager.getService()?.sendBatteryBroadcast() - ServiceManager.getService()?.sendBatteryNotification() - } else if (ServiceManager.getService()?.ancNotification?.isANCData(trimmedPacket) == true) { - ServiceManager.getService()?.ancNotification?.setStatus(trimmedPacket) - ServiceManager.getService()?.sendANCBroadcast() - ServiceManager.getService()?.updateNoiseControlWidget() - ancBytes = trimmedPacket - } else if (ServiceManager.getService()?.earDetectionNotification?.isEarDetectionData(trimmedPacket) == true) { - Log.d("CrossDevice", "Ear detection data: ${trimmedPacket.joinToString("") { "%02x".format(it) }}") - ServiceManager.getService()?.earDetectionNotification?.setStatus(trimmedPacket) - val newEarDetectionStatus = listOf( - ServiceManager.getService()?.earDetectionNotification?.status?.get(0) == 0x00.toByte(), - ServiceManager.getService()?.earDetectionNotification?.status?.get(1) == 0x00.toByte() - ) - if (earDetectionStatus == listOf(false, false) && newEarDetectionStatus.contains(true)) { - ServiceManager.getService()?.applicationContext?.sendBroadcast( - Intent("me.kavishdevar.librepods.cross_device_island") - ) - } - earDetectionStatus = newEarDetectionStatus - } - } - } - } - } - - fun sendRemotePacket(byteArray: ByteArray) { - if (clientSocket == null || clientSocket!!.outputStream == null) { - return - } - clientSocket?.outputStream?.write(byteArray) - clientSocket?.outputStream?.flush() - logPacket(byteArray, "Sent") - Log.d("CrossDevice", "Sent packet to remote device") - } - - fun notifyAirPodsConnectedRemotely(context: Context) { - val intent = Intent("me.kavishdevar.librepods.AIRPODS_CONNECTED_REMOTELY") - context.sendBroadcast(intent) - } - fun notifyAirPodsDisconnectedRemotely(context: Context) { - val intent = Intent("me.kavishdevar.librepods.AIRPODS_DISCONNECTED_REMOTELY") - context.sendBroadcast(intent) - } -} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/DragUtils.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/DragUtils.kt index 55b1eef23..e10ea20d5 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/DragUtils.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/DragUtils.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ package me.kavishdevar.librepods.utils diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/GestureDetector.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/GestureDetector.kt index 804d4cb61..9892096bc 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/GestureDetector.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/GestureDetector.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/GestureFeedback.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/GestureFeedback.kt index b7d406842..88ab8cf5f 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/GestureFeedback.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/GestureFeedback.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:Suppress("PrivatePropertyName") diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/HeadOrientation.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/HeadOrientation.kt index ad2d41841..ebdf91490 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/HeadOrientation.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/HeadOrientation.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ package me.kavishdevar.librepods.utils diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/KotlinModule.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/KotlinModule.kt index e2d5046cf..b32892686 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/KotlinModule.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/KotlinModule.kt @@ -2,762 +2,154 @@ package me.kavishdevar.librepods.utils import android.annotation.SuppressLint import android.content.Context -import android.content.Intent -import android.content.pm.ApplicationInfo -import android.content.res.ColorStateList -import android.graphics.Color -import android.graphics.drawable.GradientDrawable -import android.os.ParcelUuid +import android.os.Handler +import android.os.Looper import android.util.Log -import android.view.Gravity -import android.view.View -import android.view.ViewGroup -import android.view.animation.AccelerateInterpolator -import android.view.animation.DecelerateInterpolator -import android.widget.FrameLayout -import android.widget.ImageButton import android.widget.ImageView -import android.widget.LinearLayout import androidx.core.net.toUri -import io.github.libxposed.api.XposedInterface -import io.github.libxposed.api.XposedInterface.AfterHookCallback import io.github.libxposed.api.XposedModule -import io.github.libxposed.api.XposedModuleInterface import io.github.libxposed.api.XposedModuleInterface.ModuleLoadedParam -import io.github.libxposed.api.annotations.AfterInvocation -import io.github.libxposed.api.annotations.XposedHooker +import io.github.libxposed.api.XposedModuleInterface.PackageLoadedParam + +private const val TAG = "LibrePodsHook" -private const val TAG = "AirPodsHook" -private lateinit var module: KotlinModule @SuppressLint("DiscouragedApi", "PrivateApi") -class KotlinModule(base: XposedInterface, param: ModuleLoadedParam): XposedModule(base, param) { - init { - Log.i(TAG, "AirPodsHook module initialized at :: ${param.processName}") - module = this +class KotlinModule: XposedModule() { + override fun onModuleLoaded(param: ModuleLoadedParam) { + log(Log.INFO, TAG, "module initialized at :: ${param.processName}") + log(Log.INFO, TAG, "framework: $frameworkName($frameworkVersionCode) API $apiVersion") } - override fun onPackageLoaded(param: XposedModuleInterface.PackageLoadedParam) { - super.onPackageLoaded(param) - Log.i(TAG, "onPackageLoaded :: ${param.packageName}") + @SuppressLint("UnsafeDynamicallyLoadedCode") + override fun onPackageLoaded(param: PackageLoadedParam) { + log(Log.INFO, TAG, "onPackageLoaded :: ${param.packageName}") if (param.packageName == "com.google.android.bluetooth" || param.packageName == "com.android.bluetooth") { - Log.i(TAG, "Bluetooth app detected, hooking l2c_fcr_chk_chan_modes") - + log(Log.INFO, TAG, "Bluetooth app detected, hooking l2c_fcr_chk_chan_modes") try { if (param.isFirstPackage) { - Log.i(TAG, "Loading native library for Bluetooth hook") - System.loadLibrary("l2c_fcr_hook") - Log.i(TAG, "Native library loaded successfully") - } - } catch (e: Exception) { - Log.e(TAG, "Failed to load native library: ${e.message}", e) - } - } - - if (param.packageName == "com.google.android.settings") { - Log.i(TAG, "Settings app detected, hooking Bluetooth icon handling") - try { - val headerControllerClass = param.classLoader.loadClass( - "com.google.android.settings.bluetooth.AdvancedBluetoothDetailsHeaderController") - - val updateIconMethod = headerControllerClass.getDeclaredMethod( - "updateIcon", - ImageView::class.java, - String::class.java) - - hook(updateIconMethod, BluetoothIconHooker::class.java) - Log.i(TAG, "Successfully hooked updateIcon method in Bluetooth settings") - - try { - val displayPreferenceMethod = headerControllerClass.getDeclaredMethod( - "displayPreference", - param.classLoader.loadClass("androidx.preference.PreferenceScreen")) - - hook(displayPreferenceMethod, BluetoothSettingsAirPodsHooker::class.java) - Log.i(TAG, "Successfully hooked displayPreference for AirPods button injection") - } catch (e: Exception) { - Log.e(TAG, "Failed to hook displayPreference: ${e.message}", e) - } - } catch (e: Exception) { - Log.e(TAG, "Failed to hook Bluetooth icon handler: ${e.message}", e) - } - } - - if (param.packageName == "com.android.settings") { - Log.i(TAG, "Settings app detected, hooking Bluetooth icon handling") - try { - val headerControllerClass = param.classLoader.loadClass( - "com.android.settings.bluetooth.AdvancedBluetoothDetailsHeaderController") - - val updateIconMethod = headerControllerClass.getDeclaredMethod( - "updateIcon", - ImageView::class.java, - String::class.java) - - hook(updateIconMethod, BluetoothIconHooker::class.java) - Log.i(TAG, "Successfully hooked updateIcon method in Bluetooth settings") - - try { - val displayPreferenceMethod = headerControllerClass.getDeclaredMethod( - "displayPreference", - param.classLoader.loadClass("androidx.preference.PreferenceScreen")) - - hook(displayPreferenceMethod, BluetoothSettingsAirPodsHooker::class.java) - Log.i(TAG, "Successfully hooked displayPreference for AirPods button injection") - } catch (e: Exception) { - Log.e(TAG, "Failed to hook displayPreference: ${e.message}", e) - } - } catch (e: Exception) { - Log.e(TAG, "Failed to hook Bluetooth icon handler: ${e.message}", e) - } - } - } - - @XposedHooker - class BluetoothSettingsAirPodsHooker : XposedInterface.Hooker { - companion object { - private const val AIRPODS_UUID = "74ec2172-0bad-4d01-8f77-997b2be0722a" - private const val LIBREPODS_PREFERENCE_KEY = "librepods_open_preference" - private const val ACTION_SET_ANC_MODE = "me.kavishdevar.librepods.SET_ANC_MODE" - private const val EXTRA_ANC_MODE = "anc_mode" - - private const val ANC_MODE_OFF = 1 - private const val ANC_MODE_NOISE_CANCELLATION = 2 - private const val ANC_MODE_TRANSPARENCY = 3 - private const val ANC_MODE_ADAPTIVE = 4 - - private var currentAncMode = ANC_MODE_NOISE_CANCELLATION - - @JvmStatic - @AfterInvocation - fun afterDisplayPreference(callback: AfterHookCallback) { - try { - val controller = callback.thisObject!! - val preferenceScreen = callback.args[0]!! - - val context = preferenceScreen.javaClass.getMethod("getContext").invoke(preferenceScreen) as Context - - val deviceField = controller.javaClass.getDeclaredField("mCachedDevice") - deviceField.isAccessible = true - val cachedDevice = deviceField.get(controller) ?: return - - val getDeviceMethod = cachedDevice.javaClass.getMethod("getDevice") - val bluetoothDevice = getDeviceMethod.invoke(cachedDevice) ?: return + val abi = android.os.Build.SUPPORTED_ABIS.first() + val soName = "libl2c_fcr_hook.so" - val uuidsMethod = bluetoothDevice.javaClass.getMethod("getUuids") - val uuids = uuidsMethod.invoke(bluetoothDevice) as? Array - - if (uuids != null) { - val isAirPods = uuids.any { it.uuid.toString() == AIRPODS_UUID } - - if (isAirPods) { - Log.i(TAG, "AirPods device detected in settings, injecting controls") - - val findPreferenceMethod = preferenceScreen.javaClass.getMethod("findPreference", CharSequence::class.java) - val existingPref = findPreferenceMethod.invoke(preferenceScreen, LIBREPODS_PREFERENCE_KEY) - - if (existingPref != null) { - Log.i(TAG, "LIBREPODS button already exists, skipping") - return - } - - val preferenceClass = preferenceScreen.javaClass.classLoader.loadClass("androidx.preference.Preference") - val preference = preferenceClass.getConstructor(Context::class.java).newInstance(context) - - val setKeyMethod = preferenceClass.getMethod("setKey", String::class.java) - setKeyMethod.invoke(preference, LIBREPODS_PREFERENCE_KEY) - - val setTitleMethod = preferenceClass.getMethod("setTitle", CharSequence::class.java) - setTitleMethod.invoke(preference, "Open LibrePods") - - val setSummaryMethod = preferenceClass.getMethod("setSummary", CharSequence::class.java) - setSummaryMethod.invoke(preference, "Control AirPods features") - - val setIconMethod = preferenceClass.getMethod("setIcon", Int::class.java) - setIconMethod.invoke(preference, android.R.drawable.ic_menu_manage) - - val setOrderMethod = preferenceClass.getMethod("setOrder", Int::class.java) - setOrderMethod.invoke(preference, 1000) - - val intent = Intent().apply { - setClassName("me.kavishdevar.librepods", "me.kavishdevar.librepods.MainActivity") - flags = Intent.FLAG_ACTIVITY_NEW_TASK - } - val setIntentMethod = preferenceClass.getMethod("setIntent", Intent::class.java) - setIntentMethod.invoke(preference, intent) + val candidates = buildList { + add("${moduleApplicationInfo.sourceDir}!/lib/$abi/$soName") - val addPreferenceMethod = preferenceScreen.javaClass.getMethod("addPreference", preferenceClass) - addPreferenceMethod.invoke(preferenceScreen, preference) - - Log.i(TAG, "Successfully added Open LIBREPODS button to AirPods settings") + moduleApplicationInfo.splitSourceDirs?.forEach { split -> + add("$split!/lib/$abi/$soName") } } - } catch (e: Exception) { - Log.e(TAG, "Error in BluetoothSettingsAirPodsHooker: ${e.message}", e) - e.printStackTrace() - } - } - } - } - - @XposedHooker - class BluetoothIconHooker : XposedInterface.Hooker { - companion object { - @JvmStatic - @AfterInvocation - fun afterUpdateIcon(callback: AfterHookCallback) { - Log.i(TAG, "BluetoothIconHooker called with args: ${callback.args.joinToString(", ")}") - try { - val imageView = callback.args[0] as ImageView - val iconUri = callback.args[1] as String - val uri = iconUri.toUri() - if (uri.toString().startsWith("android.resource://me.kavishdevar.librepods")) { - Log.i(TAG, "Handling AirPods icon URI: $uri") + var loaded = false + for (path in candidates) { try { - val context = imageView.context - - android.os.Handler(android.os.Looper.getMainLooper()).post { - try { - val packageName = uri.authority - val packageContext = context.createPackageContext( - packageName, - Context.CONTEXT_IGNORE_SECURITY - ) - - val resPath = uri.pathSegments - if (resPath.size >= 2 && resPath[0] == "drawable") { - val resourceName = resPath[1] - val resourceId = packageContext.resources.getIdentifier( - resourceName, "drawable", packageName - ) - - if (resourceId != 0) { - val drawable = packageContext.resources.getDrawable( - resourceId, packageContext.theme - ) - - imageView.setImageDrawable(drawable) - imageView.alpha = 1.0f - - callback.result = null - - Log.i(TAG, "Successfully loaded icon from resource: $resourceName") - } else { - Log.e(TAG, "Resource not found: $resourceName") - } - } - } catch (e: Exception) { - Log.e(TAG, "Error loading resource from URI $uri: ${e.message}") - } - } - } catch (e: Exception) { - Log.e(TAG, "Error accessing context: ${e.message}") + log(Log.INFO, TAG, "Trying to load native lib from $path") + System.load(path) + log(Log.INFO, TAG, "Loaded native lib from $path") + loaded = true + break + } catch (e: Throwable) { + log(Log.WARN, TAG, "Failed to load from $path: ${e.message}") } } - } catch (e: Exception) { - Log.e(TAG, "Error in BluetoothIconHooker: ${e.message}") - e.printStackTrace() - } - } - } - } - - override fun getApplicationInfo(): ApplicationInfo { - return super.applicationInfo - } - companion object { - private const val ANC_MODE_OFF = 1 - private const val ANC_MODE_NOISE_CANCELLATION = 2 - private const val ANC_MODE_TRANSPARENCY = 3 - private const val ANC_MODE_ADAPTIVE = 4 - - private var currentANCMode = ANC_MODE_NOISE_CANCELLATION - - private const val ACTION_SET_ANC_MODE = "me.kavishdevar.librepods.SET_ANC_MODE" - private const val EXTRA_ANC_MODE = "anc_mode" - private const val ANIMATION_DURATION = 250L - - private fun addAirPodsControlsToDialog(volumeDialog: Any) { - try { - val contextField = volumeDialog.javaClass.getDeclaredField("mContext") - contextField.isAccessible = true - val context = contextField.get(volumeDialog) as Context - - val dialogViewField = volumeDialog.javaClass.getDeclaredField("mDialogView") - dialogViewField.isAccessible = true - val dialogView = dialogViewField.get(volumeDialog) as ViewGroup - - val dialogRowsViewField = volumeDialog.javaClass.getDeclaredField("mDialogRowsView") - dialogRowsViewField.isAccessible = true - val dialogRowsView = dialogRowsViewField.get(volumeDialog) as ViewGroup - - Log.d(TAG, "Found dialogRowsView: ${dialogRowsView.javaClass.name}") - - val existingContainer = dialogView.findViewWithTag("airpods_container") - if (existingContainer != null) { - Log.d(TAG, "AirPods container already exists, ensuring visibility state") - val drawer = existingContainer.findViewWithTag("airpods_drawer_container") - drawer?.visibility = View.GONE - drawer?.alpha = 0f - drawer?.translationY = 0f - val button = existingContainer.findViewWithTag("airpods_button") - button?.visibility = View.VISIBLE - button?.alpha = 1f - if (button != null) { - updateMainButtonIcon(context, button, currentANCMode) - } - return - } - - val newAirPodsButton = ImageButton(context).apply { - tag = "airpods_button" - - try { - val airPodsPackage = context.createPackageContext( - "me.kavishdevar.librepods", - Context.CONTEXT_IGNORE_SECURITY - ) - val airPodsIconRes = airPodsPackage.resources.getIdentifier( - "airpods", "drawable", "me.kavishdevar.librepods") - - if (airPodsIconRes != 0) { - val airPodsDrawable = airPodsPackage.resources.getDrawable( - airPodsIconRes, airPodsPackage.theme) - setImageDrawable(airPodsDrawable) - } else { - setImageResource(android.R.drawable.ic_media_play) - Log.d(TAG, "Using fallback icon because airpods icon resource not found") - } - } catch (e: Exception) { - setImageResource(android.R.drawable.ic_media_play) - Log.e(TAG, "Failed to load AirPods icon: ${e.message}") + if (!loaded) { + log(Log.ERROR, TAG, "Could not load $soName from base or splits") + return } - val shape = GradientDrawable() - shape.shape = GradientDrawable.RECTANGLE - shape.setColor(Color.BLACK) - background = shape - - imageTintList = ColorStateList.valueOf(Color.WHITE) - scaleType = ImageView.ScaleType.CENTER_INSIDE - - setPadding(24, 24, 24, 24) - - val params = LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - 90 - ) - params.gravity = Gravity.CENTER - params.setMargins(0, 0, 0, 0) - layoutParams = params - - setOnClickListener { - Log.d(TAG, "AirPods button clicked, toggling drawer") - val container = findAirPodsContainer(this) - val drawerContainer = container?.findViewWithTag("airpods_drawer_container") - if (drawerContainer != null && container != null) { - if (drawerContainer.visibility == View.VISIBLE) { - hideAirPodsDrawer(container, this, drawerContainer) - } else { - showAirPodsDrawer(container, this, drawerContainer) - } - } else { - Log.e(TAG, "Could not find container or drawer for toggle") - } - } - - contentDescription = "AirPods Settings" - } - - val airPodsContainer = FrameLayout(context).apply { - layoutParams = LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.WRAP_CONTENT - ) - tag = "airpods_container" + val remotePrefValue = getRemotePreferences("me.kavishdevar.librepods").getBoolean("vendor_id_hook", false) + log(Log.INFO, TAG, "sdp hook enabled (remote pref): $remotePrefValue") + NativeBridge.setSdpHook(remotePrefValue) + log(Log.INFO, TAG, "Native library loaded successfully") } - - newAirPodsButton.setOnLongClickListener { - Log.d(TAG, "AirPods button long-pressed, opening QuickSettingsDialogActivity") - val intent = Intent().apply { - setClassName("me.kavishdevar.librepods", "me.kavishdevar.librepods.QuickSettingsDialogActivity") - flags = Intent.FLAG_ACTIVITY_NEW_TASK - } - context.startActivity(intent) - try { - val dismissMethod = volumeDialog.javaClass.getMethod("dismissH") - dismissMethod.invoke(volumeDialog) - } catch (e: Exception) { - Log.w(TAG, "Could not dismiss volume dialog: ${e.message}") - } - true - } - - val airPodsDrawer = LinearLayout(context).apply { - orientation = LinearLayout.VERTICAL - layoutParams = FrameLayout.LayoutParams( - FrameLayout.LayoutParams.MATCH_PARENT, - FrameLayout.LayoutParams.WRAP_CONTENT - ).apply { - gravity = Gravity.TOP - } - tag = "airpods_drawer_container" - visibility = View.GONE - alpha = 0f - - val drawerShape = GradientDrawable() - drawerShape.shape = GradientDrawable.RECTANGLE - drawerShape.setColor(Color.BLACK) - background = drawerShape - - setPadding(16, 8, 16, 8) - } - - val buttonContainer = LinearLayout(context).apply { - orientation = LinearLayout.VERTICAL - layoutParams = FrameLayout.LayoutParams( - FrameLayout.LayoutParams.MATCH_PARENT, - FrameLayout.LayoutParams.WRAP_CONTENT - ).apply { - gravity = Gravity.TOP - } - tag = "airpods_button_container" - } - - val modes = listOf(ANC_MODE_OFF, ANC_MODE_TRANSPARENCY, ANC_MODE_ADAPTIVE, ANC_MODE_NOISE_CANCELLATION) - for (mode in modes) { - val modeOption = createAncModeOption(context, mode, mode == currentANCMode, newAirPodsButton) - airPodsDrawer.addView(modeOption) - } - - buttonContainer.addView(newAirPodsButton) - - airPodsContainer.addView(airPodsDrawer) - airPodsContainer.addView(buttonContainer) - - val settingsViewField = try { - val field = volumeDialog.javaClass.getDeclaredField("mSettingsView") - field.isAccessible = true - field.get(volumeDialog) as? View - } catch (e: Exception) { - Log.e(TAG, "Failed to get settings view field: ${e.message}") - null - } - - if (settingsViewField != null && settingsViewField.parent is ViewGroup) { - val settingsParent = settingsViewField.parent as ViewGroup - val settingsIndex = findViewIndexInParent(settingsParent, settingsViewField) - - if (settingsIndex >= 0) { - settingsParent.addView(airPodsContainer, settingsIndex) - Log.i(TAG, "Added AirPods controls before settings button") - } else { - settingsParent.addView(airPodsContainer) - Log.i(TAG, "Added AirPods controls to the end of settings parent") - } - } else { - dialogView.addView(airPodsContainer) - Log.i(TAG, "Fallback: Added AirPods controls to dialog view") - } - - updateMainButtonIcon(context, newAirPodsButton, currentANCMode) - - Log.i(TAG, "Successfully added AirPods button and drawer to volume dialog") } catch (e: Exception) { - Log.e(TAG, "Error adding AirPods button to volume panel: ${e.message}") - e.printStackTrace() + log(Log.ERROR, TAG, "Failed to load native library: ${e.message}") } } - private fun findViewIndexInParent(parent: ViewGroup, view: View): Int { - for (i in 0 until parent.childCount) { - if (parent.getChildAt(i) == view) { - return i - } - } - return -1 + if (param.packageName == "com.google.android.settings") { + hookSettingsController(param, "com.google.android.settings.bluetooth.AdvancedBluetoothDetailsHeaderController") } - private fun updateMainButtonIcon(context: Context, button: ImageButton, mode: Int) { - try { - val pkgContext = context.createPackageContext( - "me.kavishdevar.librepods", - Context.CONTEXT_IGNORE_SECURITY - ) - - val resName = when (mode) { - ANC_MODE_OFF -> "noise_cancellation" - ANC_MODE_TRANSPARENCY -> "transparency" - ANC_MODE_ADAPTIVE -> "adaptive" - ANC_MODE_NOISE_CANCELLATION -> "noise_cancellation" - else -> "noise_cancellation" - } - - val resId = pkgContext.resources.getIdentifier( - resName, "drawable", "me.kavishdevar.librepods" - ) - - if (resId != 0) { - val drawable = pkgContext.resources.getDrawable(resId, pkgContext.theme) - button.setImageDrawable(drawable) - button.setColorFilter(Color.WHITE) - } else { - button.setImageResource(getIconResourceForMode(mode)) - button.setColorFilter(Color.WHITE) - } - } catch (e: Exception) { - button.setImageResource(getIconResourceForMode(mode)) - button.setColorFilter(Color.WHITE) - } + if (param.packageName == "com.android.settings") { + hookSettingsController(param, "com.android.settings.bluetooth.AdvancedBluetoothDetailsHeaderController") } + } - private fun createAncModeOption(context: Context, mode: Int, isSelected: Boolean, mainButton: ImageButton): LinearLayout { - return LinearLayout(context).apply { - orientation = LinearLayout.HORIZONTAL - layoutParams = LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.WRAP_CONTENT - ).apply { - setMargins(0, 6, 0, 6) - } - gravity = Gravity.CENTER - setPadding(24, 16, 24, 16) - tag = "anc_mode_${mode}" - - val icon = ImageView(context).apply { - layoutParams = LinearLayout.LayoutParams(60, 60).apply { - gravity = Gravity.CENTER - } - tag = "mode_icon_$mode" - - try { - val packageContext = context.createPackageContext( - "me.kavishdevar.librepods", - Context.CONTEXT_IGNORE_SECURITY - ) - - val resourceName = when (mode) { - ANC_MODE_OFF -> "noise_cancellation" - ANC_MODE_TRANSPARENCY -> "transparency" - ANC_MODE_ADAPTIVE -> "adaptive" - ANC_MODE_NOISE_CANCELLATION -> "noise_cancellation" - else -> "noise_cancellation" - } + private fun hookSettingsController(param: PackageLoadedParam, className: String) { + log(Log.INFO, TAG, "Settings app detected, hooking Bluetooth icon handling") + try { + val headerControllerClass = Class.forName(className, false, param.defaultClassLoader) + val updateIconMethod = headerControllerClass.getDeclaredMethod( + "updateIcon", + ImageView::class.java, + String::class.java + ) - val resourceId = packageContext.resources.getIdentifier( - resourceName, "drawable", "me.kavishdevar.librepods" - ) + hook(updateIconMethod).intercept { chain -> + try { + log(Log.INFO, TAG, "Bluetooth icon hook called with args: ${chain.args.joinToString(", ")}") + val imageView = chain.args[0] as? ImageView + val iconUri = chain.args[1] as? String - if (resourceId != 0) { - val drawable = packageContext.resources.getDrawable( - resourceId, packageContext.theme - ) - setImageDrawable(drawable) - } else { - setImageResource(getIconResourceForMode(mode)) - } - } catch (e: Exception) { - setImageResource(getIconResourceForMode(mode)) - Log.e(TAG, "Failed to load custom drawable for mode $mode: ${e.message}") + if (imageView == null || iconUri == null) { + return@intercept chain.proceed() } - if (isSelected) { - setColorFilter(Color.BLACK) - } else { - setColorFilter(Color.WHITE) + val uri = iconUri.toUri() + if (!uri.toString().startsWith("android.resource://me.kavishdevar.librepods")) { + return@intercept chain.proceed() } - } - - addView(icon) - - background = if (isSelected) { - createSelectedBackground(context) - } else { - null - } - setOnClickListener { - Log.d(TAG, "ANC mode selected: $mode (was: $currentANCMode)") - val container = findAirPodsContainer(this) - val drawerContainer = container?.findViewWithTag("airpods_drawer_container") + log(Log.INFO, TAG, "Handling AirPods icon URI: $uri") - if (currentANCMode == mode) { - if (drawerContainer != null && container != null) { - hideAirPodsDrawer(container, mainButton, drawerContainer) - } - return@setOnClickListener - } - - currentANCMode = mode + Handler(Looper.getMainLooper()).post { + try { + val context = imageView.context + val packageName = uri.authority ?: return@post + val packageContext = context.createPackageContext( + packageName, + Context.CONTEXT_IGNORE_SECURITY + ) - val parentDrawer = parent as? ViewGroup - if (parentDrawer != null) { - for (i in 0 until parentDrawer.childCount) { - val child = parentDrawer.getChildAt(i) as? LinearLayout - if (child != null && child.tag.toString().startsWith("anc_mode_")) { - val childModeStr = child.tag.toString().substringAfter("anc_mode_") - val childMode = childModeStr.toIntOrNull() ?: -1 - val childIcon = child.findViewWithTag("mode_icon_${childMode}") + val resPath = uri.pathSegments + if (resPath.size >= 2 && resPath[0] == "drawable") { + val resourceName = resPath[1] + val resourceId = packageContext.resources.getIdentifier( + resourceName, "drawable", packageName + ) - if (childMode == mode) { - child.background = createSelectedBackground(context) - childIcon?.setColorFilter(Color.BLACK) + if (resourceId != 0) { + val drawable = packageContext.resources.getDrawable( + resourceId, packageContext.theme + ) + imageView.setImageDrawable(drawable) + imageView.alpha = 1.0f + log(Log.INFO, TAG, "Successfully loaded icon from resource: $resourceName") } else { - child.background = null - childIcon?.setColorFilter(Color.WHITE) + log(Log.ERROR, TAG, "Resource not found: $resourceName") } } + } catch (e: Exception) { + log(Log.ERROR, TAG, "Error loading resource from URI $uri: ${e.message}") } } - - val intent = Intent(ACTION_SET_ANC_MODE).apply { - setPackage("me.kavishdevar.librepods") - putExtra(EXTRA_ANC_MODE, mode) - } - context.sendBroadcast(intent) - Log.d(TAG, "Sent broadcast to change ANC mode to: ${getLabelForMode(currentANCMode)}") - - - updateMainButtonIcon(context, mainButton, mode) - - if (drawerContainer != null && container != null) { - android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({ - hideAirPodsDrawer(container, mainButton, drawerContainer) - }, 50) - } - } - } - } - - private fun createSelectedBackground(context: Context): GradientDrawable { - return GradientDrawable().apply { - shape = GradientDrawable.RECTANGLE - setColor(Color.WHITE) - cornerRadius = 50f - } - } - - private fun findAirPodsContainer(view: View): ViewGroup? { - var current: View? = view - while (current != null) { - if (current is ViewGroup && current.tag == "airpods_container") { - return current - } - val parent = current.parent - if (parent is ViewGroup && parent.tag == "airpods_container") { - return parent - } - current = parent as? View - } - Log.w(TAG, "Could not find airpods_container ancestor") - return null - } - - private fun showAirPodsDrawer(container: ViewGroup, mainButton: ImageButton, drawerContainer: View) { - Log.d(TAG, "Showing AirPods drawer") - val selectedModeView = drawerContainer.findViewWithTag("anc_mode_$currentANCMode") - val selectedModeIcon = selectedModeView?.findViewWithTag("mode_icon_$currentANCMode") - val buttonContainer = container.findViewWithTag("airpods_button_container") - - if (selectedModeView == null || selectedModeIcon == null) { - Log.e(TAG, "Cannot find selected mode view or icon for show animation") - - drawerContainer.alpha = 0f - drawerContainer.visibility = View.VISIBLE - - drawerContainer.animate() - .alpha(1f) - .setDuration(ANIMATION_DURATION) - .start() - - buttonContainer?.animate() - ?.alpha(0f) - ?.setDuration(ANIMATION_DURATION / 2) - ?.setStartDelay(ANIMATION_DURATION / 2) - ?.withEndAction { - buttonContainer.visibility = View.GONE - } - ?.start() - - return - } - - drawerContainer.measure( - View.MeasureSpec.makeMeasureSpec(container.width, View.MeasureSpec.EXACTLY), - View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) - ) - - val drawerHeight = drawerContainer.measuredHeight - - drawerContainer.alpha = 0f - drawerContainer.visibility = View.VISIBLE - drawerContainer.translationY = -drawerHeight.toFloat() - - drawerContainer.animate() - .translationY(0f) - .alpha(1f) - .setDuration(ANIMATION_DURATION) - .setInterpolator(DecelerateInterpolator()) - .start() - - buttonContainer?.animate() - ?.alpha(0f) - ?.setDuration(ANIMATION_DURATION / 2) - ?.setStartDelay(ANIMATION_DURATION / 3) - ?.withEndAction { - buttonContainer.visibility = View.GONE + null + } catch (e: Exception) { + log(Log.ERROR, TAG, "Error in Bluetooth icon hook: ${e.message}") + chain.proceed() } - ?.start() - } - - private fun hideAirPodsDrawer(container: ViewGroup, mainButton: ImageButton, drawerContainer: View) { - Log.d(TAG, "Hiding AirPods drawer") - val buttonContainer = container.findViewWithTag("airpods_button_container") - - if (buttonContainer != null && buttonContainer.visibility != View.VISIBLE) { - buttonContainer.alpha = 0f - buttonContainer.visibility = View.VISIBLE } - buttonContainer?.animate() - ?.alpha(1f) - ?.setDuration(ANIMATION_DURATION / 2) - ?.start() - - drawerContainer.animate() - .translationY(-drawerContainer.height.toFloat()) - .alpha(0f) - .setDuration(ANIMATION_DURATION) - .setInterpolator(AccelerateInterpolator()) - .setStartDelay(ANIMATION_DURATION / 4) - .withEndAction { - drawerContainer.visibility = View.GONE - drawerContainer.translationY = 0f - } - .start() + log(Log.INFO, TAG, "Successfully hooked updateIcon method in Bluetooth settings") + } catch (e: Exception) { + log(Log.ERROR, TAG, "Failed to hook Bluetooth icon handler: ${e.message}") } + } +} - private fun getIconResourceForMode(mode: Int): Int { - return when (mode) { - ANC_MODE_OFF -> android.R.drawable.ic_lock_silent_mode - ANC_MODE_TRANSPARENCY -> android.R.drawable.ic_lock_silent_mode_off - ANC_MODE_ADAPTIVE -> android.R.drawable.ic_menu_compass - ANC_MODE_NOISE_CANCELLATION -> android.R.drawable.ic_lock_idle_charging - else -> android.R.drawable.ic_lock_silent_mode_off - } - } - private fun getLabelForMode(mode: Int): String { - return when (mode) { - ANC_MODE_OFF -> "Off" - ANC_MODE_TRANSPARENCY -> "Transparency" - ANC_MODE_ADAPTIVE -> "Adaptive" - ANC_MODE_NOISE_CANCELLATION -> "Noise Cancellation" - else -> "Unknown" - } - } - } +object NativeBridge { + external fun setSdpHook(enabled: Boolean) } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/LogCollector.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/LogCollector.kt index d03ca48c6..246fd785a 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/LogCollector.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/LogCollector.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ package me.kavishdevar.librepods.utils diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/MediaController.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/MediaController.kt index 778a09783..400e0ff72 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/MediaController.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/MediaController.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) @@ -171,8 +171,10 @@ object MediaController { } if (configs != null && !iPausedTheMedia) { + val localMac = ServiceManager.getService()?.localMac ?: return + if (localMac == "") return ServiceManager.getService()?.aacpManager?.sendMediaInformataion( - ServiceManager.getService()?.localMac ?: return, + localMac, isActive ) Log.d("MediaController", "User changed media state themselves; will wait for ear detection pause before auto-play") diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/RadareOffsetFinder.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/RadareOffsetFinder.kt index 50ede42eb..e5a1e7bdc 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/RadareOffsetFinder.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/RadareOffsetFinder.kt @@ -1,20 +1,20 @@ /* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ @file:OptIn(ExperimentalEncodingApi::class) @@ -40,7 +40,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi class RadareOffsetFinder(context: Context) { companion object { private const val TAG = "RadareOffsetFinder" - private const val RADARE2_URL = "https://hc-cdn.hel1.your-objectstorage.com/s/v3/c9898243c42c0d3d1387de9a37d57ce9df77f9c9_radare2-5.9.9-android-aarch64.tar.gz" + private const val RADARE2_URL = "https://github.com/devnoname120/radare2/releases/download/5.9.8-android-aln/radare2-5.9.9-android-aarch64-aln.tar.gz" private const val HOOK_OFFSET_PROP = "persist.librepods.hook_offset" private const val CFG_REQ_OFFSET_PROP = "persist.librepods.cfg_req_offset" private const val CSM_CONFIG_OFFSET_PROP = "persist.librepods.csm_config_offset" @@ -115,6 +115,11 @@ class RadareOffsetFinder(context: Context) { } fun isSdpOffsetAvailable(): Boolean { + val sharedPreferences = ServiceManager.getService()?.applicationContext?.getSharedPreferences("settings", Context.MODE_PRIVATE) // ik not good practice- too lazy + if (sharedPreferences?.getBoolean("skip_setup", false) == true) { + Log.d(TAG, "Setup skipped, returning true for SDP offset.") + return true + } try { val process = Runtime.getRuntime().exec(arrayOf("/system/bin/getprop", SDP_OFFSET_PROP)) val reader = BufferedReader(InputStreamReader(process.inputStream)) @@ -462,7 +467,7 @@ class RadareOffsetFinder(context: Context) { // findAndSaveL2cuProcessCfgReqOffset(libraryPath, envSetup) // findAndSaveL2cCsmConfigOffset(libraryPath, envSetup) // findAndSaveL2cuSendPeerInfoReqOffset(libraryPath, envSetup) - + // findAndSaveSdpOffset(libraryPath, envSetup) Should not be run by default, only when user asks for it. } catch (e: Exception) { diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/RootlessSupport.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/RootlessSupport.kt new file mode 100644 index 000000000..e30464fbc --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/RootlessSupport.kt @@ -0,0 +1,36 @@ +/* + LibrePods - AirPods liberated from Apple’s ecosystem + Copyright (C) 2025 LibrePods contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package me.kavishdevar.librepods.utils + +import android.content.SharedPreferences +import android.os.Build + +fun isSupported(sharedPreferences: SharedPreferences): Boolean { + if (Build.VERSION.SDK_INT == 37) return true + val isBypassFlagActive = sharedPreferences.getBoolean("bypass_device_check.v2", false) + if (isBypassFlagActive) return true + val isPixel = Build.MANUFACTURER.lowercase() == "google" + val isOppoFamily = Build.MANUFACTURER.lowercase() in listOf("oneplus", "oppo", "realme") + if (isPixel && Build.VERSION.SDK_INT == 36) { + return Build.ID.startsWith("CP1A") + } else if (isOppoFamily) { + return Build.VERSION.SDK_INT >= 36 + } + return false +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/SystemAPIUtils.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/SystemAPIUtils.kt index 694fc86cf..cd91e24c3 100644 --- a/android/app/src/main/java/me/kavishdevar/librepods/utils/SystemAPIUtils.kt +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/SystemAPIUtils.kt @@ -2,7 +2,6 @@ package me.kavishdevar.librepods.utils import android.bluetooth.BluetoothDevice import android.util.Log -import org.lsposed.hiddenapibypass.HiddenApiBypass object SystemApisUtils { @@ -288,18 +287,16 @@ object SystemApisUtils { /** * Helper method to set metadata using HiddenApiBypass */ - fun setMetadata(device: BluetoothDevice, key: Int, value: ByteArray): Boolean { + fun setMetadata(device: BluetoothDevice, key: Int, value: ByteArray): Boolean { return try { - val result = HiddenApiBypass.invoke( - BluetoothDevice::class.java, - device, + val method = BluetoothDevice::class.java.getMethod( "setMetadata", - key, - value - ) as Boolean - result + Int::class.java, + ByteArray::class.java + ) + method.invoke(device, key, value) as Boolean } catch (e: Exception) { - Log.e("SystemApisUtils", "Failed to set metadata for key $key", e) + Log.w("SystemApisUtils", "Failed to set metadata for key $key: ${e.message}") false } } diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/XposedServiceHolder.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/XposedServiceHolder.kt new file mode 100644 index 000000000..27a4ffc98 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/XposedServiceHolder.kt @@ -0,0 +1,7 @@ +package me.kavishdevar.librepods.utils + +import io.github.libxposed.service.XposedService + +object XposedServiceHolder { + var service: XposedService? = null +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/utils/XposedState.kt b/android/app/src/main/java/me/kavishdevar/librepods/utils/XposedState.kt new file mode 100644 index 000000000..284e5e656 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/librepods/utils/XposedState.kt @@ -0,0 +1,6 @@ +package me.kavishdevar.librepods.utils + +object XposedState { + var isAvailable: Boolean = false + var bluetoothScopeEnabled: Boolean = false +} diff --git a/android/app/src/main/java/me/kavishdevar/librepods/widgets/BatteryWidget.kt b/android/app/src/main/java/me/kavishdevar/librepods/widgets/BatteryWidget.kt deleted file mode 100644 index f67588b14..000000000 --- a/android/app/src/main/java/me/kavishdevar/librepods/widgets/BatteryWidget.kt +++ /dev/null @@ -1,37 +0,0 @@ -/* - * LibrePods - AirPods liberated from Apple’s ecosystem - * - * Copyright (C) 2025 LibrePods contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -@file:OptIn(ExperimentalEncodingApi::class) - -package me.kavishdevar.librepods.widgets - -import android.appwidget.AppWidgetManager -import android.appwidget.AppWidgetProvider -import android.content.Context -import me.kavishdevar.librepods.services.ServiceManager -import kotlin.io.encoding.ExperimentalEncodingApi - -class BatteryWidget : AppWidgetProvider() { - override fun onUpdate( - context: Context, - appWidgetManager: AppWidgetManager, - appWidgetIds: IntArray - ) { - ServiceManager.getService()?.updateBattery() - } -} diff --git a/android/app/src/main/res-apple/drawable/airpods_1.png b/android/app/src/main/res-apple/drawable/airpods_1.png deleted file mode 100644 index 681ee750a..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_1.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_1_buds.png b/android/app/src/main/res-apple/drawable/airpods_1_buds.png deleted file mode 100644 index 8bea6a255..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_1_buds.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_1_case.png b/android/app/src/main/res-apple/drawable/airpods_1_case.png deleted file mode 100644 index be694b048..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_1_case.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_1_left.png b/android/app/src/main/res-apple/drawable/airpods_1_left.png deleted file mode 100644 index 88e13948e..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_1_left.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_1_right.png b/android/app/src/main/res-apple/drawable/airpods_1_right.png deleted file mode 100644 index 76495bee9..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_1_right.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_2.png b/android/app/src/main/res-apple/drawable/airpods_2.png deleted file mode 100644 index 681ee750a..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_2.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_2_buds.png b/android/app/src/main/res-apple/drawable/airpods_2_buds.png deleted file mode 100644 index 8bea6a255..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_2_buds.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_2_case.png b/android/app/src/main/res-apple/drawable/airpods_2_case.png deleted file mode 100644 index be694b048..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_2_case.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_2_left.png b/android/app/src/main/res-apple/drawable/airpods_2_left.png deleted file mode 100644 index 88e13948e..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_2_left.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_2_right.png b/android/app/src/main/res-apple/drawable/airpods_2_right.png deleted file mode 100644 index 76495bee9..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_2_right.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_3.png b/android/app/src/main/res-apple/drawable/airpods_3.png deleted file mode 100644 index 681ee750a..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_3.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_3_buds.png b/android/app/src/main/res-apple/drawable/airpods_3_buds.png deleted file mode 100644 index 8bea6a255..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_3_buds.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_3_case.png b/android/app/src/main/res-apple/drawable/airpods_3_case.png deleted file mode 100644 index be694b048..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_3_case.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_3_left.png b/android/app/src/main/res-apple/drawable/airpods_3_left.png deleted file mode 100644 index 88e13948e..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_3_left.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_3_right.png b/android/app/src/main/res-apple/drawable/airpods_3_right.png deleted file mode 100644 index 76495bee9..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_3_right.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_4.png b/android/app/src/main/res-apple/drawable/airpods_4.png deleted file mode 100644 index 681ee750a..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_4.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_4_buds.png b/android/app/src/main/res-apple/drawable/airpods_4_buds.png deleted file mode 100644 index 8bea6a255..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_4_buds.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_4_case.png b/android/app/src/main/res-apple/drawable/airpods_4_case.png deleted file mode 100644 index be694b048..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_4_case.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_4_left.png b/android/app/src/main/res-apple/drawable/airpods_4_left.png deleted file mode 100644 index 88e13948e..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_4_left.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_4_right.png b/android/app/src/main/res-apple/drawable/airpods_4_right.png deleted file mode 100644 index 76495bee9..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_4_right.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_pro_1.png b/android/app/src/main/res-apple/drawable/airpods_pro_1.png deleted file mode 100644 index 681ee750a..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_pro_1.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_pro_1_buds.png b/android/app/src/main/res-apple/drawable/airpods_pro_1_buds.png deleted file mode 100644 index 8bea6a255..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_pro_1_buds.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_pro_1_case.png b/android/app/src/main/res-apple/drawable/airpods_pro_1_case.png deleted file mode 100644 index be694b048..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_pro_1_case.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_pro_1_left.png b/android/app/src/main/res-apple/drawable/airpods_pro_1_left.png deleted file mode 100644 index 88e13948e..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_pro_1_left.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_pro_1_right.png b/android/app/src/main/res-apple/drawable/airpods_pro_1_right.png deleted file mode 100644 index 76495bee9..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_pro_1_right.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_pro_3.png b/android/app/src/main/res-apple/drawable/airpods_pro_3.png deleted file mode 100644 index 681ee750a..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_pro_3.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_pro_3_buds.png b/android/app/src/main/res-apple/drawable/airpods_pro_3_buds.png deleted file mode 100644 index 8bea6a255..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_pro_3_buds.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_pro_3_case.png b/android/app/src/main/res-apple/drawable/airpods_pro_3_case.png deleted file mode 100644 index be694b048..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_pro_3_case.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_pro_3_left.png b/android/app/src/main/res-apple/drawable/airpods_pro_3_left.png deleted file mode 100644 index 88e13948e..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_pro_3_left.png and /dev/null differ diff --git a/android/app/src/main/res-apple/drawable/airpods_pro_3_right.png b/android/app/src/main/res-apple/drawable/airpods_pro_3_right.png deleted file mode 100644 index 76495bee9..000000000 Binary files a/android/app/src/main/res-apple/drawable/airpods_pro_3_right.png and /dev/null differ diff --git a/android/app/src/main/res/resources.properties b/android/app/src/main/res/resources.properties new file mode 100644 index 000000000..92481bb0b --- /dev/null +++ b/android/app/src/main/res/resources.properties @@ -0,0 +1 @@ +unqualifiedResLocale=en diff --git a/android/app/src/main/res/value-it/strings.xml b/android/app/src/main/res/value-it/strings.xml new file mode 100644 index 000000000..5933b0838 --- /dev/null +++ b/android/app/src/main/res/value-it/strings.xml @@ -0,0 +1,213 @@ + + LibrePods + Libera i tuoi AirPods dall'ecosistema Apple. + Visualizza lo stato della batteria dei tuoi AirPods direttamente dalla schermata principale! + Accessibilità + Volume Tono + Regola il volume del tono degli effetti sonori riprodotti dagli AirPods. + Audio + Audio Adattivo + Personalizza Audio Adattivo + L'audio adattivo risponde dinamicamente al tuo ambiente e cancella o permette i rumori esterni. Puoi personalizzare l'Audio Adattivo per permettere più o meno rumore. + Auricolari + Custodia + Test + Nome + Modalità di Ascolto + Spento + Trasparenza + Adattivo + Cancellazione del Rumore + Premi e Tieni Premuto sugli AirPods + Premi e tieni premuto sullo stelo per alternare tra le modalità di ascolto selezionate. + Gesti della Testa + Sinistra + Destra + Consapevolezza Conversazionale + Abbassa il volume dei contenuti multimediali e riduce il rumore di fondo quando inizi a parlare con altre persone. + Volume Personalizzato + Regola il volume dei contenuti multimediali in risposta al tuo ambiente. + Cancellazione del Rumore con un Solo AirPod + Consenti agli AirPods di essere messi in modalità di cancellazione del rumore quando è presente un solo AirPod nell'orecchio. + Controllo Volume + Regola il volume scorrendo verso l'alto o verso il basso sul sensore situato sullo stelo degli AirPods Pro. + AirPods non connessi + Si prega di connettere i tuoi AirPods per accedere alle impostazioni. + Indietro + Personalizzazioni + Volume relativo + Riduce a una percentuale del volume corrente invece del volume massimo. + Metti in Pausa la Musica + Quando inizi a parlare, la musica verrà messa in pausa. + ESEMPIO + Aggiungi widget + Controlla la Modalità di Controllo del Rumore direttamente dalla tua Schermata Principale. + Connesso + Connesso a Linux + Connesso + Spostato su Linux + Spostato su %1$s + Riconnetti dalla notifica + Tracciamento della Testa + Annuisci per rispondere alle chiamate e scuoti la testa per rifiutarle. + Generale + Azione del Tile Impostazioni Rapide + Mostra la finestra di dialogo per il controllo del rumore al tocco. + Alterna tra le modalità al tocco. + Sviluppatore + Apri le Impostazioni degli AirPods + Gestisci le funzionalità e le preferenze degli AirPods + Rilevamento Automatico dell'Orecchio + Riproduzione Automatica + Pausa Automatica + Risoluzione dei Problemi + Raccogli i log per diagnosticare i problemi con la connessione degli AirPods + Raccogli Log + Log Salvati + Nessun log salvato trovato + Preferenze di Connessione Automatica + Connetti ai tuoi AirPods quando il loro stato è: + Disconnesso + Gli AirPods non sono connessi a un dispositivo + Inattivo + Un dispositivo è connesso ai tuoi AirPods, ma non riproduce contenuti multimediali né è in chiamata + Riproduzione di contenuti multimediali + Un dispositivo sta riproducendo contenuti multimediali sui tuoi AirPods + In chiamata + Un dispositivo è in chiamata con i tuoi AirPods + Connetti agli AirPods quando il tuo telefono è: + Ricezione di una chiamata + Il tuo telefono inizia a squillare + Avvio della riproduzione di contenuti multimediali + Il tuo telefono inizia a riprodurre contenuti multimediali + Annulla + Puoi personalizzare la modalità Trasparenza per i tuoi AirPods Pro per aiutarti a sentire ciò che ti circonda. + La Riduzione dei Suoni Forti può ridurre attivamente la tua esposizione ai forti rumori ambientali quando in modalità Trasparenza e Adattiva. La Riduzione dei Suoni Forti non è attiva in modalità Spento. + Riduzione dei Suoni Forti + Controlli Chiamata + Connetti automaticamente a questo dispositivo + Quando abilitato, gli AirPods tenteranno di connettersi automaticamente a questo dispositivo. Altrimenti, tenteranno di connettersi automaticamente solo se sono stati connessi in precedenza. + Metti in pausa i contenuti multimediali quando ti addormenti + Modalità Ascolto Disattivata + Quando questa opzione è attiva, le modalità di ascolto degli AirPods includeranno un'opzione "Spento". I livelli di suono forti non vengono ridotti quando la modalità di ascolto è impostata su "Spento". + Microfono + Modalità Microfono + Automatico + Sempre Destro + Sempre Sinistro + Rispondi alla chiamata + Silenzia/Riattiva + Riaggancia + Premi una Volta + Premi Due Volte + Apparecchio Acustico + Regolazioni + Scorri per controllare l'amplificazione + Quando sei in modalità Trasparenza e nessun contenuto multimediale è in riproduzione, scorri verso l'alto e verso il basso sui controlli Touch dei tuoi AirPods Pro per aumentare o diminuire l'amplificazione dei suoni ambientali. + Modalità Trasparenza + Personalizza la Modalità Trasparenza + Velocità di Pressione + Regola la velocità richiesta per premere due o tre volte sui tuoi AirPods. + Durata della Pressione Prolungata + Regola la durata richiesta per premere e tenere premuto sui tuoi AirPods. + Velocità di Scorrimento del Volume + Per evitare regolazioni involontarie del volume, seleziona il tempo di attesa preferito tra gli scorrimenti. + Equalizzatore + Applica EQ a + Telefono + Media + Banda %d + Predefinito + Più lento + Il più lento + Più lungo + Il più lungo + Più scuro + Più luminoso + Meno + Di più + Amplificazione + Bilanciamento + Tono + Riduzione del Rumore Ambientale + Potenziamento Conversazione + Potenziamento Conversazione concentra i tuoi AirPods Pro sulla persona che parla di fronte a te, rendendo più facile sentire in una conversazione faccia a faccia. + Gli AirPods possono utilizzare i risultati di un test dell'udito per apportare modifiche che migliorano la chiarezza delle voci e dei suoni intorno a te.\n\nApparecchio Acustico è destinato solo a persone con perdita dell'udito da lieve a moderata. + Assistenza Media + Gli AirPods Pro possono utilizzare i risultati di un test dell'udito per apportare modifiche che migliorano la chiarezza di musica, video e chiamate. + Regola Musica e Video + Regola Chiamate + Widget + Mostra la batteria del telefono nel widget + Visualizza il livello della batteria del tuo telefono nel widget accanto alla batteria degli AirPods + Volume Consapevolezza Conversazionale + Tile Impostazioni Rapide + Apri finestra di dialogo per il controllo + Se disabilitato, cliccando sul QS si scorrerà tra le modalità. Se abilitato, verrà mostrata una finestra di dialogo per controllare la modalità di controllo del rumore e la consapevolezza conversazionale. + Disconnetti AirPods quando non indossati + Sarai ancora in grado di controllarli con l'app - questo disconnette solo l'audio. + Opzioni Avanzate + Imposta Chiave di Risoluzione Identità (IRK) + Imposta manualmente il valore IRK utilizzato per risolvere gli indirizzi casuali BLE + Imposta Chiave di Crittografia + Imposta manualmente il valore ENC_KEY utilizzato per decrittografare le pubblicità BLE + Utilizza pacchetti alternativi di tracciamento della testa + Abilita questo se il tracciamento della testa non funziona per te. Questo invia dati diversi agli AirPods per richiedere/interrompere i dati di tracciamento della testa. + Comportati come un dispositivo Apple + Abilita la connettività multi-dispositivo e le funzionalità di Accessibilità come la personalizzazione della modalità Trasparenza (amplificazione, tono, riduzione del rumore ambientale, potenziamento conversazione ed EQ) + Potrebbe essere instabile!! Un massimo di due dispositivi possono essere connessi ai tuoi AirPods. Se li stai usando con un dispositivo Apple come un iPad o un Mac, connetti prima quel dispositivo e poi il tuo Android. + Reimposta Offset Hook + Questo cancellerà l'offset hook corrente e richiederà di rifare la procedura di configurazione. Sei sicuro di voler continuare? + Reimposta + Offset hook è stato resettato. Reindirizzamento alla configurazione... + Impossibile reimpostare l'offset hook + IRK impostata correttamente + Chiave di crittografia impostata correttamente + Valore Esadecimale IRK + Valore Esadecimale ENC_KEY + Inserisci IRK di 16 byte come stringa esadecimale (32 caratteri): + Inserisci ENC_KEY di 16 byte come stringa esadecimale (32 caratteri): + Devono essere esattamente 32 caratteri esadecimali + Errore durante la conversione esadecimale: + Offset trovato, riavviare il processo Bluetooth + Assistente Digitale + Attivo + Telecomando Fotocamera + Controllo Fotocamera + Scatta una foto, avvia o interrompi la registrazione e altro utilizzando Premere una Volta o Premere e Tenere Premuto. Quando si utilizzano gli AirPods per le azioni della fotocamera, se si seleziona Premere una Volta, i gesti di controllo dei media non saranno disponibili e, se si seleziona Premere e Tenere Premuto, la modalità di ascolto e i gesti dell'Assistente Digitale non saranno disponibili. + Imposta un pacchetto app personalizzato per il rilevamento della fotocamera + Imposta Appid Fotocamera Personalizzata + Inserisci l'id dell'applicazione della fotocamera: + Appid Fotocamera Personalizzata + Appid fotocamera personalizzata impostata correttamente + Ascoltatore fotocamera + Servizio di ascolto per LibrePods per rilevare quando la fotocamera è attiva per attivare il controllo della fotocamera sugli AirPods. + Licenze Open Source + Aggiorna Test Uditivo + Aggiorna Risultato Test Uditivo + ATT Manager è nullo, prova a riconnetterti. + Sono richieste le seguenti autorizzazioni per utilizzare l'app. Si prega di concederle per continuare. + Scuoti la testa o annuisci! + Accesso Root Richiesto + Questa app ha bisogno dell'accesso root per agganciarsi alla libreria Bluetooth + L'accesso root è stato negato. Si prega di concedere i permessi di root. + Passaggi per la Risoluzione dei Problemi + Si prega di inserire i valori di perdita in dbHL + Informazioni + Nome Modello + Numero Modello + Numero di Serie + Versione + Salute Uditiva + Protezione dell'Udito + Uso in Ambienti di Lavoro + Protezione EN 352 + La protezione EN 352 limita il livello massimo dei media a 82 dBA e soddisfa i requisiti applicabili dello standard EN 352 per la protezione individuale dell'udito. + Rumore Ambientale + Riconnetti all'ultimo dispositivo connesso + Disconnetti + Disattiva la gestione del rumore + Lascia entrare i suoni esterni + Regola dinamicamente il rumore esterno + Blocca i suoni esterni + diff --git a/android/app/src/main/res/values-de/strings.xml b/android/app/src/main/res/values-de/strings.xml new file mode 100644 index 000000000..51b4077a1 --- /dev/null +++ b/android/app/src/main/res/values-de/strings.xml @@ -0,0 +1,7 @@ + + Popup-Animationen + Popup unten + Zeigt das Popup im iOS-Stil unten an, wenn AirPods sich verbinden. + Dynamic Island Popup + Zeigt das Popup im Dynamic-Island-Stil oben für Verbindungs- und Übergabe-Ereignisse. + diff --git a/android/app/src/main/res/values-es/strings.xml b/android/app/src/main/res/values-es/strings.xml new file mode 100644 index 000000000..34125e314 --- /dev/null +++ b/android/app/src/main/res/values-es/strings.xml @@ -0,0 +1,218 @@ + + LibrePods + Libera tus AirPods del ecosistema de Apple. + ¡Ve el estado de batería de tus AirPods desde tu pantalla de inicio! + Accesibilidad + Volumen del tono + Ajusta el volumen de los efectos de sonido reproducidos por los AirPods. + Audio + Audio Adaptativo + Personalizar Audio Adaptativo + El audio adaptativo responde al entorno y cancela o permite ruido externo. Puedes ajustarlo para permitir más o menos ruido. + Auriculares + Estuche + Probar + Nombre + Modo de Escucha + Desactivado + Transparencia + Adaptativo + Cancelación de Ruido + Pulsar y Mantener AirPods + Mantén presionado para alternar entre los modos seleccionados. + Gestos de Cabeza + Izquierdo + Derecho + Detección de Conversación + Reduce el volumen y el ruido de fondo cuando comienzas a hablar. + Volumen Personalizado + Ajusta el volumen en función del entorno. + Cancelación de sonido con un solo AirPod + Permite activar la cancelación de sonido con un solo auricular puesto. + Control de Volumen + Ajusta el volumen deslizando arriba o abajo en el control táctil de los AirPods Pro. + AirPods no conectados + Por favor, conecta tus AirPods para acceder a los ajustes. + Atrás + Personalización + Volumen relativo + Reduce a un porcentaje del volumen actual en vez del volumen máximo. + Pausar música + La música se pausará cuando comiences a hablar. + EJEMPLO + Añadir widget + Controla el modo de control de ruido desde tu pantalla de inicio. + Conectado + Conectado a Linux + Conectado + Transferido a Linux + Transferido a %1$s + Reconectar desde notificación + Seguimiento de Cabeza + Asiente para contestar y niega para rechazar. + General + Acción del botón de Ajustes Rápidos + Mostrar diálogo de control de ruido al tocar. + Alternar modos al tocar. + Desarrollador + Abrir Ajustes de AirPods + Gestionar funciones y preferencias + Detección Automática de Oído + Reproducción automática + Pausa automática + Solución de Problemas + Recopila registros para diagnosticar problemas de conexión de los AirPods + Recopilar registros + Guardar registros + No se encontraron registros + Preferencias de Autoconexión + Conectar a tus AirPods cuando su estado sea: + Desconectado + AirPods no conectados a ningún dispositivo + Inactivos + Un dispositivo está conectado a tus AirPods, pero no está reproduciendo audio ni llamando + Reproduciendo + Un dispositivo está reproduciendo audio en tus AirPods + En llamada + Un dispositivo está en llamada con tus AirPods + Conectar a tus AirPods cuando el teléfono esté: + Llamada entrante + El teléfono empieza a sonar + Iniciando reproducción + El teléfono empieza a reproducir audio + Deshacer + Puedes personalizar el modo Transparencia de tus AirPods Pro para oír mejor tu entorno. + Reducción de Sonidos Fuertes puede reducir activamente la exposición a entornos ruidosos en modo Transparencia y Adaptativo. Reducción de Sonidos Fuertes no está activa en modo Desactivado. + Reducción de Sonidos Fuertes + Controles de Llamada + Conectar a este dispositivo automáticamente + Al activarse, los AirPods intentarán conectarse automáticamente a este dispositivo. Si no es posible, se intentarán conectar al último dispositivo utilizado. + Pausar audio al quedarse dormido + Modo de Escucha Desactivado + Cuando está activado, los modos de escucha de AirPods incluyen la opción Desactivado. Cuando el modo de Escucha Desactivado está activado no se reducen ruidos fuertes. + Micrófono + Modo de Micrófono + Automático + Siempre derecho + Siempre izquierdo + Responder + Silenciar/Desilenciar + Colgar + Pulsar una vez + Pulsar dos veces + Audífono + Ajustes + Deslizar para controlar amplificación + Cuando en modo Transparencia y no hay audio reproduciéndose, desliza hacia arriba y/o abajo en los controles táctiles de los AirPods Pro para ajustar la amplificación ambiental. + Modo Transparencia + Personalizar Modo Transparencia + Velocidad de pulsación + Ajusta la velocidad necesaria para pulsar dos o tres veces en tus AirPods. + Duración de pulsación prolongada + Ajusta la duración requerida para pulsación prolongada en tus AirPods. + Velocidad de deslizamiento + Selecciona el tiempo entre deslizamientos para evitar ajustes involuntarios. + Ecualizador (EQ) + Aplicar EQ a + Teléfono + Multimedia + Banda %d + Predeterminado + Más lento + Muy lento + Más largo + Muy largo + Más oscuro + Más claro + Menos + Más + Amplificación + Balance + Tono + Reducción de Ruido Ambiental + Refuerzo de Conversación + Refuerzo de Conversación enfoca tu AirPods Pro en la persona frente a ti facilitando la escucha de conversaciones cara a cara. + Los AirPods pueden usar resultados de pruebas auditivas para mejorar la claridad de voces y sonidos de tu alrededor.\n\nEl modo Audífono tiene como objetivo ayudar a personas con problemas auditivos leves o moderados. + Asistencia Multimedia + Los AirPods Pro pueden usar resultados de pruebas auditivas para mejorar la claridad de música, video y llamadas. + Ajustar Música y Video + Ajustar Llamadas + Widget + Mostrar batería del teléfono en Widget + Mostrar la batería del teléfono junto a la de los AirPods en el Widget. + Volumen de Detección de Conversación + Botón de Ajustes Rápidos + Abrir diálogo de controles + Si está desactivado, al pulsar Ajustes rápidos alterna modos. Si está activado, muestra un diálogo de controles para control de ruido y detección de conversaciones. + Desconectar AirPods cuando no estén puestos + Aún podrás controlarlos con la aplicación, este ajuste sólo desconecta el audio. + Opciones Avanzadas + Establecer Identity Resolving Key (IRK) + Configura manualmente valor utilizado IRK para resolver direcciones aleatorias BLE. + Establecer Clave de Cifrado (ENC_KEY) + Configurar manualmente valor ENC_KEY utilizado para descifrar BLE cifrado. + Utilizar paquetes head tracking alternativos + Activar si head tracking no funciona. Esto enviará datos distintos a AirPods para solicitar/detener datos head tracking. + Actuar como dispositivo Apple + Activa conectividad multidispositivo y funciones de Accesibilidad como modo transparencia personalizado (amplificación, tono, reducción de ruido ambiente, potenciador de conversaciones, y EQ). + ¡Puede ser inestable! Se puede conectar como máximo dos dispositivos a tus AirPods. Si estás utilizando un dispositivo de Apple, como un iPad o Mac, por favor conéctalo antes y posteriormente conecta tu dispositivo Android. + Restablecer Hook Offset + Esto elimina el hook offset actual y requiere volver a realizar la configuración inicial. ¿Estás seguro que quieres continuar? + Restablecer + Hook offset restablecido. Redirigiendo a configuración inicial... + Error al restablecer hook offset + IRK ha sido establecido + Clave de cifrado establecida + Valor IRK Hex + Valor ENC_KEY Hex + Introducir 16-byte IRK como formato hexadecimal (32 caracteres): + Introducir 16-byte ENC_KEY como formato hexadecimal (32 caracteres): + Debe tener exactamente 32 caracteres hexadecimales + Error convirtiendo hex: + Por favor, reinicie el proceso Bluetooth + Asistente Digital + Activado + Control Remoto de Cámara + Control de Cámara + Toma fotos o inicia grabación usando Pulsar una vez o Pulsación Prolongada. Los AirPods para acciones de la cámara: si selecciona Pulsar una vez, los gestos de control multimedia no estarán disponibles, y si selecciona Pulsación Prolongada, el modo de escucha y los gestos del Asistente Digital no estarán disponibles. + Configurar un paquete de aplicaciones personalizado para la detección de la cámara + Establecer Appid de cámara personalizada + Introduzca el ID de la aplicación de la cámara: + Aplicación de cámara personalizada Appid + Appid de cámara establecido correctamente + Escucha de cámara + Servicio de escucha para LibrePods que detecta cuándo la cámara está activa para activar el control de la cámara en los AirPods. + Licencias de Código Abierto + Actualizar Prueba Auditiva + Actualizar el Resultado de la Prueba Auditiva + ATT Manager es nulo. Intente reconectar. + Se requieren los siguientes permisos para utilizar la aplicación. Por favor, autorícelos para continuar. + ¡Mueve la cabeza o asiente! + Se requiere acceso root + Esta aplicación necesita acceso root para conectarse a la biblioteca Bluetooth + Se ha denegado el acceso root. Por favor, conceda permisos root. + Pasos para la resolución de problemas + Introduzca los valores de pérdida en dbHL. + Acerca de + Nombre del modelo + Número de modelo + Número de serie + Versión + Salud Auditiva + Protección Auditiva + Workspace en uso + Protección EN 352 + La norma EN 352 limita el nivel máximo de los medios a 82 dBA y cumple los requisitos aplicables de la norma EN 352 para la protección auditiva personal. + Ruido ambiental + Reconectar al último dispositivo conectado + Desconectar + Desactiva la gestión del ruido + Deja entrar los sonidos externos + Ajuste dinámico del ruido externo + Bloquea los sonidos externos + Animaciones emergentes + Ventana emergente inferior + Muestra la ventana emergente estilo iOS en la parte inferior cuando los AirPods se conectan. + Ventana emergente Dynamic Island + Muestra la ventana emergente estilo Dynamic Island en la parte superior para eventos de conexión y traspaso. + diff --git a/android/app/src/main/res/values-fr/strings.xml b/android/app/src/main/res/values-fr/strings.xml new file mode 100644 index 000000000..9a24bcdb5 --- /dev/null +++ b/android/app/src/main/res/values-fr/strings.xml @@ -0,0 +1,218 @@ + + LibrePods + Libérez vos AirPods de l\'écosystème Apple. + Voyez l\'état de la batterie de vos AirPods directement depuis votre écran d\'accueil ! + Accessibilité + Volume de la tonalité + Ajustez le volume des effets sonores émis sur les AirPods. + Audio + Audio Adaptatif + Personnaliser l\'Audio Adaptatif + L\'audio Adaptatif répond dynamiquement à votre environnement et annule ou laisse entrer le bruit extérieur. Vous pouvez personnaliser l\'Audio Adaptatif pour laisser entrer plus ou moins de bruit. + Écouteurs + Boîtier + Test + Nom + Mode d\'écoute + Désactivé + Transparence + Adaptatif + Réduction de bruit + Appuyer et maintenir les AirPods + Maintenez la tige pour passer entre les modes d\'écoute sélectionnés. + Gestes de la tête + Gauche + Droite + Détection des conversations + Baisse le volume des médias et réduit le bruit de fond lorsque vous commencez à parler à quelqu\'un. + Volume personnalisé + Ajuste le volume des médias en réponse à votre environnement. + Réduction du bruit avec un écouteur + Permet d\'activer la réduction de bruit même avec un seul AirPod à l\'oreille. + Contrôle du volume + Ajustez le volume en balayant vers le haut ou vers le bas sur le capteur situé sur la tige des AirPods Pro. + AirPods non connectés + Veuillez connecter vos AirPods pour accéder aux réglages. + Retour + Personnalisations + Volume relatif + Réduit à un pourcentage du volume actuel plutôt qu\'au volume maximum. + Mettre la musique en pause + Quand vous commencez à parler, la musique sera mise en pause. + EXEMPLE + Ajouter le widget + Contrôlez le mode de réduction de bruit directement depuis votre écran d\'accueil. + Connecté + Connecté à Linux + Connecté + Déplacé vers Linux + Déplacé vers %1$s + Reconnecté depuis la notification + Suivi de la tête + Hochez la tête pour répondre aux appels, secouez-la pour refuser. + Général + Action de la tuile dans les réglages rapides + Afficher le dialogue de contrôle du bruit au toucher. + Faire défiler les modes au toucher. + Développeur + Ouvrir les réglages des AirPods + Gérer les fonctionnalités et préférences des AirPods + Détection automatique des oreilles + Lecture Automatique + Pause Automatique + Dépannage + Collecter des journaux pour diagnostiquer les problèmes de connexion des AirPods + Collecter les journaux + Journaux enregistrés + Aucun journal sauvegardé trouvé + Préférences d\'auto-connexion + Se connecter aux AirPods lorsque leur état est : + Déconnectés + Les AirPods ne sont connectés à aucun appareil + Inactifs + Un appareil est connecté à vos AirPods, mais ne lit pas de média et n\'est pas en appel + Lecture de média + Un appareil lit du média via les AirPods + En appel + Un appareil est en appel via les AirPods + Se connecter aux AirPods lorsque votre téléphone : + Reçoit un appel + Votre téléphone commence à sonner + Démarre la lecture média + Votre téléphone commence à lire un média + Annuler + Vous pouvez personnaliser le mode Transparence de vos AirPods Pro pour vous aider à entendre ce qui vous entoure. + La réduction des sons forts peut réduire activement votre exposition aux bruits ambiants forts en mode Transparence et Adaptatif. La réduction des sons forts n\'est pas active en mode Désactivé. + Réduction des sons forts + Contrôle des appels + Se connecter automatiquement à cet appareil + Quand ce mode est activé, les AirPods essaieront de se connecter automatiquement à cet appareil. Sinon, ils essaieront uniquement de se connecter automatiquement au dernier appareil connecté. + Mettre en pause l\'écoute au moment de s\'endormir + Mode d\'écoute Désactivé + Quand ce paramètre est activé, les modes d\'écoute incluront une option Désactivé. Les sons forts ne sont pas réduits en mode Désactivé. + Microphone + Mode du microphone + Automatique + Toujours à droite + Toujours à gauche + Répondre aux appels + Muet / Activer le son + Raccrocher + Appuyer une fois + Appuyer deux fois + Aide auditive + Ajustements + Balayer pour contrôler l\'amplification + En mode Transparence et sans lecture média, balayez sur les commandes tactiles des AirPods Pro pour augmenter ou diminuer l\'amplification des sons environnants. + Mode Transparence + Personnaliser le mode Transparence + Vitesse d\'appui + Ajustez la vitesse requise pour appuyer deux ou trois fois sur vos AirPods. + Durée d\'appui et de maintien + Ajustez la durée requise pour maintenir la pression sur vos AirPods. + Vitesse du balayage du volume + Pour éviter les changements de volume involontaires, sélectionnez le délai préféré entre les balayages. + Égaliseur + Appliquer l\'EQ à + Téléphone + Média + Bande %d + Par défaut + Plus lent + Très lent + Plus long + Très long + Plus sombre + Plus clair + Moins + Plus + Amplification + Balance + Tonalité + Réduction des bruits ambiants + Amplificateur de conversation + L\'Amplificateur de conversation concentre les AirPods Pro sur la personne en face de vous, facilitant les conversations en face-à-face. + Les AirPods peuvent utiliser les résultats d\'un test auditif pour améliorer la clarté des voix et des sons autour de vous.\n\nL\'aide auditive est destinée aux personnes ayant une perte auditive légère à modérée. + Aide multimédia + Les AirPods Pro peuvent utiliser les résultats d\'un test auditif pour améliorer la clarté de la musique, des vidéos et des appels. + Ajuster la musique et les vidéos + Ajuster les appels + Widget + Afficher la batterie du téléphone dans le widget + Affiche le niveau de batterie du téléphone dans le widget avec celle des AirPods + Volume de détection des conversations + Tuile des réglages rapides + Ouvrir le dialogue de contrôle + Si désactivé, appuyer sur la tuile fera défiler les modes. Si activé, un dialogue apparaîtra pour contrôler le mode d\'écoute et la détection de conversation. + Déconnecter les AirPods quand ils ne sont pas portés + Vous pourrez toujours les contrôler depuis l\'app — cela déconnecte juste l\'audio. + Options avancées + Définir la clé d\'identité et de résolution (IRK) + Définir manuellement la clé IRK utilisée pour la résolution des adresses BLE aléatoires + Définir la clé de chiffrement + Définir manuellement la clé ENC_KEY utilisée pour déchiffrer les publicités BLE + Utiliser des paquets alternatifs pour le suivi de la tête + Activez ceci si le suivi de tête ne fonctionne pas. Cela envoie un autre type de données aux AirPods pour demander/arrêter le suivi de la tête. + Se comporter comme un appareil Apple + Active la connectivité multi-appareils et les fonctionnalités d\'accessibilité comme la personnalisation du mode transparence (amplification, tonalité, réduction de bruit ambiant, amplificateur de conversations, EQ) + Peut être instable !! Un maximum de deux appareils peut être connecté à vos AirPods. Si vous utilisez un appareil Apple comme un iPad ou un Mac, connectez-le d\'abord, puis connectez votre Android. + Réinitialiser l\'offset du hook + Cela effacera l\'offset actuel et nécessitera de refaire la configuration. Voulez-vous vraiment continuer ? + Réinitialiser + Hook offset réinitialisé. Redirection vers la configuration… + Impossible de réinitialiser l\'hook offset + Clé IRK définie avec succès + Clé de chiffrement définie avec succès + Valeur hex IRK + Valeur hex ENC_KEY + Entrez l\'IRK de 16 octets en string hexadécimal (32 caractères) : + Entrez l\'ENC_KEY de 16 octets en string hexadécimal (32 caractères) : + Doit contenir exactement 32 caractères hexadécimaux + Erreur lors de la conversion hexadécimale : + Offset trouvé. Veuillez redémarrer le processus Bluetooth. + Assistant numérique + Activé + Télécommande de l\'appareil photo + Contrôle de la caméra + Prenez une photo, lancez/arrêtez un enregistrement, etc., avec un appui simple ou un appui long. Si vous utilisez un appui simple, les gestes de contrôle multimédia seront indisponibles ; si vous utilisez un appui long, les gestes de mode d\'écoute et d\'assistant numérique seront indisponibles. + Définir un paquet d\'application de caméra personnalisé + Définir un appid de caméra personnalisé + Entrez l\'identifiant de l\'application caméra : + Appid caméra personnalisé + Appid caméra personnalisé défini avec succès + Service d\'écoute de la caméra + Service d\'écoute pour que LibrePods détecte quand la caméra est active afin d\'activer le contrôle caméra via les AirPods. + Licences open source + Mettre à jour le test d\'audition + Mettre à jour les résultats du test d\'audition + ATT Manager est null, essayez de reconnecter. + Les permissions suivantes sont requises pour utiliser l\'application. Veuillez les accorder pour continuer. + Secouez la tête ou hochez-la ! + Accès root requis + Cette application nécessite l\'accès root pour s\'injecter dans la bibliothèque Bluetooth. + Accès root refusé. Veuillez accorder les permissions root. + Étapes de dépannage + Veuillez entrer les valeurs de perte en dBHL + À propos + Nom du modèle + Numéro du modèle + Numéro de série + Version + Santé auditive + Protection auditive + Usage en environnement de travail + Protection EN 352 + La protection EN 352 limite le niveau sonore maximal à 82 dBA et répond aux exigences applicables de la norme EN 352 pour la protection auditive personnelle. + Bruit environnemental + Reconnecter au dernier appareil + Déconnecter + Désactiver la gestion du bruit + Laisser entrer les sons extérieurs + Ajuster dynamiquement les sons extérieurs + Bloquer les sons extérieurs + Animations contextuelles + Fenêtre contextuelle en bas + Afficher la fenêtre contextuelle de style iOS en bas de l\'écran lors de la connexion des AirPods. + Fenêtre Dynamic Island + Afficher la fenêtre de style Dynamic Island en haut de l\'écran pour les événements de connexion et de transfert. + diff --git a/android/app/src/main/res/values-pt/strings.xml b/android/app/src/main/res/values-pt/strings.xml new file mode 100644 index 000000000..c646198b7 --- /dev/null +++ b/android/app/src/main/res/values-pt/strings.xml @@ -0,0 +1,218 @@ + + LibrePods + Libere seus AirPods do ecossistema da Apple. + Veja o status da bateria dos seus AirPods diretamente na tela inicial! + Acessibilidade + Volume do Tom + Ajuste o volume do tom dos efeitos sonoros reproduzidos pelos AirPods. + Áudio + Áudio Adaptativo + Personalizar Áudio Adaptativo + O áudio adaptativo responde dinamicamente ao seu ambiente e cancela ou permite ruídos externos. Você pode personalizar o Áudio Adaptativo para permitir mais ou menos ruído. + Fones + Estojo + Teste + Nome + Modo de Escuta + Desligado + Transparência + Adaptativo + Cancelamento de Ruído + Pressionar e Segurar AirPods + Pressione e segure a haste para alternar entre os modos de escuta selecionados. + Gestos com a Cabeça + Esquerdo + Direito + Consciência Conversacional + Reduz o volume da mídia e diminui o ruído de fundo quando você começa a falar com outras pessoas. + Volume Personalizado + Ajusta o volume da mídia em resposta ao seu ambiente. + Cancelamento de Ruído com um AirPod + Permite que os AirPods sejam colocados em modo de cancelamento de ruído quando apenas um AirPod está no seu ouvido. + Controle de Volume + Ajuste o volume deslizando para cima ou para baixo no sensor localizado na haste dos AirPods Pro. + AirPods não conectados + Por favor, conecte seus AirPods para acessar as configurações. + Voltar + Personalizações + Volume relativo + Reduz para uma porcentagem do volume atual em vez do volume máximo. + Pausar Música + Quando você começar a falar, a música será pausada. + EXEMPLO + Adicionar widget + Controle o Modo de Controle de Ruído diretamente da sua Tela Inicial. + Conectado + Conectado ao Linux + Conectado + Movido para Linux + Movido para %1$s + Reconectar pela notificação + Rastreamento de Cabeça + Acene para atender chamadas e balance a cabeça para recusar. + Geral + Ação do Bloco de Configurações Rápidas + Mostrar diálogo de controle de ruído ao tocar. + Alternar entre modos ao tocar. + Desenvolvedor + Abrir Configurações dos AirPods + Gerencie recursos e preferências dos AirPods + Detecção Automática de Ouvido + Reprodução Automática + Pausa Automática + Solução de Problemas + Coletar logs para diagnosticar problemas com a conexão dos AirPods + Coletar Logs + Logs Salvos + Nenhum log salvo encontrado + Preferências de Auto-conexão + Conectar aos seus AirPods quando o status for: + Desconectado + Os AirPods não estão conectados a um dispositivo + Inativo + Um dispositivo está conectado aos seus AirPods, mas não está reproduzindo mídia ou em uma chamada + Reproduzindo mídia + Um dispositivo está reproduzindo mídia nos seus AirPods + Em chamada + Um dispositivo está em uma chamada com seus AirPods + Conectar aos AirPods quando seu telefone estiver: + Recebendo uma chamada + Seu telefone começa a tocar + Iniciando reprodução de mídia + Seu telefone começa a reproduzir mídia + Desfazer + Você pode personalizar o modo de Transparência para seus AirPods Pro para ajudá-lo a ouvir o que está ao seu redor. + A Redução de Som Alto pode reduzir ativamente sua exposição a ruídos ambientais altos quando estiver nos modos Transparência e Adaptativo. A Redução de Som Alto não está ativa no modo Desligado. + Redução de Som Alto + Controles de Chamada + Conectar a este dispositivo automaticamente + Quando habilitado, os AirPods tentarão conectar a este dispositivo automaticamente. Caso contrário, eles tentarão conectar automaticamente apenas quando conectados pela última vez. + Pausar mídia ao adormecer + Modo de Escuta Desligado + Quando isso estiver ativado, os modos de escuta dos AirPods incluirão uma opção Desligado. Os níveis de som alto não são reduzidos quando o modo de escuta está definido como Desligado. + Microfone + Modo do Microfone + Automático + Sempre Direito + Sempre Esquerdo + Atender chamada + Silenciar/Ativar Som + Desligar + Pressionar Uma Vez + Pressionar Duas Vezes + Auxiliar de Audição + Ajustes + Deslize para controlar a amplificação + Quando estiver no modo Transparência e nenhuma mídia estiver sendo reproduzida, deslize para cima e para baixo nos controles de toque dos seus AirPods Pro para aumentar ou diminuir a amplificação dos sons ambientais. + Modo Transparência + Personalizar Modo Transparência + Velocidade de Pressionamento + Ajuste a velocidade necessária para pressionar duas ou três vezes nos seus AirPods. + Duração de Pressionar e Segurar + Ajuste a duração necessária para pressionar e segurar nos seus AirPods. + Velocidade de Deslize de Volume + Para evitar ajustes de volume não intencionais, selecione o tempo de espera preferido entre deslizes. + Equalizador + Aplicar EQ a + Telefone + Mídia + Banda %d + Padrão + Mais Lento + Mais Lento + Mais Longo + Mais Longo + Mais Escuro + Mais Claro + Menos + Mais + Amplificação + Balanço + Tom + Redução de Ruído Ambiental + Amplificação de Conversa + A Amplificação de Conversa foca seus AirPods Pro na pessoa falando na sua frente, facilitando ouvir em uma conversa face a face. + Os AirPods podem usar os resultados de um teste auditivo para fazer ajustes que melhoram a clareza de vozes e sons ao seu redor.\n\nO Auxiliar de Audição é destinado apenas para pessoas com perda auditiva leve a moderada percebida. + Assistente de Mídia + Os AirPods Pro podem usar os resultados de um teste auditivo para fazer ajustes que melhoram a clareza de música, vídeo e chamadas. + Ajustar Música e Vídeo + Ajustar Chamadas + Widget + Mostrar bateria do telefone no widget + Exiba o nível de bateria do seu telefone no widget junto com a bateria dos AirPods + Volume de Consciência Conversacional + Bloco de Configurações Rápidas + Abrir diálogo para controlar + Se desabilitado, clicar no bloco de configurações rápidas alternará entre modos. Se habilitado, mostrará um diálogo para controlar o modo de controle de ruído e a consciência conversacional + Desconectar AirPods quando não estiver usando + Você ainda poderá controlá-los com o aplicativo - isso apenas desconecta o áudio. + Opções Avançadas + Definir Chave de Resolução de Identidade (IRK) + Defina manualmente o valor IRK usado para resolver endereços aleatórios BLE + Definir Chave de Criptografia + Defina manualmente o valor ENC_KEY usado para descriptografar anúncios BLE + Usar pacotes alternativos de rastreamento de cabeça + Habilite isso se o rastreamento de cabeça não funcionar para você. Isso envia dados diferentes para os AirPods para solicitar/parar dados de rastreamento de cabeça. + Agir como um dispositivo Apple + Habilita conectividade multi-dispositivo e recursos de Acessibilidade como personalização do modo de transparência (amplificação, tom, redução de ruído ambiental, amplificação de conversa e equalizador) + Pode ser instável!! Um máximo de dois dispositivos pode estar conectado aos seus AirPods. Se você estiver usando com um dispositivo Apple como iPad ou Mac, então conecte esse dispositivo primeiro e depois seu Android. + Redefinir Offset do Hook + Isso limpará o offset do hook atual e exigirá que você passe pelo processo de configuração novamente. Tem certeza de que deseja continuar? + Redefinir + O offset do hook foi redefinido. Redirecionando para a configuração... + Falha ao redefinir o offset do hook + IRK foi definido com sucesso + A chave de criptografia foi definida com sucesso + Valor Hexadecimal IRK + Valor Hexadecimal ENC_KEY + Digite o IRK de 16 bytes como string hexadecimal (32 caracteres): + Digite o ENC_KEY de 16 bytes como string hexadecimal (32 caracteres): + Deve ter exatamente 32 caracteres hexadecimais + Erro ao converter hexadecimal: + Offset encontrado, por favor reinicie o processo Bluetooth + Assistente Digital + Ligado + Controle Remoto da Câmera + Controle da Câmera + Capture uma foto, inicie ou pare a gravação e mais usando Pressionar Uma Vez ou Pressionar e Segurar. Ao usar AirPods para ações da câmera, se você selecionar Pressionar Uma Vez, os gestos de controle de mídia estarão indisponíveis, e se você selecionar Pressionar e Segurar, os gestos de modo de escuta e Assistente Digital estarão indisponíveis. + Defina um pacote de aplicativo personalizado para detecção de câmera + Definir ID do Aplicativo de Câmera Personalizado + Digite o ID do aplicativo da câmera: + ID do Aplicativo de Câmera Personalizado + ID do aplicativo de câmera personalizado definido com sucesso + Ouvinte de câmera + Serviço de ouvinte do LibrePods para detectar quando a câmera está ativa para ativar o controle da câmera nos AirPods. + Licenças de Código Aberto + Atualizar Teste Auditivo + Atualizar Resultado do Teste Auditivo + O gerenciador ATT está nulo, tente reconectar. + As seguintes permissões são necessárias para usar o aplicativo. Por favor, conceda-as para continuar. + Balance a cabeça ou acene! + Acesso Root Necessário + Este aplicativo precisa de acesso root para conectar-se à biblioteca Bluetooth + O acesso root foi negado. Por favor, conceda permissões root. + Etapas de Solução de Problemas + Por favor, digite os valores de perda em dbHL + Sobre + Nome do Modelo + Número do Modelo + Número de Série + Versão + Saúde Auditiva + Proteção Auditiva + Uso no Local de Trabalho + Proteção EN 352 + A Proteção EN 352 limita o nível máximo de mídia a 82 dBA e atende aos requisitos aplicáveis do padrão EN 352 para proteção auditiva pessoal. + Ruído Ambiental + Reconectar ao último dispositivo conectado + Desconectar + Desativa o gerenciamento de ruído + Permite sons externos + Ajusta dinamicamente o ruído externo + Bloqueia sons externos + Animações de pop-up + Pop-up inferior + Exibe o pop-up estilo iOS na parte inferior quando os AirPods se conectam. + Pop-up Dynamic Island + Exibe o pop-up estilo Dynamic Island no topo da tela em eventos de conexão e transferência. + diff --git a/android/app/src/main/res/values-tr/strings.xml b/android/app/src/main/res/values-tr/strings.xml new file mode 100644 index 000000000..1e6631c9a --- /dev/null +++ b/android/app/src/main/res/values-tr/strings.xml @@ -0,0 +1,213 @@ + + LibrePods + AirPods\'unuzu Apple\'ın ekosisteminden kurtarın + Ana ekranınızdan doğrudan AirPods pil durumunuzu görün! + Erişilebilirlik + Ton Seviyesi + AirPods tarafından çalınan ses efektlerinin ton seviyesini ayarlayın. + Ses + Uyarlanabilir Ses + Uyarlanabilir Sesi Özelleştir + Uyarlanabilir ses, ortamınıza dinamik olarak tepki verir ve dış gürültüyü engeller veya geçirir. Uyarlanabilir Sesi daha fazla veya daha az gürültü geçirecek şekilde özelleştirebilirsiniz. + Kulaklıklar + Kılıf + Test + İsim + Dinleme Modu + Kapalı + Şeffaflık + Uyarlanabilir + Gürültü Engelleme + AirPods\'a Basılı Tutun + Seçili dinleme modları arasında geçiş yapmak için sapı basılı tutun. + Kafa Hareketleri + Sol + Sağ + Konuşma Farkındalığı + Başkalarıyla konuşmaya başladığınızda medya sesini düşürür ve arka plan gürültüsünü azaltır. + Kişiselleştirilmiş Ses + Ortamınıza göre medya sesini ayarlar. + Tek AirPod ile Gürültü Engelleme + Sadece bir AirPod kulağınızdayken gürültü engelleme moduna alınmasına izin verin. + Ses Kontrolü + AirPods Pro sapında bulunan sensörde yukarı veya aşağı kaydırarak sesi ayarlayın. + AirPods bağlı değil + Ayarlara erişmek için lütfen AirPods\'unuzu bağlayın. + Geri + Özelleştirmeler + Göreceli ses + Maksimum ses yerine mevcut sesin yüzdesine göre azaltır. + Müziği Duraklat + Konuşmaya başladığınızda müzik duraklatılacaktır. + ÖRNEK + Widget ekle + Gürültü Kontrol Modunu doğrudan Ana Ekranınızdan kontrol edin. + Bağlı + Linux\'a bağlı + Bağlı + Linux\'a taşındı + %1$s cihazına taşındı + Bildirimden yeniden bağlan + Kafa Takibi + Aramaları yanıtlamak için başınızı sallayın, reddetmek için başınızı sallayın. + Genel + Hızlı Ayarlar Döşemesi Eylemi + Dokunulduğunda gürültü kontrolü iletişim kutusunu göster. + Dokunulduğunda modlar arasında geçiş yap. + Geliştirici + AirPods Ayarlarını Aç + AirPods özelliklerini ve tercihlerini yönetin + Otomatik Kulak Algılama + Otomatik Oynat + Otomatik Duraklat + Sorun Giderme + AirPods bağlantı sorunlarını teşhis etmek için log toplayın + Log Topla + Kaydedilmiş Loglar + Kaydedilmiş log bulunamadı + Otomatik Bağlanma tercihleri + Durumu şu olduğunda AirPods\'unuza bağlanın: + Bağlantı kesildi + AirPods hiçbir cihaza bağlı değil + Boşta + Bir cihaz AirPods\'unuza bağlı, ancak medya oynatmıyor veya aramada değil + Medya oynatılıyor + Bir cihaz AirPods\'unuzda medya oynatıyor + Aramada + Bir cihaz AirPods\'unuzla aramada + Telefonunuz şu durumdayken AirPods\'a bağlanın: + Arama alınıyor + Telefonunuz çalmaya başlar + Medya oynatma başlıyor + Telefonunuz medya oynatmaya başlar + Geri Al + AirPods Pro\'nuz için Şeffaflık modunu, etrafınızdakileri duymanıza yardımcı olacak şekilde özelleştirebilirsiniz. + Yüksek Ses Azaltma, Şeffaflık ve Uyarlanabilir moddayken yüksek çevresel gürültülere maruz kalmanızı aktif olarak azaltabilir. Kapalı modda Yüksek Ses Azaltma aktif değildir. + Yüksek Ses Azaltma + Arama Kontrolleri + Bu cihaza otomatik olarak bağlan + Etkinleştirildiğinde, AirPods bu cihaza otomatik olarak bağlanmaya çalışacaktır. Aksi takdirde, yalnızca son bağlandığında otomatik bağlanmaya çalışacaktır. + Uykuya dalarken medyayı duraklat + Kapalı Dinleme Modu + Bu açıkken, AirPods dinleme modları bir Kapalı seçeneği içerecektir. Dinleme modu Kapalı olarak ayarlandığında yüksek ses seviyeleri azaltılmaz. + Mikrofon + Mikrofon Modu + Otomatik + Her Zaman Sağ + Her Zaman Sol + Aramayı yanıtla + Sessize Al/Aç + Aramayı Sonlandır + Bir Kez Bas + İki Kez Bas + İşitme Cihazı + Ayarlamalar + Güçlendirmeyi kontrol etmek için kaydırın + Şeffaflık modundayken ve medya oynatılmıyorken, çevresel seslerin güçlendirmesini artırmak veya azaltmak için AirPods Pro\'nuzun Dokunmatik kontrollerinde yukarı ve aşağı kaydırın. + Şeffaflık Modu + Şeffaflık Modunu Özelleştir + Basma Hızı + AirPods\'unuzda iki veya üç kez basmak için gereken hızı ayarlayın. + Basılı Tutma Süresi + AirPods\'unuzda basılı tutmak için gereken süreyi ayarlayın. + Ses Kaydırma Hızı + İstenmeyen ses ayarlamalarını önlemek için, kaydırmalar arasındaki tercih edilen bekleme süresini seçin. + Ekolayzer + EQ\'yu uygula + Telefon + Medya + Bant %d + Varsayılan + Daha Yavaş + En Yavaş + Daha Uzun + En Uzun + Daha Koyu + Daha Parlak + Daha Az + Daha Fazla + Güçlendirme + Denge + Ton + Ortam Gürültüsü Azaltma + Konuşma Güçlendirme + Konuşma Güçlendirme, AirPods Pro\'nuzu önünüzde konuşan kişiye odaklar, yüz yüze konuşmada duymayı kolaylaştırır. + AirPods, etrafınızdaki seslerin ve konuşmaların netliğini artıran ayarlamalar yapmak için bir işitme testinin sonuçlarını kullanabilir.\n\nİşitme Cihazı yalnızca hafif ila orta derecede işitme kaybı olan kişiler için tasarlanmıştır. + Medya Yardımı + AirPods Pro, müzik, video ve aramaların netliğini artıran ayarlamalar yapmak için bir işitme testinin sonuçlarını kullanabilir. + Müzik ve Videoyu Ayarla + Aramaları Ayarla + Widget + Widget\'ta telefon pilini göster + Widget\'ta AirPods piliyle birlikte telefonunuzun pil seviyesini göster + Konuşma Farkındalığı Sesi + Hızlı Ayarlar Döşemesi + Kontrol için iletişim kutusunu aç + Devre dışı bırakılırsa, Hızlı Ayarlar\'a tıklamak modlar arasında geçiş yapar. Etkinleştirilirse, gürültü kontrol modu ve konuşma farkındalığını kontrol etmek için bir iletişim kutusu gösterir + Takmadığınızda AirPods\'u bağlantıyı kes + Uygulama ile hala kontrol edebileceksiniz - bu sadece sesi keser. + Gelişmiş Seçenekler + Kimlik Çözümleme Anahtarı (IRK) Ayarla + BLE rastgele adreslerini çözmek için kullanılan IRK değerini manuel olarak ayarlayın + Şifreleme Anahtarı Ayarla + BLE duyurularını şifresini çözmek için kullanılan ENC_KEY değerini manuel olarak ayarlayın + Alternatif kafa takibi paketlerini kullan + Kafa takibi sizin için çalışmıyorsa bunu etkinleştirin. Bu, kafa takibi verilerini istemek/durdurmak için AirPods\'a farklı veriler gönderir. + Apple cihazı gibi davran + Çoklu cihaz bağlantısını ve Şeffaflık modunu özelleştirme (güçlendirme, ton, ortam gürültüsü azaltma, konuşma güçlendirme ve EQ) gibi Erişilebilirlik özelliklerini etkinleştirir + Kararsız olabilir!! AirPods\'unuza maksimum iki cihaz bağlanabilir. iPad veya Mac gibi bir Apple cihazıyla kullanıyorsanız, lütfen önce o cihazı, sonra Android\'inizi bağlayın. + Kanca Ofsetini Sıfırla + Bu, mevcut kanca ofsetini temizleyecek ve kurulum sürecinden tekrar geçmenizi gerektirecektir. Devam etmek istediğinizden emin misiniz? + Sıfırla + Kanca ofseti sıfırlandı. Kuruluma yönlendiriliyor... + Kanca ofseti sıfırlanamadı + IRK başarıyla ayarlandı + Şifreleme anahtarı başarıyla ayarlandı + IRK Onaltılık Değeri + ENC_KEY Onaltılık Değeri + 16 baytlık IRK\'yi onaltılık dize olarak girin (32 karakter): + 16 baytlık ENC_KEY\'i onaltılık dize olarak girin (32 karakter): + Tam olarak 32 onaltılık karakter olmalıdır + Onaltılık dönüştürme hatası: + Ofset bulundu, lütfen Bluetooth sürecini yeniden başlatın + Dijital Asistan + Açık + Kamera Uzaktan Kumandası + Kamera Kontrolü + Bir Kez Bas veya Basılı Tut kullanarak fotoğraf çekin, kaydı başlatın veya durdurun ve daha fazlasını yapın. Kamera işlemleri için AirPods kullanırken, Bir Kez Bas\'ı seçerseniz, medya kontrol hareketleri kullanılamaz ve Basılı Tut\'u seçerseniz, dinleme modu ve Dijital Asistan hareketleri kullanılamaz. + Kamera algılama için özel uygulama paketi ayarlayın + Özel Kamera uygulama kimliğini ayarla + Kamera uygulamasının uygulama kimliğini girin: + Özel Kamera uygulama kimliği + Özel kamera uygulama kimliği başarıyla ayarlandı + Kamera dinleyicisi + Kamera aktif olduğunda algılamak ve AirPods\'ta kamera kontrolünü etkinleştirmek için LibrePods dinleyici servisi. + Açık Kaynak Lisansları + İşitme Testini Güncelle + İşitme Testi Sonucunu Güncelle + ATT Yöneticisi null, yeniden bağlanmayı deneyin. + Uygulamayı kullanmak için aşağıdaki izinler gereklidir. Devam etmek için lütfen bunları verin. + Başınızı sallayın veya başınızı sallayın! + Root Erişimi Gerekli + Bu uygulama Bluetooth kütüphanesine bağlanmak için root erişimine ihtiyaç duyar + Root erişimi reddedildi. Lütfen root izinlerini verin. + Sorun Giderme Adımları + Lütfen kayıp değerlerini dbHL cinsinden girin + Hakkında + Model Adı + Model Numarası + Seri Numarası + Sürüm + İşitme Sağlığı + İşitme Koruması + İş Yeri Kullanımı + EN 352 Koruması + EN 352 Koruması, medyanın maksimum seviyesini 82 dBA ile sınırlar ve kişisel işitme koruması için geçerli EN 352 Standart gereksinimlerini karşılar. + Çevresel Gürültü + Son bağlanan cihaza yeniden bağlan + Bağlantıyı Kes + Gürültü yönetimini kapatır + Dış sesleri içeri alır + Dış gürültüyü dinamik olarak ayarlar + Dış sesleri engeller + diff --git a/android/app/src/main/res/values-uk/strings.xml b/android/app/src/main/res/values-uk/strings.xml new file mode 100644 index 000000000..ce9569441 --- /dev/null +++ b/android/app/src/main/res/values-uk/strings.xml @@ -0,0 +1,213 @@ + + LibrePods + Звільніть ваші AirPods від екосистеми Apple + Перегляньте статус батареї ваших AirPods прямо з головного екрана! + Доступність + Гучність Тону + Налаштуйте гучність тону звукових ефектів, які відтворюються на AirPods. + Аудіо + Адаптивний Звук + Налаштувати Адаптивний Звук + Адаптивний звук динамічно реагує на ваше оточення та приглушує або пропускає зовнішній шум. Ви можете налаштувати Адаптивний Звук, щоб пропускати більше або менше шуму. + Навушники + Кейс + Тест + Назва + Режим Прослуховування + Вимкнено + Проникність + Адаптування + Шумогасіння + Натисніть і утримуйте AirPods + Натисніть і утримуйте ніжку, щоб перемикатися між обраними режимами прослуховування. + Жести Головою + Лівий + Правий + Виявлення Розмови + Знижує гучність медіа та зменшує фоновий шум, коли ви починаєте говорити. + Персональна Гучність + Налаштовує гучність медіа відповідно до вашого оточення. + Шумогасіння з одним AirPod + Дозволяє вмикати режим шумогасіння на AirPods, коли лише один AirPod знаходиться у вашому вусі. + Налаштування Гучності + Налаштуйте гучність, проводячи вгору або вниз по сенсору, розташованому на ніжці AirPods Pro. + AirPods не підключені + Будь ласка, підключіть ваші AirPods, щоб отримати доступ до налаштувань. + Назад + Персоналізація + Відносна гучність + Зменшує до відсотка від поточної гучності, а не від максимальної. + Призупинити Музику + Коли ви почнете говорити, музику буде призупинено. + ПРИКЛАД + Додати віджет + Керуйте режимом шумоконтролю прямо з головного екрана. + Підключено + Підключено до Linux + Підключено + Переміщено до Linux + Переміщено до %1$s + Перепідключитися через повідомлення + Відстеження Голови + Кивніть, щоб відповісти на дзвінок, і похитайте головою, щоб відхилити. + Основне + Дія плитки швидких налаштувань + Показати діалог шумоконтролю при натисканні. + Перемикатися між режимами при натисканні. + Розробник + Відкрити Налаштування AirPods + Керуйте функціями та налаштуваннями AirPods + Автоматичне Розпізнавання Вуха + Автовідтворення + Автопауза + Усунення несправностей + Зібрати логи для діагностики проблем з підключенням AirPods + Зібрати Логи + Збережені Логи + Збережені Логи не знайдено + Налаштування авто-підключення + Підключатися до ваших AirPods, коли їхній статус: + Відʼєднано + AirPods не підключені до жодного пристрою + Бездіяльний + Пристрій підключено до ваших AirPods, але не відтворює медіа і не на дзвінку + Відтворення медіа + Пристрій відтворює медіа на ваших AirPods + На дзвінку + Пристрій на дзвінку з вашими AirPods + Підключатися до AirPods, коли ваш телефон: + Отримання дзвінка + Ваш телефон починає дзвонити + Початок відтворення медіа + Ваш телефон починає відтворювати медіа + Скасувати + Ви можете налаштувати режим проникності для ваших AirPods Pro, щоб допомогти чути, що відбувається навколо. + Зменшення гучних звуків може активно зменшити вплив гучних навколишніх шумів на вас у режимах Проникності та Адаптування. Зменшення гучних звуків не активне у вимкненому режимі. + Зменшення гучних звуків + Контроль дзвінків + Підключатися до цього пристрою автоматично + Коли ця опція ввімкнена, AirPods будуть автоматично підключатися до цього пристрою. Коли вимкнена, вони будуть автопідключатися лише до пристрою, до якого підключалися востаннє. + Призупинити медіа при засипанні + Вимкнути режим прослуховування + Коли це ввімкнено, режими прослуховування AirPods будуть включати опцію «Вимкнено». Гучні звуки не зменшуються, коли режим прослуховування встановлений на «Вимкнено». + Мікрофон + Режим мікрофона + Автоматичний + Завжди правий + Завжди лівий + Відповісти на дзвінок + Вимкнути/Увімкнути звук + Завершити Дзвінок + Натиснути один раз + Натиснути двічі + Слуховий апарат + Налаштування + Провести пальцем для керування підсиленням + Коли в режимі Проникності і медіа не відтворюється, проведіть пальцем вгору або вниз по сенсорних елементах керування ваших AirPods Pro, щоб збільшити або зменшити підсилення навколишніх звуків. + Режим Проникності + Налаштувати режим проникності + Швидкість натискання + Налаштуйте швидкість, необхідну для натискання два або три рази на ваших AirPods. + Тривалість натискання і утримування + Налаштуйте тривалість, необхідну для натискання і утримування на ваших AirPods. + Швидкість проведення пальцем для гучності + Щоб запобігти ненавмисним налаштуванням гучності, виберіть бажаний час очікування між проведеннями пальцем. + Еквалайзер + Застосувати EQ до + Телефон + Медіа + Смуга %d + За замовчуванням + Повільніше + Найповільніше + Довше + Найдовше + Темніше + Яскравіше + Менше + Більше + Підсилення + Баланс + Тон + Зменшення навколишнього шуму + Підсилення розмови + Підсилення розмови фокусує ваші AirPods Pro на людині, яка говорить перед вами, полегшуючи спілкування віч-на-віч. + AirPods можуть використовувати результати тесту слуху для налаштувань, які покращують чіткість голосів та звуків навколо вас.\n\nРежим слухового апарата призначений лише для людей із легким або помірним зниженням слуху. + Допомога з медіа + AirPods Pro можуть використовувати результати тесту слуху для налаштувань, які покращують чіткість музики, відео та дзвінків. + Налаштувати музику та відео + Налаштувати дзвінки + Віджет + Показати заряд телефону у віджеті + Відображати рівень заряду вашого телефону у віджеті разом із зарядом AirPods + Гучність Усвідомлення Розмови + Плитка Швидких Налаштувань + Відкрити діалог для керування + Якщо вимкнено, натискання на плитку швидких налаштувань перемикатиме між режимами. Якщо ввімкнено, вона покаже діалог для керування режимом шумоконтролю та усвідомленням розмови + Відʼєднати AirPods, коли ви їх не носите + Ви все ще зможете керувати ними через додаток — це просто відʼєднує аудіо. + Розширені Налаштування + Встановити Ключ Ідентифікації (IRK) + Вручну встановити значення IRK, що використовується для розпізнавання випадкових адрес BLE + Встановити Ключ Шифрування + Вручну встановити значення ENC_KEY, що використовується для розшифровки оголошень BLE + Використовувати альтернативні пакети відстеження голови + Ввімкніть це, якщо відстеження голови не працює у вас. Це надсилає різні дані до AirPods для запиту/зупинки даних відстеження голови. + Діяти як пристрій Apple + Увімкнює багатопристроєву з\'єднаність та функції доступності, такі як налаштування режиму проникності (підсилення, тон, зменшення навколишнього шуму, підсилення розмови та еквалайзер) + Може бути нестабільним!! Максимум два пристрої можуть бути підключені до ваших AirPods. Якщо ви використовуєте з пристроєм Apple, таким як iPad або Mac, то спочатку підключіть цей пристрій, а потім ваш Android. + Скинути Зміщення Хука + Це очистить поточне зміщення хука та потребуватиме повторного налаштування. Ви впевнені, що хочете продовжити? + Скинути + Зміщення хука було скинуто. Перенаправлення до налаштування... + Не вдалося скинути зміщення хука + IRK було успішно встановлено + Ключ шифрування було успішно встановлено + Шістнадцяткове Значення IRK + Шістнадцяткове Значення ENC_KEY + Введіть 16-байтовий IRK як шістнадцятковий рядок (32 символи): + Введіть 16-байтовий ENC_KEY як шістнадцятковий рядок (32 символи): + Має бути точно 32 шістнадцяткових символи + Помилка перетворення шістнадцяткового числа: + Знайдено зміщення, будь ласка, перезапустіть Bluetooth + Цифровий Асистент + Увімкнено + Дистанційне Управління Камерою + Управління Камерою + Зробіть фото, почніть або зупиніть запис та інше, натиснувши один раз або утримавши ніжку. Коли використовуєте AirPods для керування камерою, якщо ви оберете одне натискання, жести керування медіа будуть недоступні, а якщо утримання, режими прослуховування та жести Цифрового Асистента будуть недоступні. + Встановіть власну програму для виявлення камери + Встановити власний ID програми камери + Введіть ID програми камери: + Власний ID програми камери + Власний ID програми камери встановлено успішно + Слухач камери + Служба слухача LibrePods для виявлення, коли камера активна, щоб активувати керування камерою на AirPods. + Ліцензії Відкритого Коду + Оновити Тест Слуху + Оновити Результат Тесту Слуху + Менеджер АТТ відсутній, спробуйте перепідключитися. + Для використання додатку потрібні наступні дозволи. Будь ласка, надайте їх, щоб продовжити. + Похитайте головою або кивніть! + Потрібен Root-доступ + Цей додаток потребує root-доступу, щоб підключитися до бібліотеки Bluetooth + Root-доступ було відмовлено. Будь ласка, надайте root-дозволи. + Кроки Усунення Несправностей + Будь ласка, введіть значення втрат у дБНС + Про додаток + Назва Моделі + Номер Моделі + Серійний Номер + Версія + Здоров\'я Слуху + Захист Слуху + Використання На Робочому Місці + Захист EN 352 + Захист EN 352 обмежує максимальний рівень медіа до 82 дБА та відповідає застосовним стандартам EN 352 для особистого захисту слуху. + Навколишній Шум + Перепідключитися до останнього підключеного пристрою + Відʼєднатися + Вимикає керування шумом + Пропускає зовнішні звуки + Динамічно налаштовує зовнішній шум + Блокує зовнішні звуки + diff --git a/android/app/src/main/res/values-vi/strings.xml b/android/app/src/main/res/values-vi/strings.xml new file mode 100644 index 000000000..f461c4e3a --- /dev/null +++ b/android/app/src/main/res/values-vi/strings.xml @@ -0,0 +1,213 @@ + + LibrePods + Sử dụng AirPods của bạn mà không cần hệ sinh thái của Apple. + Xem trạng thái pin AirPods trên màn hình chính! + Trợ năng + Âm lượng âm báo + Điều chỉnh âm lượng của hiệu ứng âm thanh do AirPods phát ra. + Âm thanh + Âm thanh thích ứng + Tùy chỉnh âm thanh thích ứng + Âm thanh thích ứng tự động phản ứng với môi trường xung quanh và chặn hoặc cho phép tiếng ồn bên ngoài. Bạn có thể tùy chỉnh Âm thanh thích ứng để cho phép nhiều hoặc ít tiếng ồn hơn. + Tai nghe + Hộp sạc + Kiểm tra + Tên + Chế độ nghe + Tắt + Xuyên âm + Thích ứng + Chống ồn chủ động + Nhấn và giữ AirPods + Nhấn và giữ thân tai nghe để chuyển giữa các chế độ nghe đã chọn. + Cử chỉ đầu + Trái + Phải + Phát hiện giọng nói + Tự động bật chế độ xuyên âm khi bạn bắt đầu nói chuyện với người khác. + Âm lượng cá nhân hóa + Điều chỉnh âm lượng Media phù hợp với môi trường xung quanh. + Chống ồn với một bên tai nghe + Cho phép AirPods bật chế độ chống ồn ngay cả khi chỉ sử dụng một bên tai nghe. + Điều khiển âm lượng + Điều chỉnh âm lượng bằng cách vuốt lên hoặc xuống trên cảm biến nằm ở thân tai nghe. + AirPods chưa được kết nối + Vui lòng kết nối đến AirPods của bạn để truy cập cài đặt. + Quay lại + Tùy chỉnh + Âm lượng tương đối + Giảm xuống phần trăm của âm lượng hiện tại thay vì âm lượng tối đa. + Tạm dừng nhạc + Khi bạn nói, nhạc sẽ bị tạm dừng. + EXAMPLE + Thêm widget + Điều khiển chế độ chống ồn trực tiếp từ màn hình chính. + Đã kết nối + Đã kết nối với Linux + Đã kết nối + Đã chuyển sang Linux + Đã chuyển sang %1$s + Kết nối lại từ thông báo + Theo dõi chuyển động đầu + Gật đầu để trả lời cuộc gọi, và lắc đầu để từ chối. + Chung + Hành động ô cài đặt nhanh + Hiển thị hộp thoại kiểm soát tiếng ồn khi chạm. + Chuyển đổi qua các chế độ khi chạm. + Tùy chọn nhà phát triển + Mở cài đặt AirPods + Quản lý tính năng và tùy chọn AirPods + Tự động phát hiện đeo tai nghe + Tự động phát + Tự động tạm dừng + Khắc phục sự cố + Thu thập nhật ký để chẩn đoán sự cố kết nối AirPods + Thu thập nhật ký + Nhật ký đã lưu + Không tìm thấy nhật ký đã lưu + Tùy chọn tự động kết nối + Kết nối với AirPods khi trạng thái là: + Đã ngắt kết nối + AirPods không kết nối với thiết bị nào + Rảnh tay + AirPods đã kết nối tới thiết bị nhưng không phát Media hoặc đang gọi + Đang phát Media + AirPods đang phát Media + Đang gọi + AirPods được dùng với cuộc gọi + Kết nối với AirPods khi điện thoại: + Nhận cuộc gọi + Điện thoại bắt đầu đổ chuông + Bắt đầu phát Media + Điện thoại bắt đầu phát Media + Hoàn tác + Bạn có thể tùy chỉnh chế độ xuyên âm cho AirPods để giúp bạn nghe những gì xung quanh. + Giảm âm thanh lớn có thể chủ động giảm tiếp xúc với tiếng ồn môi trường lớn khi ở chế độ xuyên âm và Thích ứng. Giảm âm thanh lớn không hoạt động ở chế độ Tắt. + Giảm âm thanh lớn + Điều khiển cuộc gọi + Tự động kết nối với thiết bị này + Khi bật, AirPods sẽ cố gắng tự động kết nối với thiết bị này. Nếu không, chúng sẽ chỉ tự động kết nối khi đã kết nối lần cuối. + Tạm dừng Media khi ngủ + Chế độ nghe Tắt + Khi bật, các chế độ nghe của AirPods sẽ bao gồm tùy chọn Tắt. Mức âm thanh lớn không được giảm khi chế độ nghe được đặt thành Tắt. + Micro + Chế độ micro + Tự động + Micro luôn ở bên phải + Micro luôn ở bên trái + Trả lời cuộc gọi + Bật/tắt tiếng + Kết thúc cuộc gọi + Nhấn một lần + Nhấn hai lần + Trợ thính + Điều chỉnh + Vuốt để điều khiển âm thanh xung quanh + Khi ở chế độ xuyên âm và không phát Media, vuốt lên và xuống trên điều khiển cảm ứng của AirPods để tăng hoặc giảm độ âm thanh xung quanh. + Chế độ Xuyên âm + Tùy chỉnh chế độ xuyên âm + Tốc độ nhấn + Điều chỉnh tốc độ cần thiết để nhấn hai hoặc ba lần trên AirPods. + Thời gian nhấn và giữ + Để điều chỉnh thời gian cần thiết, nhấn và giữ trên AirPods. + Tốc độ vuốt âm lượng + Để tránh điều chỉnh âm lượng ngoài ý muốn, hãy chọn thời gian chờ giữa các lần vuốt. + Bộ chỉnh âm + Áp dụng EQ cho + Điện thoại + Media + Dải %d + Mặc định + Chậm hơn + Chậm nhất + Lâu hơn + Lâu nhất + Tối hơn + Sáng hơn + Ít hơn + Nhiều hơn + Khuếch đại + Cân bằng + Âm sắc + Giảm tiếng ồn xung quanh + Tăng cường hội thoại + Chế độ Tăng cường hội thoại giúp AirPods tập trung vào người đang nói trước mặt bạn, giúp dễ nghe hơn trong cuộc trò chuyện trực tiếp. + AirPods có thể sử dụng kết quả của bài kiểm tra thính lực để thực hiện điều chỉnh cải thiện độ rõ của giọng nói và âm thanh xung quanh.\n\nTrợ thính chỉ dành cho người bị giảm thính lực nhẹ đến trung bình. + Hỗ trợ Media + AirPods có thể sử dụng kết quả của bài kiểm tra thính lực để thực hiện điều chỉnh cải thiện độ rõ của âm nhạc, video và cuộc gọi. + Điều chỉnh Media + Điều chỉnh cuộc gọi + Widget + Hiển thị pin điện thoại trong widget + Hiển thị pin điện thoại trong widget cùng với pin AirPods + Âm lượng nhận biết hội thoại + Ô cài đặt nhanh + Mở hộp thoại để điều khiển + Nếu tắt, nhấp vào QS sẽ chuyển đổi qua các chế độ. Nếu bật, nó sẽ hiển thị hộp thoại để điều khiển chế độ chống ồn và nhận biết hội thoại + Ngắt kết nối AirPods khi không đeo + Bạn vẫn có thể điều khiển chúng bằng ứng dụng - điều này chỉ ngắt kết nối âm thanh. + Tùy chọn nâng cao + Đặt khóa phân giải danh tính (IRK) + Đặt thủ công giá trị IRK được sử dụng để phân giải địa chỉ ngẫu nhiên BLE + Đặt khóa mã hóa + Đặt thủ công giá trị ENC_KEY được sử dụng để giải mã quảng cáo BLE + Sử dụng gói theo dõi đầu thay thế + Bật tính năng này nếu theo dõi chuyển động đầu không hoạt động. Điều này gửi dữ liệu khác đến AirPods để yêu cầu/dừng dữ liệu theo dõi chuyển động đầu. + Hoạt động như thiết bị Apple + Bật kết nối đa thiết bị và các tính năng Trợ năng như tùy chỉnh chế độ xuyên âm (khuếch đại, âm sắc, giảm tiếng ồn môi trường, tăng cường hội thoại và EQ) + Có thể không ổn định!! Tối đa hai thiết bị có thể kết nối với AirPods của bạn. Nếu bạn đang sử dụng với thiết bị Apple như iPad hoặc Mac, vui lòng kết nối thiết bị đó trước rồi mới đến Android. + Đặt lại độ lệch hook + Thao tác này sẽ xóa độ lệch hook hiện tại và yêu cầu bạn thực hiện lại quy trình thiết lập. Bạn có chắc chắn muốn tiếp tục? + Đặt lại + Đã đặt lại độ lệch hook. Đang chuyển hướng đến thiết lập... + Không thể đặt lại độ lệch hook + Đã đặt IRK thành công + Đã đặt khóa mã hóa thành công + Giá trị Hex IRK + Giá trị Hex ENC_KEY + Nhập IRK 16 byte dưới dạng chuỗi hex (32 ký tự): + Nhập ENC_KEY 16 byte dưới dạng chuỗi hex (32 ký tự): + Phải chính xác 32 ký tự hex + Lỗi chuyển đổi hex: + vui lòng khởi động lại tiến trình Bluetooth + Trợ lý kỹ thuật số + Bật + Điều khiển máy ảnh từ xa + Điều khiển máy ảnh + Chụp ảnh, bắt đầu hoặc dừng quay video và nhiều hơn nữa bằng cách Nhấn một lần hoặc Nhấn và giữ. Khi sử dụng AirPods cho các hành động máy ảnh, nếu bạn chọn Nhấn một lần, cử chỉ điều khiển Media sẽ không khả dụng và nếu bạn chọn Nhấn và giữ, các cử chỉ chế độ nghe và Trợ lý kỹ thuật số sẽ không khả dụng. + Đặt gói ứng dụng tùy chỉnh để phát hiện máy ảnh + Đặt ID ứng dụng máy ảnh tùy chỉnh + Nhập ID ứng dụng của ứng dụng máy ảnh: + ID ứng dụng máy ảnh tùy chỉnh + Đã đặt ID ứng dụng máy ảnh tùy chỉnh thành công + Trình lắng nghe máy ảnh + Dịch vụ lắng nghe để LibrePods phát hiện khi máy ảnh đang hoạt động để kích hoạt điều khiển máy ảnh trên AirPods. + Giấy phép mã nguồn mở + Cập nhật bài kiểm tra thính lực + Cập nhật kết quả kiểm tra thính lực + Trình quản lý ATT là null, thử kết nối lại. + Các quyền sau là cần thiết để sử dụng ứng dụng. Vui lòng cấp chúng để tiếp tục. + Lắc đầu hoặc gật đầu! + Yêu cầu quyền truy cập root + Ứng dụng này cần quyền truy cập root để hook vào thư viện Bluetooth + Quyền truy cập root đã bị từ chối. Vui lòng cấp quyền root. + Các bước khắc phục sự cố + Vui lòng nhập giá trị mất thính lực tính bằng dbHL + Giới thiệu + Tên sản phẩm + Số kiểu + Số sê-ri + Phiên bản + Sức khỏe thính giác + Bảo vệ thính giác + Sử dụng nơi làm việc + Bảo vệ EN 352 + Bảo vệ EN 352 giới hạn mức tối đa của Media ở 82 dBA và đáp ứng các yêu cầu Tiêu chuẩn EN 352 hiện hành về bảo vệ thính giác cá nhân. + Tiếng ồn môi trường + Kết nối lại với thiết bị được kết nối lần cuối + Ngắt kết nối + Tắt quản lý tiếng ồn + Cho phép âm thanh bên ngoài + Điều chỉnh động tiếng ồn bên ngoài + Chặn âm thanh bên ngoài + diff --git a/android/app/src/main/res/values-zh-rCN/strings.xml b/android/app/src/main/res/values-zh-rCN/strings.xml index d727f899f..6fccbd2f7 100644 --- a/android/app/src/main/res/values-zh-rCN/strings.xml +++ b/android/app/src/main/res/values-zh-rCN/strings.xml @@ -33,7 +33,7 @@ AirPods 未连接 请连接 AirPods 以访问设置。 返回 - 自定义 + 自定义 相对音量 降低到当前音量的百分比,而不是最大音量。 暂停音乐 @@ -194,6 +194,11 @@ Root 权限被拒绝。请授予 Root 权限。 故障排除步骤 请输入 dbHL 中的损失值 + 关于 + 型号名称 + 型号编号 + 序列号 + 版本 听力健康 听力保护 工作区使用 @@ -202,6 +207,8 @@ 环境噪音 重新连接到上次连接的设备 断开连接 - 我最近丢了我的左耳 AirPod。如果你觉得 LibrePods 有用,请考虑在 GitHub Sponsors 上支持我,这样我就可以购买一个替换品并继续从事这个项目——即使是少量捐助也能发挥很大作用。感谢你的支持! - 支持 LibrePods - \ No newline at end of file + 关闭噪音管理 + 允许外部声音进入 + 动态调整外部噪音 + 阻隔外部声音 + diff --git a/android/app/src/main/res/values-zh-rTW/strings.xml b/android/app/src/main/res/values-zh-rTW/strings.xml new file mode 100644 index 000000000..c3e3ad5c1 --- /dev/null +++ b/android/app/src/main/res/values-zh-rTW/strings.xml @@ -0,0 +1,244 @@ + + LibrePods + 讓你的 AirPods 擺脫 Apple 生態系統的束縛。 + 直接從主畫面查看 AirPods 電池狀態! + 輔助使用 + 提示音音量 + 調整 AirPods 播放音效的提示音音量。 + 音訊 + 自適應音訊 + 自訂自適應音訊 + 自適應音訊會動態回應你的環境,並消除或允許外部噪音。你可以自訂自適應音訊以允許更多或更少的噪音。 + 耳機 + 充電盒 + 測試 + 名稱 + 聽覺模式 + 關閉 + 通透模式 + 自適應 + 降噪 + 按住 AirPods + 按住耳機柄即可在選定的聽覺模式之間循環切換。 + 頭部手勢 + 左耳 + 右耳 + 對話感知 + 當你開始與他人交談時,降低媒體音量並減少背景噪音。 + 個人化音量 + 根據你的環境調整媒體音量。 + 使用一只 AirPod 進行降噪 + 允許在僅配戴一只 AirPod 時進入降噪模式。 + 音量控制 + 透過在 AirPods Pro 耳機柄上的感測器向上或向下滑動來調整音量。 + 未連接 AirPods + 請連接你的 AirPods 以存取設定。 + 返回 + 自訂 + 相對音量 + 降低至當前音量的百分比,而不是最大音量。 + 暫停音樂 + 當你開始說話時,音樂將會暫停。 + 範例 + 新增小工具 + 直接從主畫面控制聽覺模式。 + 已連線 + 已連線至 Linux + 已連線 + 已移至 Linux + 已移至 %1$s + 從通知重新連線 + 頭部追蹤 + 點頭接聽來電,搖頭拒接。 + 一般 + 快速設定方塊動作 + 輕觸時顯示聽覺模式對話方塊。 + 輕觸時循環切換模式。 + 開發人員 + 開啟 AirPods 設定 + 管理 AirPods 功能與偏好設定 + 自動耳朵偵測 + 自動播放 + 自動暫停 + 疑難排解 + 收集記錄以診斷 AirPods 連線問題 + 收集記錄 + 已儲存的記錄 + 找不到已儲存的記錄 + 自動連線偏好設定 + 當 AirPods 處於以下狀態時連線: + 已中斷連線 + AirPods 未連接至任何裝置 + 閒置 + 裝置已連接至你的 AirPods,但未播放媒體或通話中 + 正在播放媒體 + 裝置正在你的 AirPods 上播放媒體 + 通話中 + 裝置正在使用你的 AirPods 進行通話 + 當你的手機處於以下狀態時連接至 AirPods: + 接到來電 + 你的手機開始響鈴 + 開始播放媒體 + 你的手機開始播放媒體 + 復原 + 你可以自訂 AirPods Pro 的通透模式,以協助你聽見周圍的聲音。 + 「降低高音量」可在通透模式和自適應模式下,主動減少你接觸到的環境高噪音。在「關閉」模式下,「降低高音量」不會作用。 + 降低高音量 + 通話控制 + 自動連接此裝置 + 啟用後,AirPods 將嘗試自動連接至此裝置。否則,它們僅會在上次連接過此裝置時嘗試自動連接。 + 入睡時暫停媒體 + 「關閉」聽覺模式 + 開啟此選項後,AirPods 聽覺模式將包含「關閉」選項。當聽覺模式設為「關閉」時,不會降低高音量。 + 麥克風 + 麥克風模式 + 自動 + 總是右耳 + 總是左耳 + 接聽來電 + 靜音/取消靜音 + 掛斷 + 按一下 + 按兩下 + 助聽器 + 調整 + 滑動以控制增強 + 在通透模式且未播放媒體時,在 AirPods Pro 的觸控控制上向上或向下滑動,可增加或減少環境聲音的增強效果。 + 通透模式 + 自訂通透模式 + 按壓速度 + 調整在 AirPods 上按兩下或三下所需的速度。 + 按住持續時間 + 調整在 AirPods 上按住所須的時間。 + 音量滑動速度 + 為防止意外調整音量,請選擇滑動之間的偏好等待時間。 + 等化器 + 套用 EQ 至 + 電話 + 媒體 + 頻段 %d + 預設 + 較慢 + 最慢 + 較長 + 最長 + 較低沉 + 較清亮 + 較少 + 較多 + 增強 + 平衡 + 音色 + 環境噪音抑制 + 對話增強 + 「對話增強」會將你的 AirPods Pro 聚焦於你面前說話的人,讓你在面對面交談時更容易聽清楚。 + AirPods 可以使用聽力測試的結果進行調整,以改善你周圍的語音和聲音清晰度。 + +助聽器功能僅適用於有輕度至中度聽力受損的人士。 + 媒體輔助 + AirPods Pro 可以使用聽力測試的結果進行調整,以改善音樂、影片和通話的清晰度。 + 調整音樂與影片 + 調整通話 + 小工具 + 在小工具中顯示手機電量 + 在小工具中同時顯示手機電量與 AirPods 電量 + 對話感知音量 + 快速設定方塊 + 開啟控制對話方塊 + 若停用,點擊快速設定方塊將循環切換模式。若啟用,則會顯示用於控制聽覺模式和對話感知的對話方塊。 + 未配戴時中斷 AirPods 連線 + 你仍可使用應用程式控制它們,此選項僅會中斷音訊連線。 + 進階選項 + 設定身分解析金鑰 (IRK) + 手動設定用於解析 BLE 隨機位址的 IRK 值 + 設定加密金鑰 + 手動設定用於解密 BLE 廣播的 ENC_KEY 值 + 使用替代頭部追蹤封包 + 如果頭部追蹤對你無效,請啟用此選項。這會傳送不同的資料給 AirPods 以請求/停止頭部追蹤資料。 + 作為 Apple 裝置 + 啟用多裝置連線及輔助使用功能,例如自訂通透模式(增強、音色、環境噪音抑制、對話增強及 EQ)。 + 可能不穩定!!你的 AirPods 最多只能同時連接兩個裝置。如果你正與 iPad 或 Mac 等 Apple 裝置搭配使用,請先連接該裝置,然後再連接你的 Android。 + 重設 Hook 偏移量 + 這將清除目前的 Hook 偏移量,並需要你再次進行設定程序。確定要繼續嗎? + 重設 + Hook 偏移量已重設。正在重新導向至設定... + 重設 Hook 偏移量失敗 + IRK 已設定成功 + 加密金鑰已設定成功 + IRK 十六進位值 + ENC_KEY 十六進位值 + 輸入 16 位元組 IRK 為十六進位字串(32 個字元): + 輸入 16 位元組 ENC_KEY 為十六進位字串(32 個字元): + 必須剛好是 32 個十六進位字元 + 轉換十六進位時發生錯誤: + 找到偏移量,請重新啟動藍牙程序 + 語音助理 + 開啟 + 相機遙控 + 相機控制 + 使用「按一下」或「按住」來拍攝相片、開始或停止錄影等。當使用 AirPods 進行相機動作時,若選擇「按一下」,媒體控制手勢將無法使用;若選擇「按住」,聽覺模式和語音助理手勢將無法使用。 + 設定用於相機偵測的自訂應用程式套件 + 設定自訂相機應用程式 ID + 輸入相機應用程式的應用程式 ID: + 自訂相機應用程式 ID + 自訂相機應用程式 ID 設定成功 + 相機監聽器 + LibrePods 的監聽器服務,用於偵測相機何時啟用,以啟動 AirPods 上的相機控制。 + 開放原始碼授權 + 更新聽力測試 + 更新聽力測試結果 + ATT Manager 為空值,請嘗試重新連線。 + 需要以下權限才能使用此應用程式。請授權以繼續。 + 搖頭或點頭! + 需要 Root 權限 + 此應用程式需要 Root 權限才能 Hook 藍牙程式庫 + Root 權限被拒絕。請授權 Root 權限。 + 疑難排解步驟 + 請輸入 dbHL 中的損失值 + 關於 + 型號名稱 + 型號號碼 + 序號 + 版本 + 聽力健康 + 聽力保護 + 工作場所使用 + EN 352 防護 + EN 352 防護將媒體的最大音量限制為 82 dBA,並符合個人聽力保護的適用 EN 352 標準要求。 + 環境噪音 + 重新連接至上次連接的裝置 + 中斷連線 + 關閉噪音管理 + 允許外部聲音 + 動態調整外部噪音 + 阻隔外部聲音 + 解鎖進階功能 + 購買 %s + 恢復購買 + 取下時自動停止播放音訊,戴上時恢復播放。 + 電池 + 在應用程式與通知中查看準確的電池狀態。 + 直接從應用程式或快速設定中切換聽覺模式。 + 進階裝置設定 + 自訂個人化音量、自適應音訊、入睡時暫停媒體及其他輔助使用設定等功能。 + 自動連線 + 啟用並自訂自動連接至 AirPods 的功能。 + 存取應用程式自訂功能,包括小工具中的手機電量、對話感知音量,以及更多即將推出的自訂功能。 + 支援開發 + LibrePods 由單一開發者開發。升級有助於維持應用程式的運作。 + 功能的可用性取決於你的 AirPods 型號與韌體版本。 + 聯絡 + 電子郵件 + Discord + GitHub Issues + 版本代碼 + 建置類型 + + + 設定 + 需要 Xposed + 略過相容性檢查 + 你確定你的裝置原生支援或已啟用 Xposed 模組嗎? + 不支援 + 請查看儲存庫以獲取更多資訊。 + diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 4f6b82b44..f866a2741 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -34,7 +34,7 @@ AirPods not connected Please connect your AirPods to access settings. Back - Customizations + Customizations Relative volume Reduces to a percentage of the current volume instead of the maximum volume. Pause Music @@ -140,6 +140,11 @@ Widget Show phone battery in widget Display your phone\'s battery level in the widget alongside AirPods battery + Popup Animations + Bottom sheet popup + Show the iOS-style modal popup at the bottom when AirPods connect. + Dynamic Island popup + Show the Dynamic Island-style popup at the top for connection and takeover events. Conversational Awareness Volume Quick Settings Tile Open dialog for controlling @@ -169,7 +174,7 @@ Enter 16-byte ENC_KEY as hex string (32 characters): Must be exactly 32 hex characters Error converting hex: - Found offset please restart the Bluetooth process + Please restart the Bluetooth process Digital Assistant On Camera Remote @@ -206,8 +211,72 @@ Environmental Noise Reconnect to last connected device Disconnect - Support me - Never show again - I recently lost my left AirPod. If you\'ve found LibrePods useful, consider supporting me on GitHub Sponsors so I can buy a replacement and continue working on this project- even a little amount goes a long way. Thank you for your support! - Support LibrePods + Turns off noise management + Lets in external sounds + Dynamically adjust external noise + Blocks out external sounds + Unlock advanced features + Buy %s + Restore purchases + Automatically stop playing audio when you take them off, and resume playback when you put them back on. + Battery + View accurate battery status in the app and notification. + Switch between listening modes directly from the app or Quick Settings. + Advanced device settings + Customize settings like Personalized Volume, Adaptive Audio, Pause media when falling asleep, and other Accessibility settings. + Automatic Connection + Enable and customize automatic connection to AirPods. + Get access to app customizations, including phone battery in widget, conversational awareness volume, and many more upcoming customization features. + Support the development + LibrePods is developed by a single developer. Upgrading helps keep the app alive. + Feature availability depends on your AirPods model and firmware version. + Contact + E-Mail + Discord + GitHub Issues + Version code + Flavor + Build type + No + Yes + Settings + requires xposed + Bypass compatibility check + Are you sure your device is supported natively/you have Xposed module enabled? + Not supported + + Many devices are not supported due to limitations in the Android Bluetooth stack. + \nOn these devices, root access with an Xposed framework is required for full functionality. + \n\nThis limitation has been addressed in newer Android versions. The following device configurations can run the app natively: + \n• Google Pixel® running Android 16 March update and later with the lateset Play system update + \n• Google Pixel® running 17 Beta 3 and above + \n• OnePlus devices running OxygenOS 16 or later + \n• Oppo devices running ColorOS 16 or later + \n\nFor details, see the project documentation. + + (Name your own price) + + This device may not be supported due to platform limitations and requires an Xposed framework. Tick the checkbox below and type OK to continue. + + Type "%s" to continue + Proceed + I have read compatibility requirements. + Device information + Build ID + Manufacturer + Free features + Advanced features + Digital Assistant on Long Press + Invoke Digital Assistant when long pressing the AirPods Pro stem. + Customizations unavailable. Connect your AirPods at least once to access. + Xposed available + App enabled in Xposed + Subject + Describe your issue + Optimized Charge Limit + AirPods can learn from your daily usage and determine when to charge to an optmized limit and when to allow or full charge. This limit adapts to your daily usage and preserves your battery lifespan over time.\nThis setting may not affect unsupported AirPods, or AirPods on an older firmware version. + Enable LibrePods in Xposed or update your device to proceed. + Due to an error in billing, premium access will expire in %1$d days. If you already upgraded the app, please click on this message to email billing@kavish.xyz to restore or verify access. Apologies for the inconvenience. + Custom + Recommended diff --git a/android/app/src/main/resources/META-INF/xposed/module.prop b/android/app/src/main/resources/META-INF/xposed/module.prop index 8dc7ff352..c3975fedc 100644 --- a/android/app/src/main/resources/META-INF/xposed/module.prop +++ b/android/app/src/main/resources/META-INF/xposed/module.prop @@ -1,3 +1,3 @@ -minApiVersion=100 -targetApiVersion=100 +minApiVersion=101 +targetApiVersion=101 staticScope=true diff --git a/android/build.gradle.kts b/android/build.gradle.kts index 31555c091..45682a060 100644 --- a/android/build.gradle.kts +++ b/android/build.gradle.kts @@ -1,7 +1,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { alias(libs.plugins.android.application) apply false - alias(libs.plugins.kotlin.android) apply false alias(libs.plugins.kotlin.compose) apply false alias(libs.plugins.aboutLibraries) apply false -} \ No newline at end of file +// alias(libs.plugins.hilt) apply false +} diff --git a/android/gradle.properties b/android/gradle.properties index 2c138d5cf..8d8170162 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -6,7 +6,7 @@ # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. -org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 +org.gradle.jvmargs=-Xmx8192m -Dfile.encoding=UTF-8 # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. For more details, visit # https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects @@ -22,4 +22,17 @@ kotlin.code.style=official # thereby reducing the size of the R class for that library android.nonTransitiveRClass=true -android.javaCompile.suppressSourceTargetDeprecationWarning=true \ No newline at end of file +android.javaCompile.suppressSourceTargetDeprecationWarning=true + +org.gradle.caching=true +org.gradle.configuration-cache=true +#android.defaults.buildfeatures.resvalues=true +#android.sdk.defaultTargetSdkToCompileSdkIfUnset=false +#android.enableAppCompileTimeRClass=false +#android.usesSdkInManifest.disallowed=false +#android.uniquePackageNames=false +#android.dependency.useConstraints=true +#android.r8.strictFullModeForKeepRules=false +#android.r8.optimizedResourceShrinking=false +#android.builtInKotlin=false +#android.newDsl=false diff --git a/android/gradle/libs.versions.toml b/android/gradle/libs.versions.toml index 95f9ba628..b81f1bd7c 100644 --- a/android/gradle/libs.versions.toml +++ b/android/gradle/libs.versions.toml @@ -1,28 +1,29 @@ [versions] -accompanistPermissions = "0.36.0" -agp = "8.9.1" -hiddenapibypass = "6.1" -kotlin = "2.1.10" -coreKtx = "1.17.0" -lifecycleRuntimeKtx = "2.8.7" -activityCompose = "1.10.1" -composeBom = "2025.04.00" -annotations = "26.0.2" -navigationCompose = "2.8.9" +accompanistPermissions = "0.37.3" +agp = "9.1.1" +kotlin = "2.3.21" +coreKtx = "1.18.0" +lifecycleRuntimeKtx = "2.10.0" +activityCompose = "1.13.0" +composeBom = "2026.05.00" +annotations = "26.1.0" +navigationCompose = "2.9.8" constraintlayout = "2.2.1" -haze = "1.6.10" -hazeMaterials = "1.6.10" +haze = "1.7.2" +hazeMaterials = "1.7.2" dynamicanimation = "1.1.0" -foundationLayout = "1.9.1" -uiTooling = "1.9.1" -mockk = "1.14.3" -ui = "1.9.2" -aboutLibraries = "13.0.0-rc01" +aboutLibraries = "14.2.0" +materialIconsCore = "1.7.8" +backdrop = "2.0.0-alpha03" +billing = "8.3.0" +hilt = "2.59.2" +xposed = "101.0.0" +lifecycleProcess = "2.10.0" +play = "2.0.2" [libraries] accompanist-permissions = { module = "com.google.accompanist:accompanist-permissions", version.ref = "accompanistPermissions" } androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } -hiddenapibypass = { module = "org.lsposed.hiddenapibypass:hiddenapibypass", version.ref = "hiddenapibypass" } androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" } androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" } androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" } @@ -36,15 +37,24 @@ androidx-constraintlayout = { group = "androidx.constraintlayout", name = "const haze = { group = "dev.chrisbanes.haze", name = "haze", version.ref = "haze" } haze-materials = { group = "dev.chrisbanes.haze", name = "haze-materials", version.ref = "hazeMaterials" } androidx-dynamicanimation = { group = "androidx.dynamicanimation", name = "dynamicanimation", version.ref = "dynamicanimation" } -androidx-compose-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout", version.ref = "foundationLayout" } -androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling", version.ref = "uiTooling" } -mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" } -androidx-compose-ui = { group = "androidx.compose.ui", name = "ui", version.ref = "ui" } +androidx-compose-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout"} +androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" } +androidx-compose-ui = { group = "androidx.compose.ui", name = "ui" } aboutlibraries = { group = "com.mikepenz", name = "aboutlibraries", version.ref = "aboutLibraries" } aboutlibraries-compose-m3 = { group = "com.mikepenz", name = "aboutlibraries-compose-m3", version.ref = "aboutLibraries" } +androidx-compose-material-icons-core = { group = "androidx.compose.material", name = "material-icons-core", version.ref = "materialIconsCore" } +backdrop = { group = "io.github.kyant0", name = "backdrop", version.ref = "backdrop" } +billing = { group = "com.android.billingclient", name = "billing-ktx", version.ref = "billing" } +hilt = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" } +hilt-compiler = { group = "com.google.dagger", name = "hilt-compiler", version.ref = "hilt" } +libxposed-api = { group = "io.github.libxposed", name = "api", version.ref = "xposed" } +libxposed-service = { group = "io.github.libxposed", name = "service", version.ref = "xposed" } +androidx-lifecycle-process = { group = "androidx.lifecycle", name = "lifecycle-process", version.ref = "lifecycleProcess" } +play-review = { group = "com.google.android.play", name="review", version.ref = "play" } +play-review-ktx = { group = "com.google.android.play", name="review-ktx", version.ref = "play" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" } -kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } -aboutLibraries = { id = "com.mikepenz.aboutlibraries.plugin", version.ref = "aboutLibraries" } \ No newline at end of file +aboutLibraries = { id = "com.mikepenz.aboutlibraries.plugin", version.ref = "aboutLibraries" } +hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" } diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 4e7f0c702..c9214345a 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Mon Oct 07 22:30:36 IST 2024 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/build-magisk-module.sh b/build-magisk-module.sh deleted file mode 100755 index 7f8c1950f..000000000 --- a/build-magisk-module.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -set -eux - -cd root-module -rm -f ../btl2capfix.zip - -# COPYFILE_DISABLE env is a macOS fix to avoid parasitic files in ZIPs: https://superuser.com/a/260264 -export COPYFILE_DISABLE=1 -curl -L -o ./radare2-5.9.9-android-aarch64.tar.gz "https://hc-cdn.hel1.your-objectstorage.com/s/v3/25e8dbfe13892b4c26f3e01bfa45197f170bb0e7_radare2-5.9.9-android-aarch64.tar.gz" -zip -r ../btl2capfix.zip . -x \*.DS_Store \*__MACOSX \*DEBIAN ._\* .gitignore diff --git a/AAP Definitions.md b/docs/AAP Definitions.md similarity index 94% rename from AAP Definitions.md rename to docs/AAP Definitions.md index 42dc6025c..87b23d905 100644 --- a/AAP Definitions.md +++ b/docs/AAP Definitions.md @@ -122,7 +122,7 @@ If primary is removed, mic will be changed and the secondary will be the new pri ## Conversational Awareness -AirPods send conversational awareness packets when the person wearing them start speaking. The packet format is as follows: +AirPods send conversational awareness packets when the person wearing them starts speaking. The packet format is as follows: ```plaintext 04 00 04 00 4B 00 02 00 01 [level] @@ -307,7 +307,7 @@ All values are formatted as IEEE 754 floats in little endian order. ## Configure Stem Long Press -I have noted all the packets sent to configure what the press and hold of the steam should do. The packets sent are specific to the current state. And are probably overwritten everytime the AirPods are connected to a new (apple) device that is not synced with icloud (i think)... So, for non-Apple device too, the configuration needs to be stored and overwritten everytime the AirPods are connected to the device. That is the only way to keep the configuration. +I have noted all the packets sent to configure what the press and hold of the steam should do. The packets sent are specific to the current state. And are probably overwritten everytime the AirPods are connected to a new (apple) device that is not synced with icloud (i think)... So, for non-Apple devices too, the configuration needs to be stored and overwritten everytime the AirPods are connected to the device. That is the only way to keep the configuration. This is also the only way to control the configuration as the previous state needs to be known, and then the new state can be set. @@ -403,20 +403,3 @@ Once tracking is active, the AirPods stream sensor packets with the following co | orientation 3 | 47 | 2 | | Horizontal Acceleration | 51 | 2 | | Vertical Acceleration | 53 | 2 | - -# LICENSE - -LibrePods - AirPods liberated from Apple’s ecosystem -Copyright (C) 2025 LibrePods contributors - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published -by the Free Software Foundation, either version 3 of the License. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see . diff --git a/docs/control_commands.md b/docs/control_commands.md index 88a01c1f1..d667ca248 100644 --- a/docs/control_commands.md +++ b/docs/control_commands.md @@ -16,53 +16,53 @@ Bytes that are not used are set to `0x00`. From what I've observed, the `data3` ## Identifiers and details -| Command identifier | Description | -|--------------|---------------------| -| 0x01 | Mic Mode | -| 0x05 | Button Send Mode | -| 0x06 | Owns connection | -| 0x0A | Ear Detection | -| 0x12 | VoiceTrigger for Siri | -| 0x14 | SingleClickMode | -| 0x15 | DoubleClickMode | -| 0x16 | ClickHoldMode | -| 0x17 | DoubleClickInterval | -| 0x18 | ClickHoldInterval | -| 0x1A | ListeningModeConfigs | -| 0x1B | OneBudANCMode | -| 0x1C | CrownRotationDirection | -| 0x0D | ListeningMode | -| 0x1E | AutoAnswerMode | -| 0x1F | Chime Volume | -| 0x20 | Connect Automatically | -| 0x23 | VolumeSwipeInterval | -| 0x24 | Call Management Config | -| 0x25 | VolumeSwipeMode | -| 0x26 | Adaptive Volume Config | -| 0x27 | Software Mute config | -| 0x28 | Conversation Detect config | -| 0x29 | SSL | -| 0x2C | Hearing Aid Enrolled and Hearing Aid Enabled | -| 0x2E | AutoANC Strength | -| 0x2F | HPS Gain Swipe | -| 0x30 | HRM enable/disable state | -| 0x31 | In Case Tone config | -| 0x32 | Siri Multitone config | -| 0x33 | Hearing Assist config | -| 0x34 | Allow Off Option for Listening Mode config | -| 0x35 | Sleep Detection config | -| 0x36 | Allow Auto Connect | -| 0x37 | PPE Toggle config | -| 0x38 | Personal Protective Equipment Cap Level config | -| 0x39 | Raw Gestures config | -| 0x3A | Temporary Pairing Config | -| 0x3B | Dynamic End of Charge config | -| 0x3C | System Siri message config | -| 0x3D | Hearing Aid Generic config | -| 0x3E | Uplink EQ Bud config | -| 0x3F | Uplink EQ Source config | -| 0x40 | In Case Tone Volume | -| 0x41 | Disable Button Input config | +| Command identifier | Description | +| ------------------ | ---------------------------------------------- | +| 0x01 | Mic Mode | +| 0x05 | Button Send Mode | +| 0x06 | Owns connection | +| 0x0A | Ear Detection | +| 0x12 | VoiceTrigger for Siri | +| 0x14 | SingleClickMode | +| 0x15 | DoubleClickMode | +| 0x16 | ClickHoldMode | +| 0x17 | DoubleClickInterval | +| 0x18 | ClickHoldInterval | +| 0x1A | ListeningModeConfigs | +| 0x1B | OneBudANCMode | +| 0x1C | CrownRotationDirection | +| 0x0D | ListeningMode | +| 0x1E | AutoAnswerMode | +| 0x1F | Chime Volume | +| 0x20 | Connect Automatically | +| 0x23 | VolumeSwipeInterval | +| 0x24 | Call Management Config | +| 0x25 | VolumeSwipeMode | +| 0x26 | Adaptive Volume Config | +| 0x27 | Software Mute config | +| 0x28 | Conversation Detect config | +| 0x29 | SSL | +| 0x2C | Hearing Aid Enrolled and Hearing Aid Enabled | +| 0x2E | AutoANC Strength | +| 0x2F | HPS Gain Swipe | +| 0x30 | HRM enable/disable state | +| 0x31 | In Case Tone config | +| 0x32 | Siri Multitone config | +| 0x33 | Hearing Assist config | +| 0x34 | Allow Off Option for Listening Mode config | +| 0x35 | Sleep Detection config | +| 0x36 | Allow Auto Connect | +| 0x37 | PPE Toggle config | +| 0x38 | Personal Protective Equipment Cap Level config | +| 0x39 | Raw Gestures config | +| 0x3A | Temporary Pairing Config | +| 0x3B | Dynamic End of Charge config | +| 0x3C | System Siri message config | +| 0x3D | Hearing Aid Generic config | +| 0x3E | Uplink EQ Bud config | +| 0x3F | Uplink EQ Source config | +| 0x40 | In Case Tone Volume | +| 0x41 | Disable Button Input config | ## Command Details diff --git a/docs/device-info.md b/docs/device-info.md new file mode 100644 index 000000000..2c8177607 --- /dev/null +++ b/docs/device-info.md @@ -0,0 +1,26 @@ +--- +opcode: 0x001D +title: Device Information +description: Information about AirPods, such as model, firmware version, and serial number. This can not be requested from the accessory; it is only sent by the accessory to the host upon connection. +--- + +## Device information + +The device information packet is sent by the accessory to the host upon connection. It contains various details about the AirPods, including model number, software version, and serial number. + +Each `null` indicates the start of a new string field. + +The data is in this order: +- Name +- Model number +- Manufacturer (always "Apple Inc.") +- Serial number +- Version 1 +- Version 2 +- Hardware revision (?) (I have `1.0.0`) +- Updater app version (?) (I have `com.apple.accessory.updater.app.71`) +- Serial number (Left Bud) +- Serial number (Right Bud) +- Version (?) (I have `8454371`) +- A few more bytes, I don't know what they are + diff --git a/docs/opcodes.md b/docs/opcodes.md new file mode 100644 index 000000000..d5299564a --- /dev/null +++ b/docs/opcodes.md @@ -0,0 +1,33 @@ +# AACP opcodes + +AACP (Apple Accessory Communication Protocol) uses various opcodes to define different types of actions and commands. Each opcode is a 16-bit integer that specifies the kind of operation being performed. The opcode is sent in little-endian format as part of the AACP packet structure. + + +| Opcode (Hex) | Destination | Description | +| ------------ | ----------- | ------------------------------------------------------------------ | +| 0x0001 | Accessory | Unknown | +| 0x0004 | Host | [Battery report](/docs/battery_report.md) | +| 0x0006 | Host | [Ear detection](/docs/ear-detection_report.md) | +| 0x0009 | Both | [Control commands](/docs/control_commands.md) | +| 0x000D | Accessory | [Audio source req](/docs/audio-source.md) | +| 0x000E | Host | [Audio source resp](/docs/audio-source.md) | +| 0x000F | Accessory | [Notification register](/docs/notification-register.md) | +| 0x0010 | Accessory | [Smart routing relay](/docs/smart-routing-relay.md#send) | +| 0x0011 | Host | [Smart routing response](/docs/smart-routing-relay.md#receive) | +| 0x0014 | Accessory | Send connected device MAC | +| 0x0017 | Both | Multiple things - undocumented | +| 0x0019 | Host | [Stem press](/docs/stem-press.md) | +| 0x001B | Accessory | [Timestamp](/docs/timestamp.md) | +| 0x001D | Host | [Device Information](/docs/device-info.md) | +| 0x001E | Accessory | [Rename device](/docs/rename.md) | +| 0x0022 | Accessory | Unknown | +| 0x0029 | Accessory | [Host capabilities](/docs/host-capabilities.md#another-opcode) (?) | +| 0x002B | Host | Paired devices (?) | +| 0x002D | Accessory | [List of connected dev. req](/docs/connected-devices.md#send) | +| 0x002E | Host | [List of connected devices](/docs/connected-devices.md#receive) | +| 0x0030 | Accessory | [BLE keys req](/docs/ble-keys.md) | +| 0x0031 | Host | [BLE keys response](/docs/ble-keys.md) | +| 0x004B | Host | [Conversation awareness](/docs/conversational-awareness.md) | +| 0x004D | Accessory | [Host capabilities](/docs/host-capabilities.md) | +| 0x004F | Both | Information req/res (doesn't work, even with apple's DID) | +| 0x0053 | Both | [EQ data](/docs/eq.md) | \ No newline at end of file diff --git a/CHANGELOG.md b/extras/CHANGELOG.md similarity index 100% rename from CHANGELOG.md rename to extras/CHANGELOG.md diff --git a/proximity_keys.py b/extras/proximity_keys.py similarity index 64% rename from proximity_keys.py rename to extras/proximity_keys.py index a0a9e4218..29ec444f2 100644 --- a/proximity_keys.py +++ b/extras/proximity_keys.py @@ -4,50 +4,53 @@ # See https://github.com/google/bumble/blob/main/docs/mkdocs/src/platforms/windows.md for usage. # You need to associate WinUSB with your Bluetooth interface. Once done, you can roll back to the original driver from Device Manager. -import sys import asyncio -import argparse +import colorama import logging import platform -from typing import Any, Optional - -from colorama import Fore, Style, init as colorama_init -colorama_init(autoreset=True) - -handler = logging.StreamHandler() -class ColorFormatter(logging.Formatter): - COLORS = { +from argparse import ArgumentParser, Namespace +from asyncio import Queue, TimeoutError +from colorama import Fore, Style +from logging import Formatter, LogRecord, Logger, StreamHandler +from socket import socket as Socket +from typing import Any, Dict, List, Optional, Tuple + +colorama.init(autoreset=True) + +handler: StreamHandler = StreamHandler() +class ColorFormatter(Formatter): + COLORS: Dict[int, str] = { logging.DEBUG: Fore.BLUE, logging.INFO: Fore.GREEN, logging.WARNING: Fore.YELLOW, logging.ERROR: Fore.RED, logging.CRITICAL: Fore.MAGENTA, } - def format(self, record): - color = self.COLORS.get(record.levelno, "") - prefix = f"{color}[{record.levelname}:{record.name}]{Style.RESET_ALL}" + def format(self, record: LogRecord) -> str: + color: str = self.COLORS.get(record.levelno, "") + prefix: str = f"{color}[{record.levelname}:{record.name}]{Style.RESET_ALL}" return f"{prefix} {record.getMessage()}" handler.setFormatter(ColorFormatter()) logging.basicConfig(level=logging.INFO, handlers=[handler]) -logger = logging.getLogger("proximitykeys") +logger: Logger = logging.getLogger("proximitykeys") -PROXIMITY_KEY_TYPES = {0x01: "IRK", 0x04: "ENC_KEY"} +PROXIMITY_KEY_TYPES: Dict[int, str] = {0x01: "IRK", 0x04: "ENC_KEY"} -def parse_proximity_keys_response(data: bytes): +def parse_proximity_keys_response(data: bytes) -> Optional[List[Tuple[str, bytes]]]: if len(data) < 7 or data[4] != 0x31: return None - key_count = data[6] - keys = [] - offset = 7 + key_count: int = data[6] + keys: List[Tuple[str, bytes]] = [] + offset: int = 7 for _ in range(key_count): if offset + 3 >= len(data): break - key_type = data[offset] - key_length = data[offset + 2] + key_type: int = data[offset] + key_length: int = data[offset + 2] offset += 4 if offset + key_length > len(data): break - key_bytes = data[offset:offset + key_length] + key_bytes: bytes = data[offset:offset + key_length] keys.append((PROXIMITY_KEY_TYPES.get(key_type, f"TYPE_{key_type:02X}"), key_bytes)) offset += key_length return keys @@ -55,7 +58,7 @@ def parse_proximity_keys_response(data: bytes): def hexdump(data: bytes) -> str: return " ".join(f"{b:02X}" for b in data) -async def run_bumble(bdaddr: str): +async def run_bumble(bdaddr: str) -> int: try: from bumble.l2cap import ClassicChannelSpec from bumble.transport import open_transport @@ -68,19 +71,23 @@ async def run_bumble(bdaddr: str): logger.error("Bumble not installed") return 1 - PSM_PROXIMITY = 0x1001 - HANDSHAKE = bytes.fromhex("00 00 04 00 01 00 02 00 00 00 00 00 00 00 00 00") - KEY_REQ = bytes.fromhex("04 00 04 00 30 00 05 00") + PSM_PROXIMITY: int = 0x1001 + HANDSHAKE: bytes = bytes.fromhex("00 00 04 00 01 00 02 00 00 00 00 00 00 00 00 00") + KEY_REQ: bytes = bytes.fromhex("04 00 04 00 30 00 05 00") class KeyStore: - async def delete(self, name: str): pass - async def update(self, name: str, keys: Any): pass - async def get(self, _name: str) -> Optional[Any]: return None - async def get_all(self): return [] - - async def get_resolving_keys(self) -> list[tuple[bytes, Any]]: - all_keys = await self.get_all() - resolving_keys = [] + async def delete(self, name: str) -> None: + pass + async def update(self, name: str, keys: Any) -> None: + pass + async def get(self, _name: str) -> Optional[Any]: + return None + async def get_all(self) -> List[Tuple[str, Any]]: + return [] + + async def get_resolving_keys(self) -> List[Tuple[bytes, Any]]: + all_keys: List[Tuple[str, Any]] = await self.get_all() + resolving_keys: List[Tuple[bytes, Any]] = [] for name, keys in all_keys: if getattr(keys, "irk", None) is not None: resolving_keys.append(( @@ -89,8 +96,8 @@ async def get_resolving_keys(self) -> list[tuple[bytes, Any]]: )) return resolving_keys - async def exchange_keys(channel, timeout=5.0): - recv_q: asyncio.Queue = asyncio.Queue() + async def exchange_keys(channel: Any, timeout: float = 5.0) -> Optional[List[Tuple[str, bytes]]]: + recv_q: Queue = Queue() channel.sink = lambda sdu: recv_q.put_nowait(sdu) logger.info("Sending handshake packet...") channel.send_pdu(HANDSHAKE) @@ -99,19 +106,19 @@ async def exchange_keys(channel, timeout=5.0): channel.send_pdu(KEY_REQ) while True: try: - pkt = await asyncio.wait_for(recv_q.get(), timeout) - except asyncio.TimeoutError: + pkt: bytes = await asyncio.wait_for(recv_q.get(), timeout) + except TimeoutError: logger.error("Timed out waiting for SDU response") return None logger.debug("Received SDU (%d bytes): %s", len(pkt), hexdump(pkt)) - keys = parse_proximity_keys_response(pkt) + keys: Optional[List[Tuple[str, bytes]]] = parse_proximity_keys_response(pkt) if keys: return keys - async def get_device(): + async def get_device() -> Tuple[Any, Device]: logger.info("Opening transport...") - transport = await open_transport("usb:0") - device = Device(host=Host(controller_source=transport.source, controller_sink=transport.sink)) + transport: Any = await open_transport("usb:0") + device: Device = Device(host=Host(controller_source=transport.source, controller_sink=transport.sink)) device.classic_enabled = True device.le_enabled = False device.keystore = KeyStore() @@ -123,15 +130,15 @@ async def get_device(): logger.info("Device powered on") return transport, device - async def create_channel_and_exchange(conn): - spec = ClassicChannelSpec(psm=PSM_PROXIMITY, mtu=2048) + async def create_channel_and_exchange(conn: Any) -> None: + spec: ClassicChannelSpec = ClassicChannelSpec(psm=PSM_PROXIMITY, mtu=2048) logger.info("Requesting L2CAP channel on PSM = 0x%04X", spec.psm) if not conn.is_encrypted: logger.info("Enabling link encryption...") await conn.encrypt() await asyncio.sleep(0.05) - channel = await conn.create_l2cap_channel(spec=spec) - keys = await exchange_keys(channel, timeout=8.0) + channel: Any = await conn.create_l2cap_channel(spec=spec) + keys: Optional[List[Tuple[str, bytes]]] = await exchange_keys(channel, timeout=8.0) if not keys: logger.warning("No proximity keys found") return @@ -165,14 +172,14 @@ async def create_channel_and_exchange(conn): logger.info("Transport closed") return 0 -def run_linux(bdaddr: str): +def run_linux(bdaddr: str) -> None: import socket - PSM = 0x1001 - handshake = bytes.fromhex("00 00 04 00 01 00 02 00 00 00 00 00 00 00 00 00") - key_req = bytes.fromhex("04 00 04 00 30 00 05 00") + PSM: int = 0x1001 + handshake: bytes = bytes.fromhex("00 00 04 00 01 00 02 00 00 00 00 00 00 00 00 00") + key_req: bytes = bytes.fromhex("04 00 04 00 30 00 05 00") logger.info("Connecting to %s (L2CAP)...", bdaddr) - sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP) + sock: Socket = Socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP) try: sock.connect((bdaddr, PSM)) logger.info("Connected, sending handshake and key request...") @@ -180,9 +187,9 @@ def run_linux(bdaddr: str): sock.send(key_req) while True: - pkt = sock.recv(1024) + pkt: bytes = sock.recv(1024) logger.debug("Received packet (%d bytes): %s", len(pkt), hexdump(pkt)) - keys = parse_proximity_keys_response(pkt) + keys: Optional[List[Tuple[str, bytes]]] = parse_proximity_keys_response(pkt) if keys: logger.info("Keys successfully retrieved") print(f"{Fore.CYAN}{Style.BRIGHT}Proximity Keys:{Style.RESET_ALL}") @@ -197,12 +204,12 @@ def run_linux(bdaddr: str): sock.close() logger.info("Connection closed") -def main(): - parser = argparse.ArgumentParser() +def main() -> None: + parser: ArgumentParser = ArgumentParser() parser.add_argument("bdaddr") parser.add_argument("--debug", action="store_true") parser.add_argument("--bumble", action="store_true") - args = parser.parse_args() + args: Namespace = parser.parse_args() logging.getLogger().setLevel(logging.DEBUG if args.debug else logging.INFO) if args.bumble or platform.system() == "Windows": diff --git a/update.json b/extras/update_nonpatch.json similarity index 50% rename from update.json rename to extras/update_nonpatch.json index 310ff9480..d603133fc 100644 --- a/update.json +++ b/extras/update_nonpatch.json @@ -1,6 +1,6 @@ { - "version": "v0.0.3", - "versionCode": 3, - "zipUrl": "https://github.com/kavishdevar/librepods/releases/download/v0.0.3/btl2capfix-v0.0.3.zip", - "changelog": "https://raw.githubusercontent.com/kavishdevar/librepods/main/CHANGELOG.md" -} + "version": "v0.2.6", + "versionCode": 46, + "zipUrl": "https://github.com/kavishdevar/librepods/releases/download/v0.2.6/LibrePods-FOSS-v0.2.6-release.zip", + "changelog": "https://raw.githubusercontent.com/kavishdevar/librepods/main/extras/CHANGELOG.md" +} \ No newline at end of file diff --git a/head-tracking/README.md b/head-tracking/README.md deleted file mode 100644 index 7ded83c54..000000000 --- a/head-tracking/README.md +++ /dev/null @@ -1,60 +0,0 @@ -# AirPods Head Tracking Visualizer - -This implements head tracking with AirPods by gathering sensor data over l2cap, processing orientation and acceleration values, and detecting head gestures. The codebase is split into the following components: - -# How to use - -Connect your airpods and change the mac address in `plot.py` to your airpods mac address. Then run the following command to start the program. - -```bash -python plot.py -``` - -Alternatively, you can directly run the `gestures.py` to just detect gestures. - -```bash -python gestures.py -``` - -- **Connection and Data Collection** - The project uses a custom ConnectionManager (imported in multiple files) to connect via Bluetooth to AirPods. Once connected, sensor packets are received in raw hex format. An AirPodsTracker class (in `plot.py`) handles the start/stop of tracking, logging of raw data, and parsing of packets into useful fields. - -- **Orientation Calculation and Visualization** - The `HeadOrientation` class (in `head_orientation.py`) is responsible for: - - **Calibration:** - A set number of samples (default 10) are collected to calculate the neutral (baseline) values for the sensors. For example: - `o1_neutral = np.mean(samples[:, 0])` - - **Calculating Angles:** - For each new packet, the raw orientation values are normalized by subtracting the neutral baseline. Then: - - **Pitch** is computed as: - ``` - pitch = (o2_norm + o3_norm) / 2 / 32000 * 180 - ``` - This averages the deviations from neutral, scales the result to degrees (assuming a sensor range around 32000), thus giving a smooth estimation of up/down tilt. - - **Yaw** is computed as: - ``` - yaw = (o2_norm - o3_norm) / 2 / 32000 * 180 - ``` - Here, the difference between the two sensor axes is used to detect left/right rotation. - - **ASCII Visualization:** - Based on the calculated pitch and yaw, an ASCII art "face" is generated. The algorithm rotates points on a circle using simple trigonometric formulas (with scaling factors based on sensor depth) to build an approximate visual representation of head orientation. - -- **Live Plotting and Interactive Commands** - The code offers both terminal-based plotting and graphical plotting via matplotlib. The AirPodsTracker manages live plotting by maintaining a buffer of recent packets. When in terminal mode, the code uses libraries like `asciichartpy` and `drawille` to render charts; in graphical mode, it creates live-updating plots. - -- **Gesture Detection** - The `GestureDetector` class (in `gestures.py`) processes the head tracking data to detect nodding ("Yes") or head shaking ("No"): - - **Smoothing:** - Raw horizontal and vertical sensor data undergo moving-average smoothing using small fixed-size buffers. This reduces noise and provides a steadier signal. - - **Peak and Trough Detection:** - The code monitors small sections (e.g. the last 4 values) to compute variance and dynamically determine thresholds for direction changes. When a significant reversal (e.g. from increasing to decreasing) is detected that surpasses the dynamic threshold value (derived partly from a fixed threshold and variance), a peak or trough is recorded. - - **Rhythm Consistency:** - Time intervals between detected peaks are captured. The consistency of these intervals (by comparing them to their mean and computing relative variance) is used to evaluate whether the movement is rhythmic—a trait of intentional gestures. - - **Confidence Calculation:** - Multiple factors are considered: - - **Amplitude Factor:** Compares the average detected peak amplitude with a constant (like 600) to provide a normalized measure. - - **Rhythm Factor:** Derived from the consistency of the time intervals of the peaks. - - **Alternation Factor:** Verifies that the signal alternates (for instance, switching between positive and negative values). - - **Isolation Factor:** Checks that movement on the target axis (vertical for nodding, horizontal for shaking) dominates over the non-target axis. - - A weighted sum of these factors forms a confidence score which, if above a predefined threshold (e.g. 0.7), confirms a detected gesture. diff --git a/head-tracking/connection_manager.py b/head-tracking/connection_manager.py deleted file mode 100644 index 1e18b047d..000000000 --- a/head-tracking/connection_manager.py +++ /dev/null @@ -1,62 +0,0 @@ -import bluetooth -import logging - -class ConnectionManager: - INIT_CMD = "00 00 04 00 01 00 02 00 00 00 00 00 00 00 00 00" - START_CMD = "04 00 04 00 17 00 00 00 10 00 10 00 08 A1 02 42 0B 08 0E 10 02 1A 05 01 40 9C 00 00" - STOP_CMD = "04 00 04 00 17 00 00 00 10 00 11 00 08 7E 10 02 42 0B 08 4E 10 02 1A 05 01 00 00 00 00" - - def __init__(self, bt_addr="28:2D:7F:C2:05:5B", psm=0x1001, logger=None): - self.bt_addr = bt_addr - self.psm = psm - self.logger = logger if logger else logging.getLogger(__name__) - self.sock = None - self.connected = False - self.started = False - - def connect(self): - self.logger.info(f"Connecting to {self.bt_addr} on PSM {self.psm:#04x}...") - try: - self.sock = bluetooth.BluetoothSocket(bluetooth.L2CAP) - self.sock.connect((self.bt_addr, self.psm)) - self.connected = True - self.logger.info("Connected to AirPods.") - self.sock.send(bytes.fromhex(self.INIT_CMD)) - self.logger.info("Initialization complete.") - except Exception as e: - self.logger.error(f"Connection failed: {e}") - self.connected = False - return self.connected - - def send_start(self): - if not self.connected: - self.logger.error("Not connected. Cannot send START command.") - return False - if not self.started: - self.sock.send(bytes.fromhex(self.START_CMD)) - self.started = True - self.logger.info("START command sent.") - else: - self.logger.info("START command has already been sent.") - return True - - def send_stop(self): - if self.connected and self.started: - try: - self.sock.send(bytes.fromhex(self.STOP_CMD)) - self.logger.info("STOP command sent.") - self.started = False - except Exception as e: - self.logger.error(f"Error sending STOP command: {e}") - else: - self.logger.info("Cannot send STOP; not started or not connected.") - - def disconnect(self): - if self.sock: - try: - self.sock.close() - self.logger.info("Disconnected from AirPods.") - except Exception as e: - self.logger.error(f"Error during disconnect: {e}") - self.connected = False - self.started = False \ No newline at end of file diff --git a/head-tracking/gestures.py b/head-tracking/gestures.py deleted file mode 100644 index 394b72a89..000000000 --- a/head-tracking/gestures.py +++ /dev/null @@ -1,381 +0,0 @@ -import bluetooth -import threading -import time -import logging -import statistics -from collections import deque - -class Colors: - RESET = "\033[0m" - BOLD = "\033[1m" - RED = "\033[91m" - GREEN = "\033[92m" - YELLOW = "\033[93m" - BLUE = "\033[94m" - MAGENTA = "\033[95m" - CYAN = "\033[96m" - WHITE = "\033[97m" - BG_BLACK = "\033[40m" - -class ColorFormatter(logging.Formatter): - FORMATS = { - logging.DEBUG: Colors.BLUE + "[%(levelname)s] %(message)s" + Colors.RESET, - logging.INFO: Colors.GREEN + "%(message)s" + Colors.RESET, - logging.WARNING: Colors.YELLOW + "%(message)s" + Colors.RESET, - logging.ERROR: Colors.RED + "[%(levelname)s] %(message)s" + Colors.RESET, - logging.CRITICAL: Colors.RED + Colors.BOLD + "[%(levelname)s] %(message)s" + Colors.RESET - } - - def format(self, record): - log_fmt = self.FORMATS.get(record.levelno) - formatter = logging.Formatter(log_fmt, datefmt="%H:%M:%S") - return formatter.format(record) - -handler = logging.StreamHandler() -handler.setFormatter(ColorFormatter()) -log = logging.getLogger(__name__) -log.setLevel(logging.INFO) -log.addHandler(handler) -log.propagate = False - -class GestureDetector: - INIT_CMD = "00 00 04 00 01 00 02 00 00 00 00 00 00 00 00 00" - START_CMD = "04 00 04 00 17 00 00 00 10 00 10 00 08 A1 02 42 0B 08 0E 10 02 1A 05 01 40 9C 00 00" - STOP_CMD = "04 00 04 00 17 00 00 00 10 00 11 00 08 7E 10 02 42 0B 08 4E 10 02 1A 05 01 00 00 00 00" - - def __init__(self, conn=None): - self.sock = None - self.bt_addr = "28:2D:7F:C2:05:5B" - self.psm = 0x1001 - self.running = False - self.data_lock = threading.Lock() - - self.horiz_buffer = deque(maxlen=100) - self.vert_buffer = deque(maxlen=100) - - self.horiz_avg_buffer = deque(maxlen=5) - self.vert_avg_buffer = deque(maxlen=5) - - self.horiz_peaks = [] - self.horiz_troughs = [] - self.vert_peaks = [] - self.vert_troughs = [] - - self.last_peak_time = 0 - self.peak_intervals = deque(maxlen=5) - - self.peak_threshold = 400 - self.direction_change_threshold = 175 - self.rhythm_consistency_threshold = 0.5 - - self.horiz_increasing = None - self.vert_increasing = None - - self.required_extremes = 3 - self.detection_timeout = 15 - - self.min_confidence_threshold = 0.7 - - self.conn = conn - - def connect(self): - try: - log.info(f"Connecting to AirPods at {self.bt_addr}...") - if self.conn is None: - from connection_manager import ConnectionManager - self.conn = ConnectionManager(self.bt_addr, self.psm, logger=log) - if not self.conn.connect(): - return False - else: - if not self.conn.connected: - if not self.conn.connect(): - return False - self.sock = self.conn.sock - log.info(f"{Colors.GREEN}✓ Connected to AirPods via ConnectionManager{Colors.RESET}") - return True - except Exception as e: - log.error(f"{Colors.RED}Connection failed: {e}{Colors.RESET}") - return False - - def process_data(self): - """Process incoming head tracking data.""" - self.conn.send_start() - log.info(f"{Colors.GREEN}✓ Head tracking activated{Colors.RESET}") - - self.running = True - start_time = time.time() - - log.info(f"{Colors.GREEN}Ready! Make a YES or NO gesture{Colors.RESET}") - log.info(f"{Colors.YELLOW}Tip: Use natural, moderate speed head movements{Colors.RESET}") - - while self.running: - if time.time() - start_time > self.detection_timeout: - log.warning(f"{Colors.YELLOW}⚠️ Detection timeout reached. No gesture detected.{Colors.RESET}") - self.running = False - break - - try: - if not self.sock: - log.error("Socket not available.") - break - data = self.sock.recv(1024) - formatted = self.format_hex(data) - if self.is_valid_tracking_packet(formatted): - raw_bytes = bytes.fromhex(formatted.replace(" ", "")) - horizontal, vertical = self.extract_orientation_values(raw_bytes) - - if horizontal is not None and vertical is not None: - smooth_h, smooth_v = self.apply_smoothing(horizontal, vertical) - - with self.data_lock: - self.horiz_buffer.append(smooth_h) - self.vert_buffer.append(smooth_v) - - self.detect_peaks_and_troughs() - gesture = self.detect_gestures() - - if gesture: - self.running = False - break - - except Exception as e: - if self.running: - log.error(f"Data processing error: {e}") - break - - def disconnect(self): - """Disconnect from socket.""" - self.conn.disconnect() - - def format_hex(self, data): - """Format binary data to readable hex string.""" - hex_str = data.hex() - return ' '.join(hex_str[i:i+2] for i in range(0, len(hex_str), 2)) - - def is_valid_tracking_packet(self, hex_string): - """Verify packet is a valid head tracking packet.""" - standard_header = "04 00 04 00 17 00 00 00 10 00 45 00" - alternate_header = "04 00 04 00 17 00 00 00 10 00 44 00" - if not hex_string.startswith(standard_header) and not hex_string.startswith(alternate_header): - return False - - if len(hex_string.split()) < 80: - return False - - return True - - def extract_orientation_values(self, raw_bytes): - """Extract head orientation data from packet.""" - try: - horizontal = int.from_bytes(raw_bytes[51:53], byteorder='little', signed=True) - vertical = int.from_bytes(raw_bytes[53:55], byteorder='little', signed=True) - - return horizontal, vertical - except Exception as e: - log.debug(f"Failed to extract orientation: {e}") - return None, None - - def apply_smoothing(self, horizontal, vertical): - """Apply moving average smoothing (Apple-like filtering).""" - self.horiz_avg_buffer.append(horizontal) - self.vert_avg_buffer.append(vertical) - - smooth_horiz = sum(self.horiz_avg_buffer) / len(self.horiz_avg_buffer) - smooth_vert = sum(self.vert_avg_buffer) / len(self.vert_avg_buffer) - - return smooth_horiz, smooth_vert - - def detect_peaks_and_troughs(self): - """Detect motion direction changes with Apple-like refinements.""" - if len(self.horiz_buffer) < 4 or len(self.vert_buffer) < 4: - return - - h_values = list(self.horiz_buffer)[-4:] - v_values = list(self.vert_buffer)[-4:] - - h_variance = statistics.variance(h_values) if len(h_values) > 1 else 0 - v_variance = statistics.variance(v_values) if len(v_values) > 1 else 0 - - current = self.horiz_buffer[-1] - prev = self.horiz_buffer[-2] - - if self.horiz_increasing is None: - self.horiz_increasing = current > prev - - dynamic_h_threshold = max(100, min(self.direction_change_threshold, h_variance / 3)) - - if self.horiz_increasing and current < prev - dynamic_h_threshold: - if abs(prev) > self.peak_threshold: - self.horiz_peaks.append((len(self.horiz_buffer)-1, prev, time.time())) - direction = "➡️ " if prev > 0 else "⬅️ " - log.info(f"{Colors.CYAN}{direction} Horizontal max: {prev} (threshold: {dynamic_h_threshold:.1f}){Colors.RESET}") - - now = time.time() - if self.last_peak_time > 0: - interval = now - self.last_peak_time - self.peak_intervals.append(interval) - self.last_peak_time = now - - self.horiz_increasing = False - - elif not self.horiz_increasing and current > prev + dynamic_h_threshold: - if abs(prev) > self.peak_threshold: - self.horiz_troughs.append((len(self.horiz_buffer)-1, prev, time.time())) - direction = "➡️ " if prev > 0 else "⬅️ " - log.info(f"{Colors.CYAN}{direction} Horizontal max: {prev} (threshold: {dynamic_h_threshold:.1f}){Colors.RESET}") - - now = time.time() - if self.last_peak_time > 0: - interval = now - self.last_peak_time - self.peak_intervals.append(interval) - self.last_peak_time = now - - self.horiz_increasing = True - - current = self.vert_buffer[-1] - prev = self.vert_buffer[-2] - - if self.vert_increasing is None: - self.vert_increasing = current > prev - - dynamic_v_threshold = max(100, min(self.direction_change_threshold, v_variance / 3)) - - if self.vert_increasing and current < prev - dynamic_v_threshold: - if abs(prev) > self.peak_threshold: - self.vert_peaks.append((len(self.vert_buffer)-1, prev, time.time())) - direction = "⬆️ " if prev > 0 else "⬇️ " - log.info(f"{Colors.MAGENTA}{direction} Vertical max: {prev} (threshold: {dynamic_v_threshold:.1f}){Colors.RESET}") - - now = time.time() - if self.last_peak_time > 0: - interval = now - self.last_peak_time - self.peak_intervals.append(interval) - self.last_peak_time = now - - self.vert_increasing = False - - elif not self.vert_increasing and current > prev + dynamic_v_threshold: - if abs(prev) > self.peak_threshold: - self.vert_troughs.append((len(self.vert_buffer)-1, prev, time.time())) - direction = "⬆️ " if prev > 0 else "⬇️ " - log.info(f"{Colors.MAGENTA}{direction} Vertical max: {prev} (threshold: {dynamic_v_threshold:.1f}){Colors.RESET}") - - now = time.time() - if self.last_peak_time > 0: - interval = now - self.last_peak_time - self.peak_intervals.append(interval) - self.last_peak_time = now - - self.vert_increasing = True - - def calculate_rhythm_consistency(self): - """Calculate how consistent the timing between peaks is (Apple-like).""" - if len(self.peak_intervals) < 2: - return 0 - - mean_interval = statistics.mean(self.peak_intervals) - if mean_interval == 0: - return 0 - - variances = [(i/mean_interval - 1.0) ** 2 for i in self.peak_intervals] - consistency = 1.0 - min(1.0, statistics.mean(variances) / self.rhythm_consistency_threshold) - return max(0, consistency) - - def calculate_confidence_score(self, extremes, is_vertical=True): - """Calculate confidence score for gesture detection (Apple-like).""" - if len(extremes) < self.required_extremes: - return 0.0 - - sorted_extremes = sorted(extremes, key=lambda x: x[0]) - - recent = sorted_extremes[-self.required_extremes:] - - avg_amplitude = sum(abs(val) for _, val, _ in recent) / len(recent) - amplitude_factor = min(1.0, avg_amplitude / 600) - - rhythm_factor = self.calculate_rhythm_consistency() - - signs = [1 if val > 0 else -1 for _, val, _ in recent] - alternating = all(signs[i] != signs[i-1] for i in range(1, len(signs))) - alternation_factor = 1.0 if alternating else 0.5 - - if is_vertical: - vert_amp = sum(abs(val) for _, val, _ in recent) / len(recent) - horiz_vals = list(self.horiz_buffer)[-len(recent)*2:] - horiz_amp = sum(abs(val) for val in horiz_vals) / len(horiz_vals) if horiz_vals else 0 - isolation_factor = min(1.0, vert_amp / (horiz_amp + 0.1) * 1.2) - else: - horiz_amp = sum(abs(val) for _, val, _ in recent) - vert_vals = list(self.vert_buffer)[-len(recent)*2:] - vert_amp = sum(abs(val) for val in vert_vals) / len(vert_vals) if vert_vals else 0 - isolation_factor = min(1.0, horiz_amp / (vert_amp + 0.1) * 1.2) - - confidence = ( - amplitude_factor * 0.4 + - rhythm_factor * 0.2 + - alternation_factor * 0.2 + - isolation_factor * 0.2 - ) - - return confidence - - def detect_gestures(self): - """Recognize head gesture patterns with Apple-like intelligence.""" - if len(self.vert_peaks) + len(self.vert_troughs) >= self.required_extremes: - all_extremes = sorted(self.vert_peaks + self.vert_troughs, key=lambda x: x[0]) - - confidence = self.calculate_confidence_score(all_extremes, is_vertical=True) - - log.info(f"Vertical motion confidence: {confidence:.2f} (need {self.min_confidence_threshold:.2f})") - - if confidence >= self.min_confidence_threshold: - log.info(f"{Colors.GREEN}🎯 \"Yes\" Gesture Detected (confidence: {confidence:.2f}){Colors.RESET}") - return "YES" - - if len(self.horiz_peaks) + len(self.horiz_troughs) >= self.required_extremes: - all_extremes = sorted(self.horiz_peaks + self.horiz_troughs, key=lambda x: x[0]) - - confidence = self.calculate_confidence_score(all_extremes, is_vertical=False) - - log.info(f"Horizontal motion confidence: {confidence:.2f} (need {self.min_confidence_threshold:.2f})") - - if confidence >= self.min_confidence_threshold: - log.info(f"{Colors.GREEN}🎯 \"No\" gesture detected (confidence: {confidence:.2f}){Colors.RESET}") - return "NO" - - return None - - def start_detection(self): - """Begin gesture detection process.""" - log.info(f"{Colors.BOLD}{Colors.WHITE}Starting gesture detection...{Colors.RESET}") - - if not self.connect(): - log.error(f"{Colors.RED}Failed to connect to AirPods.{Colors.RESET}") - return - - data_thread = threading.Thread(target=self.process_data) - data_thread.daemon = True - data_thread.start() - - try: - data_thread.join(timeout=self.detection_timeout + 2) - if data_thread.is_alive(): - log.warning(f"{Colors.YELLOW}⚠️ Timeout reached. Stopping detection.{Colors.RESET}") - self.running = False - except KeyboardInterrupt: - log.info(f"{Colors.YELLOW}Detection canceled by user.{Colors.RESET}") - self.running = False - if __name__ == "__main__": - self.disconnect() - log.info(f"{Colors.GREEN}Gesture detection complete.{Colors.RESET}") - -if __name__ == "__main__": - print(f"{Colors.BG_BLACK}{Colors.CYAN}╔════════════════════════════════════════╗{Colors.RESET}") - print(f"{Colors.BG_BLACK}{Colors.CYAN}║ AirPods Head Gesture Detector ║{Colors.RESET}") - print(f"{Colors.BG_BLACK}{Colors.CYAN}╚════════════════════════════════════════╝{Colors.RESET}") - print(f"\n{Colors.WHITE}This program detects head gestures using AirPods:{Colors.RESET}") - print(f"{Colors.GREEN}• YES: {Colors.WHITE}nodding head up and down{Colors.RESET}") - print(f"{Colors.RED}• NO: {Colors.WHITE}shaking head left and right{Colors.RESET}\n") - - detector = GestureDetector() - detector.start_detection() \ No newline at end of file diff --git a/head-tracking/head_orientation.py b/head-tracking/head_orientation.py deleted file mode 100644 index d27cb853e..000000000 --- a/head-tracking/head_orientation.py +++ /dev/null @@ -1,142 +0,0 @@ -import math -import drawille -import numpy as np -import logging -import os - -class Colors: - RESET = "\033[0m" - BOLD = "\033[1m" - RED = "\033[91m" - GREEN = "\033[92m" - YELLOW = "\033[93m" - BLUE = "\033[94m" - MAGENTA = "\033[95m" - CYAN = "\033[96m" - WHITE = "\033[97m" - BG_BLACK = "\033[40m" - -class ColorFormatter(logging.Formatter): - FORMATS = { - logging.DEBUG: Colors.BLUE + "[%(levelname)s] %(message)s" + Colors.RESET, - logging.INFO: Colors.GREEN + "%(message)s" + Colors.RESET, - logging.WARNING: Colors.YELLOW + "%(message)s" + Colors.RESET, - logging.ERROR: Colors.RED + "[%(levelname)s] %(message)s" + Colors.RESET, - logging.CRITICAL: Colors.RED + Colors.BOLD + "[%(levelname)s] %(message)s" + Colors.RESET - } - - def format(self, record): - log_fmt = self.FORMATS.get(record.levelno) - formatter = logging.Formatter(log_fmt, datefmt="%H:%M:%S") - return formatter.format(record) - -handler = logging.StreamHandler() -handler.setFormatter(ColorFormatter()) -log = logging.getLogger(__name__) -log.setLevel(logging.INFO) -log.addHandler(handler) -log.propagate = False - - -class HeadOrientation: - def __init__(self, use_terminal=False): - self.orientation_offset = 5500 - self.o1_neutral = 19000 - self.o2_neutral = 0 - self.o3_neutral = 0 - self.calibration_samples = [] - self.calibration_complete = False - self.calibration_sample_count = 10 - self.fig = None - self.ax = None - self.arrow = None - self.animation = None - self.use_terminal = use_terminal - - def reset_calibration(self): - self.calibration_samples = [] - self.calibration_complete = False - - def add_calibration_sample(self, orientation_values): - if len(self.calibration_samples) < self.calibration_sample_count: - self.calibration_samples.append(orientation_values) - return False - if not self.calibration_complete: - self._calculate_calibration() - return True - return True - - def _calculate_calibration(self): - if len(self.calibration_samples) < 3: - log.warning("Not enough calibration samples") - return - samples = np.array(self.calibration_samples) - self.o1_neutral = np.mean(samples[:, 0]) - avg_o2 = np.mean(samples[:, 1]) - avg_o3 = np.mean(samples[:, 2]) - self.o2_neutral = avg_o2 - self.o3_neutral = avg_o3 - log.info("Calibration complete: o1_neutral=%.2f, o2_neutral=%.2f, o3_neutral=%.2f", - self.o1_neutral, self.o2_neutral, self.o3_neutral) - self.calibration_complete = True - - def calculate_orientation(self, o1, o2, o3): - if not self.calibration_complete: - return {'pitch': 0, 'yaw': 0} - o1_norm = o1 - self.o1_neutral - o2_norm = o2 - self.o2_neutral - o3_norm = o3 - self.o3_neutral - pitch = (o2_norm + o3_norm) / 2 / 32000 * 180 - yaw = (o2_norm - o3_norm) / 2 / 32000 * 180 - return {'pitch': pitch, 'yaw': yaw} - - def create_face_art(self, pitch, yaw): - if self.use_terminal: - try: - ts = os.get_terminal_size() - width, height = ts.columns, ts.lines * 2 - except Exception: - width, height = 80, 40 - else: - width, height = 80, 40 - center_x, center_y = width // 2, height // 2 - radius = (min(width, height) // 2 - 2) // 2 - pitch_rad = math.radians(pitch) - yaw_rad = math.radians(yaw) - canvas = drawille.Canvas() - def rotate_point(x, y, z, pitch_r, yaw_r): - cos_y, sin_y = math.cos(yaw_r), math.sin(yaw_r) - cos_p, sin_p = math.cos(pitch_r), math.sin(pitch_r) - x1 = x * cos_y - z * sin_y - z1 = x * sin_y + z * cos_y - y1 = y * cos_p - z1 * sin_p - z2 = y * sin_p + z1 * cos_p - scale = 1 + (z2 / width) - return int(center_x + x1 * scale), int(center_y + y1 * scale) - for angle in range(0, 360, 2): - rad = math.radians(angle) - x = radius * math.cos(rad) - y = radius * math.sin(rad) - x1, y1 = rotate_point(x, y, 0, pitch_rad, yaw_rad) - canvas.set(x1, y1) - for eye in [(-radius//2, -radius//3, 2), (radius//2, -radius//3, 2)]: - ex, ey, ez = eye - x1, y1 = rotate_point(ex, ey, ez, pitch_rad, yaw_rad) - for dx in [-1, 0, 1]: - for dy in [-1, 0, 1]: - canvas.set(x1 + dx, y1 + dy) - nx, ny = rotate_point(0, 0, 1, pitch_rad, yaw_rad) - for dx in [-1, 0, 1]: - for dy in [-1, 0, 1]: - canvas.set(nx + dx, ny + dy) - smile_depth = radius // 8 - mouth_local_y = radius // 4 - mouth_length = radius - for x_offset in range(-mouth_length // 2, mouth_length // 2 + 1): - norm = abs(x_offset) / (mouth_length / 2) - y_offset = int((1 - norm ** 2) * smile_depth) - local_x = x_offset - local_y = mouth_local_y + y_offset - mx, my = rotate_point(local_x, local_y, 0, pitch_rad, yaw_rad) - canvas.set(mx, my) - return canvas.frame() diff --git a/head-tracking/plot.py b/head-tracking/plot.py deleted file mode 100644 index 38ccea186..000000000 --- a/head-tracking/plot.py +++ /dev/null @@ -1,860 +0,0 @@ -import struct -import bluetooth -import threading -import time -from datetime import datetime -import numpy as np -import matplotlib.pyplot as plt -from matplotlib.animation import FuncAnimation -import os -import asciichartpy as acp -from rich.live import Live -from rich.layout import Layout -from rich.panel import Panel -from rich.console import Console -import drawille -from head_orientation import HeadOrientation -import logging -from connection_manager import ConnectionManager - -class Colors: - RESET = "\033[0m" - BOLD = "\033[1m" - RED = "\033[91m" - GREEN = "\033[92m" - YELLOW = "\033[93m" - BLUE = "\033[94m" - MAGENTA = "\033[95m" - CYAN = "\033[96m" - WHITE = "\033[97m" - BG_BLACK = "\033[40m" - -class ColorFormatter(logging.Formatter): - FORMATS = { - logging.DEBUG: Colors.BLUE + "[%(levelname)s] %(message)s" + Colors.RESET, - logging.INFO: Colors.GREEN + "%(message)s" + Colors.RESET, - logging.WARNING: Colors.YELLOW + "%(message)s" + Colors.RESET, - logging.ERROR: Colors.RED + "[%(levelname)s] %(message)s" + Colors.RESET, - logging.CRITICAL: Colors.RED + Colors.BOLD + "[%(levelname)s] %(message)s" + Colors.RESET - } - - def format(self, record): - log_fmt = self.FORMATS.get(record.levelno) - formatter = logging.Formatter(log_fmt, datefmt="%H:%M:%S") - return formatter.format(record) - -handler = logging.StreamHandler() -handler.setFormatter(ColorFormatter()) -logger = logging.getLogger("airpods-head-tracking") -logger.setLevel(logging.INFO) -logger.addHandler(handler) -logger.propagate = True - -INIT_CMD = "00 00 04 00 01 00 02 00 00 00 00 00 00 00 00 00" -NOTIF_CMD = "04 00 04 00 0F 00 FF FF FE FF" -START_CMD = "04 00 04 00 17 00 00 00 10 00 10 00 08 A1 02 42 0B 08 0E 10 02 1A 05 01 40 9C 00 00" -STOP_CMD = "04 00 04 00 17 00 00 00 10 00 11 00 08 7E 10 02 42 0B 08 4E 10 02 1A 05 01 00 00 00 00" - -KEY_FIELDS = { - "orientation 1": (43, 2), - "orientation 2": (45, 2), - "orientation 3": (47, 2), - - "Horizontal Acceleration": (51, 2), - "Vertical Acceleration": (53, 2), - - "unkown 1": (61, 2), - "unkown 2 ": (49, 2), -} - -class AirPodsTracker: - def __init__(self): - self.sock = None - self.recording = False - self.log_file = None - self.listener_thread = None - self.bt_addr = "28:2D:7F:C2:05:5B" - self.psm = 0x1001 - self.raw_packets = [] - self.parsed_packets = [] - self.live_data = [] - self.live_plotting = False - self.animation = None - self.fig = None - self.axes = None - self.lines = {} - self.selected_fields = [] - self.data_lock = threading.Lock() - self.orientation_offset = 5500 - self.use_terminal = True # '--terminal' in sys.argv - self.orientation_visualizer = HeadOrientation(use_terminal=self.use_terminal) - - self.conn = None - - def connect(self): - try: - logger.info("Trying to connect to %s on PSM 0x%04X...", self.bt_addr, self.psm) - self.conn = ConnectionManager(self.bt_addr, self.psm, logger=logger) - if not self.conn.connect(): - logger.error("Connection failed via ConnectionManager.") - return False - self.sock = self.conn.sock - self.sock.send(bytes.fromhex(NOTIF_CMD)) - logger.info("Sent initialization command.") - - self.listener_thread = threading.Thread(target=self.listen, daemon=True) - self.listener_thread.start() - return True - except Exception as e: - logger.error("Connection error: %s", e) - return False - - def start_tracking(self, duration=None): - if not self.recording: - self.conn.send_start() - filename = "head_tracking_" + datetime.now().strftime("%Y%m%d_%H%M%S") + ".log" - self.log_file = open(filename, "w") - self.recording = True - logger.info("Recording started. Saving data to %s", filename) - - if duration is not None and duration > 0: - def auto_stop(): - time.sleep(duration) - if self.recording: - self.stop_tracking() - logger.info("Recording automatically stopped after %s seconds.", duration) - - timer_thread = threading.Thread(target=auto_stop, daemon=True) - timer_thread.start() - logger.info("Will automatically stop recording after %s seconds.", duration) - else: - logger.info("Already recording.") - - def stop_tracking(self): - if self.recording: - self.conn.send_stop() - self.recording = False - if self.log_file is not None: - self.log_file.close() - self.log_file = None - logger.info("Recording stopped.") - else: - logger.info("Not currently recording.") - - def format_hex(self, data): - hex_str = data.hex() - return ' '.join(hex_str[i:i + 2] for i in range(0, len(hex_str), 2)) - - def parse_raw_packet(self, hex_string): - return bytes.fromhex(hex_string.replace(" ", "")) - - def interpret_bytes(self, raw_bytes, start, length, data_type="signed_short"): - if start + length > len(raw_bytes): - return None - - if data_type == "signed_short": - return int.from_bytes(raw_bytes[start:start + 2], byteorder='little', signed=True) - elif data_type == "unsigned_short": - return int.from_bytes(raw_bytes[start:start + 2], byteorder='little', signed=False) - elif data_type == "signed_short_be": - return int.from_bytes(raw_bytes[start:start + 2], byteorder='big', signed=True) - elif data_type == "float_le": - if start + 4 <= len(raw_bytes): - return struct.unpack('f', raw_bytes[start:start + 4])[0] - return None - - def normalize_orientation(self, value, field_name): - if 'orientation' in field_name.lower(): - return value + self.orientation_offset - - return value - - def parse_packet_all_fields(self, raw_bytes): - packet = {} - - packet["seq_num"] = int.from_bytes(raw_bytes[12:14], byteorder='little') - - for field_name, (start, length) in KEY_FIELDS.items(): - if field_name == "float_val" and start + 4 <= len(raw_bytes): - packet[field_name] = self.interpret_bytes(raw_bytes, start, 4, "float_le") - else: - raw_value = self.interpret_bytes(raw_bytes, start, length, "signed_short") - if raw_value is not None: - packet[field_name] = self.normalize_orientation(raw_value, field_name) - - for i in range(30, min(90, len(raw_bytes) - 1), 2): - field_name = f"byte_{i:02d}" - raw_value = self.interpret_bytes(raw_bytes, i, 2, "signed_short") - if raw_value is not None: - packet[field_name] = self.normalize_orientation(raw_value, field_name) - - return packet - - def apply_dark_theme(self, fig, axes): - fig.patch.set_facecolor('#1e1e1e') - for ax in axes: - ax.set_facecolor('#2d2d2d') - - ax.title.set_color('white') - ax.xaxis.label.set_color('white') - ax.yaxis.label.set_color('white') - ax.tick_params(colors='white') - ax.tick_params(axis='x', colors='white') - ax.tick_params(axis='y', colors='white') - - ax.grid(True, color='#555555', alpha=0.3, linestyle='--') - - for spine in ax.spines.values(): - spine.set_color('#555555') - - legend = ax.get_legend() - if (legend): - legend.get_frame().set_facecolor('#2d2d2d') - legend.get_frame().set_alpha(0.7) - for text in legend.get_texts(): - text.set_color('white') - - def listen(self): - while True: - try: - data = self.sock.recv(1024) - formatted = self.format_hex(data) - timestamp = datetime.now().isoformat() - - is_valid = self.is_valid_tracking_packet(formatted) - - if not self.live_plotting: - if is_valid: - logger.info("%s - Response: %s...", timestamp, formatted[:60]) - else: - logger.info("%s - Skipped non-tracking packet.", timestamp) - - if is_valid: - if self.recording and self.log_file is not None: - self.log_file.write(formatted + "\n") - self.log_file.flush() - - try: - raw_bytes = self.parse_raw_packet(formatted) - packet = self.parse_packet_all_fields(raw_bytes) - - with self.data_lock: - self.live_data.append(packet) - if len(self.live_data) > 300: - self.live_data.pop(0) - - except Exception as e: - logger.error(f"Error parsing packet: {e}") - - except Exception as e: - logger.error("Error receiving data: %s", e) - break - - def load_log_file(self, filepath): - self.raw_packets = [] - self.parsed_packets = [] - try: - with open(filepath, 'r') as f: - for line in f: - line = line.strip() - if line: - try: - raw_bytes = self.parse_raw_packet(line) - self.raw_packets.append(raw_bytes) - packet = self.parse_packet_all_fields(raw_bytes) - - min_seq_num = min( - [parsed_packet["seq_num"] for parsed_packet in self.parsed_packets], default=0 - ) - - if packet["seq_num"] > min_seq_num: - self.parsed_packets.append(packet) - - except Exception as e: - logger.error(f"Error parsing line: {e}") - - logger.info(f"Loaded {len(self.parsed_packets)} packets from {filepath}") - return True - except Exception as e: - logger.error(f"Error loading log file: {e}") - return False - - def extract_field_values(self, field_name, data_source='loaded'): - if data_source == 'loaded': - data = self.parsed_packets - else: - with self.data_lock: - data = self.live_data.copy() - - values = [packet.get(field_name, 0) for packet in data if field_name in packet] - - if data_source == 'live' and len(values) > 5: - try: - values = np.array(values, dtype=float) - values = np.convolve(values, np.ones(5) / 5, mode='valid') - except Exception as e: - logger.warning(f"Smoothing error (non-critical): {e}") - - return values - - def is_valid_tracking_packet(self, hex_string): - standard_header = "04 00 04 00 17 00 00 00 10 00" - - if not hex_string.startswith(standard_header): - if self.live_plotting: - logger.warning("Invalid packet header: %s", hex_string[:30]) - return False - - if len(hex_string.split()) < 80: - if self.live_plotting: - logger.warning("Invalid packet length: %s", hex_string[:30]) - return False - - return True - - - def plot_fields(self, field_names=None): - if not self.parsed_packets: - logger.error("No data to plot. Load a log file first.") - return - - if field_names is None: - field_names = list(KEY_FIELDS.keys()) - - if not self.orientation_visualizer.calibration_complete: - if len(self.parsed_packets) < self.orientation_visualizer.calibration_sample_count: - logger.error("Not enough packets for calibration. Need at least 10 packets.") - return - for packet in self.parsed_packets[:self.orientation_visualizer.calibration_sample_count]: - self.orientation_visualizer.add_calibration_sample([ - packet.get('orientation 1', 0), - packet.get('orientation 2', 0), - packet.get('orientation 3', 0) - ]) - - if self.use_terminal: - self._plot_fields_terminal(field_names) - - else: - acceleration_fields = [f for f in field_names if 'acceleration' in f.lower()] - orientation_fields = [f for f in field_names if 'orientation' in f.lower()] - other_fields = [f for f in field_names if f not in acceleration_fields + orientation_fields] - - fig, axes = plt.subplots(3, 1, figsize=(14, 12), sharex=True) - self.apply_dark_theme(fig, axes) - - acceleration_colors = ['#FFFF00', '#00FFFF'] - orientation_colors = ['#FF00FF', '#00FF00', '#FFA500'] - other_colors = ['#52b788', '#f4a261', '#e76f51', '#2a9d8f'] - - if acceleration_fields: - for i, field in enumerate(acceleration_fields): - values = self.extract_field_values(field) - axes[0].plot(values, label=field, color=acceleration_colors[i % len(acceleration_colors)], linewidth=2) - axes[0].set_title("Acceleration Data", fontsize=14) - axes[0].legend() - - if orientation_fields: - for i, field in enumerate(orientation_fields): - values = self.extract_field_values(field) - axes[1].plot(values, label=field, color=orientation_colors[i % len(orientation_colors)], linewidth=2) - axes[1].set_title("Orientation Data", fontsize=14) - axes[1].legend() - - if other_fields: - for i, field in enumerate(other_fields): - values = self.extract_field_values(field) - axes[2].plot(values, label=field, color=other_colors[i % len(other_colors)], linewidth=2) - axes[2].set_title("Other Fields", fontsize=14) - axes[2].legend() - - plt.xlabel("Packet Index", fontsize=12) - plt.tight_layout() - plt.show() - - def _plot_fields_terminal(self, field_names): - """Internal method for terminal-based plotting""" - terminal_width = os.get_terminal_size().columns - plot_width = min(terminal_width - 10, 120) - plot_height = 15 - - acceleration_fields = [f for f in field_names if 'acceleration' in f.lower()] - orientation_fields = [f for f in field_names if 'orientation' in f.lower()] - other_fields = [f for f in field_names if f not in acceleration_fields + orientation_fields] - - def plot_group(fields, title): - if not fields: - return - - print(f"\n{title}") - print("=" * len(title)) - - for field in fields: - values = self.extract_field_values(field) - if len(values) > plot_width: - values = values[-plot_width:] - - if title == "Acceleration Data": - chart = acp.plot(values, {'height': plot_height}) - print(chart) - else: - chart = acp.plot(values, {'height': plot_height}) - print(chart) - - print(f"Min: {min(values):.2f}, Max: {max(values):.2f}, " + - f"Mean: {np.mean(values):.2f}") - print() - - plot_group(acceleration_fields, "Acceleration Data") - plot_group(orientation_fields, "Orientation Data") - plot_group(other_fields, "Other Fields") - - def create_braille_plot(self, values, width=80, height=20, y_label=True, fixed_y_min=None, fixed_y_max=None): - canvas = drawille.Canvas() - if fixed_y_min is None or fixed_y_max is None: - local_min, local_max = min(values), max(values) - else: - local_min, local_max = fixed_y_min, fixed_y_max - y_range = local_max - local_min or 1 - x_step = max(1, len(values) // width) - for i, v in enumerate(values[::x_step]): - y = int(((v - local_min) / y_range) * (height * 2 - 1)) - canvas.set(i, y) - frame = canvas.frame() - if y_label: - lines = frame.split('\n') - labeled_lines = [] - for idx, line in enumerate(lines): - if idx == 0: - labeled_lines.append(f"{local_max:6.0f} {line}") - elif idx == len(lines)-1: - labeled_lines.append(f"{local_min:6.0f} {line}") - else: - labeled_lines.append(" " + line) - frame = "\n".join(labeled_lines) - return frame - - def _start_live_plotting_terminal(self, record_data=False, duration=None): - import sys, select, tty, termios - old_settings = termios.tcgetattr(sys.stdin) - tty.setcbreak(sys.stdin.fileno()) - console = Console() - term_width = console.width - plot_width = round(min(term_width / 2 - 15, 120)) - ori_height = 10 - - def make_compact_layout(): - layout = Layout() - layout.split_column( - Layout(name="header", size=3), - Layout(name="main", ratio=1), - ) - layout["main"].split_row( - Layout(name="accelerations", ratio=1), - Layout(name="orientations", ratio=1) - ) - layout["accelerations"].split_column( - Layout(name="vertical", ratio=1), - Layout(name="horizontal", ratio=1) - ) - layout["orientations"].split_column( - Layout(name="face", ratio=1), - Layout(name="raw", ratio=1) - ) - return layout - - layout = make_compact_layout() - - try: - import time - with Live(layout, refresh_per_second=20, screen=True) as live: - while True: - if sys.stdin in select.select([sys.stdin], [], [], 0)[0]: - ch = sys.stdin.read(1) - if ch == 'p': - self.paused = not self.paused - logger.info("Paused" if self.paused else "Resumed") - if self.paused: - time.sleep(0.1) - rec_str = " [red][REC][/red]" if record_data else "" - left = "AirPods Head Tracking - v1.0.0" - right = "Ctrl+C - Close | p - Pause" + rec_str - status = "[bold red]Paused[/bold red]" - header = list(" " * term_width) - header[0:len(left)] = list(left) - header[term_width - len(right):] = list(right) - start = (term_width - len(status)) // 2 - header[start:start+len(status)] = list(status) - header_text = "".join(header) - layout["header"].update(Panel(header_text, style="bold white on black")) - continue - - with self.data_lock: - if len(self.live_data) < 1: - continue - latest = self.live_data[-1] - data = self.live_data[-plot_width:] - - if not self.orientation_visualizer.calibration_complete: - sample = [ - latest.get('orientation 1', 0), - latest.get('orientation 2', 0), - latest.get('orientation 3', 0) - ] - self.orientation_visualizer.add_calibration_sample(sample) - time.sleep(0.05) - rec_str = " [red][REC][/red]" if record_data else "" - - left = "AirPods Head Tracking - v1.0.0" - status = "[bold yellow]Calibrating...[/bold yellow]" - right = "Ctrl+C - Close | p - Pause" - remaining = max(term_width - len(left) - len(right), 0) - header_text = f"{left}{status.center(remaining)}{right}{rec_str}" - layout["header"].update(Panel(header_text, style="bold white on black")) - live.refresh() - continue - - o1 = latest.get('orientation 1', 0) - o2 = latest.get('orientation 2', 0) - o3 = latest.get('orientation 3', 0) - orientation = self.orientation_visualizer.calculate_orientation(o1, o2, o3) - pitch = orientation['pitch'] - yaw = orientation['yaw'] - - h_accel = [p.get('Horizontal Acceleration', 0) for p in data] - v_accel = [p.get('Vertical Acceleration', 0) for p in data] - if len(h_accel) > plot_width: - h_accel = h_accel[-plot_width:] - if len(v_accel) > plot_width: - v_accel = v_accel[-plot_width:] - global_min = min(min(v_accel), min(h_accel)) - global_max = max(max(v_accel), max(h_accel)) - config_acc = {'height': 20, 'min': global_min, 'max': global_max} - vert_plot = acp.plot(v_accel, config_acc) - horiz_plot = acp.plot(h_accel, config_acc) - - rec_str = " [red][REC][/red]" if record_data else "" - left = "AirPods Head Tracking - v1.0.0" - right = "Ctrl+C - Close | p - Pause" + rec_str - status = "[bold green]Live[/bold green]" - header = list(" " * term_width) - header[0:len(left)] = list(left) - header[term_width - len(right):] = list(right) - start = (term_width - len(status)) // 2 - header[start:start+len(status)] = list(status) - header_text = "".join(header) - layout["header"].update(Panel(header_text, style="bold white on black")) - - face_art = self.orientation_visualizer.create_face_art(pitch, yaw) - layout["accelerations"]["vertical"].update(Panel( - "[bold yellow]Vertical Acceleration[/]\n" + - vert_plot + "\n" + - f"Cur: {v_accel[-1]:6.1f} | Min: {min(v_accel):6.1f} | Max: {max(v_accel):6.1f}", - style="yellow" - )) - layout["accelerations"]["horizontal"].update(Panel( - "[bold cyan]Horizontal Acceleration[/]\n" + - horiz_plot + "\n" + - f"Cur: {h_accel[-1]:6.1f} | Min: {min(h_accel):6.1f} | Max: {max(h_accel):6.1f}", - style="cyan" - )) - layout["orientations"]["face"].update(Panel(face_art, title="[green]Orientation - Visualization[/]", style="green")) - - o2_values = [p.get('orientation 2', 0) for p in data[-plot_width:]] - o3_values = [p.get('orientation 3', 0) for p in data[-plot_width:]] - o2_values = o2_values[:plot_width] - o3_values = o3_values[:plot_width] - common_min = min(min(o2_values), min(o3_values)) - common_max = max(max(o2_values), max(o3_values)) - config_ori = {'height': ori_height, 'min': common_min, 'max': common_max, 'format': "{:6.0f}"} - chart_o2 = acp.plot(o2_values, config_ori) - chart_o3 = acp.plot(o3_values, config_ori) - layout["orientations"]["raw"].update(Panel( - "[bold yellow]Orientation 1:[/]\n" + chart_o2 + "\n" + - f"Cur: {o2_values[-1]:6.1f} | Min: {min(o2_values):6.1f} | Max: {max(o2_values):6.1f}\n\n" + - "[bold green]Orientation 2:[/]\n" + chart_o3 + "\n" + - f"Cur: {o3_values[-1]:6.1f} | Min: {min(o3_values):6.1f} | Max: {max(o3_values):6.1f}", - title="[cyan]Orientation Raw[/]", style="yellow" - )) - live.refresh() - time.sleep(0.05) - except KeyboardInterrupt: - logger.info("\nStopped.") - if record_data: - self.stop_tracking() - else: - if self.sock: - self.sock.send(bytes.fromhex(STOP_CMD)) - finally: - termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings) - - def _start_live_plotting(self, record_data=False, duration=None): - terminal_width = os.get_terminal_size().columns - plot_width = min(terminal_width - 10, 80) - plot_height = 10 - - try: - while True: - os.system('clear' if os.name == 'posix' else 'cls') - with self.data_lock: - if len(self.live_data) == 0: - print("\nWaiting for data...") - time.sleep(0.1) - continue - - data = self.live_data[-plot_width:] - - acceleration_fields = [f for f in KEY_FIELDS.keys() if 'acceleration' in f.lower()] - orientation_fields = [f for f in KEY_FIELDS.keys() if 'orientation' in f.lower()] - other_fields = [f for f in KEY_FIELDS.keys() if f not in acceleration_fields + orientation_fields] - - def plot_group(fields, title): - if not fields: - return - - print(f"\n{title}") - print("=" * len(title)) - - for field in fields: - values = [packet.get(field, 0) for packet in data if field in packet] - if len(values) > 0: - chart = acp.plot(values, {'height': plot_height}) - print(chart) - print(f"Current: {values[-1]:.2f}, " + - f"Min: {min(values):.2f}, Max: {max(values):.2f}") - print() - - plot_group(acceleration_fields, "Acceleration Data") - plot_group(orientation_fields, "Orientation Data") - plot_group(other_fields, "Other Fields") - - print("\nPress Ctrl+C to stop plotting") - time.sleep(0.1) - - except KeyboardInterrupt: - logger.info("\nLive plotting stopped.") - self.sock.send(bytes.fromhex(STOP_CMD)) - if record_data: - self.stop_tracking() - self.live_plotting = False - - def start_live_plotting(self, record_data=False, duration=None): - if self.sock is None: - if not self.connect(): - logger.error("Could not connect to AirPods. Live plotting aborted.") - return - if not self.recording and record_data: - self.start_tracking(duration) - logger.info("Recording enabled during live plotting") - elif not self.recording: - self.sock.send(bytes.fromhex(START_CMD)) - logger.info("Head tracking started (not recording to file)") - with self.data_lock: - self.live_data = [] - self.live_plotting = True - self.paused = False - if self.use_terminal: - self._start_live_plotting_terminal(record_data, duration) - else: - from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec - fig = plt.figure(figsize=(14, 6)) - gs = GridSpec(1, 2, width_ratios=[1, 1]) - ax_accel = fig.add_subplot(gs[0]) - subgs = GridSpecFromSubplotSpec(2, 1, subplot_spec=gs[1], height_ratios=[2, 1]) - ax_head_top = fig.add_subplot(subgs[0], projection='3d') - ax_ori = fig.add_subplot(subgs[1]) - - ax_accel.set_title("Acceleration Data") - ax_accel.set_xlabel("Packet Index") - ax_accel.set_ylabel("Acceleration") - ax_accel.legend(loc='upper right', framealpha=0.7) - fig.patch.set_facecolor('#1e1e1e') - ax_accel.set_facecolor('#2d2d2d') - self.apply_dark_theme(fig, [ax_accel, ax_head_top, ax_ori]) - plt.ion() - - def update_plot(_): - with self.data_lock: - data = self.live_data.copy() - if len(data) == 0: - return - - latest = data[-1] - - if not self.orientation_visualizer.calibration_complete: - sample = [ - latest.get('orientation 1', 0), - latest.get('orientation 2', 0), - latest.get('orientation 3', 0) - ] - self.orientation_visualizer.add_calibration_sample(sample) - ax_head_top.cla() - ax_head_top.text(0.5, 0.5, "Calibrating... please wait", horizontalalignment='center', verticalalignment='center', transform=ax_head_top.transAxes, color='white') - fig.canvas.draw_idle() - return - - h_accel = [p.get('Horizontal Acceleration', 0) for p in data] - v_accel = [p.get('Vertical Acceleration', 0) for p in data] - x_vals = list(range(len(h_accel))) - ax_accel.cla() - ax_accel.plot(x_vals, v_accel, label='Vertical Acceleration', color='#FFFF00', linewidth=2) - ax_accel.plot(x_vals, h_accel, label='Horizontal Acceleration', color='#00FFFF', linewidth=2) - ax_accel.set_title("Acceleration Data") - ax_accel.set_xlabel("Packet Index") - ax_accel.set_ylabel("Acceleration") - ax_accel.legend(loc='upper right', framealpha=0.7) - ax_accel.set_facecolor('#2d2d2d') - ax_accel.title.set_color('white') - ax_accel.xaxis.label.set_color('white') - ax_accel.yaxis.label.set_color('white') - - latest = data[-1] - o1 = latest.get('orientation 1', 0) - o2 = latest.get('orientation 2', 0) - o3 = latest.get('orientation 3', 0) - orientation = self.orientation_visualizer.calculate_orientation(o1, o2, o3) - pitch = orientation['pitch'] - yaw = orientation['yaw'] - - ax_head_top.cla() - ax_head_top.set_title("Head Orientation") - ax_head_top.set_xlim([-1, 1]) - ax_head_top.set_ylim([-1, 1]) - ax_head_top.set_zlim([-1, 1]) - ax_head_top.set_facecolor('#2d2d2d') - pitch_rad = np.radians(pitch) - yaw_rad = np.radians(yaw) - Rz = np.array([ - [np.cos(yaw_rad), np.sin(yaw_rad), 0], - [-np.sin(yaw_rad), np.cos(yaw_rad), 0], - [0, 0, 1] - ]) - Ry = np.array([ - [np.cos(pitch_rad), 0, np.sin(pitch_rad)], - [0, 1, 0], - [-np.sin(pitch_rad), 0, np.cos(pitch_rad)] - ]) - R = Rz @ Ry - dir_vec = R @ np.array([1, 0, 0]) - ax_head_top.quiver(0, 0, 0, dir_vec[0], dir_vec[1], dir_vec[2], - color='r', length=0.8, linewidth=3) - - ax_ori.cla() - o2_values = [p.get('orientation 2', 0) for p in data] - o3_values = [p.get('orientation 3', 0) for p in data] - x_range = list(range(len(o2_values))) - ax_ori.plot(x_range, o2_values, label='Orientation 1', color='red', linewidth=2) - ax_ori.plot(x_range, o3_values, label='Orientation 2', color='green', linewidth=2) - ax_ori.set_facecolor('#2d2d2d') - ax_ori.tick_params(colors='white') - ax_ori.set_title("Orientation Raw") - ax_ori.legend(facecolor='#2d2d2d', edgecolor='#555555', - labelcolor='white', loc='upper right') - ax_ori.text(0.95, 0.9, f"Pitch: {pitch:.1f}°\nYaw: {yaw:.1f}°", - transform=ax_ori.transAxes, color='white', - ha='right', va='top', bbox=dict(facecolor='#2d2d2d', alpha=0.5)) - fig.canvas.draw_idle() - self.animation = FuncAnimation( - fig, update_plot, - interval=20, - blit=False, - cache_frame_data=False - ) - plt.show(block=True) - self.sock.send(bytes.fromhex(STOP_CMD)) - logger.info("Stopping head tracking AirPods.") - if self.recording and record_data: - self.stop_tracking() - logger.info("Recording stopped after sending close command") - else: - logger.info("Live plotting ended (no recording to stop).") - self.live_plotting = False - self.animation = None - plt.ioff() - - def interactive_mode(self): - from prompt_toolkit import PromptSession - session = PromptSession("> ") - logger.info("\nAirPods Head Tracking Analyzer") - print("------------------------------") - logger.info("Commands:") - print(" connect - connect to your AirPods") - print(" start [seconds] - start recording head tracking data, optionally for specified duration") - print(" stop - stop recording") - print(" load - load and parse a log file") - print(" plot - plot all sensor data fields") - print(" live [seconds] - start live plotting (without recording), optionally stop recording after seconds") - print(" liver [seconds] - start live plotting with recording, optionally stop recording after seconds") - print(" gestures - start gesture detection") - print(" quit - exit the program") - - while True: - try: - cmd_input = session.prompt("> ") - cmd_parts = cmd_input.strip().split() - if not cmd_parts: - continue - cmd = cmd_parts[0].lower() - if cmd == "connect": - self.connect() - elif cmd == "start": - duration = float(cmd_parts[1]) if len(cmd_parts) > 1 else None - self.start_tracking(duration) - elif cmd == "stop": - self.stop_tracking() - elif cmd == "load" and len(cmd_parts) > 1: - self.load_log_file(cmd_parts[1]) - elif cmd == "plot": - self.plot_fields() - elif cmd == "live": - duration = float(cmd_parts[1]) if len(cmd_parts) > 1 else None - logger.info("Starting live plotting mode (without recording)%s.", - f" for {duration} seconds" if duration else "") - self.start_live_plotting(record_data=False, duration=duration) - elif cmd == "liver": - duration = float(cmd_parts[1]) if len(cmd_parts) > 1 else None - logger.info("Starting live plotting mode WITH recording%s.", - f" for {duration} seconds" if duration else "") - self.start_live_plotting(record_data=True, duration=duration) - elif cmd == "gestures": - from gestures import GestureDetector - if self.conn is not None: - detector = GestureDetector(conn=self.conn) - else: - detector = GestureDetector() - detector.start_detection() - elif cmd == "quit": - logger.info("Exiting.") - if self.conn != None: - self.conn.disconnect() - break - elif cmd == "help": - logger.info("\nAirPods Head Tracking Analyzer") - logger.info("------------------------------") - logger.info("Commands:") - logger.info(" connect - connect to your AirPods") - logger.info(" start [seconds] - start recording head tracking data, optionally for specified duration") - logger.info(" stop - stop recording") - logger.info(" load - load and parse a log file") - logger.info(" plot - plot all sensor data fields") - logger.info(" live [seconds] - start live plotting (without recording), optionally stop recording after seconds") - logger.info(" liver [seconds] - start live plotting with recording, optionally stop recording after seconds") - logger.info(" gestures - start gesture detection") - logger.info(" quit - exit the program") - else: - logger.info("Unknown command. Type 'help' to see available commands.") - except KeyboardInterrupt: - logger.info("Use 'quit' to exit.") - except EOFError: - logger.info("Exiting.") - if self.conn != None: - self.conn.disconnect() - break - -if __name__ == "__main__": - import sys - tracker = AirPodsTracker() - tracker.interactive_mode() \ No newline at end of file diff --git a/head-tracking/requirements.txt b/head-tracking/requirements.txt deleted file mode 100644 index 0a66bdc0e..000000000 --- a/head-tracking/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -drawille -numpy -pybluez -matplotlib -asciichartpy -rich \ No newline at end of file diff --git a/imgs/banner-dark.png b/imgs/banner-dark.png new file mode 100644 index 000000000..234b58fbe Binary files /dev/null and b/imgs/banner-dark.png differ diff --git a/linux-rust/assets/devices/pod.png b/linux-rust/assets/devices/pod.png new file mode 100644 index 000000000..64a45b267 Binary files /dev/null and b/linux-rust/assets/devices/pod.png differ diff --git a/linux-rust/assets/devices/pod3.png b/linux-rust/assets/devices/pod3.png new file mode 100644 index 000000000..5f6d6779c Binary files /dev/null and b/linux-rust/assets/devices/pod3.png differ diff --git a/linux-rust/assets/devices/pod3_case.png b/linux-rust/assets/devices/pod3_case.png new file mode 100644 index 000000000..c6d0c5437 Binary files /dev/null and b/linux-rust/assets/devices/pod3_case.png differ diff --git a/linux-rust/assets/devices/pod4_case.png b/linux-rust/assets/devices/pod4_case.png new file mode 100644 index 000000000..117dc7aa6 Binary files /dev/null and b/linux-rust/assets/devices/pod4_case.png differ diff --git a/linux-rust/assets/devices/pod_case.png b/linux-rust/assets/devices/pod_case.png new file mode 100644 index 000000000..737afc30a Binary files /dev/null and b/linux-rust/assets/devices/pod_case.png differ diff --git a/linux-rust/assets/devices/podmax.png b/linux-rust/assets/devices/podmax.png new file mode 100644 index 000000000..499e50742 Binary files /dev/null and b/linux-rust/assets/devices/podmax.png differ diff --git a/linux-rust/assets/devices/podpro.png b/linux-rust/assets/devices/podpro.png new file mode 100644 index 000000000..bbec8bc91 Binary files /dev/null and b/linux-rust/assets/devices/podpro.png differ diff --git a/linux-rust/assets/devices/podpro_case.png b/linux-rust/assets/devices/podpro_case.png new file mode 100644 index 000000000..9118a676c Binary files /dev/null and b/linux-rust/assets/devices/podpro_case.png differ diff --git a/linux-rust/assets/icons/adaptive.png b/linux-rust/assets/icons/adaptive.png new file mode 100644 index 000000000..b0863357c Binary files /dev/null and b/linux-rust/assets/icons/adaptive.png differ diff --git a/linux-rust/assets/icons/noise_cancellation.png b/linux-rust/assets/icons/noise_cancellation.png new file mode 100644 index 000000000..0b4ec6e88 Binary files /dev/null and b/linux-rust/assets/icons/noise_cancellation.png differ diff --git a/linux-rust/assets/icons/transparency.png b/linux-rust/assets/icons/transparency.png new file mode 100644 index 000000000..b03b74cbf Binary files /dev/null and b/linux-rust/assets/icons/transparency.png differ diff --git a/linux-rust/src/devices/airpods.rs b/linux-rust/src/devices/airpods.rs index f0e876cf0..b6845a2aa 100644 --- a/linux-rust/src/devices/airpods.rs +++ b/linux-rust/src/devices/airpods.rs @@ -412,3 +412,24 @@ pub struct AirPodsInformation { pub version3: String, pub le_keys: AirPodsLEKeys, } + +impl AirPodsInformation { + /// Returns a friendly model name for the raw model number, if known. + /// Mapping ported from linux/enums.h parseModelNumber(). + /// Source: https://support.apple.com/en-us/109525 + pub fn friendly_model_name(&self) -> Option<&'static str> { + match self.model_number.as_str() { + "A1523" | "A1722" => Some("AirPods 1st Gen"), + "A2032" | "A2031" => Some("AirPods 2nd Gen"), + "A2564" | "A2565" => Some("AirPods 3rd Gen"), + "A3053" | "A3050" | "A3054" => Some("AirPods 4"), + "A3055" | "A3056" | "A3057" => Some("AirPods 4 ANC"), + "A2083" | "A2084" => Some("AirPods Pro"), + "A2698" | "A2699" | "A2931" => Some("AirPods Pro 2"), + "A3047" | "A3048" | "A3049" => Some("AirPods Pro 2 USB-C"), + "A2096" => Some("AirPods Max"), + "A3184" => Some("AirPods Max USB-C"), + _ => None, + } + } +} diff --git a/linux-rust/src/devices/enums.rs b/linux-rust/src/devices/enums.rs index 5768d1802..56fd27741 100644 --- a/linux-rust/src/devices/enums.rs +++ b/linux-rust/src/devices/enums.rs @@ -52,15 +52,15 @@ impl Display for DeviceState { #[derive(Clone, Debug)] pub struct AirPodsState { pub device_name: String, + pub model: AirPodsModel, pub noise_control_mode: AirPodsNoiseControlMode, - pub noise_control_state: combo_box::State, pub conversation_awareness_enabled: bool, pub personalized_volume_enabled: bool, pub allow_off_mode: bool, pub battery: Vec, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub enum AirPodsNoiseControlMode { Off, NoiseCancellation, @@ -80,13 +80,13 @@ impl Display for AirPodsNoiseControlMode { } impl AirPodsNoiseControlMode { - pub fn from_byte(value: &u8) -> Self { + pub fn from_byte(value: &u8) -> Option { match value { - 0x01 => AirPodsNoiseControlMode::Off, - 0x02 => AirPodsNoiseControlMode::NoiseCancellation, - 0x03 => AirPodsNoiseControlMode::Transparency, - 0x04 => AirPodsNoiseControlMode::Adaptive, - _ => AirPodsNoiseControlMode::Off, + 0x01 => Some(AirPodsNoiseControlMode::Off), + 0x02 => Some(AirPodsNoiseControlMode::NoiseCancellation), + 0x03 => Some(AirPodsNoiseControlMode::Transparency), + 0x04 => Some(AirPodsNoiseControlMode::Adaptive), + _ => None, } } pub fn to_byte(&self) -> u8 { @@ -99,6 +99,69 @@ impl AirPodsNoiseControlMode { } } +/// AirPods hardware model, used for selecting correct device artwork. +/// Mapping ported from linux/enums.h. +#[derive(Clone, Debug, PartialEq)] +pub enum AirPodsModel { + Unknown, + AirPods1, + AirPods2, + AirPods3, + AirPods4, + AirPods4ANC, + AirPodsPro, + AirPodsPro2Lightning, + AirPodsPro2USBC, + AirPodsMaxLightning, + AirPodsMaxUSBC, +} + +impl AirPodsModel { + /// Parse a raw model number string into an AirPodsModel. + /// Source: https://support.apple.com/en-us/109525 + pub fn from_model_number(model_number: &str) -> Self { + match model_number { + "A1523" | "A1722" => AirPodsModel::AirPods1, + "A2032" | "A2031" => AirPodsModel::AirPods2, + "A2564" | "A2565" => AirPodsModel::AirPods3, + "A3053" | "A3050" | "A3054" => AirPodsModel::AirPods4, + "A3055" | "A3056" | "A3057" => AirPodsModel::AirPods4ANC, + "A2083" | "A2084" => AirPodsModel::AirPodsPro, + "A2698" | "A2699" | "A2931" => AirPodsModel::AirPodsPro2Lightning, + "A3047" | "A3048" | "A3049" => AirPodsModel::AirPodsPro2USBC, + "A2096" => AirPodsModel::AirPodsMaxLightning, + "A3184" => AirPodsModel::AirPodsMaxUSBC, + _ => AirPodsModel::Unknown, + } + } + + /// Returns (bud_image_filename, case_image_filename) for this model. + /// Images are in `assets/devices/`. + pub fn device_images(&self) -> (&'static str, &'static str) { + match self { + AirPodsModel::AirPods1 | AirPodsModel::AirPods2 => ("pod.png", "pod_case.png"), + AirPodsModel::AirPods3 => ("pod3.png", "pod3_case.png"), + AirPodsModel::AirPods4 | AirPodsModel::AirPods4ANC => ("pod3.png", "pod4_case.png"), + AirPodsModel::AirPodsPro + | AirPodsModel::AirPodsPro2Lightning + | AirPodsModel::AirPodsPro2USBC => ("podpro.png", "podpro_case.png"), + AirPodsModel::AirPodsMaxLightning | AirPodsModel::AirPodsMaxUSBC => { + ("podmax.png", "podmax.png") // Max has no separate case image + } + AirPodsModel::Unknown => ("pod.png", "pod_case.png"), + } + } + + /// Whether this model is an over-ear headphone (AirPods Max) + /// vs in-ear earbuds. Affects battery layout (single vs L/R/Case). + pub fn is_over_ear(&self) -> bool { + matches!( + self, + AirPodsModel::AirPodsMaxLightning | AirPodsModel::AirPodsMaxUSBC + ) + } +} + #[derive(Clone, Debug)] pub struct NothingState { pub anc_mode: NothingAncMode, diff --git a/linux-rust/src/ui/airpods.rs b/linux-rust/src/ui/airpods.rs index 9335b5e05..24455ae15 100644 --- a/linux-rust/src/ui/airpods.rs +++ b/linux-rust/src/ui/airpods.rs @@ -1,11 +1,11 @@ -use crate::bluetooth::aacp::{AACPManager, ControlCommandIdentifiers}; +use crate::bluetooth::aacp::{AACPManager, BatteryComponent, BatteryStatus, ControlCommandIdentifiers}; use iced::Alignment::End; use iced::border::Radius; -use iced::overlay::menu; use iced::widget::button::Style; +use iced::widget::image; use iced::widget::rule::FillMode; use iced::widget::{ - Space, button, column, combo_box, container, row, rule, text, text_input, toggler, + Space, button, column, container, row, rule, scrollable, text, text_input, toggler, }; use iced::{Background, Border, Center, Color, Length, Padding, Theme}; use log::error; @@ -14,14 +14,335 @@ use std::sync::Arc; use std::thread; use tokio::runtime::Runtime; // use crate::bluetooth::att::ATTManager; -use crate::devices::enums::{AirPodsState, DeviceData, DeviceInformation, DeviceState}; +use crate::devices::enums::{ + AirPodsNoiseControlMode, AirPodsState, DeviceData, DeviceInformation, DeviceState, +}; use crate::ui::window::Message; +// Embed the listening mode icons at compile time from the Android assets +const ICON_NOISE_CANCELLATION: &[u8] = + include_bytes!("../../assets/icons/noise_cancellation.png"); +const ICON_TRANSPARENCY: &[u8] = include_bytes!("../../assets/icons/transparency.png"); +const ICON_ADAPTIVE: &[u8] = include_bytes!("../../assets/icons/adaptive.png"); + +// Embed device images at compile time from the legacy Qt assets +const IMG_POD: &[u8] = include_bytes!("../../assets/devices/pod.png"); +const IMG_POD_CASE: &[u8] = include_bytes!("../../assets/devices/pod_case.png"); +const IMG_POD3: &[u8] = include_bytes!("../../assets/devices/pod3.png"); +const IMG_POD3_CASE: &[u8] = include_bytes!("../../assets/devices/pod3_case.png"); +const IMG_POD4_CASE: &[u8] = include_bytes!("../../assets/devices/pod4_case.png"); +const IMG_PODPRO: &[u8] = include_bytes!("../../assets/devices/podpro.png"); +const IMG_PODPRO_CASE: &[u8] = include_bytes!("../../assets/devices/podpro_case.png"); +const IMG_PODMAX: &[u8] = include_bytes!("../../assets/devices/podmax.png"); + +/// Get the embedded bytes for a device image filename. +fn device_image_bytes(filename: &str) -> &'static [u8] { + match filename { + "pod.png" => IMG_POD, + "pod_case.png" => IMG_POD_CASE, + "pod3.png" => IMG_POD3, + "pod3_case.png" => IMG_POD3_CASE, + "pod4_case.png" => IMG_POD4_CASE, + "podpro.png" => IMG_PODPRO, + "podpro_case.png" => IMG_PODPRO_CASE, + "podmax.png" => IMG_PODMAX, + _ => IMG_POD, + } +} + +/// Battery level color: green >50%, yellow 20-50%, red <20%. +fn battery_color(level: u8) -> Color { + if level <= 20 { + Color::from_rgb(1.0, 0.27, 0.23) // #FF453A — red + } else if level <= 50 { + Color::from_rgb(1.0, 0.84, 0.04) // #FFD60A — yellow + } else { + Color::from_rgb(0.19, 0.82, 0.35) // #30D158 — green + } +} + +/// Build a single battery column: device image + battery bar + percentage. +fn battery_column<'a>( + img_bytes: &'static [u8], + label: &'a str, + level: Option, + status: Option, + img_width: f32, +) -> iced::Element<'a, Message> { + let is_disconnected = status == Some(BatteryStatus::Disconnected); + let is_charging = status == Some(BatteryStatus::Charging); + let opacity = if is_disconnected { 0.35 } else { 1.0 }; + + let device_img = container( + image(image::Handle::from_bytes(img_bytes)) + .width(Length::Fixed(img_width)) + ) + .center_x(Length::Fill); + + // Bar width and height constants + let bar_total_width = 80.0_f32; + let bar_height = 8.0_f32; + + let bar_and_text: iced::Element<'a, Message> = if is_disconnected { + // Disconnected: show empty dimmed bar + label with "–" + column![ + // Empty dimmed battery bar + container(Space::new()) + .width(Length::Fixed(bar_total_width)) + .height(bar_height) + .style(move |theme: &Theme| { + let mut s = container::Style::default(); + s.background = Some(Background::Color( + theme.palette().text.scale_alpha(0.08), + )); + s.border = Border::default().rounded(4); + s + }), + // Label + "–" + row![ + text(label).size(12).style(move |theme: &Theme| { + let mut s = text::Style::default(); + s.color = Some(theme.palette().text.scale_alpha(0.35)); + s + }), + Space::new().width(Length::Fill), + text("–").size(14).style(move |theme: &Theme| { + let mut s = text::Style::default(); + s.color = Some(theme.palette().text.scale_alpha(0.35)); + s + }) + ] + .width(Length::Fixed(bar_total_width)) + .align_y(Center) + ] + .spacing(4) + .align_x(iced::Alignment::Center) + .into() + } else if let Some(lvl) = level { + let color = battery_color(lvl); + let bar_fill = (lvl as f32 / 100.0).clamp(0.02, 1.0); + + let charging_indicator: iced::Element<'a, Message> = if is_charging { + text(" ⚡").size(12).style(move |_theme: &Theme| { + let mut s = text::Style::default(); + s.color = Some(Color::from_rgb(0.19, 0.82, 0.35)); + s + }).into() + } else { + Space::new().into() + }; + + column![ + // Battery bar + container( + container(Space::new()) + .width(Length::FillPortion((bar_fill * 100.0) as u16)) + .height(bar_height) + .style(move |_theme: &Theme| { + let mut s = container::Style::default(); + s.background = Some(Background::Color(color)); + s.border = Border::default().rounded(4); + s + }) + ) + .width(Length::Fixed(bar_total_width)) + .height(bar_height) + .style(move |theme: &Theme| { + let mut s = container::Style::default(); + s.background = Some(Background::Color( + theme.palette().text.scale_alpha(0.1), + )); + s.border = Border::default().rounded(4); + s + }), + // Label + percentage + row![ + text(label).size(12).style(move |theme: &Theme| { + let mut s = text::Style::default(); + s.color = Some(theme.palette().text.scale_alpha(0.55)); + s + }), + Space::new().width(Length::Fill), + text(format!("{}%", lvl)).size(14), + charging_indicator + ] + .width(Length::Fixed(bar_total_width)) + .align_y(Center) + ] + .spacing(4) + .align_x(iced::Alignment::Center) + .into() + } else { + Space::new().into() + }; + + container( + column![ + device_img, + Space::new().height(8), + bar_and_text + ] + .align_x(iced::Alignment::Center) + ) + .style(move |_theme: &Theme| { + let mut s = container::Style::default(); + s.text_color = Some(Color::WHITE.scale_alpha(opacity)); + s + }) + .center_x(Length::Fill) + .into() +} + +/// Build the full battery view section with device images and battery indicators. +fn battery_view<'a>(state: &'a AirPodsState) -> iced::Element<'a, Message> { + let (bud_img_name, case_img_name) = state.model.device_images(); + let bud_bytes = device_image_bytes(bud_img_name); + let case_bytes = device_image_bytes(case_img_name); + + let battery = &state.battery; + + // Check for headphone-only (AirPods Max) + let headphone = battery.iter().find(|b| b.component == BatteryComponent::Headphone); + + if state.model.is_over_ear() || headphone.is_some() { + // AirPods Max: single headphone display + let hp = headphone; + let level = hp.map(|b| b.level); + let status = hp.map(|b| b.status); + + container( + battery_column(bud_bytes, "", level, status, 80.0) + ) + .center_x(Length::Fill) + .padding(Padding { + top: 12.0, + bottom: 12.0, + left: 20.0, + right: 20.0, + }) + .into() + } else { + // Earbuds: L + R + Case + let left = battery.iter().find(|b| b.component == BatteryComponent::Left); + let right = battery.iter().find(|b| b.component == BatteryComponent::Right); + let case = battery.iter().find(|b| b.component == BatteryComponent::Case); + + let left_level = left.map(|b| b.level); + let left_status = left.map(|b| b.status); + let right_level = right.map(|b| b.level); + let right_status = right.map(|b| b.status); + let case_level = case.map(|b| b.level); + let case_status = case.map(|b| b.status); + + container( + row![ + battery_column(bud_bytes, "L", left_level, left_status, 48.0), + battery_column(bud_bytes, "R", right_level, right_status, 48.0), + battery_column(case_bytes, "Case", case_level, case_status, 60.0) + ] + .spacing(24) + .align_y(iced::Alignment::End) + ) + .center_x(Length::Fill) + .padding(Padding { + top: 12.0, + bottom: 12.0, + left: 20.0, + right: 20.0, + }) + .into() + } +} + +/// Build a single segmented button for a listening mode. +fn listening_mode_button<'a>( + mode: AirPodsNoiseControlMode, + is_selected: bool, + icon_bytes: Option<&'static [u8]>, + label: &'a str, + mac: String, +) -> iced::Element<'a, Message> { + let icon_element: iced::Element<'a, Message> = if let Some(bytes) = icon_bytes { + image(image::Handle::from_bytes(bytes)) + .width(28) + .height(28) + .into() + } else { + // "Off" mode uses a unicode power symbol instead of a PNG icon + text("\u{23FB}") + .size(22) + .align_x(Center) + .style(move |theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(if is_selected { + theme.palette().primary + } else { + theme.palette().text.scale_alpha(0.6) + }); + style + }) + .into() + }; + + let label_text = text(label).size(11).align_x(Center).style( + move |theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(if is_selected { + theme.palette().primary + } else { + theme.palette().text.scale_alpha(0.7) + }); + style + }, + ); + + let content = column![icon_element, label_text] + .spacing(4) + .align_x(Center) + .width(Length::Fill); + + button(content) + .padding(Padding { + top: 10.0, + bottom: 8.0, + left: 4.0, + right: 4.0, + }) + .width(Length::Fill) + .style(move |theme: &Theme, _status| { + let mut style = Style::default(); + if is_selected { + style.background = + Some(Background::Color(theme.palette().primary.scale_alpha(0.15))); + style.border = Border { + width: 1.5, + color: theme.palette().primary.scale_alpha(0.5), + radius: Radius::from(12.0), + }; + } else { + style.background = Some(Background::Color(Color::TRANSPARENT)); + style.border = Border { + width: 1.0, + color: theme.palette().text.scale_alpha(0.1), + radius: Radius::from(12.0), + }; + } + style.text_color = theme.palette().text; + style + }) + // Only send a message — side effects (AACP command) are handled in update() + .on_press(Message::SetListeningMode(mac, mode)) + .into() +} + pub fn airpods_view<'a>( mac: &'a str, devices_list: &HashMap, state: &'a AirPodsState, aacp_manager: Arc, + show_serials: bool, + show_device_info: bool, + show_off_listening_mode: bool, // att_manager: Arc ) -> iced::widget::Container<'a, Message> { let mac = mac.to_string(); @@ -92,92 +413,84 @@ pub fn airpods_view<'a>( style }); + // --- Segmented listening mode control --- + let mut mode_buttons: Vec> = Vec::new(); + + // Conditionally include "Off" based on the app setting + if show_off_listening_mode { + mode_buttons.push(listening_mode_button( + AirPodsNoiseControlMode::Off, + state.noise_control_mode == AirPodsNoiseControlMode::Off, + None, + "Off", + mac.clone(), + )); + } + + mode_buttons.push(listening_mode_button( + AirPodsNoiseControlMode::NoiseCancellation, + state.noise_control_mode == AirPodsNoiseControlMode::NoiseCancellation, + Some(ICON_NOISE_CANCELLATION), + "Noise Cancellation", + mac.clone(), + )); + + mode_buttons.push(listening_mode_button( + AirPodsNoiseControlMode::Transparency, + state.noise_control_mode == AirPodsNoiseControlMode::Transparency, + Some(ICON_TRANSPARENCY), + "Transparency", + mac.clone(), + )); + + mode_buttons.push(listening_mode_button( + AirPodsNoiseControlMode::Adaptive, + state.noise_control_mode == AirPodsNoiseControlMode::Adaptive, + Some(ICON_ADAPTIVE), + "Adaptive", + mac.clone(), + )); + let listening_mode = container( - row![ - text("Listening Mode").size(16).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().text); - style - }), - Space::new().width(Length::Fill), - { - let state_clone = state.clone(); - let mac = mac.clone(); - // this combo_box doesn't go really well with the design, but I am not writing my own dropdown menu for this - combo_box( - &state.noise_control_state, - "Select Listening Mode", - Some(&state.noise_control_mode.clone()), - { - let aacp_manager = aacp_manager.clone(); - move |selected_mode| { - let aacp_manager = aacp_manager.clone(); - let selected_mode_c = selected_mode.clone(); - run_async_in_thread(async move { - aacp_manager - .send_control_command( - ControlCommandIdentifiers::ListeningMode, - &[selected_mode_c.to_byte()], - ) - .await - .expect("Failed to send Noise Control Mode command"); - }); - let mut state = state_clone.clone(); - state.noise_control_mode = selected_mode.clone(); - Message::StateChanged(mac.to_string(), DeviceState::AirPods(state)) - } - }, - ) - .width(Length::from(200)) - .input_style(|theme: &Theme, _status| text_input::Style { - background: Background::Color(theme.palette().primary.scale_alpha(0.2)), - border: Border { - width: 1.0, - color: theme.palette().text.scale_alpha(0.3), - radius: Radius::from(4.0), - }, - icon: Default::default(), - placeholder: theme.palette().text, - value: theme.palette().text, - selection: Default::default(), - }) - .padding(Padding { - top: 5.0, - bottom: 5.0, - left: 10.0, - right: 10.0, - }) - .menu_style(|theme: &Theme| menu::Style { - background: Background::Color(theme.palette().background), - border: Border { - width: 1.0, - color: theme.palette().text, - radius: Radius::from(4.0), - }, - text_color: theme.palette().text, - selected_text_color: theme.palette().text, - selected_background: Background::Color( - theme.palette().primary.scale_alpha(0.3), - ), - shadow: Default::default() + column![ + container( + text("Listening Mode").size(18).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().primary); + style }) - } + ) + .padding(Padding { + top: 0.0, + bottom: 4.0, + left: 4.0, + right: 4.0, + }), + container( + row(mode_buttons).spacing(6) + ) + .padding(Padding { + top: 4.0, + bottom: 4.0, + left: 4.0, + right: 4.0, + }) + .style(|theme: &Theme| { + let mut style = container::Style::default(); + style.background = + Some(Background::Color(theme.palette().primary.scale_alpha(0.05))); + let mut border = Border::default(); + border.color = theme.palette().primary.scale_alpha(0.3); + style.border = border.rounded(16); + style + }) ] - .align_y(Center), ) .padding(Padding { top: 5.0, bottom: 5.0, - left: 18.0, - right: 18.0, - }) - .style(|theme: &Theme| { - let mut style = container::Style::default(); - style.background = Some(Background::Color(theme.palette().primary.scale_alpha(0.1))); - let mut border = Border::default(); - border.color = theme.palette().primary.scale_alpha(0.5); - style.border = border.rounded(16); - style + left: 14.0, + right: 14.0, }); let mac_audio = mac.clone(); @@ -309,196 +622,181 @@ pub fn airpods_view<'a>( ) ]; - let off_listening_mode_toggle = { - let aacp_manager_olm = aacp_manager.clone(); - let mac = mac.clone(); - container(row![ - column![ - text("Off Listening Mode").size(16), - text("When this is on, AirPods listening modes will include an Off option. Loud sound levels are not reduced when listening mode is set to Off.").size(12).style( - |theme: &Theme| { + let mut information_col = column![]; + if let Some(device) = devices_list.get(mac_information.as_str()) { + if let Some(DeviceInformation::AirPods(ref airpods_info)) = device.information { + let chevron = if show_device_info { "\u{25be}" } else { "\u{25b8}" }; + let header = button( + row![ + text(format!("{} Device Information", chevron)).size(18).style(|theme: &Theme| { let mut style = text::Style::default(); - style.color = Some(theme.palette().text.scale_alpha(0.7)); + style.color = Some(theme.palette().primary); style - } - ).width(Length::Fill) - ].width(Length::Fill), - toggler(state.allow_off_mode) - .on_toggle(move |is_enabled| { - let aacp_manager = aacp_manager_olm.clone(); - run_async_in_thread( - async move { - aacp_manager.send_control_command( - ControlCommandIdentifiers::AllowOffOption, - if is_enabled { &[0x01] } else { &[0x02] } - ).await.expect("Failed to send Off Listening Mode command"); - } - ); - let mut state = state.clone(); - state.allow_off_mode = is_enabled; - Message::StateChanged(mac.to_string(), DeviceState::AirPods(state)) - }) - .spacing(0) - .size(20) - ] - .align_y(Center) - .spacing(8) - ) - .padding(Padding{ + }), + ] + .align_y(iced::Alignment::Center) + ) + .style(|_theme: &Theme, _status| { + let mut style = Style::default(); + style.background = Some(Background::Color(Color::TRANSPARENT)); + style.text_color = Color::TRANSPARENT; + style + }) + .padding(Padding { top: 5.0, bottom: 5.0, left: 18.0, right: 18.0, }) - .style( - |theme: &Theme| { - let mut style = container::Style::default(); - style.background = Some(Background::Color(theme.palette().primary.scale_alpha(0.1))); - let mut border = Border::default(); - border.color = theme.palette().primary.scale_alpha(0.5); - style.border = border.rounded(16); - style - } - ) - }; + .on_press(Message::ToggleDeviceInfo); - let mut information_col = column![]; - if let Some(device) = devices_list.get(mac_information.as_str()) { - if let Some(DeviceInformation::AirPods(ref airpods_info)) = device.information { - let info_rows = column![ - row![ - text("Model Number").size(16).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().text); - style - }), - Space::new().width(Length::Fill), - text(airpods_info.model_number.clone()).size(16) - ], - row![ - text("Manufacturer").size(16).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().text); - style - }), - Space::new().width(Length::Fill), - text(airpods_info.manufacturer.clone()).size(16) - ], - row![ - text("Serial Number").size(16).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().text); - style - }), - Space::new().width(Length::Fill), - button(text(airpods_info.serial_number.clone()).size(16)) - .style(|theme: &Theme, _status| { - let mut style = Style::default(); - style.text_color = theme.palette().text; - style.background = Some(Background::Color(Color::TRANSPARENT)); + if show_device_info { + let serial_display = |serial: String| -> String { + if show_serials { serial } else { "\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}".to_string() } + }; + let eye_icon = if show_serials { "\u{1f441}" } else { "\u{25c9}" }; + + let info_rows = column![ + row![ + text("Model Number").size(16).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text); style - }) - .padding(0) - .on_press(Message::CopyToClipboard(airpods_info.serial_number.clone())) - ], - row![ - text("Left Serial Number").size(16).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().text); - style - }), - Space::new().width(Length::Fill), - button(text(airpods_info.left_serial_number.clone()).size(16)) - .style(|theme: &Theme, _status| { - let mut style = Style::default(); - style.text_color = theme.palette().text; - style.background = Some(Background::Color(Color::TRANSPARENT)); + }), + Space::new().width(Length::Fill), + text(match airpods_info.friendly_model_name() { + Some(name) => format!("{} ({})", airpods_info.model_number, name), + None => airpods_info.model_number.clone(), + }).size(16) + ], + row![ + text("Manufacturer").size(16).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text); style - }) - .padding(0) - .on_press(Message::CopyToClipboard( - airpods_info.left_serial_number.clone() - )) - ], - row![ - text("Right Serial Number").size(16).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().text); - style - }), - Space::new().width(Length::Fill), - button(text(airpods_info.right_serial_number.clone()).size(16)) - .style(|theme: &Theme, _status| { - let mut style = Style::default(); - style.text_color = theme.palette().text; - style.background = Some(Background::Color(Color::TRANSPARENT)); + }), + Space::new().width(Length::Fill), + text(airpods_info.manufacturer.clone()).size(16) + ], + row![ + text("Serial Number").size(16).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text); style - }) - .padding(0) - .on_press(Message::CopyToClipboard( - airpods_info.right_serial_number.clone() - )) - ], - row![ - text("Version 1").size(16).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().text); - style - }), - Space::new().width(Length::Fill), - text(airpods_info.version1.clone()).size(16) - ], - row![ - text("Version 2").size(16).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().text); - style - }), - Space::new().width(Length::Fill), - text(airpods_info.version2.clone()).size(16) - ], - row![ - text("Version 3").size(16).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().text); - style - }), - Space::new().width(Length::Fill), - text(airpods_info.version3.clone()).size(16) + }), + Space::new().width(Length::Fill), + button( + row![ + text(serial_display(airpods_info.serial_number.clone())).size(16), + text(eye_icon).size(14), + ].spacing(6).align_y(iced::Alignment::Center) + ) + .style(|theme: &Theme, _status| { + let mut style = Style::default(); + style.text_color = theme.palette().text; + style.background = Some(Background::Color(Color::TRANSPARENT)); + style + }) + .padding(0) + .on_press(Message::ToggleSerialVisibility) + ], + row![ + text("Left Serial Number").size(16).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text); + style + }), + Space::new().width(Length::Fill), + button( + row![ + text(serial_display(airpods_info.left_serial_number.clone())).size(16), + text(eye_icon).size(14), + ].spacing(6).align_y(iced::Alignment::Center) + ) + .style(|theme: &Theme, _status| { + let mut style = Style::default(); + style.text_color = theme.palette().text; + style.background = Some(Background::Color(Color::TRANSPARENT)); + style + }) + .padding(0) + .on_press(Message::ToggleSerialVisibility) + ], + row![ + text("Right Serial Number").size(16).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text); + style + }), + Space::new().width(Length::Fill), + button( + row![ + text(serial_display(airpods_info.right_serial_number.clone())).size(16), + text(eye_icon).size(14), + ].spacing(6).align_y(iced::Alignment::Center) + ) + .style(|theme: &Theme, _status| { + let mut style = Style::default(); + style.text_color = theme.palette().text; + style.background = Some(Background::Color(Color::TRANSPARENT)); + style + }) + .padding(0) + .on_press(Message::ToggleSerialVisibility) + ], + row![ + text("Version 1").size(16).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text); + style + }), + Space::new().width(Length::Fill), + text(airpods_info.version1.clone()).size(16) + ], + row![ + text("Version 2").size(16).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text); + style + }), + Space::new().width(Length::Fill), + text(airpods_info.version2.clone()).size(16) + ], + row![ + text("Version 3").size(16).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text); + style + }), + Space::new().width(Length::Fill), + text(airpods_info.version3.clone()).size(16) + ] ] - ] - .spacing(4) - .padding(8); + .spacing(4) + .padding(8); - information_col = column![ - container(text("Device Information").size(18).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().primary); - style - })) - .padding(Padding { - top: 5.0, - bottom: 5.0, - left: 18.0, - right: 18.0, - }), - container(info_rows) - .padding(Padding { - top: 5.0, - bottom: 5.0, - left: 10.0, - right: 10.0, - }) - .style(|theme: &Theme| { - let mut style = container::Style::default(); - style.background = - Some(Background::Color(theme.palette().primary.scale_alpha(0.1))); - let mut border = Border::default(); - border.color = theme.palette().primary.scale_alpha(0.5); - style.border = border.rounded(16); - style - }) - ]; + information_col = column![ + header, + container(info_rows) + .padding(Padding { + top: 5.0, + bottom: 5.0, + left: 10.0, + right: 10.0, + }) + .style(|theme: &Theme| { + let mut style = container::Style::default(); + style.background = + Some(Background::Color(theme.palette().primary.scale_alpha(0.1))); + let mut border = Border::default(); + border.color = theme.palette().primary.scale_alpha(0.5); + style.border = border.rounded(16); + style + }) + ]; + } else { + information_col = column![header]; + } } else { error!( "Expected AirPodsInformation for device {}, got something else", @@ -507,20 +805,25 @@ pub fn airpods_view<'a>( } } - container(column![ + // Battery view with device images + let battery_section = battery_view(state); + + let content = container(column![ rename_input, - Space::new().height(Length::from(20)), + Space::new().height(Length::from(10)), + battery_section, + Space::new().height(Length::from(10)), listening_mode, Space::new().height(Length::from(20)), audio_settings_col, Space::new().height(Length::from(20)), - off_listening_mode_toggle, - Space::new().height(Length::from(20)), information_col ]) .padding(20) - .center_x(Length::Fill) - .height(Length::Fill) + .center_x(Length::Fill); + + container(scrollable(content).height(Length::Fill)) + .height(Length::Fill) } fn run_async_in_thread(fut: F) diff --git a/linux-rust/src/ui/nothing.rs b/linux-rust/src/ui/nothing.rs index d683f0b16..907930b42 100644 --- a/linux-rust/src/ui/nothing.rs +++ b/linux-rust/src/ui/nothing.rs @@ -5,7 +5,7 @@ use iced::border::Radius; use iced::overlay::menu; use iced::widget::combo_box; use iced::widget::text_input; -use iced::widget::{Space, column, container, row, text}; +use iced::widget::{Space, button, column, container, row, scrollable, text}; use iced::{Background, Border, Length, Theme}; use std::collections::HashMap; use std::sync::Arc; @@ -17,37 +17,75 @@ pub fn nothing_view<'a>( devices_list: &HashMap, state: &'a NothingState, att_manager: Arc, + show_serials: bool, + show_device_info: bool, ) -> iced::widget::Container<'a, Message> { let mut information_col = iced::widget::column![]; let mac = mac.to_string(); if let Some(device) = devices_list.get(mac.as_str()) && let Some(DeviceInformation::Nothing(ref nothing_info)) = device.information { - information_col = information_col - .push(text("Device Information").size(18).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().primary); - style - })) - .push(Space::new().height(iced::Length::from(10))) - .push(iced::widget::row![ - text("Serial Number").size(16).style(|theme: &Theme| { - let mut style = text::Style::default(); - style.color = Some(theme.palette().text); - style - }), - Space::new().width(Length::Fill), - text(nothing_info.serial_number.clone()).size(16) - ]) - .push(iced::widget::row![ - text("Firmware Version").size(16).style(|theme: &Theme| { + let chevron = if show_device_info { "\u{25be}" } else { "\u{25b8}" }; + let header = button( + row![ + text(format!("{} Device Information", chevron)).size(18).style(|theme: &Theme| { let mut style = text::Style::default(); - style.color = Some(theme.palette().text); + style.color = Some(theme.palette().primary); style }), - Space::new().width(Length::Fill), - text(nothing_info.firmware_version.clone()).size(16) - ]); + ] + .align_y(iced::Alignment::Center) + ) + .style(|_theme: &Theme, _status| { + let mut style = button::Style::default(); + style.background = Some(Background::Color(iced::Color::TRANSPARENT)); + style.text_color = iced::Color::TRANSPARENT; + style + }) + .padding(0) + .on_press(Message::ToggleDeviceInfo); + + if show_device_info { + let eye_icon = if show_serials { "\u{1f441}" } else { "\u{25c9}" }; + let serial_text = if show_serials { nothing_info.serial_number.clone() } else { "\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}".to_string() }; + + information_col = information_col + .push(header) + .push(Space::new().height(iced::Length::from(10))) + .push(iced::widget::row![ + text("Serial Number").size(16).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text); + style + }), + Space::new().width(Length::Fill), + button( + row![ + text(serial_text).size(16), + text(eye_icon).size(14), + ].spacing(6).align_y(iced::Alignment::Center) + ) + .style(|theme: &Theme, _status| { + let mut style = button::Style::default(); + style.text_color = theme.palette().text; + style.background = Some(Background::Color(iced::Color::TRANSPARENT)); + style + }) + .padding(0) + .on_press(Message::ToggleSerialVisibility) + ]) + .push(iced::widget::row![ + text("Firmware Version").size(16).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text); + style + }), + Space::new().width(Length::Fill), + text(nothing_info.firmware_version.clone()).size(16) + ]); + } else { + information_col = information_col.push(header); + } } let noise_control_mode = container( @@ -158,7 +196,7 @@ pub fn nothing_view<'a>( style }); - container(column![ + let content = container(column![ noise_control_mode, Space::new().height(Length::from(20)), container(information_col) @@ -174,8 +212,10 @@ pub fn nothing_view<'a>( .padding(20) ]) .padding(20) - .center_x(Length::Fill) - .height(Length::Fill) + .center_x(Length::Fill); + + container(scrollable(content).height(Length::Fill)) + .height(Length::Fill) } fn run_async_in_thread(fut: F) diff --git a/linux-rust/src/ui/tray.rs b/linux-rust/src/ui/tray.rs index b3adbc53a..eff81e38c 100644 --- a/linux-rust/src/ui/tray.rs +++ b/linux-rust/src/ui/tray.rs @@ -148,7 +148,12 @@ impl ksni::Tray for MyTray { .get(current) .map(|&(_, val)| val) .unwrap_or(0x02); - let _ = tx.send((ControlCommandIdentifiers::ListeningMode, vec![value])); + // Only send if the mode actually changed (avoid feedback loop + // from ksni firing select during menu rebuilds) + if this.listening_mode != Some(value) { + let _ = tx.send((ControlCommandIdentifiers::ListeningMode, vec![value])); + this.listening_mode = Some(value); + } } }), options: options diff --git a/linux-rust/src/ui/window.rs b/linux-rust/src/ui/window.rs index 4574b97ce..727f2d782 100644 --- a/linux-rust/src/ui/window.rs +++ b/linux-rust/src/ui/window.rs @@ -3,8 +3,8 @@ use crate::bluetooth::aacp::{ }; use crate::bluetooth::managers::DeviceManagers; use crate::devices::enums::{ - AirPodsNoiseControlMode, AirPodsState, DeviceData, DeviceState, DeviceType, NothingAncMode, - NothingState, + AirPodsModel, AirPodsNoiseControlMode, AirPodsState, DeviceData, DeviceInformation, DeviceState, + DeviceType, NothingAncMode, NothingState, }; use crate::ui::airpods::airpods_view; use crate::ui::messages::BluetoothUIMessage; @@ -19,9 +19,9 @@ use iced::widget::{ Space, button, column, combo_box, container, pane_grid, row, rule, scrollable, text, text_input, toggler }; -use iced::{Background, Border, Center, Element, Font, Length, Padding, Size, Subscription, Task, Theme, daemon, window, Settings, Program}; +use iced::{Background, Border, Center, Color, Element, Font, Length, Padding, Size, Subscription, Task, Theme, daemon, window, Settings, Program}; use log::{debug, error}; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use tokio::sync::mpsc::UnboundedReceiver; @@ -76,6 +76,10 @@ pub struct App { selected_device_type: Option, tray_text_mode: bool, stem_control: bool, + show_serials: bool, + show_device_info: bool, + show_off_listening_mode: bool, + connecting_devices: HashSet, } pub struct BluetoothState { @@ -108,6 +112,12 @@ pub enum Message { StateChanged(String, DeviceState), TrayTextModeChanged(bool), // yes, I know I should add all settings to a struct, but I'm lazy StemControlChanged(bool), + ToggleSerialVisibility, + ToggleDeviceInfo, + ConnectDevice(String), + ConnectResult(String, bool), + ShowOffListeningModeChanged(bool), + SetListeningMode(String, AirPodsNoiseControlMode), } #[derive(Clone, Debug, PartialEq, Eq, Hash)] @@ -166,6 +176,11 @@ impl App { .and_then(|v| v.get("stem_control").cloned()) .and_then(|s| serde_json::from_value(s).ok()) .unwrap_or(false); + let show_off_listening_mode = settings + .clone() + .and_then(|v| v.get("show_off_listening_mode").cloned()) + .and_then(|s| serde_json::from_value(s).ok()) + .unwrap_or(true); let bluetooth_state = BluetoothState::new(); @@ -217,6 +232,10 @@ impl App { device_managers, tray_text_mode, stem_control, + show_serials: false, + show_device_info: false, + show_off_listening_mode, + connecting_devices: HashSet::new(), }, Task::batch(vec![open_task, wait_task]), ) @@ -253,6 +272,7 @@ impl App { "theme": self.selected_theme, "tray_text_mode": self.tray_text_mode, "stem_control": self.stem_control, + "show_off_listening_mode": self.show_off_listening_mode, }); debug!( "Writing settings to {}: {}", @@ -328,7 +348,7 @@ impl App { let aacp_manager_state = aacp_manager.state.clone(); let state = aacp_manager_state.blocking_lock(); debug!("AACP manager found for AirPods device {}", mac); - let device_name = { + let (device_name, model) = { let devices_json = std::fs::read_to_string(get_devices_path()) .unwrap_or_else(|e| { error!("Failed to read devices file: {}", e); @@ -339,37 +359,35 @@ impl App { error!("Deserialization failed: {}", e); HashMap::new() }); - devices_list + let name = devices_list .get(&mac) .map(|d| d.name.clone()) - .unwrap_or_else(|| "Unknown Device".to_string()) + .unwrap_or_else(|| "Unknown Device".to_string()); + let model = devices_list + .get(&mac) + .and_then(|d| d.information.as_ref()) + .and_then(|info| { + if let DeviceInformation::AirPods(ap) = info { + Some(AirPodsModel::from_model_number(&ap.model_number)) + } else { + None + } + }) + .unwrap_or(AirPodsModel::Unknown); + (name, model) }; self.device_states.insert(mac.clone(), DeviceState::AirPods(AirPodsState { device_name, + model, battery: state.battery_info.clone(), noise_control_mode: state.control_command_status_list.iter().find_map(|status| { if status.identifier == ControlCommandIdentifiers::ListeningMode { - status.value.first().map(AirPodsNoiseControlMode::from_byte) + status.value.first().and_then(AirPodsNoiseControlMode::from_byte) } else { None } }).unwrap_or(AirPodsNoiseControlMode::Transparency), - noise_control_state: combo_box::State::new( - { - let mut modes = vec![ - AirPodsNoiseControlMode::Transparency, - AirPodsNoiseControlMode::NoiseCancellation, - AirPodsNoiseControlMode::Adaptive - ]; - if state.control_command_status_list.iter().any(|status| { - status.identifier == ControlCommandIdentifiers::AllowOffOption && - matches!(status.value.as_slice(), [0x01]) - }) { - modes.insert(0, AirPodsNoiseControlMode::Off); - } - modes - } - ), + conversation_awareness_enabled: state.control_command_status_list.iter().any(|status| { status.identifier == ControlCommandIdentifiers::ConversationDetectConfig && matches!(status.value.as_slice(), [0x01]) @@ -429,15 +447,21 @@ impl App { match event { AACPEvent::ControlCommand(status) => match status.identifier { ControlCommandIdentifiers::ListeningMode => { - let mode = status + if let Some(mode) = status .value .first() - .map(AirPodsNoiseControlMode::from_byte) - .unwrap_or(AirPodsNoiseControlMode::Transparency); - if let Some(DeviceState::AirPods(state)) = - self.device_states.get_mut(&mac) + .and_then(AirPodsNoiseControlMode::from_byte) { - state.noise_control_mode = mode; + if let Some(DeviceState::AirPods(state)) = + self.device_states.get_mut(&mac) + { + state.noise_control_mode = mode; + } + } else { + log::warn!( + "Ignoring unknown ListeningMode value: {:?}", + status.value + ); } } ControlCommandIdentifiers::ConversationDetectConfig => { @@ -492,17 +516,6 @@ impl App { self.device_states.get_mut(&mac) { state.allow_off_mode = is_enabled; - state.noise_control_state = combo_box::State::new({ - let mut modes = vec![ - AirPodsNoiseControlMode::Transparency, - AirPodsNoiseControlMode::NoiseCancellation, - AirPodsNoiseControlMode::Adaptive, - ]; - if is_enabled { - modes.insert(0, AirPodsNoiseControlMode::Off); - } - modes - }); } } _ => { @@ -608,19 +621,9 @@ impl App { devices_list.get(&mac).map(|d| d.type_.clone()) }; if let Some(DeviceType::AirPods) = type_ - && let Some(DeviceState::AirPods(state)) = self.device_states.get_mut(&mac) + && let Some(DeviceState::AirPods(_state)) = self.device_states.get_mut(&mac) { - state.noise_control_state = combo_box::State::new({ - let mut modes = vec![ - AirPodsNoiseControlMode::Transparency, - AirPodsNoiseControlMode::NoiseCancellation, - AirPodsNoiseControlMode::Adaptive, - ]; - if state.allow_off_mode { - modes.insert(0, AirPodsNoiseControlMode::Off); - } - modes - }); + // No-op: segmented buttons determine available modes at render time } Task::none() } @@ -631,6 +634,7 @@ impl App { "theme": self.selected_theme, "tray_text_mode": self.tray_text_mode, "stem_control": self.stem_control, + "show_off_listening_mode": self.show_off_listening_mode, }); debug!( "Writing settings to {}: {}", @@ -647,6 +651,35 @@ impl App { "theme": self.selected_theme, "tray_text_mode": self.tray_text_mode, "stem_control": self.stem_control, + "show_off_listening_mode": self.show_off_listening_mode, + }); + debug!( + "Writing settings to {}: {}", + app_settings_path.to_str().unwrap(), + settings + ); + std::fs::write(app_settings_path, settings.to_string()).ok(); + Task::none() + } + Message::ToggleSerialVisibility => { + self.show_serials = !self.show_serials; + Task::none() + } + Message::ToggleDeviceInfo => { + self.show_device_info = !self.show_device_info; + if !self.show_device_info { + self.show_serials = false; + } + Task::none() + } + Message::ShowOffListeningModeChanged(is_enabled) => { + self.show_off_listening_mode = is_enabled; + let app_settings_path = get_app_settings_path(); + let settings = serde_json::json!({ + "theme": self.selected_theme, + "tray_text_mode": self.tray_text_mode, + "stem_control": self.stem_control, + "show_off_listening_mode": self.show_off_listening_mode, }); debug!( "Writing settings to {}: {}", @@ -656,6 +689,52 @@ impl App { std::fs::write(app_settings_path, settings.to_string()).ok(); Task::none() } + Message::ConnectDevice(mac) => { + self.connecting_devices.insert(mac.clone()); + Task::perform( + async move { + let output = tokio::process::Command::new("bluetoothctl") + .arg("connect") + .arg(&mac) + .output() + .await; + let success = output.map(|o| o.status.success()).unwrap_or(false); + (mac, success) + }, + |(mac, success)| Message::ConnectResult(mac, success), + ) + } + Message::ConnectResult(mac, _success) => { + self.connecting_devices.remove(&mac); + Task::none() + } + Message::SetListeningMode(mac, mode) => { + // Send the AACP command to change the listening mode + let device_managers = self.device_managers.blocking_read(); + if let Some(managers) = device_managers.get(&mac) { + if let Some(aacp_manager) = managers.get_aacp() { + let mode_byte = mode.to_byte(); + let aacp = aacp_manager.clone(); + std::thread::spawn(move || { + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async move { + if let Err(e) = aacp.send_control_command( + ControlCommandIdentifiers::ListeningMode, + &[mode_byte], + ).await { + log::error!("Failed to send Noise Control Mode command: {}", e); + } + }); + }); + } + } + drop(device_managers); + // Update the local UI state + if let Some(DeviceState::AirPods(state)) = self.device_states.get_mut(&mac) { + state.noise_control_mode = mode; + } + Task::none() + } } } @@ -864,6 +943,62 @@ impl App { let device_type = devices_list.get(id).map(|d| d.type_.clone()); let device_state = self.device_states.get(id); debug!("Rendering device view for {}: type={:?}, state={:?}", id, device_type, device_state); + let is_connected = self.bluetooth_state.connected_devices.contains(id); + let is_connecting = self.connecting_devices.contains(id); + let device_name = devices_list.get(id).map(|d| d.name.clone()).unwrap_or_else(|| id.clone()); + + if !is_connected && !is_connecting { + let id_clone = id.clone(); + container( + column![ + text("\u{1F3A7}").size(64), + Space::new().height(Length::from(16)), + text(device_name).size(22), + Space::new().height(Length::from(8)), + text("Not Connected").size(14).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text.scale_alpha(0.5)); + style + }), + Space::new().height(Length::from(24)), + button( + container( + text("Connect").size(15) + ) + .padding(Padding { top: 8.0, bottom: 8.0, left: 24.0, right: 24.0 }) + ) + .style(|theme: &Theme, _status| { + let mut style = Style::default(); + style.background = Some(Background::Color(theme.palette().primary)); + style.text_color = Color::WHITE; + style.border = Border::default().rounded(10); + style + }) + .padding(0) + .on_press(Message::ConnectDevice(id_clone)) + ] + .align_x(iced::Alignment::Center) + ) + .center_x(Length::Fill) + .center_y(Length::Fill) + } else if is_connecting { + container( + column![ + text("\u{1F3A7}").size(64), + Space::new().height(Length::from(16)), + text(device_name).size(22), + Space::new().height(Length::from(8)), + text("Connecting\u{2026}").size(14).style(|theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().primary); + style + }), + ] + .align_x(iced::Alignment::Center) + ) + .center_x(Length::Fill) + .center_y(Length::Fill) + } else { match device_type { Some(DeviceType::AirPods) => { @@ -875,7 +1010,10 @@ impl App { id, &devices_list, state, - aacp_manager.clone() + aacp_manager.clone(), + self.show_serials, + self.show_device_info, + self.show_off_listening_mode, )) }) } @@ -883,7 +1021,7 @@ impl App { } }).unwrap_or_else(|| { container( - text("Required managers or state not available for this AirPods device").size(16) + text("Loading device...").size(16) ) .center_x(Length::Fill) .center_y(Length::Fill) @@ -893,26 +1031,24 @@ impl App { if let Some(DeviceState::Nothing(state)) = device_state { if let Some(device_managers) = device_managers.get(id) { if let Some(att_manager) = device_managers.get_att() { - nothing_view(id, &devices_list, state, att_manager.clone()) + nothing_view(id, &devices_list, state, att_manager.clone(), self.show_serials, self.show_device_info) } else { - error!("No ATT manager found for Nothing device {}", id); container( - text("No valid ATT manager found for this Nothing device").size(16) + text("Loading device...").size(16) ) .center_x(Length::Fill) .center_y(Length::Fill) } } else { - error!("No manager found for Nothing device {}", id); container( - text("No manager found for this Nothing device").size(16) + text("Loading device...").size(16) ) .center_x(Length::Fill) .center_y(Length::Fill) } } else { container( - text("No state available for this Nothing device").size(16) + text("Loading device...").size(16) ) .center_x(Length::Fill) .center_y(Length::Fill) @@ -924,6 +1060,7 @@ impl App { .center_y(Length::Fill) } } + } } } Tab::Settings => { @@ -1112,7 +1249,46 @@ impl App { left: 18.0, right: 18.0, }), - stem_control_toggle + stem_control_toggle, + container( + row![ + column![ + text("Show Off listening mode").size(16), + text("When enabled, an Off option appears in the listening mode controls. Loud sound levels are not reduced when listening mode is set to Off.").size(12).style( + |theme: &Theme| { + let mut style = text::Style::default(); + style.color = Some(theme.palette().text.scale_alpha(0.7)); + style + } + ).width(Length::Fill) + ].width(Length::Fill), + toggler(self.show_off_listening_mode) + .on_toggle(move |is_enabled| { + Message::ShowOffListeningModeChanged(is_enabled) + }) + .spacing(0) + .size(20) + ] + .align_y(Center) + .spacing(12) + ) + .padding(Padding{ + top: 5.0, + bottom: 5.0, + left: 18.0, + right: 18.0, + }) + .style( + |theme: &Theme| { + let mut style = container::Style::default(); + style.background = Some(Background::Color(theme.palette().primary.scale_alpha(0.1))); + let mut border = Border::default(); + border.color = theme.palette().primary.scale_alpha(0.5); + style.border = border.rounded(16); + style + } + ) + .align_y(Center) ] .spacing(12); diff --git a/linux/CMakeLists.txt b/linux/CMakeLists.txt index 4f73b8dc2..43051ce69 100644 --- a/linux/CMakeLists.txt +++ b/linux/CMakeLists.txt @@ -4,13 +4,18 @@ project(linux VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) -find_package(Qt6 REQUIRED COMPONENTS Quick Widgets Bluetooth DBus) +find_package(Qt6 REQUIRED COMPONENTS Quick Widgets Bluetooth DBus LinguistTools) find_package(OpenSSL REQUIRED) find_package(PkgConfig REQUIRED) pkg_check_modules(PULSEAUDIO REQUIRED libpulse) qt_standard_project_setup() +# Translation files +set(TS_FILES + translations/librepods_tr.ts +) + qt_add_executable(librepods main.cpp logger.h @@ -73,6 +78,16 @@ target_link_libraries(librepods PRIVATE Qt6::Quick Qt6::Widgets Qt6::Bluetooth Qt6::DBus OpenSSL::SSL OpenSSL::Crypto ${PULSEAUDIO_LIBRARIES} ) +qt_add_executable(librepods-ctl + librepods-ctl.cpp +) +target_link_libraries(librepods-ctl + PRIVATE Qt6::Core Qt6::Network +) +install(TARGETS librepods-ctl + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) + target_include_directories(librepods PRIVATE ${PULSEAUDIO_INCLUDE_DIRS}) include(GNUInstallDirs) @@ -83,3 +98,15 @@ install(TARGETS librepods ) install(FILES assets/me.kavishdevar.librepods.desktop DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/applications") +install(FILES assets/librepods.svg + DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/scalable/apps") + +# Translation support +qt_add_translations(librepods + TS_FILES ${TS_FILES} + QM_FILES_OUTPUT_VARIABLE QM_FILES +) + +# Install translation files +install(FILES ${QM_FILES} + DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/librepods/translations") diff --git a/linux/Main.qml b/linux/Main.qml index 983a2a5a5..c7b235c33 100644 --- a/linux/Main.qml +++ b/linux/Main.qml @@ -81,7 +81,7 @@ ApplicationWindow { Label { anchors.centerIn: parent - text: airPodsTrayApp.airpodsConnected ? "Connected" : "Disconnected" + text: airPodsTrayApp.airpodsConnected ? qsTr("Connected") : qsTr("Disconnected") color: "white" font.pixelSize: 12 font.weight: Font.Medium @@ -118,11 +118,19 @@ ApplicationWindow { batteryLevel: airPodsTrayApp.deviceInfo.battery.caseLevel isCharging: airPodsTrayApp.deviceInfo.battery.caseCharging } + + PodColumn { + visible: airPodsTrayApp.deviceInfo.battery.headsetAvailable + inEar: true + iconSource: "qrc:/icons/assets/" + airPodsTrayApp.deviceInfo.podIcon + batteryLevel: airPodsTrayApp.deviceInfo.battery.headsetLevel + isCharging: airPodsTrayApp.deviceInfo.battery.headsetCharging + } } SegmentedControl { anchors.horizontalCenter: parent.horizontalCenter - model: ["Off", "Noise Cancellation", "Transparency", "Adaptive"] + model: [qsTr("Off"), qsTr("Noise Cancellation"), qsTr("Transparency"), qsTr("Adaptive")] currentIndex: airPodsTrayApp.deviceInfo.noiseControlMode onCurrentIndexChanged: airPodsTrayApp.setNoiseControlModeInt(currentIndex) visible: airPodsTrayApp.airpodsConnected @@ -145,21 +153,21 @@ ApplicationWindow { onValueChanged: if (pressed) debounceTimer.restart() Label { - text: "Adaptive Noise Level: " + parent.value + text: qsTr("Adaptive Noise Level: ") + parent.value anchors.top: parent.bottom } } Switch { visible: airPodsTrayApp.airpodsConnected - text: "Conversational Awareness" + text: qsTr("Conversational Awareness") checked: airPodsTrayApp.deviceInfo.conversationalAwareness onCheckedChanged: airPodsTrayApp.setConversationalAwareness(checked) } Switch { visible: airPodsTrayApp.airpodsConnected - text: "Hearing Aid" + text: qsTr("Hearing Aid") checked: airPodsTrayApp.deviceInfo.hearingAidEnabled onCheckedChanged: airPodsTrayApp.setHearingAidEnabled(checked) } @@ -181,7 +189,7 @@ ApplicationWindow { id: settingsPage Page { id: settingsPageItem - title: "Settings" + title: qsTr("Settings") ScrollView { anchors.fill: parent @@ -192,7 +200,7 @@ ApplicationWindow { padding: 20 Label { - text: "Settings" + text: qsTr("Settings") font.pixelSize: 24 // center the label anchors.horizontalCenter: parent.horizontalCenter @@ -202,19 +210,19 @@ ApplicationWindow { spacing: 5 // Small gap between label and ComboBox Label { - text: "Pause Behavior When Removing AirPods:" + text: qsTr("Pause Behavior When Removing AirPods:") } ComboBox { width: parent.width // Ensures full width - model: ["One Removed", "Both Removed", "Never"] + model: [qsTr("One Removed"), qsTr("Both Removed"), qsTr("Never")] currentIndex: airPodsTrayApp.earDetectionBehavior onActivated: airPodsTrayApp.earDetectionBehavior = currentIndex } } Switch { - text: "Cross-Device Connectivity with Android" + text: qsTr("Cross-Device Connectivity with Android") checked: airPodsTrayApp.crossDeviceEnabled onCheckedChanged: { airPodsTrayApp.setCrossDeviceEnabled(checked) @@ -222,26 +230,26 @@ ApplicationWindow { } Switch { - text: "Auto-Start on Login" + text: qsTr("Auto-Start on Login") checked: airPodsTrayApp.autoStartManager.autoStartEnabled onCheckedChanged: airPodsTrayApp.autoStartManager.autoStartEnabled = checked } Switch { - text: "Enable System Notifications" + text: qsTr("Enable System Notifications") checked: airPodsTrayApp.notificationsEnabled onCheckedChanged: airPodsTrayApp.notificationsEnabled = checked } Switch { visible: airPodsTrayApp.airpodsConnected - text: "One Bud ANC Mode" + text: qsTr("One Bud ANC Mode") checked: airPodsTrayApp.deviceInfo.oneBudANCMode onCheckedChanged: airPodsTrayApp.deviceInfo.oneBudANCMode = checked ToolTip { visible: parent.hovered - text: "Enable ANC when using one AirPod\n(More noise reduction, but uses more battery)" + text: qsTr("Enable ANC when using one AirPod\n(More noise reduction, but uses more battery)") delay: 500 } } @@ -249,7 +257,7 @@ ApplicationWindow { Row { spacing: 5 Label { - text: "Bluetooth Retry Attempts:" + text: qsTr("Bluetooth Retry Attempts:") anchors.verticalCenter: parent.verticalCenter } SpinBox { @@ -271,7 +279,7 @@ ApplicationWindow { } Button { - text: "Rename" + text: qsTr("Rename") onClicked: airPodsTrayApp.renameAirPods(newNameField.text) } } @@ -287,14 +295,14 @@ ApplicationWindow { } Button { - text: "Change Phone MAC" + text: qsTr("Change Phone MAC") onClicked: airPodsTrayApp.setPhoneMac(newPhoneMacField.text) } } Button { - text: "Show Magic Cloud Keys QR" + text: qsTr("Show Magic Cloud Keys QR") onClicked: keysQrDialog.show() } @@ -318,4 +326,4 @@ ApplicationWindow { } } } -} \ No newline at end of file +} diff --git a/linux/README.md b/linux/README.md index 742d99115..10648c968 100644 --- a/linux/README.md +++ b/linux/README.md @@ -1,5 +1,30 @@ +# LibrePods on Linux + +A new rewrite is being worked upon. Please look at the list of features in the root README to see what's supported in the new version. + +The rewrite can be found in the `linux/rust` branch [here](https://github.com/kavishdevar/librepods/tree/linux/rust/linux-rust). Follow the development in [PR #241](https://github.com/kavishdevar/librepods/pull/241). + + +## Installation + +### GitHub Releases + +The app is ready to download as an AppImage or an executable. You can download the latest pre-release from the [GitHub releases](https://github.com/kavishdevar/librepods/releases?q="linux-v0"). + +### Nightly Builds (recommended) + +You can also try the latest build of the new version from the [GitHub Actions artifacts](https://github.com/kavishdevar/librepods/actions/workflows/ci-linux-rust.yml). On the latest successful workflow run, download the **librepods-x86_64.AppImage** or **librepods** binary from **Artifacts**. + + +![new version screenshot](https://github.com/user-attachments/assets/86b3c871-89a8-4e49-861a-5119de1e1d28) + +
+ README for the old version + # LibrePods Linux +![screenshot](imgs/main-app.png) + A native Linux application to control your AirPods, with support for: - Noise Control modes (Off, Transparency, Adaptive, Noise Cancellation) @@ -41,6 +66,31 @@ A native Linux application to control your AirPods, with support for: # For Fedora sudo dnf install openssl-devel ``` +4. Libpulse development headers + + ```bash + # On Arch Linux / EndevaourOS, these are included in the libpulse package, so you might already have them installed. + sudo pacman -S libpulse + + # For Debian / Ubuntu + sudo apt-get install libpulse-dev + + # For Fedora + sudo dnf install pulseaudio-libs-devel + ``` +5. Cmake + + ```bash + # For Arch Linux / EndeavourOS + sudo pacman -S cmake + + # For Debian / Ubuntu + sudo apt-get install cmake + + # For Fedora + sudo dnf install cmake + ``` + ## Setup 1. Build the application: @@ -82,7 +132,8 @@ Then restart WirePlumber: systemctl --user restart wireplumber ``` -**Note:** Do NOT run `mpris-proxy` with WirePlumber - it will conflict and break media controls. +> [!WARNING] +> Do NOT run `mpris-proxy` with WirePlumber - it will conflict and break media controls. #### PulseAudio @@ -101,6 +152,35 @@ systemctl --user enable --now mpris-proxy - View battery levels - Control playback + +## CLI Control + +`librepods-ctl` is a small command-line tool that lets you access LibrePods from the terminal or via scripts, as long as the main application is running. + +### Usage +```bash +librepods-ctl +``` + +### Commands + +| Command | Description | +|---|---| +| `noise:off` | Disable noise control | +| `noise:anc` | Enable Active Noise Cancellation | +| `noise:transparency` | Enable Transparency mode | +| `noise:adaptive` | Enable Adaptive mode | + +### Example +```bash +# Enable ANC +librepods-ctl noise:anc + +# Enable Transparency mode +librepods-ctl noise:transparency +``` + + ## Hearing Aid To use hearing aid features, you need to have an audiogram. To enable/disable hearing aid, you can use the toggle in the main app. But, to adjust the settings and set the audiogram, you need to use a different script which is located in this folder as `hearing_aid.py`. You can run it with: @@ -131,4 +211,6 @@ It is possible that the AirPods disconnect after a short period of time and play ### Why a separate script? -Because I discovered that QBluetooth doesn't support connecting to a socket with its PSM, only a UUID can be used. I could add a dependency on BlueZ, but then having two bluetooth interfaces seems unnecessary. So, I decided to use a separate script for hearing aid features. In the future, QBluetooth will be replaced with BlueZ native calls, and then everything will be in one application. \ No newline at end of file +Because I discovered that QBluetooth doesn't support connecting to a socket with its PSM, only a UUID can be used. I could add a dependency on BlueZ, but then having two bluetooth interfaces seems unnecessary. So, I decided to use a separate script for hearing aid features. In the future, QBluetooth will be replaced with BlueZ native calls, and then everything will be in one application. + +
\ No newline at end of file diff --git a/linux/airpods_packets.h b/linux/airpods_packets.h index 52662723f..94153a484 100644 --- a/linux/airpods_packets.h +++ b/linux/airpods_packets.h @@ -113,24 +113,24 @@ namespace AirPodsPackets static const QByteArray HEADER = ControlCommand::HEADER + static_cast(0x2C); static const QByteArray ENABLED = ControlCommand::createCommand(0x2C, 0x01, 0x01); static const QByteArray DISABLED = ControlCommand::createCommand(0x2C, 0x02, 0x02); - + inline std::optional parseState(const QByteArray &data) { if (!data.startsWith(HEADER) || data.size() < HEADER.size() + 2) return std::nullopt; - + QByteArray value = data.mid(HEADER.size(), 2); if (value.size() != 2) return std::nullopt; - + char b1 = value.at(0); char b2 = value.at(1); - + if (b1 == 0x01 && b2 == 0x01) return true; if (b1 == 0x02 || b2 == 0x02) return false; - + return std::nullopt; } } diff --git a/linux/assets/librepods.svg b/linux/assets/librepods.svg new file mode 100644 index 000000000..d43867248 --- /dev/null +++ b/linux/assets/librepods.svg @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/linux/battery.hpp b/linux/battery.hpp index 99119e4ad..63f30e009 100644 --- a/linux/battery.hpp +++ b/linux/battery.hpp @@ -19,6 +19,9 @@ class Battery : public QObject Q_PROPERTY(quint8 rightPodLevel READ getRightPodLevel NOTIFY batteryStatusChanged) Q_PROPERTY(bool rightPodCharging READ isRightPodCharging NOTIFY batteryStatusChanged) Q_PROPERTY(bool rightPodAvailable READ isRightPodAvailable NOTIFY batteryStatusChanged) + Q_PROPERTY(quint8 headsetLevel READ getHeadsetLevel NOTIFY batteryStatusChanged) + Q_PROPERTY(bool headsetCharging READ isHeadsetCharging NOTIFY batteryStatusChanged) + Q_PROPERTY(bool headsetAvailable READ isHeadsetAvailable NOTIFY batteryStatusChanged) Q_PROPERTY(quint8 caseLevel READ getCaseLevel NOTIFY batteryStatusChanged) Q_PROPERTY(bool caseCharging READ isCaseCharging NOTIFY batteryStatusChanged) Q_PROPERTY(bool caseAvailable READ isCaseAvailable NOTIFY batteryStatusChanged) @@ -32,6 +35,7 @@ class Battery : public QObject void reset() { // Initialize all components to unknown state + states[Component::Headset] = {}; states[Component::Left] = {}; states[Component::Right] = {}; states[Component::Case] = {}; @@ -41,6 +45,7 @@ class Battery : public QObject // Enum for AirPods components enum class Component { + Headset = 0x01, // AirPods Max Right = 0x02, Left = 0x04, Case = 0x08, @@ -105,7 +110,7 @@ class Battery : public QObject } // If this is a pod (Left or Right), add it to the list - if (comp == Component::Left || comp == Component::Right) + if (comp == Component::Left || comp == Component::Right || comp == Component::Headset) { podsInPacket.append(comp); } @@ -117,11 +122,17 @@ class Battery : public QObject // Set primary and secondary pods based on order if (!podsInPacket.isEmpty()) { - Component newPrimaryPod = podsInPacket[0]; // First pod is primary - if (newPrimaryPod != primaryPod) - { - primaryPod = newPrimaryPod; + if (podsInPacket.count() == 1 && podsInPacket[0] == Component::Headset) { + // AirPods Max + primaryPod = podsInPacket[0]; emit primaryChanged(); + } else { + Component newPrimaryPod = podsInPacket[0]; // First pod is primary + if (newPrimaryPod != primaryPod) + { + primaryPod = newPrimaryPod; + emit primaryChanged(); + } } } if (podsInPacket.size() >= 2) @@ -132,14 +143,18 @@ class Battery : public QObject // Emit signal to notify about battery status change emit batteryStatusChanged(); - // Log which is left and right pod - LOG_INFO("Primary Pod:" << primaryPod); - LOG_INFO("Secondary Pod:" << secondaryPod); + if (primaryPod == Component::Headset) { + LOG_INFO("Primary Pod:" << primaryPod); + } else { + // Log which is left and right pod + LOG_INFO("Primary Pod:" << primaryPod); + LOG_INFO("Secondary Pod:" << secondaryPod); + } return true; } - bool parseEncryptedPacket(const QByteArray &packet, bool isLeftPodPrimary, bool podInCase) + bool parseEncryptedPacket(const QByteArray &packet, bool isLeftPodPrimary, bool podInCase, bool isHeadset) { // Validate packet size (expect 16 bytes based on provided payloads) if (packet.size() != 16) @@ -160,30 +175,42 @@ class Battery : public QObject auto [isLeftCharging, rawLeftBattery] = formatBattery(rawLeftBatteryByte); auto [isRightCharging, rawRightBattery] = formatBattery(rawRightBatteryByte); auto [isCaseCharging, rawCaseBattery] = formatBattery(rawCaseBatteryByte); + if (isHeadset) { + int batteries[] = {rawLeftBattery, rawRightBattery, rawCaseBattery}; + bool statuses[] = {isLeftCharging, isRightCharging, isCaseCharging}; + // Find the first battery that isn't CHAR_MAX + auto it = std::find_if(std::begin(batteries), std::end(batteries), [](int i) { return i != CHAR_MAX; }); + if (it != std::end(batteries)) { + std::size_t idx = it - std::begin(batteries); + int battery = *it; + primaryPod = Component::Headset; + states[Component::Headset] = {static_cast(battery), statuses[idx] ? BatteryStatus::Charging : BatteryStatus::Discharging}; + } + } else { + if (rawLeftBattery == CHAR_MAX) { + rawLeftBattery = states.value(Component::Left).level; // Use last valid level + isLeftCharging = states.value(Component::Left).status == BatteryStatus::Charging; + } - if (rawLeftBattery == CHAR_MAX) { - rawLeftBattery = states.value(Component::Left).level; // Use last valid level - isLeftCharging = states.value(Component::Left).status == BatteryStatus::Charging; - } - - if (rawRightBattery == CHAR_MAX) { - rawRightBattery = states.value(Component::Right).level; // Use last valid level - isRightCharging = states.value(Component::Right).status == BatteryStatus::Charging; - } + if (rawRightBattery == CHAR_MAX) { + rawRightBattery = states.value(Component::Right).level; // Use last valid level + isRightCharging = states.value(Component::Right).status == BatteryStatus::Charging; + } - if (rawCaseBattery == CHAR_MAX) { - rawCaseBattery = states.value(Component::Case).level; // Use last valid level - isCaseCharging = states.value(Component::Case).status == BatteryStatus::Charging; - } + if (rawCaseBattery == CHAR_MAX) { + rawCaseBattery = states.value(Component::Case).level; // Use last valid level + isCaseCharging = states.value(Component::Case).status == BatteryStatus::Charging; + } - // Update states - states[Component::Left] = {static_cast(rawLeftBattery), isLeftCharging ? BatteryStatus::Charging : BatteryStatus::Discharging}; - states[Component::Right] = {static_cast(rawRightBattery), isRightCharging ? BatteryStatus::Charging : BatteryStatus::Discharging}; - if (podInCase) { - states[Component::Case] = {static_cast(rawCaseBattery), isCaseCharging ? BatteryStatus::Charging : BatteryStatus::Discharging}; + // Update states + states[Component::Left] = {static_cast(rawLeftBattery), isLeftCharging ? BatteryStatus::Charging : BatteryStatus::Discharging}; + states[Component::Right] = {static_cast(rawRightBattery), isRightCharging ? BatteryStatus::Charging : BatteryStatus::Discharging}; + if (podInCase) { + states[Component::Case] = {static_cast(rawCaseBattery), isCaseCharging ? BatteryStatus::Charging : BatteryStatus::Discharging}; + } + primaryPod = isLeftPodPrimary ? Component::Left : Component::Right; + secondaryPod = isLeftPodPrimary ? Component::Right : Component::Left; } - primaryPod = isLeftPodPrimary ? Component::Left : Component::Right; - secondaryPod = isLeftPodPrimary ? Component::Right : Component::Left; emit batteryStatusChanged(); emit primaryChanged(); @@ -236,6 +263,9 @@ class Battery : public QObject quint8 getCaseLevel() const { return states.value(Component::Case).level; } bool isCaseCharging() const { return isStatus(Component::Case, BatteryStatus::Charging); } bool isCaseAvailable() const { return !isStatus(Component::Case, BatteryStatus::Disconnected); } + quint8 getHeadsetLevel() const { return states.value(Component::Headset).level; } + bool isHeadsetCharging() const { return isStatus(Component::Headset, BatteryStatus::Charging); } + bool isHeadsetAvailable() const { return !isStatus(Component::Headset, BatteryStatus::Disconnected); } signals: void batteryStatusChanged(); @@ -257,4 +287,4 @@ class Battery : public QObject QMap states; Component primaryPod; Component secondaryPod; -}; \ No newline at end of file +}; diff --git a/linux/deviceinfo.hpp b/linux/deviceinfo.hpp index 7a4c7a0c4..a3ac8affd 100644 --- a/linux/deviceinfo.hpp +++ b/linux/deviceinfo.hpp @@ -197,7 +197,12 @@ class DeviceInfo : public QObject int leftLevel = getBattery()->getState(Battery::Component::Left).level; int rightLevel = getBattery()->getState(Battery::Component::Right).level; int caseLevel = getBattery()->getState(Battery::Component::Case).level; - setBatteryStatus(QString("Left: %1%, Right: %2%, Case: %3%").arg(leftLevel).arg(rightLevel).arg(caseLevel)); + if (getBattery()->getPrimaryPod() == Battery::Component::Headset) { + int headsetLevel = getBattery()->getState(Battery::Component::Headset).level; + setBatteryStatus(QString("Headset: %1%").arg(headsetLevel)); + } else { + setBatteryStatus(QString("Left: %1%, Right: %2%, Case: %3%").arg(leftLevel).arg(rightLevel).arg(caseLevel)); + } } signals: @@ -229,4 +234,4 @@ class DeviceInfo : public QObject QString m_manufacturer; QString m_bluetoothAddress; EarDetection *m_earDetection; -}; \ No newline at end of file +}; diff --git a/linux/enums.h b/linux/enums.h index 347e33805..815415db4 100644 --- a/linux/enums.h +++ b/linux/enums.h @@ -85,11 +85,23 @@ namespace AirpodsTrayApp return {"podpro.png", "podpro_case.png"}; case AirPodsModel::AirPodsMaxLightning: case AirPodsModel::AirPodsMaxUSBC: - return {"max.png", "max_case.png"}; + return {"podmax.png", "max_case.png"}; default: return {"pod.png", "pod_case.png"}; // Default icon for unknown models } } + // TODO: Only used for parseEncryptedPacket for battery status. Is it possible to determine this + // from the data in the packet rather than by model? i.e number of batteries + inline bool isModelHeadset(AirPodsModel model) { + switch (model) { + case AirPodsModel::AirPodsMaxLightning: + case AirPodsModel::AirPodsMaxUSBC: + return true; + default: + return false; + } + } + } -} \ No newline at end of file +} diff --git a/linux/hearing-aid-adjustments.py b/linux/hearing-aid-adjustments.py index 2312b8ed4..85420b12b 100644 --- a/linux/hearing-aid-adjustments.py +++ b/linux/hearing-aid-adjustments.py @@ -1,10 +1,13 @@ -import sys +import logging +import signal import socket import struct +import sys import threading +from socket import socket as Socket from queue import Queue -import logging -import signal +from threading import Thread +from typing import Any, Dict, List, Optional # Configure logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') @@ -12,47 +15,47 @@ from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QSlider, QCheckBox, QPushButton, QLineEdit, QFormLayout, QGridLayout from PyQt5.QtCore import Qt, QTimer, pyqtSignal, QObject -OPCODE_READ_REQUEST = 0x0A -OPCODE_WRITE_REQUEST = 0x12 -OPCODE_HANDLE_VALUE_NTF = 0x1B +OPCODE_READ_REQUEST: int = 0x0A +OPCODE_WRITE_REQUEST: int = 0x12 +OPCODE_HANDLE_VALUE_NTF: int = 0x1B -ATT_HANDLES = { +ATT_HANDLES: Dict[str, int] = { 'TRANSPARENCY': 0x18, 'LOUD_SOUND_REDUCTION': 0x1B, 'HEARING_AID': 0x2A, } -ATT_CCCD_HANDLES = { +ATT_CCCD_HANDLES: Dict[str, int] = { 'TRANSPARENCY': ATT_HANDLES['TRANSPARENCY'] + 1, 'LOUD_SOUND_REDUCTION': ATT_HANDLES['LOUD_SOUND_REDUCTION'] + 1, 'HEARING_AID': ATT_HANDLES['HEARING_AID'] + 1, } -PSM_ATT = 31 +PSM_ATT: int = 31 class ATTManager: - def __init__(self, mac_address): - self.mac_address = mac_address - self.sock = None - self.responses = Queue() - self.listeners = {} - self.notification_thread = None - self.running = False + def __init__(self, mac_address: str) -> None: + self.mac_address: str = mac_address + self.sock: Optional[Socket] = None + self.responses: Queue = Queue() + self.listeners: Dict[int, List[Any]] = {} + self.notification_thread: Optional[Thread] = None + self.running: bool = False # Avoid logging full MAC address to prevent sensitive data exposure - mac_tail = ':'.join(mac_address.split(':')[-2:]) if isinstance(mac_address, str) and ':' in mac_address else '[redacted]' + mac_tail: str = ':'.join(mac_address.split(':')[-2:]) if isinstance(mac_address, str) and ':' in mac_address else '[redacted]' logging.info(f"ATTManager initialized") - def connect(self): + def connect(self) -> None: logging.info("Attempting to connect to ATT socket") - self.sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP) + self.sock = Socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP) self.sock.connect((self.mac_address, PSM_ATT)) self.sock.settimeout(0.1) self.running = True - self.notification_thread = threading.Thread(target=self._listen_notifications) + self.notification_thread = Thread(target=self._listen_notifications) self.notification_thread.start() logging.info("Connected to ATT socket") - def disconnect(self): + def disconnect(self) -> None: logging.info("Disconnecting from ATT socket") self.running = False if self.sock: @@ -63,37 +66,37 @@ def disconnect(self): self.notification_thread.join(timeout=1.0) logging.info("Disconnected from ATT socket") - def register_listener(self, handle, listener): + def register_listener(self, handle: int, listener: Any) -> None: if handle not in self.listeners: self.listeners[handle] = [] self.listeners[handle].append(listener) logging.debug(f"Registered listener for handle {handle}") - def unregister_listener(self, handle, listener): + def unregister_listener(self, handle: int, listener: Any) -> None: if handle in self.listeners: self.listeners[handle].remove(listener) logging.debug(f"Unregistered listener for handle {handle}") - def enable_notifications(self, handle): + def enable_notifications(self, handle: Any) -> None: self.write_cccd(handle, b'\x01\x00') logging.info(f"Enabled notifications for handle {handle.name}") - def read(self, handle): - handle_value = ATT_HANDLES[handle.name] - lsb = handle_value & 0xFF - msb = (handle_value >> 8) & 0xFF - pdu = bytes([OPCODE_READ_REQUEST, lsb, msb]) + def read(self, handle: Any) -> bytes: + handle_value: int = ATT_HANDLES[handle.name] + lsb: int = handle_value & 0xFF + msb: int = (handle_value >> 8) & 0xFF + pdu: bytes = bytes([OPCODE_READ_REQUEST, lsb, msb]) logging.debug(f"Sending read request for handle {handle.name}: {pdu.hex()}") self._write_raw(pdu) - response = self._read_response() + response: bytes = self._read_response() logging.debug(f"Read response for handle {handle.name}: {response.hex()}") return response - def write(self, handle, value): - handle_value = ATT_HANDLES[handle.name] - lsb = handle_value & 0xFF - msb = (handle_value >> 8) & 0xFF - pdu = bytes([OPCODE_WRITE_REQUEST, lsb, msb]) + value + def write(self, handle: Any, value: bytes) -> None: + handle_value: int = ATT_HANDLES[handle.name] + lsb: int = handle_value & 0xFF + msb: int = (handle_value >> 8) & 0xFF + pdu: bytes = bytes([OPCODE_WRITE_REQUEST, lsb, msb]) + value logging.debug(f"Sending write request for handle {handle.name}: {pdu.hex()}") self._write_raw(pdu) try: @@ -102,11 +105,11 @@ def write(self, handle, value): except: logging.warning(f"No write response received for handle {handle.name}") - def write_cccd(self, handle, value): - handle_value = ATT_CCCD_HANDLES[handle.name] - lsb = handle_value & 0xFF - msb = (handle_value >> 8) & 0xFF - pdu = bytes([OPCODE_WRITE_REQUEST, lsb, msb]) + value + def write_cccd(self, handle: Any, value: bytes) -> None: + handle_value: int = ATT_CCCD_HANDLES[handle.name] + lsb: int = handle_value & 0xFF + msb: int = (handle_value >> 8) & 0xFF + pdu: bytes = bytes([OPCODE_WRITE_REQUEST, lsb, msb]) + value logging.debug(f"Sending CCCD write request for handle {handle.name}: {pdu.hex()}") self._write_raw(pdu) try: @@ -115,42 +118,42 @@ def write_cccd(self, handle, value): except: logging.warning(f"No CCCD write response received for handle {handle.name}") - def _write_raw(self, pdu): + def _write_raw(self, pdu: bytes) -> None: self.sock.send(pdu) logging.debug(f"Sent PDU: {pdu.hex()}") - def _read_pdu(self): + def _read_pdu(self) -> Optional[bytes]: try: - data = self.sock.recv(512) + data: bytes = self.sock.recv(512) logging.debug(f"Received PDU: {data.hex()}") return data - except socket.timeout: + except TimeoutError: return None except: raise - def _read_response(self, timeout=2.0): + def _read_response(self, timeout: float = 2.0) -> bytes: try: - response = self.responses.get(timeout=timeout)[1:] # Skip opcode + response: bytes = self.responses.get(timeout=timeout)[1:] # Skip opcode logging.debug(f"Response received: {response.hex()}") return response except: logging.error("No response received within timeout") raise Exception("No response received") - def _listen_notifications(self): + def _listen_notifications(self) -> None: logging.info("Starting notification listener thread") while self.running: try: - pdu = self._read_pdu() + pdu: Optional[bytes] = self._read_pdu() except: break if pdu is None: continue if len(pdu) > 0 and pdu[0] == OPCODE_HANDLE_VALUE_NTF: logging.debug(f"Notification PDU received: {pdu.hex()}") - handle = pdu[1] | (pdu[2] << 8) - value = pdu[3:] + handle: int = pdu[1] | (pdu[2] << 8) + value: bytes = pdu[3:] logging.debug(f"Notification for handle {handle}: {value.hex()}") if handle in self.listeners: for listener in self.listeners[handle]: @@ -165,36 +168,36 @@ def _listen_notifications(self): logging.error(f"Reconnection failed: {e}") class HearingAidSettings: - def __init__(self, left_eq, right_eq, left_amp, right_amp, left_tone, right_tone, - left_conv, right_conv, left_anr, right_anr, net_amp, balance, own_voice): - self.left_eq = left_eq - self.right_eq = right_eq - self.left_amplification = left_amp - self.right_amplification = right_amp - self.left_tone = left_tone - self.right_tone = right_tone - self.left_conversation_boost = left_conv - self.right_conversation_boost = right_conv - self.left_ambient_noise_reduction = left_anr - self.right_ambient_noise_reduction = right_anr - self.net_amplification = net_amp - self.balance = balance - self.own_voice_amplification = own_voice + def __init__(self, left_eq: List[float], right_eq: List[float], left_amp: float, right_amp: float, left_tone: float, right_tone: float, + left_conv: bool, right_conv: bool, left_anr: float, right_anr: float, net_amp: float, balance: float, own_voice: float) -> None: + self.left_eq: List[float] = left_eq + self.right_eq: List[float] = right_eq + self.left_amplification: float = left_amp + self.right_amplification: float = right_amp + self.left_tone: float = left_tone + self.right_tone: float = right_tone + self.left_conversation_boost: bool = left_conv + self.right_conversation_boost: bool = right_conv + self.left_ambient_noise_reduction: float = left_anr + self.right_ambient_noise_reduction: float = right_anr + self.net_amplification: float = net_amp + self.balance: float = balance + self.own_voice_amplification: float = own_voice logging.debug(f"HearingAidSettings created: amp={net_amp}, balance={balance}, tone={left_tone}, anr={left_anr}, conv={left_conv}") -def parse_hearing_aid_settings(data): +def parse_hearing_aid_settings(data: bytes) -> Optional[HearingAidSettings]: logging.debug(f"Parsing hearing aid settings from data: {data.hex()}") if len(data) < 104: logging.warning("Data too short for parsing") return None - buffer = data - offset = 0 + buffer: bytes = data + offset: int = 0 offset += 4 logging.info(f"Parsing hearing aid settings, starting read at offset 4, value: {buffer[offset]:02x}") - left_eq = [] + left_eq: List[float] = [] for i in range(8): val, = struct.unpack(' None: logging.info("Sending hearing aid settings") - data = att_manager.read(type('Handle', (), {'name': 'HEARING_AID'})()) + data: bytes = att_manager.read(type('Handle', (), {'name': 'HEARING_AID'})()) if len(data) < 104: logging.error("Read data too short for sending settings") return - buffer = bytearray(data) + buffer: bytearray = bytearray(data) # Modify byte at index 2 to 0x64 buffer[2] = 0x64 @@ -272,16 +275,16 @@ def send_hearing_aid_settings(att_manager, settings): logging.info("Hearing aid settings sent") class SignalEmitter(QObject): - update_ui = pyqtSignal(HearingAidSettings) + update_ui: pyqtSignal = pyqtSignal(HearingAidSettings) class HearingAidApp(QWidget): - def __init__(self, mac_address): + def __init__(self, mac_address: str) -> None: super().__init__() - self.mac_address = mac_address - self.att_manager = ATTManager(mac_address) - self.emitter = SignalEmitter() + self.mac_address: str = mac_address + self.att_manager: ATTManager = ATTManager(mac_address) + self.emitter: SignalEmitter = SignalEmitter() self.emitter.update_ui.connect(self.on_update_ui) - self.debounce_timer = QTimer() + self.debounce_timer: QTimer = QTimer() self.debounce_timer.setSingleShot(True) self.debounce_timer.timeout.connect(self.send_settings) logging.info("HearingAidConfig initialized") @@ -289,25 +292,25 @@ def __init__(self, mac_address): self.init_ui() self.connect_att() - def init_ui(self): + def init_ui(self) -> None: logging.debug("Initializing UI") self.setWindowTitle("Hearing Aid Adjustments") - layout = QVBoxLayout() + layout: QVBoxLayout = QVBoxLayout() # EQ Inputs - eq_layout = QGridLayout() - self.left_eq_inputs = [] - self.right_eq_inputs = [] + eq_layout: QGridLayout = QGridLayout() + self.left_eq_inputs: List[QLineEdit] = [] + self.right_eq_inputs: List[QLineEdit] = [] - eq_labels = ["250Hz", "500Hz", "1kHz", "2kHz", "3kHz", "4kHz", "6kHz", "8kHz"] + eq_labels: List[str] = ["250Hz", "500Hz", "1kHz", "2kHz", "3kHz", "4kHz", "6kHz", "8kHz"] eq_layout.addWidget(QLabel("Frequency"), 0, 0) eq_layout.addWidget(QLabel("Left"), 0, 1) eq_layout.addWidget(QLabel("Right"), 0, 2) for i, label in enumerate(eq_labels): eq_layout.addWidget(QLabel(label), i + 1, 0) - left_input = QLineEdit() - right_input = QLineEdit() + left_input: QLineEdit = QLineEdit() + right_input: QLineEdit = QLineEdit() left_input.setPlaceholderText("Left") right_input.setPlaceholderText("Right") self.left_eq_inputs.append(left_input) @@ -315,52 +318,52 @@ def init_ui(self): eq_layout.addWidget(left_input, i + 1, 1) eq_layout.addWidget(right_input, i + 1, 2) - eq_group = QWidget() + eq_group: QWidget = QWidget() eq_group.setLayout(eq_layout) layout.addWidget(QLabel("Loss, in dBHL")) layout.addWidget(eq_group) # Amplification - self.amp_slider = QSlider(Qt.Horizontal) + self.amp_slider: QSlider = QSlider(Qt.Horizontal) self.amp_slider.setRange(-100, 100) self.amp_slider.setValue(50) layout.addWidget(QLabel("Amplification")) layout.addWidget(self.amp_slider) # Balance - self.balance_slider = QSlider(Qt.Horizontal) + self.balance_slider: QSlider = QSlider(Qt.Horizontal) self.balance_slider.setRange(-100, 100) self.balance_slider.setValue(50) layout.addWidget(QLabel("Balance")) layout.addWidget(self.balance_slider) # Tone - self.tone_slider = QSlider(Qt.Horizontal) + self.tone_slider: QSlider = QSlider(Qt.Horizontal) self.tone_slider.setRange(-100, 100) self.tone_slider.setValue(50) layout.addWidget(QLabel("Tone")) layout.addWidget(self.tone_slider) # Ambient Noise Reduction - self.anr_slider = QSlider(Qt.Horizontal) + self.anr_slider: QSlider = QSlider(Qt.Horizontal) self.anr_slider.setRange(0, 100) self.anr_slider.setValue(0) layout.addWidget(QLabel("Ambient Noise Reduction")) layout.addWidget(self.anr_slider) # Conversation Boost - self.conv_checkbox = QCheckBox("Conversation Boost") + self.conv_checkbox: QCheckBox = QCheckBox("Conversation Boost") layout.addWidget(self.conv_checkbox) # Own Voice Amplification - self.own_voice_slider = QSlider(Qt.Horizontal) + self.own_voice_slider: QSlider = QSlider(Qt.Horizontal) self.own_voice_slider.setRange(0, 100) self.own_voice_slider.setValue(50) # layout.addWidget(QLabel("Own Voice Amplification")) # layout.addWidget(self.own_voice_slider) # seems to have no effect # Reset button - self.reset_button = QPushButton("Reset") + self.reset_button: QPushButton = QPushButton("Reset") layout.addWidget(self.reset_button) # Connect signals @@ -377,15 +380,15 @@ def init_ui(self): self.setLayout(layout) logging.debug("UI initialized") - def connect_att(self): + def connect_att(self) -> None: logging.info("Connecting to ATT in UI") try: self.att_manager.connect() self.att_manager.enable_notifications(type('Handle', (), {'name': 'HEARING_AID'})()) self.att_manager.register_listener(ATT_HANDLES['HEARING_AID'], self.on_notification) # Initial read - data = self.att_manager.read(type('Handle', (), {'name': 'HEARING_AID'})()) - settings = parse_hearing_aid_settings(data) + data: bytes = self.att_manager.read(type('Handle', (), {'name': 'HEARING_AID'})()) + settings: Optional[HearingAidSettings] = parse_hearing_aid_settings(data) if settings: self.emitter.update_ui.emit(settings) logging.info("Initial settings loaded") @@ -396,13 +399,13 @@ def connect_att(self): else: logging.error(f"Connection failed: {e}") - def on_notification(self, value): + def on_notification(self, value: bytes) -> None: logging.debug("Notification received") - settings = parse_hearing_aid_settings(value) + settings: Optional[HearingAidSettings] = parse_hearing_aid_settings(value) if settings: self.emitter.update_ui.emit(settings) - def on_update_ui(self, settings): + def on_update_ui(self, settings: HearingAidSettings) -> None: logging.debug("Updating UI with settings") self.amp_slider.setValue(int(settings.net_amplification * 100)) self.balance_slider.setValue(int(settings.balance * 100)) @@ -416,30 +419,30 @@ def on_update_ui(self, settings): for i, value in enumerate(settings.right_eq): self.right_eq_inputs[i].setText(f"{value:.2f}") - def on_value_changed(self): + def on_value_changed(self) -> None: logging.debug("UI value changed, starting debounce") self.debounce_timer.start(100) - def send_settings(self): + def send_settings(self) -> None: logging.info("Sending settings from UI") - amp = self.amp_slider.value() / 100.0 - balance = self.balance_slider.value() / 100.0 - tone = self.tone_slider.value() / 100.0 - anr = self.anr_slider.value() / 100.0 - conv = self.conv_checkbox.isChecked() - own_voice = self.own_voice_slider.value() / 100.0 + amp: float = self.amp_slider.value() / 100.0 + balance: float = self.balance_slider.value() / 100.0 + tone: float = self.tone_slider.value() / 100.0 + anr: float = self.anr_slider.value() / 100.0 + conv: bool = self.conv_checkbox.isChecked() + own_voice: float = self.own_voice_slider.value() / 100.0 - left_amp = amp + (0.5 - balance) * amp * 2 if balance < 0 else amp - right_amp = amp + (balance - 0.5) * amp * 2 if balance > 0 else amp + left_amp: float = amp + (0.5 - balance) * amp * 2 if balance < 0 else amp + right_amp: float = amp + (balance - 0.5) * amp * 2 if balance > 0 else amp - left_eq = [float(input_box.text() or 0) for input_box in self.left_eq_inputs] - right_eq = [float(input_box.text() or 0) for input_box in self.right_eq_inputs] + left_eq: List[float] = [float(input_box.text() or 0) for input_box in self.left_eq_inputs] + right_eq: List[float] = [float(input_box.text() or 0) for input_box in self.right_eq_inputs] - settings = HearingAidSettings( + settings: HearingAidSettings = HearingAidSettings( left_eq, right_eq, left_amp, right_amp, tone, tone, conv, conv, anr, anr, amp, balance, own_voice ) - threading.Thread(target=send_hearing_aid_settings, args=(self.att_manager, settings)).start() + Thread(target=send_hearing_aid_settings, args=(self.att_manager, settings)).start() def reset_settings(self): logging.debug("Resetting settings to defaults") @@ -451,26 +454,25 @@ def reset_settings(self): self.own_voice_slider.setValue(50) self.on_value_changed() - def closeEvent(self, event): + def closeEvent(self, event: Any) -> None: logging.info("Closing app") self.att_manager.disconnect() event.accept() if __name__ == "__main__": - mac = None if len(sys.argv) != 2: logging.error("Usage: python hearing-aid-adjustments.py ") sys.exit(1) - mac = sys.argv[1] - mac_regex = r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$' + mac: str = sys.argv[1] + mac_regex: str = r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$' import re if not re.match(mac_regex, mac): logging.error("Invalid MAC address format") sys.exit(1) logging.info(f"Starting app") - app = QApplication(sys.argv) + app: QApplication = QApplication(sys.argv) - def quit_app(signum, frame): + def quit_app(signum: int, frame: Any) -> None: app.quit() signal.signal(signal.SIGINT, quit_app) diff --git a/linux/librepods-ctl.cpp b/linux/librepods-ctl.cpp new file mode 100644 index 000000000..71c32ce95 --- /dev/null +++ b/linux/librepods-ctl.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +int main(int argc, char *argv[]) { + QCoreApplication app(argc, argv); + + if (argc < 2) { + QTextStream(stderr) << "Usage: librepods-ctl \n" + << "Commands:\n" + << " noise:off Disable noise control\n" + << " noise:anc Enable Active Noise Cancellation\n" + << " noise:transparency Enable Transparency mode\n" + << " noise:adaptive Enable Adaptive mode\n"; + return 1; + } + + QLocalSocket socket; + socket.connectToServer("app_server"); + + if (!socket.waitForConnected(500)) { + QTextStream(stderr) << "Could not connect to librepods (is it running?)\n"; + return 1; + } + + socket.write(QByteArray(argv[1])); + socket.flush(); + socket.waitForBytesWritten(200); + socket.disconnectFromServer(); + return 0; +} diff --git a/linux/main.cpp b/linux/main.cpp index 9e8d7b9ad..7b1826b49 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -12,6 +12,10 @@ #include #include #include +#include +#include +#include +#include #include "airpods_packets.h" #include "logger.h" @@ -666,7 +670,7 @@ private slots: else if (data.startsWith(AirPodsPackets::Parse::FEATURES_ACK)) { writePacketToSocket(AirPodsPackets::Connection::REQUEST_NOTIFICATIONS, "Request notifications packet written: "); - + QTimer::singleShot(2000, this, [this]() { if (m_deviceInfo->batteryStatus().isEmpty()) { writePacketToSocket(AirPodsPackets::Connection::REQUEST_NOTIFICATIONS, "Request notifications packet written: "); @@ -718,7 +722,7 @@ private slots: mediaController->handleEarDetection(m_deviceInfo->getEarDetection()); } // Battery Status - else if (data.size() == 22 && data.startsWith(AirPodsPackets::Parse::BATTERY_STATUS)) + else if ((data.size() == 22 || data.size() == 12) && data.startsWith(AirPodsPackets::Parse::BATTERY_STATUS)) { m_deviceInfo->getBattery()->parsePacket(data); m_deviceInfo->updateBatteryStatus(); @@ -766,7 +770,7 @@ private slots: } QBluetoothAddress phoneAddress("00:00:00:00:00:00"); // Default address, will be overwritten if PHONE_MAC_ADDRESS is set QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); - + if (!env.value("PHONE_MAC_ADDRESS").isEmpty()) { phoneAddress = QBluetoothAddress(env.value("PHONE_MAC_ADDRESS")); @@ -875,7 +879,7 @@ private slots: if (BLEUtils::isValidIrkRpa(m_deviceInfo->magicAccIRK(), device.address)) { m_deviceInfo->setModel(device.modelName); auto decryptet = BLEUtils::decryptLastBytes(device.encryptedPayload, m_deviceInfo->magicAccEncKey()); - m_deviceInfo->getBattery()->parseEncryptedPacket(decryptet, device.primaryLeft, device.isThisPodInTheCase); + m_deviceInfo->getBattery()->parseEncryptedPacket(decryptet, device.primaryLeft, device.isThisPodInTheCase, isModelHeadset(m_deviceInfo->model())); m_deviceInfo->getEarDetection()->overrideEarDetectionStatus(device.isPrimaryInEar, device.isSecondaryInEar); } } @@ -987,31 +991,44 @@ private slots: int main(int argc, char *argv[]) { QApplication app(argc, argv); - QSharedMemory sharedMemory; - sharedMemory.setKey("TcpServer-Key2"); + // Load translations + QTranslator *translator = new QTranslator(&app); + QString locale = QLocale::system().name(); - // Check if app is already open - if(sharedMemory.create(1) == false) - { - LOG_INFO("Another instance already running! Opening App Window Instead"); - QLocalSocket socket; - // Connect to the original app, then trigger the reopen signal - socket.connectToServer("app_server"); - if (socket.waitForConnected(500)) { - socket.write("reopen"); - socket.flush(); - socket.waitForBytesWritten(500); - socket.disconnectFromServer(); - app.exit(); // exit; process already running - return 0; - } - else - { - // Failed connection, log and open the app (assume it's not running) - LOG_ERROR("Failed to connect to the original app instance. Assuming it is not running."); - LOG_DEBUG("Socket error: " << socket.errorString()); + // Try to load translation from various locations + QStringList translationPaths = { + QCoreApplication::applicationDirPath() + "/translations", + QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/librepods/translations", + "/usr/share/librepods/translations", + "/usr/local/share/librepods/translations" + }; + + for (const QString &path : translationPaths) { + if (translator->load("librepods_" + locale, path)) { + app.installTranslator(translator); + break; } } + + QLocalServer::removeServer("app_server"); + + QFile stale("/tmp/app_server"); + if (stale.exists()) + stale.remove(); + + QLocalSocket socket_check; + socket_check.connectToServer("app_server"); + + if (socket_check.waitForConnected(300)) { + LOG_INFO("Another instance already running! Reopening window..."); + + socket_check.write("reopen"); + socket_check.flush(); + socket_check.waitForBytesWritten(200); + socket_check.disconnectFromServer(); + + return 0; + } app.setDesktopFileName("me.kavishdevar.librepods"); app.setQuitOnLastWindowClosed(false); @@ -1072,6 +1089,18 @@ int main(int argc, char *argv[]) { trayApp->loadMainModule(); } } + else if (msg == "noise:off") { + trayApp->setNoiseControlModeInt(0); + } + else if (msg == "noise:anc") { + trayApp->setNoiseControlModeInt(1); + } + else if (msg == "noise:transparency") { + trayApp->setNoiseControlModeInt(2); + } + else if (msg == "noise:adaptive") { + trayApp->setNoiseControlModeInt(3); + } else { LOG_ERROR("Unknown message received: " << msg); @@ -1083,7 +1112,7 @@ int main(int argc, char *argv[]) { LOG_ERROR("Failed to connect to the duplicate app instance"); LOG_DEBUG("Connection error: " << socket->errorString()); }); - + // Handle server-level errors QObject::connect(&server, &QLocalServer::serverError, [&]() { LOG_ERROR("Server failed to accept a new connection"); @@ -1092,8 +1121,16 @@ int main(int argc, char *argv[]) { }); QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]() { - LOG_DEBUG("Application is about to quit. Cleaning up..."); - sharedMemory.detach(); + LOG_DEBUG("Application quitting. Cleaning up local server..."); + + if (server.isListening()) { + server.close(); + } + + QLocalServer::removeServer("app_server"); + QFile stale("/tmp/app_server"); + if (stale.exists()) + stale.remove(); }); return app.exec(); } diff --git a/linux/media/mediacontroller.cpp b/linux/media/mediacontroller.cpp index 531efd5b2..078129c5a 100644 --- a/linux/media/mediacontroller.cpp +++ b/linux/media/mediacontroller.cpp @@ -101,40 +101,54 @@ bool MediaController::isActiveOutputDeviceAirPods() { } void MediaController::handleConversationalAwareness(const QByteArray &data) { - LOG_DEBUG("Handling conversational awareness data: " << data.toHex()); - bool lowered = data[9] == 0x01; - LOG_INFO("Conversational awareness: " << (lowered ? "enabled" : "disabled")); - - if (lowered) { - if (initialVolume == -1 && isActiveOutputDeviceAirPods()) { - QString defaultSink = m_pulseAudio->getDefaultSink(); - initialVolume = m_pulseAudio->getSinkVolume(defaultSink); - if (initialVolume == -1) { - LOG_ERROR("Failed to get initial volume"); + if (data.size() < 10) { + LOG_ERROR("Invalid conversational awareness packet"); return; - } - LOG_DEBUG("Initial volume: " << initialVolume << "%"); - } - QString defaultSink = m_pulseAudio->getDefaultSink(); - int targetVolume = initialVolume * 0.20; - if (m_pulseAudio->setSinkVolume(defaultSink, targetVolume)) { - LOG_INFO("Volume lowered to 0.20 of initial which is " << targetVolume << "%"); - } else { - LOG_ERROR("Failed to lower volume"); } - } else { - if (initialVolume != -1 && isActiveOutputDeviceAirPods()) { - QString defaultSink = m_pulseAudio->getDefaultSink(); - if (m_pulseAudio->setSinkVolume(defaultSink, initialVolume)) { - LOG_INFO("Volume restored to " << initialVolume << "%"); - } else { - LOG_ERROR("Failed to restore volume"); - } - initialVolume = -1; + + uint8_t flag = (uint8_t)data[9]; + + switch (flag) { + case 0x01: + LOG_INFO("Conversational awareness event: voice detected"); + + if (initialVolume == -1 && isActiveOutputDeviceAirPods()) { + QString sink = m_pulseAudio->getDefaultSink(); + initialVolume = m_pulseAudio->getSinkVolume(sink); + LOG_DEBUG("Initial volume saved: " << initialVolume << "%"); + } + + if (initialVolume != -1) { + QString sink = m_pulseAudio->getDefaultSink(); + int target = initialVolume * 0.20; + m_pulseAudio->setSinkVolume(sink, target); + LOG_INFO("Volume lowered to " << target << "%"); + } + break; + + case 0x08: + LOG_INFO("Conversational awareness disabled"); + initialVolume = -1; + break; + + case 0x09: + LOG_INFO("Conversational awareness enabled"); + break; + + default: + LOG_INFO("Conversational awareness event: voice ended"); + + if (initialVolume != -1 && isActiveOutputDeviceAirPods()) { + QString sink = m_pulseAudio->getDefaultSink(); + m_pulseAudio->setSinkVolume(sink, initialVolume); + LOG_INFO("Volume restored to " << initialVolume << "%"); + initialVolume = -1; + } + break; } - } } + bool MediaController::isA2dpProfileAvailable() { if (m_deviceOutputName.isEmpty()) { return false; diff --git a/linux/translations/librepods_it_IT.ts b/linux/translations/librepods_it_IT.ts new file mode 100644 index 000000000..f0970b205 --- /dev/null +++ b/linux/translations/librepods_it_IT.ts @@ -0,0 +1,151 @@ + + + + + Main + + Connected + Connesso + + + Disconnected + Disconnesso + + + Off + Non attivo + + + Noise Cancellation + Cancellazione rumore + + + Transparency + Trasparenza + + + Adaptive + Adattivo + + + Adaptive Noise Level: + Livello rumore adattivo: + + + Conversational Awareness + Rilevamento conversazione + + + Hearing Aid + Apparecchio acustico + + + Settings + Impostazioni + + + Pause Behavior When Removing AirPods: + Pausa alla rimozione delle AirPods: + + + One Removed + Una rimossa + + + Both Removed + Entrambe rimosse + + + Never + Mai + + + Cross-Device Connectivity with Android + Connettività multi-dispositivo con Android + + + Auto-Start on Login + Avvio automatico all'accesso + + + Enable System Notifications + Abilita notifiche di sistema + + + One Bud ANC Mode + Modalità ANC singolo auricolare + + + Enable ANC when using one AirPod +(More noise reduction, but uses more battery) + Abilita ANC con un solo AirPod +(Maggiore riduzione rumore, ma consuma più batteria) + + + Bluetooth Retry Attempts: + Tentativi riprova Bluetooth: + + + Rename + Rinomina + + + Change Phone MAC + Cambia MAC Telefono + + + Show Magic Cloud Keys QR + Mostra QR Magic Cloud Keys + + + + TrayIconManager + + Battery Status: + Stato batteria: + + + Open + Apri + + + Settings + Impostazioni + + + Toggle Conversational Awareness + Attiva/Disattiva Rilevamento conversazione + + + Adaptive + Adattivo + + + Transparency + Trasparenza + + + Noise Cancellation + Cancellazione rumore + + + Off + Non attivo + + + Quit + Esci + + + + AirPodsTrayApp + + AirPods Disconnected + AirPods disconnesse + + + Your AirPods have been disconnected + Le tue AirPods sono state disconnesse + + + diff --git a/linux/translations/librepods_tr.ts b/linux/translations/librepods_tr.ts new file mode 100644 index 000000000..34521ab80 --- /dev/null +++ b/linux/translations/librepods_tr.ts @@ -0,0 +1,151 @@ + + + + + Main + + Connected + Bağlı + + + Disconnected + Bağlantı Kesildi + + + Off + Kapalı + + + Noise Cancellation + Gürültü Engelleme + + + Transparency + Şeffaflık + + + Adaptive + Uyarlanabilir + + + Adaptive Noise Level: + Uyarlanabilir Gürültü Seviyesi: + + + Conversational Awareness + Konuşma Farkındalığı + + + Hearing Aid + İşitme Cihazı + + + Settings + Ayarlar + + + Pause Behavior When Removing AirPods: + AirPods Çıkarıldığında Duraklatma Davranışı: + + + One Removed + Biri Çıkarıldığında + + + Both Removed + İkisi de Çıkarıldığında + + + Never + Asla + + + Cross-Device Connectivity with Android + Android ile Çapraz Cihaz Bağlantısı + + + Auto-Start on Login + Oturum Açıldığında Otomatik Başlat + + + Enable System Notifications + Sistem Bildirimlerini Etkinleştir + + + One Bud ANC Mode + Tek Kulaklık ANC Modu + + + Enable ANC when using one AirPod +(More noise reduction, but uses more battery) + Tek AirPod kullanırken ANC'yi etkinleştir +(Daha fazla gürültü azaltma, ancak daha fazla pil kullanır) + + + Bluetooth Retry Attempts: + Bluetooth Yeniden Deneme Sayısı: + + + Rename + Yeniden Adlandır + + + Change Phone MAC + Telefon MAC Adresini Değiştir + + + Show Magic Cloud Keys QR + Magic Cloud Anahtarları QR'ını Göster + + + + TrayIconManager + + Battery Status: + Pil Durumu: + + + Open + + + + Settings + Ayarlar + + + Toggle Conversational Awareness + Konuşma Farkındalığını Aç/Kapat + + + Adaptive + Uyarlanabilir + + + Transparency + Şeffaflık + + + Noise Cancellation + Gürültü Engelleme + + + Off + Kapalı + + + Quit + Çıkış + + + + AirPodsTrayApp + + AirPods Disconnected + AirPods Bağlantısı Kesildi + + + Your AirPods have been disconnected + AirPods'unuzun bağlantısı kesildi + + + diff --git a/linux/translations/librepods_zh_TW.ts b/linux/translations/librepods_zh_TW.ts new file mode 100644 index 000000000..5f33b8795 --- /dev/null +++ b/linux/translations/librepods_zh_TW.ts @@ -0,0 +1,151 @@ + + + + + Main + + Connected + 已連線 + + + Disconnected + 已中斷連線 + + + Off + 關閉 + + + Noise Cancellation + 降噪 + + + Transparency + 通透模式 + + + Adaptive + 自適應 + + + Adaptive Noise Level: + 自適應噪音等級: + + + Conversational Awareness + 對話感知 + + + Hearing Aid + 助聽器 + + + Settings + 設定 + + + Pause Behavior When Removing AirPods: + 取下 AirPods 時的暫停行為: + + + One Removed + 取下其中一只時 + + + Both Removed + 兩只都取下時 + + + Never + 永不 + + + Cross-Device Connectivity with Android + 與 Android 的跨裝置連線 + + + Auto-Start on Login + 登入時自動啟動 + + + Enable System Notifications + 啟用系統通知 + + + One Bud ANC Mode + 單耳 ANC 模式 + + + Enable ANC when using one AirPod +(More noise reduction, but uses more battery) + 使用一只 AirPod 時啟用 ANC +(更多降噪效果,但更耗電) + + + Bluetooth Retry Attempts: + Bluetooth 重試次數: + + + Rename + 重新命名 + + + Change Phone MAC + 變更手機 MAC 位址 + + + Show Magic Cloud Keys QR + 顯示 Magic Cloud Key QR 碼 + + + + TrayIconManager + + Battery Status: + 電池狀態: + + + Open + 開啟 + + + Settings + 設定 + + + Toggle Conversational Awareness + 切換對話感知 + + + Adaptive + 自適應 + + + Transparency + 通透模式 + + + Noise Cancellation + 降噪 + + + Off + 關閉 + + + Quit + 結束 + + + + AirPodsTrayApp + + AirPods Disconnected + AirPods 已中斷連線 + + + Your AirPods have been disconnected + 你的 AirPods 已中斷連線 + + + diff --git a/linux/trayiconmanager.cpp b/linux/trayiconmanager.cpp index 57c0a68b5..738feecf1 100644 --- a/linux/trayiconmanager.cpp +++ b/linux/trayiconmanager.cpp @@ -36,7 +36,7 @@ void TrayIconManager::showNotification(const QString &title, const QString &mess void TrayIconManager::TrayIconManager::updateBatteryStatus(const QString &status) { - trayIcon->setToolTip("Battery Status: " + status); + trayIcon->setToolTip(tr("Battery Status: ") + status); updateIconFromBattery(status); } @@ -57,20 +57,20 @@ void TrayIconManager::updateConversationalAwareness(bool enabled) void TrayIconManager::setupMenuActions() { // Open action - QAction *openAction = new QAction("Open", trayMenu); + QAction *openAction = new QAction(tr("Open"), trayMenu); trayMenu->addAction(openAction); connect(openAction, &QAction::triggered, qApp, [this](){emit openApp();}); // Settings Menu - QAction *settingsMenu = new QAction("Settings", trayMenu); + QAction *settingsMenu = new QAction(tr("Settings"), trayMenu); trayMenu->addAction(settingsMenu); connect(settingsMenu, &QAction::triggered, qApp, [this](){emit openSettings();}); trayMenu->addSeparator(); // Conversational Awareness Toggle - caToggleAction = new QAction("Toggle Conversational Awareness", trayMenu); + caToggleAction = new QAction(tr("Toggle Conversational Awareness"), trayMenu); caToggleAction->setCheckable(true); trayMenu->addAction(caToggleAction); connect(caToggleAction, &QAction::triggered, this, [this](bool checked) @@ -81,10 +81,10 @@ void TrayIconManager::setupMenuActions() // Noise Control Options noiseControlGroup = new QActionGroup(trayMenu); const QPair noiseOptions[] = { - {"Adaptive", NoiseControlMode::Adaptive}, - {"Transparency", NoiseControlMode::Transparency}, - {"Noise Cancellation", NoiseControlMode::NoiseCancellation}, - {"Off", NoiseControlMode::Off}}; + {tr("Adaptive"), NoiseControlMode::Adaptive}, + {tr("Transparency"), NoiseControlMode::Transparency}, + {tr("Noise Cancellation"), NoiseControlMode::NoiseCancellation}, + {tr("Off"), NoiseControlMode::Off}}; for (auto option : noiseOptions) { @@ -100,7 +100,7 @@ void TrayIconManager::setupMenuActions() trayMenu->addSeparator(); // Quit action - QAction *quitAction = new QAction("Quit", trayMenu); + QAction *quitAction = new QAction(tr("Quit"), trayMenu); trayMenu->addAction(quitAction); connect(quitAction, &QAction::triggered, qApp, &QApplication::quit); } @@ -109,25 +109,26 @@ void TrayIconManager::updateIconFromBattery(const QString &status) { int leftLevel = 0; int rightLevel = 0; + int minLevel = 0; if (!status.isEmpty()) { // Parse the battery status string QStringList parts = status.split(", "); - if (parts.size() >= 2) - { + if (parts.size() >= 2) { leftLevel = parts[0].split(": ")[1].replace("%", "").toInt(); rightLevel = parts[1].split(": ")[1].replace("%", "").toInt(); + minLevel = (leftLevel == 0) ? rightLevel : (rightLevel == 0) ? leftLevel + : qMin(leftLevel, rightLevel); + } else if (parts.size() == 1) { + minLevel = parts[0].split(": ")[1].replace("%", "").toInt(); } } - - int minLevel = (leftLevel == 0) ? rightLevel : (rightLevel == 0) ? leftLevel - : qMin(leftLevel, rightLevel); QPixmap pixmap(32, 32); pixmap.fill(Qt::transparent); QPainter painter(&pixmap); - painter.setPen(QApplication::palette().color(QPalette::WindowText)); + painter.setPen(Qt::white); painter.setFont(QFont("Arial", 12, QFont::Bold)); painter.drawText(pixmap.rect(), Qt::AlignCenter, QString::number(minLevel) + "%"); painter.end(); @@ -141,4 +142,5 @@ void TrayIconManager::onTrayIconActivated(QSystemTrayIcon::ActivationReason reas { emit trayClicked(); } -} \ No newline at end of file +} + diff --git a/root-module/META-INF/com/google/android/update-binary b/root-module-manual/META-INF/com/google/android/update-binary similarity index 100% rename from root-module/META-INF/com/google/android/update-binary rename to root-module-manual/META-INF/com/google/android/update-binary diff --git a/root-module/META-INF/com/google/android/updater-script b/root-module-manual/META-INF/com/google/android/updater-script similarity index 100% rename from root-module/META-INF/com/google/android/updater-script rename to root-module-manual/META-INF/com/google/android/updater-script diff --git a/root-module-manual/module.prop b/root-module-manual/module.prop new file mode 100644 index 000000000..aeaa4129c --- /dev/null +++ b/root-module-manual/module.prop @@ -0,0 +1,7 @@ +id=librepods +name=LibrePods +version=v0.2.6 +versionCode=46 +author=@kavishdevar +description=Installs LibrePods as a system app for granting BLUETOOTH_PRIVILEGED and MODIFY_PHONE_STATE permission for better integraion with android. +updateJson=https://raw.githubusercontent.com/kavishdevar/librepods/main/extras/update_nonpatch.json diff --git a/root-module-manual/system/etc/permissions/privapp-permissions-librepods.xml b/root-module-manual/system/etc/permissions/privapp-permissions-librepods.xml new file mode 100644 index 000000000..801c3edbb --- /dev/null +++ b/root-module-manual/system/etc/permissions/privapp-permissions-librepods.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/root-module/.gitignore b/root-module/.gitignore deleted file mode 100644 index 6d86d9566..000000000 --- a/root-module/.gitignore +++ /dev/null @@ -1 +0,0 @@ -system/ diff --git a/root-module/busybox/busybox-arm64 b/root-module/busybox/busybox-arm64 deleted file mode 100644 index 9a8246d17..000000000 Binary files a/root-module/busybox/busybox-arm64 and /dev/null differ diff --git a/root-module/busybox/xz b/root-module/busybox/xz deleted file mode 100644 index d414c7171..000000000 --- a/root-module/busybox/xz +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -exec /data/local/tmp/aln_unzip/busybox/busybox-arm64 xz "$@" diff --git a/root-module/customize.sh b/root-module/customize.sh deleted file mode 100644 index 9b01cd2f2..000000000 --- a/root-module/customize.sh +++ /dev/null @@ -1,190 +0,0 @@ -#!/system/bin/sh - -# Note: these two exec redirs are not strictly POSIX-compliant, so they can be commented out if we notice that it shows a syntax error in some environments (unlikely to happen) - -# Redirect stdout to ui_print otherwise it's not shown -exec 1> >(while read -r line; do ui_print "[O] $line"; done) -# Redirect stderr to ui_print otherwise it's not shown + ignore useless radare2 warning that clutters the logs -exec 2> >(while read -r line; do echo "$line" | grep -qv "Cannot determine entrypoint, using" && ui_print "[E] $line"; done) - -TEMP_DIR="/data/local/tmp/aln_patch" - -# Note: this dir cannot be changed without recompiling radare2 because this prefix are hardcoded inside the radare2 binaries: /data/local/tmp/aln_unzip/org.radare.radare2installer/radare2/ -UNZIP_DIR="/data/local/tmp/aln_unzip" -SOURCE_FILE="" -LIBRARY_NAME="" -APEX_DIR=false - -# Clean things up if the script crashes or exits -trap 'rm -rf "$TEMP_DIR" "$UNZIP_DIR"' EXIT INT TERM - -# https://github.com/Magisk-Modules-Repo/busybox-ndk/blob/master/busybox-arm64 -BUSYBOX="$UNZIP_DIR/busybox/busybox-arm64" -XZ="$UNZIP_DIR/busybox/xz" - -rm -rf "$TEMP_DIR" "$UNZIP_DIR" -mkdir -p "$TEMP_DIR" "$UNZIP_DIR" - -# Manually extract the $ZIPFILE to a temporary directory -ui_print "Extracting module files..." -unzip -d "$UNZIP_DIR" -oq "$ZIPFILE" || { - ui_print "Error: Failed to extract module files." - abort "Failed to unzip $ZIPFILE" -} - -set_perm "$BUSYBOX" 0 0 755 -set_perm "$XZ" 0 0 755 - -# The bundled radare2 is a custom build that works without Termux: https://github.com/devnoname120/radare2/releases/tag/5.9.8-android-aln -ui_print "Extracting radare2 to /data/local/tmp/aln_unzip..." -$BUSYBOX tar xf "$UNZIP_DIR/radare2-5.9.9-android-aarch64-aln.tar.gz" -C / || { - abort "Failed to extract "$UNZIP_DIR/radare2-5.9.9-android-aarch64-aln.tar.gz"." -} - - -if [ "$(uname -m)" = "aarch64" ]; then - export LD_LIBRARY_PATH="$UNZIP_DIR/org.radare.radare2installer/radare2/lib:$LD_LIBRARY_PATH" - export PATH="$UNZIP_DIR/org.radare.radare2installer/radare2/bin:$PATH" - export PATH="$UNZIP_DIR/busybox:$PATH" - export RABIN2="$UNZIP_DIR/org.radare.radare2installer/radare2/bin/rabin2" - export RADARE2="$UNZIP_DIR/org.radare.radare2installer/radare2/bin/radare2" -else - abort "arm64 archicture required, arm32 not supported" -fi - -set_perm "$RABIN2" 0 0 755 -set_perm "$RADARE2" 0 0 755 - -if [ -f "$RABIN2" ]; then - ui_print "rabin2 binary is ready." -else - ui_print "Error: rabin2 binary not found." - abort "rabin2 binary not found." -fi - -if [ -f "$RADARE2" ]; then - ui_print "radare2 binary is ready." -else - ui_print "Error: radare2 binary not found." - abort "radare2 binary not found." -fi - -if [ -f "$BUSYBOX" ]; then - ui_print "busybox binary is ready." -else - ui_print "Error: busybox binary not found." - abort "busybox binary not found." -fi - -if [ -f "$XZ" ]; then - ui_print "xz shim is ready." -else - ui_print "Error: xz shim not found." - abort "xz shim not found." -fi - -for lib_path in \ - "/apex/com.android.btservices/lib64/libbluetooth_jni.so" \ - "/system/lib64/libbluetooth_jni.so" \ - "/system/lib64/libbluetooth_qti.so" \ - "/system_ext/lib64/libbluetooth_qti.so"; do - if [ -f "$lib_path" ]; then - ui_print "Detected library: $lib_path" - [ -z "$SOURCE_FILE" ] && SOURCE_FILE="$lib_path" - [ -z "$LIBRARY_NAME" ] && LIBRARY_NAME="$(basename "$lib_path")" - fi -done - -[ -z "$SOURCE_FILE" ] && { - ui_print "Error: No target library found." - abort "No target library found." -} - -if echo "$LIBRARY_NAME" | grep -q "qti"; then - ui_print "ERROR: \"qti\" Bluetooth libraries are NOT supported by the patcher and you won't be able to use aln. Aborting..." - abort "Bluetooth driver not compatible." -fi - -ui_print "Calculating patch addresses for $SOURCE_FILE..." - -# export R2_LIBDIR="$UNZIP_DIR/radare2-android/libs/arm64-v8a" -# export R2_BINDIR="$UNZIP_DIR/radare2-android/bin/arm64-v8a" - -# $RADARE2 -H 1>&2 - -# ldd $RABIN2 1>&2 -# ldd $RADARE2 1>&2 - -symbols="$($RABIN2 -q -E "$SOURCE_FILE")" || abort "Failed to extract symbols from $SOURCE_FILE." - -get_symbol_address() { - symb_address=$(echo "$symbols" | grep "$1" | cut -d ' ' -f1 | tr -d '\n') - [ -n "$symb_address" ] || abort "Failed to obtain address for symbol $1" - echo "$symb_address" -} - -l2c_fcr_chk_chan_modes_address="$(get_symbol_address 'l2c_fcr_chk_chan_modes')" -ui_print " l2c_fcr_chk_chan_modes_address=$l2c_fcr_chk_chan_modes_address" - -l2cu_send_peer_info_req_address="$(get_symbol_address 'l2cu_send_peer_info_req')" -ui_print " l2cu_send_peer_info_req_address=$l2cu_send_peer_info_req_address" - - -cp "$SOURCE_FILE" "$TEMP_DIR" - -ui_print "Patching $LIBRARY_NAME..." - -apply_patch() { - $RADARE2 -q -e bin.cache=true -w -c "s $1; wx $2; wci" "$TEMP_DIR/$LIBRARY_NAME" || abort "Failed to apply $1 patch." -} - -apply_patch "$l2c_fcr_chk_chan_modes_address" "20008052c0035fd6" -apply_patch "$l2cu_send_peer_info_req_address" "c0035fd6" - -if [ -f "$TEMP_DIR/$LIBRARY_NAME" ]; then - ui_print "Installing patched file..." - - if echo "$SOURCE_FILE" | grep -q "/system/lib64"; then - TARGET_DIR="$MODPATH/system/lib64" - elif echo "$SOURCE_FILE" | grep -q "/apex/"; then - TARGET_DIR="$MODPATH/system/lib64" - APEX_DIR=true - else - TARGET_DIR="$MODPATH/system/lib" - fi - - mkdir -p "$TARGET_DIR" - - cp "$TEMP_DIR/$LIBRARY_NAME" "$TARGET_DIR/$LIBRARY_NAME" - set_perm "$TARGET_DIR/$LIBRARY_NAME" 0 0 644 - ui_print "Patched file installed at $TARGET_DIR/$LIBRARY_NAME" - - if [ "$APEX_DIR" = true ]; then - POST_DATA_FS_SCRIPT="$MODPATH/post-data-fs.sh" - APEX_LIB_DIR="/apex/com.android.btservices/lib64" - MOD_APEX_LIB_DIR="$MODPATH/apex/com.android.btservices/lib64" - WORK_DIR="$MODPATH/apex/com.android.btservices/work" - - mkdir -p "$MOD_APEX_LIB_DIR" "$WORK_DIR" - - cp "$TEMP_DIR/$LIBRARY_NAME" "$MOD_APEX_LIB_DIR/$LIBRARY_NAME" - set_perm "$MOD_APEX_LIB_DIR/$LIBRARY_NAME" 0 0 644 - - cat < "$POST_DATA_FS_SCRIPT" -#!/system/bin/sh -mount -t overlay overlay -o lowerdir=$APEX_LIB_DIR,upperdir=$MOD_APEX_LIB_DIR,workdir=$WORK_DIR $APEX_LIB_DIR -EOF - - set_perm "$POST_DATA_FS_SCRIPT" 0 0 755 - ui_print "Created script for apex library handling." - ui_print "You can now restart your device and test aln!" - ui_print "Note: If your Bluetooth doesn't work anymore after restarting, then uninstall this module and report the issue at the link below." - ui_print "https://github.com/kavishdevar/librepods/issues/new" - fi -else - ui_print "Error: patched file missing." - rm -rf "$TEMP_DIR" "$UNZIP_DIR" - abort "Failed to patch the library." -fi - -rm -rf "$TEMP_DIR" "$UNZIP_DIR" diff --git a/root-module/module.prop b/root-module/module.prop deleted file mode 100644 index 0193b1446..000000000 --- a/root-module/module.prop +++ /dev/null @@ -1,7 +0,0 @@ -id=btl2capfix -name=Bluetooth L2CAP workaround for AirPods -version=v3 -versionCode=3 -author=@devnoname120 and @kavishdevar -description=Fixes the Bluetooth L2CAP connection issue with AirPods -updateJson=https://raw.githubusercontent.com/kavishdevar/librepods/main/update.json diff --git a/update_nonpatch.json b/update_nonpatch.json index 3e98d3edf..d603133fc 100644 --- a/update_nonpatch.json +++ b/update_nonpatch.json @@ -1,6 +1,6 @@ { - "version": "v0.1.0-rc.4", - "versionCode": 3, - "zipUrl": "https://github.com/kavishdevar/librepods/releases/download/v0.1.0-rc.4/LibrePods-v0.1.0-rc.4.zip", - "changelog": "https://raw.githubusercontent.com/kavishdevar/librepods/main/CHANGELOG.md" -} + "version": "v0.2.6", + "versionCode": 46, + "zipUrl": "https://github.com/kavishdevar/librepods/releases/download/v0.2.6/LibrePods-FOSS-v0.2.6-release.zip", + "changelog": "https://raw.githubusercontent.com/kavishdevar/librepods/main/extras/CHANGELOG.md" +} \ No newline at end of file