Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ jobs:
with:
distribution: temurin
java-version: 17
- uses: gradle/actions/setup-gradle@v6
- uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1
if: github.event_name != 'pull_request' || steps.filter.outputs.code == 'true'
- uses: android-actions/setup-android@v4
- uses: android-actions/setup-android@be39fa834029ff78f1a44aa3bb0819b8fc2bd8fd # v4.0.4
if: github.event_name != 'pull_request' || steps.filter.outputs.code == 'true'
- name: Unit tests and coverage
if: github.event_name != 'pull_request' || steps.filter.outputs.code == 'true'
Expand All @@ -86,7 +86,7 @@ jobs:
name: roborazzi-diffs
path: app/build/outputs/roborazzi/
if-no-files-found: ignore
- uses: codecov/codecov-action@v7
- uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1
if: github.event_name == 'push'
with:
files: app/build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ jobs:
distribution: 'temurin'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v6
uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1

- name: Setup Android SDK
uses: android-actions/setup-android@v4
uses: android-actions/setup-android@be39fa834029ff78f1a44aa3bb0819b8fc2bd8fd # v4.0.4

- name: Decode keystore
run: echo "${{ secrets.KEYSTORE_BASE64 }}" | base64 -d > app/release.keystore
Expand All @@ -68,7 +68,7 @@ jobs:
run: rm -f app/release.keystore

- name: Create GitHub Release
uses: softprops/action-gh-release@v3
uses: softprops/action-gh-release@8a0cd651e5b2365bd640854a3fa5dfc70e4875f3 # v3.0.4
with:
tag_name: ${{ steps.version.outputs.tag }}
name: ${{ steps.version.outputs.tag }}
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Android monitoring agent for CashPilot that tracks passive income apps (EarnApp,
- Ktor HTTP client
- Kotlinx Serialization
- DataStore Preferences
- Android SDK 26+ (minSdk), compileSdk 36, targetSdk 35
- Android SDK 26+ (minSdk), compileSdk 37, targetSdk 36
- Gradle with version catalogs (`libs.versions.toml`)
- ProGuard (release builds: `isMinifyEnabled = true`, `isShrinkResources = true`)

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
<application
android:name=".CashPilotApp"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:supportsRtl="true"
android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/Theme.CashPilotAndroid">
android:theme="@style/Theme.CashPilotAndroid"
tools:targetApi="31">

<activity
android:name=".ui.MainActivity"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/cashpilot/android/service/Heartbeat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ object Heartbeat {
fun outcomeOf(status: Int): Outcome = if (status == 401 || status == 403) Outcome.KEY_REFUSED else Outcome.REJECTED

private const val TAG = "Heartbeat"
private const val PREFS = "heartbeat_state"
/** SharedPreferences name. res/xml/data_extraction_rules.xml keeps it on this device. */
internal const val PREFS = "heartbeat_state"
private const val K_LAST = "last_heartbeat"
private const val K_FAILED = "last_heartbeat_failed"
private const val K_EARNINGS = "earnings_json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import com.cashpilot.android.model.Settings
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "cashpilot_settings")
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = SettingsStore.FILE_NAME)

object SettingsStore {
/** The DataStore name. res/xml/data_extraction_rules.xml keeps its file on this device. */
internal const val FILE_NAME = "cashpilot_settings"

private val SERVER_URL = stringPreferencesKey("server_url")
private val API_KEY = stringPreferencesKey("api_key")
private val WORKER_KEY = stringPreferencesKey("worker_key")
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/xml/data_extraction_rules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android 12 and later ignore allowBackup="false" for device-to-device
transfer, so without these rules the settings DataStore, which holds the
fleet key and this phone's worker key, is copied to a new phone. Keep it on
this device in both cases: a new phone sets up again and gets its own
worker key. The heartbeat state (last server URL and earnings snapshot)
stays too, so that phone starts clean. DataBackupRulesTest checks both
are excluded here. -->
<data-extraction-rules>
<cloud-backup>
<exclude domain="file" path="datastore" />
<exclude domain="sharedpref" path="heartbeat_state.xml" />
</cloud-backup>
<device-transfer>
<exclude domain="file" path="datastore" />
<exclude domain="sharedpref" path="heartbeat_state.xml" />
</device-transfer>
</data-extraction-rules>
101 changes: 101 additions & 0 deletions app/src/test/java/com/cashpilot/android/DataBackupRulesTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.cashpilot.android

import android.content.Context
import androidx.datastore.preferences.preferencesDataStoreFile
import com.cashpilot.android.service.Heartbeat
import com.cashpilot.android.util.SettingsStore
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.xmlpull.v1.XmlPullParser

/**
* The settings DataStore holds the fleet key and this phone's worker key, and
* must not leave the device.
*
* allowBackup="false" stops cloud backup, but Android 12 and later ignore it for
* device-to-device transfer. Only the dataExtractionRules file can keep the
* DataStore off a new phone, so this checks the manifest points at it and that
* the DataStore file and the heartbeat state are excluded in both sections.
*/
// Same SDK as IconGoldenTest: Robolectric 4.17 cannot run this module's targetSdk.
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34])
class DataBackupRulesTest {

private val context: Context = RuntimeEnvironment.getApplication()

/** domain/path excludes per section, read from the merged resource. */
private fun excludes(): Map<String, List<Pair<String, String>>> {
val result = mutableMapOf<String, MutableList<Pair<String, String>>>()
val parser = context.resources.getXml(R.xml.data_extraction_rules)
var section: String? = null
while (parser.next() != XmlPullParser.END_DOCUMENT) {
if (parser.eventType != XmlPullParser.START_TAG) continue
when (parser.name) {
"cloud-backup", "device-transfer" -> {
section = parser.name
result.getOrPut(parser.name) { mutableListOf() }
}
"exclude" -> result.getValue(section!!).add(
parser.getAttributeValue(null, "domain") to
parser.getAttributeValue(null, "path"),
)
}
}
return result
}

@Test
fun `the manifest points at the data extraction rules`() {
// Robolectric does not fill ApplicationInfo.dataExtractionRulesRes, so
// read the merged manifest that the Android Gradle plugin hands it.
val props = java.util.Properties()
javaClass.classLoader!!.getResourceAsStream("com/android/tools/test_config.properties")!!
.use { props.load(it) }
val manifest = java.io.File(props.getProperty("android_merged_manifest"))
val doc = javax.xml.parsers.DocumentBuilderFactory.newInstance()
.apply { isNamespaceAware = true }.newDocumentBuilder().parse(manifest)
val app = doc.getElementsByTagName("application").item(0) as org.w3c.dom.Element
val android = "http://schemas.android.com/apk/res/android"
assertEquals("@xml/data_extraction_rules", app.getAttributeNS(android, "dataExtractionRules"))
}

/**
* The path of the settings DataStore, relative to filesDir, from the same
* function the preferencesDataStore delegate uses. Nothing is written: the
* delegate is one store per process, bound to whichever test touched it
* first, so a file written from here can land in another test's filesDir.
*/
private fun settingsFile(): String =
context.preferencesDataStoreFile(SettingsStore.FILE_NAME).relativeTo(context.filesDir).path

private fun assertExcluded(domain: String, file: String) {
val rules = excludes()
for (section in listOf("cloud-backup", "device-transfer")) {
val paths = rules[section].orEmpty()
.filter { it.first == domain }.map { it.second.trimEnd('/') }
assertTrue(
"$section does not exclude $domain:$file",
paths.any { it == "." || file == it || file.startsWith("$it/") },
)
}
}

@Test
fun `the settings DataStore is kept out of cloud backup and device transfer`() {
assertEquals("datastore/cashpilot_settings.preferences_pb", settingsFile())
assertExcluded("file", settingsFile())
}

@Test
fun `the heartbeat state is kept out of cloud backup and device transfer`() {
// Last server URL and earnings snapshot: no key, but a phone set up
// again starts clean, as allowBackup="false" intends.
assertExcluded("sharedpref", "${Heartbeat.PREFS}.xml")
}
}
Loading