usage_stats 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 69

usage_stats #

Query Android Usage Statistics (Configuration, Events, App Usage)

Install #

Add usage_stats as a dependency in pubspec.yaml.

Android #

NB: Requires API level 22 as a minimum!

Add the following permission to the manifest namespace in AndroidManifest.xml:

    <uses-permission
        android:name="android.permission.PACKAGE_USAGE_STATS"
        tools:ignore="ProtectedPermissions" />

Usage #

import 'package:usage_stats/usage_stats.dart';

getUsage() async {

    DateTime endDate = new DateTime.now();
    DateTime startDate = DateTime(endDate.year, endDate.month, endDate.day, 0, 0, 0);
    
    // grant usage permission - opens Usage Settings
    UsageStats.grantUsagePermission();
    
    // check if permission is granted
    bool isPermission = UsageStats.checkUsagePermission();
    
    // query events
    List<EventUsageInfo> events = await UsageStats.queryEvents(startDate, endDate);
    
    // query usage stats
    List<UsageInfo> usageStats = await UsageStats.queryUsageStats(startDate, endDate);
    
    // query eventStats API Level 28
    List<EventInfo> eventStats = await UsageStats.queryEventStats(startDate, endDate);
    
    // query configurations
    List<ConfigurationInfo> configurations = await UsageStats.queryConfiguration(startDate, endDate);
    
    // query aggregated Usage statistics
    Map<String, UsageInfo> queryAndAggregateUsageStats = await UsageStats.queryAndAggregateUsageStats(startDate, endDate);

}

To DO #

  • Add option to pass Interval Type in queryUsageStats, queryEventStats method

0.1.0 #

  • Add return type
  • Add classes for UsageInfo, EventInfo, ConfigurationInfo, EventUsageInfo
  • Update example and ReadMe

0.0.2 #

  • Fix pubspec.yaml bug

0.0.1 #

  • Add Query UsageStats API
  • Add Grant Usage Permission and Check Permission API
  • Add Query Event and EventStats API
  • Add Query Configuration API

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:usage_stats/usage_stats.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<EventUsageInfo> events;

  @override
  void initState() {
    super.initState();
    initUsage();
  }

  Future<void> initUsage() async {
    UsageStats.grantUsagePermission();
    DateTime endDate = new DateTime.now();
    DateTime startDate = DateTime(endDate.year, endDate.month, 29, 0, 0, 0);
    List<EventUsageInfo> queryEvents =
        await UsageStats.queryEvents(startDate, endDate);

    this.setState(() {
      events = queryEvents.reversed.toList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text("Usage Stats"),
        ),
        body: Container(
            child: ListView.separated(
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(events[index].packageName),
                    subtitle: Text(
                        "Last time used: ${DateTime.fromMillisecondsSinceEpoch(int.parse(events[index].timeStamp)).toIso8601String()}"),
                    trailing: Text(events[index].eventType),
                  );
                },
                separatorBuilder: (context, index) => Divider(),
                itemCount: events.length)),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            initUsage();
          },
          child: Icon(
            Icons.refresh,
          ),
          mini: true,
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  usage_stats: ^0.1.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:usage_stats/usage_stats.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
37
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform ios

Because of import path [package:usage_stats/usage_stats.dart] that declares support for platforms: android

Package does not support Flutter platform linux

Because of import path [package:usage_stats/usage_stats.dart] that declares support for platforms: android

Package does not support Flutter platform macos

Because of import path [package:usage_stats/usage_stats.dart] that declares support for platforms: android

Package does not support Flutter platform web

Because of import path [package:usage_stats/usage_stats.dart] that declares support for platforms: android

Package does not support Flutter platform windows

Because of import path [package:usage_stats/usage_stats.dart] that declares support for platforms: android

Package not compatible with SDK dart

because of import path [usage_stats] that is in a package requiring null.

Health suggestions

Format lib/usage_stats.dart.

Run flutter format to format lib/usage_stats.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test