getMeasurement method

  1. @override
Future<Measurement> getMeasurement()
override

Subclasses should implement this method to collect a Measurement.

Can return null if no data is available. Can return an Error measurement if an error occurs.

Implementation

@override
Future<Measurement> getMeasurement() async {
  // get the last mark - if null, go back as specified in history
  DateTime start =
      samplingConfiguration.lastTime ??
      DateTime.now().subtract(samplingConfiguration.past);
  DateTime end = DateTime.now();

  debug(
    'Collecting app usage - start: ${start.toUtc()}, end: ${end.toUtc()}',
  );
  List<app_usage.AppUsageInfo> infos = await app_usage.AppUsage().getAppUsage(
    start,
    end,
  );

  Map<String, AppUsageInfo> usage = {};
  for (var info in infos) {
    usage[info.packageName] = AppUsageInfo.fromAppUsageInfo(info);
  }

  return Measurement(
    sensorStartTime: start.microsecondsSinceEpoch,
    sensorEndTime: end.microsecondsSinceEpoch,
    data: AppUsage(start.toUtc(), end.toUtc(), usage),
  );
}