Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinX8 committed Jan 19, 2023
1 parent 6aaa683 commit a9ce375
Show file tree
Hide file tree
Showing 13 changed files with 500 additions and 449 deletions.
24 changes: 11 additions & 13 deletions lib/logger/android_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import 'package:permission_handler/permission_handler.dart';

class AndroidLogger extends SentenceLogger {
static final AndroidLogger _instance = AndroidLogger.init();
@override
final RegExp blacklist = RegExp(".*system.*|.*keyboard.*|.*input.*|"
".*honeyboard.*|.*swiftkey.*|.*lawnchair.*|.*launcher.*|.*settings.*");



factory AndroidLogger() {
return _instance;
Expand Down Expand Up @@ -55,24 +50,27 @@ class AndroidLogger extends SentenceLogger {
),
);
var status = await Permission.notification.request();
if (status.isGranted) {
FlutterForegroundTask.startService(notificationTitle: "Sentiment Tracker",
notificationText: "Analyzing sentence sentiment");
}
if (status.isGranted) {
FlutterForegroundTask.startService(
notificationTitle: "Sentiment Tracker",
notificationText: "Analyzing sentence sentiment");
}
}

Future<void> startAccessibility() async {
bool status = await FlutterAccessibilityService.isAccessibilityPermissionEnabled();
bool status =
await FlutterAccessibilityService.isAccessibilityPermissionEnabled();
if (!status) {
status = await FlutterAccessibilityService.requestAccessibilityPermission();
status =
await FlutterAccessibilityService.requestAccessibilityPermission();
}
if (status) {
FlutterAccessibilityService.accessStream.listen(_accessibilityListener);
}
}

static void _accessibilityListener(AccessibilityEvent event) {
if (AndroidLogger().blacklist.hasMatch(event.packageName!)){
if (AndroidLogger().blacklist.hasMatch(event.packageName!)) {
return;
}
if (event.eventType == EventType.typeWindowStateChanged) {
Expand Down Expand Up @@ -106,4 +104,4 @@ extension NameConversion on AccessibilityEvent {
}
return null;
}
}
}
41 changes: 24 additions & 17 deletions lib/logger/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import 'dart:isolate';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:drift/isolate.dart';
import 'package:flutter/cupertino.dart';
import 'package:negate/sentiment_db.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../sentiment_analysis.dart';

Expand All @@ -29,7 +27,8 @@ class SentenceLogger {
static late DriftIsolate _iso;
static late TfParams _tfp;
static const int _updateFreq = 1; //update db every 5 minutes
RegExp blacklist = RegExp(r".*system.*|.*keyboard.*|.*input.*|.*honeyboard.*|.*swiftkey.*");
RegExp blacklist =
RegExp(r".*system.*|.*keyboard.*|.*input.*|.*honeyboard.*|.*swiftkey.*");
String _lastUsedApp = "";
bool _dbUpdated = false;
double? _multiplier;
Expand All @@ -41,7 +40,8 @@ class SentenceLogger {
SentenceLogger.init();

void logToDB() {
Isolate.spawn(SentimentDB.addSentiments, AddSentimentRequest(_appMap, _iso.connectPort));
Isolate.spawn(SentimentDB.addSentiments,
AddSentimentRequest(_appMap, _iso.connectPort));
}

static void _startBackground(IsolateStartRequest request) {
Expand All @@ -52,7 +52,7 @@ class SentenceLogger {
// background isolate. If we used DriftIsolate.spawn, a third isolate would be
// started which is not what we want!
final driftIsolate = DriftIsolate.inCurrent(
() => DatabaseConnection(executor),
() => DatabaseConnection(executor),
);
// inform the starting isolate about this, so that it can call .connect()
request.sendDriftIsolate.send(driftIsolate);
Expand All @@ -62,7 +62,8 @@ class SentenceLogger {
var rPort = ReceivePort();
await Isolate.spawn(
_startBackground,
IsolateStartRequest(sendDriftIsolate: rPort.sendPort, targetPath: request.targetPath),
IsolateStartRequest(
sendDriftIsolate: rPort.sendPort, targetPath: request.targetPath),
);

var prefs = request.prefs;
Expand Down Expand Up @@ -93,8 +94,7 @@ class SentenceLogger {
SentenceLogger._sentence.clear();
}

void addAppEntry
() {
void addAppEntry() {
log(getSentence());
if (getSentence().length < 6) {
clearSentence();
Expand All @@ -108,22 +108,27 @@ class SentenceLogger {

var now = DateTime.now();
//Update average score for all apps used in the last 10 minutes as well
var appsInPeriod = _appMap.entries.where((element) => element.value.lastTimeUsed.difference(now).inMinutes <= 10);
var appsInPeriod = _appMap.entries.where((element) =>
element.value.lastTimeUsed.difference(now).inMinutes <= 10);
for (var app in appsInPeriod) {
if (app.key == name) continue;
if (_multiplier == null) {
app.value.avgScore =
((app.value.avgScore * app.value.numCalled) + score) /
(++app.value.numCalled);
} else {
app.value.avgScore = (app.value.avgScore * _multiplier!) + (score * (1 - _multiplier!));
app.value.avgScore =
(app.value.avgScore * _multiplier!) + (score * (1 - _multiplier!));
}
}

if (_appMap.containsKey(name)) {
double timeUsedSince = now.difference(_appMap[name]!.lastTimeUsed).inSeconds.toDouble() / 60;
double timeUsedSince =
now.difference(_appMap[name]!.lastTimeUsed).inSeconds.toDouble() / 60;
double totalTimeUsed = _appMap[name]!.totalTimeUsed + timeUsedSince;
double avgScore = ((_appMap[name]!.avgScore * _appMap[name]!.numCalled) + score) / (_appMap[name]!.numCalled + 1);
double avgScore =
((_appMap[name]!.avgScore * _appMap[name]!.numCalled) + score) /
(_appMap[name]!.numCalled + 1);

//If the next hour has been reached reset the average score and time used
if (now.hour != _appMap[name]!.lastTimeUsed.hour) {
Expand All @@ -135,7 +140,8 @@ class SentenceLogger {
}
_appMap[name]!.numCalled = 0;
}
_appMap[name] = AppList(now, totalTimeUsed, avgScore, _appMap[name]!.numCalled + 1);
_appMap[name] =
AppList(now, totalTimeUsed, avgScore, _appMap[name]!.numCalled + 1);
} else {
_appMap.putIfAbsent(name, () => AppList(now, 0, score, 1));
}
Expand All @@ -154,8 +160,8 @@ class SentenceLogger {
return;
}
if (_appMap.containsKey(name)) {
double timeUsedSince = now.difference(_appMap[name]!.lastTimeUsed)
.inSeconds.toDouble() / 60;
double timeUsedSince =
now.difference(_appMap[name]!.lastTimeUsed).inSeconds.toDouble() / 60;
if (name == _lastUsedApp) {
if (now.hour != _appMap[name]!.lastTimeUsed.hour) {
if (timeUsedSince / now.minute > 1) {
Expand All @@ -181,6 +187,7 @@ class SentenceLogger {

void addAppIcon(String name, Uint8List icon) {
_appIcons.add(name);
Isolate.spawn(SentimentDB.addAppIcon, AddAppIconRequest(name, icon, _iso.connectPort));
Isolate.spawn(SentimentDB.addAppIcon,
AddAppIconRequest(name, icon, _iso.connectPort));
}
}
}
14 changes: 7 additions & 7 deletions lib/logger/logger_factory.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import 'dart:io' show Platform;

import 'package:negate/logger/logger.dart';

import '../sentiment_db.dart';
import 'android_logger.dart';
import 'win_logger.dart';

class LoggerFactory {

@pragma('vm:entry-point')
static Future<void> startLoggerFactory(TfliteRequest request) async {
if (Platform.isWindows) {
Expand All @@ -19,13 +16,16 @@ class LoggerFactory {
}
}

static SentenceLogger getLogger() {
static RegExp getLoggerRegex() {
if (Platform.isWindows) {
return WinLogger();
return RegExp(
r".*system.*|.*keyboard.*|.*input.*|.*honeyboard.*|.*swiftkey.*|.*settings.*|.*explorer.*|.*host$|"
r".*lockapp.*|.*widgets.*|.*setup.*|.*uninstall.*|.*taskmgr.*|.*openwith.*|.*msiexec.*");
} else if (Platform.isAndroid) {
return AndroidLogger();
return RegExp(r".*system.*|.*keyboard.*|.*input.*|"
r".*honeyboard.*|.*swiftkey.*|.*lawnchair.*|.*launcher.*|.*settings.*");
} else {
throw UnsupportedError("This platform is unsupported!");
}
}
}
}
Loading

0 comments on commit a9ce375

Please sign in to comment.