""" Android Service QuickSettings 组件 — TileService, Tile 等 方法签名完全对齐 Android SDK (android.service.quicksettings 包) """ import time from typing import Any, Callable, Dict, List, Optional from .._core import Stub, AndroidOnlyError # ═══════════════════════════════════════════════════════════════ # Tile # ═══════════════════════════════════════════════════════════════ class Tile(Stub): """Tile — 快捷设置磁贴""" # 状态常量 STATE_ACTIVE = 1 STATE_INACTIVE = 2 STATE_UNAVAILABLE = 0 def __init__(self): super().__init__() self._state: int = Tile.STATE_INACTIVE self._label: str = "" self._subtitle: str = "" self._content_description: str = "" self._icon: Optional[Any] = None self._icon_res_id: int = 0 self._meta_data: Optional[Dict[str, Any]] = None self._last_updated: int = 0 def getState(self) -> int: """获取磁贴状态""" return self._state def setState(self, state: int): """设置磁贴状态""" self._state = state self._last_updated = int(time.time() * 1000) def getLabel(self) -> str: """获取标签""" return self._label def setLabel(self, label: str): """设置标签""" self._label = label self._last_updated = int(time.time() * 1000) def getSubtitle(self) -> str: """获取副标题""" return self._subtitle def setSubtitle(self, subtitle: str): """设置副标题""" self._subtitle = subtitle self._last_updated = int(time.time() * 1000) def getContentDescription(self) -> str: """获取内容描述""" return self._content_description def setContentDescription(self, content_description: str): """设置内容描述""" self._content_description = content_description def getIcon(self) -> Optional[Any]: """获取图标""" return self._icon def setIcon(self, icon: Any): """设置图标""" self._icon = icon self._last_updated = int(time.time() * 1000) def updateTile(self): """更新磁贴 (通知系统刷新)""" self._last_updated = int(time.time() * 1000) def getMetaData(self) -> Optional[Dict[str, Any]]: return self._meta_data def setMetaData(self, meta_data: Dict[str, Any]): self._meta_data = meta_data def toString(self) -> str: state_names = { Tile.STATE_ACTIVE: "ACTIVE", Tile.STATE_INACTIVE: "INACTIVE", Tile.STATE_UNAVAILABLE: "UNAVAILABLE", } return (f"Tile(label={self._label}, state=" f"{state_names.get(self._state, self._state)})") def hashCode(self) -> int: return hash((self._label, self._state)) & 0x7FFFFFFF def equals(self, other) -> bool: if not isinstance(other, Tile): return False return (self._label == other._label and self._state == other._state) # ═══════════════════════════════════════════════════════════════ # TileService # ═══════════════════════════════════════════════════════════════ class TileService(Stub): """TileService — 快捷设置磁贴服务""" # 动作 ACTION_QS_TILE = "android.service.quicksettings.action.QS_TILE" ACTION_REQUEST_LISTENING = ( "android.service.quicksettings.action.REQUEST_LISTENING" ) META_DATA_ACTIVE_TILE = "android.service.quicksettings.ACTIVE_TILE" def __init__(self): super().__init__() self._qs_tile: Optional[Tile] = None self._locked: bool = False self._secure: bool = False self._listening: bool = False self._tile_added: bool = False self._tile_removed: bool = False @staticmethod def requestListeningState(context: Any, component: Any): """请求监听状态 (Android-only)""" pass def isLocked(self) -> bool: """检查设备是否锁定""" return self._locked def isSecure(self) -> bool: """检查设备是否安全锁定""" return self._secure def startActivity(self, intent: Any): """启动 Activity (Android-only)""" raise AndroidOnlyError( "TileService.startActivity() 仅在 Android 设备上可用" ) def showDialog(self, dialog: Any): """显示对话框""" pass def getQsTile(self) -> Optional[Tile]: """获取快速设置磁贴""" return self._qs_tile def setQsTile(self, tile: Tile): self._qs_tile = tile def unlockAndRun(self, runnable: Callable): """解锁并运行""" runnable() def onTileAdded(self): """磁贴被添加时回调""" self._tile_added = True def onTileRemoved(self): """磁贴被移除时回调""" self._tile_removed = True def onStartListening(self): """开始监听时回调""" self._listening = True def onStopListening(self): """停止监听时回调""" self._listening = False def onClick(self): """被点击时回调""" if self._qs_tile: current_state = self._qs_tile.getState() if current_state == Tile.STATE_ACTIVE: self._qs_tile.setState(Tile.STATE_INACTIVE) elif current_state == Tile.STATE_INACTIVE: self._qs_tile.setState(Tile.STATE_ACTIVE) def onDestroy(self): """销毁时回调""" pass def isListening(self) -> bool: return self._listening def toString(self) -> str: return (f"TileService(locked={self._locked}, secure={self._secure}, " f"listening={self._listening})") def hashCode(self) -> int: return id(self) & 0x7FFFFFFF def equals(self, other) -> bool: return self is other # ═══════════════════════════════════════════════════════════════ # __all__ # ═══════════════════════════════════════════════════════════════ __all__ = [ "TileService", "Tile", ]