From 8714cfcfab71ad39e1e5b3ec63f673a7fa1de602 Mon Sep 17 00:00:00 2001 From: alexSSB Date: Mon, 16 Nov 2026 22:31:25 +0200 Subject: [PATCH 1/3] Fix FileStatisticsDataEx reading the access time as the modified time ModifiedTime and ChangedTime were placed one 8-byte field early, so ModifiedTime overlapped AccessTime (offset 44) and ChangedTime read the modified time (52). Per adbd's sync_stat_v2 layout they sit at 52 and 60. The listing tests used one timestamp for all three times, which hid the overlap; the first entry now has distinct times. Co-Authored-By: Claude Opus 5.5 --- .../SyncServiceTests.Async.cs | 18 ++++++++++-------- .../SyncServiceTests.cs | 9 +++++---- .../Models/FileStatisticsData.V2.cs | 4 ++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs b/AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs index 8764882..9bd8c58 100644 --- a/AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs +++ b/AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs @@ -178,9 +178,9 @@ public async Task GetListingExAsyncTest() 208, 7, 0, 0, 13, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 151, 130, 56, 86, 0, 0, 0, 0, 152, 130, 56, 86, 0, 0, 0, 0, - 152, 130, 56, 86, 0, 0, 0, 0, - 152, 130, 56, 86, 0, 0, 0, 0 + 153, 130, 56, 86, 0, 0, 0, 0 ], [ 0, 0, 0, 0, @@ -247,9 +247,10 @@ public async Task GetListingExAsyncTest() Assert.Equal(0u, dir.Size); Assert.Equal(2000u, dir.UserId); Assert.Equal(9997u, dir.GroupId); - Assert.Equal(time, dir.AccessTime); + // Distinct times, so a field read from another's offset fails + Assert.Equal(time.AddSeconds(-1), dir.AccessTime); Assert.Equal(time, dir.ModifiedTime); - Assert.Equal(time, dir.ChangedTime); + Assert.Equal(time.AddSeconds(1), dir.ChangedTime); Assert.Equal($"drwxr-x--x\t0\t{dir.ModifiedTime}\t.", dir.ToString()); FileStatisticsEx parentDir = value[1]; @@ -382,9 +383,9 @@ public async Task GetAsyncListingExTest() 208, 7, 0, 0, 13, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 151, 130, 56, 86, 0, 0, 0, 0, 152, 130, 56, 86, 0, 0, 0, 0, - 152, 130, 56, 86, 0, 0, 0, 0, - 152, 130, 56, 86, 0, 0, 0, 0 + 153, 130, 56, 86, 0, 0, 0, 0 ], [ 0, 0, 0, 0, @@ -451,9 +452,10 @@ public async Task GetAsyncListingExTest() Assert.Equal(0u, dir.Size); Assert.Equal(2000u, dir.UserId); Assert.Equal(9997u, dir.GroupId); - Assert.Equal(time, dir.AccessTime); + // Distinct times, so a field read from another's offset fails + Assert.Equal(time.AddSeconds(-1), dir.AccessTime); Assert.Equal(time, dir.ModifiedTime); - Assert.Equal(time, dir.ChangedTime); + Assert.Equal(time.AddSeconds(1), dir.ChangedTime); Assert.Equal($"drwxr-x--x\t0\t{dir.ModifiedTime}\t.", dir.ToString()); FileStatisticsEx parentDir = value[1]; diff --git a/AdvancedSharpAdbClient.Tests/SyncServiceTests.cs b/AdvancedSharpAdbClient.Tests/SyncServiceTests.cs index 859d122..c73334a 100644 --- a/AdvancedSharpAdbClient.Tests/SyncServiceTests.cs +++ b/AdvancedSharpAdbClient.Tests/SyncServiceTests.cs @@ -185,9 +185,9 @@ public void GetListingExTest() 208, 7, 0, 0, 13, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 151, 130, 56, 86, 0, 0, 0, 0, 152, 130, 56, 86, 0, 0, 0, 0, - 152, 130, 56, 86, 0, 0, 0, 0, - 152, 130, 56, 86, 0, 0, 0, 0 + 153, 130, 56, 86, 0, 0, 0, 0 ], [ 0, 0, 0, 0, @@ -253,9 +253,10 @@ public void GetListingExTest() Assert.Equal(0u, dir.Size); Assert.Equal(2000u, dir.UserId); Assert.Equal(9997u, dir.GroupId); - Assert.Equal(time, dir.AccessTime); + // Distinct times, so a field read from another's offset fails + Assert.Equal(time.AddSeconds(-1), dir.AccessTime); Assert.Equal(time, dir.ModifiedTime); - Assert.Equal(time, dir.ChangedTime); + Assert.Equal(time.AddSeconds(1), dir.ChangedTime); Assert.Equal($"drwxr-x--x\t0\t{dir.ModifiedTime}\t.", dir.ToString()); FileStatisticsEx parentDir = value[1]; diff --git a/AdvancedSharpAdbClient/Models/FileStatisticsData.V2.cs b/AdvancedSharpAdbClient/Models/FileStatisticsData.V2.cs index 8a57903..291412e 100644 --- a/AdvancedSharpAdbClient/Models/FileStatisticsData.V2.cs +++ b/AdvancedSharpAdbClient/Models/FileStatisticsData.V2.cs @@ -96,13 +96,13 @@ public readonly record struct FileStatisticsDataEx(uint Error, ulong Device, ulo /// /// Gets the timestamp indicating when the item was last modified. /// - [field: FieldOffset((5 * sizeof(uint)) + (2 * sizeof(ulong)) + sizeof(ulong))] + [field: FieldOffset((5 * sizeof(uint)) + (3 * sizeof(ulong)) + sizeof(long))] public long ModifiedTime { get; init; } = ModifiedTime; /// /// Gets the timestamp indicating when the last change occurred. /// - [field: FieldOffset((5 * sizeof(uint)) + (2 * sizeof(ulong)) + (2 * sizeof(ulong)))] + [field: FieldOffset((5 * sizeof(uint)) + (3 * sizeof(ulong)) + (2 * sizeof(long)))] public long ChangedTime { get; init; } = ChangedTime; /// From 3a6e307b23637a72d5334f419399868985b33386 Mon Sep 17 00:00:00 2001 From: alexSSB Date: Mon, 16 Nov 2026 22:44:43 +0200 Subject: [PATCH 2/3] Fix split install counts racing and depending on the callback InstallMultiplePackageAsync counted finished splits with ++count inside callback?.Invoke(...) in parallel continuations, which had three effects: - Without a callback, the argument (and ++count) was never evaluated, so every multi-package install threw "2 should process but only 0 processed". - Two continuations could increment at once and lose one, throwing "only 1 processed" (the intermittent InstallMultiplePackageAsyncTest failure). - Reports could arrive out of order, PackageFinished going 2 then 1. The count now increments on its own, and with the callback under a lock. InstallMultiplePackageWithoutCallbackAsyncTest covers the first case. Co-Authored-By: Claude Opus 5.5 --- .../PackageManagerTests.Async.cs | 38 +++++++++++++++++++ .../DeviceCommands/PackageManager.Async.cs | 36 ++++++++++++++++-- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs b/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs index 82bfe86..1a7c345 100644 --- a/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs +++ b/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs @@ -209,6 +209,44 @@ await manager.InstallMultiplePackageAsync(["Assets/TestApp/split_config.arm64_v8 Assert.True(syncService.UploadedFiles.ContainsKey("/data/local/tmp/split_config.xxhdpi.apk")); } + /// + /// Tests the method without a callback. + /// + [Fact] + public async Task InstallMultiplePackageWithoutCallbackAsyncTest() + { + DummySyncService syncService = new(); + + DummyAdbClient adbClient = new(); + + adbClient.Commands["shell:pm list packages -f"] = "package:/system/app/Gallery2/Gallery2.apk=com.android.gallery3d"; + adbClient.Commands["shell:pm install-create"] = "Success: created install session [936013062]"; + adbClient.Commands["shell:pm install-create -p com.google.android.gms"] = "Success: created install session [936013062]"; + adbClient.Commands["shell:pm install-write 936013062 base.apk \"/data/local/tmp/base.apk\""] = "Success"; + adbClient.Commands["shell:pm install-write 936013062 split0.apk \"/data/local/tmp/split_config.arm64_v8a.apk\""] = "Success"; + adbClient.Commands["shell:pm install-write 936013062 split1.apk \"/data/local/tmp/split_config.xxhdpi.apk\""] = "Success"; + adbClient.Commands["shell:pm install-commit 936013062"] = "Success"; + adbClient.Commands["shell:rm \"/data/local/tmp/base.apk\""] = string.Empty; + adbClient.Commands["shell:rm \"/data/local/tmp/split_config.arm64_v8a.apk\""] = string.Empty; + adbClient.Commands["shell:rm \"/data/local/tmp/split_config.xxhdpi.apk\""] = string.Empty; + + PackageManager manager = new(adbClient, Device, (c, d) => syncService); + + // The split counts must not depend on a callback being there to report them + await manager.InstallMultiplePackageAsync("Assets/TestApp/base.apk", ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], + (System.Action?)null, TestContext.Current.CancellationToken); + + Assert.Equal(9, adbClient.ReceivedCommands.Count); + Assert.Equal("shell:rm \"/data/local/tmp/base.apk\"", adbClient.ReceivedCommands[8]); + + adbClient.ReceivedCommands.Clear(); + + await manager.InstallMultiplePackageAsync(["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], "com.google.android.gms", + (System.Action?)null, TestContext.Current.CancellationToken); + + Assert.Equal(6, adbClient.ReceivedCommands.Count); + } + [Fact] public async Task UninstallPackageAsyncTest() { diff --git a/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs b/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs index adf958c..bb1263f 100644 --- a/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs +++ b/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs @@ -167,11 +167,18 @@ void OnSplitSyncProgressChanged(string? sender, SyncProgressChangedEventArgs arg await InstallMultipleRemotePackageAsync(baseRemoteFilePath, splitRemoteFilePaths, callback, cancellationToken, arguments); int count = 0; + // Continuations finish in parallel: counting outside the callback keeps it when there's none, + // and the lock keeps the count exact and its reports in order + object progressLock = new(); callback?.Invoke(new InstallProgressEventArgs(0, splitRemoteFilePaths.Length + 1, PackageInstallProgressState.PostInstall)); await splitRemoteFilePaths.Select(async x => { await RemoveRemotePackageAsync(x, cancellationToken).ConfigureAwait(false); - callback?.Invoke(new InstallProgressEventArgs(++count, splitRemoteFilePaths.Length + 1, PackageInstallProgressState.PostInstall)); + lock (progressLock) + { + count++; + callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFilePaths.Length + 1, PackageInstallProgressState.PostInstall)); + } }).WhenAll().ConfigureAwait(false); if (count < splitRemoteFilePaths.Length) @@ -237,11 +244,18 @@ void OnSyncProgressChanged(string? sender, SyncProgressChangedEventArgs args) await InstallMultipleRemotePackageAsync(splitRemoteFilePaths, packageName, callback, cancellationToken, arguments); int count = 0; + // Continuations finish in parallel: counting outside the callback keeps it when there's none, + // and the lock keeps the count exact and its reports in order + object progressLock = new(); callback?.Invoke(new InstallProgressEventArgs(0, splitRemoteFilePaths.Length, PackageInstallProgressState.PostInstall)); await splitRemoteFilePaths.Select(async x => { await RemoveRemotePackageAsync(x, cancellationToken).ConfigureAwait(false); - callback?.Invoke(new InstallProgressEventArgs(++count, splitRemoteFilePaths.Length, PackageInstallProgressState.PostInstall)); + lock (progressLock) + { + count++; + callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFilePaths.Length, PackageInstallProgressState.PostInstall)); + } }).WhenAll().ConfigureAwait(false); if (count < splitRemoteFilePaths.Length) @@ -279,10 +293,17 @@ public async Task InstallMultipleRemotePackageAsync(string baseRemoteFilePath, I callback?.Invoke(new InstallProgressEventArgs(1, splitRemoteFileCount + 1, PackageInstallProgressState.WriteSession)); int count = 0; + // Continuations finish in parallel: counting outside the callback keeps it when there's none, + // and the lock keeps the count exact and its reports in order + object progressLock = new(); await splitRemoteFilePaths.Select(async (splitRemoteFilePath, index) => { await WriteInstallSessionAsync(session, $"split{index}", splitRemoteFilePath, cancellationToken).ConfigureAwait(false); - callback?.Invoke(new InstallProgressEventArgs(++count, splitRemoteFileCount + 1, PackageInstallProgressState.WriteSession)); + lock (progressLock) + { + count++; + callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFileCount + 1, PackageInstallProgressState.WriteSession)); + } }).WhenAll().ConfigureAwait(false); if (count < splitRemoteFileCount) @@ -324,10 +345,17 @@ public async Task InstallMultipleRemotePackageAsync(IEnumerable splitRem callback?.Invoke(new InstallProgressEventArgs(0, splitRemoteFileCount, PackageInstallProgressState.WriteSession)); int count = 0; + // Continuations finish in parallel: counting outside the callback keeps it when there's none, + // and the lock keeps the count exact and its reports in order + object progressLock = new(); await splitRemoteFilePaths.Select(async (splitRemoteFilePath, index) => { await WriteInstallSessionAsync(session, $"split{index}", splitRemoteFilePath, cancellationToken).ConfigureAwait(false); - callback?.Invoke(new InstallProgressEventArgs(++count, splitRemoteFileCount, PackageInstallProgressState.WriteSession)); + lock (progressLock) + { + count++; + callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFileCount, PackageInstallProgressState.WriteSession)); + } }).WhenAll().ConfigureAwait(false); if (count < splitRemoteFileCount) From 30b99efb5f79398bccc84fcadcf05c76aa6df991 Mon Sep 17 00:00:00 2001 From: where where Date: Tue, 17 Nov 2026 12:13:57 +0800 Subject: [PATCH 3/3] Remove comment Fix warning --- .../PackageManagerTests.Async.cs | 31 +++++++---- .../DeviceCommands/PackageManagerTests.cs | 54 +++++++++++++++++-- .../DeviceCommands/PackageManager.Async.cs | 24 +++------ 3 files changed, 79 insertions(+), 30 deletions(-) diff --git a/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs b/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs index 1a7c345..52b0cb5 100644 --- a/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs +++ b/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Threading.Tasks; using Xunit; @@ -24,10 +25,10 @@ public async Task InstallRemotePackageAsyncTest() adbClient.ReceivedCommands.Clear(); - await manager.InstallRemotePackageAsync("/data/base.apk", new InstallProgress(PackageInstallProgressState.Installing), TestContext.Current.CancellationToken, new string[] { "-r", "-t" }); + await manager.InstallRemotePackageAsync("/data/base.apk", new InstallProgress(PackageInstallProgressState.Installing), TestContext.Current.CancellationToken, ["-r", "-t"]); - Assert.Single(adbClient.ReceivedCommands); - Assert.Equal("shell:pm install -r -t \"/data/base.apk\"", adbClient.ReceivedCommands[0]); + string item = Assert.Single(adbClient.ReceivedCommands); + Assert.Equal("shell:pm install -r -t \"/data/base.apk\"", item); } [Fact] @@ -156,7 +157,9 @@ public async Task InstallMultiplePackageAsyncTest() PackageManager manager = new(adbClient, Device, (c, d) => syncService); - await manager.InstallMultiplePackageAsync("Assets/TestApp/base.apk", ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], + await manager.InstallMultiplePackageAsync( + "Assets/TestApp/base.apk", + ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], new InstallProgress( PackageInstallProgressState.Preparing, PackageInstallProgressState.Uploading, @@ -185,7 +188,9 @@ await manager.InstallMultiplePackageAsync("Assets/TestApp/base.apk", ["Assets/Te syncService.UploadedFiles.Clear(); adbClient.ReceivedCommands.Clear(); - await manager.InstallMultiplePackageAsync(["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], "com.google.android.gms", + await manager.InstallMultiplePackageAsync( + ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], + "com.google.android.gms", new InstallProgress( PackageInstallProgressState.Preparing, PackageInstallProgressState.Uploading, @@ -233,16 +238,22 @@ public async Task InstallMultiplePackageWithoutCallbackAsyncTest() PackageManager manager = new(adbClient, Device, (c, d) => syncService); // The split counts must not depend on a callback being there to report them - await manager.InstallMultiplePackageAsync("Assets/TestApp/base.apk", ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], - (System.Action?)null, TestContext.Current.CancellationToken); + await manager.InstallMultiplePackageAsync( + "Assets/TestApp/base.apk", + ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], + callback: null, + TestContext.Current.CancellationToken); Assert.Equal(9, adbClient.ReceivedCommands.Count); Assert.Equal("shell:rm \"/data/local/tmp/base.apk\"", adbClient.ReceivedCommands[8]); adbClient.ReceivedCommands.Clear(); - await manager.InstallMultiplePackageAsync(["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], "com.google.android.gms", - (System.Action?)null, TestContext.Current.CancellationToken); + await manager.InstallMultiplePackageAsync( + ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], + "com.google.android.gms", + callback: null, + TestContext.Current.CancellationToken); Assert.Equal(6, adbClient.ReceivedCommands.Count); } diff --git a/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.cs b/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.cs index 52a9057..508d1ee 100644 --- a/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.cs +++ b/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.cs @@ -54,8 +54,8 @@ public void InstallRemotePackageTest() manager.InstallRemotePackage("/data/base.apk", new InstallProgress(PackageInstallProgressState.Installing), "-r", "-t"); - Assert.Single(adbClient.ReceivedCommands); - Assert.Equal("shell:pm install -r -t \"/data/base.apk\"", adbClient.ReceivedCommands[0]); + string item = Assert.Single(adbClient.ReceivedCommands); + Assert.Equal("shell:pm install -r -t \"/data/base.apk\"", item); } [Fact] @@ -181,7 +181,9 @@ public void InstallMultiplePackageTest() PackageManager manager = new(adbClient, Device, (c, d) => syncService); - manager.InstallMultiplePackage("Assets/TestApp/base.apk", ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], + manager.InstallMultiplePackage( + "Assets/TestApp/base.apk", + ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], new InstallProgress( PackageInstallProgressState.Preparing, PackageInstallProgressState.Uploading, @@ -209,7 +211,9 @@ public void InstallMultiplePackageTest() syncService.UploadedFiles.Clear(); adbClient.ReceivedCommands.Clear(); - manager.InstallMultiplePackage(["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], "com.google.android.gms", + manager.InstallMultiplePackage( + ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], + "com.google.android.gms", new InstallProgress( PackageInstallProgressState.Preparing, PackageInstallProgressState.Uploading, @@ -232,6 +236,48 @@ public void InstallMultiplePackageTest() Assert.True(syncService.UploadedFiles.ContainsKey("/data/local/tmp/split_config.xxhdpi.apk")); } + /// + /// Tests the method without a callback. + /// + [Fact] + public void InstallMultiplePackageWithoutCallbackTest() + { + DummySyncService syncService = new(); + + DummyAdbClient adbClient = new(); + + adbClient.Commands["shell:pm list packages -f"] = "package:/system/app/Gallery2/Gallery2.apk=com.android.gallery3d"; + adbClient.Commands["shell:pm install-create"] = "Success: created install session [936013062]"; + adbClient.Commands["shell:pm install-create -p com.google.android.gms"] = "Success: created install session [936013062]"; + adbClient.Commands["shell:pm install-write 936013062 base.apk \"/data/local/tmp/base.apk\""] = "Success"; + adbClient.Commands["shell:pm install-write 936013062 split0.apk \"/data/local/tmp/split_config.arm64_v8a.apk\""] = "Success"; + adbClient.Commands["shell:pm install-write 936013062 split1.apk \"/data/local/tmp/split_config.xxhdpi.apk\""] = "Success"; + adbClient.Commands["shell:pm install-commit 936013062"] = "Success"; + adbClient.Commands["shell:rm \"/data/local/tmp/base.apk\""] = string.Empty; + adbClient.Commands["shell:rm \"/data/local/tmp/split_config.arm64_v8a.apk\""] = string.Empty; + adbClient.Commands["shell:rm \"/data/local/tmp/split_config.xxhdpi.apk\""] = string.Empty; + + PackageManager manager = new(adbClient, Device, (c, d) => syncService); + + // The split counts must not depend on a callback being there to report them + manager.InstallMultiplePackage( + "Assets/TestApp/base.apk", + ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], + callback: null); + + Assert.Equal(9, adbClient.ReceivedCommands.Count); + Assert.Equal("shell:rm \"/data/local/tmp/base.apk\"", adbClient.ReceivedCommands[8]); + + adbClient.ReceivedCommands.Clear(); + + manager.InstallMultiplePackage( + ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], + "com.google.android.gms", + callback: null); + + Assert.Equal(6, adbClient.ReceivedCommands.Count); + } + [Fact] public void UninstallPackageTest() { diff --git a/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs b/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs index bb1263f..571c8fd 100644 --- a/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs +++ b/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs @@ -167,14 +167,12 @@ void OnSplitSyncProgressChanged(string? sender, SyncProgressChangedEventArgs arg await InstallMultipleRemotePackageAsync(baseRemoteFilePath, splitRemoteFilePaths, callback, cancellationToken, arguments); int count = 0; - // Continuations finish in parallel: counting outside the callback keeps it when there's none, - // and the lock keeps the count exact and its reports in order - object progressLock = new(); + object locker = new(); callback?.Invoke(new InstallProgressEventArgs(0, splitRemoteFilePaths.Length + 1, PackageInstallProgressState.PostInstall)); await splitRemoteFilePaths.Select(async x => { await RemoveRemotePackageAsync(x, cancellationToken).ConfigureAwait(false); - lock (progressLock) + lock (locker) { count++; callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFilePaths.Length + 1, PackageInstallProgressState.PostInstall)); @@ -244,14 +242,12 @@ void OnSyncProgressChanged(string? sender, SyncProgressChangedEventArgs args) await InstallMultipleRemotePackageAsync(splitRemoteFilePaths, packageName, callback, cancellationToken, arguments); int count = 0; - // Continuations finish in parallel: counting outside the callback keeps it when there's none, - // and the lock keeps the count exact and its reports in order - object progressLock = new(); + object locker = new(); callback?.Invoke(new InstallProgressEventArgs(0, splitRemoteFilePaths.Length, PackageInstallProgressState.PostInstall)); await splitRemoteFilePaths.Select(async x => { await RemoveRemotePackageAsync(x, cancellationToken).ConfigureAwait(false); - lock (progressLock) + lock (locker) { count++; callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFilePaths.Length, PackageInstallProgressState.PostInstall)); @@ -293,13 +289,11 @@ public async Task InstallMultipleRemotePackageAsync(string baseRemoteFilePath, I callback?.Invoke(new InstallProgressEventArgs(1, splitRemoteFileCount + 1, PackageInstallProgressState.WriteSession)); int count = 0; - // Continuations finish in parallel: counting outside the callback keeps it when there's none, - // and the lock keeps the count exact and its reports in order - object progressLock = new(); + object locker = new(); await splitRemoteFilePaths.Select(async (splitRemoteFilePath, index) => { await WriteInstallSessionAsync(session, $"split{index}", splitRemoteFilePath, cancellationToken).ConfigureAwait(false); - lock (progressLock) + lock (locker) { count++; callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFileCount + 1, PackageInstallProgressState.WriteSession)); @@ -345,13 +339,11 @@ public async Task InstallMultipleRemotePackageAsync(IEnumerable splitRem callback?.Invoke(new InstallProgressEventArgs(0, splitRemoteFileCount, PackageInstallProgressState.WriteSession)); int count = 0; - // Continuations finish in parallel: counting outside the callback keeps it when there's none, - // and the lock keeps the count exact and its reports in order - object progressLock = new(); + object locker = new(); await splitRemoteFilePaths.Select(async (splitRemoteFilePath, index) => { await WriteInstallSessionAsync(session, $"split{index}", splitRemoteFilePath, cancellationToken).ConfigureAwait(false); - lock (progressLock) + lock (locker) { count++; callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFileCount, PackageInstallProgressState.WriteSession));