diff --git a/.github/workflows/build-only.yml b/.github/workflows/build-only.yml
new file mode 100644
index 00000000..a21e3b28
--- /dev/null
+++ b/.github/workflows/build-only.yml
@@ -0,0 +1,251 @@
+name: "Build only"
+
+on:
+ push:
+ branches: [ master ]
+ tags:
+ - v*
+ pull_request:
+ branches: [ master ]
+
+
+env:
+ NDK_VERSION: '25.2.9519653'
+ NODE_VERSION: '16'
+ JAVA_VERSION: '17'
+
+jobs:
+ build-rust:
+ name: Build aw-server-rust
+ runs-on: macos-latest
+ steps:
+ - uses: actions/checkout@v3
+ with:
+ submodules: 'recursive'
+ - name: Set RELEASE
+ run: |
+ echo "RELEASE=${{ startsWith(github.ref_name, 'v') }}" >> $GITHUB_ENV
+
+ - name: Get aw-server-rust submodule commit
+ id: submodule-commit
+ run: |
+ echo "commit=$(git rev-parse HEAD:aw-server-rust)" >> $GITHUB_OUTPUT
+
+ - name: Cache JNI libs
+ uses: actions/cache@v3
+ id: cache-jniLibs
+ env:
+ cache-name: jniLibs
+ with:
+ path: mobile/src/main/jniLibs/
+ key: ${{ env.cache-name }}-release-${{ env.RELEASE }}-ndk-${{ env.NDK_VERSION }}-${{ steps.submodule-commit.outputs.commit }}
+
+ - name: Display structure of downloaded files
+ if: steps.cache-jniLibs.outputs.cache-hit == 'true'
+ run: |
+ pushd mobile/src/main/jniLibs && ls -R && popd
+
+ # Android SDK & NDK
+ - name: Set up Android SDK
+ if: steps.cache-jniLibs.outputs.cache-hit != 'true'
+ uses: android-actions/setup-android@v2
+ - name: Set up Android NDK
+ if: steps.cache-jniLibs.outputs.cache-hit != 'true'
+ run: |
+ sdkmanager "ndk;${{ env.NDK_VERSION }}"
+ ANDROID_NDK_HOME="$ANDROID_SDK_ROOT/ndk/${{ env.NDK_VERSION }}"
+ ls $ANDROID_NDK_HOME
+ echo "ANDROID_NDK_HOME=$ANDROID_NDK_HOME" >> $GITHUB_ENV
+
+ # Rust
+ - name: Set up Rust
+ id: toolchain
+ uses: dtolnay/rust-toolchain@stable
+ if: steps.cache-jniLibs.outputs.cache-hit != 'true'
+
+ - name: Set up Rust toolchain for Android NDK
+ if: steps.cache-jniLibs.outputs.cache-hit != 'true'
+ run: |
+ ./aw-server-rust/install-ndk.sh
+
+ - name: Cache cargo build
+ uses: actions/cache@v3
+ if: steps.cache-jniLibs.outputs.cache-hit != 'true'
+ env:
+ cache-name: cargo-build-target
+ with:
+ path: aw-server-rust/target
+ # key needs to contain cachekey due to https://github.com/ActivityWatch/aw-server-rust/issues/180
+ key: ${{ env.cache-name }}-${{ runner.os }}-release-${{ env.RELEASE }}-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles('**/Cargo.lock') }}
+ restore-keys: |
+ ${{ env.cache-name }}-${{ runner.os }}-release-${{ env.RELEASE }}-${{ steps.toolchain.outputs.cachekey }}-
+
+ - name: Build aw-server-rust
+ if: steps.cache-jniLibs.outputs.cache-hit != 'true'
+ run: |
+ make aw-server-rust
+
+ - name: Check that jniLibs present
+ run: |
+ test -e mobile/src/main/jniLibs/x86_64/libaw_server.so
+
+ # This needs to be a seperate job since fastlane update_version,
+ # fails if run concurrently (such as in build apk/aab matrix),
+ # thus we need to run it once and and reuse the results.
+ # https://github.com/fastlane/fastlane/issues/13689#issuecomment-439217502
+ get-versionCode:
+ name: Get latest versionCode
+ runs-on: ubuntu-latest
+ outputs:
+ versionCode: ${{ steps.versionCode.outputs.versionCode }}
+
+ steps:
+ - uses: actions/checkout@v2
+ with:
+ submodules: 'recursive'
+
+ - name: Output versionCode
+ id: versionCode
+ run: |
+ cat mobile/build.gradle | grep versionCode | sed 's/.*\s\([0-9]*\)$/versionCode=\1/' >> "$GITHUB_OUTPUT"
+
+ build-apk:
+ name: Build ${{ matrix.type }}
+ runs-on: ubuntu-latest
+ needs: [build-rust, get-versionCode]
+ strategy:
+ fail-fast: true
+ matrix:
+ type: ['apk', 'aab']
+
+ steps:
+ - uses: actions/checkout@v2
+ with:
+ submodules: 'recursive'
+
+ - uses: ActivityWatch/check-version-format-action@v2
+ id: version
+ with:
+ prefix: 'v'
+
+ - name: Echo version
+ run: |
+ echo "${{ steps.version.outputs.full }} (stable: ${{ steps.version.outputs.is_stable }})"
+
+ - name: Set RELEASE
+ run: |
+ # Build in release mode if on a tag/release (longer build times)
+ echo "RELEASE=${{ startsWith(github.ref_name, 'v') }}" >> $GITHUB_ENV
+
+ - name: Set up JDK
+ uses: actions/setup-java@v1
+ with:
+ java-version: ${{ env.JAVA_VERSION }}
+
+ # Android SDK & NDK
+ - name: Set up Android SDK
+ uses: android-actions/setup-android@v2
+ - name: Set up Android NDK
+ run: |
+ sdkmanager "ndk;${{ env.NDK_VERSION }}"
+ ANDROID_NDK_HOME="$ANDROID_SDK_ROOT/ndk/${{ env.NDK_VERSION }}"
+ ls $ANDROID_NDK_HOME
+ echo "ANDROID_NDK_HOME=$ANDROID_NDK_HOME" >> $GITHUB_ENV
+
+ - name: Get aw-server-rust submodule commit
+ id: submodule-commit
+ run: |
+ echo "commit=$(git rev-parse HEAD:aw-server-rust)" >> $GITHUB_OUTPUT
+
+ # Restores jniLibs from cache
+ # `actions/cache/restore` only restores, without saving back in a post-hook
+ - uses: actions/cache/restore@v3
+ id: cache-jniLibs
+ env:
+ cache-name: jniLibs
+ with:
+ path: mobile/src/main/jniLibs/
+ key: ${{ env.cache-name }}-release-${{ env.RELEASE }}-ndk-${{ env.NDK_VERSION }}-${{ steps.submodule-commit.outputs.commit }}
+ fail-on-cache-miss: true
+
+ - name: Check that jniLibs present
+ run: |
+ test -e mobile/src/main/jniLibs/x86_64/libaw_server.so
+
+ - name: Set versionName
+ if: startsWith(github.ref, 'refs/tags/v') # only on runs triggered from tag
+ run: |
+ # Sets versionName, tail used to skip "v" at start of tag name
+ SHORT_VERSION=$(echo "${{ github.ref_name }}" | tail -c +2 -)
+ sed -i "s/versionName \".*\"/versionName \"$SHORT_VERSION\"/g" \
+ mobile/build.gradle
+
+ - name: Set versionCode
+ run: |
+ # Sets versionCode
+ sed -i "s/versionCode .*/versionCode ${{needs.get-versionCode.outputs.versionCode}}/" \
+ mobile/build.gradle
+
+ - uses: adnsio/setup-age-action@v1.2.0
+ - name: Load Android secrets
+ if: env.KEY_ANDROID_JKS != null
+ env:
+ KEY_ANDROID_JKS: ${{ secrets.KEY_ANDROID_JKS }}
+ run: |
+ printf "$KEY_ANDROID_JKS" > android.jks.key
+ cat android.jks.age | age -d -i android.jks.key -o android.jks
+ rm android.jks.key
+
+ - name: Assemble
+ env:
+ JKS_STOREPASS: ${{ secrets.KEY_ANDROID_JKS_STOREPASS }}
+ JKS_KEYPASS: ${{ secrets.KEY_ANDROID_JKS_KEYPASS }}
+ run: |
+ make dist/aw-android.${{ matrix.type }}
+
+ - name: Upload
+ uses: actions/upload-artifact@v4
+ with:
+ name: aw-android.${{ matrix.type }}
+ path: dist/aw-android*.${{ matrix.type }}
+
+ release-gh:
+ needs: build-apk
+ if: startsWith(github.ref, 'refs/tags/v') # only on runs triggered from tag
+ runs-on: macos-latest
+ steps:
+
+ # Will download all artifacts to path
+ - name: Download release APK
+ uses: actions/download-artifact@v6
+ with:
+ name: aw-android.apk
+ path: dist
+
+ - name: Download release AAB
+ uses: actions/download-artifact@v6
+ with:
+ name: aw-android.aab
+ path: dist
+
+ - name: Display structure of downloaded files
+ working-directory: dist
+ run: ls -R
+
+ # detect if version tag is stable/beta
+ - uses: nowsprinting/check-version-format-action@v4
+ id: version
+ with:
+ prefix: 'v'
+
+ # create a release
+ - name: Release
+ uses: softprops/action-gh-release@v1
+ with:
+ draft: true
+ prerelease: ${{ !(steps.version.outputs.is_stable == 'true') }} # must compare to true, since boolean outputs are actually just strings, and "false" is truthy since it's not empty: https://github.com/actions/runner/issues/1483#issuecomment-994986996
+ files: |
+ dist/*.apk
+ dist/*.aab
+ # body_path: dist/release_notes/release_notes.md
+
diff --git a/.gitmodules b/.gitmodules
index 6973ad29..ca027bea 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,6 +1,7 @@
[submodule "aw-server-rust"]
path = aw-server-rust
- url = https://github.com/ActivityWatch/aw-server-rust.git
+ url = https://github.com/0xbrayo/aw-server-rust.git
+ branch = dev/sync-jni
[submodule "mobile/src/main/res/drawable/media"]
path = mobile/src/main/res/drawable/media
url = https://github.com/ActivityWatch/media.git
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
index b589d56e..b86273d9 100644
--- a/.idea/compiler.xml
+++ b/.idea/compiler.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 5480c98a..fa406ad2 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,10 +1,12 @@
+
+
-
+
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
index 35eb1ddf..2617fefd 100644
--- a/.idea/vcs.xml
+++ b/.idea/vcs.xml
@@ -2,5 +2,6 @@
+
\ No newline at end of file
diff --git a/Makefile b/Makefile
index e1648c5a..38b87684 100644
--- a/Makefile
+++ b/Makefile
@@ -151,7 +151,8 @@ export ON_ANDROID := -- --android
aw-server-rust: $(JNILIBS)
.PHONY: $(JNILIBS)
-$(JNILIBS): $(JNI_arm7)/libaw_server.so $(JNI_arm8)/libaw_server.so $(JNI_x86)/libaw_server.so $(JNI_x64)/libaw_server.so
+$(JNILIBS): $(JNI_arm7)/libaw_server.so $(JNI_arm8)/libaw_server.so $(JNI_x86)/libaw_server.so $(JNI_x64)/libaw_server.so \
+ $(JNI_arm7)/libaw_sync.so $(JNI_arm8)/libaw_sync.so $(JNI_x86)/libaw_sync.so $(JNI_x64)/libaw_sync.so
@ls -lL $@/*/* # Check that symlinks are valid
# There must be a better way to do this without repeating almost the same rule over and over?
@@ -170,6 +171,20 @@ $(JNI_x64)/libaw_server.so: $(TARGETDIR_x64)/$(RELEASE_TYPE)/libaw_server.so
mkdir -p $$(dirname $@)
if [ -z "$(TARGET)" ] || [ "$(TARGET)" == "x86_64" ]; then ln -fnv $$(pwd)/$^ $@; fi
+# aw-sync.so targets
+$(JNI_arm7)/libaw_sync.so: $(TARGETDIR_arm7)/$(RELEASE_TYPE)/libaw_sync.so
+ mkdir -p $$(dirname $@)
+ if [ -z "$(TARGET)" ] || [ "$(TARGET)" == "arm" ]; then ln -fnv $$(pwd)/$^ $@; fi
+$(JNI_arm8)/libaw_sync.so: $(TARGETDIR_arm8)/$(RELEASE_TYPE)/libaw_sync.so
+ mkdir -p $$(dirname $@)
+ if [ -z "$(TARGET)" ] || [ "$(TARGET)" == "arm64" ]; then ln -fnv $$(pwd)/$^ $@; fi
+$(JNI_x86)/libaw_sync.so: $(TARGETDIR_x86)/$(RELEASE_TYPE)/libaw_sync.so
+ mkdir -p $$(dirname $@)
+ if [ -z "$(TARGET)" ] || [ "$(TARGET)" == "x86" ]; then ln -fnv $$(pwd)/$^ $@; fi
+$(JNI_x64)/libaw_sync.so: $(TARGETDIR_x64)/$(RELEASE_TYPE)/libaw_sync.so
+ mkdir -p $$(dirname $@)
+ if [ -z "$(TARGET)" ] || [ "$(TARGET)" == "x86_64" ]; then ln -fnv $$(pwd)/$^ $@; fi
+
RUSTFLAGS_ANDROID="-C debuginfo=2 -Awarnings"
# Explanation of RUSTFLAGS:
# `-Awarnings` allows all warnings, for cleaner output (warnings should be detected in aw-server-rust CI anyway)
@@ -192,6 +207,21 @@ $(RS_SRCDIR)/target/%/$(RELEASE_TYPE)/libaw_server.so: $(RS_SOURCES) $(WEBUI_DIS
env RUSTFLAGS=$(RUSTFLAGS_ANDROID) make -C aw-server-rust android; \
fi
+# Pattern rule for building libaw_sync.so (similar to libaw_server.so)
+$(RS_SRCDIR)/target/%/$(RELEASE_TYPE)/libaw_sync.so: $(RS_SOURCES) $(WEBUI_DISTDIR)
+ @echo $@
+ @echo "Release type: $(RELEASE_TYPE)"
+ @# if we indicate in CI via USE_PREBUILT that we've
+ @# fetched prebuilt libaw_sync.so from aw-server-rust repo,
+ @# then don't rebuild it
+ @# also check libraries exist, if not, error
+ @if [ "$$USE_PREBUILT" == "true" ] && [ -f $@ ]; then \
+ echo "Using prebuilt libaw_sync.so"; \
+ else \
+ echo "Building libaw_sync.so from aw-server-rust repo"; \
+ env RUSTFLAGS=$(RUSTFLAGS_ANDROID) make -C aw-server-rust android; \
+ fi
+
# aw-webui
.PHONY: $(WEBUI_DISTDIR)
$(WEBUI_DISTDIR):
@@ -210,4 +240,4 @@ clean:
.PHONY: fastlane/metadata/android/en-US/images/icon.png
fastlane/metadata/android/en-US/images/icon.png: aw-server-rust/aw-webui/media/logo/logo.png
- convert $< -resize 75% -gravity center -background white -extent 512x512 $@
+ magick $< -resize 75% -gravity center -background white -extent 512x512 $@
diff --git a/README.md b/README.md
index bbb62012..ee198191 100644
--- a/README.md
+++ b/README.md
@@ -38,7 +38,7 @@ If you haven't already, initialize the submodules with: `git submodule update --
To build aw-server-rust you need to have Rust nightly installed (with rustup). Then you can build it with:
-```
+```sh
export ANDROID_NDK_HOME=`pwd`/aw-server-rust/NDK # The path to your NDK
pushd aw-server-rust && ./install-ndk.sh; popd # This configures the NDK for use with Rust, and installs the NDK if missing
env RELEASE=false make aw-server-rust # Set RELEASE=true to build in release mode (slower build, harder to debug)
diff --git a/android.jks.age b/android.jks.age
index 7a1d3290..9d9b8d4f 100644
Binary files a/android.jks.age and b/android.jks.age differ
diff --git a/aw-server-rust b/aw-server-rust
index dc70318e..cce8d655 160000
--- a/aw-server-rust
+++ b/aw-server-rust
@@ -1 +1 @@
-Subproject commit dc70318e819efc0d0535a5d7bd35a0c7ab8e9106
+Subproject commit cce8d6557b3096b30e8a169515f632306ac09b1e
diff --git a/build.gradle b/build.gradle
index 0d0482b8..98832fb5 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,10 +1,10 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
- ext.kotlin_version = '1.7.20'
- ext.androidXTestVersion = '1.5.0'
- ext.espressoVersion = '3.5.0'
- ext.extJUnitVersion = '1.1.4'
+ ext.kotlin_version = '1.9.0'
+ ext.androidXTestVersion = '1.7.0'
+ ext.espressoVersion = '3.7.0'
+ ext.extJUnitVersion = '1.3.0'
ext.servicesVersion = '1.4.2'
repositories {
google()
@@ -14,7 +14,7 @@ buildscript {
}
}
dependencies {
- classpath 'com.android.tools.build:gradle:8.1.1'
+ classpath 'com.android.tools.build:gradle:8.13.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'gradle.plugin.org.mozilla.rust-android-gradle:plugin:0.8.3'
diff --git a/docs/aw-sync-android-investigation.md b/docs/aw-sync-android-investigation.md
new file mode 100644
index 00000000..100d8713
--- /dev/null
+++ b/docs/aw-sync-android-investigation.md
@@ -0,0 +1,511 @@
+# aw-sync Android Implementation Investigation
+
+**Branch**: `investigate-aw-sync`
+**Date**: 2025-10-16
+**Status**: Phases 1-3 Complete (aw-server-rust), Phases 4-5 Pending (aw-android)
+
+## Update: Implementation Progress
+
+### ✅ Completed in aw-server-rust (dev/sync-jni branch)
+
+**Phase 1: Library Preparation**
+- Modified `aw-sync/Cargo.toml` to make CLI dependencies optional
+- Added JNI dependency for Android target
+- Created `aw-sync/src/android.rs` with JNI bindings
+- Made `main.rs` conditional on `cli` feature
+- Fixed `sync.rs` to conditionally import clap
+
+**Phase 2: Android Build Integration**
+- Updated `compile-android.sh` to build aw-sync alongside aw-server
+- Configured to build with `--no-default-features` for Android
+
+**Phase 3: JNI Implementation**
+- Implemented 5 JNI functions in android.rs:
+ - `syncPullAll()` - Pull from all hosts
+ - `syncPull(hostname)` - Pull from specific host
+ - `syncPush()` - Push local data
+ - `syncBoth()` - Full bidirectional sync
+ - `getSyncDir()` - Get sync directory path
+- All functions return JSON responses with success/error status
+- Build verified: `cargo check -p aw-sync --lib --no-default-features` ✅
+
+**Commits:**
+- `dc06dea` - feat(aw-sync): add Android JNI support and library build
+- `b3fd7ef` - fix(aw-sync): make clap import and ValueEnum derive conditional
+
+### 🔄 Next: Phases 4-5 in aw-android
+
+These phases need to be implemented in the aw-android repository.
+
+## Executive Summary
+
+aw-sync can be made to work on Android, but requires several modifications:
+1. Build aw-sync as a shared library for Android (currently only aw-server is built)
+2. Add JNI bindings to expose sync functions to Java/Kotlin
+3. Handle Android-specific storage paths
+4. Remove CLI-specific dependencies that don't work on Android
+5. Integrate into the Android app UI
+
+## Current State
+
+### What Works
+- aw-server-rust successfully runs on Android as a native library via JNI
+- aw-server-rust is compiled for all Android architectures (arm64, x86_64, x86, arm)
+- The Android app has a working JNI interface (`RustInterface.kt`)
+- aw-sync exists as a library and CLI tool in aw-server-rust
+
+### What Doesn't Work Yet
+- aw-sync is not built for Android (compile-android.sh only builds aw-server)
+- aw-sync uses CLI-specific dependencies (clap, ctrlc) that don't work on Android
+- aw-sync uses standard Unix directory conventions that don't match Android's storage model
+- No JNI interface exists for aw-sync functionality
+
+## Technical Analysis
+
+### 1. Build System
+
+**Current State**: `compile-android.sh` only builds aw-server
+
+**Required Change**: Also build aw-sync as a library alongside aw-server
+
+### 2. Directory Handling
+
+**Issue**: aw-sync's `get_sync_dir()` uses `home_dir()` which doesn't work reliably on Android.
+
+**Solution**: aw-sync already supports the `AW_SYNC_DIR` environment variable! We can set this from Android:
+- Use `Os.setenv()` to set the sync directory before loading the library
+- Point to Android-appropriate storage (internal or external app directory)
+
+### 3. Dependency Issues
+
+**Problematic Dependencies**:
+- `clap` - CLI argument parsing (not needed for library)
+- `ctrlc` - Signal handling (not needed for library)
+- `appdirs` - Directory resolution (can be worked around with env vars)
+
+**Solution**: Make these dependencies optional and only compile them for the binary target.
+
+### 4. JNI Interface Pattern
+
+aw-server already demonstrates the pattern:
+- Load library with `System.loadLibrary()`
+- Declare external functions in Kotlin
+- Call Rust functions via JNI
+- Run blocking operations in background threads
+
+## Implementation Plan
+
+### Phase 1: Library Preparation
+1. Modify `aw-sync/Cargo.toml` to make CLI dependencies optional
+2. Create `aw-sync/src/lib.rs` that exports core sync functions
+3. Add JNI bindings module for Android
+
+### Phase 2: Android Build Integration
+1. Modify `compile-android.sh` to also build aw-sync
+2. Copy `libaw_sync.so` to `jniLibs/` for each architecture
+3. Verify build outputs
+
+### Phase 3: JNI Implementation
+1. Create Rust JNI functions in aw-sync for:
+ - Pull sync from directory
+ - Push sync to directory
+ - List buckets and sync status
+2. Create Kotlin wrapper class `SyncInterface.kt`
+3. Handle sync operations in background threads
+
+### Phase 4: Android App Integration
+1. Add sync settings UI
+2. Add manual sync button
+3. Implement periodic sync with WorkManager
+4. Handle storage permissions
+5. Show sync status/progress to user
+
+### Phase 5: Testing & Polish
+1. Test pull/push operations
+2. Test with multiple devices
+3. Add error handling and user feedback
+4. Document setup process
+
+## Key Code Changes Needed
+
+### 1. aw-sync/Cargo.toml
+Make CLI dependencies optional and add JNI support for Android.
+
+### 2. aw-sync/src/lib.rs (new)
+Export core sync functions and conditionally compile JNI bindings for Android.
+
+### 3. aw-sync/src/android.rs (new)
+JNI function implementations for pull, push, and status operations.
+
+### 4. compile-android.sh
+Add aw-sync to the build pipeline alongside aw-server.
+
+### 5. SyncInterface.kt (new)
+Kotlin wrapper following the same pattern as `RustInterface.kt`.
+
+## Challenges & Considerations
+
+### Storage Location
+- **Internal Storage**: Private to app, more secure
+- **External Storage**: Accessible to sync apps (Syncthing, etc.), requires permissions
+- **Recommendation**: Use external storage for compatibility with sync tools
+
+### Sync Triggers
+- Manual sync via button (immediate)
+- Periodic background sync with WorkManager (when conditions met)
+- Respect battery optimization and network preferences
+
+### Permissions Required
+- `READ_EXTERNAL_STORAGE` / `WRITE_EXTERNAL_STORAGE`
+- `INTERNET` (for potential cloud sync backends)
+
+### Battery & Data Optimization
+- Use WorkManager for intelligent scheduling
+- Default to WiFi-only sync
+- Respect battery optimization settings
+- Show sync status to keep user informed
+
+## Feasibility Assessment
+
+**Overall: FEASIBLE**
+
+The implementation follows the established pattern from aw-server integration. Key advantages:
+- aw-sync is already modular and library-friendly
+- Environment variable configuration already supported
+- JNI pattern proven with aw-server
+- No fundamental blockers identified
+
+Main work areas:
+1. Build system changes (straightforward)
+2. JNI bindings (following existing pattern)
+3. Android app integration (UI and background scheduling)
+
+Estimated complexity: **Medium**
+- Most complex part is proper JNI implementation and error handling
+- UI integration is straightforward
+- Testing with real sync scenarios will require setup
+
+## Next Steps
+
+1. **Prototype**: Build aw-sync for Android and verify library loads
+2. **JNI Basics**: Implement minimal JNI bindings for one sync operation
+3. **Integration**: Add basic UI to trigger sync manually
+4. **Expand**: Add remaining operations and background sync
+5. **Polish**: Error handling, status reporting, documentation
+
+## References
+
+- aw-server-rust Android integration: `/mobile/src/main/java/net/activitywatch/android/RustInterface.kt`
+- aw-sync implementation: `/aw-server-rust/aw-sync/`
+- Current build script: `/aw-server-rust/compile-android.sh`
+- aw-sync README: `/aw-server-rust/aw-sync/README.md`
+
+## Phase 4-5 Implementation Guide (aw-android)
+
+### Prerequisites
+
+1. Rebuild aw-server-rust with sync support:
+ ```bash
+ cd aw-server-rust
+ git checkout dev/sync-jni
+ ./compile-android.sh
+ ```
+ This will generate both `libaw_server.so` and `libaw_sync.so` for all architectures.
+
+2. Copy the generated libraries to aw-android:
+ ```bash
+ # From aw-server-rust directory
+ for arch in arm64-v8a armeabi-v7a x86 x86_64; do
+ cp target/*/release/libaw_sync.so ../aw-android/mobile/src/main/jniLibs/$arch/
+ done
+ ```
+
+### Phase 4: Android App Integration
+
+#### Step 1: Create SyncInterface.kt
+
+Create `mobile/src/main/java/net/activitywatch/android/SyncInterface.kt`:
+
+```kotlin
+package net.activitywatch.android
+
+import android.content.Context
+import android.os.Handler
+import android.os.Looper
+import android.system.Os
+import android.util.Log
+import org.json.JSONObject
+import java.util.concurrent.Executors
+
+private const val TAG = "SyncInterface"
+
+class SyncInterface(context: Context) {
+ private val appContext: Context = context.applicationContext
+ private val syncDir: String
+
+ init {
+ // Set sync directory to external storage for access by sync apps
+ syncDir = appContext.getExternalFilesDir(null)?.absolutePath + "/ActivityWatchSync"
+ Os.setenv("AW_SYNC_DIR", syncDir, true)
+
+ // Create sync directory if it doesn't exist
+ java.io.File(syncDir).mkdirs()
+
+ System.loadLibrary("aw_sync")
+ Log.i(TAG, "aw-sync initialized with sync dir: $syncDir")
+ }
+
+ // Native functions
+ private external fun syncPullAll(port: Int): String
+ private external fun syncPull(port: Int, hostname: String): String
+ private external fun syncPush(port: Int): String
+ private external fun syncBoth(port: Int): String
+ external fun getSyncDir(): String
+
+ // Async wrappers
+ fun syncPullAllAsync(callback: (Boolean, String) -> Unit) {
+ performSyncAsync("Pull All") {
+ syncPullAll(5600)
+ }.also { (success, message) ->
+ callback(success, message)
+ }
+ }
+
+ fun syncPushAsync(callback: (Boolean, String) -> Unit) {
+ performSyncAsync("Push") {
+ syncPush(5600)
+ }.also { (success, message) ->
+ callback(success, message)
+ }
+ }
+
+ fun syncBothAsync(callback: (Boolean, String) -> Unit) {
+ performSyncAsync("Full Sync") {
+ syncBoth(5600)
+ }.also { (success, message) ->
+ callback(success, message)
+ }
+ }
+
+ private fun performSyncAsync(operation: String, syncFn: () -> String): Pair {
+ val executor = Executors.newSingleThreadExecutor()
+ val handler = Handler(Looper.getMainLooper())
+
+ var result: Pair = Pair(false, "")
+
+ executor.execute {
+ try {
+ val response = syncFn()
+ val json = JSONObject(response)
+ val success = json.getBoolean("success")
+ val message = if (success) {
+ json.getString("message")
+ } else {
+ json.getString("error")
+ }
+ result = Pair(success, message)
+
+ handler.post {
+ Log.i(TAG, "$operation completed: $message")
+ }
+ } catch (e: Exception) {
+ result = Pair(false, "Exception: ${e.message}")
+ handler.post {
+ Log.e(TAG, "$operation failed", e)
+ }
+ }
+ }
+
+ return result
+ }
+
+ fun getSyncDirectory(): String = syncDir
+}
+```
+
+#### Step 2: Add Sync UI
+
+Create a sync fragment or add to settings. Example in `SettingsFragment.kt`:
+
+```kotlin
+// In your settings or main activity
+private lateinit var syncInterface: SyncInterface
+
+override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ syncInterface = SyncInterface(requireContext())
+
+ // Add sync buttons
+ binding.btnSyncPull.setOnClickListener {
+ showSyncProgress("Pulling data...")
+ syncInterface.syncPullAllAsync { success, message ->
+ hideSyncProgress()
+ showSyncResult(success, message)
+ }
+ }
+
+ binding.btnSyncPush.setOnClickListener {
+ showSyncProgress("Pushing data...")
+ syncInterface.syncPushAsync { success, message ->
+ hideSyncProgress()
+ showSyncResult(success, message)
+ }
+ }
+
+ binding.btnSyncBoth.setOnClickListener {
+ showSyncProgress("Syncing...")
+ syncInterface.syncBothAsync { success, message ->
+ hideSyncProgress()
+ showSyncResult(success, message)
+ }
+ }
+
+ // Display sync directory
+ binding.txtSyncDir.text = "Sync Directory: ${syncInterface.getSyncDirectory()}"
+}
+```
+
+#### Step 3: Add Storage Permissions
+
+In `AndroidManifest.xml`:
+
+```xml
+
+
+```
+
+Request permissions at runtime for Android 6.0+.
+
+### Phase 5: Background Sync with WorkManager
+
+#### Step 1: Add WorkManager Dependency
+
+In `build.gradle`:
+
+```gradle
+dependencies {
+ implementation "androidx.work:work-runtime-ktx:2.8.1"
+}
+```
+
+#### Step 2: Create SyncWorker
+
+Create `mobile/src/main/java/net/activitywatch/android/workers/SyncWorker.kt`:
+
+```kotlin
+package net.activitywatch.android.workers
+
+import android.content.Context
+import androidx.work.*
+import net.activitywatch.android.SyncInterface
+import java.util.concurrent.TimeUnit
+
+class SyncWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
+
+ override fun doWork(): Result {
+ return try {
+ val syncInterface = SyncInterface(applicationContext)
+
+ // Perform sync operation
+ syncInterface.syncBothAsync { success, message ->
+ if (success) {
+ Log.i("SyncWorker", "Background sync successful: $message")
+ } else {
+ Log.w("SyncWorker", "Background sync failed: $message")
+ }
+ }
+
+ Result.success()
+ } catch (e: Exception) {
+ Log.e("SyncWorker", "Background sync error", e)
+ Result.retry()
+ }
+ }
+
+ companion object {
+ fun schedulePeriodic(context: Context) {
+ val constraints = Constraints.Builder()
+ .setRequiredNetworkType(NetworkType.UNMETERED) // WiFi only
+ .setRequiresBatteryNotLow(true)
+ .build()
+
+ val syncRequest = PeriodicWorkRequestBuilder(
+ 30, TimeUnit.MINUTES
+ )
+ .setConstraints(constraints)
+ .build()
+
+ WorkManager.getInstance(context)
+ .enqueueUniquePeriodicWork(
+ "periodic_sync",
+ ExistingPeriodicWorkPolicy.KEEP,
+ syncRequest
+ )
+ }
+ }
+}
+```
+
+#### Step 3: Schedule Background Sync
+
+In your Application class or MainActivity:
+
+```kotlin
+override fun onCreate() {
+ super.onCreate()
+
+ // Schedule periodic sync
+ if (PreferenceManager.getDefaultSharedPreferences(this)
+ .getBoolean("enable_background_sync", false)) {
+ SyncWorker.schedulePeriodic(this)
+ }
+}
+```
+
+### Testing
+
+1. **Manual Sync Test**:
+ - Trigger manual sync from UI
+ - Check logs for success/error messages
+ - Verify files created in sync directory
+
+2. **Multi-Device Test**:
+ - Set up Syncthing or similar to sync the directory
+ - Generate events on device A
+ - Sync push from device A
+ - Sync pull on device B
+ - Verify events appear on device B
+
+3. **Background Sync Test**:
+ - Enable background sync
+ - Wait for scheduled execution
+ - Check WorkManager logs
+
+### Troubleshooting
+
+**Library Not Loading**:
+- Verify `libaw_sync.so` exists in all `jniLibs/` folders
+- Check NDK build output for errors
+- Ensure correct architecture for test device
+
+**Sync Directory Not Found**:
+- Check storage permissions granted
+- Verify `AW_SYNC_DIR` set correctly
+- Check external storage is available
+
+**JNI Function Not Found**:
+- Verify JNI function names match exactly
+- Check package name in android.rs matches Kotlin
+- Rebuild native libraries
+
+### Next Steps
+
+1. Build aw-server-rust with new sync support
+2. Copy libraries to aw-android
+3. Implement SyncInterface.kt
+4. Add basic UI with sync buttons
+5. Test manual sync operations
+6. Add WorkManager for background sync
+7. Polish UI and error handling
+8. Update documentation
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
index f6b961fd..d64cd491 100644
Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 59bc51a2..9f64451e 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,5 +1,8 @@
+#Fri Sep 26 14:49:59 CEST 2025
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
+networkTimeout=10000
+validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
diff --git a/gradlew b/gradlew
index cccdd3d5..1aa94a42 100755
--- a/gradlew
+++ b/gradlew
@@ -1,78 +1,127 @@
-#!/usr/bin/env sh
+#!/bin/sh
+
+#
+# Copyright © 2015-2021 the original authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
##############################################################################
-##
-## Gradle start up script for UN*X
-##
+#
+# Gradle start up script for POSIX generated by Gradle.
+#
+# Important for running:
+#
+# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
+# noncompliant, but you have some other compliant shell such as ksh or
+# bash, then to run this script, type that shell name before the whole
+# command line, like:
+#
+# ksh Gradle
+#
+# Busybox and similar reduced shells will NOT work, because this script
+# requires all of these POSIX shell features:
+# * functions;
+# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
+# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
+# * compound commands having a testable exit status, especially «case»;
+# * various built-in commands including «command», «set», and «ulimit».
+#
+# Important for patching:
+#
+# (2) This script targets any POSIX shell, so it avoids extensions provided
+# by Bash, Ksh, etc; in particular arrays are avoided.
+#
+# The "traditional" practice of packing multiple parameters into a
+# space-separated string is a well documented source of bugs and security
+# problems, so this is (mostly) avoided, by progressively accumulating
+# options in "$@", and eventually passing that to Java.
+#
+# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
+# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
+# see the in-line comments for details.
+#
+# There are tweaks for specific operating systems such as AIX, CygWin,
+# Darwin, MinGW, and NonStop.
+#
+# (3) This script is generated from the Groovy template
+# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
+# within the Gradle project.
+#
+# You can find Gradle at https://github.com/gradle/gradle/.
+#
##############################################################################
# Attempt to set APP_HOME
+
# Resolve links: $0 may be a link
-PRG="$0"
-# Need this for relative symlinks.
-while [ -h "$PRG" ] ; do
- ls=`ls -ld "$PRG"`
- link=`expr "$ls" : '.*-> \(.*\)$'`
- if expr "$link" : '/.*' > /dev/null; then
- PRG="$link"
- else
- PRG=`dirname "$PRG"`"/$link"
- fi
+app_path=$0
+
+# Need this for daisy-chained symlinks.
+while
+ APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
+ [ -h "$app_path" ]
+do
+ ls=$( ls -ld "$app_path" )
+ link=${ls#*' -> '}
+ case $link in #(
+ /*) app_path=$link ;; #(
+ *) app_path=$APP_HOME$link ;;
+ esac
done
-SAVED="`pwd`"
-cd "`dirname \"$PRG\"`/" >/dev/null
-APP_HOME="`pwd -P`"
-cd "$SAVED" >/dev/null
-
-APP_NAME="Gradle"
-APP_BASE_NAME=`basename "$0"`
-# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
-DEFAULT_JVM_OPTS=""
+# This is normally unused
+# shellcheck disable=SC2034
+APP_BASE_NAME=${0##*/}
+# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
+APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
-MAX_FD="maximum"
+MAX_FD=maximum
warn () {
echo "$*"
-}
+} >&2
die () {
echo
echo "$*"
echo
exit 1
-}
+} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
-case "`uname`" in
- CYGWIN* )
- cygwin=true
- ;;
- Darwin* )
- darwin=true
- ;;
- MINGW* )
- msys=true
- ;;
- NONSTOP* )
- nonstop=true
- ;;
+case "$( uname )" in #(
+ CYGWIN* ) cygwin=true ;; #(
+ Darwin* ) darwin=true ;; #(
+ MSYS* | MINGW* ) msys=true ;; #(
+ NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
- JAVACMD="$JAVA_HOME/jre/sh/java"
+ JAVACMD=$JAVA_HOME/jre/sh/java
else
- JAVACMD="$JAVA_HOME/bin/java"
+ JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
@@ -81,92 +130,120 @@ Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
- JAVACMD="java"
- which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+ JAVACMD=java
+ if ! command -v java >/dev/null 2>&1
+ then
+ die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
+ fi
fi
# Increase the maximum file descriptors if we can.
-if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
- MAX_FD_LIMIT=`ulimit -H -n`
- if [ $? -eq 0 ] ; then
- if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
- MAX_FD="$MAX_FD_LIMIT"
- fi
- ulimit -n $MAX_FD
- if [ $? -ne 0 ] ; then
- warn "Could not set maximum file descriptor limit: $MAX_FD"
- fi
- else
- warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
- fi
+if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
+ case $MAX_FD in #(
+ max*)
+ # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
+ # shellcheck disable=SC2039,SC3045
+ MAX_FD=$( ulimit -H -n ) ||
+ warn "Could not query maximum file descriptor limit"
+ esac
+ case $MAX_FD in #(
+ '' | soft) :;; #(
+ *)
+ # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
+ # shellcheck disable=SC2039,SC3045
+ ulimit -n "$MAX_FD" ||
+ warn "Could not set maximum file descriptor limit to $MAX_FD"
+ esac
fi
-# For Darwin, add options to specify how the application appears in the dock
-if $darwin; then
- GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
-fi
+# Collect all arguments for the java command, stacking in reverse order:
+# * args from the command line
+# * the main class name
+# * -classpath
+# * -D...appname settings
+# * --module-path (only if needed)
+# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
+
+# For Cygwin or MSYS, switch paths to Windows format before running java
+if "$cygwin" || "$msys" ; then
+ APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
+ CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
+
+ JAVACMD=$( cygpath --unix "$JAVACMD" )
-# For Cygwin, switch paths to Windows format before running java
-if $cygwin ; then
- APP_HOME=`cygpath --path --mixed "$APP_HOME"`
- CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
- JAVACMD=`cygpath --unix "$JAVACMD"`
-
- # We build the pattern for arguments to be converted via cygpath
- ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
- SEP=""
- for dir in $ROOTDIRSRAW ; do
- ROOTDIRS="$ROOTDIRS$SEP$dir"
- SEP="|"
- done
- OURCYGPATTERN="(^($ROOTDIRS))"
- # Add a user-defined pattern to the cygpath arguments
- if [ "$GRADLE_CYGPATTERN" != "" ] ; then
- OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
- fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
- i=0
- for arg in "$@" ; do
- CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
- CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
-
- if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
- eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
- else
- eval `echo args$i`="\"$arg\""
+ for arg do
+ if
+ case $arg in #(
+ -*) false ;; # don't mess with options #(
+ /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
+ [ -e "$t" ] ;; #(
+ *) false ;;
+ esac
+ then
+ arg=$( cygpath --path --ignore --mixed "$arg" )
fi
- i=$((i+1))
+ # Roll the args list around exactly as many times as the number of
+ # args, so each arg winds up back in the position where it started, but
+ # possibly modified.
+ #
+ # NB: a `for` loop captures its iteration list before it begins, so
+ # changing the positional parameters here affects neither the number of
+ # iterations, nor the values presented in `arg`.
+ shift # remove old arg
+ set -- "$@" "$arg" # push replacement arg
done
- case $i in
- (0) set -- ;;
- (1) set -- "$args0" ;;
- (2) set -- "$args0" "$args1" ;;
- (3) set -- "$args0" "$args1" "$args2" ;;
- (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
- (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
- (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
- (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
- (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
- (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
- esac
fi
-# Escape application args
-save () {
- for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
- echo " "
-}
-APP_ARGS=$(save "$@")
-
-# Collect all arguments for the java command, following the shell quoting and substitution rules
-eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
-# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
-if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
- cd "$(dirname "$0")"
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
+
+# Collect all arguments for the java command:
+# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
+# and any embedded shellness will be escaped.
+# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
+# treated as '${Hostname}' itself on the command line.
+
+set -- \
+ "-Dorg.gradle.appname=$APP_BASE_NAME" \
+ -classpath "$CLASSPATH" \
+ org.gradle.wrapper.GradleWrapperMain \
+ "$@"
+
+# Stop when "xargs" is not available.
+if ! command -v xargs >/dev/null 2>&1
+then
+ die "xargs is not available"
fi
+# Use "xargs" to parse quoted args.
+#
+# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
+#
+# In Bash we could simply go:
+#
+# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
+# set -- "${ARGS[@]}" "$@"
+#
+# but POSIX shell has neither arrays nor command substitution, so instead we
+# post-process each arg (as a line of input to sed) to backslash-escape any
+# character that might be a shell metacharacter, then use eval to reverse
+# that process (while maintaining the separation between arguments), and wrap
+# the whole thing up as a single "set" statement.
+#
+# This will of course break if any of these variables contains a newline or
+# an unmatched quote.
+#
+
+eval "set -- $(
+ printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
+ xargs -n1 |
+ sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
+ tr '\n' ' '
+ )" '"$@"'
+
exec "$JAVACMD" "$@"
diff --git a/gradlew.bat b/gradlew.bat
index f9553162..6689b85b 100644
--- a/gradlew.bat
+++ b/gradlew.bat
@@ -1,84 +1,92 @@
-@if "%DEBUG%" == "" @echo off
-@rem ##########################################################################
-@rem
-@rem Gradle startup script for Windows
-@rem
-@rem ##########################################################################
-
-@rem Set local scope for the variables with windows NT shell
-if "%OS%"=="Windows_NT" setlocal
-
-set DIRNAME=%~dp0
-if "%DIRNAME%" == "" set DIRNAME=.
-set APP_BASE_NAME=%~n0
-set APP_HOME=%DIRNAME%
-
-@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
-set DEFAULT_JVM_OPTS=
-
-@rem Find java.exe
-if defined JAVA_HOME goto findJavaFromJavaHome
-
-set JAVA_EXE=java.exe
-%JAVA_EXE% -version >NUL 2>&1
-if "%ERRORLEVEL%" == "0" goto init
-
-echo.
-echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
-echo.
-echo Please set the JAVA_HOME variable in your environment to match the
-echo location of your Java installation.
-
-goto fail
-
-:findJavaFromJavaHome
-set JAVA_HOME=%JAVA_HOME:"=%
-set JAVA_EXE=%JAVA_HOME%/bin/java.exe
-
-if exist "%JAVA_EXE%" goto init
-
-echo.
-echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
-echo.
-echo Please set the JAVA_HOME variable in your environment to match the
-echo location of your Java installation.
-
-goto fail
-
-:init
-@rem Get command-line arguments, handling Windows variants
-
-if not "%OS%" == "Windows_NT" goto win9xME_args
-
-:win9xME_args
-@rem Slurp the command line arguments.
-set CMD_LINE_ARGS=
-set _SKIP=2
-
-:win9xME_args_slurp
-if "x%~1" == "x" goto execute
-
-set CMD_LINE_ARGS=%*
-
-:execute
-@rem Setup the command line
-
-set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
-
-@rem Execute Gradle
-"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
-
-:end
-@rem End local scope for the variables with windows NT shell
-if "%ERRORLEVEL%"=="0" goto mainEnd
-
-:fail
-rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
-rem the _cmd.exe /c_ return code!
-if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
-exit /b 1
-
-:mainEnd
-if "%OS%"=="Windows_NT" endlocal
-
-:omega
+@rem
+@rem Copyright 2015 the original author or authors.
+@rem
+@rem Licensed under the Apache License, Version 2.0 (the "License");
+@rem you may not use this file except in compliance with the License.
+@rem You may obtain a copy of the License at
+@rem
+@rem https://www.apache.org/licenses/LICENSE-2.0
+@rem
+@rem Unless required by applicable law or agreed to in writing, software
+@rem distributed under the License is distributed on an "AS IS" BASIS,
+@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+@rem See the License for the specific language governing permissions and
+@rem limitations under the License.
+@rem
+
+@if "%DEBUG%"=="" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%"=="" set DIRNAME=.
+@rem This is normally unused
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Resolve any "." and ".." in APP_HOME to make it shorter.
+for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if %ERRORLEVEL% equ 0 goto execute
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto execute
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
+
+:end
+@rem End local scope for the variables with windows NT shell
+if %ERRORLEVEL% equ 0 goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+set EXIT_CODE=%ERRORLEVEL%
+if %EXIT_CODE% equ 0 set EXIT_CODE=1
+if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
+exit /b %EXIT_CODE%
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/mobile/build.gradle b/mobile/build.gradle
index 8476d85c..5ed16904 100644
--- a/mobile/build.gradle
+++ b/mobile/build.gradle
@@ -7,7 +7,7 @@ android {
defaultConfig {
applicationId "net.activitywatch.android"
- minSdkVersion 24
+ minSdkVersion 25
targetSdkVersion 34
// Set in CI on tagged commit
@@ -42,13 +42,19 @@ android {
}
}
compileOptions {
- sourceCompatibility = '1.8'
- targetCompatibility = '1.8'
+ sourceCompatibility = JavaVersion.VERSION_11
+ targetCompatibility = JavaVersion.VERSION_11
+ coreLibraryDesugaringEnabled true
}
kotlinOptions {
- jvmTarget = "1.8"
+ jvmTarget = "11"
}
+
+ lint {
+ baseline = file("lint-baseline.xml")
+ }
+
namespace 'net.activitywatch.android'
// Never got this to work...
//if (project.hasProperty("doNotStrip")) {
@@ -69,19 +75,21 @@ android {
}
dependencies {
+ coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.5'
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
- implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+ implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
- implementation 'androidx.appcompat:appcompat:1.5.1'
+ implementation 'androidx.appcompat:appcompat:1.7.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
- implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
+ implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
- implementation 'androidx.annotation:annotation:1.5.0'
+ implementation 'androidx.annotation:annotation:1.9.1'
- implementation 'com.google.android.material:material:1.7.0'
- implementation 'com.jakewharton.threetenabp:threetenabp:1.4.3'
+ implementation 'com.google.android.material:material:1.13.0'
+ implementation 'com.jakewharton.threetenabp:threetenabp:1.4.9'
testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test.ext:junit-ktx:$extJUnitVersion"
@@ -90,6 +98,11 @@ dependencies {
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
androidTestUtil "androidx.test.services:test-services:$servicesVersion"
androidTestImplementation "androidx.test.espresso:espresso-web:$espressoVersion"
+ androidTestImplementation "androidx.browser:browser:1.8.0"
+ androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.3.0'
+ androidTestImplementation('org.awaitility:awaitility:4.3.0') {
+ exclude group: 'org.hamcrest', module: 'hamcrest'
+ }
}
// Can be used to build with: ./gradlew cargoBuild
diff --git a/mobile/lint-baseline.xml b/mobile/lint-baseline.xml
new file mode 100644
index 00000000..8f0cfe18
--- /dev/null
+++ b/mobile/lint-baseline.xml
@@ -0,0 +1,784 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/mobile/src/androidTest/java/net/activitywatch/android/ScreenshotTest.kt b/mobile/src/androidTest/java/net/activitywatch/android/ScreenshotTest.kt
index 4040f3c7..9fb01069 100644
--- a/mobile/src/androidTest/java/net/activitywatch/android/ScreenshotTest.kt
+++ b/mobile/src/androidTest/java/net/activitywatch/android/ScreenshotTest.kt
@@ -4,9 +4,8 @@ import android.content.Intent
import android.util.Log
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
-import androidx.test.core.app.takeScreenshot
import androidx.test.core.graphics.writeToTestStorage
-import androidx.test.espresso.matcher.ViewMatchers.*
+import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import org.junit.Rule
import org.junit.Test
@@ -50,7 +49,7 @@ class ScreenshotTest {
Thread.sleep(5000)
Log.i(TAG, "Taking screenshot")
- val bitmap = takeScreenshot()
+ val bitmap = InstrumentationRegistry.getInstrumentation().uiAutomation.takeScreenshot()
// Only supported on API levels >=28
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
diff --git a/mobile/src/androidTest/java/net/activitywatch/android/watcher/WebWatcherTest.kt b/mobile/src/androidTest/java/net/activitywatch/android/watcher/WebWatcherTest.kt
new file mode 100644
index 00000000..5995d42b
--- /dev/null
+++ b/mobile/src/androidTest/java/net/activitywatch/android/watcher/WebWatcherTest.kt
@@ -0,0 +1,154 @@
+package net.activitywatch.android.watcher
+
+import android.app.UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES
+import android.content.Context
+import android.content.Intent
+import android.content.pm.PackageManager
+import android.net.Uri
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.filters.LargeTest
+import androidx.test.platform.app.InstrumentationRegistry
+import androidx.test.rule.ServiceTestRule
+import net.activitywatch.android.RustInterface
+import net.activitywatch.android.watcher.utils.MAX_CONDITION_WAIT_TIME_MILLIS
+import net.activitywatch.android.watcher.utils.PAGE_MAX_WAIT_TIME_MILLIS
+import net.activitywatch.android.watcher.utils.PAGE_VISIT_TIME_MILLIS
+import net.activitywatch.android.watcher.utils.createCustomTabsWrapper
+import org.awaitility.Awaitility.await
+import org.hamcrest.CoreMatchers.not
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.TypeSafeMatcher
+import org.json.JSONArray
+import org.json.JSONObject
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import java.util.concurrent.TimeUnit.MILLISECONDS
+import kotlin.time.Duration.Companion.milliseconds
+
+private const val BUCKET_NAME = "aw-watcher-android-web"
+
+@LargeTest
+@RunWith(AndroidJUnit4::class)
+class WebWatcherTest {
+
+ @get:Rule
+ val serviceTestRule: ServiceTestRule = ServiceTestRule()
+
+ private val context = InstrumentationRegistry.getInstrumentation().targetContext
+ private val applicationContext = ApplicationProvider.getApplicationContext()
+
+ private val testWebPages = listOf(
+ WebPage("https://example.com", "Example Domain"),
+ WebPage("https://example.org", "Example Domain"),
+ WebPage("https://example.net", "Example Domain"),
+ WebPage("https://w3.org", "W3C"),
+ )
+
+ @Test
+ fun registerWebActivities() {
+ val ri = RustInterface(context)
+
+ Intent(applicationContext, WebWatcher::class.java)
+ .also { serviceTestRule.bindService(it) }
+ .also { enableAccessibilityService(serviceName = it.component!!.flattenToString()) }
+
+ val browsers = getAvailableBrowsers()
+ .also { assertThat(it, not(emptyList())) }
+
+ browsers.forEach { browser ->
+ openUris(uris = testWebPages.map { it.url }, browser = browser)
+ openHome() // to commit last event
+
+ val matchers = testWebPages.map { it.toMatcher(browser) }
+
+ await("expected events for: $browser").atMost(MAX_CONDITION_WAIT_TIME_MILLIS, MILLISECONDS).until {
+ val rawEvents = ri.getEvents(BUCKET_NAME, 100)
+ val events = JSONArray(rawEvents).asListOfJsonObjects()
+ .filter { it.getJSONObject("data").getString("browser") == browser }
+
+ matchers.all { matcher -> events.any { matcher.matches(it) } }
+ }
+ }
+ }
+
+ private fun enableAccessibilityService(serviceName: String) {
+ executeShellCmd("settings put secure enabled_accessibility_services $serviceName")
+ executeShellCmd("settings put secure accessibility_enabled 1")
+ }
+
+ private fun executeShellCmd(cmd: String) {
+ InstrumentationRegistry.getInstrumentation()
+ .getUiAutomation(FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES)
+ .executeShellCommand(cmd)
+ }
+
+ private fun getAvailableBrowsers() : List {
+ val activityIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://"))
+ return context.packageManager
+ .queryIntentActivities(activityIntent, PackageManager.MATCH_ALL)
+ .map { it.activityInfo.packageName.toString() }
+ }
+
+ private fun openUris(uris: List, browser: String) {
+ val customTabs = createCustomTabsWrapper(browser, context)
+ uris.forEach { uri -> customTabs.openAndWait(
+ uri,
+ pageVisitTime = PAGE_VISIT_TIME_MILLIS.milliseconds,
+ maxWaitTime = PAGE_MAX_WAIT_TIME_MILLIS.milliseconds
+ )}
+ }
+
+ private fun openHome() {
+ val intent = Intent(Intent.ACTION_MAIN).apply {
+ addCategory(Intent.CATEGORY_HOME)
+ flags = Intent.FLAG_ACTIVITY_NEW_TASK
+ }
+
+ context.startActivity(intent)
+ }
+}
+
+private fun JSONArray.asListOfJsonObjects() = this.let {
+ jsonArray -> (0 until jsonArray.length()).map { jsonArray.get(it) as JSONObject }
+}
+
+data class WebPage(val url: String, val title: String) {
+ fun toMatcher(expectedBrowser: String): WebWatcherEventMatcher = WebWatcherEventMatcher(
+ expectedUrl = url.removePrefix("https://"),
+ expectedTitle = title.takeIf { shouldMatchTitle(expectedBrowser) },
+ expectedBrowser = expectedBrowser,
+ )
+
+ // Samsung Internet does not match title at all as no android.webkit.WebView node is present
+ private fun shouldMatchTitle(browser: String) = browser != "com.sec.android.app.sbrowser"
+}
+
+class WebWatcherEventMatcher(
+ private val expectedUrl: String,
+ private val expectedTitle: String?,
+ private val expectedBrowser: String
+) : TypeSafeMatcher() {
+
+ override fun describeTo(description: org.hamcrest.Description?) {
+ description?.appendText("event with url=$expectedUrl registered by: $expectedBrowser")
+ }
+
+ override fun matchesSafely(obj: JSONObject): Boolean {
+ val timestamp = obj.optString("timestamp", "")
+
+ val duration = obj.optLong("duration", -1)
+ val data = obj.optJSONObject("data")
+
+ val url = data?.optString("url")
+ val title = data?.optString("title")
+ val browser = data?.optString("browser")
+
+ return timestamp.isNotBlank()
+ && duration >= 0
+ && url?.startsWith(expectedUrl) ?: false
+ && expectedTitle?.let { it == title } ?: true
+ && browser == expectedBrowser
+ }
+}
\ No newline at end of file
diff --git a/mobile/src/androidTest/java/net/activitywatch/android/watcher/utils/CustomTabsWrapper.kt b/mobile/src/androidTest/java/net/activitywatch/android/watcher/utils/CustomTabsWrapper.kt
new file mode 100644
index 00000000..d829c6be
--- /dev/null
+++ b/mobile/src/androidTest/java/net/activitywatch/android/watcher/utils/CustomTabsWrapper.kt
@@ -0,0 +1,134 @@
+package net.activitywatch.android.watcher.utils
+
+import android.content.ComponentName
+import android.content.Context
+import android.content.Intent
+import android.net.Uri
+import android.os.Bundle
+import androidx.browser.customtabs.CustomTabsCallback
+import androidx.browser.customtabs.CustomTabsCallback.NAVIGATION_FINISHED
+import androidx.browser.customtabs.CustomTabsCallback.NAVIGATION_STARTED
+import androidx.browser.customtabs.CustomTabsClient
+import androidx.browser.customtabs.CustomTabsIntent
+import androidx.browser.customtabs.CustomTabsServiceConnection
+import org.awaitility.Awaitility.await
+import java.util.concurrent.CompletableFuture
+import java.util.concurrent.LinkedBlockingQueue
+import java.util.concurrent.TimeUnit
+import kotlin.time.Duration
+import kotlin.time.toJavaDuration
+
+private const val FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK + Intent.FLAG_ACTIVITY_NO_HISTORY
+
+fun createCustomTabsWrapper(browser: String, context: Context) : CustomTabsWrapper {
+ val navigationEventsQueue = LinkedBlockingQueue()
+
+ val customTabsCallback = object : CustomTabsCallback() {
+ override fun onNavigationEvent(navigationEvent: Int, extras: Bundle?) {
+ if (navigationEvent == NAVIGATION_STARTED || navigationEvent == NAVIGATION_FINISHED) {
+ navigationEventsQueue.offer(navigationEvent)
+ }
+ }
+ }
+
+ val customTabsIntent =
+ createCustomTabsIntentWithCallback(context, browser, customTabsCallback)
+ ?: createFallbackCustomTabsIntent(browser)
+
+ return CustomTabsWrapper(customTabsIntent, context, navigationEventsQueue)
+}
+
+private fun createCustomTabsIntentWithCallback(context: Context, browser: String, callback: CustomTabsCallback) : CustomTabsIntent? {
+ val customTabsIntent: CompletableFuture = CompletableFuture()
+
+ return CustomTabsClient.bindCustomTabsService(context, browser, object : CustomTabsServiceConnection() {
+ override fun onCustomTabsServiceConnected(name: ComponentName, client: CustomTabsClient) {
+ val session = client.newSession(callback)
+ client.warmup(0)
+
+ customTabsIntent.complete(
+ CustomTabsIntent.Builder(session).build().also {
+ it.intent.setPackage(browser)
+ it.intent.addFlags(FLAGS)
+ }
+ )
+ }
+
+ override fun onServiceDisconnected(name: ComponentName) {}
+ }).takeIf { it }?.run {
+ customTabsIntent.get(CUSTOM_TABS_SERVICE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
+ }
+}
+
+private fun createFallbackCustomTabsIntent(browser: String) = CustomTabsIntent.Builder().build()
+ .also {
+ it.intent.setPackage(browser)
+ it.intent.addFlags(FLAGS)
+ }
+
+class CustomTabsWrapper(
+ private val customTabsIntent: CustomTabsIntent,
+ private val context: Context,
+ navigationEventsQueue: LinkedBlockingQueue?
+) {
+
+ private val navigationCompletionAwaiter : NavigationCompletionAwaiter;
+
+ init {
+ val fallback = FallbackNavigationCompletionAwaiter()
+ navigationCompletionAwaiter = navigationEventsQueue?.let {
+ EventBasedNavigationCompletionAwaiter(it, fallback)
+ } ?: fallback
+ }
+
+ fun openAndWait(uri: String, pageVisitTime: Duration, maxWaitTime: Duration) {
+ customTabsIntent.launchUrl(context, Uri.parse(uri))
+ navigationCompletionAwaiter.waitForNavigationCompleted(pageVisitTime, maxWaitTime)
+ }
+}
+
+private interface NavigationCompletionAwaiter {
+ fun waitForNavigationCompleted(pageVisitTime: Duration, maxWaitTime: Duration)
+}
+
+private class EventBasedNavigationCompletionAwaiter(
+ private val navigationEventsQueue: LinkedBlockingQueue,
+ private val fallback: NavigationCompletionAwaiter,
+) : NavigationCompletionAwaiter {
+
+ private var useFallback = false
+
+ private fun waitForNavigationStarted() : Boolean {
+ val event = navigationEventsQueue.poll(NAVIGATION_STARTED_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
+ return event == NAVIGATION_STARTED
+ }
+
+ override fun waitForNavigationCompleted(
+ pageVisitTime: Duration,
+ maxWaitTime: Duration
+ ) {
+ if (!useFallback && waitForNavigationStarted()) {
+ await()
+ .pollDelay(pageVisitTime.toJavaDuration())
+ .atMost(maxWaitTime.toJavaDuration())
+ .until { navigationEventsQueue.peek() == NAVIGATION_FINISHED }
+ navigationEventsQueue.peek()
+ } else {
+ useFallback = true
+ fallback.waitForNavigationCompleted(pageVisitTime, maxWaitTime)
+ }
+ }
+}
+
+private class FallbackNavigationCompletionAwaiter : NavigationCompletionAwaiter {
+ override fun waitForNavigationCompleted(
+ pageVisitTime: Duration,
+ maxWaitTime: Duration
+ ) {
+ await()
+ .pollDelay(pageVisitTime.toJavaDuration())
+ .atMost(maxWaitTime.toJavaDuration())
+ .until { true } // just wait page visit time as no callback is available
+ }
+
+}
\ No newline at end of file
diff --git a/mobile/src/androidTest/java/net/activitywatch/android/watcher/utils/TestTimeouts.kt b/mobile/src/androidTest/java/net/activitywatch/android/watcher/utils/TestTimeouts.kt
new file mode 100644
index 00000000..f185a95d
--- /dev/null
+++ b/mobile/src/androidTest/java/net/activitywatch/android/watcher/utils/TestTimeouts.kt
@@ -0,0 +1,8 @@
+package net.activitywatch.android.watcher.utils
+
+const val PAGE_VISIT_TIME_MILLIS = 5000L
+const val PAGE_MAX_WAIT_TIME_MILLIS = 10000L
+const val MAX_CONDITION_WAIT_TIME_MILLIS = 10000L
+
+const val CUSTOM_TABS_SERVICE_TIMEOUT_MILLIS = 30000L
+const val NAVIGATION_STARTED_TIMEOUT_MILLIS = 10000L
\ No newline at end of file
diff --git a/mobile/src/main/AndroidManifest.xml b/mobile/src/main/AndroidManifest.xml
index c983de3a..70cd20cb 100644
--- a/mobile/src/main/AndroidManifest.xml
+++ b/mobile/src/main/AndroidManifest.xml
@@ -1,17 +1,15 @@
+ xmlns:tools="http://schemas.android.com/tools"
+ tools:ignore="MissingLeanbackLauncher">
-
-
+
@@ -22,7 +20,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/aw_launcher_round"
android:icon="@mipmap/aw_launcher"
- android:banner="@mipmap/aw_launcher"
+
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config">
@@ -39,6 +37,7 @@
+
-
diff --git a/mobile/src/main/java/net/activitywatch/android/MainActivity.kt b/mobile/src/main/java/net/activitywatch/android/MainActivity.kt
index 0c439e0f..46c025c5 100644
--- a/mobile/src/main/java/net/activitywatch/android/MainActivity.kt
+++ b/mobile/src/main/java/net/activitywatch/android/MainActivity.kt
@@ -3,14 +3,15 @@ package net.activitywatch.android
import android.content.Intent
import android.net.Uri
import android.os.Bundle
-import com.google.android.material.snackbar.Snackbar
-import com.google.android.material.navigation.NavigationView
-import androidx.core.view.GravityCompat
-import androidx.appcompat.app.AppCompatActivity
+import android.util.Log
import android.view.Menu
import android.view.MenuItem
+import androidx.activity.OnBackPressedCallback
+import androidx.appcompat.app.AppCompatActivity
+import androidx.core.view.GravityCompat
import androidx.fragment.app.Fragment
-import android.util.Log
+import com.google.android.material.navigation.NavigationView
+import com.google.android.material.snackbar.Snackbar
import net.activitywatch.android.databinding.ActivityMainBinding
import net.activitywatch.android.fragments.TestFragment
import net.activitywatch.android.fragments.WebUIFragment
@@ -24,6 +25,7 @@ const val baseURL = "http://127.0.0.1:5600"
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener, WebUIFragment.OnFragmentInteractionListener {
private lateinit var binding: ActivityMainBinding
+ private lateinit var syncScheduler: SyncScheduler
val version: String
get() {
@@ -57,7 +59,11 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
binding.navView.setNavigationItemSelectedListener(this)
val ri = RustInterface(this)
- ri.startServerTask(this)
+ ri.startServerTask()
+
+ // Start automatic sync scheduler
+ syncScheduler = SyncScheduler(this)
+ syncScheduler.start()
if (savedInstanceState != null) {
return
@@ -65,6 +71,16 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
val firstFragment = WebUIFragment.newInstance(baseURL)
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, firstFragment).commit()
+
+ onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
+ override fun handleOnBackPressed() {
+ if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
+ binding.drawerLayout.closeDrawer(GravityCompat.START)
+ } else {
+ finish()
+ }
+ }
+ })
}
override fun onResume() {
@@ -73,17 +89,11 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
// Ensures data is always fresh when app is opened,
// even if it was up to an hour since the last logging-alarm was triggered.
val usw = UsageStatsWatcher(this)
+ val mode = if (usw.isUsingDiscreteEvents()) "discrete event insertion" else "heartbeat merging"
+ Log.i("MainActivity", "Using $mode mode for event tracking")
usw.sendHeartbeats()
}
- override fun onBackPressed() {
- if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
- binding.drawerLayout.closeDrawer(GravityCompat.START)
- } else {
- super.onBackPressed()
- }
- }
-
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
@@ -155,4 +165,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
binding.drawerLayout.closeDrawer(GravityCompat.START)
return true
}
+
+ override fun onDestroy() {
+ super.onDestroy()
+ syncScheduler.stop()
+ }
}
diff --git a/mobile/src/main/java/net/activitywatch/android/OnboardingActivity.kt b/mobile/src/main/java/net/activitywatch/android/OnboardingActivity.kt
index 76d05bf0..915beec7 100644
--- a/mobile/src/main/java/net/activitywatch/android/OnboardingActivity.kt
+++ b/mobile/src/main/java/net/activitywatch/android/OnboardingActivity.kt
@@ -8,6 +8,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
+import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
@@ -86,14 +87,16 @@ class OnboardingActivity : AppCompatActivity() {
updateButtons()
}
})
- }
- override fun onBackPressed() {
- // If back button is pressed, exit the app,
- // since we don't want to allow the user to accidentally skip onboarding.
- // (Google Play policy, due to sensitive permissions)
- // https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces#back-button
- finishAffinity()
+ onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
+ override fun handleOnBackPressed() {
+ // If back button is pressed, exit the app,
+ // since we don't want to allow the user to accidentally skip onboarding.
+ // (Google Play policy, due to sensitive permissions)
+ // https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces#back-button
+ finishAffinity()
+ }
+ })
}
}
diff --git a/mobile/src/main/java/net/activitywatch/android/RustInterface.kt b/mobile/src/main/java/net/activitywatch/android/RustInterface.kt
index 01b68c50..09533791 100644
--- a/mobile/src/main/java/net/activitywatch/android/RustInterface.kt
+++ b/mobile/src/main/java/net/activitywatch/android/RustInterface.kt
@@ -3,33 +3,35 @@ package net.activitywatch.android
import android.content.Context
import android.os.Handler
import android.os.Looper
+import android.provider.Settings
import android.system.Os
import android.util.Log
-import android.widget.Toast
+import java.util.concurrent.Executors
import net.activitywatch.android.models.Event
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.threeten.bp.Instant
-import java.io.File
-import java.util.concurrent.Executors
private const val TAG = "RustInterface"
-class RustInterface constructor(context: Context? = null) {
+class RustInterface (context: Context? = null) {
+
+ private val appContext: Context? = context?.applicationContext
init {
- // NOTE: This doesn't work, probably because I can't get gradle to not strip symbols on release builds
+ // NOTE: This doesn't work, probably because I can't get gradle to not strip symbols on
+ // release builds
Os.setenv("RUST_BACKTRACE", "1", true)
- if(context != null) {
+ if (context != null) {
Os.setenv("SQLITE_TMPDIR", context.cacheDir.absolutePath, true)
}
System.loadLibrary("aw_server")
initialize()
- if(context != null) {
+ if (context != null) {
setDataDir(context.filesDir.absolutePath)
}
}
@@ -51,13 +53,13 @@ class RustInterface constructor(context: Context? = null) {
return greeting(to)
}
- fun startServerTask(context: Context) {
- if(!serverStarted) {
+ fun startServerTask() {
+ if (!serverStarted) {
// check if port 5600 is already in use
try {
val socket = java.net.ServerSocket(5600)
socket.close()
- } catch(e: java.net.BindException) {
+ } catch (e: java.net.BindException) {
Log.e(TAG, "Port 5600 is already in use, server probably already started")
return
}
@@ -82,25 +84,74 @@ class RustInterface constructor(context: Context? = null) {
}
}
- fun createBucketHelper(bucket_id: String, type: String, hostname: String = "unknown", client: String = "aw-android") {
- if(bucket_id in getBucketsJSON().keys().asSequence()) {
+ fun createBucketHelper(bucket_id: String, type: String, client: String = "aw-android") {
+ val context =
+ appContext
+ ?: throw IllegalStateException(
+ "Context is required but was not provided during initialization"
+ )
+ val hostname = getDeviceName(context)
+ if (bucket_id in getBucketsJSON().keys().asSequence()) {
Log.i(TAG, "Bucket with ID '$bucket_id', already existed. Not creating.")
} else {
- val msg = createBucket("""{"id": "$bucket_id", "type": "$type", "hostname": "$hostname", "client": "$client"}""");
+ val msg =
+ createBucket(
+ """{"id": "$bucket_id", "type": "$type", "hostname": "$hostname", "client": "$client"}"""
+ )
Log.w(TAG, msg)
}
}
- fun heartbeatHelper(bucket_id: String, timestamp: Instant, duration: Double, data: JSONObject, pulsetime: Double = 60.0) {
+ /**
+ * Send a heartbeat event that may be merged with nearby events.
+ *
+ * Heartbeats are useful for:
+ * - Live tracking where events are sent continuously
+ * - Situations where event merging is desired
+ *
+ * However, for app usage tracking, heartbeats can cause data loss due to:
+ * - Events being merged incorrectly
+ * - Zero-duration events being created
+ * - Significant underreporting (up to 97% data loss observed)
+ *
+ * @param bucket_id The bucket to send the heartbeat to
+ * @param timestamp The event timestamp
+ * @param duration The event duration in seconds
+ * @param data Event metadata
+ * @param pulsetime Time window for merging events (default: 60 seconds)
+ */
+ fun heartbeatHelper(
+ bucket_id: String,
+ timestamp: Instant,
+ duration: Double,
+ data: JSONObject,
+ pulsetime: Double = 60.0
+ ) {
val event = Event(timestamp, duration, data)
val msg = heartbeat(bucket_id, event.toString(), pulsetime)
- //Log.w(TAG, msg)
+ // Log.w(TAG, msg)
+ }
+
+ /**
+ * Insert a discrete event that will not be merged with other events.
+ *
+ * This method is preferred for accurate app usage tracking because:
+ * - Each event represents a complete app session with precise start time and duration
+ * - Events are not merged or modified by the heartbeat system
+ * @param bucket_id The bucket to insert the event into
+ * @param timestamp The exact start time of the event
+ * @param duration The precise duration in seconds
+ * @param data Event metadata (app name, package, etc.)
+ */
+ fun insertEvent(bucket_id: String, timestamp: Instant, duration: Double, data: JSONObject) {
+ val event = Event(timestamp, duration, data)
+ val msg = heartbeat(bucket_id, event.toString(), 0.0)
}
fun getBucketsJSON(): JSONObject {
// TODO: Handle errors
val json = JSONObject(getBuckets())
- if(json.length() <= 0) {
+ if (json.length() <= 0) {
Log.w(TAG, "Length: ${json.length()}")
}
return json
@@ -111,11 +162,15 @@ class RustInterface constructor(context: Context? = null) {
val result = getEvents(bucket_id, limit)
return try {
JSONArray(result)
- } catch(e: JSONException) {
+ } catch (e: JSONException) {
Log.e(TAG, "Error when trying to fetch events from bucket: $result")
JSONArray()
}
}
+ fun getDeviceName(context: Context): String {
+ return Settings.Global.getString(context.contentResolver, Settings.Global.DEVICE_NAME)
+ ?: android.os.Build.MODEL ?: "Unknown"
+ }
fun test() {
// TODO: Move to instrumented test
diff --git a/mobile/src/main/java/net/activitywatch/android/SyncInterface.kt b/mobile/src/main/java/net/activitywatch/android/SyncInterface.kt
new file mode 100644
index 00000000..16e44109
--- /dev/null
+++ b/mobile/src/main/java/net/activitywatch/android/SyncInterface.kt
@@ -0,0 +1,105 @@
+package net.activitywatch.android
+
+import android.content.Context
+import android.os.Handler
+import android.os.Looper
+import android.system.Os
+import android.util.Log
+import org.json.JSONObject
+import java.io.File
+import java.util.concurrent.Executors
+
+private const val TAG = "SyncInterface"
+
+class SyncInterface(context: Context) {
+ private val appContext: Context = context.applicationContext
+ private val syncDir: String
+
+ init {
+ // Use Downloads folder for easy user access: /sdcard/Download/ActivityWatch/
+ val downloadsDir = android.os.Environment.getExternalStoragePublicDirectory(
+ android.os.Environment.DIRECTORY_DOWNLOADS
+ )
+ syncDir = "$downloadsDir/ActivityWatch"
+ Os.setenv("AW_SYNC_DIR", syncDir, true)
+
+ // Create sync directory if it doesn't exist
+ File(syncDir).mkdirs()
+
+ System.loadLibrary("aw_sync")
+ Log.i(TAG, "aw-sync initialized with sync dir: $syncDir")
+ }
+
+ // Native JNI functions
+ private external fun syncPullAll(port: Int, hostname: String): String
+ private external fun syncPull(port: Int, hostname: String): String
+ private external fun syncPush(port: Int, hostname: String): String
+ private external fun syncBoth(port: Int, hostname: String): String
+ external fun getSyncDir(): String
+
+ private fun getDeviceName(): String {
+ return android.provider.Settings.Global.getString(
+ appContext.contentResolver,
+ android.provider.Settings.Global.DEVICE_NAME
+ ) ?: android.os.Build.MODEL ?: "Unknown"
+ }
+
+ // Async wrapper for syncPullAll
+ fun syncPullAllAsync(callback: (Boolean, String) -> Unit) {
+ val hostname = getDeviceName()
+ performSyncAsync("Pull All", callback) {
+ syncPullAll(5600, hostname)
+ }
+ }
+
+ // Async wrapper for syncPush
+ fun syncPushAsync(callback: (Boolean, String) -> Unit) {
+ val hostname = getDeviceName()
+ performSyncAsync("Push", callback) {
+ syncPush(5600, hostname)
+ }
+ }
+
+ // Async wrapper for syncBoth
+ fun syncBothAsync(callback: (Boolean, String) -> Unit) {
+ val hostname = getDeviceName()
+ performSyncAsync("Full Sync", callback) {
+ syncBoth(5600, hostname)
+ }
+ }
+
+ private fun performSyncAsync(
+ operation: String,
+ callback: (Boolean, String) -> Unit,
+ syncFn: () -> String
+ ) {
+ val executor = Executors.newSingleThreadExecutor()
+ val handler = Handler(Looper.getMainLooper())
+
+ executor.execute {
+ try {
+ val response = syncFn()
+ val json = JSONObject(response)
+ val success = json.getBoolean("success")
+ val message = if (success) {
+ json.getString("message")
+ } else {
+ json.getString("error")
+ }
+
+ handler.post {
+ Log.i(TAG, "$operation completed: success=$success, message=$message")
+ callback(success, message)
+ }
+ } catch (e: Exception) {
+ val errorMsg = "Exception: ${e.message}"
+ handler.post {
+ Log.e(TAG, "$operation failed", e)
+ callback(false, errorMsg)
+ }
+ }
+ }
+ }
+
+ fun getSyncDirectory(): String = syncDir
+}
diff --git a/mobile/src/main/java/net/activitywatch/android/SyncScheduler.kt b/mobile/src/main/java/net/activitywatch/android/SyncScheduler.kt
new file mode 100644
index 00000000..6faa7170
--- /dev/null
+++ b/mobile/src/main/java/net/activitywatch/android/SyncScheduler.kt
@@ -0,0 +1,62 @@
+package net.activitywatch.android
+
+import android.content.Context
+import android.os.Handler
+import android.os.Looper
+import android.util.Log
+
+private const val TAG = "SyncScheduler"
+
+class SyncScheduler(private val context: Context) {
+ private val handler = Handler(Looper.getMainLooper())
+ private lateinit var syncInterface: SyncInterface
+ private var isRunning = false
+
+ private val syncRunnable = object : Runnable {
+ override fun run() {
+ if (isRunning) {
+ performSync()
+ // Schedule next sync in 15 minutes
+ handler.postDelayed(this, 15 * 60 * 1000L)
+ }
+ }
+ }
+
+ fun start() {
+ if (isRunning) {
+ Log.w(TAG, "Sync scheduler already running")
+ return
+ }
+
+ Log.i(TAG, "Starting sync scheduler - first sync in 1 minute, then every 15 minutes")
+ isRunning = true
+
+ try {
+ syncInterface = SyncInterface(context)
+
+ // First sync after 1 minute
+ handler.postDelayed(syncRunnable, 60 * 1000L)
+ } catch (e: Exception) {
+ Log.e(TAG, "Failed to start sync scheduler", e)
+ isRunning = false
+ }
+ }
+
+ fun stop() {
+ Log.i(TAG, "Stopping sync scheduler")
+ isRunning = false
+ handler.removeCallbacks(syncRunnable)
+ }
+
+ private fun performSync() {
+ Log.i(TAG, "Performing automatic sync...")
+
+ syncInterface.syncBothAsync { success, message ->
+ if (success) {
+ Log.i(TAG, "Automatic sync completed successfully: $message")
+ } else {
+ Log.w(TAG, "Automatic sync failed: $message")
+ }
+ }
+ }
+}
diff --git a/mobile/src/main/java/net/activitywatch/android/data/SessionModels.kt b/mobile/src/main/java/net/activitywatch/android/data/SessionModels.kt
new file mode 100644
index 00000000..50b00fba
--- /dev/null
+++ b/mobile/src/main/java/net/activitywatch/android/data/SessionModels.kt
@@ -0,0 +1,93 @@
+package net.activitywatch.android.data
+
+import org.json.JSONObject
+
+/**
+ * Represents a single app usage session
+ */
+data class AppSession(
+ val packageName: String,
+ val appName: String,
+ val className: String = "",
+ val startTime: Long,
+ val endTime: Long,
+ val durationMs: Long = endTime - startTime
+) {
+ val durationMinutes: Double get() = durationMs / 60000.0
+ val durationHours: Double get() = durationMs / 3600000.0
+ val durationSeconds: Double get() = durationMs / 1000.0
+
+ /**
+ * Convert session to ActivityWatch Event JSON format for heartbeats
+ */
+ fun toEventData(): JSONObject {
+ val data = JSONObject()
+ data.put("app", appName)
+ data.put("package", packageName)
+ if (className.isNotEmpty()) {
+ data.put("classname", className)
+ }
+ return data
+ }
+}
+
+/**
+ * Represents aggregated usage data for a single app
+ */
+data class AppUsageSummary(
+ val packageName: String,
+ val appName: String,
+ val totalTimeMs: Long,
+ val sessionCount: Int,
+ val sessions: List
+) {
+ val totalMinutes: Double get() = totalTimeMs / 60000.0
+ val totalHours: Double get() = totalTimeMs / 3600000.0
+ val averageSessionMs: Long get() = if (sessionCount > 0) totalTimeMs / sessionCount else 0
+ val averageSessionMinutes: Double get() = averageSessionMs / 60000.0
+}
+
+/**
+ * Represents a complete timeline for a single day
+ */
+data class DayTimeline(
+ val date: Long, // timestamp of the day start
+ val sessions: List,
+ val appSummaries: List,
+ val totalScreenTimeMs: Long
+) {
+ val totalScreenTimeHours: Double get() = totalScreenTimeMs / 3600000.0
+ val totalScreenTimeMinutes: Double get() = totalScreenTimeMs / 60000.0
+ val uniqueAppsCount: Int get() = appSummaries.size
+}
+
+/**
+ * Internal model for tracking active activities during parsing
+ */
+internal data class ActivityState(
+ val packageName: String,
+ val className: String,
+ val appName: String,
+ val startTime: Long
+)
+
+/**
+ * Internal model for raw usage events during parsing
+ */
+internal data class UsageEvent(
+ val eventType: Int,
+ val timeStamp: Long,
+ val packageName: String,
+ val className: String
+)
+
+/**
+ * Represents session statistics for analysis
+ */
+data class SessionStats(
+ val totalSessions: Int,
+ val averageSessionDuration: Long,
+ val longestSession: AppSession?,
+ val shortestSession: AppSession?,
+ val mostUsedApp: AppUsageSummary?
+)
diff --git a/mobile/src/main/java/net/activitywatch/android/parser/SessionParser.kt b/mobile/src/main/java/net/activitywatch/android/parser/SessionParser.kt
new file mode 100644
index 00000000..f1ed4b48
--- /dev/null
+++ b/mobile/src/main/java/net/activitywatch/android/parser/SessionParser.kt
@@ -0,0 +1,261 @@
+package net.activitywatch.android.parser
+
+import android.app.usage.UsageEvents
+import android.app.usage.UsageStatsManager
+import android.content.Context
+import android.util.Log
+import net.activitywatch.android.data.*
+import net.activitywatch.android.utils.SessionUtils
+import org.threeten.bp.DateTimeUtils
+import org.threeten.bp.Instant
+import java.util.*
+import java.text.SimpleDateFormat
+
+/**
+ * SessionParser - Converts raw Android usage events into meaningful app usage sessions
+ *
+ * CURRENT STATE: Session parsing functionality implemented but using HEARTBEAT approach
+ *
+ * BACKGROUND:
+ * - Original aw-android heartbeat system showed ~18 seconds for apps that should show ~35 minutes
+ * - This was due to heartbeat merging behavior losing most duration data
+ * - Session parser was implemented to fix this by creating discrete events with exact durations
+ * - The discrete event approach achieved 99.1% accuracy vs Digital Wellbeing (35.3min vs 35.0min)
+ *
+ * CURRENT APPROACH:
+ * - Using discrete event insertion (insertEvent method with pulsetime=0)
+ * - Session-based parsing with individual event insertion (99.1% accuracy achieved)
+ * - Session detection logic active and uses strict Digital Wellbeing compatibility
+ * - Each app session becomes a discrete event with precise start time and duration
+ */
+class SessionParser(private val context: Context) {
+
+ private val usageStatsManager = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
+
+ companion object {
+ private const val TAG = "SessionParser"
+
+ // Usage event types from UsageEvents.Event
+ // Use Android's UsageEvents.Event constants directly
+ // ACTIVITY_RESUMED = 1, MOVE_TO_FOREGROUND = 1 (same value)
+ // ACTIVITY_PAUSED = 2, MOVE_TO_BACKGROUND = 2 (same value)
+
+ // Session limits
+ private const val MAX_ORPHANED_SESSION_DURATION = 5 * 60 * 1000L // 5 minutes for sessions without proper end events
+ private const val MAX_REASONABLE_SESSION_DURATION = 4 * 60 * 60 * 1000L // 4 hours maximum for any session
+ private const val MIN_SESSION_DURATION = 1000L // 1 second minimum
+ }
+
+ /**
+ * Parse usage events for a given day and create timeline
+ */
+ fun parseUsageEventsForDay(dayStartMs: Long): DayTimeline {
+ val dayEndMs = dayStartMs + 24 * 60 * 60 * 1000 // 24 hours later
+
+ // Get raw usage events
+ val usageEvents = usageStatsManager.queryEvents(dayStartMs, dayEndMs)
+ val rawEvents = extractRawEvents(usageEvents)
+
+ Log.d(TAG, "Processing ${rawEvents.size} events for day")
+
+ // Parse events into sessions
+ val sessions = parseEventsIntoSessions(rawEvents, dayEndMs)
+
+ // Create app summaries
+ val appSummaries = createAppSummaries(sessions)
+
+ // Calculate total screen time
+ val totalScreenTime = sessions.sumOf { it.durationMs }
+
+ return DayTimeline(
+ date = dayStartMs,
+ sessions = sessions.sortedBy { it.startTime },
+ appSummaries = appSummaries.sortedByDescending { it.totalTimeMs },
+ totalScreenTimeMs = totalScreenTime
+ )
+ }
+
+ /**
+ * Parse usage events for a period starting from a timestamp
+ */
+ fun parseUsageEventsForPeriod(startTimestamp: Long, endTimestamp: Long): List {
+ val usageEvents = usageStatsManager.queryEvents(startTimestamp, endTimestamp)
+ val rawEvents = extractRawEvents(usageEvents)
+
+ Log.d(TAG, "Processing ${rawEvents.size} events for period")
+
+ return parseEventsIntoSessions(rawEvents, endTimestamp)
+ }
+
+ /**
+ * Parse usage events since a specific timestamp (for incremental updates)
+ */
+ fun parseUsageEventsSince(lastUpdateTimestamp: Long): List {
+ val currentTime = System.currentTimeMillis()
+ return parseUsageEventsForPeriod(lastUpdateTimestamp, currentTime)
+ }
+
+ /**
+ * Extract raw events from UsageEvents iterator
+ */
+ private fun extractRawEvents(usageEvents: UsageEvents): List {
+ val events = mutableListOf()
+
+ while (usageEvents.hasNextEvent()) {
+ val event = UsageEvents.Event()
+ usageEvents.getNextEvent(event)
+
+ // Filter for relevant activity events
+ if (isRelevantEvent(event)) {
+ events.add(
+ UsageEvent(
+ eventType = event.eventType,
+ timeStamp = event.timeStamp,
+ packageName = event.packageName ?: "",
+ className = event.className ?: ""
+ )
+ )
+ }
+ }
+
+ return events.sortedBy { it.timeStamp }
+ }
+
+ /**
+ * Check if an event is relevant for timeline creation
+ */
+ private fun isRelevantEvent(event: UsageEvents.Event): Boolean {
+ return when (event.eventType) {
+ UsageEvents.Event.ACTIVITY_RESUMED, // Also covers MOVE_TO_FOREGROUND (same value)
+ UsageEvents.Event.ACTIVITY_PAUSED -> true // Also covers MOVE_TO_BACKGROUND (same value)
+ else -> false
+ }
+ }
+
+ /**
+ * Parse events into app sessions using strict Digital Wellbeing logic
+ * Only counts proper RESUME->PAUSE pairs with no intervening RESUME events
+ */
+ private fun parseEventsIntoSessions(
+ events: List,
+ periodEnd: Long
+ ): List {
+ val sessions = mutableListOf()
+
+ Log.d(TAG, "Parsing ${events.size} events into sessions (strict Digital Wellbeing mode)")
+
+ // Use strict pair matching - only count clean RESUME/PAUSE pairs
+ var i = 0
+ while (i < events.size - 1) {
+ val event = events[i]
+
+ if (event.eventType == UsageEvents.Event.ACTIVITY_RESUMED) {
+ // Look for immediate matching PAUSE event for same package
+ // without any intervening RESUME events for the same package
+ var j = i + 1
+ var foundValidPause = false
+
+ while (j < events.size && !foundValidPause) {
+ val nextEvent = events[j]
+
+ if (nextEvent.packageName == event.packageName) {
+ if (nextEvent.eventType == UsageEvents.Event.ACTIVITY_PAUSED) {
+ // Found matching PAUSE - create session
+ val duration = nextEvent.timeStamp - event.timeStamp
+ if (duration > MIN_SESSION_DURATION && duration < MAX_REASONABLE_SESSION_DURATION) {
+ val appName = SessionUtils.getAppName(context, event.packageName)
+ val session = AppSession(
+ packageName = event.packageName,
+ appName = appName,
+ className = event.className,
+ startTime = event.timeStamp,
+ endTime = nextEvent.timeStamp
+ )
+ sessions.add(session)
+ Log.d(TAG, "Strict session: ${appName} ${duration}ms")
+ }
+ foundValidPause = true
+ } else if (nextEvent.eventType == UsageEvents.Event.ACTIVITY_RESUMED) {
+ // Found another RESUME for same package - invalid pair, skip this RESUME
+ Log.d(TAG, "Skipping session for ${event.packageName}: RESUME without PAUSE at ${java.util.Date(nextEvent.timeStamp)}")
+ break
+ }
+ }
+ j++
+ }
+ }
+ i++
+ }
+
+ Log.d(TAG, "Created ${sessions.size} strict sessions")
+
+ return sessions
+ }
+
+ // Removed old session handling methods - using conservative pair matching instead
+
+ /**
+ * Create app usage summaries from sessions
+ */
+ private fun createAppSummaries(sessions: List): List {
+ val appSessionsMap = sessions.groupBy { it.packageName }
+
+ return appSessionsMap.map { (packageName, appSessions) ->
+ AppUsageSummary(
+ packageName = packageName,
+ appName = appSessions.first().appName, // All sessions for same app should have same name
+ totalTimeMs = appSessions.sumOf { it.durationMs },
+ sessionCount = appSessions.size,
+ sessions = appSessions.sortedBy { it.startTime }
+ )
+ }
+ }
+
+ /**
+ * Parse usage events for multiple days
+ */
+ fun parseUsageEventsForPeriod(startDay: Long, numberOfDays: Int): List {
+ val timelines = mutableListOf()
+
+ for (i in 0 until numberOfDays) {
+ val dayStart = startDay + (i * 24 * 60 * 60 * 1000)
+ val timeline = parseUsageEventsForDay(dayStart)
+ timelines.add(timeline)
+ }
+
+ return timelines
+ }
+
+ /**
+ * Get session statistics for analysis
+ */
+ fun getSessionStats(sessions: List): SessionStats {
+ if (sessions.isEmpty()) {
+ return SessionStats(
+ totalSessions = 0,
+ averageSessionDuration = 0L,
+ longestSession = null,
+ shortestSession = null,
+ mostUsedApp = null
+ )
+ }
+
+ val averageDuration = sessions.map { it.durationMs }.average().toLong()
+ val longestSession = sessions.maxByOrNull { it.durationMs }
+ val shortestSession = sessions.minByOrNull { it.durationMs }
+
+ // Calculate most used app
+ val appSummaries = createAppSummaries(sessions)
+ val mostUsedApp = appSummaries.maxByOrNull { it.totalTimeMs }
+
+ return SessionStats(
+ totalSessions = sessions.size,
+ averageSessionDuration = averageDuration,
+ longestSession = longestSession,
+ shortestSession = shortestSession,
+ mostUsedApp = mostUsedApp
+ )
+ }
+
+
+}
diff --git a/mobile/src/main/java/net/activitywatch/android/utils/SessionUtils.kt b/mobile/src/main/java/net/activitywatch/android/utils/SessionUtils.kt
new file mode 100644
index 00000000..28eecc79
--- /dev/null
+++ b/mobile/src/main/java/net/activitywatch/android/utils/SessionUtils.kt
@@ -0,0 +1,210 @@
+package net.activitywatch.android.utils
+
+import android.app.AppOpsManager
+import android.content.Context
+import android.content.Intent
+import android.content.pm.PackageManager
+import android.provider.Settings
+import java.text.SimpleDateFormat
+import java.util.*
+import java.util.concurrent.TimeUnit
+import kotlin.math.roundToInt
+
+object SessionUtils {
+
+ /**
+ * Check if the app has usage stats permission
+ */
+ fun hasUsageStatsPermission(context: Context): Boolean {
+ val appOpsManager = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
+ val mode = appOpsManager.checkOpNoThrow(
+ AppOpsManager.OPSTR_GET_USAGE_STATS,
+ android.os.Process.myUid(),
+ context.packageName
+ )
+ return mode == AppOpsManager.MODE_ALLOWED
+ }
+
+ /**
+ * Open usage access settings
+ */
+ fun openUsageAccessSettings(context: Context) {
+ val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
+ context.startActivity(intent)
+ }
+
+ /**
+ * Format duration in milliseconds to human readable string
+ */
+ fun formatDuration(durationMs: Long): String {
+ val hours = TimeUnit.MILLISECONDS.toHours(durationMs)
+ val minutes = TimeUnit.MILLISECONDS.toMinutes(durationMs) % 60
+ val seconds = TimeUnit.MILLISECONDS.toSeconds(durationMs) % 60
+
+ return when {
+ hours > 0 -> "${hours}h ${minutes}m"
+ minutes > 0 -> "${minutes}m ${seconds}s"
+ else -> "${seconds}s"
+ }
+ }
+
+ /**
+ * Format duration for short display (e.g., in charts)
+ */
+ fun formatDurationShort(durationMs: Long): String {
+ val hours = durationMs / 3600000.0
+ val minutes = durationMs / 60000.0
+
+ return when {
+ hours >= 1 -> "${String.format("%.1f", hours)}h"
+ minutes >= 1 -> "${minutes.roundToInt()}m"
+ else -> "${(durationMs / 1000.0).roundToInt()}s"
+ }
+ }
+
+ /**
+ * Format timestamp to readable date
+ */
+ fun formatDate(timestamp: Long): String {
+ val format = SimpleDateFormat("MMM dd, yyyy", Locale.getDefault())
+ return format.format(Date(timestamp))
+ }
+
+ /**
+ * Format timestamp to time
+ */
+ fun formatTime(timestamp: Long): String {
+ val format = SimpleDateFormat("HH:mm", Locale.getDefault())
+ return format.format(Date(timestamp))
+ }
+
+ /**
+ * Format timestamp to date and time
+ */
+ fun formatDateTime(timestamp: Long): String {
+ val format = SimpleDateFormat("MMM dd, yyyy HH:mm", Locale.getDefault())
+ return format.format(Date(timestamp))
+ }
+
+ /**
+ * Get start of day timestamp
+ */
+ fun getStartOfDay(timestamp: Long = System.currentTimeMillis()): Long {
+ val calendar = Calendar.getInstance()
+ calendar.timeInMillis = timestamp
+ calendar.set(Calendar.HOUR_OF_DAY, 0)
+ calendar.set(Calendar.MINUTE, 0)
+ calendar.set(Calendar.SECOND, 0)
+ calendar.set(Calendar.MILLISECOND, 0)
+ return calendar.timeInMillis
+ }
+
+ /**
+ * Get end of day timestamp
+ */
+ fun getEndOfDay(timestamp: Long = System.currentTimeMillis()): Long {
+ val calendar = Calendar.getInstance()
+ calendar.timeInMillis = timestamp
+ calendar.set(Calendar.HOUR_OF_DAY, 23)
+ calendar.set(Calendar.MINUTE, 59)
+ calendar.set(Calendar.SECOND, 59)
+ calendar.set(Calendar.MILLISECOND, 999)
+ return calendar.timeInMillis
+ }
+
+ /**
+ * Get start of day timestamp for X days ago
+ */
+ fun getStartOfDayDaysAgo(daysAgo: Int): Long {
+ val calendar = Calendar.getInstance()
+ calendar.add(Calendar.DAY_OF_YEAR, -daysAgo)
+ calendar.set(Calendar.HOUR_OF_DAY, 0)
+ calendar.set(Calendar.MINUTE, 0)
+ calendar.set(Calendar.SECOND, 0)
+ calendar.set(Calendar.MILLISECOND, 0)
+ return calendar.timeInMillis
+ }
+
+ /**
+ * Convert hours to a readable percentage of day
+ */
+ fun formatScreenTimePercentage(hours: Double): String {
+ val percentage = (hours / 24.0 * 100).roundToInt()
+ return "$percentage%"
+ }
+
+ /**
+ * Get human-readable app name from package name
+ */
+ fun getAppName(context: Context, packageName: String): String {
+ return try {
+ val packageManager = context.packageManager
+ val appInfo = packageManager.getApplicationInfo(packageName, 0)
+ packageManager.getApplicationLabel(appInfo).toString()
+ } catch (e: PackageManager.NameNotFoundException) {
+ // Fallback to package name if app is not found (uninstalled apps)
+ packageName.split(".").lastOrNull() ?: packageName
+ }
+ }
+
+ /**
+ * Check if a duration is reasonable for a session
+ */
+ fun isReasonableDuration(durationMs: Long): Boolean {
+ val minDuration = 1000L // 1 second
+ val maxDuration = 4 * 60 * 60 * 1000L // 4 hours
+ return durationMs >= minDuration && durationMs <= maxDuration
+ }
+
+ /**
+ * Check if a timestamp is reasonable (not too far in the future)
+ */
+ fun isReasonableTimestamp(timestamp: Long): Boolean {
+ val currentTime = System.currentTimeMillis()
+ val maxFutureTime = currentTime + 60000 // 1 minute in the future
+ return timestamp <= maxFutureTime
+ }
+
+ /**
+ * Calculate session gap between two sessions in milliseconds
+ */
+ fun calculateSessionGap(session1EndTime: Long, session2StartTime: Long): Long {
+ return maxOf(0L, session2StartTime - session1EndTime)
+ }
+
+ /**
+ * Check if two sessions are from the same app
+ */
+ fun isSameApp(packageName1: String, packageName2: String): Boolean {
+ return packageName1 == packageName2
+ }
+
+ /**
+ * Get day of week string
+ */
+ fun getDayOfWeek(timestamp: Long): String {
+ val format = SimpleDateFormat("EEEE", Locale.getDefault())
+ return format.format(Date(timestamp))
+ }
+
+ /**
+ * Convert milliseconds to seconds with decimal precision
+ */
+ fun msToSeconds(ms: Long): Double {
+ return ms / 1000.0
+ }
+
+ /**
+ * Convert milliseconds to minutes with decimal precision
+ */
+ fun msToMinutes(ms: Long): Double {
+ return ms / 60000.0
+ }
+
+ /**
+ * Convert milliseconds to hours with decimal precision
+ */
+ fun msToHours(ms: Long): Double {
+ return ms / 3600000.0
+ }
+}
diff --git a/mobile/src/main/java/net/activitywatch/android/watcher/ChromeWatcher.kt b/mobile/src/main/java/net/activitywatch/android/watcher/ChromeWatcher.kt
deleted file mode 100644
index 80528ad2..00000000
--- a/mobile/src/main/java/net/activitywatch/android/watcher/ChromeWatcher.kt
+++ /dev/null
@@ -1,136 +0,0 @@
-package net.activitywatch.android.watcher
-
-import android.accessibilityservice.AccessibilityService
-import android.app.AlarmManager
-import android.app.AppOpsManager
-import android.app.PendingIntent
-import android.app.usage.UsageEvents
-import android.app.usage.UsageStatsManager
-import android.content.Context
-import android.content.Intent
-import android.content.pm.ApplicationInfo
-import android.content.pm.PackageManager
-import android.os.*
-import android.provider.Settings
-import android.util.Log
-import android.view.accessibility.AccessibilityEvent
-import android.view.accessibility.AccessibilityNodeInfo
-import android.widget.Toast
-import android.annotation.TargetApi
-import androidx.annotation.RequiresApi
-import net.activitywatch.android.R
-import net.activitywatch.android.RustInterface
-import net.activitywatch.android.models.Event
-import org.json.JSONObject
-import org.threeten.bp.DateTimeUtils
-import org.threeten.bp.Duration
-import org.threeten.bp.Instant
-import java.net.URL
-import java.text.ParseException
-import java.text.SimpleDateFormat
-
-class ChromeWatcher : AccessibilityService() {
-
- private val TAG = "ChromeWatcher"
- private val bucket_id = "aw-watcher-android-web-chrome"
-
- private var ri : RustInterface? = null
-
- var lastUrlTimestamp : Instant? = null
- var lastUrl : String? = null
- var lastTitle : String? = null
-
- override fun onCreate() {
- ri = RustInterface(applicationContext)
- ri?.createBucketHelper(bucket_id, "web.tab.current")
- }
-
- override fun onAccessibilityEvent(event: AccessibilityEvent) {
-
- // TODO: This method is called very often, which might affect performance. Future optimizations needed.
-
- // Only track Chrome and System events
- if (event.packageName != "com.android.chrome" && event.packageName != "com.android.systemui") {
- onUrl(null)
- return
- }
-
- try {
- if (event.source != null) {
- // Get URL
- val urlBars = event.source!!.findAccessibilityNodeInfosByViewId("com.android.chrome:id/url_bar")
- if (urlBars.any()) {
- val newUrl = "http://" + urlBars[0].text.toString() // TODO: We can't access the URI scheme, so we assume HTTP.
- onUrl(newUrl)
- }
-
- // Get title
- val webView = findWebView(event.source!!)
- if (webView != null) {
- val title = webView.text.toString()
- if (title != lastTitle) {
- lastTitle = title
- Log.i(TAG, "Title: ${lastTitle}")
- }
- }
- }
- }
- catch(ex : Exception) {
- Log.e(TAG, ex.message!!)
- }
- }
-
- fun findWebView(info : AccessibilityNodeInfo) : AccessibilityNodeInfo? {
- if(info.className == "android.webkit.WebView" && info.text != null)
- return info
-
- for (i in 0 until info.childCount) {
- val child = info.getChild(i)
- val webView = findWebView(child)
- if (webView != null) {
- return webView
- }
- child?.recycle()
- }
-
- return null
- }
-
- fun onUrl(newUrl : String?) {
- if (newUrl != lastUrl) { // URL changed
- if (newUrl != null) {
- Log.i(TAG, "Url: $newUrl")
- }
- if (lastUrl != null) {
- // Log last URL and title as a completed browser event.
- // We wait for the event to complete (marked by a change in URL) to ensure that
- // we had a chance to receive the title of the page, which often only arrives after
- // the page loads completely and/or the user interacts with the page.
- logBrowserEvent(lastUrl!!, lastTitle ?: "", lastUrlTimestamp!!)
- }
-
- lastUrlTimestamp = Instant.ofEpochMilli(System.currentTimeMillis())
- lastUrl = newUrl
- lastTitle = null
- }
- }
-
- fun logBrowserEvent(url: String, title: String, lastUrlTimestamp : Instant) {
- val now = Instant.ofEpochMilli(System.currentTimeMillis())
- val start = lastUrlTimestamp
- val end = now
- val duration = Duration.between(start, end)
-
- val data = JSONObject()
- data.put("url", url)
- data.put("title", title)
- data.put("audible", false) // TODO
- data.put("incognito", false) // TODO
-
- ri?.heartbeatHelper(bucket_id, start, duration.seconds.toDouble(), data, 1.0)
- }
-
- override fun onInterrupt() {
- TODO("not implemented")
- }
-}
diff --git a/mobile/src/main/java/net/activitywatch/android/watcher/SessionEventWatcher.kt b/mobile/src/main/java/net/activitywatch/android/watcher/SessionEventWatcher.kt
new file mode 100644
index 00000000..a2637a04
--- /dev/null
+++ b/mobile/src/main/java/net/activitywatch/android/watcher/SessionEventWatcher.kt
@@ -0,0 +1,254 @@
+package net.activitywatch.android.watcher
+
+import android.content.Context
+import android.os.AsyncTask
+import android.util.Log
+import net.activitywatch.android.RustInterface
+import net.activitywatch.android.data.AppSession
+import net.activitywatch.android.parser.SessionParser
+import net.activitywatch.android.utils.SessionUtils
+import org.json.JSONObject
+import org.threeten.bp.DateTimeUtils
+import org.threeten.bp.Instant
+import java.text.ParseException
+import java.text.SimpleDateFormat
+import java.util.Locale
+
+const val SESSION_BUCKET_ID = "aw-watcher-android-test"
+const val UNLOCK_BUCKET_ID = "aw-watcher-android-unlock"
+
+class SessionEventWatcher(val context: Context) {
+ private val ri = RustInterface(context)
+ private val sessionParser = SessionParser(context)
+ private val isoFormatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US)
+
+ var lastUpdated: Instant? = null
+
+ companion object {
+ const val TAG = "SessionEventWatcher"
+ }
+
+ /**
+ * Send individual events based on parsed sessions instead of heartbeats
+ */
+ fun sendSessionEvents() {
+ Log.w(TAG, "Starting SendSessionEventTask")
+ SendSessionEventTask().execute()
+ }
+
+ private fun getLastEventTime(): Instant? {
+ val events = ri.getEventsJSON(SESSION_BUCKET_ID, limit = 1)
+ return if (events.length() == 1) {
+ val lastEvent = events[0] as JSONObject
+ val timestampString = lastEvent.getString("timestamp")
+ try {
+ val timeCreatedDate = isoFormatter.parse(timestampString)
+ DateTimeUtils.toInstant(timeCreatedDate)
+ } catch (e: ParseException) {
+ Log.e(TAG, "Unable to parse timestamp: $timestampString")
+ null
+ }
+ } else {
+ Log.w(TAG, "More or less than one event was retrieved when trying to get last event")
+ null
+ }
+ }
+
+ private inner class SendSessionEventTask : AsyncTask() {
+ override fun doInBackground(vararg params: Void?): Int {
+ Log.i(TAG, "Sending session events...")
+
+ // Create bucket for session events
+ ri.createBucketHelper(SESSION_BUCKET_ID, "currentwindow")
+ ri.createBucketHelper(UNLOCK_BUCKET_ID, "os.lockscreen.unlocks")
+
+ lastUpdated = getLastEventTime()
+ Log.w(TAG, "lastUpdated: ${lastUpdated?.toString() ?: "never"}")
+
+ val startTimestamp = lastUpdated?.toEpochMilli() ?: 0L
+ val sessions = sessionParser.parseUsageEventsSince(startTimestamp)
+
+ var eventsSent = 0
+
+ for (session in sessions) {
+ // Insert session as individual event
+ insertSessionAsEvent(session)
+
+ if (eventsSent % 10 == 0) {
+ publishProgress(session)
+ }
+ eventsSent++
+ }
+
+ return eventsSent
+ }
+
+ override fun onProgressUpdate(vararg progress: AppSession) {
+ val session = progress[0]
+ val timestamp = DateTimeUtils.toInstant(java.util.Date(session.endTime))
+ lastUpdated = timestamp
+ Log.i(TAG, "Progress: ${session.appName} - ${lastUpdated.toString()}")
+ }
+
+ override fun onPostExecute(result: Int?) {
+ Log.w(TAG, "Finished SendSessionEventTask, sent $result session events")
+ }
+ }
+
+ /**
+ * Insert a single session as an individual event (not a heartbeat)
+ */
+ private fun insertSessionAsEvent(session: AppSession) {
+ val startInstant = DateTimeUtils.toInstant(java.util.Date(session.startTime))
+ val duration = session.durationSeconds
+ val data = session.toEventData()
+
+ // Use insertEvent method to insert as discrete event
+ // This prevents merging behavior and treats each session as a separate event
+ ri.insertEvent(SESSION_BUCKET_ID, startInstant, duration, data)
+
+ Log.d(TAG, "Inserted session event for ${session.appName}: ${SessionUtils.formatDuration(session.durationMs)} (${session.startTime} - ${session.endTime})")
+ }
+
+ /**
+ * Send session events for a specific day
+ */
+ fun sendSessionEventsForDay(dayStartMs: Long) {
+ Log.i(TAG, "Sending session events for specific day: ${SessionUtils.formatDate(dayStartMs)}")
+
+ val timeline = sessionParser.parseUsageEventsForDay(dayStartMs)
+
+ ri.createBucketHelper(SESSION_BUCKET_ID, "currentwindow")
+
+ var eventsSent = 0
+ for (session in timeline.sessions) {
+ insertSessionAsEvent(session)
+ eventsSent++
+ }
+
+ Log.i(TAG, "Sent $eventsSent session events for day")
+ }
+
+ /**
+ * Send session events for a date range
+ */
+ fun sendSessionEventsForPeriod(startTimestamp: Long, endTimestamp: Long) {
+ Log.i(TAG, "Sending session events for period: ${SessionUtils.formatDateTime(startTimestamp)} to ${SessionUtils.formatDateTime(endTimestamp)}")
+
+ val sessions = sessionParser.parseUsageEventsForPeriod(startTimestamp, endTimestamp)
+
+ ri.createBucketHelper(SESSION_BUCKET_ID, "currentwindow")
+
+ var eventsSent = 0
+ for (session in sessions) {
+ insertSessionAsEvent(session)
+ eventsSent++
+ }
+
+ Log.i(TAG, "Sent $eventsSent session events for period")
+ }
+
+ /**
+ * Insert multiple sessions as individual events
+ */
+ fun insertSessionsAsEvents(sessions: List) {
+ Log.i(TAG, "Inserting ${sessions.size} sessions as individual events")
+
+ ri.createBucketHelper(SESSION_BUCKET_ID, "currentwindow")
+
+ var eventsSent = 0
+ for (session in sessions) {
+ insertSessionAsEvent(session)
+ eventsSent++
+ }
+
+ Log.i(TAG, "Inserted $eventsSent session events")
+ }
+
+ /**
+ * Get timeline for analysis without sending events
+ */
+ fun getTimelineForDay(dayStartMs: Long) = sessionParser.parseUsageEventsForDay(dayStartMs)
+
+ /**
+ * Get sessions since last update for analysis
+ */
+ fun getSessionsSinceLastUpdate(): List {
+ val startTimestamp = lastUpdated?.toEpochMilli() ?: 0L
+ return sessionParser.parseUsageEventsSince(startTimestamp)
+ }
+
+ /**
+ * Force refresh - resend all sessions for today as individual events
+ */
+ fun forceRefreshToday() {
+ val today = SessionUtils.getStartOfDay()
+ sendSessionEventsForDay(today)
+ }
+
+ /**
+ * Send session events for the last N days
+ */
+ fun sendSessionEventsForLastDays(numberOfDays: Int) {
+ Log.i(TAG, "Sending session events for last $numberOfDays days")
+
+ ri.createBucketHelper(SESSION_BUCKET_ID, "currentwindow")
+
+ var totalEventsSent = 0
+
+ for (i in 0 until numberOfDays) {
+ val dayStart = SessionUtils.getStartOfDayDaysAgo(i)
+ val timeline = sessionParser.parseUsageEventsForDay(dayStart)
+
+ for (session in timeline.sessions) {
+ insertSessionAsEvent(session)
+ totalEventsSent++
+ }
+
+ Log.d(TAG, "Sent ${timeline.sessions.size} session events for day ${SessionUtils.formatDate(dayStart)}")
+ }
+
+ Log.i(TAG, "Sent total of $totalEventsSent session events for last $numberOfDays days")
+ }
+
+ /**
+ * Insert session as discrete event with specific timestamp and duration
+ */
+ fun insertSessionEvent(
+ packageName: String,
+ appName: String,
+ className: String = "",
+ startTime: Long,
+ durationMs: Long
+ ) {
+ val session = AppSession(
+ packageName = packageName,
+ appName = appName,
+ className = className,
+ startTime = startTime,
+ endTime = startTime + durationMs
+ )
+
+ ri.createBucketHelper(SESSION_BUCKET_ID, "currentwindow")
+ insertSessionAsEvent(session)
+
+ Log.d(TAG, "Inserted individual session event for $appName: ${SessionUtils.formatDuration(durationMs)}")
+ }
+
+ /**
+ * Clear all session events from the bucket (useful for testing)
+ */
+ fun clearSessionEvents() {
+ // Note: There's no direct clear method in RustInterface
+ // This is a placeholder for potential future implementation
+ Log.w(TAG, "Clear session events not implemented - would require new RustInterface method")
+ }
+
+ /**
+ * Get count of events in session bucket
+ */
+ fun getSessionEventCount(): Int {
+ val events = ri.getEventsJSON(SESSION_BUCKET_ID)
+ return events.length()
+ }
+}
diff --git a/mobile/src/main/java/net/activitywatch/android/watcher/UsageStatsWatcher.kt b/mobile/src/main/java/net/activitywatch/android/watcher/UsageStatsWatcher.kt
index 60247ab5..fbc04619 100644
--- a/mobile/src/main/java/net/activitywatch/android/watcher/UsageStatsWatcher.kt
+++ b/mobile/src/main/java/net/activitywatch/android/watcher/UsageStatsWatcher.kt
@@ -21,21 +21,25 @@ import android.view.accessibility.AccessibilityManager
import android.widget.Toast
import net.activitywatch.android.RustInterface
import net.activitywatch.android.models.Event
+import net.activitywatch.android.watcher.SessionEventWatcher
import org.json.JSONObject
import org.threeten.bp.DateTimeUtils
import org.threeten.bp.Instant
import java.net.URL
import java.text.ParseException
import java.text.SimpleDateFormat
+import java.util.Locale
const val bucket_id = "aw-watcher-android-test"
const val unlock_bucket_id = "aw-watcher-android-unlock"
class UsageStatsWatcher constructor(val context: Context) {
private val ri = RustInterface(context)
- private val isoFormatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
+ private val isoFormatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US)
+ private val sessionWatcher = SessionEventWatcher(context)
var lastUpdated: Instant? = null
+ var useSessionBasedEvents = true // Toggle between individual events and session-based events
enum class PermissionStatus {
@@ -261,8 +265,63 @@ class UsageStatsWatcher constructor(val context: Context) {
* Returns the number of events sent
*/
fun sendHeartbeats() {
- Log.w(TAG, "Starting SendHeartbeatTask")
- SendHeartbeatsTask().execute()
+ if (useSessionBasedEvents) {
+ Log.w(TAG, "Starting Session-based events")
+ sessionWatcher.sendSessionEvents()
+ } else {
+ Log.w(TAG, "Starting SendHeartbeatTask (individual events)")
+ SendHeartbeatsTask().execute()
+ }
+ }
+
+ /**
+ * Send session-based events for today only
+ */
+ fun sendSessionEventsForToday() {
+ sessionWatcher.forceRefreshToday()
+ }
+
+ /**
+ * Send session-based events for the last N days
+ */
+ fun sendSessionEventsForLastDays(numberOfDays: Int) {
+ sessionWatcher.sendSessionEventsForLastDays(numberOfDays)
+ }
+
+ /**
+ * Enable or disable session-based events
+ */
+ fun setSessionBasedEvents(enabled: Boolean) {
+ useSessionBasedEvents = enabled
+ Log.i(TAG, "Session-based events ${if (enabled) "enabled" else "disabled"}")
}
+ /**
+ * Enable discrete event insertion mode (recommended for accuracy)
+ */
+ fun enableDiscreteEventMode() {
+ setSessionBasedEvents(true)
+ Log.i(TAG, "Switched to discrete event insertion mode (insert_event with pulsetime=0)")
+ }
+
+ /**
+ * Enable heartbeat mode (traditional merging behavior)
+ */
+ fun enableHeartbeatMode() {
+ setSessionBasedEvents(false)
+ Log.i(TAG, "Switched to heartbeat mode (traditional event merging)")
+ }
+
+ /**
+ * Check if currently using discrete events
+ */
+ fun isUsingDiscreteEvents(): Boolean = useSessionBasedEvents
+
+ /**
+ * Get current timeline for analysis
+ */
+ fun getTodayTimeline() = sessionWatcher.getTimelineForDay(
+ net.activitywatch.android.utils.SessionUtils.getStartOfDay()
+ )
+
}
diff --git a/mobile/src/main/java/net/activitywatch/android/watcher/WebWatcher.kt b/mobile/src/main/java/net/activitywatch/android/watcher/WebWatcher.kt
new file mode 100644
index 00000000..1950b8ff
--- /dev/null
+++ b/mobile/src/main/java/net/activitywatch/android/watcher/WebWatcher.kt
@@ -0,0 +1,164 @@
+package net.activitywatch.android.watcher
+
+import android.accessibilityservice.AccessibilityService
+import android.util.Log
+import android.view.accessibility.AccessibilityEvent
+import android.view.accessibility.AccessibilityNodeInfo
+import androidx.core.util.Function
+import net.activitywatch.android.RustInterface
+import org.json.JSONObject
+import org.threeten.bp.Duration
+import org.threeten.bp.Instant
+
+private typealias UrlExtractor = (AccessibilityEvent) -> String?
+
+private fun extractTextByViewId(
+ event: AccessibilityEvent,
+ viewId: String,
+ transformer: Function? = null
+) = event.source
+ ?.findAccessibilityNodeInfosByViewId(viewId)
+ ?.firstOrNull()?.text?.toString()
+ ?.let { originalValue -> transformer?.apply(originalValue) ?: originalValue }
+
+class WebWatcher : AccessibilityService() {
+
+ private val stripProtocol: (String) -> String = { url ->
+ url.removePrefix("http://").removePrefix("https://")
+ }
+
+ private val TAG = "WebWatcher"
+ private val bucket_id = "aw-watcher-android-web"
+
+ private var ri : RustInterface? = null
+
+ private var lastUrlTimestamp : Instant? = null
+ private var lastUrl : String? = null
+ private var lastBrowser: String? = null
+ private var lastWindowTitle : String? = null
+ private var lastWindowId: Int? = null
+
+ private val urlExtractors : Map = mapOf(
+ "com.android.chrome" to { event ->
+ extractTextByViewId(event, "com.android.chrome:id/url_bar")
+ },
+ "org.mozilla.firefox" to { event ->
+ // Firefox has multiple variants depending on version
+ extractTextByViewId(event, "org.mozilla.firefox:id/url_bar_title")
+ ?: extractTextByViewId(event, "org.mozilla.firefox:id/mozac_browser_toolbar_url_view")
+ },
+ "com.sec.android.app.sbrowser" to { event ->
+ extractTextByViewId(event, "com.sec.android.app.sbrowser:id/location_bar_edit_text")
+ ?: extractTextByViewId(event, "com.sec.android.app.sbrowser:id/custom_tab_toolbar_url_bar_text", transformer = stripProtocol)
+ },
+ "com.opera.browser" to { event ->
+ extractTextByViewId(event, "com.opera.browser:id/url_field")
+ ?: extractTextByViewId(event, "com.opera.browser:id/address_field")
+ },
+ "com.microsoft.emmx" to { event ->
+ extractTextByViewId(event, "com.microsoft.emmx:id/url_bar")
+ }
+ )
+
+ override fun onCreate() {
+ Log.i(TAG, "Creating WebWatcher")
+ ri = RustInterface(applicationContext).also { it.createBucketHelper(bucket_id, "web.tab.current") }
+ }
+
+ // TODO: This method is called very often, which might affect performance. Future optimizations needed.
+ override fun onAccessibilityEvent(event: AccessibilityEvent) {
+ if (shouldIgnoreEvent(event)) {
+ return
+ }
+
+ val packageName = event.packageName?.toString()
+ val urlExtractor = urlExtractors[packageName]
+
+ val windowChanged = windowChanged(event.windowId)
+ lastWindowId = event.windowId
+
+ if (urlExtractor == null) {
+ // for some browsers like Firefox event.packageName can be null (no extractor matched)
+ // but we are still on the same window
+ if (windowChanged) {
+ handleUrl(null, newBrowser = null)
+ }
+
+ return
+ }
+
+ try {
+ event.source?.let { source ->
+ val newUrl = urlExtractor(event)
+
+ newUrl?.let { handleUrl(it, newBrowser = packageName) }.also {
+ findWebView(source)
+ ?.let { handleWindowTitle(it.text.toString()) }
+ }
+ }
+ } catch(ex : Exception) {
+ Log.e(TAG, ex.message!!)
+ }
+ }
+
+ private fun windowChanged(windowId: Int): Boolean = windowId != lastWindowId
+
+ private fun shouldIgnoreEvent(event: AccessibilityEvent) =
+ event.packageName == "com.android.systemui"
+
+ private fun findWebView(info : AccessibilityNodeInfo) : AccessibilityNodeInfo? {
+ if (info.className == "android.webkit.WebView" && info.text != null) return info
+
+ return (0 until info.childCount)
+ .mapNotNull { info.getChild(it) }
+ .firstNotNullOfOrNull { child ->
+ findWebView(child).also { child.recycle() }
+ }
+ }
+
+ private fun handleUrl(newUrl : String?, newBrowser: String?) {
+ if (newUrl != lastUrl || newBrowser != lastBrowser) {
+ newUrl?.let { Log.i(TAG, "Url: $it, browser: $newBrowser") }
+ lastUrl?.let { url ->
+ lastBrowser?.let { browser ->
+ // Log last URL and title as a completed browser event.
+ // We wait for the event to complete (marked by a change in URL) to ensure that
+ // we had a chance to receive the title of the page, which often only arrives after
+ // the page loads completely and/or the user interacts with the page.
+ val windowTitle = lastWindowTitle ?: ""
+ logBrowserEvent(url, browser, windowTitle, lastUrlTimestamp!!)
+ }
+ }
+
+ lastUrlTimestamp = Instant.ofEpochMilli(System.currentTimeMillis())
+ lastUrl = newUrl
+ lastBrowser = newBrowser
+ lastWindowTitle = null
+ }
+ }
+
+ private fun handleWindowTitle(newWindowTitle: String) {
+ if (newWindowTitle != lastWindowTitle) {
+ lastWindowTitle = newWindowTitle
+ Log.i(TAG, "Title: $lastWindowTitle")
+ }
+ }
+
+ private fun logBrowserEvent(url: String, browser: String, windowTitle: String, lastUrlTimestamp : Instant) {
+ val now = Instant.ofEpochMilli(System.currentTimeMillis())
+ val start = lastUrlTimestamp
+ val duration = Duration.between(lastUrlTimestamp, now)
+
+ val data = JSONObject()
+ .put("url", url)
+ .put("browser", browser)
+ .put("title", windowTitle)
+ .put("audible", false) // TODO
+ .put("incognito", false) // TODO
+
+ Log.i(TAG, "Registered event: $data")
+ ri?.heartbeatHelper(bucket_id, start, duration.seconds.toDouble(), data, 1.0)
+ }
+
+ override fun onInterrupt() {}
+}