Miracast Enabler v2.0.0-rc1: LSPosed module rewrite

Complete rewrite from KernelSU shell scripts to LSPosed module using
modern libxposed API. Hooks Android's hidden Wi-Fi Display framework
to enable native Miracast support on Android 12+ devices.

- Framework resource hooks (config_enableWifiDisplay)
- WifiDisplayStatus feature state override
- System feature and permission injection
- Settings Cast UI integration
- Quick Settings tile
- Settings activity with device-specific options
This commit is contained in:
sssnake
2026-04-03 07:29:54 -07:00
parent b1e4ffd828
commit df295abc97
13 changed files with 399 additions and 306 deletions

104
README.md Normal file
View File

@@ -0,0 +1,104 @@
# Miracast Enabler
An LSPosed/Xposed module that unlocks native Miracast (Wi-Fi Display) on Android 12+ devices where it has been hidden by the OEM.
Android still ships the full Miracast/WFD protocol stack in the framework — `WifiDisplayAdapter`, `WifiDisplayController`, `RemoteDisplay`, and all supporting classes. OEMs simply disable it via a config flag and hide it behind the Chromecast-only Cast UI. This module hooks the framework to re-enable it, giving you native Miracast with zero custom streaming code.
## Features
- **Native Wi-Fi Display** — uses Android's built-in Miracast stack, not a custom implementation
- **Quick Settings tile** — scan for and connect to Miracast receivers from the notification shade
- **Framework hooks** — forces `config_enableWifiDisplay` and `config_wifiDisplaySupportsProtectedBuffers` to true at the resource level
- **Cast UI integration** — unhides WFD routes in Settings > Cast so Miracast sinks appear alongside Chromecast devices
- **Permission grants** — automatically grants `CONFIGURE_WIFI_DISPLAY` and `CONTROL_WIFI_DISPLAY` to the module
- **Feature injection** — forces `android.software.wifi_display` system feature to report as available
- **Foldable support** — display source selector for Pixel Fold, Pixel 9 Pro Fold, and Pixel 10 Pro Fold
- **Settings app** — configure resolution, HDCP, GPU composition, Wi-Fi concurrency
## Requirements
- Android 12+ (API 31+)
- LSPosed or LSPosed-IT installed and active
- Root (KernelSU, Magisk, or APatch)
- Wi-Fi Direct capable hardware (most modern phones)
## Installation
1. Install the APK
2. Open LSPosed manager
3. Enable "Miracast Enabler" module
4. Set scope to: `System Framework`, `System UI`, `Settings`
5. Reboot
6. Add the Miracast tile to Quick Settings, or go to Settings > Connected devices > Cast
## Supported Devices
Primarily tested on Pixel devices with Tensor SoCs:
| Device | Codename | SoC | Status |
|--------|----------|-----|--------|
| Pixel 10 Pro Fold | rango | Tensor G5 | Primary target |
| Pixel 9 Pro Fold | comet/cometl | Tensor G4 | Supported |
| Pixel Fold | felix | Tensor G2 | Supported |
| Pixel 9 series | caiman/komodo/tokay/blazer | Tensor G4 | Supported |
| Pixel 10 series | laguna platform | Tensor G5 | Supported |
| Other Android 12+ | — | — | Should work if WFD classes exist in framework |
## Configuration
Open the Miracast Enabler app to access settings:
| Setting | Description | Default |
|---------|-------------|---------|
| Resolution | Max output resolution (auto, 720p, 1080p @ 30/60fps) | 1080p 30fps |
| HDCP | Content protection (disable for better compatibility) | Off |
| Display source | Inner/outer display on foldables | Inner |
| GPU composition | Fix black/green screen on Tensor devices | On |
| Single-channel concurrency | Prevent Wi-Fi latency spikes during cast | Off |
## How It Works
The module hooks into four processes:
1. **System Server (`android`)** — hooks `Resources.getBoolean()` to force Wi-Fi Display config flags true, hooks `hasSystemFeature()` to report WFD support, hooks `WifiDisplayStatus.getFeatureState()` to return `FEATURE_STATE_ON`, and grants WFD permissions to the module package.
2. **Settings (`com.android.settings`)** — hooks `WifiDisplayStatus.getFeatureState()` and `WifiDisplayPreferenceController.getAvailabilityStatus()` so the Wi-Fi Display section appears in Cast preferences.
3. **SystemUI (`com.android.systemui`)** — provides the Quick Settings tile for scanning and connecting.
4. **App process** — the settings activity reads/writes `SharedPreferences` which the hooks consume via `XSharedPreferences`.
## Building
```bash
# Requires Android SDK with API 34 and build-tools 34.0.0
echo "sdk.dir=/path/to/android-sdk" > local.properties
./gradlew assembleRelease
```
Output APK: `app/build/outputs/apk/release/app-release.apk`
## Troubleshooting
**Miracast option doesn't appear after reboot**
- Verify the module is enabled in LSPosed with correct scope
- Check LSPosed logs for "MiracastEnabler" entries
- Some ROMs may need a second reboot after first activation
**Black or green screen during cast**
- Enable "Force GPU composition" in module settings
- This is a known issue on Tensor SoCs where the HWC doesn't handle virtual display layers
**Connection drops or high latency**
- Enable "Single-channel concurrency" to prevent the Wi-Fi chip from splitting STA and P2P across bands
- Move closer to the Miracast receiver
- Ensure no heavy Wi-Fi traffic on the same network
**No Miracast receivers found**
- Ensure the receiver is in pairing/discovery mode
- Check that Wi-Fi Direct is not disabled by MDM or device policy
- Verify Wi-Fi is on and connected
## License
MIT

55
RELEASE.md Normal file
View File

@@ -0,0 +1,55 @@
# Miracast Enabler v2.0.0
**Native Miracast support for Android 12+ via LSPosed**
---
## What's New
Complete rewrite as an LSPosed/Xposed module. Instead of trying to set system properties after boot (which never worked reliably), the module now hooks directly into Android's framework to re-enable the hidden Wi-Fi Display (Miracast) stack.
### Highlights
- **It actually works now.** The v1.x KernelSU approach of setting properties and fabricating overlays couldn't overcome the framework-level disabling. This version hooks the right classes at the right time.
- **Quick Settings tile** — tap to open Cast settings where Miracast receivers now appear. Tap again to disconnect an active session.
- **Zero custom streaming** — uses Android's own `WifiDisplayAdapter`, `WifiDisplayController`, and `RemoteDisplay` classes. The protocol stack was always there, just hidden.
- **Settings app** — resolution caps, HDCP toggle, foldable display source, Tensor-specific GPU and Wi-Fi fixes.
### Framework Hooks
| Hook | Target | Purpose |
|------|--------|---------|
| `Resources.getBoolean()` | All processes (zygote) | Force `config_enableWifiDisplay = true` |
| `WifiDisplayStatus.getFeatureState()` | system_server, Settings | Return `FEATURE_STATE_ON` |
| `hasSystemFeature()` | system_server | Report `android.software.wifi_display` as available |
| `WifiDisplayPreferenceController` | Settings | Make WFD section visible in Cast preferences |
| Permission check | system_server | Grant `CONFIGURE_WIFI_DISPLAY` / `CONTROL_WIFI_DISPLAY` |
### Device Support
Optimized for Pixel phones with Tensor SoCs (Pixel 6 through Pixel 10 series, including all Fold variants). Should work on any Android 12+ device where the WFD framework classes still exist — which is most devices, since Google hasn't actually removed the code.
### Requirements
- Android 12+ (API 31+)
- LSPosed or LSPosed-IT
- Root via KernelSU, Magisk, or APatch
## Installation
1. Download and install `miracast-enabler-v2.0.0.apk`
2. Open LSPosed → Modules → enable Miracast Enabler
3. Set scope: System Framework, System UI, Settings
4. Reboot
5. Open Settings → Connected devices → Cast, or use the Quick Settings tile
## Known Issues
- First scan after boot may take 10-15 seconds for receivers to appear
- Tensor G4/G5: enable "Force GPU composition" if you see black/green frames
- Some Miracast receivers require HDCP — toggle it on in settings if connection fails with a specific receiver
- Google Home app Cast screen still filters WFD routes (use system Settings instead)
---
**Full changelog:** v1.0.0 (KernelSU shell scripts) → v2.0.0 (LSPosed framework hooks)

View File

@@ -27,7 +27,7 @@ android {
} }
dependencies { dependencies {
compileOnly("de.robv.android.xposed:api:82") compileOnly("io.github.libxposed:api:101.0.1")
implementation("androidx.preference:preference:1.2.1") implementation("androidx.preference:preference:1.2.1")
implementation("androidx.appcompat:appcompat:1.6.1") implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0") implementation("com.google.android.material:material:1.11.0")

View File

@@ -2,30 +2,22 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application <application
android:label="Miracast Enabler" android:label="@string/app_name"
android:description="@string/app_description"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar" android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar"
android:supportsRtl="true"> android:supportsRtl="true">
<!-- LSPosed module metadata --> <!-- LSPosed modern API: module metadata via META-INF/xposed/ files -->
<meta-data <meta-data
android:name="xposedmodule" android:name="xposedmodule"
android:value="true" /> android:value="true" />
<meta-data
android:name="xposedminversion"
android:value="82" />
<meta-data
android:name="xposeddescription"
android:value="Enables native Miracast (Wi-Fi Display) on Android devices" />
<meta-data
android:name="xposedscope"
android:resource="@array/xposed_scope" />
<!-- Settings launcher --> <!-- Settings launcher -->
<activity <activity
android:name=".ui.SettingsActivity" android:name=".ui.SettingsActivity"
android:exported="true" android:exported="true"
android:label="Miracast Enabler"> android:label="@string/app_name">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
@@ -36,7 +28,7 @@
<service <service
android:name=".tile.MiracastTileService" android:name=".tile.MiracastTileService"
android:icon="@drawable/ic_cast" android:icon="@drawable/ic_cast"
android:label="Miracast" android:label="@string/tile_label"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE" android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>

View File

@@ -1,37 +1,78 @@
package com.miracast.enabler; package com.miracast.enabler;
import de.robv.android.xposed.IXposedHookLoadPackage; import android.content.res.Resources;
import de.robv.android.xposed.IXposedHookZygoteInit;
import de.robv.android.xposed.XposedBridge; import io.github.libxposed.api.XposedInterface;
import de.robv.android.xposed.callbacks.XC_LoadPackage; import io.github.libxposed.api.XposedModule;
import io.github.libxposed.api.XposedModuleInterface;
import com.miracast.enabler.hooks.FrameworkResourceHook;
import com.miracast.enabler.hooks.DisplayManagerHook; import com.miracast.enabler.hooks.DisplayManagerHook;
import com.miracast.enabler.hooks.SystemServerHook; import com.miracast.enabler.hooks.SystemServerHook;
import com.miracast.enabler.hooks.MediaRouterHook; import com.miracast.enabler.hooks.MediaRouterHook;
public class MainHook implements IXposedHookLoadPackage, IXposedHookZygoteInit { public class MainHook extends XposedModule {
private static final String TAG = "MiracastEnabler"; private static final String TAG = "MiracastEnabler";
@Override public MainHook() {
public void initZygote(StartupParam startupParam) { super();
XposedBridge.log(TAG + ": initZygote");
FrameworkResourceHook.hookZygote();
} }
@Override @Override
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) { public void onModuleLoaded(ModuleLoadedParam param) {
switch (lpparam.packageName) { log(TAG, "module loaded in " + param.getProcessName());
case "android": }
XposedBridge.log(TAG + ": hooking system_server");
SystemServerHook.hook(lpparam); @Override
DisplayManagerHook.hook(lpparam); public void onSystemServerStarting(SystemServerStartingParam param) {
break; log(TAG, "onSystemServerStarting");
case "com.android.settings": hookResourcesBoolean(param.getClassLoader());
XposedBridge.log(TAG + ": hooking Settings"); DisplayManagerHook.hook(this, param);
MediaRouterHook.hookSettings(lpparam); SystemServerHook.hook(this, param);
break; }
@Override
public void onPackageLoaded(PackageLoadedParam param) {
String pkg = param.getPackageName();
log(TAG, "onPackageLoaded " + pkg);
hookResourcesBoolean(param.getDefaultClassLoader());
if ("com.android.settings".equals(pkg)) {
MediaRouterHook.hookSettings(this, param);
}
}
private void hookResourcesBoolean(ClassLoader classLoader) {
try {
var method = Resources.class.getDeclaredMethod("getBoolean", int.class);
hook(method).intercept(new ResourceBooleanHooker());
log(TAG, "hooked Resources.getBoolean");
} catch (Throwable t) {
log(TAG, "failed to hook Resources.getBoolean: " + t.getMessage());
}
}
private void log(String tag, String msg) {
log(0, tag, msg);
}
private static class ResourceBooleanHooker implements XposedInterface.Hooker {
@Override
public Object intercept(XposedInterface.Chain chain) throws Throwable {
Object result = chain.proceed();
int resId = (int) chain.getArg(0);
try {
Resources res = (Resources) chain.getThisObject();
String name = res.getResourceEntryName(resId);
if ("config_enableWifiDisplay".equals(name)) {
return true;
} else if ("config_wifiDisplaySupportsProtectedBuffers".equals(name)) {
return true;
}
} catch (Resources.NotFoundException ignored) {
}
return result;
} }
} }
} }

View File

@@ -1,78 +1,56 @@
package com.miracast.enabler.hooks; package com.miracast.enabler.hooks;
import de.robv.android.xposed.XC_MethodHook; import io.github.libxposed.api.XposedInterface;
import de.robv.android.xposed.XposedBridge; import io.github.libxposed.api.XposedModule;
import de.robv.android.xposed.XposedHelpers; import io.github.libxposed.api.XposedModuleInterface;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import java.lang.reflect.Method;
/**
* Hooks in system_server targeting WifiDisplayAdapter and WifiDisplayController
* to ensure the WFD stack initializes and scans correctly.
*
* On some builds, WifiDisplayController.requestStartScan() has additional
* gating checks beyond the resource flag. This hook ensures scanning proceeds.
*/
public class DisplayManagerHook { public class DisplayManagerHook {
private static final String TAG = "MiracastEnabler/Display"; private static final String TAG = "MiracastEnabler/Display";
public static void hook(XC_LoadPackage.LoadPackageParam lpparam) { public static void hook(XposedModule module, XposedModuleInterface.SystemServerStartingParam param) {
hookWifiDisplayFeatureState(lpparam); hookFeatureState(module, param);
hookWifiDisplayController(lpparam); hookController(module, param);
} }
/** private static void hookFeatureState(XposedModule module, XposedModuleInterface.SystemServerStartingParam param) {
* Hook WifiDisplayStatus.getFeatureState() to always return FEATURE_STATE_ON (3).
* This ensures that any code checking the feature state (Settings, SystemUI)
* sees WFD as fully enabled.
*/
private static void hookWifiDisplayFeatureState(XC_LoadPackage.LoadPackageParam lpparam) {
try { try {
XposedHelpers.findAndHookMethod( Class<?> clazz = param.getClassLoader()
"android.hardware.display.WifiDisplayStatus", .loadClass("android.hardware.display.WifiDisplayStatus");
lpparam.classLoader, Method method = clazz.getDeclaredMethod("getFeatureState");
"getFeatureState", module.hook(method).intercept(new FeatureStateHooker());
new XC_MethodHook() { module.log(0, TAG, "hooked WifiDisplayStatus.getFeatureState -> ON");
@Override
protected void afterHookedMethod(MethodHookParam param) {
param.setResult(3); // FEATURE_STATE_ON
}
}
);
XposedBridge.log(TAG + ": hooked WifiDisplayStatus.getFeatureState -> ON");
} catch (Throwable t) { } catch (Throwable t) {
XposedBridge.log(TAG + ": failed to hook getFeatureState"); module.log(0, TAG, "failed to hook getFeatureState: " + t.getMessage());
XposedBridge.log(t);
} }
} }
/** private static void hookController(XposedModule module, XposedModuleInterface.SystemServerStartingParam param) {
* Hook WifiDisplayController to ensure scanning is not blocked by
* vendor-specific checks (e.g., missing WFD IE support flag).
*/
private static void hookWifiDisplayController(XC_LoadPackage.LoadPackageParam lpparam) {
try { try {
Class<?> controllerClass = XposedHelpers.findClass( Class<?> clazz = param.getClassLoader()
"com.android.server.display.WifiDisplayController", .loadClass("com.android.server.display.WifiDisplayController");
lpparam.classLoader Method method = clazz.getDeclaredMethod("requestStartScan");
); module.hook(method).intercept(new ScanHooker());
module.log(0, TAG, "hooked WifiDisplayController.requestStartScan");
// Log when scan starts to confirm the WFD stack is active
XposedHelpers.findAndHookMethod(
controllerClass,
"requestStartScan",
new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
XposedBridge.log(TAG + ": WifiDisplayController.requestStartScan() called");
}
}
);
XposedBridge.log(TAG + ": hooked WifiDisplayController");
} catch (Throwable t) { } catch (Throwable t) {
// Controller class name may differ or not exist — non-fatal module.log(0, TAG, "WifiDisplayController hook skipped: " + t.getMessage());
XposedBridge.log(TAG + ": WifiDisplayController hook skipped: " + t.getMessage()); }
}
private static class FeatureStateHooker implements XposedInterface.Hooker {
@Override
public Object intercept(XposedInterface.Chain chain) throws Throwable {
chain.proceed();
return 3; // FEATURE_STATE_ON
}
}
private static class ScanHooker implements XposedInterface.Hooker {
@Override
public Object intercept(XposedInterface.Chain chain) throws Throwable {
return chain.proceed();
} }
} }
} }

View File

@@ -1,52 +1,4 @@
package com.miracast.enabler.hooks; package com.miracast.enabler.hooks;
import android.content.res.Resources; // Resource hooking is now handled directly in MainHook via ResourceBooleanHooker.
// This class is kept as a placeholder for any future resource-specific hooks.
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
/**
* Hooks Resources.getBoolean() globally (in zygote) so that every process —
* including system_server, Settings, and SystemUI — sees
* config_enableWifiDisplay = true and
* config_wifiDisplaySupportsProtectedBuffers = true.
*
* This is the primary mechanism: WifiDisplayAdapter in DisplayManagerService
* checks these resources at startup to decide whether to register the WFD
* display adapter at all.
*/
public class FrameworkResourceHook {
private static final String TAG = "MiracastEnabler/Resource";
public static void hookZygote() {
try {
XposedHelpers.findAndHookMethod(
Resources.class,
"getBoolean",
int.class,
new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
int resId = (int) param.args[0];
try {
Resources res = (Resources) param.thisObject;
String name = res.getResourceEntryName(resId);
if ("config_enableWifiDisplay".equals(name)) {
param.setResult(true);
} else if ("config_wifiDisplaySupportsProtectedBuffers".equals(name)) {
param.setResult(true);
}
} catch (Resources.NotFoundException ignored) {
}
}
}
);
XposedBridge.log(TAG + ": hooked Resources.getBoolean");
} catch (Throwable t) {
XposedBridge.log(TAG + ": failed to hook Resources.getBoolean");
XposedBridge.log(t);
}
}
}

View File

@@ -1,90 +1,33 @@
package com.miracast.enabler.hooks; package com.miracast.enabler.hooks;
import de.robv.android.xposed.XC_MethodHook; import io.github.libxposed.api.XposedInterface;
import de.robv.android.xposed.XposedBridge; import io.github.libxposed.api.XposedModule;
import de.robv.android.xposed.XposedHelpers; import io.github.libxposed.api.XposedModuleInterface;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import java.lang.reflect.Method;
/**
* Hooks in the Settings app to make WFD (Miracast) sinks visible
* in the Cast / Connected Devices UI.
*
* Android Settings checks WifiDisplayStatus.getFeatureState() to decide
* whether to show the Wi-Fi Display settings section. We force it to
* FEATURE_STATE_ON so the WFD option appears.
*
* We also hook in Settings to ensure the WifiDisplaySettings fragment
* is reachable and not filtered out.
*/
public class MediaRouterHook { public class MediaRouterHook {
private static final String TAG = "MiracastEnabler/Router"; private static final String TAG = "MiracastEnabler/Router";
public static void hookSettings(XC_LoadPackage.LoadPackageParam lpparam) { public static void hookSettings(XposedModule module, XposedModuleInterface.PackageLoadedParam param) {
hookFeatureStateInSettings(lpparam); hookFeatureStateInSettings(module, param);
hookWifiDisplaySettings(lpparam); hookWifiDisplaySettings(module, param);
} }
/** private static void hookFeatureStateInSettings(XposedModule module, XposedModuleInterface.PackageLoadedParam param) {
* In the Settings process, hook WifiDisplayStatus.getFeatureState()
* to return FEATURE_STATE_ON. This makes the Wi-Fi Display section
* appear in Cast preferences.
*/
private static void hookFeatureStateInSettings(XC_LoadPackage.LoadPackageParam lpparam) {
try { try {
XposedHelpers.findAndHookMethod( Class<?> clazz = param.getDefaultClassLoader()
"android.hardware.display.WifiDisplayStatus", .loadClass("android.hardware.display.WifiDisplayStatus");
lpparam.classLoader, Method method = clazz.getDeclaredMethod("getFeatureState");
"getFeatureState", module.hook(method).intercept(new FeatureStateOnHooker());
new XC_MethodHook() { module.log(0, TAG, "hooked getFeatureState in Settings");
@Override
protected void afterHookedMethod(MethodHookParam param) {
param.setResult(3); // FEATURE_STATE_ON
}
}
);
XposedBridge.log(TAG + ": hooked getFeatureState in Settings");
} catch (Throwable t) { } catch (Throwable t) {
XposedBridge.log(TAG + ": failed to hook getFeatureState in Settings"); module.log(0, TAG, "failed to hook getFeatureState in Settings: " + t.getMessage());
XposedBridge.log(t);
} }
} }
/** private static void hookWifiDisplaySettings(XposedModule module, XposedModuleInterface.PackageLoadedParam param) {
* Try to hook the WifiDisplaySettings fragment to ensure it initializes
* even if the Settings app tries to hide it based on device config.
*/
private static void hookWifiDisplaySettings(XC_LoadPackage.LoadPackageParam lpparam) {
// On AOSP Settings, WifiDisplaySettings is a PreferenceFragment that
// checks isAvailable() based on the feature state. Since we already
// hook getFeatureState, this should work. But some OEMs override
// the availability check separately.
String[] possibleClasses = {
"com.android.settings.wfd.WifiDisplaySettings",
"com.android.settings.display.WifiDisplaySettings",
"com.android.settings.connecteddevice.WifiDisplaySettings",
};
for (String className : possibleClasses) {
try {
Class<?> clazz = XposedHelpers.findClass(className, lpparam.classLoader);
XposedHelpers.findAndHookMethod(
clazz,
"isAvailable",
new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
param.setResult(true);
}
}
);
XposedBridge.log(TAG + ": hooked " + className + ".isAvailable()");
return;
} catch (Throwable ignored) {
}
}
// Also try hooking the preference controller that gates WFD visibility
String[] controllerClasses = { String[] controllerClasses = {
"com.android.settings.wfd.WifiDisplayPreferenceController", "com.android.settings.wfd.WifiDisplayPreferenceController",
"com.android.settings.display.WifiDisplayPreferenceController", "com.android.settings.display.WifiDisplayPreferenceController",
@@ -92,23 +35,56 @@ public class MediaRouterHook {
for (String className : controllerClasses) { for (String className : controllerClasses) {
try { try {
Class<?> clazz = XposedHelpers.findClass(className, lpparam.classLoader); Class<?> clazz = param.getDefaultClassLoader().loadClass(className);
XposedHelpers.findAndHookMethod( Method method = clazz.getDeclaredMethod("getAvailabilityStatus");
clazz, module.hook(method).intercept(new AvailabilityHooker());
"getAvailabilityStatus", module.log(0, TAG, "hooked " + className + ".getAvailabilityStatus()");
new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
param.setResult(0); // AVAILABLE = 0
}
}
);
XposedBridge.log(TAG + ": hooked " + className + ".getAvailabilityStatus()");
return; return;
} catch (Throwable ignored) { } catch (Throwable ignored) {
} }
} }
XposedBridge.log(TAG + ": no WifiDisplaySettings class found to hook (non-fatal)"); String[] fragmentClasses = {
"com.android.settings.wfd.WifiDisplaySettings",
"com.android.settings.display.WifiDisplaySettings",
"com.android.settings.connecteddevice.WifiDisplaySettings",
};
for (String className : fragmentClasses) {
try {
Class<?> clazz = param.getDefaultClassLoader().loadClass(className);
Method method = clazz.getDeclaredMethod("isAvailable");
module.hook(method).intercept(new IsAvailableHooker());
module.log(0, TAG, "hooked " + className + ".isAvailable()");
return;
} catch (Throwable ignored) {
}
}
module.log(0, TAG, "no WifiDisplaySettings class found (non-fatal)");
}
private static class FeatureStateOnHooker implements XposedInterface.Hooker {
@Override
public Object intercept(XposedInterface.Chain chain) throws Throwable {
chain.proceed();
return 3; // FEATURE_STATE_ON
}
}
private static class AvailabilityHooker implements XposedInterface.Hooker {
@Override
public Object intercept(XposedInterface.Chain chain) throws Throwable {
chain.proceed();
return 0; // AVAILABLE
}
}
private static class IsAvailableHooker implements XposedInterface.Hooker {
@Override
public Object intercept(XposedInterface.Chain chain) throws Throwable {
chain.proceed();
return true;
}
} }
} }

View File

@@ -2,54 +2,35 @@ package com.miracast.enabler.hooks;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import de.robv.android.xposed.XC_MethodHook; import io.github.libxposed.api.XposedInterface;
import de.robv.android.xposed.XposedBridge; import io.github.libxposed.api.XposedModule;
import de.robv.android.xposed.XposedHelpers; import io.github.libxposed.api.XposedModuleInterface;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import java.lang.reflect.Method;
/**
* Hooks in system_server to:
* 1. Force hasSystemFeature("android.software.wifi_display") = true
* 2. Grant CONFIGURE_WIFI_DISPLAY and CONTROL_WIFI_DISPLAY permissions
* to our package so the QS tile can control WFD.
*/
public class SystemServerHook { public class SystemServerHook {
private static final String TAG = "MiracastEnabler/System"; private static final String TAG = "MiracastEnabler/System";
private static final String OUR_PACKAGE = "com.miracast.enabler"; private static final String OUR_PACKAGE = "com.miracast.enabler";
public static void hook(XC_LoadPackage.LoadPackageParam lpparam) { public static void hook(XposedModule module, XposedModuleInterface.SystemServerStartingParam param) {
hookSystemFeature(lpparam); hookSystemFeature(module, param);
hookPermissions(lpparam); hookPermissions(module, param);
} }
private static void hookSystemFeature(XC_LoadPackage.LoadPackageParam lpparam) { private static void hookSystemFeature(XposedModule module, XposedModuleInterface.SystemServerStartingParam param) {
try { try {
XposedHelpers.findAndHookMethod( Method method = param.getClassLoader()
"android.app.ApplicationPackageManager", .loadClass("android.app.ApplicationPackageManager")
lpparam.classLoader, .getDeclaredMethod("hasSystemFeature", String.class);
"hasSystemFeature", module.hook(method).intercept(new SystemFeatureHooker());
String.class, module.log(0, TAG, "hooked hasSystemFeature");
new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
String feature = (String) param.args[0];
if ("android.software.wifi_display".equals(feature)
|| "android.hardware.wifi.direct".equals(feature)) {
param.setResult(true);
}
}
}
);
XposedBridge.log(TAG + ": hooked hasSystemFeature");
} catch (Throwable t) { } catch (Throwable t) {
XposedBridge.log(TAG + ": failed to hook hasSystemFeature"); module.log(0, TAG, "failed to hook hasSystemFeature: " + t.getMessage());
XposedBridge.log(t);
} }
} }
private static void hookPermissions(XC_LoadPackage.LoadPackageParam lpparam) { private static void hookPermissions(XposedModule module, XposedModuleInterface.SystemServerStartingParam param) {
// Try multiple permission check paths — the class name varies by Android version
String[] permClasses = { String[] permClasses = {
"com.android.server.pm.permission.PermissionManagerServiceImpl", "com.android.server.pm.permission.PermissionManagerServiceImpl",
"com.android.server.pm.permission.PermissionManagerService", "com.android.server.pm.permission.PermissionManagerService",
@@ -57,49 +38,23 @@ public class SystemServerHook {
for (String className : permClasses) { for (String className : permClasses) {
try { try {
Class<?> clazz = XposedHelpers.findClass(className, lpparam.classLoader); Class<?> clazz = param.getClassLoader().loadClass(className);
XposedHelpers.findAndHookMethod( Method method = clazz.getDeclaredMethod("checkPermission", String.class, String.class, int.class);
clazz, module.hook(method).intercept(new PermissionHooker());
"checkPermission", module.log(0, TAG, "hooked permissions via " + className);
String.class, String.class, int.class,
new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
String perm = (String) param.args[0];
String pkg = (String) param.args[1];
if (OUR_PACKAGE.equals(pkg) && isWfdPermission(perm)) {
param.setResult(PackageManager.PERMISSION_GRANTED);
}
}
}
);
XposedBridge.log(TAG + ": hooked permissions via " + className);
return; return;
} catch (Throwable ignored) { } catch (Throwable ignored) {
} }
} }
// Fallback: hook the UID-based checkUidPermission
try { try {
XposedHelpers.findAndHookMethod( Method method = param.getClassLoader()
"android.app.ActivityManager", .loadClass("android.app.ActivityManager")
lpparam.classLoader, .getDeclaredMethod("checkComponentPermission", String.class, int.class, int.class, boolean.class);
"checkComponentPermission", module.hook(method).intercept(new ComponentPermissionHooker());
String.class, int.class, int.class, boolean.class, module.log(0, TAG, "hooked permissions via ActivityManager fallback");
new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
String perm = (String) param.args[0];
if (perm != null && isWfdPermission(perm)) {
param.setResult(PackageManager.PERMISSION_GRANTED);
}
}
}
);
XposedBridge.log(TAG + ": hooked permissions via ActivityManager fallback");
} catch (Throwable t) { } catch (Throwable t) {
XposedBridge.log(TAG + ": failed to hook permissions"); module.log(0, TAG, "failed to hook permissions: " + t.getMessage());
XposedBridge.log(t);
} }
} }
@@ -107,4 +62,42 @@ public class SystemServerHook {
return "android.permission.CONFIGURE_WIFI_DISPLAY".equals(perm) return "android.permission.CONFIGURE_WIFI_DISPLAY".equals(perm)
|| "android.permission.CONTROL_WIFI_DISPLAY".equals(perm); || "android.permission.CONTROL_WIFI_DISPLAY".equals(perm);
} }
private static class SystemFeatureHooker implements XposedInterface.Hooker {
@Override
public Object intercept(XposedInterface.Chain chain) throws Throwable {
Object result = chain.proceed();
String feature = (String) chain.getArg(0);
if ("android.software.wifi_display".equals(feature)
|| "android.hardware.wifi.direct".equals(feature)) {
return true;
}
return result;
}
}
private static class PermissionHooker implements XposedInterface.Hooker {
@Override
public Object intercept(XposedInterface.Chain chain) throws Throwable {
Object result = chain.proceed();
String perm = (String) chain.getArg(0);
String pkg = (String) chain.getArg(1);
if (OUR_PACKAGE.equals(pkg) && isWfdPermission(perm)) {
return PackageManager.PERMISSION_GRANTED;
}
return result;
}
}
private static class ComponentPermissionHooker implements XposedInterface.Hooker {
@Override
public Object intercept(XposedInterface.Chain chain) throws Throwable {
Object result = chain.proceed();
String perm = (String) chain.getArg(0);
if (perm != null && isWfdPermission(perm)) {
return PackageManager.PERMISSION_GRANTED;
}
return result;
}
}
} }

View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="app_name">Miracast Enabler</string> <string name="app_name">Miracast Enabler</string>
<string name="app_description">Enables native Miracast (Wi-Fi Display) on Android devices</string>
<string name="tile_label">Miracast</string> <string name="tile_label">Miracast</string>
<string name="tile_scanning">Scanning…</string> <string name="tile_scanning">Scanning…</string>
<string name="tile_connected">Connected</string> <string name="tile_connected">Connected</string>
@@ -57,9 +58,4 @@
<item>3</item> <item>3</item>
</string-array> </string-array>
<string-array name="xposed_scope">
<item>android</item>
<item>com.android.systemui</item>
<item>com.android.settings</item>
</string-array>
</resources> </resources>

View File

@@ -0,0 +1,3 @@
minApiVersion=100
targetApiVersion=100
staticScope=false

View File

@@ -0,0 +1,3 @@
android
com.android.systemui
com.android.settings