Skip to content

Commit

Permalink
add base con mode support (untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Dec 1, 2024
1 parent 3ac1835 commit cb7d643
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Backend/Bluetooth/bluetooth_manager_plus.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:built_collection/built_collection.dart';
import 'package:collection/collection.dart';
Expand Down Expand Up @@ -90,6 +91,8 @@ Future<void> initFlutterBluePlus(InitFlutterBluePlusRef ref) async {
//transaction.setTag('Known Device', 'Yes');
} else {
baseStoredDevice = BaseStoredDevice(deviceDefinition.uuid, deviceID, deviceDefinition.deviceType.color(ref: ref).value)..name = getNameFromBTName(deviceDefinition.btName);
int code = Random().nextInt(899999) + 100000;
baseStoredDevice.conModePin = code.toString();
statefulDevice = BaseStatefulDevice(deviceDefinition, baseStoredDevice);
//transaction.setTag('Known Device', 'No');
Future(() => ref.read(knownDevicesProvider.notifier).add(statefulDevice));
Expand Down
2 changes: 2 additions & 0 deletions lib/Backend/Definitions/Device/device_definition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ class BaseStoredDevice extends ChangeNotifier {
int rightHomePosition = 1;
@HiveField(12, defaultValue: "")
String conModePin = "";
@HiveField(13, defaultValue: false)
bool conModeEnabled = false;

int get color => _color;

Expand Down
44 changes: 44 additions & 0 deletions lib/Frontend/Widgets/manage_gear.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flex_color_picker/flex_color_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:tail_app/Backend/move_lists.dart';
import 'package:tail_app/Frontend/Widgets/tutorial_card.dart';

import '../../Backend/Bluetooth/bluetooth_manager.dart';
import '../../Backend/Bluetooth/bluetooth_manager_plus.dart';
Expand Down Expand Up @@ -185,6 +186,7 @@ class _ManageGearState extends ConsumerState<ManageGear> {
ManageGearHomePosition(device: device!),
],
ManageGearBatteryGraph(device: device!),
ManageGearConventionMode(device: device!),
ManageGearDebug(device: device!),
],
OverflowBar(
Expand Down Expand Up @@ -247,6 +249,48 @@ class _ManageGearState extends ConsumerState<ManageGear> {
}
}

class ManageGearConventionMode extends ConsumerWidget {
final BaseStatefulDevice device;

const ManageGearConventionMode({super.key, required this.device});

@override
Widget build(BuildContext context, WidgetRef ref) {
return ExpansionTile(
title: Text(manageGearConModeTitle()),
subtitle: Text(manageGearConModeDescription()),
children: [
PageInfoCard(
text: "Super secret anti hacker power (Insert guide and reset instructions here)",
),
ListTile(
title: Text(manageGearConModeToggleTitle()),
subtitle: Text("Upon enabling 'Convention Mode' your gear will be rebooted and you will be prompted to enter the pincode. Please view and memorize the pincode before enabling"),
trailing: Switch(
value: device.baseStoredDevice.conModeEnabled,
onChanged: (value) {
//reject if gear disconnected
if (value) {
BluetoothMessage bluetoothMessage = BluetoothMessage(message: "SETPUSSKEY ${device.baseStoredDevice.conModePin}", device: device, timestamp: DateTime.timestamp());
device.commandQueue.addCommand(bluetoothMessage);
device.baseStoredDevice.conModeEnabled = true;
ref.read(knownDevicesProvider.notifier).store();
} else {
BluetoothMessage bluetoothMessage = BluetoothMessage(message: "STOPPUSSKEY", device: device, timestamp: DateTime.timestamp());
device.commandQueue.addCommand(bluetoothMessage);
device.baseStoredDevice.conModeEnabled = false;
ref.read(knownDevicesProvider.notifier).store();
}
}),
),
OverflowBar(
children: [FilledButton(onPressed: () => PinCodeRoute(pin: device.baseStoredDevice.conModePin).push(context), child: Text(manageGearConModePincodeTitle()))],
)
],
);
}
}

class ManageGearBatteryGraph extends StatelessWidget {
final BaseStatefulDevice device;

Expand Down
31 changes: 31 additions & 0 deletions lib/Frontend/Widgets/pincode_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import '../translation_string_definitions.dart';

class PincodeDialog extends StatelessWidget {
const PincodeDialog({super.key, required this.pin});

final String pin;

@override
Widget build(BuildContext context) {
return AlertDialog(
actionsAlignment: MainAxisAlignment.center,
actions: [TextButton(onPressed: () => context.pop(), child: Text(ok()))],
title: Text(manageGearConModePincodeTitle()),
content: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Center(
child: Text(
pin,
style: Theme.of(context).textTheme.titleLarge,
),
),
],
),
);
}
}
4 changes: 4 additions & 0 deletions lib/Frontend/Widgets/scan_for_new_device.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:animate_do/animate_do.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
Expand Down Expand Up @@ -198,6 +200,8 @@ class _ScanForNewDevice extends ConsumerState<ScanForNewDevice> {
baseStoredDevice = BaseStoredDevice(value.uuid, "DEV${value.deviceType.name}", value.deviceType.color(ref: ref).value)..name = getNameFromBTName(value.btName);
statefulDevice = BaseStatefulDevice(value, baseStoredDevice);
statefulDevice.deviceConnectionState.value = ConnectivityState.connected;
int code = Random().nextInt(899999) + 100000;
baseStoredDevice.conModePin = code.toString();
if (!ref.read(knownDevicesProvider).containsKey(baseStoredDevice.btMACAddress)) {
ref.read(knownDevicesProvider.notifier).add(statefulDevice);
}
Expand Down
19 changes: 19 additions & 0 deletions lib/Frontend/go_router_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import '../Backend/plausible_dio.dart';
import '../constants.dart';
import 'Widgets/color_picker_dialog.dart';
import 'Widgets/manage_gear.dart';
import 'Widgets/pincode_dialog.dart';
import 'Widgets/scan_for_new_device.dart';
import 'pages/action_selector.dart';
import 'pages/actions.dart';
Expand Down Expand Up @@ -121,6 +122,24 @@ class ColorPickerRoute extends GoRouteData {
);
}

@TypedGoRoute<PinCodeRoute>(
path: '/pincode',
name: 'Pin Code',
)
class PinCodeRoute extends GoRouteData {
const PinCodeRoute({required this.pin});

final String pin;
static final GlobalKey<NavigatorState> $navigatorKey = rootNavigatorKey;

@override
Page<void> buildPage(BuildContext context, GoRouterState state) => DialogPage(
key: state.pageKey,
name: state.name,
child: PincodeDialog(pin: pin),
);
}

@TypedGoRoute<ScanForGearRoute>(
path: '/scan',
name: 'Scan for new devices',
Expand Down
8 changes: 8 additions & 0 deletions lib/Frontend/translation_string_definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,11 @@ String settingsTailBlogWifiOnlyDescription() => Intl.message("Prevent loading Ta
String supportTitle() => Intl.message('Support', name: 'supportTitle', desc: 'The label and title of the email support link on the more page');

String supportDescription() => Intl.message('Tap the blue chat icon to contact support', name: 'supportDescription', desc: 'The description of the email support link on the more page');

String manageGearConModeTitle() => Intl.message('Convention Mode', name: 'manageGearConModeTitle', desc: 'The title for the convention mode page on the gear page');

String manageGearConModeDescription() => Intl.message('Convention Mode is an additional security measure to prevent other devices from connecting to your gear.', name: 'manageGearConModeDescription', desc: 'The description for the convention mode page on the gear page');

String manageGearConModePincodeTitle() => Intl.message('View Pin Code', name: 'manageGearConModePincodeTitle', desc: 'The description for the pin mode button on the gear page');

String manageGearConModeToggleTitle() => Intl.message('Enable Convention Mode', name: 'manageGearConModeToggleTitle', desc: 'The description for the convention mode enabled button on the gear page');

0 comments on commit cb7d643

Please sign in to comment.