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
5 changes: 4 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ manifest disagree. `AutomationPropertiesTests` walks every helper's page.
A helper's view model is created once per process and kept while the user
visits other helpers, so a running timer keeps running. A helper whose state
must survive the process being killed persists it itself, through
`HelperServices.Settings` (`IHelperSettings`).
`HelperServices.Settings` (`IHelperSettings`). Einsatz data, such as the
Hydrantenkarte's Einsatzstelle, goes through `HelperServices.DeviceOnlySettings`
instead: Android's backup copies `Settings` into the user's Google account, and
`BackupRulesTests` holds the line that it never copies the device-only file.

## Mobile

Expand Down
1 change: 1 addition & 0 deletions changelog.d/+backup-einsatzstelle.security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The Hydrantenkarte's Einsatzstelle and the downloaded Kartenpakete no longer go into Android's cloud backup in the user's Google account.
7 changes: 7 additions & 0 deletions src/FwAssist.App.Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.sensor.compass" android:required="false" />
<!-- Android's Auto Backup copies the app's data into the user's Google account. The two rule
files keep the Einsatzstelle (helper-settings-device) and the Kartenpakete (files/maps) out
of it: the first is Einsatz data, the second large enough to push the app past the backup
quota, after which Android backs up nothing. data_extraction_rules is read from Android 12,
fullBackupContent before. BackupRulesTests holds the line. -->
<application android:label="FwAssist" android:icon="@drawable/ic_launcher"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:theme="@style/MyTheme.NoActionBar" android:usesCleartextTraffic="false">

<!-- singleTask: a helper's own icon brings the running app forward and arrives through
Expand Down
5 changes: 3 additions & 2 deletions src/FwAssist.App.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ protected override void OnCreate(global::Android.OS.Bundle? savedInstanceState)
new HelperServices(
time,
new DispatcherTimerTicker(),
new AndroidHelperSettings(context),
new AndroidHelperSettings(context, AndroidHelperSettings.BackedUp),
new RegionPackLibrary(Http, Path.Join(files, "maps")),
_location,
_compass,
_signal),
_signal,
new AndroidHelperSettings(context, AndroidHelperSettings.DeviceOnly)),
new AndroidLauncherIcons(context),
new AndroidExternalLinks(context),
AppVersionText.Of(typeof(MainActivity).Assembly));
Expand Down
7 changes: 7 additions & 0 deletions src/FwAssist.App.Android/Resources/xml/backup_rules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android 11 and older, which read this instead of data_extraction_rules.xml. Same exclusions:
the Einsatzstelle and the Kartenpakete never go into the user's Google account. -->
<full-backup-content>
<exclude domain="sharedpref" path="helper-settings-device.xml" />
<exclude domain="file" path="maps/" />
</full-backup-content>
11 changes: 11 additions & 0 deletions src/FwAssist.App.Android/Resources/xml/data_extraction_rules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android 12 and newer. Cloud backup goes to the user's Google account, so the Einsatzstelle and
the Kartenpakete stay out of it; see the comment on <application> in AndroidManifest.xml. A
device-to-device transfer goes only to the user's own new phone and keeps everything.
The names match AndroidHelperSettings.DeviceOnly and the "maps" folder in MainActivity. -->
<data-extraction-rules>
<cloud-backup>
<exclude domain="sharedpref" path="helper-settings-device.xml" />
<exclude domain="file" path="maps/" />
</cloud-backup>
</data-extraction-rules>
19 changes: 15 additions & 4 deletions src/FwAssist.App.Android/Services/AndroidHelperSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@

namespace FwAssist.App.Android.Services;

/// <summary>Helper settings in the app's private SharedPreferences. Nothing leaves the phone.</summary>
/// <summary>
/// Helper settings in one of the app's private SharedPreferences files. The app sends none of it
/// anywhere; Android's own backup may copy <see cref="BackedUp"/>, but never <see cref="DeviceOnly"/>.
/// </summary>
internal sealed class AndroidHelperSettings : IHelperSettings
{
private const string FileName = "helper-settings";
/// <summary>Small preferences that Android's backup may carry to a new phone.</summary>
public const string BackedUp = "helper-settings";

/// <summary>
/// Einsatz data. Resources/xml/data_extraction_rules.xml and backup_rules.xml exclude this file
/// by name, as <c>helper-settings-device.xml</c>; rename it only together with them.
/// </summary>
public const string DeviceOnly = "helper-settings-device";

private readonly ISharedPreferences _preferences;

public AndroidHelperSettings(Context context)
public AndroidHelperSettings(Context context, string fileName)
{
ArgumentNullException.ThrowIfNull(context);
_preferences = context.GetSharedPreferences(FileName, FileCreationMode.Private)
ArgumentException.ThrowIfNullOrEmpty(fileName);
_preferences = context.GetSharedPreferences(fileName, FileCreationMode.Private)
?? throw new InvalidOperationException("Android returned no SharedPreferences.");
}

Expand Down
7 changes: 6 additions & 1 deletion src/FwAssist.AppLogic/Helpers/HelperServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ namespace FwAssist.AppLogic.Helpers;
/// <param name="Location">The phone's live position; null where the platform offers none.</param>
/// <param name="Compass">Which way the phone points; null where the platform offers none.</param>
/// <param name="Signal">A tone and vibration for something the user must notice; null stays quiet.</param>
/// <param name="DeviceOnlySettings">
/// Like <paramref name="Settings"/>, for values that must never leave the phone, not even into
/// Android's backup: Einsatz data such as the Hydrantenkarte's Einsatzstelle. Null keeps nothing.
/// </param>
public sealed record HelperServices(
TimeProvider Time,
ITicker Ticker,
IHelperSettings? Settings = null,
IRegionPackLibrary? Maps = null,
ILocationSource? Location = null,
ICompass? Compass = null,
IAttentionSignal? Signal = null);
IAttentionSignal? Signal = null,
IHelperSettings? DeviceOnlySettings = null);
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using FwAssist.AppLogic.Helpers;
using FwAssist.AppLogic.Services;

namespace FwAssist.Helpers.Hydranten;

Expand All @@ -20,6 +21,25 @@ public sealed class HydrantenModule : IHelperModule
public ObservableObject CreateViewModel(HelperServices services)
{
ArgumentNullException.ThrowIfNull(services);
return new HydrantenkarteViewModel(services.Maps, services.Settings, services.Location, services.Compass, services.Time, services.Signal);
MoveEinsatzstelleOutOfBackup(services.Settings, services.DeviceOnlySettings);
return new HydrantenkarteViewModel(services.Maps, services.DeviceOnlySettings, services.Location, services.Compass, services.Time, services.Signal);
}

// Up to 0.2.1 the Einsatzstelle sat in the settings Android backs up. A copy found there moves
// to the device-only settings, unless those already hold a newer one, and is blanked either way:
// IHelperSettings cannot remove a key, and empty is what the view model writes for "none".
private static void MoveEinsatzstelleOutOfBackup(IHelperSettings? backedUp, IHelperSettings? deviceOnly)
{
if (backedUp?.Get(HydrantenkarteViewModel.EinsatzstelleKey) is not { Length: > 0 } old)
{
return;
}

if (deviceOnly is not null && string.IsNullOrEmpty(deviceOnly.Get(HydrantenkarteViewModel.EinsatzstelleKey)))
{
deviceOnly.Set(HydrantenkarteViewModel.EinsatzstelleKey, old);
}

backedUp.Set(HydrantenkarteViewModel.EinsatzstelleKey, string.Empty);
}
}
49 changes: 49 additions & 0 deletions tests/FwAssist.Acceptance.Tests/BackupRulesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Xml.Linq;

namespace FwAssist.Acceptance.Tests;

// Android's Auto Backup copies an app's preferences and files into the user's Google account. Two
// things must stay out: the device-only preferences, where the Hydrantenkarte keeps the Einsatzstelle,
// and the Kartenpakete, which are large enough to push the app past the backup quota, after which
// Android backs up nothing at all. Android 12+ reads data_extraction_rules, older versions backup_rules.
public class BackupRulesTests
{
private const string DeviceOnlyPreferences = "helper-settings-device.xml";
private const string Kartenpakete = "maps/";

private static readonly XNamespace Android = "http://schemas.android.com/apk/res/android";

private static XDocument Load(string name) => XDocument.Load(Path.Join(AppContext.BaseDirectory, name));

private static List<(string Domain, string Path)> Excluded(XElement rules) =>
rules.Elements("exclude")
.Select(e => ((string?)e.Attribute("domain") ?? string.Empty, (string?)e.Attribute("path") ?? string.Empty))
.ToList();

[Fact]
public void The_manifest_names_both_rule_files()
{
var application = Assert.Single(Load("AndroidManifest.xml").Descendants("application"));

Assert.Equal("@xml/data_extraction_rules", (string?)application.Attribute(Android + "dataExtractionRules"));
Assert.Equal("@xml/backup_rules", (string?)application.Attribute(Android + "fullBackupContent"));
}

[Fact]
public void The_cloud_backup_of_android_12_and_newer_leaves_out_the_einsatzstelle_and_the_kartenpakete()
{
var cloud = Assert.Single(Load("data_extraction_rules.xml").Elements("data-extraction-rules").Elements("cloud-backup"));

Assert.Contains(("sharedpref", DeviceOnlyPreferences), Excluded(cloud));
Assert.Contains(("file", Kartenpakete), Excluded(cloud));
}

[Fact]
public void The_backup_of_android_11_and_older_leaves_out_the_einsatzstelle_and_the_kartenpakete()
{
var rules = Assert.Single(Load("backup_rules.xml").Elements("full-backup-content"));

Assert.Contains(("sharedpref", DeviceOnlyPreferences), Excluded(rules));
Assert.Contains(("file", Kartenpakete), Excluded(rules));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<!-- Read by LauncherAliasTests: the manifest is the Android head's, which this project cannot
reference (it targets net10.0-android). -->
<None Include="..\..\src\FwAssist.App.Android\AndroidManifest.xml" Link="AndroidManifest.xml" CopyToOutputDirectory="PreserveNewest" />
<!-- Read by BackupRulesTests, for the same reason. -->
<None Include="..\..\src\FwAssist.App.Android\Resources\xml\data_extraction_rules.xml" Link="data_extraction_rules.xml" CopyToOutputDirectory="PreserveNewest" />
<None Include="..\..\src\FwAssist.App.Android\Resources\xml\backup_rules.xml" Link="backup_rules.xml" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
</ItemGroup>

<ItemGroup>
<!-- The handwritten Kartenpaket library the AppLogic tests use, and the shared clock. -->
<!-- The handwritten Kartenpaket library the AppLogic tests use, the shared clock, and the ticker
HydrantenModuleTests hands the module. -->
<Compile Include="..\FwAssist.AppLogic.Tests\FakeRegionPackLibrary.cs" Link="Shared\FakeRegionPackLibrary.cs" />
<Compile Include="..\Shared\FakeTimeProvider.cs" Link="Shared\FakeTimeProvider.cs" />
<Compile Include="..\Shared\ManualTicker.cs" Link="Shared\ManualTicker.cs" />
</ItemGroup>

</Project>
69 changes: 69 additions & 0 deletions tests/FwAssist.Helpers.Hydranten.Tests/HydrantenModuleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using FwAssist.AppLogic.Helpers;
using FwAssist.AppLogic.Services;
using FwAssist.Core.Map;
using FwAssist.Testing;

namespace FwAssist.Helpers.Hydranten.Tests;

// The Einsatzstelle is where a real Einsatz is, so it lives in the settings Android never backs up.
public class HydrantenModuleTests
{
private const string Key = HydrantenkarteViewModel.EinsatzstelleKey;

private static readonly GeoPoint Marktplatz = new(48.1785, 11.2555);
private static readonly GeoPoint Bahnhof = new(48.1702, 11.2489);

private readonly InMemoryHelperSettings _backedUp = new();
private readonly InMemoryHelperSettings _deviceOnly = new();

private HydrantenkarteViewModel Open() =>
Assert.IsType<HydrantenkarteViewModel>(new HydrantenModule().CreateViewModel(
new HelperServices(new FakeTimeProvider(), new ManualTicker(), _backedUp, DeviceOnlySettings: _deviceOnly)));

private static string? Shown(HydrantenkarteViewModel vm) =>
vm.Einsatzstelle is { } at ? HydrantenkarteViewModel.Persisted(at) : null;

[Fact]
public void The_einsatzstelle_comes_from_the_device_only_settings()
{
_deviceOnly.Set(Key, HydrantenkarteViewModel.Persisted(Marktplatz));

Assert.Equal(HydrantenkarteViewModel.Persisted(Marktplatz), Shown(Open()));
}

[Fact]
public void Clearing_the_einsatzstelle_writes_only_the_device_only_settings()
{
_deviceOnly.Set(Key, HydrantenkarteViewModel.Persisted(Marktplatz));
var vm = Open();

vm.EinsatzstelleLoeschenCommand.Execute(null);

Assert.Equal(string.Empty, _deviceOnly.Get(Key));
Assert.Null(_backedUp.Get(Key));
}

[Fact]
public void An_einsatzstelle_from_before_the_update_moves_out_of_the_backed_up_settings()
{
_backedUp.Set(Key, HydrantenkarteViewModel.Persisted(Marktplatz));

var vm = Open();

Assert.Equal(HydrantenkarteViewModel.Persisted(Marktplatz), Shown(vm));
Assert.Equal(HydrantenkarteViewModel.Persisted(Marktplatz), _deviceOnly.Get(Key));
Assert.Equal(string.Empty, _backedUp.Get(Key));
}

[Fact]
public void A_device_only_einsatzstelle_wins_over_an_old_copy_which_is_still_removed()
{
_backedUp.Set(Key, HydrantenkarteViewModel.Persisted(Marktplatz));
_deviceOnly.Set(Key, HydrantenkarteViewModel.Persisted(Bahnhof));

var vm = Open();

Assert.Equal(HydrantenkarteViewModel.Persisted(Bahnhof), Shown(vm));
Assert.Equal(string.Empty, _backedUp.Get(Key));
}
}