session-ios/SessionShareExtension/ShareAppExtensionContext.swift
Morgan Pretty e768bebe6d Merge remote-tracking branch 'upstream/dev' into feature/job-runner-unit-tests
# Conflicts:
#	Session.xcodeproj/project.pbxproj
#	Session/Meta/Session-Prefix.pch
#	Session/Notifications/SyncPushTokensJob.swift
#	Session/Utilities/BackgroundPoller.swift
#	SessionMessagingKit/Configuration.swift
#	SessionMessagingKit/Database/Models/Profile.swift
#	SessionMessagingKit/Jobs/Types/AttachmentDownloadJob.swift
#	SessionMessagingKit/Jobs/Types/AttachmentUploadJob.swift
#	SessionMessagingKit/Jobs/Types/DisappearingMessagesJob.swift
#	SessionMessagingKit/Jobs/Types/FailedAttachmentDownloadsJob.swift
#	SessionMessagingKit/Jobs/Types/FailedMessageSendsJob.swift
#	SessionMessagingKit/Jobs/Types/GroupLeavingJob.swift
#	SessionMessagingKit/Jobs/Types/MessageReceiveJob.swift
#	SessionMessagingKit/Jobs/Types/MessageSendJob.swift
#	SessionMessagingKit/Jobs/Types/NotifyPushServerJob.swift
#	SessionMessagingKit/Jobs/Types/RetrieveDefaultOpenGroupRoomsJob.swift
#	SessionMessagingKit/Jobs/Types/SendReadReceiptsJob.swift
#	SessionMessagingKit/Jobs/Types/UpdateProfilePictureJob.swift
#	SessionMessagingKit/Sending & Receiving/MessageSender.swift
#	SessionMessagingKit/Sending & Receiving/Pollers/ClosedGroupPoller.swift
#	SessionMessagingKit/Utilities/AppReadiness.m
#	SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift
#	SessionMessagingKitTests/_TestUtilities/TestOnionRequestAPI.swift
#	SessionShareExtension/ShareNavController.swift
#	SessionSnodeKit/Jobs/GetSnodePoolJob.swift
#	SessionUtilitiesKit/Configuration.swift
#	SessionUtilitiesKit/Database/Utilities/Database+Utilities.swift
#	SessionUtilitiesKit/JobRunner/JobRunner.swift
#	SignalUtilitiesKit/Meta/SignalUtilitiesKit.h
#	SignalUtilitiesKit/Utilities/SSKAsserts.h
2023-07-18 10:02:51 +10:00

202 lines
6.2 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import UIKit
import SignalUtilitiesKit
import SessionUtilitiesKit
import SessionMessagingKit
import SignalCoreKit
/// This is _NOT_ a singleton and will be instantiated each time that the SAE is used.
final class ShareAppExtensionContext: NSObject, AppContext {
var rootViewController: UIViewController
var reportedApplicationState: UIApplication.State
let appLaunchTime = Date()
let isMainApp = false
let isMainAppAndActive = false
var isShareExtension: Bool = true
var mainWindow: UIWindow?
var wasWokenUpByPushNotification: Bool = false
private static var _isRTL: Bool = {
// Borrowed from PureLayout's AppExtension compatible RTL support.
// App Extensions may not access -[UIApplication sharedApplication]; fall back
// to checking the bundle's preferred localization character direction
return (
Locale.characterDirection(
forLanguage: (Bundle.main.preferredLocalizations.first ?? "")
) == Locale.LanguageDirection.rightToLeft
)
}()
var isRTL: Bool { return ShareAppExtensionContext._isRTL }
var statusBarHeight: CGFloat { return 20 }
var openSystemSettingsAction: UIAlertAction?
// MARK: - Initialization
init(rootViewController: UIViewController) {
self.rootViewController = rootViewController
self.reportedApplicationState = .active
super.init()
NotificationCenter.default.addObserver(
self,
selector: #selector(extensionHostDidBecomeActive(notification:)),
name: .NSExtensionHostDidBecomeActive,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(extensionHostWillResignActive(notification:)),
name: .NSExtensionHostWillResignActive,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(extensionHostDidEnterBackground(notification:)),
name: .NSExtensionHostDidEnterBackground,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(extensionHostWillEnterForeground(notification:)),
name: .NSExtensionHostWillEnterForeground,
object: nil
)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
// MARK: - Notifications
@objc private func extensionHostDidBecomeActive(notification: NSNotification) {
AssertIsOnMainThread()
OWSLogger.info("")
self.reportedApplicationState = .active
NotificationCenter.default.post(
name: .OWSApplicationDidBecomeActive,
object: nil
)
}
@objc private func extensionHostWillResignActive(notification: NSNotification) {
AssertIsOnMainThread()
self.reportedApplicationState = .inactive
OWSLogger.info("")
DDLog.flushLog()
NotificationCenter.default.post(
name: .OWSApplicationWillResignActive,
object: nil
)
}
@objc private func extensionHostDidEnterBackground(notification: NSNotification) {
AssertIsOnMainThread()
OWSLogger.info("")
DDLog.flushLog()
self.reportedApplicationState = .background
NotificationCenter.default.post(
name: .OWSApplicationDidEnterBackground,
object: nil
)
}
@objc private func extensionHostWillEnterForeground(notification: NSNotification) {
AssertIsOnMainThread()
OWSLogger.info("")
self.reportedApplicationState = .inactive
NotificationCenter.default.post(
name: .OWSApplicationWillEnterForeground,
object: nil
)
}
// MARK: - AppContext Functions
func isAppForegroundAndActive() -> Bool {
return (reportedApplicationState == .active)
}
func isInBackground() -> Bool {
return (reportedApplicationState == .background)
}
func frontmostViewController() -> UIViewController? {
return rootViewController.findFrontmostViewController(ignoringAlerts: true)
}
func appDocumentDirectoryPath() -> String {
let targetPath: String? = FileManager.default
.urls(
for: .documentDirectory,
in: .userDomainMask
)
.last?
.path
owsAssertDebug(targetPath != nil)
return (targetPath ?? "")
}
func appSharedDataDirectoryPath() -> String {
let targetPath: String? = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: SignalApplicationGroup)?
.path
owsAssertDebug(targetPath != nil)
return (targetPath ?? "")
}
func appUserDefaults() -> UserDefaults {
let targetUserDefaults: UserDefaults? = UserDefaults(suiteName: SignalApplicationGroup)
owsAssertDebug(targetUserDefaults != nil)
return (targetUserDefaults ?? UserDefaults.standard)
}
func setStatusBarHidden(_ isHidden: Bool, animated isAnimated: Bool) {
OWSLogger.info("Ignoring request to show/hide status bar since we're in an app extension")
}
func beginBackgroundTask(expirationHandler: @escaping BackgroundTaskExpirationHandler) -> UIBackgroundTaskIdentifier {
return .invalid
}
func endBackgroundTask(_ backgroundTaskIdentifier: UIBackgroundTaskIdentifier) {
owsAssertDebug(backgroundTaskIdentifier == .invalid)
}
func ensureSleepBlocking(_ shouldBeBlocking: Bool, blockingObjects: [Any]) {
OWSLogger.debug("Ignoring request to block sleep.")
}
func setMainAppBadgeNumber(_ value: Int) {
owsFailDebug("")
}
func setNetworkActivityIndicatorVisible(_ value: Bool) {
owsFailDebug("")
}
func runNowOr(whenMainAppIsActive block: @escaping AppActiveBlock) {
owsFailDebug("cannot run main app active blocks in share extension.")
}
}