Browse Source

'Add To Home Screen' hack stops working; remove ShortcutManager

B. Petersen 1 năm trước cách đây
mục cha
commit
509b9f6d6f

+ 0 - 4
deltachat-ios.xcodeproj/project.pbxproj

@@ -118,7 +118,6 @@
 		30E348E524F6647D005C93D1 /* FileTextCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30E348E424F6647D005C93D1 /* FileTextCell.swift */; };
 		30E6E35A293FEEA70093871E /* SimpleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30E6E359293FEEA70093871E /* SimpleLogger.swift */; };
 		30E6E35B293FF9F30093871E /* SimpleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30E6E359293FEEA70093871E /* SimpleLogger.swift */; };
-		30E83EFD289BF32C0035614C /* ShortcutManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30E83EFC289BF32C0035614C /* ShortcutManager.swift */; };
 		30E8F2132447285600CE2C90 /* ShareViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30E8F2122447285600CE2C90 /* ShareViewController.swift */; };
 		30E8F2162447285600CE2C90 /* MainInterface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 30E8F2142447285600CE2C90 /* MainInterface.storyboard */; };
 		30E8F21A2447285600CE2C90 /* Delta Chat.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 30E8F2102447285600CE2C90 /* Delta Chat.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
@@ -394,7 +393,6 @@
 		30E348E024F53772005C93D1 /* ImageTextCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageTextCell.swift; sourceTree = "<group>"; };
 		30E348E424F6647D005C93D1 /* FileTextCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileTextCell.swift; sourceTree = "<group>"; };
 		30E6E359293FEEA70093871E /* SimpleLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleLogger.swift; sourceTree = "<group>"; };
-		30E83EFC289BF32C0035614C /* ShortcutManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutManager.swift; sourceTree = "<group>"; };
 		30E8F2102447285600CE2C90 /* Delta Chat.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = "Delta Chat.appex"; sourceTree = BUILT_PRODUCTS_DIR; };
 		30E8F2122447285600CE2C90 /* ShareViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareViewController.swift; sourceTree = "<group>"; };
 		30E8F2152447285600CE2C90 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/MainInterface.storyboard; sourceTree = "<group>"; };
@@ -969,7 +967,6 @@
 				21D6C9392606190600D0755A /* NotificationManager.swift */,
 				302D544F268B6B2300A8B271 /* MessageUtils.swift */,
 				3011E8042787365D00214221 /* KeychainManager.swift */,
-				30E83EFC289BF32C0035614C /* ShortcutManager.swift */,
 				30CE137728D9C40700158DF4 /* ChatDropInteraction.swift */,
 				30E6E359293FEEA70093871E /* SimpleLogger.swift */,
 			);
@@ -1467,7 +1464,6 @@
 				30E6E35A293FEEA70093871E /* SimpleLogger.swift in Sources */,
 				7A9FB1441FB061E2001FEA36 /* AppDelegate.swift in Sources */,
 				30C7D5EE28F47E620078D24C /* MessageCounter.swift in Sources */,
-				30E83EFD289BF32C0035614C /* ShortcutManager.swift in Sources */,
 				305501742798CDE1008FD5CA /* WebxdcViewController.swift in Sources */,
 				3034929F25752FC800A523D0 /* MediaPreview.swift in Sources */,
 				AE76E5EE242BF2EA003CF461 /* WelcomeViewController.swift in Sources */,

+ 1 - 0
deltachat-ios/AppDelegate.swift

@@ -200,6 +200,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         case "mailto":
             return appCoordinator.handleMailtoURL(url)
         case "chat.delta.deeplink":
+            // support existing 'Add To Home Screen' shortcuts (we do not support adding new ones, see #1880)
             return appCoordinator.handleDeepLinkURL(url)
         default:
             return false

+ 0 - 10
deltachat-ios/Controller/WebxdcViewController.swift

@@ -19,8 +19,6 @@ class WebxdcViewController: WebViewViewController {
     var sourceCodeUrl: String?
     private var allowInternet: Bool = false
 
-    private var shortcutManager: ShortcutManager?
-
     private lazy var moreButton: UIBarButtonItem = {
         let image: UIImage?
         if #available(iOS 13.0, *) {
@@ -239,7 +237,6 @@ class WebxdcViewController: WebViewViewController {
     
     init(dcContext: DcContext, messageId: Int) {
         self.messageId = messageId
-        self.shortcutManager = ShortcutManager(dcContext: dcContext, messageId: messageId)
         super.init(dcContext: dcContext)
     }
     
@@ -323,7 +320,6 @@ class WebxdcViewController: WebViewViewController {
         if let msgChangedObserver = msgChangedObserver {
             nc.removeObserver(msgChangedObserver)
         }
-        shortcutManager = nil
     }
 
     override func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
@@ -392,8 +388,6 @@ class WebxdcViewController: WebViewViewController {
         let alert = UIAlertController(title: webxdcName + " – " + String.localized("webxdc_app"),
                                       message: nil,
                                       preferredStyle: .safeActionSheet)
-        let addToHomescreenAction = UIAlertAction(title: String.localized("add_to_home_screen"), style: .default, handler: addToHomeScreen(_:))
-        alert.addAction(addToHomescreenAction)
         if sourceCodeUrl != nil {
             let sourceCodeAction = UIAlertAction(title: String.localized("source_code"), style: .default, handler: openUrl(_:))
             alert.addAction(sourceCodeAction)
@@ -403,10 +397,6 @@ class WebxdcViewController: WebViewViewController {
         self.present(alert, animated: true, completion: nil)
     }
 
-    private func addToHomeScreen(_ action: UIAlertAction) {
-        shortcutManager?.showShortcutLandingPage()
-    }
-
     private func openUrl(_ action: UIAlertAction) {
         if let sourceCodeUrl = sourceCodeUrl,
            let url = URL(string: sourceCodeUrl) {

+ 0 - 21
deltachat-ios/Extensions/UIImage+Extension.swift

@@ -43,27 +43,6 @@ extension UIImage {
         guard let cgImage = image?.cgImage else { return nil }
         self.init(cgImage: cgImage)
     }
-
-    public func generateSplash(backgroundColor: UIColor, isPortrait: Bool) -> UIImage? {
-        let rect: CGRect
-        if isPortrait {
-            rect = CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.width,
-                                                          height: UIScreen.main.bounds.height))
-        } else {
-            rect = CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.height,
-                                                          height: UIScreen.main.bounds.width))
-        }
-
-        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
-        backgroundColor.setFill()
-        UIRectFill(rect)
-        let horizontalPadding = (rect.width - self.size.width) / 2
-        let verticalPadding = (rect.height - self.size.height) / 2 + UIApplication.shared.statusBarFrame.height
-        self.draw(in: CGRect(horizontalPadding, verticalPadding, self.size.width, self.size.height))
-        let finalImage = UIGraphicsGetImageFromCurrentImageContext()
-        UIGraphicsEndImageContext()
-        return finalImage
-    }
 }
 
 public enum ImageType: String {

+ 0 - 257
deltachat-ios/Helper/ShortcutManager.swift

@@ -1,257 +0,0 @@
-import Foundation
-import UIKit
-import SafariServices
-import Swifter
-import DcCore
-import SDWebImage
-
-public class ShortcutManager {
-
-    lazy var server: HttpServer = {
-        let server = HttpServer()
-        return server
-    }()
-
-    private let defaultPort: UInt16 = 8200
-    private var localPort: UInt16 = 8200
-    private let dcContext: DcContext
-    private let messageId: Int
-
-    public init(dcContext: DcContext, messageId: Int) {
-        self.dcContext = dcContext
-        self.messageId = messageId
-    }
-
-    deinit {
-        server.stop()
-    }
-
-    public lazy var shareIconBase64: String = {
-        if #available(iOS 13.0, *) {
-            return UIImage(systemName: "square.and.arrow.up")?.withTintColor(DcColors.primary).pngData()?.base64EncodedString() ?? ""
-        } else {
-            return ""
-        }
-    }()
-
-    public lazy var addToHomeIconBase64: String = {
-        if #available(iOS 13.0, *) {
-            return UIImage(systemName: "plus.square")?.withTintColor(DcColors.defaultInverseColor).pngData()?.base64EncodedString() ?? ""
-        } else {
-            return ""
-        }
-    }()
-
-    private lazy var scaledDownLogo: UIImage? = {
-        let msg = dcContext.getMessage(id: messageId)
-        return msg.getWebxdcPreviewImage()?
-            .scaleDownImage(toMax: 160)?
-            .sd_roundedCornerImage(withRadius: 12,
-                                   corners: SDRectCorner.allCorners,
-                                   borderWidth: 0,
-                                   borderColor: nil)
-    }()
-
-    public lazy var landscapeSplashBase64: String = {
-        let image = scaledDownLogo?
-            .generateSplash(backgroundColor: DcColors.chatBackgroundColor, isPortrait: false)?
-            .pngData()?
-            .base64EncodedString() ?? ""
-        return image
-    }()
-
-    public lazy var portraitSplashBase64: String = {
-        let image = scaledDownLogo?
-            .generateSplash(backgroundColor: DcColors.chatBackgroundColor, isPortrait: true)?
-            .pngData()?
-            .base64EncodedString() ?? ""
-        return image
-    }()
-
-    func showShortcutLandingPage() {
-        let message = dcContext.getMessage(id: messageId)
-        if message.type != DC_MSG_WEBXDC {
-            return
-        }
-
-        let deepLink = "chat.delta.deeplink://webxdc?accountId=\(dcContext.id)&chatId=\(message.chatId)&msgId=\(message.id)"
-        guard let deepLinkUrl = URL(string: deepLink) else {
-            return
-        }
-
-        let infoDict = message.getWebxdcInfoDict()
-        let iconData = scaledDownLogo?.pngData() ?? UIImage(named: "appicon")?.pngData() ?? nil
-        let document = infoDict["document"] as? String ?? ""
-        let webxdcName = infoDict["name"] as? String ?? "ErrName" // name should not be empty
-        let iconTitle = document.isEmpty ? webxdcName : document
-
-        let iconBase64 = iconData?.base64EncodedString() ?? ""
-        let html = htmlFor(title: iconTitle,
-                           urlToRedirect: deepLinkUrl,
-                           iconBase64: iconBase64)
-        guard let base64 = html.data(using: .utf8)?.base64EncodedString() else {
-            return
-        }
-        server["/s"] = { _ in
-            var headers = ["Location": "data:text/html;base64,\(base64)"]
-            headers["Cache-Control"] = "no-store"
-            return .raw(301, "Moved Permanently", headers, nil)
-        }
-
-        var attempts = 0
-        var tryToReconnect = true
-        while tryToReconnect && attempts < 5 {
-            do {
-                localPort = defaultPort + UInt16(Int.random(in: 1...100))
-                try server.start(localPort)
-                tryToReconnect = false
-            } catch SocketError.bindFailed(let errorString) {
-                attempts += 1
-                tryToReconnect = true
-                logger.error(errorString)
-            } catch {
-                tryToReconnect = false
-                logger.error(error)
-            }
-        }
-
-        guard let shortcutUrl = URL(string: "http://localhost:\(localPort)/s") else {
-            return
-        }
-
-        UIApplication.shared.open(shortcutUrl)
-    }
-
-    private func htmlFor(title: String, urlToRedirect: URL, iconBase64: String) -> String {
-        return """
-        <html>
-        <head>
-        <title>\(title)</title>
-        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
-        <meta name="apple-mobile-web-app-capable" content="yes">
-        <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
-        <meta name="apple-mobile-web-app-title" content="\(title)">
-        <link rel="apple-touch-startup-image" media="(orientation: landscape)" href="data:image/jpeg;base64,\(landscapeSplashBase64)"/>
-        <link rel="apple-touch-startup-image" media="(orientation: portrait)" href="data:image/jpeg;base64,\(portraitSplashBase64)"/>
-        <link rel="apple-touch-icon-precomposed" href="data:image/jpeg;base64,\(iconBase64)"/>
-        </head>
-        <body>
-        <a id="redirect" href="\(urlToRedirect.absoluteString)"></a>
-        </body>
-        </html>
-        <style>
-            h1, h2 {
-                text-align: center;
-            }
-            .appContainer {
-              padding-top: 0px;
-              text-align: center;
-              display: block;
-              justify-content: center;
-              align-items: center;
-            }
-            .screenshotWrapper {
-              padding-top: 0px;
-              display: flex;
-              justify-content: center;
-              align-items: center;
-              width: 100vw;
-              height: 100vh;
-              position: relative;
-            }
-            .screenshotImage {
-              width: 160px;
-              height: 160px;
-            }
-            div {
-                padding: 1rem 1rem 1rem 1rem;
-            }
-            body {
-                font-size: 16pt;
-                font-family: -apple-system, sans-serif;
-                -webkit-text-size-adjust: none;
-            }
-            .previewImage {
-                height: 90px;
-                width: 90px;
-                padding: .5rem .5rem .5rem .5rem;
-            }
-            .webxdcName {
-                text-align: center;
-                font-size: 19pt;
-                font-family: -apple-system, sans-serif;
-                -webkit-text-size-adjust: none;
-            }
-            .iconImage {
-                height: 20px;
-                width: 20px;
-                padding: 0 .5rem 0 .5rem;
-            }
-
-            @media (prefers-color-scheme: dark) {
-              body {
-                background-color: black !important;
-                color: #eee;
-              }
-            }
-         </style>
-         <script type="text/javascript">
-            if (window.navigator.standalone) {
-                var appContainer = document.createElement('div');
-                appContainer.classList.add("screenshotWrapper");
-                var img = document.createElement('img');
-                img.src = "data:image/png;base64,\(iconBase64)";
-                img.classList.add("screenshotImage");
-                appContainer.appendChild(img);
-                document.body.appendChild(appContainer);
-
-                var element = document.getElementById('redirect');
-                var event = document.createEvent('MouseEvents');
-                event.initEvent('click', true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
-                setTimeout(function() { element.dispatchEvent(event); }, 25);
-            } else {
-                 var div = document.createElement('div');
-                 var header = document.createElement('h2');
-                 header.append('\(String.localized("add_to_home_screen"))');
-                 div.appendChild(header);
-                 document.body.appendChild(div);
-
-                 var appContainer = document.createElement('div');
-                 appContainer.classList.add("appContainer");
-                 var img = document.createElement('img');
-                 img.src = "data:image/png;base64,\(iconBase64)";
-                 img.classList.add("previewImage");
-                 appContainer.appendChild(img);
-                 var header = document.createElement('h1');
-                 header.append('\(title)');
-                 header.classList.add("webxdcName");
-                 appContainer.appendChild(header);
-                 document.body.appendChild(appContainer);
-
-                 var div = document.createElement('div');
-                 div.appendChild(document.createTextNode('1. '));
-                 var img = document.createElement('img');
-                 img.src = "data:image/png;base64,\(shareIconBase64)";
-                 img.classList.add("iconImage");
-                 div.appendChild(img);
-                 var node = document.createTextNode("\(String.localized("shortcut_step1_tap_share_btn").replacingOccurrences(of: "\"", with: " - "))");
-                 div.appendChild(node);
-                 document.body.appendChild(div);
-
-                 var div = document.createElement('div');
-                 div.appendChild(document.createTextNode('2. '));
-                 var img = document.createElement('img');
-                 img.src = "data:image/png;base64,\(addToHomeIconBase64)";
-                 img.classList.add("iconImage");
-                 div.appendChild(img);
-                 var node = document.createTextNode("\(String.localized("shortcut_step2_tap_add_to_home_screen").replacingOccurrences(of: "\"", with: " - "))");
-                 div.appendChild(node);
-
-                 document.body.appendChild(div);
-            }
-        </script>
-        """
-
-    }
-}