flutter_accessibility_service 0.2.5 flutter_accessibility_service: ^0.2.5 copied to clipboard
Flutter plugin for interacting with Accessibility Service in Android.
import 'dart:async';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_accessibility_service/accessibility_event.dart';
import 'package:flutter_accessibility_service/constants.dart';
import 'package:flutter_accessibility_service/flutter_accessibility_service.dart';
import 'package:flutter_accessibility_service_example/overlay.dart';
import 'package:collection/collection.dart';
@pragma("vm:entry-point")
void accessibilityOverlay() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: BlockingOverlay(),
),
);
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
StreamSubscription<AccessibilityEvent>? _subscription;
List<AccessibilityEvent?> events = [];
DateTime eventDateTime = DateTime.now();
bool foundSearchField = false;
bool setText = false;
bool clickFirstSearch = false;
@override
void initState() {
super.initState();
}
void handleAccessiblityStream() {
foundSearchField = false;
setText = false;
if (_subscription?.isPaused ?? false) {
_subscription?.resume();
return;
}
_subscription =
FlutterAccessibilityService.accessStream.listen((event) async {
setState(() {
events.add(event);
});
// automateScroll(event);
// log("$event");
// automateWikipedia(event);
handleOverlay(event);
});
}
void handleOverlay(AccessibilityEvent event) async {
if (event.packageName!.contains('youtube')) {
log('$event');
}
if (event.packageName!.contains('youtube') && event.isFocused!) {
eventDateTime = event.eventTime!;
await FlutterAccessibilityService.showOverlayWindow();
} else if (eventDateTime.difference(event.eventTime!).inSeconds.abs() > 2 ||
(event.eventType == EventType.typeWindowStateChanged &&
!event.isFocused!)) {
await FlutterAccessibilityService.hideOverlayWindow();
}
}
void automateWikipedia(AccessibilityEvent event) async {
if (!event.packageName!.contains('wikipedia')) return;
log('$event');
final searchIt = [...event.subNodes!, event].firstWhereOrNull(
(element) => element.text == 'Search Wikipedia' && element.isClickable!,
);
log("Searchable Field: $searchIt");
if (searchIt != null) {
await doAction(searchIt, NodeAction.actionClick);
final editField = [...event.subNodes!, event].firstWhereOrNull(
(element) => element.text == 'Search Wikipedia' && element.isEditable!,
);
if (editField != null) {
await doAction(editField, NodeAction.actionSetText, "Lionel Messi");
}
final item = [...event.subNodes!, event].firstWhereOrNull(
(element) => element.text == 'Messi–Ronaldo rivalry',
);
if (item != null) {
await doAction(item, NodeAction.actionSelect);
}
}
}
Future<bool> doAction(
AccessibilityEvent node,
NodeAction action, [
dynamic argument,
]) async {
return await FlutterAccessibilityService.performAction(
node,
action,
argument,
);
}
void automateScroll(AccessibilityEvent node) async {
if (!node.packageName!.contains('youtube')) return;
log("$node");
if (node.isFocused!) {
final scrollableNode = findScrollableNode(node);
log('$scrollableNode', name: 'SCROLLABLE- XX');
if (scrollableNode != null) {
await FlutterAccessibilityService.performAction(
node,
NodeAction.actionScrollForward,
);
}
}
}
AccessibilityEvent? findScrollableNode(AccessibilityEvent rootNode) {
if (rootNode.isScrollable! &&
rootNode.actions!.contains(NodeAction.actionScrollForward)) {
return rootNode;
}
if (rootNode.subNodes!.isEmpty) return null;
for (int i = 0; i < rootNode.subNodes!.length; i++) {
final childNode = rootNode.subNodes![i];
final scrollableChild = findScrollableNode(childNode);
if (scrollableChild != null) {
return scrollableChild;
}
}
return null;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextButton(
onPressed: () async {
await FlutterAccessibilityService
.requestAccessibilityPermission();
},
child: const Text("Request Permission"),
),
const SizedBox(height: 20.0),
TextButton(
onPressed: () async {
final bool res = await FlutterAccessibilityService
.isAccessibilityPermissionEnabled();
log("Is enabled: $res");
},
child: const Text("Check Permission"),
),
const SizedBox(height: 20.0),
TextButton(
onPressed: handleAccessiblityStream,
child: const Text("Start Stream"),
),
const SizedBox(height: 20.0),
TextButton(
onPressed: () {
_subscription?.cancel();
},
child: const Text("Stop Stream"),
),
TextButton(
onPressed: () async {
await FlutterAccessibilityService.performGlobalAction(
GlobalAction.globalActionTakeScreenshot,
);
},
child: const Text("Take ScreenShot"),
),
TextButton(
onPressed: () async {
final list = await FlutterAccessibilityService
.getSystemActions();
log('$list');
},
child: const Text("List GlobalActions"),
),
],
),
),
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: events.length,
itemBuilder: (_, index) => ListTile(
title: Text(events[index]!.packageName!),
subtitle: Text(
(events[index]!.subNodes ?? [])
.map((e) => e.actions)
.expand((element) => element!)
.contains(NodeAction.actionClick)
? 'Have Action to click'
: '',
),
),
),
)
],
),
),
),
);
}
}