65 lines
1.6 KiB
Dart
65 lines
1.6 KiB
Dart
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;
|
|
}
|
|
}
|