Skip to content
Open
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
1 change: 1 addition & 0 deletions apple/Resources/zh-Hans.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"NSMicrophoneUsageDescription" = "你允许的 Android 应用可以使用本机麦克风。请在本 App 的“设置”›“系统”中开启。";
362 changes: 362 additions & 0 deletions apple/Resources/zh-Hans.lproj/Localizable.strings

Large diffs are not rendered by default.

31 changes: 16 additions & 15 deletions apple/Shared/ADB/ADBConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ enum ADBError: LocalizedError {

var errorDescription: String? {
switch self {
case .connect(let detail): return "Couldn't connect to Android: \(detail)"
case .closed: return "The connection to Android closed."
case .timeout(let what): return "Android didn't answer in time (\(what))."
case .protocolError(let detail): return "Unexpected answer from Android (\(detail))."
case .unauthorized: return "Android hasn't allowed this Mac to connect yet."
case .serviceUnavailable(let service): return "Android refused \(service)."
case .connect(let detail): return L10n.format("Couldn't connect to Android: %@", detail)
case .closed: return L10n.text("The connection to Android closed.")
case .timeout(let what): return L10n.format("Android didn't answer in time (%@).", what)
case .protocolError(let detail): return L10n.format("Unexpected answer from Android (%@).", detail)
case .unauthorized: return L10n.text("Android hasn't allowed this Mac to connect yet.")
case .serviceUnavailable(let service): return L10n.format("Android refused %@.", service)
case .command(let output): return output
}
}
Expand Down Expand Up @@ -99,7 +99,7 @@ final class ADBConnection: @unchecked Sendable {
do {
message = try readMessage()
} catch ADBError.timeout {
throw sentPublicKey ? ADBError.unauthorized : ADBError.timeout("handshake")
throw sentPublicKey ? ADBError.unauthorized : ADBError.timeout(L10n.text("handshake"))
}
switch Command(rawValue: message.command) {
case .cnxn:
Expand All @@ -122,9 +122,10 @@ final class ADBConnection: @unchecked Sendable {
throw ADBError.unauthorized
}
case .stls:
throw ADBError.protocolError("TLS is not supported on this port")
throw ADBError.protocolError(L10n.text("TLS is not supported on this port"))
default:
throw ADBError.protocolError(String(format: "command 0x%08x during handshake", message.command))
throw ADBError.protocolError(L10n.format("command %@ during handshake",
String(format: "0x%08x", message.command)))
}
}
}
Expand Down Expand Up @@ -166,7 +167,7 @@ final class ADBConnection: @unchecked Sendable {
guard stream.waitUntilOpen(timeout: timeout) else {
forget(localID)
if stream.isClosed { throw ADBError.serviceUnavailable(service) }
throw ADBError.timeout("opening \(service)")
throw ADBError.timeout(L10n.format("opening %@", service))
}
return stream
}
Expand Down Expand Up @@ -293,9 +294,9 @@ final class ADBConnection: @unchecked Sendable {
header.withUnsafeBytes { UInt32(littleEndian: $0.loadUnaligned(fromByteOffset: i * 4, as: UInt32.self)) }
}
let command = word(0)
guard word(5) == command ^ 0xffff_ffff else { throw ADBError.protocolError("bad magic") }
guard word(5) == command ^ 0xffff_ffff else { throw ADBError.protocolError(L10n.text("bad magic")) }
let length = Int(word(3))
guard length <= 16 * 1024 * 1024 else { throw ADBError.protocolError("oversized message") }
guard length <= 16 * 1024 * 1024 else { throw ADBError.protocolError(L10n.text("oversized message")) }
let payload = length > 0 ? try readExactly(length) : Data()
return Message(command: command, arg0: word(1), arg1: word(2), payload: payload)
}
Expand All @@ -316,7 +317,7 @@ final class ADBConnection: @unchecked Sendable {
} else if errno == EINTR {
continue
} else if errno == EAGAIN || errno == EWOULDBLOCK {
throw ADBError.timeout("read")
throw ADBError.timeout(L10n.text("read"))
} else {
throw ADBError.closed
}
Expand Down Expand Up @@ -344,7 +345,7 @@ final class ADBConnection: @unchecked Sendable {
address.sin_port = port.bigEndian
guard inet_pton(AF_INET, host, &address.sin_addr) == 1 else {
Darwin.close(socket)
throw ADBError.connect("bad address \(host)")
throw ADBError.connect(L10n.format("bad address %@", host))
}

// Non-blocking connect so a dead port fails fast.
Expand All @@ -368,7 +369,7 @@ final class ADBConnection: @unchecked Sendable {
getsockopt(socket, SOL_SOCKET, SO_ERROR, &error, &length)
if ready <= 0 || error != 0 {
Darwin.close(socket)
throw ADBError.connect(ready <= 0 ? "timed out" : String(cString: strerror(error)))
throw ADBError.connect(ready <= 0 ? L10n.text("timed out") : String(cString: strerror(error)))
}
}
_ = fcntl(socket, F_SETFL, flags & ~O_NONBLOCK)
Expand Down
12 changes: 6 additions & 6 deletions apple/Shared/ADB/ADBKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ final class ADBKey: @unchecked Sendable {
case publicKey
var errorDescription: String? {
switch self {
case .generate(let detail): return "Couldn't create the ADB key: \(detail)"
case .sign(let detail): return "Couldn't sign the ADB token: \(detail)"
case .publicKey: return "Couldn't read the ADB public key."
case .generate(let detail): return L10n.format("Couldn't create the ADB key: %@", detail)
case .sign(let detail): return L10n.format("Couldn't sign the ADB token: %@", detail)
case .publicKey: return L10n.text("Couldn't read the ADB public key.")
}
}
}
Expand All @@ -52,10 +52,10 @@ final class ADBKey: @unchecked Sendable {
]
var error: Unmanaged<CFError>?
guard let key = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
throw Failure.generate(error?.takeRetainedValue().localizedDescription ?? "unknown error")
throw Failure.generate(error?.takeRetainedValue().localizedDescription ?? L10n.text("unknown error"))
}
guard let exported = SecKeyCopyExternalRepresentation(key, &error) as Data? else {
throw Failure.generate(error?.takeRetainedValue().localizedDescription ?? "export failed")
throw Failure.generate(error?.takeRetainedValue().localizedDescription ?? L10n.text("export failed"))
}
FileManager.default.createFile(atPath: file.path, contents: exported, attributes: [.posixPermissions: 0o600])
privateKey = key
Expand All @@ -72,7 +72,7 @@ final class ADBKey: @unchecked Sendable {
func sign(token: Data) throws -> Data {
var error: Unmanaged<CFError>?
guard let signature = SecKeyCreateSignature(privateKey, .rsaSignatureDigestPKCS1v15SHA1, token as CFData, &error) as Data? else {
throw Failure.sign(error?.takeRetainedValue().localizedDescription ?? "unknown error")
throw Failure.sign(error?.takeRetainedValue().localizedDescription ?? L10n.text("unknown error"))
}
return signature
}
Expand Down
6 changes: 3 additions & 3 deletions apple/Shared/ADB/ADBSync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension ADBConnection {
try stream.write(Self.syncRequest("STAT", Data(path.utf8)))
let reply = try stream.readExactly(16, timeout: 15)
guard String(decoding: reply.prefix(4), as: UTF8.self) == "STAT" else {
throw ADBError.protocolError("STAT reply")
throw ADBError.protocolError(L10n.text("STAT reply"))
}
try? stream.write(Self.syncRequest("QUIT", Data()))
return RemoteFile(mode: reply.le32(4), size: reply.le32(8), modified: reply.le32(12))
Expand Down Expand Up @@ -72,10 +72,10 @@ extension ADBConnection {
let id = String(decoding: reply.prefix(4), as: UTF8.self)
if id == "FAIL" {
let length = Int(reply.le32(4))
let message = (try? stream.readExactly(length, timeout: 5)).map { String(decoding: $0, as: UTF8.self) } ?? "push failed"
let message = (try? stream.readExactly(length, timeout: 5)).map { String(decoding: $0, as: UTF8.self) } ?? L10n.text("push failed")
throw ADBError.command(message)
}
guard id == "OKAY" else { throw ADBError.protocolError("SEND reply \(id)") }
guard id == "OKAY" else { throw ADBError.protocolError(L10n.format("SEND reply %@", id)) }
try? stream.write(Self.syncHeader("QUIT", 0))
}

Expand Down
16 changes: 16 additions & 0 deletions apple/Shared/Localization.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Mac Android Subsystem - localized strings for text held in String values.
// SwiftUI string literals localize themselves; model, status, notice and AppKit
// strings go through here so the English text is the lookup key.
// Licensed under the GNU General Public License v3 or later.

import Foundation

enum L10n {
static func text(_ key: String) -> String {
NSLocalizedString(key, comment: "")
}

static func format(_ key: String, _ arguments: String...) -> String {
String(format: NSLocalizedString(key, comment: ""), arguments: arguments.map { $0 as CVarArg })
}
}
24 changes: 12 additions & 12 deletions apple/Shared/PerformanceProfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ enum PerformanceMode: String, Codable, CaseIterable, Identifiable, Sendable {

var title: String {
switch self {
case .powerSaver: return "Power Saver"
case .smart: return "Smart"
case .standard: return "Standard"
case .performance: return "Performance"
case .powerSaver: return L10n.text("Power Saver")
case .smart: return L10n.text("Smart")
case .standard: return L10n.text("Standard")
case .performance: return L10n.text("Performance")
}
}

Expand All @@ -65,13 +65,13 @@ enum PerformanceMode: String, Codable, CaseIterable, Identifiable, Sendable {
var summary: String {
switch self {
case .powerSaver:
return "Android only runs while an Android app is open, and adapts while it does: frugal by default, a little faster for the app in front when your Mac has power to spare."
return L10n.text("Android only runs while an Android app is open, and adapts while it does: frugal by default, a little faster for the app in front when your Mac has power to spare.")
case .smart:
return "Stays ready so apps open instantly, and adapts all the time: full speed for the app you're using, and power saving as soon as other apps need your Mac or the battery runs low."
return L10n.text("Stays ready so apps open instantly, and adapts all the time: full speed for the app you're using, and power saving as soon as other apps need your Mac or the battery runs low.")
case .standard:
return "Android runs as it is, with nothing tuned: every processor core, all of its memory and its own power settings. Stays ready in the background."
return L10n.text("Android runs as it is, with nothing tuned: every processor core, all of its memory and its own power settings. Stays ready in the background.")
case .performance:
return "Maximum performance, all the time: every core and all of Android's memory, its game optimizations, up to 120 fps and lower input latency."
return L10n.text("Maximum performance, all the time: every core and all of Android's memory, its game optimizations, up to 120 fps and lower input latency.")
}
}
}
Expand All @@ -85,10 +85,10 @@ enum PerformanceLevel: String, Codable, Sendable {

var title: String {
switch self {
case .saver: return "Saving power"
case .balanced: return "Balanced"
case .standard: return "Standard"
case .performance: return "Performance"
case .saver: return L10n.text("Saving power")
case .balanced: return L10n.text("Balanced")
case .standard: return L10n.text("Standard")
case .performance: return L10n.text("Performance")
}
}

Expand Down
33 changes: 18 additions & 15 deletions apple/Shared/ZipExtractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ enum ZipExtractor {

var errorDescription: String? {
switch self {
case .notAZip: return "This file isn't a zip archive."
case .unsupportedMethod(let name, let method): return "\(name) uses an unsupported compression method (\(method)). Re-zip it with standard compression."
case .corrupt(let detail): return "The zip archive is damaged (\(detail))."
case .unsafePath(let name): return "The archive contains an unsafe path: \(name)"
case .notAZip: return L10n.text("This file isn't a zip archive.")
case .unsupportedMethod(let name, let method):
return L10n.format("%@ uses an unsupported compression method (%@). Re-zip it with standard compression.",
name, String(method))
case .corrupt(let detail): return L10n.format("The zip archive is damaged (%@).", detail)
case .unsafePath(let name): return L10n.format("The archive contains an unsafe path: %@", name)
}
}
}
Expand Down Expand Up @@ -78,11 +80,11 @@ enum ZipExtractor {

// ZIP64: a locator sits 20 bytes before the classic record.
if count == 0xFFFF || directorySize == 0xFFFF_FFFF || directoryOffset == 0xFFFF_FFFF {
guard eocd >= 20, tail.zipLE32(eocd - 20) == 0x0706_4b50 else { throw Failure.corrupt("missing ZIP64 locator") }
guard eocd >= 20, tail.zipLE32(eocd - 20) == 0x0706_4b50 else { throw Failure.corrupt(L10n.text("missing ZIP64 locator")) }
let zip64Offset = tail.zipLE64(eocd - 20 + 8)
try handle.seek(toOffset: zip64Offset)
guard let record = try handle.read(upToCount: 56), record.count == 56, record.zipLE32(0) == 0x0606_4b50 else {
throw Failure.corrupt("bad ZIP64 record")
throw Failure.corrupt(L10n.text("bad ZIP64 record"))
}
count = record.zipLE64(32)
directorySize = record.zipLE64(40)
Expand All @@ -91,13 +93,13 @@ enum ZipExtractor {

try handle.seek(toOffset: directoryOffset)
guard let directory = try handle.read(upToCount: Int(directorySize)), directory.count == Int(directorySize) else {
throw Failure.corrupt("truncated central directory")
throw Failure.corrupt(L10n.text("truncated central directory"))
}

var entries: [Entry] = []
var p = 0
for _ in 0..<count {
guard p + 46 <= directory.count, directory.zipLE32(p) == 0x0201_4b50 else { throw Failure.corrupt("bad directory entry") }
guard p + 46 <= directory.count, directory.zipLE32(p) == 0x0201_4b50 else { throw Failure.corrupt(L10n.text("bad directory entry")) }
let flags = directory.zipLE16(p + 8)
let method = directory.zipLE16(p + 10)
var compressed = UInt64(directory.zipLE32(p + 20))
Expand All @@ -107,7 +109,7 @@ enum ZipExtractor {
let commentLength = Int(directory.zipLE16(p + 32))
var offset = UInt64(directory.zipLE32(p + 42))
let nameStart = p + 46
guard nameStart + nameLength + extraLength <= directory.count else { throw Failure.corrupt("bad name") }
guard nameStart + nameLength + extraLength <= directory.count else { throw Failure.corrupt(L10n.text("bad name")) }
let nameData = directory.subdata(in: nameStart..<(nameStart + nameLength))
// Bit 11 = UTF-8 names; older tools use CP437, which is ASCII for typical file names.
let name = String(data: nameData, encoding: (flags & 0x800) != 0 ? .utf8 : .isoLatin1) ?? ""
Expand Down Expand Up @@ -139,7 +141,7 @@ enum ZipExtractor {
private static func extract(_ entry: Entry, from handle: FileHandle, to target: URL) throws {
try handle.seek(toOffset: entry.localHeaderOffset)
guard let header = try handle.read(upToCount: 30), header.count == 30, header.zipLE32(0) == 0x0403_4b50 else {
throw Failure.corrupt("bad local header for \(entry.name)")
throw Failure.corrupt(L10n.format("bad local header for %@", entry.name))
}
let dataStart = entry.localHeaderOffset + 30 + UInt64(header.zipLE16(26)) + UInt64(header.zipLE16(28))
try handle.seek(toOffset: dataStart)
Expand All @@ -154,7 +156,7 @@ enum ZipExtractor {
while remaining > 0 {
try autoreleasepool {
let data = try handle.read(upToCount: Int(min(UInt64(chunk), remaining))) ?? Data()
guard !data.isEmpty else { throw Failure.corrupt("unexpected end of \(entry.name)") }
guard !data.isEmpty else { throw Failure.corrupt(L10n.format("unexpected end of %@", entry.name)) }
try out.write(contentsOf: data)
remaining -= UInt64(data.count)
}
Expand All @@ -167,15 +169,16 @@ enum ZipExtractor {

let size = try out.offset()
if size != entry.uncompressedSize {
throw Failure.corrupt("\(entry.name) is \(size) bytes, expected \(entry.uncompressedSize)")
throw Failure.corrupt(L10n.format("%@ is %@ bytes, expected %@",
entry.name, String(size), String(entry.uncompressedSize)))
}
}

private static func inflate(_ entry: Entry, from handle: FileHandle, to out: FileHandle) throws {
let stream = UnsafeMutablePointer<compression_stream>.allocate(capacity: 1)
defer { stream.deallocate() }
guard compression_stream_init(stream, COMPRESSION_STREAM_DECODE, COMPRESSION_ZLIB) == COMPRESSION_STATUS_OK else {
throw Failure.corrupt("could not start decompression")
throw Failure.corrupt(L10n.text("could not start decompression"))
}
defer { compression_stream_destroy(stream) }

Expand Down Expand Up @@ -218,10 +221,10 @@ enum ZipExtractor {
case COMPRESSION_STATUS_OK:
// Out of input with nothing left to emit means the stream is truncated.
if remaining == 0, stream.pointee.src_size == 0, idleRounds > 2 {
throw Failure.corrupt("unexpected end of \(entry.name)")
throw Failure.corrupt(L10n.format("unexpected end of %@", entry.name))
}
default:
throw Failure.corrupt("could not inflate \(entry.name)")
throw Failure.corrupt(L10n.format("could not inflate %@", entry.name))
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions apple/Subsystem/Model/AndroidImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ struct AndroidImage: Codable, Equatable, Sendable {

var errorDescription: String? {
switch self {
case .noPackage: return "This file doesn't contain a LineageOS virtual machine (a .utm folder with config.plist)."
case .noDisks: return "The virtual machine has no disks."
case .overlay(let output): return "Couldn't prepare Android's disks: \(output)"
case .noRelease: return "No Android image is available to download yet. Pick a UTM-VM-lineage zip instead (see the guide)."
case .checksum: return "The download is damaged (its checksum doesn't match). Try again."
case .download(let detail): return "The download failed: \(detail)"
case .noPackage: return L10n.text("This file doesn't contain a LineageOS virtual machine (a .utm folder with config.plist).")
case .noDisks: return L10n.text("The virtual machine has no disks.")
case .overlay(let output): return L10n.format("Couldn't prepare Android's disks: %@", output)
case .noRelease: return L10n.text("No Android image is available to download yet. Pick a UTM-VM-lineage zip instead (see the guide).")
case .checksum: return L10n.text("The download is damaged (its checksum doesn't match). Try again.")
case .download(let detail): return L10n.format("The download failed: %@", detail)
}
}
}
Expand Down
Loading