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..9a001de1 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,10 +1,11 @@ + - + 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..0d78d160 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 +$(JNI_arm7)/libaw_sync.so: $(TARGETDIR_arm7)/$(RELEASE_TYPE)/libaw_sync.so + mkdir -p $$(dirname $@) + # if target is empty, then create symlink + 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,17 @@ $(RS_SRCDIR)/target/%/$(RELEASE_TYPE)/libaw_server.so: $(RS_SOURCES) $(WEBUI_DIS env RUSTFLAGS=$(RUSTFLAGS_ANDROID) make -C aw-server-rust android; \ fi +# Same rule for libaw_sync.so (but without webui dependency) +$(RS_SRCDIR)/target/%/$(RELEASE_TYPE)/libaw_sync.so: $(RS_SOURCES) + @echo $@ + @echo "Release type: $(RELEASE_TYPE)" + @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 +236,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/aw-server-rust b/aw-server-rust index dc70318e..9567a1ae 160000 --- a/aw-server-rust +++ b/aw-server-rust @@ -1 +1 @@ -Subproject commit dc70318e819efc0d0535a5d7bd35a0c7ab8e9106 +Subproject commit 9567a1ae5c57a36a7ac50915f3eaa12f5157da1c 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/gradle.properties b/gradle.properties index 3d8ce0ca..ee656e91 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,3 +15,6 @@ org.gradle.jvmargs=-Xmx1536m kotlin.code.style=official android.useAndroidX=true android.enableJetifier=true + +# Enable native symbol preservation for debugging +# doNotStrip=true 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..5045bc6b 100644 --- a/mobile/build.gradle +++ b/mobile/build.gradle @@ -7,7 +7,7 @@ android { defaultConfig { applicationId "net.activitywatch.android" - minSdkVersion 24 + minSdkVersion 26 targetSdkVersion 34 // Set in CI on tagged commit @@ -29,6 +29,7 @@ android { } buildTypes { release { + applicationIdSuffix ".debug" minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' ndk { @@ -42,20 +43,29 @@ 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")) { - // packagingOptions { - // doNotStrip '**/*.so' - // } - //} + if (project.hasProperty("doNotStrip")) { + println "Configuring doNotStrip for jniLibs" + packaging { + jniLibs { + keepDebugSymbols += "**/*.so" + } + } + } // Creates a resource versionName with the full version // https://stackoverflow.com/a/36468650/965332 @@ -69,19 +79,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 +102,15 @@ 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' + } + + // WorkManager + implementation "androidx.work:work-runtime-ktx:2.9.1" + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3' } // 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..cc5ac29a 100644 --- a/mobile/src/main/AndroidManifest.xml +++ b/mobile/src/main/AndroidManifest.xml @@ -1,20 +1,20 @@ + xmlns:tools="http://schemas.android.com/tools" + tools:ignore="MissingLeanbackLauncher"> - - + + + @@ -39,6 +39,7 @@ + - @@ -67,5 +68,11 @@ + + diff --git a/mobile/src/main/java/net/activitywatch/android/BackgroundService.kt b/mobile/src/main/java/net/activitywatch/android/BackgroundService.kt new file mode 100644 index 00000000..0980a49e --- /dev/null +++ b/mobile/src/main/java/net/activitywatch/android/BackgroundService.kt @@ -0,0 +1,119 @@ +package net.activitywatch.android + +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.IBinder +import android.util.Log +import androidx.core.app.NotificationCompat + +private const val TAG = "BackgroundService" +private const val CHANNEL_ID = "aw_background_channel" +private const val NOTIFICATION_ID = 1 + +class BackgroundService : Service() { + + private lateinit var syncScheduler: SyncScheduler + private lateinit var rustInterface: RustInterface + + override fun onCreate() { + super.onCreate() + Log.i(TAG, "BackgroundService created") + rustInterface = RustInterface(this) + syncScheduler = SyncScheduler(this) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + Log.i(TAG, "BackgroundService started") + + createNotificationChannel() + val notification = createNotification() + startForeground(NOTIFICATION_ID, notification) + + // Start the server + rustInterface.startServerTask() + + // Start the sync scheduler + syncScheduler.start() + + // Schedule event parsing + scheduleEventParsing() + + return START_STICKY + } + + private fun scheduleEventParsing() { + val currentDate = java.util.Calendar.getInstance() + val dueDate = java.util.Calendar.getInstance() + // Set to midnight + dueDate.set(java.util.Calendar.HOUR_OF_DAY, 0) + dueDate.set(java.util.Calendar.MINUTE, 0) + dueDate.set(java.util.Calendar.SECOND, 0) + if (dueDate.before(currentDate)) { + dueDate.add(java.util.Calendar.HOUR_OF_DAY, 24) + } + val timeDiff = dueDate.timeInMillis - currentDate.timeInMillis + + val saveRequest = androidx.work.PeriodicWorkRequest.Builder( + net.activitywatch.android.workers.EventParsingWorker::class.java, + 24, java.util.concurrent.TimeUnit.HOURS + ) + .setInitialDelay(timeDiff, java.util.concurrent.TimeUnit.MILLISECONDS) + .addTag("EventParsing") + .build() + + androidx.work.WorkManager.getInstance(this).enqueueUniquePeriodicWork( + "EventParsingWorker", + androidx.work.ExistingPeriodicWorkPolicy.UPDATE, + saveRequest + ) + Log.i(TAG, "Scheduled event parsing worker with initial delay: ${timeDiff}ms") + } + + override fun onDestroy() { + Log.i(TAG, "BackgroundService destroyed") + syncScheduler.stop() + super.onDestroy() + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } + + private fun createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val name = "ActivityWatch Background Service" + val descriptionText = "Keeps ActivityWatch server and sync running in the background" + val importance = NotificationManager.IMPORTANCE_LOW + val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { + description = descriptionText + } + val notificationManager: NotificationManager = + getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + notificationManager.createNotificationChannel(channel) + } + } + + private fun createNotification(): Notification { + val pendingIntent: PendingIntent = + Intent(this, MainActivity::class.java).let { notificationIntent -> + PendingIntent.getActivity( + this, 0, notificationIntent, + PendingIntent.FLAG_IMMUTABLE + ) + } + + return NotificationCompat.Builder(this, CHANNEL_ID) + .setContentTitle("ActivityWatch Server") + .setContentText("Server and sync running in background") + .setSmallIcon(R.mipmap.aw_launcher_round) // Make sure this icon exists or use a fallback + .setContentIntent(pendingIntent) + .setPriority(NotificationCompat.PRIORITY_LOW) + .build() + } +} diff --git a/mobile/src/main/java/net/activitywatch/android/MainActivity.kt b/mobile/src/main/java/net/activitywatch/android/MainActivity.kt index 0c439e0f..8b05b770 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() { @@ -56,8 +58,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte binding.navView.setNavigationItemSelectedListener(this) - val ri = RustInterface(this) - ri.startServerTask(this) + // Start background service to keep server and sync running + val serviceIntent = Intent(this, BackgroundService::class.java) + startForegroundService(serviceIntent) if (savedInstanceState != null) { return @@ -65,6 +68,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 +86,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 +162,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte binding.drawerLayout.closeDrawer(GravityCompat.START) return true } + + override fun onDestroy() { + super.onDestroy() + // syncScheduler.stop() // Handled by BackgroundService + } } 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/SyncAlarmReceiver.kt b/mobile/src/main/java/net/activitywatch/android/SyncAlarmReceiver.kt new file mode 100644 index 00000000..22d38564 --- /dev/null +++ b/mobile/src/main/java/net/activitywatch/android/SyncAlarmReceiver.kt @@ -0,0 +1,43 @@ +package net.activitywatch.android + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.util.Log + +private const val TAG = "SyncAlarmReceiver" + +class SyncAlarmReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + Log.i(TAG, "SyncAlarmReceiver called with action: ${intent.action}") + + when (intent.action) { + "net.activitywatch.android.SYNC_ALARM" -> { + Log.i(TAG, "Performing scheduled sync...") + performSync(context) + } + "android.intent.action.BOOT_COMPLETED" -> { + Log.i(TAG, "Device booted, rescheduling sync alarm") + SyncScheduler(context).start() + } + else -> { + Log.w(TAG, "Unknown intent action: ${intent.action}") + } + } + } + + private fun performSync(context: Context) { + try { + val syncInterface = SyncInterface(context) + syncInterface.syncBothAsync { success, message -> + if (success) { + Log.i(TAG, "Automatic sync completed successfully: $message") + } else { + Log.w(TAG, "Automatic sync failed: $message") + } + } + } catch (e: Exception) { + Log.e(TAG, "Failed to perform sync", e) + } + } +} 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..07c99d56 --- /dev/null +++ b/mobile/src/main/java/net/activitywatch/android/SyncInterface.kt @@ -0,0 +1,115 @@ +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) + + // Set XDG environment variables to app-writable paths + // This is required for aw-client-rust (used by aw-sync) to create lock files + val cacheDir = context.cacheDir.absolutePath + val filesDir = context.filesDir.absolutePath + + Os.setenv("XDG_CACHE_HOME", cacheDir, true) + Os.setenv("XDG_CONFIG_HOME", "$filesDir/config", true) + Os.setenv("XDG_DATA_HOME", "$filesDir/data", 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 { + Log.i(TAG, "Starting sync operation: $operation") + 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..4e70fb0c --- /dev/null +++ b/mobile/src/main/java/net/activitywatch/android/SyncScheduler.kt @@ -0,0 +1,63 @@ +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 + Log.i(TAG, "Scheduling 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/AlarmReceiver.kt b/mobile/src/main/java/net/activitywatch/android/watcher/AlarmReceiver.kt index c4bdd9be..8ec71d75 100644 --- a/mobile/src/main/java/net/activitywatch/android/watcher/AlarmReceiver.kt +++ b/mobile/src/main/java/net/activitywatch/android/watcher/AlarmReceiver.kt @@ -12,9 +12,14 @@ class AlarmReceiver : BroadcastReceiver() { Log.w(TAG, "AlarmReceiver called") val usw = UsageStatsWatcher(context) if (intent.action == "android.intent.action.BOOT_COMPLETED") { - // FIXME: Doesn't seem to be triggered as it should - Log.w(TAG, "Received BOOT_COMPLETED, setting up alarm") + Log.w(TAG, "Received BOOT_COMPLETED, setting up alarm and starting background service") usw.setupAlarm() + val serviceIntent = Intent(context, net.activitywatch.android.BackgroundService::class.java) + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + context.startForegroundService(serviceIntent) + } else { + context.startService(serviceIntent) + } } else if(intent.action == "net.activitywatch.android.watcher.LOG_DATA") { Log.w(TAG, "Action ${intent.action}, running sendHeartbeats") if(UsageStatsWatcher.isUsageAllowed(context)) { 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..20493fa4 --- /dev/null +++ b/mobile/src/main/java/net/activitywatch/android/watcher/SessionEventWatcher.kt @@ -0,0 +1,247 @@ +package net.activitywatch.android.watcher + +import android.content.Context +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 +import kotlinx.coroutines.* + +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") + CoroutineScope(Dispatchers.IO).launch { + val result = processEventsSinceLastUpdate() + Log.w(TAG, "Finished SendSessionEventTask, sent $result session events") + } + } + + 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 + } + } + + /** + * Synchronously process events since last update. + * Returns the number of events sent. + */ + fun processEventsSinceLastUpdate(): Int { + Log.i(TAG, "Processing 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) + eventsSent++ + } + + Log.i(TAG, "Finished processing events, sent $eventsSent session events") + return eventsSent + } + + + + /** + * 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..b4b7921c --- /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 { if (it == null) 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() {} +} diff --git a/mobile/src/main/java/net/activitywatch/android/workers/EventParsingWorker.kt b/mobile/src/main/java/net/activitywatch/android/workers/EventParsingWorker.kt new file mode 100644 index 00000000..6220c491 --- /dev/null +++ b/mobile/src/main/java/net/activitywatch/android/workers/EventParsingWorker.kt @@ -0,0 +1,25 @@ +package net.activitywatch.android.workers + +import android.content.Context +import android.util.Log +import androidx.work.Worker +import androidx.work.WorkerParameters +import net.activitywatch.android.watcher.SessionEventWatcher + +private const val TAG = "EventParsingWorker" + +class EventParsingWorker(context: Context, params: WorkerParameters) : Worker(context, params) { + + override fun doWork(): Result { + Log.i(TAG, "Starting periodic event parsing") + return try { + val watcher = SessionEventWatcher(applicationContext) + val eventsSent = watcher.processEventsSinceLastUpdate() + Log.i(TAG, "Successfully processed events: $eventsSent") + Result.success() + } catch (e: Exception) { + Log.e(TAG, "Error processing events", e) + Result.retry() + } + } +}