acl_sdk 0.2.1
acl_sdk: ^0.2.1 copied to clipboard
A Flutter plugin which provide ACL functioning by Unified Intelligence. Which help you collect device's data and calculate device score.
example/lib/main.dart
import 'dart:async';
import 'dart:io';
import 'package:acl_sdk/acl_sdk.dart';
import 'package:acl_sdk/database/data_models/app_config.dart';
import 'package:acl_sdk/helpers/device_info_helper.dart';
import 'package:acl_sdk/helpers/permission_helper.dart';
import 'package:acl_sdk/work_manager_dispatchers/acl_fcm_on_receive_handler.dart';
import 'package:acl_sdk_example/firebase_options.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:geolocator/geolocator.dart';
import 'package:location_permissions/location_permissions.dart';
void main() {
// WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
String _platformVersion = 'Unknown';
String _deviceId = 'Unknown';
bool _isAllowAppUsage = false;
bool _isAllowLocationAlways = false;
bool _isAllowNotificationListener = false;
final _aclSdkPlugin = AclSdk();
@override
void initState() {
super.initState();
initPlatformState();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed) {
bool allowAppUsage = await PermissionHelper.isPackageUsageStatsEnable();
bool allowLocationAlways = (await LocationPermissions()
.checkPermissionStatus(
level: LocationPermissionLevel.locationAlways)) ==
PermissionStatus.granted;
bool allowNotificationListener =
await PermissionHelper.isNotificationListenerRunning();
setState(() {
_isAllowAppUsage = allowAppUsage;
_isAllowLocationAlways = allowLocationAlways;
_isAllowNotificationListener = allowNotificationListener;
});
}
super.didChangeAppLifecycleState(state);
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
bool allowAppUsage = false;
bool allowLocationAlways = false;
bool allowNotificationListener = false;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
// init firebase
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform);
await FirebaseMessaging.instance.requestPermission();
// set the background messaging handler
FirebaseMessaging.onBackgroundMessage(aclFCMOnReceiveHandler);
FirebaseMessaging.onMessage.listen(aclFCMOnReceiveHandler);
platformVersion = await _aclSdkPlugin.getPlatformVersion() ??
'Unknown platform version';
allowAppUsage = await PermissionHelper.isPackageUsageStatsEnable();
allowLocationAlways = (await LocationPermissions().checkPermissionStatus(
level: LocationPermissionLevel.locationAlways)) ==
PermissionStatus.granted;
allowNotificationListener =
await PermissionHelper.isNotificationListenerRunning();
// To get device id
_deviceId = await DeviceInfoHelper.generateDeviceId();
// To init SDK plugin
await _aclSdkPlugin.initialize(
AppConfigCreateInput(
partnerId: '',
apiEndpoint: '',
apiKey: '',
apiSecret: '',
appBundle: '',
),
isInDebugMode: true,
);
await _aclSdkPlugin.registerDevice();
await _aclSdkPlugin.mapDeviceMember('hello-world');
await _aclSdkPlugin.startCollectData();
await _aclSdkPlugin.startCollectLocation();
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
_isAllowAppUsage = allowAppUsage;
_isAllowLocationAlways = allowLocationAlways;
_isAllowNotificationListener = allowNotificationListener;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
Text(_deviceId),
Text('Running on: $_platformVersion\n'),
ElevatedButton(
onPressed: _handleAllowAppUsageClick,
child: Text(
_isAllowAppUsage == true
? 'Allowed app usage access'
: 'Press to allow app usage access!',
),
),
ElevatedButton(
onPressed: _handleAllowLocationAlwaysClick,
child: Text(
_isAllowLocationAlways == true
? 'Allowed location always access'
: 'Press to allow location always access!',
),
),
ElevatedButton(
onPressed: _handleAllowNotificationListenerClick,
child: Text(
_isAllowNotificationListener == true
? 'Allowed notification listener access'
: 'Press to allow notification listener access!',
),
),
],
),
),
),
);
}
void _handleAllowAppUsageClick() async {
final isAlreadyAllow = await PermissionHelper.isPackageUsageStatsEnable();
if (isAlreadyAllow == false) {
await PermissionHelper.goToAppUsageSetting();
}
}
void _handleAllowLocationAlwaysClick() async {
if (Platform.isIOS) {
var permission = await LocationPermissions().requestPermissions(
permissionLevel: LocationPermissionLevel.locationWhenInUse);
permission = await LocationPermissions().requestPermissions(
permissionLevel: LocationPermissionLevel.locationAlways);
if (permission == PermissionStatus.granted) {
return;
}
if (permission != PermissionStatus.denied) {
LocationPermissions().requestPermissions(
permissionLevel: LocationPermissionLevel.locationAlways);
}
setState(() {
_isAllowLocationAlways = permission == PermissionStatus.granted;
});
} else {
final permission = await Geolocator.requestPermission();
if (permission == LocationPermission.always) {
return;
}
if (permission != LocationPermission.denied &&
permission != LocationPermission.deniedForever) {
Geolocator.openAppSettings();
}
setState(() {
_isAllowLocationAlways = permission == LocationPermission.always;
});
}
await _aclSdkPlugin.startCollectLocation();
}
void _handleAllowNotificationListenerClick() async {
final isAlreadyAllow =
await PermissionHelper.isNotificationListenerRunning();
if (isAlreadyAllow == false) {
await PermissionHelper.goToNotificationListenerSetting();
}
}
}