83 lines
2.8 KiB
JavaScript
83 lines
2.8 KiB
JavaScript
const { withDangerousMod } = require("@expo/config-plugins");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
/**
|
|
* Disables the JR_shikoku_felica_balance widget on devices without NFC.
|
|
* Injects code into MainApplication.kt to check NFC availability at startup
|
|
* and disable the widget provider component if NFC is not supported.
|
|
*/
|
|
const withNfcWidgetGuard = (config) => {
|
|
return withDangerousMod(config, [
|
|
"android",
|
|
async (config) => {
|
|
const mainAppPath = path.join(
|
|
config.modRequest.projectRoot,
|
|
"android/app/src/main/java/jrshikokuinfo/xprocess/hrkn/MainApplication.kt"
|
|
);
|
|
|
|
if (!fs.existsSync(mainAppPath)) {
|
|
console.warn("[withNfcWidgetGuard] MainApplication.kt not found, skipping.");
|
|
return config;
|
|
}
|
|
|
|
let contents = fs.readFileSync(mainAppPath, "utf-8");
|
|
|
|
// Skip if already patched
|
|
if (contents.includes("disableFelicaWidgetIfNoNfc")) {
|
|
return config;
|
|
}
|
|
|
|
// Add necessary imports
|
|
contents = contents.replace(
|
|
"import android.app.Application",
|
|
"import android.app.Application\n" +
|
|
"import android.content.ComponentName\n" +
|
|
"import android.content.pm.PackageManager\n" +
|
|
"import android.nfc.NfcAdapter"
|
|
);
|
|
|
|
// Add the NFC check method and call it from onCreate
|
|
const nfcGuardMethod = `
|
|
/**
|
|
* Disable the Felica balance widget on devices without NFC hardware,
|
|
* so it won't appear in the Android widget picker.
|
|
*/
|
|
private fun disableFelicaWidgetIfNoNfc() {
|
|
if (NfcAdapter.getDefaultAdapter(this) == null) {
|
|
packageManager.setComponentEnabledSetting(
|
|
ComponentName(this, jrshikokuinfo.xprocess.hrkn.widget.JR_shikoku_felica_balance::class.java),
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
|
PackageManager.DONT_KILL_APP
|
|
)
|
|
} else {
|
|
packageManager.setComponentEnabledSetting(
|
|
ComponentName(this, jrshikokuinfo.xprocess.hrkn.widget.JR_shikoku_felica_balance::class.java),
|
|
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
|
PackageManager.DONT_KILL_APP
|
|
)
|
|
}
|
|
}`;
|
|
|
|
// Insert method before the closing brace of the class
|
|
contents = contents.replace(
|
|
/\n}\s*$/,
|
|
`\n${nfcGuardMethod}\n}\n`
|
|
);
|
|
|
|
// Insert call in onCreate after ApplicationLifecycleDispatcher
|
|
contents = contents.replace(
|
|
"ApplicationLifecycleDispatcher.onApplicationCreate(this)",
|
|
"ApplicationLifecycleDispatcher.onApplicationCreate(this)\n disableFelicaWidgetIfNoNfc()"
|
|
);
|
|
|
|
fs.writeFileSync(mainAppPath, contents);
|
|
console.log("[withNfcWidgetGuard] Patched MainApplication.kt to disable Felica widget on non-NFC devices.");
|
|
|
|
return config;
|
|
},
|
|
]);
|
|
};
|
|
|
|
module.exports = withNfcWidgetGuard;
|