[XPERIA X10 Android Development] CosmicDan:
Hello all,

I wrote this init.d script to be included in the next version of Team M&M's MIUI ROM. Here I am releasing it to the masses for inclusion in your ROM.

Automatically detects/changes light values to switch out framework-res.safe and framework-res.hwled - the script is pretty self explanatory. Enjoy :)

License...? Whatever you want, GPLv2 I guess. Just don't steal it and claim it as your own, credit would be nice.

Behold:

# ------------------------------------------------
# Team M&M's MIUI script by CosmicDan
# ------------------------------------------------
# Auto HW_LED fix
# You can change these variables to your liking, but don't blame us for explosions if you get it wrong ;)
# Pretty sure these do nothing anyway since the X10 ambient light sensor is not read by the OS (the LCD
# hardware controls it 100% on it's own AFAIK) but MiUI needs these values otherwise it won't boot.
#
# Second release - also disables "Automatic Brightness" (manual only). You can disable this toggle via XML
# now if you wish ;)
#
my_light_sensor_levels="200,400,1000,3000"
my_light_sensor_lcd_values="35,55,70,70,250"
my_light_sensor_button_values="255,255,0,0,0"
#
# ------------------------------------------------
# ------------------------------------------------
# ------------------------------------------------
# DON'T TOUCH ANYTHING BELOW HERE!
# ------------------------------------------------

# Verbose shell output for debugging purposes
# set -xv

# ------------------------------------------------
# Helper functions
# ------------------------------------------------

dodatabase()
{
if [ "$1" = "1" ];
then
if [ "$2" = "check" ];
then
dosql read "light_screen_dim";
fi
if [ "$2" = "set" ];
then
dosql write 88 "light_screen_dim" "20";
[ "$?" -eq "0" ] && light_screen_dim=20;
fi
fi
if [ "$1" = "2" ];
then
if [ "$2" = "check" ];
then
dosql read light_sensor_custom;
fi
if [ "$2" = "set" ];
then
dosql write 82 light_sensor_custom "1";
[ "$?" -eq "0" ] && light_sensor_custom=1;
fi
fi
if [ "$1" = "3" ];
then
[ "$2" = "check" ] && dosql read light_sensor_levels;
[ "$2" = "set" ] && dosql write 89 light_sensor_levels "$my_light_sensor_levels";
fi
if [ "$1" = "4" ];
then
[ "$2" = "check" ] && dosql read light_sensor_lcd_values;
[ "$2" = "set" ] && dosql write 90 light_sensor_lcd_values "$my_light_sensor_lcd_values";
fi
if [ "$1" = "5" ];
then
[ "$2" = "check" ] && dosql read light_sensor_button_values;
[ "$2" = "set" ] && dosql write 91 light_sensor_button_values "$my_light_sensor_button_values";
fi
if [ "$1" = "6" ];
then
[ "$2" = "check" ] && dosql read light_sensor_keyboard_values;
[ "$2" = "set" ] && dosql write 92 light_sensor_keyboard_values "0,0,0,0,0";
fi
if [ "$1" = "7" ];
then
[ "$2" = "check" ] && dosql read screen_brightness_mode;
[ "$2" = "set" ] && dosql write 25 screen_brightness_mode "0";
fi

}

dosql()
{
if [ "$1" = "read" ];
then
eval "$2"=`sqlite3 -nullvalue 'nullvalue' -column $SETTINGSDB 'SELECT value FROM system WHERE name='\"$2\"';'`;
elif [ "$1" = "write" ];
then
sqlite3 -nullvalue 'nullvalue' -column $SETTINGSDB 'INSERT OR REPLACE INTO system VALUES ('\"$2\"','\"$3\"','\"$4\"');';
echo "[M&M] Changed settings - \"$3\" (at _id $2) to $4";
fi
}

# ------------------------------------------------
# ------------------------------------------------
# ------------------------------------------------

echo "[M&M] Running M&M TEAM Custom Boot Parameters...";
SETTINGSDB=/data/data/com.android.providers.settings/databases/settings.db
if [ -e $SETTINGSDB ];
then
echo "[M&M] Verifying locked settings...";

dodatabase 1 check
if [ ! "$light_screen_dim" = "20" ];
then
dodatabase 1 set
else
echo "[M&M] light_screen_dim already 20, unchanged";
fi

dodatabase 2 check
if [ ! "$light_sensor_custom" = "1" ];
then
dodatabase 2 set
else
echo "[M&M] light_sensor_custom already enabled, unchanged";
fi

# Verify additional settings relating to Display

dodatabase 3 check
if [ ! "$my_light_sensor_levels" = "$light_sensor_levels" ];
then
dodatabase 3 set
else
echo "[M&M] light_sensor_levels already set, unchanged";
fi

dodatabase 4 check
if [ ! "$my_light_sensor_lcd_values" = "$light_sensor_lcd_values" ];
then
dodatabase 4 set
else
echo "[M&M] light_sensor_lcd_values already set, unchanged";
fi

dodatabase 5 check
if [ ! "$light_sensor_button_values" = "255,255,0,0,0" ];
then
dodatabase 5 set
else
echo "[M&M] light_sensor_button_values already set, unchanged";
fi

dodatabase 6 check
if [ ! "$light_sensor_keyboard_values" = "0,0,0,0,0" ];
then
dodatabase 6 set
else
echo "[M&M] light_sensor_keyboard_values already set, unchanged";
fi

dodatabase 7 check
if [ ! "$screen_brightness_mode" = "0" ];
then
dodatabase 7 set
else
echo "[M&M] screen_brightness_mode already manual, unchanged";
fi

else
echo "[M&M] Settings Provider database not found! Factory defaults assumed.";
fi

if [ "$light_screen_dim" -eq "20" ] && [ "$light_sensor_custom" -eq "1" ]; then hw_led=1; fi

if [ "$hw_led" -eq "1" ];
then
if [ -e /system/framework/framework-res.hwled ];
then
echo "[M&M] HW_LED fix is allowed but not enabled yet. Now enabling and clearing dalvik-cache...";
mount -o rw,remount -t yaffs2 /dev/block/mtdblock2 /system;
mv /system/framework/framework-res.apk /system/framework/framework-res.safe;
mv /system/framework/framework-res.hwled /system/framework/framework-res.apk;
mount -o ro,remount -t yaffs2 /dev/block/mtdblock2 /system;
sqlite3 -column $SETTINGSDB 'DROP INDEX systemIndex1;'
sqlite3 -column $SETTINGSDB 'CREATE INDEX systemIndex1 ON system (name);'
rm -r -f /data/dalvik-cache/*
fi
else
if [ -e /system/framework/framework-res.safe ];
then
echo "[M&M] HW_LED fix is NOT allowed yet but still enabled."
echo "[M&M] ...Assumed factory reset performed (or database permissions/integrity error)"
echo "[M&M] ...Now reverting to safe framework and clearing dalvik-cache to prevent bootloop...";
mount -o rw,remount -t yaffs2 /dev/block/mtdblock2 /system;
mv /system/framework/framework-res.apk /system/framework/framework-res.hwled;
mv /system/framework/framework-res.safe /system/framework/framework-res.apk;
mount -o ro,remount -t yaffs2 /dev/block/mtdblock2 /system;
if [ -e $SETTINGSDB ]; then
sqlite3 -column $SETTINGSDB 'DROP INDEX systemIndex1;'
sqlite3 -column $SETTINGSDB 'CREATE INDEX systemIndex1 ON system (name);'
fi
rm -r -f /data/dalvik-cache/*
fi
fi

SETTINGSDB=
light_screen_dim=
light_sensor_custom=
light_sensor_levels=
light_sensor_lcd_values=
light_sensor_button_values=
light_sensor_keyboard_values=
screen_brightness_mode=
my_light_sensor_levels=
my_light_sensor_lcd_values=
hw_led=
echo "[M&M] Custom boot processing complete."
exit 0

Obviously, you have the stock (safe) framework as "framework-res.apk" and then place your HW-LED modified one as framework-res.hwled

The basic summary is that it checks what the values are for display, adjusts them if needed, then enables the hwled fixed version of APK and backing up the old one. In the event of a Factory Reset (i.e. database for Settings Provider not found) it will swap the safe one back in. The user just has to reboot again to get the HW-LED one back, since the settings database has been recreated.