Commit 18454438 authored by Nicolas Richard Walter Boeckh's avatar Nicolas Richard Walter Boeckh 💬

Made kotlin wake service.

parent dfc34fbb
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"battery","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\battery-0.3.1+7\\\\","dependencies":[]},{"name":"geolocator","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\geolocator-5.2.1\\\\","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\google_api_availability-2.0.2\\\\","dependencies":[]},{"name":"location_permissions","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\location_permissions-2.0.4+1\\\\","dependencies":[]},{"name":"path_provider","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider-1.6.0\\\\","dependencies":[]},{"name":"permission_handler","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\permission_handler-4.2.0+hotfix.3\\\\","dependencies":[]},{"name":"plugin_scaffold","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\plugin_scaffold-1.2.0\\\\","dependencies":[]},{"name":"rx_ble","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\rx_ble-0.6.1\\\\","dependencies":["plugin_scaffold"]},{"name":"sqflite","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-1.2.0\\\\","dependencies":[]}],"android":[{"name":"battery","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\battery-0.3.1+7\\\\","dependencies":[]},{"name":"geolocator","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\geolocator-5.2.1\\\\","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\google_api_availability-2.0.2\\\\","dependencies":[]},{"name":"location_permissions","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\location_permissions-2.0.4+1\\\\","dependencies":[]},{"name":"path_provider","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider-1.6.0\\\\","dependencies":[]},{"name":"permission_handler","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\permission_handler-4.2.0+hotfix.3\\\\","dependencies":[]},{"name":"plugin_scaffold","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\plugin_scaffold-1.2.0\\\\","dependencies":[]},{"name":"rx_ble","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\rx_ble-0.6.1\\\\","dependencies":["plugin_scaffold"]},{"name":"sqflite","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-1.2.0\\\\","dependencies":[]}],"macos":[{"name":"sqflite","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-1.2.0\\\\","dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"battery","dependencies":[]},{"name":"geolocator","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","dependencies":[]},{"name":"location_permissions","dependencies":[]},{"name":"path_provider","dependencies":[]},{"name":"permission_handler","dependencies":[]},{"name":"plugin_scaffold","dependencies":[]},{"name":"rx_ble","dependencies":["plugin_scaffold"]},{"name":"sqflite","dependencies":[]}],"date_created":"2020-02-10 16:34:46.591794","version":"1.14.7-pre.83"}
\ No newline at end of file
{"_info":"// This is a generated file; do not edit or check into version control.","dependencyGraph":[{"name":"battery","dependencies":[]},{"name":"geolocator","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","dependencies":[]},{"name":"location_permissions","dependencies":[]},{"name":"path_provider","dependencies":[]},{"name":"permission_handler","dependencies":[]},{"name":"plugin_scaffold","dependencies":[]},{"name":"rx_ble","dependencies":["plugin_scaffold"]},{"name":"sqflite","dependencies":[]}]}
\ No newline at end of file
......@@ -62,6 +62,7 @@ flutter {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0-M2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
......
......@@ -7,6 +7,7 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:name="io.flutter.app.FlutterApplication"
android:label="logair_application"
......
package com.logair.logair_application;
class Channels {
companion object {
@JvmStatic val BATTERY_CHANNEL: String = "logair.application/battery";
@JvmStatic val BATTERY_CHARGE: String = "logair.aplication/battery_charge_status";
@JvmStatic val BLUETOOTH_CHANNEL: String = "logair.application/btle";
@JvmStatic val WAKE_CHANNEL: String = "logair.application/wake";
@JvmStatic val WAKE_CHANNEL_NOTIFICATION: String = "logair.application/wake_notification";
}
}
object Channels {
@JvmStatic val BATTERY: String = "logair.application/battery";
@JvmStatic val BATTERY_CHARGE: String = "logair.application/battery_charge_status";
@JvmStatic val WAKE: String = "logair.application/wake";
@JvmStatic val WAKE_NOTIFICATION: String = "logair.application/wake_notification";
}
......@@ -10,6 +10,7 @@ import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import io.flutter.plugin.common.MethodChannel
......@@ -19,12 +20,9 @@ import java.util.Timer;
import java.util.TimerTask;
class MainActivity: FlutterActivity() {
val timer: Timer = Timer();
val handler: Handler = Handler();
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, Channels.BATTERY_CHANNEL).setMethodCallHandler {
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, Channels.BATTERY).setMethodCallHandler {
call, result ->
if (call.method == "getBatteryLevel") {
val batteryLevel = getBatteryLevel()
......@@ -37,12 +35,34 @@ class MainActivity: FlutterActivity() {
result.notImplemented()
};
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, Channels.WAKE).setMethodCallHandler {
call, result ->
if (call.method == "startWakeService") {
modifyWakeService(Actions.START);
result.success(true);
} else if (call.method == "endWakeService") {
modifyWakeService(Actions.STOP);
result.success(false);
} else
result.notImplemented();
}
// Method Channel Dart to Platform : result is WakeService Starting
}
private fun startWakeService(action: Actions) {
private fun modifyWakeService(action: Actions) {
if (getServiceState(this) == ServiceState.STOPPED && action == Actions.STOP)
return;
log("$action");
Intent(this, WakeService::class.java).also {
it.action = action.name;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(it);
return;
}
startService(it);
}
}
private fun getBatteryLevel(): Int {
......
package com.logair.logair_application
import android.app.*;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import android.provider.Settings;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.*;
import kotlinx.coroutines.*;
import com.logair.logair_application.Channels;
import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.IBinder
import android.os.PowerManager
import android.widget.Toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.sql.Timestamp
class WakeService : Service() {
private var wakeLock: PowerManager.WakeLock? = null;
......@@ -68,7 +66,7 @@ class WakeService : Service() {
GlobalScope.launch(Dispatchers.IO) {
while (isServiceStarted) {
launch(Dispatchers.IO) {
// DO PING DART
log("PINGING DART ${Timestamp(System.currentTimeMillis())}");
}
delay(1 * 1000); // 1000ms = 1s
}
......@@ -83,7 +81,7 @@ class WakeService : Service() {
try {
wakeLock?.let {
if (it.isHeld)
id.release();
it.release();
};
stopForeground(true);
stopSelf();
......@@ -98,7 +96,7 @@ class WakeService : Service() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager;
val channel = NotificationChannel(
Channels.WAKE_CHANNEL_NOTIFICATION,
Channels.WAKE_NOTIFICATION,
"LogAir Connector Notification Channel",
NotificationManager.IMPORTANCE_HIGH
).let {
......@@ -118,7 +116,7 @@ class WakeService : Service() {
val builder: Notification.Builder =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
Notification.Builder(this, Channels.WAKE_CHANNEL_NOTIFICATION)
Notification.Builder(this, Channels.WAKE_NOTIFICATION)
else
Notification.Builder(this)
......
class Channels {
static const String BATTERY = 'logair.aplication/battery';
static const String BATTERY_CHARGE = 'logair.aplication/battery_charge_status';
static const String BLUETOOTH = 'logair.aplication/btle';
static const String BATTERY = 'logair.application/battery';
static const String BATTERY_CHARGE = 'logair.application/bsattery_charge_status';
static const String WAKE = "logair.application/wake";
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:device_preview/device_preview.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:logair_application/localization/localization_delegate.dart';
import 'package:logair_application/platform/bluetooth_le_service.dart';
import 'package:logair_application/routes/home.dart';
// TODO Lang service, intialized with the right locale, where accessible from entire program
......@@ -17,6 +18,10 @@ class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
BTLEHandler();
return MaterialApp(
localizationsDelegates: [
const InternalLocalizationDelegate(),
......
......@@ -3,7 +3,6 @@ import 'package:logair_application/enums/platform_channels.dart';
class BatteryService {
static const MethodChannel methodChannel = const MethodChannel(Channels.BATTERY);
static const EventChannel eventChannel = const EventChannel(Channels.BATTERY_CHARGE);
Future<int> get batteryLevel async {
int batteryLevel;
......
import 'package:flutter/services.dart';
import 'package:logair_application/enums/platform_channels.dart';
class BTLEHandler {
static const platform = const MethodChannel('logair.application/btle');
Future<void> bluetoothTick() {
// Do BTLE stuff
return null;
}
// TODO Tick once per second as well
}
\ No newline at end of file
import 'package:flutter/services.dart';
import 'package:logair_application/enums/platform_channels.dart';
class WakeService {
static const MethodChannel methodChannel = const MethodChannel(Channels.WAKE);
Future<bool> modifyWakeService(bool start) async {
bool wakeServiceStatus;
try {
wakeServiceStatus = await methodChannel.invokeMethod(start ? 'startWakeService' : 'endWakeService');
} on PlatformException catch (e) {
print('WAKE_SERVICE : $e');
}
return wakeServiceStatus;
}
}
\ No newline at end of file
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:logair_application/platform/wake_service.dart';
import 'package:logair_application/ui/carousel_card.dart';
import 'package:logair_application/platform/battery_service.dart';
......@@ -13,7 +14,7 @@ class DataWidget extends StatelessWidget {
children: [
RaisedButton(
onPressed: () => {
BatteryService().batteryLevel.then((value) => print(value))
BatteryService().batteryLevel.then((value) => print('Battery is at $value%'))
},
child: Text(
'Do whatever',
......@@ -21,9 +22,27 @@ class DataWidget extends StatelessWidget {
),
),
Text("Data", style: TextStyle(fontFamily: "Consolas", fontSize: 22)),
RaisedButton(
onPressed: () => {
WakeService().modifyWakeService(true).then((value) => print('Device will stay awake $value'))
},
child: Text(
'Start BG Service Ticker',
style: TextStyle(fontSize: 20)
),
),
RaisedButton(
onPressed: () => {
WakeService().modifyWakeService(false).then((value) => print('Device will stay awake $value'))
},
child: Text(
'End BG Service Ticker',
style: TextStyle(fontSize: 20)
),
)
]
)
);
return CarouselCard(child: widget, color: Colors.green);
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment