Skip to content

Commit

Permalink
change wear library
Browse files Browse the repository at this point in the history
Codel1417 committed Sep 2, 2024
1 parent e1f6b29 commit eee8883
Showing 4 changed files with 111 additions and 92 deletions.
92 changes: 29 additions & 63 deletions lib/Backend/wear_bridge.dart
Original file line number Diff line number Diff line change
@@ -1,85 +1,57 @@
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:built_collection/built_collection.dart';
import 'package:collection/collection.dart';
import 'package:cross_platform/cross_platform.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_wear_os_connectivity/flutter_wear_os_connectivity.dart';
import 'package:logging/logging.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:watch_connectivity/watch_connectivity.dart';

import 'Bluetooth/bluetooth_manager.dart';
import 'Definitions/Action/base_action.dart';
import 'Definitions/Device/device_definition.dart';
import 'action_registry.dart';
import 'favorite_actions.dart';
import 'move_lists.dart';

part 'wear_bridge.g.dart';

final Logger _wearLogger = Logger('Wear');
FlutterWearOsConnectivity _flutterWearOsConnectivity = FlutterWearOsConnectivity();
StreamSubscription<DataEvent>? _dataChangedStreamSubscription;
StreamSubscription<WearOSMessage>? _messagereceivedStreamSubscription;
StreamSubscription<CapabilityInfo>? capabilityChangedStreamSubscription;
StreamSubscription<Map<String, dynamic>>? _messageStreamSubscription;
StreamSubscription<Map<String, dynamic>>? _contextStreamSubscription;
final _watch = WatchConnectivity();

@Riverpod(keepAlive: true)
Future<void> initWear(InitWearRef ref) async {
await Future.delayed(const Duration(seconds: 5));
try {
if (!Platform.isAndroid || !await _flutterWearOsConnectivity.isSupported()) {
return;
}
_flutterWearOsConnectivity.configureWearableAPI();
_flutterWearOsConnectivity
.getConnectedDevices()
.asStream()
.expand(
(element) => element,
)
.listen((event) => _wearLogger.info("Connected Wear Device${event.name}, isNearby ${event.isNearby}"));

_dataChangedStreamSubscription = _flutterWearOsConnectivity.dataChanged().expand((element) => element).listen(
(dataEvent) {
_wearLogger.info("dataChanged ${dataEvent.type}, ${dataEvent.dataItem.mapData}");
if (!dataEvent.isDataValid || dataEvent.type != DataEventType.changed) {
return;
}
Map<String, dynamic> mapData = dataEvent.dataItem.mapData;
bool containsKey = mapData.containsKey("uuid");
if (containsKey) {
String uuid = mapData["uuid"];
BaseAction? action = ref.read(getActionFromUUIDProvider(uuid));
if (action != null) {
Iterable<BaseStatefulDevice> knownDevices = ref
.read(knownDevicesProvider)
.values
.where((element) => action.deviceCategory.contains(element.baseDeviceDefinition.deviceType))
.where((element) => element.deviceConnectionState.value == ConnectivityState.connected)
.where((element) => element.deviceState.value == DeviceState.standby);
for (BaseStatefulDevice device in knownDevices) {
runAction(action, device);
}
}
}
},
// Get the state of device connectivity
_messageStreamSubscription = _watch.messageStream.listen(
(event) => _wearLogger.info("Watch Message: $event"),
);
_contextStreamSubscription = _watch.contextStream.listen(
(event) => _wearLogger.info("Watch Context: $event"),
);
_messagereceivedStreamSubscription = _flutterWearOsConnectivity.messageReceived().listen(
(message) => _wearLogger.info("messageReceived $message"),
);
capabilityChangedStreamSubscription = _flutterWearOsConnectivity.capabilityChanged(capabilityPathURI: Uri(scheme: "wear", host: "*", path: "/*")).listen(
(event) => _wearLogger.info(
"capabilityChanged $event",
),
);

updateWearActions(ref.read(favoriteActionsProvider), ref);
} catch (e, s) {
_wearLogger.severe("exception setting up Wear $e", e, s);
}
}

Future<bool> isReachable() {
return _watch.isReachable;
}

Future<bool> isSupported() {
return _watch.isSupported;
}

Future<bool> isPaired() {
return _watch.isPaired;
}

Future<Map<String, dynamic>> applicationContext() {
return _watch.applicationContext;
}

Future<void> updateWearActions(BuiltList<FavoriteAction> favoriteActions, Ref ref) async {
try {
Iterable<BaseAction> allActions = favoriteActions
@@ -94,15 +66,9 @@ Future<void> updateWearActions(BuiltList<FavoriteAction> favoriteActions, Ref re
MapEntry("uuid", favoriteMap.keys.join("_")),
],
);
String msgJson = const JsonEncoder().convert(map);
List<int> msgBytes = const Utf8Encoder().convert(msgJson);
List<WearOsDevice> connectedDevices = await _flutterWearOsConnectivity.getConnectedDevices();
for (WearOsDevice wearOsDevice in connectedDevices) {
await _flutterWearOsConnectivity.sendMessage(Uint8List.fromList(msgBytes), deviceId: wearOsDevice.id, path: "/actions");
if (await _watch.isReachable) {
await _watch.sendMessage(map);
}

DataItem? dataItem = await _flutterWearOsConnectivity.syncData(path: "/actions", data: map, isUrgent: true);
_wearLogger.info("Message Sent successfully? ${dataItem != null}");
} catch (e, s) {
_wearLogger.severe("Unable to send favorite actions to watch", e, s);
}
56 changes: 56 additions & 0 deletions lib/Frontend/pages/developer/developer_menu.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:install_referrer/install_referrer.dart';

import '../../../Backend/logging_wrappers.dart';
import '../../../Backend/wear_bridge.dart';
import '../../../constants.dart';
import '../../../main.dart';
import '../../go_router_config.dart';
@@ -117,6 +119,60 @@ class _DeveloperMenuState extends ConsumerState<DeveloperMenu> {
},
),
),
ListTile(
title: const Text("InstallReferrer"),
subtitle: FutureBuilder(
future: InstallReferrer.referrer,
builder: (context, snapshot) {
InstallationAppReferrer? value = snapshot.data;
String referral = value != null ? value.name : "unknown";
return Text(referral);
},
),
),
const ListTile(
title: Divider(),
),
ListTile(
title: const Text("WatchIsReachable"),
subtitle: FutureBuilder(
future: isReachable(),
builder: (context, snapshot) {
bool value = snapshot.data ?? false;
return Text(value.toString());
},
),
),
ListTile(
title: const Text("WatchIsSupported"),
subtitle: FutureBuilder(
future: isSupported(),
builder: (context, snapshot) {
bool value = snapshot.data ?? false;
return Text(value.toString());
},
),
),
ListTile(
title: const Text("WatchIsPaired"),
subtitle: FutureBuilder(
future: isPaired(),
builder: (context, snapshot) {
bool value = snapshot.data ?? false;
return Text(value.toString());
},
),
),
ListTile(
title: const Text("WatchApplicationContext"),
subtitle: FutureBuilder(
future: applicationContext(),
builder: (context, snapshot) {
Map<String, dynamic> value = snapshot.data ?? {};
return Text(value.toString());
},
),
),
],
),
);
45 changes: 22 additions & 23 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -546,10 +546,10 @@ packages:
dependency: "direct main"
description:
name: flutter_adaptive_scaffold
sha256: fe16ddead74e728c88aa33e3d0cef611a556b27194639f9e9ec4a0e32400d87e
sha256: "93d20f0a4fbded8755571cc1f11a05a116cdc401ebaa15293ea2f87618fa2d18"
url: "https://pub.dev"
source: hosted
version: "0.2.3"
version: "0.2.4"
flutter_android_volume_keydown:
dependency: "direct main"
description:
@@ -571,10 +571,10 @@ packages:
dependency: "direct main"
description:
name: flutter_foreground_task
sha256: "6e89319a66f6a022773e8c698527e9cd80a92d7508154b82fb89d33e0dd64068"
sha256: "4962ffefe4352435900eb25734925f1ad002abf48e13c1ca22c9c63391be4f94"
url: "https://pub.dev"
source: hosted
version: "8.6.0"
version: "8.7.0"
flutter_gen_core:
dependency: transitive
description:
@@ -700,14 +700,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "9.1.0"
flutter_smart_watch_platform_interface:
dependency: transitive
description:
name: flutter_smart_watch_platform_interface
sha256: "66e49a77764294a5d9816be548ffab33acac54bc51292d8d01e9e07455571a2b"
url: "https://pub.dev"
source: hosted
version: "0.0.2"
flutter_svg:
dependency: transitive
description:
@@ -721,15 +713,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_wear_os_connectivity:
dependency: "direct main"
description:
path: "."
ref: HEAD
resolved-ref: "892502783a8428c7e001b53c36d8777c2b240054"
url: "https://github.com/Codel1417/flutter_wear_os_connectivity"
source: git
version: "1.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
@@ -1765,10 +1748,10 @@ packages:
dependency: "direct main"
description:
name: uuid
sha256: "83d37c7ad7aaf9aa8e275490669535c8080377cfa7a7004c24dfac53afffaa90"
sha256: f33d6bb662f0e4f79dcd7ada2e6170f3b3a2530c28fc41f49a411ddedd576a77
url: "https://pub.dev"
source: hosted
version: "4.4.2"
version: "4.5.0"
vector_graphics:
dependency: transitive
description:
@@ -1841,6 +1824,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.1"
watch_connectivity:
dependency: "direct main"
description:
name: watch_connectivity
sha256: a4257a314601c8448662d1a91421321a2e8a3207937fec4792116f1270ea8766
url: "https://pub.dev"
source: hosted
version: "0.2.0"
watch_connectivity_platform_interface:
dependency: transitive
description:
name: watch_connectivity_platform_interface
sha256: "82e8f165eac779d71bff7f6269a8be530798311cf7f3c33f1594d93468747d11"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
watcher:
dependency: transitive
description:
10 changes: 4 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ dependencies:
vector_math: ^2.1.4 # used for joystick
collection: ^1.18.0 # Priority Queue
intl: #pinned to flutter version?
uuid: ^4.4.2 # Used to generate UUID v4 ids for custom actions & move lists
uuid: ^4.5.0 # Used to generate UUID v4 ids for custom actions & move lists
json_annotation: ^4.9.0
crypto: ^3.0.5 # used for md5 hash checking during ota download
circular_buffer: ^0.11.0 # Used for serial console
@@ -39,15 +39,13 @@ dependencies:
permission_handler: ^11.3.1
url_launcher: ^6.3.0 # Open URLS in external apps
flutter_blue_plus: ^1.32.12
flutter_foreground_task: ^8.6.0 # Keep the app running in the background on android
flutter_foreground_task: ^8.7.0 # Keep the app running in the background on android
install_referrer: # Needs gradle namespace
git:
url: https://github.com/undreeyyy/flutter_plugin_install_referrer
ref: fd87e9b8f0d5ed909e929388244456f72b9b63c7
quick_actions: ^1.0.7 # puts favorites on the home screen
flutter_wear_os_connectivity:
git:
url: https://github.com/Codel1417/flutter_wear_os_connectivity
watch_connectivity: ^0.2.0
audioplayers: ^6.1.0
firebase_testlab_detector: ^1.0.2
platform: ^3.1.5
@@ -60,7 +58,7 @@ dependencies:
flutter_screen_lock: ^9.1.0 # used to hide dev mode toggle
introduction_screen: ^3.1.14 # Onboarding
flex_color_picker: ^3.5.1
flutter_adaptive_scaffold: ^0.2.3
flutter_adaptive_scaffold: ^0.2.4
animate_do: ^3.3.4
fl_chart: ^0.69.0 # Used for the battery graph
chart_sparkline: ^1.1.1 # used for the move easing visual

0 comments on commit eee8883

Please sign in to comment.