Skip to content

Commit

Permalink
Merge branch 'wear'
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Sep 2, 2024
2 parents a7ed1d2 + eee8883 commit 7160e75
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 86 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
Expand All @@ -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);
}
Expand Down
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';
Expand Down Expand Up @@ -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());
},
),
),
],
),
);
Expand Down
37 changes: 18 additions & 19 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,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:
Expand Down Expand Up @@ -716,14 +716,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:
Expand All @@ -737,15 +729,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
Expand Down Expand Up @@ -1865,6 +1848,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:
Expand Down
6 changes: 2 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7160e75

Please sign in to comment.