@@ -52,6 +52,18 @@ class AirPodsTrayApp : public QObject {
5252 Q_OBJECT
5353
5454public:
55+ enum NoiseControlMode : quint8
56+ {
57+ Off = 0 ,
58+ NoiseCancellation = 1 ,
59+ Transparency = 2 ,
60+ Adaptive = 3 ,
61+
62+ MinValue = Off,
63+ MaxValue = Adaptive,
64+ };
65+ Q_ENUM (NoiseControlMode)
66+
5567 AirPodsTrayApp (bool debugMode) : debugMode(debugMode) {
5668 if (debugMode) {
5769 QLoggingCategory::setFilterRules (" airpodsApp.debug=true" );
@@ -79,6 +91,11 @@ class AirPodsTrayApp : public QObject {
7991 QAction *adaptiveAction = new QAction (" Adaptive" , trayMenu);
8092 QAction *noiseCancellationAction = new QAction (" Noise Cancellation" , trayMenu);
8193
94+ offAction->setData (NoiseControlMode::Off);
95+ transparencyAction->setData (NoiseControlMode::Transparency);
96+ adaptiveAction->setData (NoiseControlMode::Adaptive);
97+ noiseCancellationAction->setData (NoiseControlMode::NoiseCancellation);
98+
8299 offAction->setCheckable (true );
83100 transparencyAction->setCheckable (true );
84101 adaptiveAction->setCheckable (true );
@@ -95,10 +112,14 @@ class AirPodsTrayApp : public QObject {
95112 noiseControlGroup->addAction (adaptiveAction);
96113 noiseControlGroup->addAction (noiseCancellationAction);
97114
98- connect (offAction, &QAction::triggered, this , [this ]() { setNoiseControlMode (0 ); });
99- connect (transparencyAction, &QAction::triggered, this , [this ]() { setNoiseControlMode (2 ); });
100- connect (adaptiveAction, &QAction::triggered, this , [this ]() { setNoiseControlMode (3 ); });
101- connect (noiseCancellationAction, &QAction::triggered, this , [this ]() { setNoiseControlMode (1 ); });
115+ connect (offAction, &QAction::triggered, this , [this ]()
116+ { setNoiseControlMode (NoiseControlMode::Off); });
117+ connect (transparencyAction, &QAction::triggered, this , [this ]()
118+ { setNoiseControlMode (NoiseControlMode::Transparency); });
119+ connect (adaptiveAction, &QAction::triggered, this , [this ]()
120+ { setNoiseControlMode (NoiseControlMode::Adaptive); });
121+ connect (noiseCancellationAction, &QAction::triggered, this , [this ]()
122+ { setNoiseControlMode (NoiseControlMode::NoiseCancellation); });
102123
103124 connect (this , &AirPodsTrayApp::noiseControlModeChanged, this , &AirPodsTrayApp::updateNoiseControlMenu);
104125 connect (this , &AirPodsTrayApp::batteryStatusChanged, this , &AirPodsTrayApp::updateBatteryTooltip);
@@ -274,20 +295,20 @@ public slots:
274295 }
275296 }
276297
277- void setNoiseControlMode (int mode) {
298+ void setNoiseControlMode (NoiseControlMode mode) {
278299 LOG_INFO (" Setting noise control mode to: " << mode);
279300 QByteArray packet;
280301 switch (mode) {
281- case 0 :
302+ case Off :
282303 packet = QByteArray::fromHex (" 0400040009000D01000000" );
283304 break ;
284- case 1 :
305+ case NoiseCancellation :
285306 packet = QByteArray::fromHex (" 0400040009000D02000000" );
286307 break ;
287- case 2 :
308+ case Transparency :
288309 packet = QByteArray::fromHex (" 0400040009000D03000000" );
289310 break ;
290- case 3 :
311+ case Adaptive :
291312 packet = QByteArray::fromHex (" 0400040009000D04000000" );
292313 break ;
293314 }
@@ -310,24 +331,10 @@ public slots:
310331 }
311332 }
312333
313- void updateNoiseControlMenu (int mode) {
334+ void updateNoiseControlMenu (NoiseControlMode mode) {
314335 QList<QAction *> actions = trayMenu->actions ();
315336 for (QAction *action : actions) {
316- action->setChecked (false );
317- }
318- switch (mode) {
319- case 0 :
320- actions[0 ]->setChecked (true );
321- break ;
322- case 1 :
323- actions[3 ]->setChecked (true );
324- break ;
325- case 2 :
326- actions[1 ]->setChecked (true );
327- break ;
328- case 3 :
329- actions[2 ]->setChecked (true );
330- break ;
337+ action->setChecked (action->data ().toInt () == mode);
331338 }
332339 }
333340
@@ -591,12 +598,16 @@ public slots:
591598 void parseData (const QByteArray &data) {
592599 LOG_DEBUG (" Received: " << data.toHex ());
593600 if (data.size () == 11 && data.startsWith (QByteArray::fromHex (" 0400040009000D" ))) {
594- int mode = data[7 ] - 1 ;
595- LOG_INFO (" Noise control mode: " << mode);
596- if (mode >= 0 && mode <= 3 ) {
601+ quint8 rawMode = data[7 ] - 1 ;
602+ if (rawMode >= NoiseControlMode::MinValue && rawMode <= NoiseControlMode::MaxValue)
603+ {
604+ NoiseControlMode mode = static_cast <NoiseControlMode>(rawMode);
605+ LOG_INFO (" Noise control mode: " << rawMode);
597606 emit noiseControlModeChanged (mode);
598- } else {
599- LOG_ERROR (" Invalid noise control mode value received: " << mode);
607+ }
608+ else
609+ {
610+ LOG_ERROR (" Invalid noise control mode value received: " << rawMode);
600611 }
601612 } else if (data.size () == 8 && data.startsWith (QByteArray::fromHex (" 040004000600" ))) {
602613 char primary = data[6 ];
@@ -887,7 +898,7 @@ public slots:
887898 }
888899
889900signals:
890- void noiseControlModeChanged (int mode);
901+ void noiseControlModeChanged (NoiseControlMode mode);
891902 void earDetectionStatusChanged (const QString &status);
892903 void batteryStatusChanged (const QString &status);
893904
0 commit comments