Cotton code
93 lines · 3.8 KB · SwiftRaw
// What the app that carried a call is called.//// The call log names the app by an id — Cotton's own word for the four it// draws an icon for (`cellular`, `facetime_audio`, `facetime_video`,// `whatsapp`), and the BUNDLE ID for anything else, because iOS logs a CallKit// call with the id of whatever app placed it and there is no closed set of// those. A person reads a name, never an id, so the answer is found in order:////   1. a map of ids this app knows;//   2. the display name of that app, when it is installed on this device and//      the platform will say (a Mac can; a phone cannot look up another app);//   3. a readable form of the id's last part — `com.example.SomeDialler`//      reads "Some Dialler" — so no id is ever shown raw.//// One answer for every screen that shows a call: the Calls tool and a person's// page read the same function.import Foundation#if canImport(AppKit)    import AppKit#endifpublic enum CallApp {    /// Ids this app knows, lowercased, to the name the app goes by.    static let known: [String: String] = [        "cellular": "Cellular",        "facetime_audio": "FaceTime audio",        "facetime_video": "FaceTime video",        "whatsapp": "WhatsApp",        "com.apple.telephony": "Cellular",        "com.apple.facetime": "FaceTime",        "com.skype.skype": "Skype",        "com.skype.skypeforiphone": "Skype",        "com.microsoft.skype.teams": "Microsoft Teams",        "us.zoom.videomeetings": "Zoom",        "com.google.meetings": "Google Meet",        "com.google.gvdialer": "Google Voice",        "ph.telegra.telegraph": "Telegram",        "org.whispersystems.signal": "Signal",        "com.viber": "Viber",        "com.facebook.messenger": "Messenger",        "com.burbn.instagram": "Instagram",        "jp.naver.line": "LINE",        "com.tencent.xin": "WeChat",        "com.hammerandchisel.discord": "Discord",        "com.tinyspeck.chatlyio": "Slack",        "com.cisco.webex.meetings": "Webex",        "net.whatsapp.whatsapp": "WhatsApp",        "net.whatsapp.whatsappsmb": "WhatsApp Business",    ]    /// The name a call app goes by, or nil when the row names none.    ///    /// `installedName` is the platform's lookup, injected so a test answers    /// the same on every machine.    public static func name(        _ provider: String?,        installedName: (String) -> String? = CallApp.installedName    ) -> String? {        let id = provider?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""        // "unknown" is the importer's word for a row that named no app: there        // is nothing to say, so nothing is said.        if id.isEmpty || id.lowercased() == "unknown" { return nil }        if let name = known[id.lowercased()] { return name }        if let name = installedName(id), !name.isEmpty { return name }        return readable(id)    }    /// The display name of the app with this bundle id, when this device has    /// it installed and the platform can say.    public static func installedName(_ bundleId: String) -> String? {        #if canImport(AppKit)            guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleId) else {                return nil            }            let bundle = Bundle(url: url)            let name =                (bundle?.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String)                ?? (bundle?.object(forInfoDictionaryKey: "CFBundleName") as? String)                ?? url.deletingPathExtension().lastPathComponent            return name.isEmpty ? nil : name        #else            return nil        #endif    }    /// The id's last part, as words.    static func readable(_ id: String) -> String? {        let last = id.split(separator: ".").last.map(String.init) ?? id        return PlainWords.sentence(last)    }}