AppDelegate.swift 3.14 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import UIKit
import Flutter

@UIApplicationMain
9
@objc class AppDelegate: FlutterAppDelegate, FlutterStreamHandler {
10
  private var eventSink: FlutterEventSink?;
11 12 13 14

  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
15
    GeneratedPluginRegistrant.register(with: self);
16 17 18 19
    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController;
    let batteryChannel = FlutterMethodChannel.init(name: "samples.flutter.io/battery",
                                                   binaryMessenger: controller);
    batteryChannel.setMethodCallHandler({
20
      (call: FlutterMethodCall, result: FlutterResult) -> Void in
21 22 23 24 25
      if ("getBatteryLevel" == call.method) {
        self.receiveBatteryLevel(result: result);
      } else {
        result(FlutterMethodNotImplemented);
      }
26
    });
27 28 29 30

    let chargingChannel = FlutterEventChannel.init(name: "samples.flutter.io/charging",
                                                   binaryMessenger: controller);
    chargingChannel.setStreamHandler(self);
31
    return super.application(application, didFinishLaunchingWithOptions: launchOptions);
32 33
  }

34
  private func receiveBatteryLevel(result: FlutterResult) {
35 36 37 38 39 40 41 42 43 44 45 46
    let device = UIDevice.current;
    device.isBatteryMonitoringEnabled = true;
    if (device.batteryState == UIDeviceBatteryState.unknown) {
      result(FlutterError.init(code: "UNAVAILABLE",
                               message: "Battery info unavailable",
                               details: nil));
    } else {
      result(Int(device.batteryLevel * 100));
    }
  }

  public func onListen(withArguments arguments: Any?,
47 48
                       eventSink: @escaping FlutterEventSink) -> FlutterError? {
    self.eventSink = eventSink;
49
    UIDevice.current.isBatteryMonitoringEnabled = true;
50
    self.sendBatteryStateEvent();
51 52 53 54 55 56 57 58 59
    NotificationCenter.default.addObserver(
      self,
      selector: #selector(onBatteryStateDidChange),
      name: NSNotification.Name.UIDeviceBatteryStateDidChange,
      object: nil)
    return nil;
  }

  @objc private func onBatteryStateDidChange(notification: NSNotification) {
60 61 62 63
    self.sendBatteryStateEvent();
  }

  private func sendBatteryStateEvent() {
64
    if (eventSink == nil) {
65 66 67 68 69 70
      return;
    }

    let state = UIDevice.current.batteryState;
    switch state {
    case UIDeviceBatteryState.full:
71
      eventSink!("charging");
72 73
      break;
    case UIDeviceBatteryState.charging:
74
      eventSink!("charging");
75 76
      break;
    case UIDeviceBatteryState.unplugged:
77
      eventSink!("discharging");
78 79
      break;
    default:
80 81 82
      eventSink!(FlutterError.init(code: "UNAVAILABLE",
                                   message: "Charging status unavailable",
                                   details: nil));
83 84 85 86 87 88
      break;
    }
  }

  public func onCancel(withArguments arguments: Any?) -> FlutterError? {
    NotificationCenter.default.removeObserver(self);
89
    eventSink = nil;
90
    return nil;
91 92
  }
}