Skip to content

Commit fe936cf

Browse files
committed
Improve Linq
1 parent 5def8c5 commit fe936cf

10 files changed

Lines changed: 60 additions & 65 deletions

File tree

AdvancedSharpAdbClient.Tests/AdbClientTests.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void GetDevicesTest()
119119
"host:devices-l"
120120
};
121121

122-
List<DeviceData> devices = null;
122+
IEnumerable<DeviceData> devices = null;
123123

124124
RunTest(
125125
OkResponse,
@@ -173,20 +173,17 @@ public void SetInvalidDeviceTest()
173173
[Fact]
174174
public void SetDeviceOtherException()
175175
{
176-
lock (FactoriesTests.locker)
176+
string[] requests = new string[]
177177
{
178-
string[] requests = new string[]
179-
{
180-
"host:transport:169.254.109.177:5555"
181-
};
178+
"host:transport:169.254.109.177:5555"
179+
};
182180

183-
_ = Assert.Throws<AdbException>(() =>
184-
RunTest(
185-
new AdbResponse[] { AdbResponse.FromError("Too many cats.") },
186-
NoResponseMessages,
187-
requests,
188-
() => Socket.SetDevice(Device)));
189-
}
181+
_ = Assert.Throws<AdbException>(() =>
182+
RunTest(
183+
new AdbResponse[] { AdbResponse.FromError("Too many cats.") },
184+
NoResponseMessages,
185+
requests,
186+
() => Socket.SetDevice(Device)));
190187
}
191188

192189
[Fact]

AdvancedSharpAdbClient.Tests/Dummys/DummyAdbClient.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ public Task ExecuteRemoteCommandAsync(string command, DeviceData device, IShellO
135135

136136
public Task<AppStatus> GetAppStatusAsync(DeviceData device, string packageName, CancellationToken cancellationToken) => throw new NotImplementedException();
137137

138-
public List<DeviceData> GetDevices() => throw new NotImplementedException();
138+
public IEnumerable<DeviceData> GetDevices() => throw new NotImplementedException();
139139

140-
public Task<List<DeviceData>> GetDevicesAsync(CancellationToken cancellationToken) => throw new NotImplementedException();
140+
public Task<IEnumerable<DeviceData>> GetDevicesAsync(CancellationToken cancellationToken) => throw new NotImplementedException();
141141

142-
public List<string> GetFeatureSet(DeviceData device) => throw new NotImplementedException();
142+
public IEnumerable<string> GetFeatureSet(DeviceData device) => throw new NotImplementedException();
143143

144-
public Task<List<string>> GetFeatureSetAsync(DeviceData device, CancellationToken cancellationToken) => throw new NotImplementedException();
144+
public Task<IEnumerable<string>> GetFeatureSetAsync(DeviceData device, CancellationToken cancellationToken) => throw new NotImplementedException();
145145

146146
public Task<Framebuffer> GetFrameBufferAsync(DeviceData device, CancellationToken cancellationToken) => throw new NotImplementedException();
147147

@@ -165,17 +165,17 @@ public Task ExecuteRemoteCommandAsync(string command, DeviceData device, IShellO
165165

166166
public Task<string> InstallCreateAsync(DeviceData device, string packageName, CancellationToken cancellationToken, params string[] arguments) => throw new NotImplementedException();
167167

168-
public void InstallMultiple(DeviceData device, Stream[] splitAPKs, string packageName, params string[] arguments) => throw new NotImplementedException();
168+
public void InstallMultiple(DeviceData device, IEnumerable<Stream> splitAPKs, string packageName, params string[] arguments) => throw new NotImplementedException();
169169

170-
public void InstallMultiple(DeviceData device, Stream baseAPK, Stream[] splitAPKs, params string[] arguments) => throw new NotImplementedException();
170+
public void InstallMultiple(DeviceData device, Stream baseAPK, IEnumerable<Stream> splitAPKs, params string[] arguments) => throw new NotImplementedException();
171171

172-
public Task InstallMultipleAsync(DeviceData device, Stream[] splitAPKs, string packageName, params string[] arguments) => throw new NotImplementedException();
172+
public Task InstallMultipleAsync(DeviceData device, IEnumerable<Stream> splitAPKs, string packageName, params string[] arguments) => throw new NotImplementedException();
173173

174-
public Task InstallMultipleAsync(DeviceData device, Stream[] splitAPKs, string packageName, CancellationToken cancellationToken, params string[] arguments) => throw new NotImplementedException();
174+
public Task InstallMultipleAsync(DeviceData device, IEnumerable<Stream> splitAPKs, string packageName, CancellationToken cancellationToken, params string[] arguments) => throw new NotImplementedException();
175175

176-
public Task InstallMultipleAsync(DeviceData device, Stream baseAPK, Stream[] splitAPKs, params string[] arguments) => throw new NotImplementedException();
176+
public Task InstallMultipleAsync(DeviceData device, Stream baseAPK, IEnumerable<Stream> splitAPKs, params string[] arguments) => throw new NotImplementedException();
177177

178-
public Task InstallMultipleAsync(DeviceData device, Stream baseAPK, Stream[] splitAPKs, CancellationToken cancellationToken, params string[] arguments) => throw new NotImplementedException();
178+
public Task InstallMultipleAsync(DeviceData device, Stream baseAPK, IEnumerable<Stream> splitAPKs, CancellationToken cancellationToken, params string[] arguments) => throw new NotImplementedException();
179179

180180
public void InstallWrite(DeviceData device, Stream apk, string apkName, string session) => throw new NotImplementedException();
181181

AdvancedSharpAdbClient/AdbClient.Async.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public async Task KillAdbAsync(CancellationToken cancellationToken = default)
4646
}
4747

4848
/// <inheritdoc/>
49-
public async Task<List<DeviceData>> GetDevicesAsync(CancellationToken cancellationToken = default)
49+
public async Task<IEnumerable<DeviceData>> GetDevicesAsync(CancellationToken cancellationToken = default)
5050
{
5151
using IAdbSocket socket = adbSocketFactory(EndPoint);
5252
await socket.SendAdbRequestAsync("host:devices-l", cancellationToken);
@@ -55,7 +55,7 @@ public async Task<List<DeviceData>> GetDevicesAsync(CancellationToken cancellati
5555

5656
string[] data = reply.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
5757

58-
return data.Select(DeviceData.CreateFromAdbData).ToList();
58+
return data.Select(DeviceData.CreateFromAdbData);
5959
}
6060

6161
/// <inheritdoc/>
@@ -449,11 +449,11 @@ public async Task InstallAsync(DeviceData device, Stream apk, CancellationToken
449449
}
450450

451451
/// <inheritdoc/>
452-
public Task InstallMultipleAsync(DeviceData device, Stream[] splitAPKs, string packageName, params string[] arguments) =>
452+
public Task InstallMultipleAsync(DeviceData device, IEnumerable<Stream> splitAPKs, string packageName, params string[] arguments) =>
453453
InstallMultipleAsync(device, splitAPKs, packageName, default, arguments);
454454

455455
/// <inheritdoc/>
456-
public async Task InstallMultipleAsync(DeviceData device, Stream[] splitAPKs, string packageName, CancellationToken cancellationToken, params string[] arguments)
456+
public async Task InstallMultipleAsync(DeviceData device, IEnumerable<Stream> splitAPKs, string packageName, CancellationToken cancellationToken, params string[] arguments)
457457
{
458458
EnsureDevice(device);
459459

@@ -484,11 +484,11 @@ public async Task InstallMultipleAsync(DeviceData device, Stream[] splitAPKs, st
484484
}
485485

486486
/// <inheritdoc/>
487-
public Task InstallMultipleAsync(DeviceData device, Stream baseAPK, Stream[] splitAPKs, params string[] arguments) =>
487+
public Task InstallMultipleAsync(DeviceData device, Stream baseAPK, IEnumerable<Stream> splitAPKs, params string[] arguments) =>
488488
InstallMultipleAsync(device, baseAPK, splitAPKs, default, arguments);
489489

490490
/// <inheritdoc/>
491-
public async Task InstallMultipleAsync(DeviceData device, Stream baseAPK, Stream[] splitAPKs, CancellationToken cancellationToken, params string[] arguments)
491+
public async Task InstallMultipleAsync(DeviceData device, Stream baseAPK, IEnumerable<Stream> splitAPKs, CancellationToken cancellationToken, params string[] arguments)
492492
{
493493
EnsureDevice(device);
494494

@@ -665,15 +665,15 @@ await reader.ReadLineAsync(
665665
}
666666

667667
/// <inheritdoc/>
668-
public async Task<List<string>> GetFeatureSetAsync(DeviceData device, CancellationToken cancellationToken = default)
668+
public async Task<IEnumerable<string>> GetFeatureSetAsync(DeviceData device, CancellationToken cancellationToken = default)
669669
{
670670
using IAdbSocket socket = adbSocketFactory(EndPoint);
671671
await socket.SendAdbRequestAsync($"host-serial:{device.Serial}:features", cancellationToken);
672672

673673
AdbResponse response = await socket.ReadAdbResponseAsync(cancellationToken);
674674
string features = await socket.ReadStringAsync(cancellationToken);
675675

676-
List<string> featureList = features.Split(new char[] { '\n', ',' }).ToList();
676+
IEnumerable<string> featureList = features.Split(new char[] { '\n', ',' });
677677
return featureList;
678678
}
679679

AdvancedSharpAdbClient/AdbClient.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void KillAdb()
146146
}
147147

148148
/// <inheritdoc/>
149-
public List<DeviceData> GetDevices()
149+
public IEnumerable<DeviceData> GetDevices()
150150
{
151151
using IAdbSocket socket = adbSocketFactory(EndPoint);
152152
socket.SendAdbRequest("host:devices-l");
@@ -155,7 +155,7 @@ public List<DeviceData> GetDevices()
155155

156156
string[] data = reply.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
157157

158-
return data.Select(DeviceData.CreateFromAdbData).ToList();
158+
return data.Select(DeviceData.CreateFromAdbData);
159159
}
160160

161161
/// <inheritdoc/>
@@ -464,7 +464,7 @@ public void Install(DeviceData device, Stream apk, params string[] arguments)
464464
}
465465

466466
/// <inheritdoc/>
467-
public void InstallMultiple(DeviceData device, Stream[] splitAPKs, string packageName, params string[] arguments)
467+
public void InstallMultiple(DeviceData device, IEnumerable<Stream> splitAPKs, string packageName, params string[] arguments)
468468
{
469469
EnsureDevice(device);
470470

@@ -495,7 +495,7 @@ public void InstallMultiple(DeviceData device, Stream[] splitAPKs, string packag
495495
}
496496

497497
/// <inheritdoc/>
498-
public void InstallMultiple(DeviceData device, Stream baseAPK, Stream[] splitAPKs, params string[] arguments)
498+
public void InstallMultiple(DeviceData device, Stream baseAPK, IEnumerable<Stream> splitAPKs, params string[] arguments)
499499
{
500500
EnsureDevice(device);
501501

@@ -635,15 +635,15 @@ public void InstallCommit(DeviceData device, string session)
635635
}
636636

637637
/// <inheritdoc/>
638-
public List<string> GetFeatureSet(DeviceData device)
638+
public IEnumerable<string> GetFeatureSet(DeviceData device)
639639
{
640640
using IAdbSocket socket = adbSocketFactory(EndPoint);
641641
socket.SendAdbRequest($"host-serial:{device.Serial}:features");
642642

643643
AdbResponse response = socket.ReadAdbResponse();
644644
string features = socket.ReadString();
645645

646-
List<string> featureList = features.Split(new char[] { '\n', ',' }).ToList();
646+
IEnumerable<string> featureList = features.Split(new char[] { '\n', ',' });
647647
return featureList;
648648
}
649649

AdvancedSharpAdbClient/AdbCommandLineClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void StartServer()
167167
/// </summary>
168168
/// <param name="output">The output of the <c>adb.exe version</c> command.</param>
169169
/// <returns>A <see cref="Version"/> object that represents the version of the adb command line client.</returns>
170-
internal static Version GetVersionFromOutput(List<string> output)
170+
internal static Version GetVersionFromOutput(IEnumerable<string> output)
171171
{
172172
foreach (string line in output)
173173
{

AdvancedSharpAdbClient/DeviceCommands/PackageManager.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,22 @@ public void InstallRemotePackage(string remoteFilePath, bool reinstall)
195195
/// <param name="basePackageFilePath">The absolute base app file system path to file on local host to install.</param>
196196
/// <param name="splitPackageFilePaths">The absolute split app file system paths to file on local host to install.</param>
197197
/// <param name="reinstall">Set to <see langword="true"/> if re-install of app should be performed.</param>
198-
public void InstallMultiplePackage(string basePackageFilePath, string[] splitPackageFilePaths, bool reinstall)
198+
public void InstallMultiplePackage(string basePackageFilePath, IList<string> splitPackageFilePaths, bool reinstall)
199199
{
200200
ValidateDevice();
201201

202202
string baseRemoteFilePath = SyncPackageToDevice(basePackageFilePath, OnMainSyncProgressChanged);
203203

204204
void OnMainSyncProgressChanged(object sender, SyncProgressChangedEventArgs args) => InstallProgressChanged?.Invoke(this, args.ProgressPercentage * 0.45);
205205

206-
string[] splitRemoteFilePaths = new string[splitPackageFilePaths.Length];
207-
for (int i = 0; i < splitPackageFilePaths.Length; i++)
206+
string[] splitRemoteFilePaths = new string[splitPackageFilePaths.Count];
207+
for (int i = 0; i < splitPackageFilePaths.Count; i++)
208208
{
209-
int percent = 45 + (45 * i / splitPackageFilePaths.Length);
209+
int percent = 45 + (45 * i / splitPackageFilePaths.Count);
210210

211211
splitRemoteFilePaths[i] = SyncPackageToDevice(splitPackageFilePaths[i], OnSplitSyncProgressChanged);
212212

213-
void OnSplitSyncProgressChanged(object sender, SyncProgressChangedEventArgs args) => InstallProgressChanged?.Invoke(this, percent + (args.ProgressPercentage * 0.45 / splitPackageFilePaths.Length));
213+
void OnSplitSyncProgressChanged(object sender, SyncProgressChangedEventArgs args) => InstallProgressChanged?.Invoke(this, percent + (args.ProgressPercentage * 0.45 / splitPackageFilePaths.Count));
214214
}
215215

216216
InstallMultipleRemotePackage(baseRemoteFilePath, splitRemoteFilePaths, reinstall);
@@ -232,18 +232,18 @@ public void InstallMultiplePackage(string basePackageFilePath, string[] splitPac
232232
/// <param name="splitPackageFilePaths">The absolute split app file system paths to file on local host to install.</param>
233233
/// <param name="packageName">The absolute package name of the base app.</param>
234234
/// <param name="reinstall">Set to <see langword="true"/> if re-install of app should be performed.</param>
235-
public void InstallMultiplePackage(string[] splitPackageFilePaths, string packageName, bool reinstall)
235+
public void InstallMultiplePackage(IList<string> splitPackageFilePaths, string packageName, bool reinstall)
236236
{
237237
ValidateDevice();
238238

239-
string[] splitRemoteFilePaths = new string[splitPackageFilePaths.Length];
240-
for (int i = 0; i < splitPackageFilePaths.Length; i++)
239+
string[] splitRemoteFilePaths = new string[splitPackageFilePaths.Count];
240+
for (int i = 0; i < splitPackageFilePaths.Count; i++)
241241
{
242-
int percent = 90 * i / splitPackageFilePaths.Length;
242+
int percent = 90 * i / splitPackageFilePaths.Count;
243243

244244
splitRemoteFilePaths[i] = SyncPackageToDevice(splitPackageFilePaths[i], OnSyncProgressChanged);
245245

246-
void OnSyncProgressChanged(object sender, SyncProgressChangedEventArgs args) => InstallProgressChanged?.Invoke(this, percent + (args.ProgressPercentage * 0.9 / splitPackageFilePaths.Length));
246+
void OnSyncProgressChanged(object sender, SyncProgressChangedEventArgs args) => InstallProgressChanged?.Invoke(this, percent + (args.ProgressPercentage * 0.9 / splitPackageFilePaths.Count));
247247
}
248248

249249
InstallMultipleRemotePackage(splitRemoteFilePaths, packageName, reinstall);
@@ -262,7 +262,7 @@ public void InstallMultiplePackage(string[] splitPackageFilePaths, string packag
262262
/// <param name="baseRemoteFilePath">The absolute base app file path to package file on device.</param>
263263
/// <param name="splitRemoteFilePaths">The absolute split app file paths to package file on device.</param>
264264
/// <param name="reinstall">Set to <see langword="true"/> if re-install of app should be performed.</param>
265-
public void InstallMultipleRemotePackage(string baseRemoteFilePath, string[] splitRemoteFilePaths, bool reinstall)
265+
public void InstallMultipleRemotePackage(string baseRemoteFilePath, IList<string> splitRemoteFilePaths, bool reinstall)
266266
{
267267
ValidateDevice();
268268

@@ -306,7 +306,7 @@ public void InstallMultipleRemotePackage(string baseRemoteFilePath, string[] spl
306306
/// <param name="splitRemoteFilePaths">The absolute split app file paths to package file on device.</param>
307307
/// <param name="packageName">The absolute package name of the base app.</param>
308308
/// <param name="reinstall">Set to <see langword="true"/> if re-install of app should be performed.</param>
309-
public void InstallMultipleRemotePackage(string[] splitRemoteFilePaths, string packageName, bool reinstall)
309+
public void InstallMultipleRemotePackage(IList<string> splitRemoteFilePaths, string packageName, bool reinstall)
310310
{
311311
ValidateDevice();
312312

AdvancedSharpAdbClient/DeviceMonitor.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,13 @@ private void InitializeSocket()
303303
/// </summary>
304304
private void ProcessIncomingDeviceData(string result)
305305
{
306-
List<DeviceData> list = new();
307-
308306
string[] deviceValues = result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
309307

310-
List<DeviceData> currentDevices = deviceValues.Select(DeviceData.CreateFromAdbData).ToList();
308+
IEnumerable<DeviceData> currentDevices = deviceValues.Select(DeviceData.CreateFromAdbData);
311309
UpdateDevices(currentDevices);
312310
}
313311

314-
private void UpdateDevices(List<DeviceData> devices)
312+
private void UpdateDevices(IEnumerable<DeviceData> devices)
315313
{
316314
lock (this.devices)
317315
{

0 commit comments

Comments
 (0)