π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Implement iOS screen brightness control plugin for Ente
AmanRajSinghMouryae53973347c1d
diff --git a/mobile/packages/ente_screen_brightness/ios/Classes/EnteScreenBrightnessPlugin.swift b/mobile/packages/ente_screen_brightness/ios/Classes/EnteScreenBrightnessPlugin.swiftnew file mode 100644index 0000000000..7181eb26e9--- /dev/null+++ b/mobile/packages/ente_screen_brightness/ios/Classes/EnteScreenBrightnessPlugin.swift@@ -0,0 +1,108 @@+import Flutter+import UIKit++public class EnteScreenBrightnessPlugin: NSObject, FlutterPlugin {+ private var capturedScreen: UIScreen?+ private var capturedBrightness: CGFloat?++ override init() {+ super.init()+ NotificationCenter.default.addObserver(+ self,+ selector: #selector(restoreForAppDeactivation(_:)),+ name: UIApplication.willResignActiveNotification,+ object: nil+ )+ NotificationCenter.default.addObserver(+ self,+ selector: #selector(restoreForAppDeactivation(_:)),+ name: UIApplication.willTerminateNotification,+ object: nil+ )+ }++ public static func register(with registrar: FlutterPluginRegistrar) {+ let channel = FlutterMethodChannel(+ name: "io.ente.screen_brightness",+ binaryMessenger: registrar.messenger()+ )+ let instance = EnteScreenBrightnessPlugin()+ registrar.addMethodCallDelegate(instance, channel: channel)+ }++ public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {+ switch call.method {+ case "setBrightness":+ guard let number = call.arguments as? NSNumber else {+ result(FlutterError(+ code: "INVALID_BRIGHTNESS",+ message: "Brightness must be a number from 0 to 1.",+ details: nil+ ))+ return+ }++ let brightness = CGFloat(number.doubleValue)+ guard brightness.isFinite, (0...1).contains(brightness) else {+ result(FlutterError(+ code: "INVALID_BRIGHTNESS",+ message: "Brightness must be a number from 0 to 1.",+ details: nil+ ))+ return+ }++ guard UIApplication.shared.applicationState == .active else {+ result(false)+ return+ }++ guard let screen = activeScreen() else {+ result(FlutterError(+ code: "SCREEN_UNAVAILABLE",+ message: "No active iOS screen is available.",+ details: nil+ ))+ return+ }++ if capturedBrightness == nil {+ capturedScreen = screen+ capturedBrightness = screen.brightness+ }+ screen.brightness = min(screen.brightness, brightness)+ result(true)++ case "restoreBrightness":+ restoreCapturedBrightness()+ result(nil)++ default:+ result(FlutterMethodNotImplemented)+ }+ }++ private func activeScreen() -> UIScreen? {+ return UIApplication.shared.connectedScenes+ .compactMap { $0 as? UIWindowScene }+ .first { $0.activationState == .foregroundActive }?+ .screen+ }++ @objc private func restoreForAppDeactivation(_ notification: Notification) {+ restoreCapturedBrightness()+ }++ private func restoreCapturedBrightness() {+ guard let screen = capturedScreen,+ let brightness = capturedBrightness else { return }+ screen.brightness = brightness+ capturedScreen = nil+ capturedBrightness = nil+ }++ deinit {+ restoreCapturedBrightness()+ NotificationCenter.default.removeObserver(self)+ }+}diff --git a/mobile/packages/ente_screen_brightness/ios/ente_screen_brightness.podspec b/mobile/packages/ente_screen_brightness/ios/ente_screen_brightness.podspecnew file mode 100644index 0000000000..361fa9da54--- /dev/null+++ b/mobile/packages/ente_screen_brightness/ios/ente_screen_brightness.podspec@@ -0,0 +1,14 @@+Pod::Spec.new do |s|+ s.name = 'ente_screen_brightness'+ s.version = '0.0.1'+ s.summary = 'iOS screen brightness control for Ente.'+ s.homepage = 'https://github.com/ente-io/ente'+ s.license = { :type => 'AGPL-3.0-only' }+ s.author = { 'Ente' => '[email protected]' }+ s.source = { :path => '.' }+ s.source_files = 'Classes/**/*'+ s.dependency 'Flutter'+ s.platform = :ios, '15.1'+ s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }+ s.swift_version = '5.0'+enddiff --git a/mobile/packages/ente_screen_brightness/lib/ente_screen_brightness.dart b/mobile/packages/ente_screen_brightness/lib/ente_screen_brightness.dartnew file mode 100644index 0000000000..332773ca08--- /dev/null+++ b/mobile/packages/ente_screen_brightness/lib/ente_screen_brightness.dart@@ -0,0 +1,48 @@+import "dart:io";++import "package:flutter/services.dart";++/// Temporarily controls the iOS display brightness for the current app.+///+/// iOS exposes display brightness through `UIScreen`, not as a true per-app+/// value. The native plugin captures the brightness before the first change+/// and [restore] returns the display to that value.+class EnteScreenBrightness {+ EnteScreenBrightness._();++ static const MethodChannel _channel = MethodChannel(+ "io.ente.screen_brightness",+ );++ /// Dims the iOS display to at most a normalized [value] from 0 to 1.+ ///+ /// If the display is already dimmer than [value], its brightness is left+ /// unchanged so this API never makes the user's screen brighter.+ ///+ /// The first call captures the current brightness. Later calls do not+ /// replace that captured value, so [restore] always returns to the+ /// brightness from before this dimming session.+ ///+ /// Returns whether the brightness was applied. This is a no-op on non-iOS+ /// platforms and while the iOS app is not active.+ static Future<bool> setBrightness(double value) async {+ if (!value.isFinite || value < 0 || value > 1) {+ throw RangeError.range(value, 0, 1, "value");+ }+ if (!Platform.isIOS) {+ return false;+ }+ return await _channel.invokeMethod<bool>("setBrightness", value) ?? false;+ }++ /// Restores the brightness captured by the first [setBrightness] call.+ ///+ /// Calling this without an active dimming session has no effect. This is a+ /// no-op on non-iOS platforms.+ static Future<void> restore() async {+ if (!Platform.isIOS) {+ return;+ }+ await _channel.invokeMethod<void>("restoreBrightness");+ }+}diff --git a/mobile/packages/ente_screen_brightness/pubspec.yaml b/mobile/packages/ente_screen_brightness/pubspec.yamlnew file mode 100644index 0000000000..b0fc08680e--- /dev/null+++ b/mobile/packages/ente_screen_brightness/pubspec.yaml@@ -0,0 +1,19 @@+name: ente_screen_brightness+description: iOS screen brightness control for Ente Flutter apps++resolution: workspace+environment:+ sdk: ">=3.10.0 <4.0.0"++dependencies:+ flutter:+ sdk: flutter++dev_dependencies:+ flutter_lints: 6.0.0++flutter:+ plugin:+ platforms:+ ios:+ pluginClass: EnteScreenBrightnessPlugin