Skip to content

Commit

Permalink
🩹 [Fix] set new default ADB path + remove date from Logcat (#6)
Browse files Browse the repository at this point in the history
* Create use case and errors for get adb version

* Remove day month and year in logcat view (It's useless)

* Can get default adb path

* Use default adb path

* Generate new version
  • Loading branch information
ThomasBernard03 authored May 16, 2024
1 parent ba72b7c commit 55fd124
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 74 deletions.
36 changes: 32 additions & 4 deletions AndroidTools/AndroidTools.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
817854852BC59F6500B5B2EE /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 817854842BC59F6500B5B2EE /* Preview Assets.xcassets */; };
817EE8592BC81D4100740D4A /* FileExplorerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 817EE8582BC81D4100740D4A /* FileExplorerViewModel.swift */; };
817EE85D2BC8247F00740D4A /* FileItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 817EE85C2BC8247F00740D4A /* FileItem.swift */; };
819A32DF2BF6767A00AD10E3 /* AdbRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 819A32DE2BF6767A00AD10E3 /* AdbRepository.swift */; };
819A32E12BF676E300AD10E3 /* GetAdbVersionError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 819A32E02BF676E300AD10E3 /* GetAdbVersionError.swift */; };
819A32E32BF6774D00AD10E3 /* AdbRepositoryImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 819A32E22BF6774D00AD10E3 /* AdbRepositoryImpl.swift */; };
819A32E72BF67C9200AD10E3 /* DisplayableError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 819A32E62BF67C9200AD10E3 /* DisplayableError.swift */; };
819A32E92BF69B2F00AD10E3 /* ShellHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 819A32E82BF69B2F00AD10E3 /* ShellHelper.swift */; };
819A32EB2BF6A23100AD10E3 /* GetAdbPathUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 819A32EA2BF6A23100AD10E3 /* GetAdbPathUseCase.swift */; };
819A32ED2BF6A5DC00AD10E3 /* SetAdbPathUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 819A32EC2BF6A5DC00AD10E3 /* SetAdbPathUseCase.swift */; };
81A067C72BEEBA0100E83DEF /* GetAdbVersionUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81A067C62BEEBA0100E83DEF /* GetAdbVersionUseCase.swift */; };
81B7900D2BEBA7E5008E7359 /* LogcatViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81B7900C2BEBA7E5008E7359 /* LogcatViewModel.swift */; };
81B7900F2BEBA7F3008E7359 /* LogcatView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81B7900E2BEBA7F3008E7359 /* LogcatView.swift */; };
Expand Down Expand Up @@ -113,6 +120,13 @@
817854862BC59F6500B5B2EE /* AndroidTools.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = AndroidTools.entitlements; sourceTree = "<group>"; };
817EE8582BC81D4100740D4A /* FileExplorerViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileExplorerViewModel.swift; sourceTree = "<group>"; };
817EE85C2BC8247F00740D4A /* FileItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileItem.swift; sourceTree = "<group>"; };
819A32DE2BF6767A00AD10E3 /* AdbRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdbRepository.swift; sourceTree = "<group>"; };
819A32E02BF676E300AD10E3 /* GetAdbVersionError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetAdbVersionError.swift; sourceTree = "<group>"; };
819A32E22BF6774D00AD10E3 /* AdbRepositoryImpl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdbRepositoryImpl.swift; sourceTree = "<group>"; };
819A32E62BF67C9200AD10E3 /* DisplayableError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayableError.swift; sourceTree = "<group>"; };
819A32E82BF69B2F00AD10E3 /* ShellHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShellHelper.swift; sourceTree = "<group>"; };
819A32EA2BF6A23100AD10E3 /* GetAdbPathUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetAdbPathUseCase.swift; sourceTree = "<group>"; };
819A32EC2BF6A5DC00AD10E3 /* SetAdbPathUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetAdbPathUseCase.swift; sourceTree = "<group>"; };
81A067C62BEEBA0100E83DEF /* GetAdbVersionUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetAdbVersionUseCase.swift; sourceTree = "<group>"; };
81B7900C2BEBA7E5008E7359 /* LogcatViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogcatViewModel.swift; sourceTree = "<group>"; };
81B7900E2BEBA7F3008E7359 /* LogcatView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogcatView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -157,6 +171,7 @@
814319532BC6B3AC00044934 /* Commons */ = {
isa = PBXGroup;
children = (
819A32E82BF69B2F00AD10E3 /* ShellHelper.swift */,
81E6A7642BC5D7020034EBFB /* AdbHelper.swift */,
814319542BC6B3B100044934 /* Extensions */,
8109D3632BF128A8001A4B5E /* Constants.swift */,
Expand Down Expand Up @@ -283,6 +298,7 @@
816A1D392BE8CAA300E7DA71 /* ApplicationRepository.swift */,
816A1D482BE8F67000E7DA71 /* DeviceRepository.swift */,
810E8E212BE9312B00ED561A /* FileRepository.swift */,
819A32DE2BF6767A00AD10E3 /* AdbRepository.swift */,
);
path = Repositories;
sourceTree = "<group>";
Expand All @@ -291,6 +307,8 @@
isa = PBXGroup;
children = (
816A1D3F2BE8CDE000E7DA71 /* InstallApplicationError.swift */,
819A32E02BF676E300AD10E3 /* GetAdbVersionError.swift */,
819A32E62BF67C9200AD10E3 /* DisplayableError.swift */,
);
path = Errors;
sourceTree = "<group>";
Expand All @@ -301,6 +319,7 @@
816A1D3B2BE8CAC300E7DA71 /* ApplicationRepositoryImpl.swift */,
816A1D462BE8F66100E7DA71 /* DeviceRepositoryImpl.swift */,
810E8E1F2BE9311100ED561A /* FileRepositoryImpl.swift */,
819A32E22BF6774D00AD10E3 /* AdbRepositoryImpl.swift */,
);
path = Repositories;
sourceTree = "<group>";
Expand Down Expand Up @@ -398,6 +417,8 @@
81F41C5E2BEA95FB005F1730 /* DeleteFileItemUseCase.swift */,
81F41C602BEA995D005F1730 /* ImportFileUseCase.swift */,
81A067C62BEEBA0100E83DEF /* GetAdbVersionUseCase.swift */,
819A32EA2BF6A23100AD10E3 /* GetAdbPathUseCase.swift */,
819A32EC2BF6A5DC00AD10E3 /* SetAdbPathUseCase.swift */,
);
path = UseCases;
sourceTree = "<group>";
Expand Down Expand Up @@ -479,10 +500,13 @@
buildActionMask = 2147483647;
files = (
81B790172BEC17B0008E7359 /* LogEntryItem.swift in Sources */,
819A32DF2BF6767A00AD10E3 /* AdbRepository.swift in Sources */,
81E6A7592BC5CD9D0034EBFB /* DeviceInformationView.swift in Sources */,
819A32ED2BF6A5DC00AD10E3 /* SetAdbPathUseCase.swift in Sources */,
81B7900D2BEBA7E5008E7359 /* LogcatViewModel.swift in Sources */,
814319522BC6715B00044934 /* InstallApplicationViewModel.swift in Sources */,
8109D3662BF141B0001A4B5E /* ApplicationSettingsViewModel.swift in Sources */,
819A32E12BF676E300AD10E3 /* GetAdbVersionError.swift in Sources */,
814319562BC6B3C000044934 /* UTTypeExtensions.swift in Sources */,
81E6A7512BC5CC260034EBFB /* MenuView.swift in Sources */,
816A1D452BE8D3A200E7DA71 /* CircleWavesAnimation.swift in Sources */,
Expand All @@ -501,6 +525,7 @@
81F41C5B2BEA4DF4005F1730 /* TableView.swift in Sources */,
816A1D402BE8CDE000E7DA71 /* InstallApplicationError.swift in Sources */,
8109D3602BF12575001A4B5E /* ApplicationSettingsView.swift in Sources */,
819A32EB2BF6A23100AD10E3 /* GetAdbPathUseCase.swift in Sources */,
81E982392BDD3B480004B154 /* CheckForUpdateView.swift in Sources */,
810E8E202BE9311100ED561A /* FileRepositoryImpl.swift in Sources */,
81B790112BEBA9E8008E7359 /* LogEntryModel.swift in Sources */,
Expand All @@ -509,6 +534,7 @@
81B7900F2BEBA7F3008E7359 /* LogcatView.swift in Sources */,
814319502BC6715100044934 /* InstallApplicationView.swift in Sources */,
81F41C5F2BEA95FB005F1730 /* DeleteFileItemUseCase.swift in Sources */,
819A32E72BF67C9200AD10E3 /* DisplayableError.swift in Sources */,
816A1D472BE8F66100E7DA71 /* DeviceRepositoryImpl.swift in Sources */,
810E8E222BE9312B00ED561A /* FileRepository.swift in Sources */,
812A62C12BC70C3E00388105 /* IntExtensions.swift in Sources */,
Expand All @@ -527,8 +553,10 @@
81E982312BDD0F190004B154 /* RebootDeviceUseCase.swift in Sources */,
817EE8592BC81D4100740D4A /* FileExplorerViewModel.swift in Sources */,
81E982372BDD3A740004B154 /* CheckForUpdateViewModel.swift in Sources */,
819A32E92BF69B2F00AD10E3 /* ShellHelper.swift in Sources */,
816A1D3C2BE8CAC300E7DA71 /* ApplicationRepositoryImpl.swift in Sources */,
81F41C632BEAA7B0005F1730 /* GridView.swift in Sources */,
819A32E32BF6774D00AD10E3 /* AdbRepositoryImpl.swift in Sources */,
812A62BF2BC708D400388105 /* DeviceInformationModel.swift in Sources */,
810E8E1E2BE92EE600ED561A /* FileExplorerResultModel.swift in Sources */,
812AB6EB2BD3AFFE00DB60A5 /* HomeView.swift in Sources */,
Expand Down Expand Up @@ -673,7 +701,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 4;
CURRENT_PROJECT_VERSION = 5;
DEVELOPMENT_ASSET_PATHS = "\"AndroidTools/Preview Content\"";
DEVELOPMENT_TEAM = H9DTPYA8RY;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -687,7 +715,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.3.0;
MARKETING_VERSION = 1.3.1;
PRODUCT_BUNDLE_IDENTIFIER = fr.thomasbernard03.AndroidTools;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -705,7 +733,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 4;
CURRENT_PROJECT_VERSION = 5;
DEVELOPMENT_ASSET_PATHS = "\"AndroidTools/Preview Content\"";
DEVELOPMENT_TEAM = H9DTPYA8RY;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -719,7 +747,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.3.0;
MARKETING_VERSION = 1.3.1;
PRODUCT_BUNDLE_IDENTIFIER = fr.thomasbernard03.AndroidTools;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
41 changes: 5 additions & 36 deletions AndroidTools/AndroidTools/Commons/AdbHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import Foundation

class AdbHelper {

var adbPath: String {
UserDefaults.standard.string(forKey: "adbPath") ?? "/usr/local/bin/adb"
}
private let adbRepository : AdbRepository = AdbRepositoryImpl()


func getDevices() -> [DeviceListModel] {
let command = "devices -l | awk 'NR>1 {print $1}'"
Expand Down Expand Up @@ -49,41 +48,11 @@ class AdbHelper {
}

func runAdbCommand(_ command: String) -> String {
print("Running command:\nadb \(command)")
let task = Process()
let pipe = Pipe()

task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", "\(adbPath) \(command)"]
task.launchPath = "/bin/sh"
task.launch()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
print("Result:\n\(output)")
return output
let result = try? adbRepository.runAdbCommand(command)
return result ?? ""
}

func runAdbCommand(_ command: String, outputHandler: @escaping (String) -> Void) {
print("Running command:\n adb \(command)")
let task = Process()
let pipe = Pipe()

task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", "\(adbPath) \(command)"]
task.launchPath = "/bin/sh"

pipe.fileHandleForReading.readabilityHandler = { fileHandle in
let data = fileHandle.availableData
if let output = String(data: data, encoding: .utf8) {
outputHandler(output)
}
}

task.launch()
task.waitUntilExit()
pipe.fileHandleForReading.readabilityHandler = nil
adbRepository.runAdbCommand(command, outputHandler: outputHandler)
}
}
56 changes: 56 additions & 0 deletions AndroidTools/AndroidTools/Commons/ShellHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// ShellHelper.swift
// AndroidTools
//
// Created by Thomas Bernard on 16/05/2024.
//

import Foundation
import os

class ShellHelper {

private let logger = Logger(
subsystem: Bundle.main.bundleIdentifier!,
category: String(describing: AdbRepositoryImpl.self)
)

func runAdbCommand(_ command: String) -> String {
logger.info("Running command:\n\(command)")
let task = Process()
let pipe = Pipe()

task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", command]
task.launchPath = "/bin/sh"
task.launch()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
logger.info("Result:\n\(output)")
return output
}

func runAdbCommand(_ command: String, outputHandler: @escaping (String) -> Void) {
logger.info("Running command:\n\(command)")
let task = Process()
let pipe = Pipe()

task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", command]
task.launchPath = "/bin/sh"

pipe.fileHandleForReading.readabilityHandler = { fileHandle in
let data = fileHandle.availableData
if let output = String(data: data, encoding: .utf8) {
outputHandler(output)
}
}

task.launch()
task.waitUntilExit()
pipe.fileHandleForReading.readabilityHandler = nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// AdbRepository.swift
// AndroidTools
//
// Created by Thomas Bernard on 16/05/2024.
//

import Foundation
import os

class AdbRepositoryImpl : AdbRepository {

private var shellHelper : ShellHelper = ShellHelper()

private var adbPath: String {
var preferencePath = UserDefaults.standard.string(forKey: "adbPath")

if preferencePath == nil {
if let path = try? getDefaultPath() {
preferencePath = path
} else {
preferencePath = ""
}
}

return preferencePath!
}

private let logger = Logger(
subsystem: Bundle.main.bundleIdentifier!,
category: String(describing: AdbRepositoryImpl.self)
)

func runAdbCommand(_ command: String) throws -> String {
return shellHelper.runAdbCommand("\(adbPath) \(command)")
}

func runAdbCommand(_ command: String, outputHandler: @escaping (String) -> Void) {
return shellHelper.runAdbCommand("\(adbPath) \(command)", outputHandler: outputHandler)
}


func getVersion() throws -> String {
let result = try runAdbCommand("version")

let lines = result.split(separator: "\n")

// Find the line that starts with "Version"
if let versionLine = lines.first(where: { $0.starts(with: "Version") }) {

let version = versionLine
.replacingOccurrences(of: "Version ", with: "")
.split(separator: " ")
.first ?? ""
return String(version)
}

return ""
}

func getPath() throws -> String {
return adbPath
}

private func getDefaultPath() throws -> String {
let result = shellHelper.runAdbCommand("which adb")
if result.contains("not found"){
throw AdbError.notExists
}
else {
return result
}
}

func setPath(path : String?) -> String {

if path == nil {
let defaultPath = try? getDefaultPath()
UserDefaults.standard.set(defaultPath, forKey: "adbPath")
}
else {
UserDefaults.standard.set(path, forKey: "adbPath")
}

return adbPath
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// DisplayableError.swift
// AndroidTools
//
// Created by Thomas Bernard on 16/05/2024.
//

import Foundation

enum DisplayableError : Error {
case error(message : String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// AdbError.swift
// AndroidTools
//
// Created by Thomas Bernard on 16/05/2024.
//

import Foundation

enum AdbError : Error {
case notFound(path : String)
case notExists
}
Loading

0 comments on commit 55fd124

Please sign in to comment.