Skip to content

fix(bluetooth): prevent toggling bluetooth power state in airplane mode#436

Merged
robertkill merged 1 commit intolinuxdeepin:masterfrom
robertkill:master
Mar 9, 2026
Merged

fix(bluetooth): prevent toggling bluetooth power state in airplane mode#436
robertkill merged 1 commit intolinuxdeepin:masterfrom
robertkill:master

Conversation

@robertkill
Copy link
Contributor

@robertkill robertkill commented Mar 9, 2026

Add a check in BluetoothItem::invokeMenuItem to block shifting the bluetooth power state if airplane mode is currently enabled. This prevents the background daemon from persisting the power-on state while the hardware/software rfkill is active, which causes bluetooth to be enabled unexpectedly after a relogin.

Log: Prevent toggling bluetooth power state in airplane mode

Influence:

  1. Open the quick panel and enable airplane mode
  2. Click the bluetooth toggle switch
  3. Log out and log back in
  4. Verify the bluetooth switch remains disabled in the quick panel

fix(bluetooth): 修复在飞行模式下仍能切换蓝牙电源状态的问题

在 BluetoothItem::invokeMenuItem 中添加检查,如果当前已开启
飞行模式,则拒绝切换蓝牙电源状态的执行。这防止了在 rfkill
锁定期间,后台服务错误地将开启状态持久化配置,从而导致用户注销
重登后蓝牙被意外开启。

Log: 修复在飞行模式下仍能切换蓝牙电源状态的问题

Influence:

  1. 打开快捷面板并开启飞行模式
  2. 点击蓝牙开关图标进行切换
  3. 注销并重新登录系统
  4. 验证快捷面板中蓝牙开关依然保持关闭状态

PMS: BUG-352161

Summary by Sourcery

Bug Fixes:

  • Prevent Bluetooth from being turned on via the dock toggle while airplane mode (rfkill) is active, avoiding unexpected re-enabling after relogin.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 9, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds an airplane-mode guard in the Bluetooth quick panel item so that the Bluetooth adapter power state cannot be toggled or persisted while airplane mode is enabled, preventing Bluetooth from turning on unexpectedly after relogin.

Sequence diagram for blocking Bluetooth toggle while airplane mode is enabled

sequenceDiagram
    actor User
    participant QuickPanel
    participant BluetoothItem
    participant Applet

    User->>QuickPanel: click bluetooth toggle
    QuickPanel->>BluetoothItem: invokeMenuItem(SHIFT, checked)
    BluetoothItem->>Applet: airplaneModeEnable()
    Applet-->>BluetoothItem: true
    BluetoothItem-->>QuickPanel: return (no action)

    %% No call to setAdapterPowered when airplane mode is enabled
Loading

Updated class diagram for BluetoothItem and related applet behavior

classDiagram
    class BluetoothItem {
        - bool m_adapterPowered
        - Applet* m_applet
        + void invokeMenuItem(QString menuId, bool checked)
    }

    class Applet {
        + bool airplaneModeEnable()
        + void setAdapterPowered(bool powered)
    }

    BluetoothItem --> Applet : uses

    BluetoothItem : invokeMenuItem()
    BluetoothItem : checks Applet.airplaneModeEnable()
    BluetoothItem : calls Applet.setAdapterPowered(!m_adapterPowered) when airplane mode is disabled
Loading

File-Level Changes

Change Details Files
Block Bluetooth power toggling from the dock item when airplane mode is enabled to avoid persisting an incorrect powered-on state.
  • Add an early-return guard in BluetoothItem::invokeMenuItem when the SHIFT action is invoked and airplane mode is currently enabled
  • Use the existing m_applet->airplaneModeEnable() status to decide whether to allow changing the adapter powered state
  • Leave the existing behavior for opening Bluetooth settings and for toggling power when airplane mode is off unchanged
plugins/dde-dock/bluetooth/bluetoothitem.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • When returning early due to airplane mode being enabled, consider providing some user feedback (e.g., temporary notification or visual state reset) so the user understands why the Bluetooth toggle had no effect.
  • If other code paths can change adapter power state, consider centralizing the airplane-mode guard closer to the underlying setAdapterPowered logic to avoid future callers bypassing this check.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When returning early due to airplane mode being enabled, consider providing some user feedback (e.g., temporary notification or visual state reset) so the user understands why the Bluetooth toggle had no effect.
- If other code paths can change adapter power state, consider centralizing the airplane-mode guard closer to the underlying `setAdapterPowered` logic to avoid future callers bypassing this check.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Add a check in BluetoothItem::invokeMenuItem to block shifting
the bluetooth power state if airplane mode is currently enabled.
This prevents the background daemon from persisting the power-on
state while the hardware/software rfkill is active, which causes
bluetooth to be enabled unexpectedly after a relogin.

Log: Prevent toggling bluetooth power state in airplane mode

Influence:
1. Open the quick panel and enable airplane mode
2. Click the bluetooth toggle switch
3. Log out and log back in
4. Verify the bluetooth switch remains disabled in the quick panel

fix(bluetooth): 修复在飞行模式下仍能切换蓝牙电源状态的问题

在 BluetoothItem::invokeMenuItem 中添加检查,如果当前已开启
飞行模式,则拒绝切换蓝牙电源状态的执行。这防止了在 rfkill
锁定期间,后台服务错误地将开启状态持久化配置,从而导致用户注销
重登后蓝牙被意外开启。

Log: 修复在飞行模式下仍能切换蓝牙电源状态的问题

Influence:
1. 打开快捷面板并开启飞行模式
2. 点击蓝牙开关图标进行切换
3. 注销并重新登录系统
4. 验证快捷面板中蓝牙开关依然保持关闭状态

PMS: BUG-352161
BLumia
BLumia previously approved these changes Mar 9, 2026
@deepin-ci-robot
Copy link

deepin pr auto review

针对这段代码的 diff,我将从语法逻辑、代码质量、代码性能和代码安全四个方面进行审查,并提出改进建议。

1. 语法逻辑

  • 版权年份更新
    • SPDX-FileCopyrightText 从 2016 - 2022 更新到了 2016 - 2026。
    • 审查意见:虽然语法上没有错误,但通常版权年份应更新为当前年份或包含当前年份的范围(例如 2023 或 2024)。直接更新到 2026 年可能是一种预发布策略,但在实际开发中,建议保持年份的准确性,或者使用动态年份(如果构建系统支持),或者每年手动更新一次。如果是内部项目且已达成共识,则可以接受。

2. 代码质量

  • 功能逻辑

    • invokeMenuItem 函数中,当 menuIdSHIFT(切换开关)时,增加了对 airplaneModeEnable() 的检查。
    • 审查意见:这是一个很好的改进。在飞行模式下,蓝牙通常应该被禁用且不应允许用户通过点击菜单项来开启。这段代码有效地防止了在飞行模式下切换蓝牙电源状态,符合用户预期的逻辑行为。
  • 代码可读性

    • 代码使用了 if (m_applet->airplaneModeEnable()) { return; }
    • 审查意见:这种“卫语句”风格很好,避免了深层嵌套,提高了代码的可读性。

3. 代码性能

  • 函数调用开销
    • 调用了 m_applet->airplaneModeEnable()
    • 审查意见:这是一个函数调用,如果 m_applet 是一个指针且该函数是内联的或非常简单(例如直接返回成员变量),性能开销可以忽略不计。如果该函数涉及 DBus 调用或复杂的计算,可能会引入微小的延迟。考虑到这是 UI 交互(点击菜单),这种延迟通常是可以接受的。建议确保 airplaneModeEnable() 的实现是高效的。

4. 代码安全

  • 空指针检查
    • 代码直接调用了 m_applet->airplaneModeEnable()
    • 审查意见存在潜在风险。代码中没有显式检查 m_applet 是否为 nullptr。如果 m_applet 在某些边缘情况下(例如初始化未完成或已被释放)为空,这里会导致程序崩溃。
    • 改进建议:建议增加空指针检查,或者在类的设计上确保 m_appletBluetoothItem 的生命周期内始终有效(例如使用引用代替指针,或确保在析构时置空)。

综合改进建议代码

void BluetoothItem::invokeMenuItem(const QString menuId, const bool checked)
{
    Q_UNUSED(checked);

    // 安全性改进:增加空指针检查
    if (!m_applet) {
        qWarning() << "BluetoothItem::invokeMenuItem: m_applet is null!";
        return;
    }

    if (menuId == SHIFT) {
        // 性能与逻辑优化:在飞行模式下直接返回,避免不必要的操作
        // 假设 airplaneModeEnable() 是一个轻量级的 getter
        if (m_applet->airplaneModeEnable()) {
            return;
        }
        m_applet->setAdapterPowered(!m_adapterPowered);
    } else if (menuId == SETTINGS) {
        // ...

总结

这段代码的主要目的是在飞行模式下禁用蓝牙切换功能,逻辑上是正确的。主要的改进点在于:

  1. 版权年份:建议核实是否需要更新到 2026。
  2. 空指针安全:强烈建议增加对 m_applet 的空指针检查,以增强代码的健壮性。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: BLumia, robertkill

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@robertkill robertkill merged commit 27dad2e into linuxdeepin:master Mar 9, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants