{"record":{"id":"7163f79bd4c9b0ce","repo":"Tencent/matrix","slug":"illegal-battery-state-action","errorCode":null,"errorMessage":"Illegal battery state: <action>","messagePattern":"Illegal battery state: <action>","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-battery-canary/src/main/java/com/tencent/matrix/batterycanary/BatteryEventDelegate.java","lineNumber":428,"sourceCode":"    }\n\n    @VisibleForTesting\n    void dispatchBatteryStateChangedEvent(Intent intent) {\n        String action = intent.getAction();\n        if (Intent.ACTION_BATTERY_OKAY.equals(action) || Intent.ACTION_BATTERY_LOW.equals(action)) {\n            MatrixLog.i(TAG, \"onBatteryStateChanged >> \" + action);\n            synchronized (mListenerList) {\n                BatteryState batteryState = currentState();\n                for (Listener item : mListenerList) {\n                    if (item instanceof Listener.ExListener) {\n                        if (((Listener.ExListener) item).onBatteryStateChanged(batteryState, Intent.ACTION_BATTERY_LOW.equals(action))) {\n                            removeListener(item);\n                        }\n                    }\n                }\n            }\n        } else {\n            throw new IllegalStateException(\"Illegal battery state: \" + action);\n        }\n    }\n\n    @VisibleForTesting\n    void dispatchBatteryPowerChanged(int pct) {\n        MatrixLog.i(TAG, \"onBatteryPowerChanged >> \" + pct + \"%\");\n        synchronized (mListenerList) {\n            BatteryState batteryState = currentState();\n            for (Listener item : mListenerList) {\n                if (item instanceof Listener.ExListener) {\n                    if (((Listener.ExListener) item).onBatteryPowerChanged(batteryState, pct)) {\n                        removeListener(item);\n                    }\n                }\n            }\n        }\n    }\n","sourceCodeStart":410,"sourceCodeEnd":446,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-battery-canary/src/main/java/com/tencent/matrix/batterycanary/BatteryEventDelegate.java#L410-L446","documentation":"dispatchBatteryStateChangedEvent(action) throws IllegalStateException for any battery-state broadcast action it does not recognize. It only handles the known set of ACTION_BATTERY_CHANGED-derived events (charging/discharging/full/not-charging etc.); an unknown action string falls into the terminal else branch.","triggerScenarios":"Calling onBatteryChangeEvent(...) or the async run() path with a battery state action string outside the supported set (e.g. ACTION_BATTERY_LOW / ACTION_BATTERY_OKAY, or a vendor-patched action), which is then forwarded to dispatchBatteryStateChangedEvent.","commonSituations":"Forwarding raw BroadcastReceiver actions (BatteryManager.ACTION_BATTERY_LOW, ACTION_POWER_CONNECTED) directly into the delegate; OEM ROMs emitting custom battery actions; forwarding an event from a different Android version with changed action strings.","solutions":["Only dispatch actions produced by the delegate's own parsing layer; filter out unhandled actions before calling.","Add a whitelist check: if the action is not one of the supported BatteryState events, ignore it instead of dispatching.","Upgrade the Matrix battery-canary version if a new platform action must be supported."],"exampleFix":"// before\n@Override\npublic void onReceive(Context ctx, Intent i) {\n    delegate.dispatchBatteryStateChangedEvent(i.getAction()); // throws for LOW/OKAY\n}\n\n// after\n@Override\npublic void onReceive(Context ctx, Intent i) {\n    String action = i.getAction();\n    if (BatteryManager.ACTION_BATTERY_CHANGED.equals(action)) {\n        delegate.dispatchBatteryStateChangedEvent(action);\n    }\n}","handlingStrategy":"validation","validationCode":"private static final Set<String> SUPPORTED = new HashSet<>(Arrays.asList(\n    BatteryManager.ACTION_BATTERY_CHANGED /* + delegate-supported actions */));\n\nif (!SUPPORTED.contains(action)) return; // ignore unknown battery actions","typeGuard":null,"tryCatchPattern":"try {\n    delegate.dispatchBatteryStateChangedEvent(action);\n} catch (IllegalStateException e) {\n    MatrixLog.w(TAG, \"unsupported battery action: \" + action);\n}","preventionTips":["Only forward battery events parsed by the delegate's own listener plumbing, never raw broadcast actions.","Filter out ACTION_BATTERY_LOW/ACTION_BATTERY_OKAY and OEM-specific actions at the receiver.","Test on target OEM ROMs for non-standard battery actions."],"tags":["android","battery","invalid-argument","broadcast"],"backgroundTag":"invalid-enum-value","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}