streamAppUsage method

  1. @override
Stream<Map<String, dynamic>> streamAppUsage({
  1. UsageInterval usageInterval = UsageInterval.daily,
  2. int lookbackTimeMs = 10000,
})
override

Stream app usage data in real-time.

This method returns a Stream that emits events whenever the foreground app changes. It uses the native AppMonitoringService to provide real-time updates.

Parameters:

  • usageInterval: The interval to use for usage stats queries (DAILY, WEEKLY, MONTHLY, YEARLY, BEST)
  • lookbackTimeMs: How far back in time to look for app usage data (in milliseconds)

Returns a Stream of Map<String, dynamic> containing the foreground app data.

Implementation

@override
Stream<Map<String, dynamic>> streamAppUsage({
  UsageInterval usageInterval = UsageInterval.daily,
  int lookbackTimeMs = 10000,
}) {
  // First configure the service with the specified parameters
  configureAppMonitoringService(
    interval: usageInterval,
    lookbackTimeMs: lookbackTimeMs,
  );

  // Return the stream from the event channel
  return eventChannel.receiveBroadcastStream({
    Argument.interval: usageInterval.name,
    Argument.lookbackTimeMs: lookbackTimeMs,
  }).map((dynamic event) {
    // Convert the event data to the expected type
    if (event is Map) {
      return _convertNestedMap(event);
    } else {
      throw PlatformException(
        code: 'INVALID_EVENT',
        message: 'Invalid event format received from native platform',
      );
    }
  });
}