feat: add device and background services
This commit is contained in:
parent
32fd7da5be
commit
22d364893c
2 changed files with 206 additions and 0 deletions
65
lib/services/device_service.dart
Normal file
65
lib/services/device_service.dart
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import 'package:flutter/services.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'dart:io';
|
||||
|
||||
class DeviceService {
|
||||
static const _channel = MethodChannel('xyz.nuark.update_forge_companion/device');
|
||||
|
||||
Future<String?> getInstalledVersion(String packageName) async {
|
||||
try {
|
||||
final result = await _channel.invokeMethod<String>(
|
||||
'getInstalledVersion',
|
||||
{'packageName': packageName},
|
||||
);
|
||||
return result;
|
||||
} on PlatformException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> installApk(String filePath) async {
|
||||
try {
|
||||
final result = await _channel.invokeMethod<bool>(
|
||||
'installApk',
|
||||
{'filePath': filePath},
|
||||
);
|
||||
return result ?? false;
|
||||
} on PlatformException {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> openApp(String packageName) async {
|
||||
try {
|
||||
final result = await _channel.invokeMethod<bool>(
|
||||
'openApp',
|
||||
{'packageName': packageName},
|
||||
);
|
||||
return result ?? false;
|
||||
} on PlatformException {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> downloadFile({
|
||||
required Stream<List<int>> stream,
|
||||
required int? contentLength,
|
||||
required String fileName,
|
||||
void Function(int received, int? total)? onProgress,
|
||||
}) async {
|
||||
final dir = await getTemporaryDirectory();
|
||||
final file = File('${dir.path}/$fileName');
|
||||
final sink = file.openWrite();
|
||||
|
||||
int received = 0;
|
||||
await for (final chunk in stream) {
|
||||
sink.add(chunk);
|
||||
received += chunk.length;
|
||||
onProgress?.call(received, contentLength);
|
||||
}
|
||||
await sink.flush();
|
||||
await sink.close();
|
||||
|
||||
return file.path;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue