[Xiaomi Mi Note Pro] Aqq123:
Automating the Modifications

Doing all the modifications as described above might soon get tedious. What if there was a way to automate them? Well, there actually is. With the help of an Edify ("Update") script, the whole process becomes much faster.

The Edify Script

# Xiaomi Note Pro (Leo) Customization Script
# Aqq123 @ XDA, http://xdaforums.com/showthread.php?t=3286972

# MIUI7 5.12.31, Xiaomi.eu Image

### Edify Scripting Reference ###
# abort("msg");
# delete("/path");
# delete_recursive("/path");
# format("fstype", "parttype", "/dev");
# getprop("sys.prop.name");
# get_device_compatible("device_name");
# mount("fstype", "parttype", "/dev", "/mntpt", "mntopts");
# package_extract_dir("dir_in_zip", "/dst_dir");
# package_extract_file("/src", "/dst");
# run_program("/path", "--param1", "--param2", "--param3");
# set_metadata("/path", "uid", uid, "gid", gid, "mode", 0644,
# "capabilities", 0, "selabel", "u:object_r:system_file:s0");
# set_metadata_recursive("/path", "uid", uid, "gid", gid, "dmode", 0755, "fmode", 0644,
# "capabilities", 0, "selabel", "u:object_r:system_file:s0");
# symlink(dst, src[, src2, ...]);
# ui_print("msg");
# umount("/mntpt");
######

# Compatibility Check

ui_print("Checking compatibility...");
get_device_compatible("leo") == "OK" || abort("The update is not intended for this device.");
set_progress(0.000000);
show_progress(1.34, 100);

# Mount Filesystems

ui_print("Mounting filesystems...");
mount("ext4", "EMMC", "/dev/block/bootdevice/by-name/system", "/system", "barrier=1,commit=1,data=ordered,errors=panic,max_batch_time=0,nodelalloc");
# || abort("System: Failed to mount partition!");
#mount("ext4", "EMMC", "/dev/block/bootdevice/by-name/cache", "/cache", "");
# || abort("Cache: Failed to mount partition!");
mount("ext4", "EMMC", "/dev/block/bootdevice/by-name/userdata", "/data", "");
# || abort("Data: Failed to mount partition!");

# Note: Do not use "|| abort(...)" with TWRP as partitions are already mounted.

########
# BOOT #
########

# Extract

ui_print("Boot: Extracting items...");
package_extract_file("boot-patched.img", "/dev/block/bootdevice/by-name/boot") || abort("Boot: Extraction failed!");

##########
# SYSTEM #
##########

# Delete

ui_print("System: Deleting items...");

# Apps
delete_recursive("/system/app/AnalyticsCore/");
delete_recursive("/system/app/BaiduNetworkLocation/");
delete_recursive("/system/app/BugReport/");
delete_recursive("/system/app/matcli/");
delete_recursive("/system/app/Metok/");
delete_recursive("/system/app/MiLinkService/");
delete_recursive("/system/app/NVItem/");
delete_recursive("/system/app/PicoTts/");
delete_recursive("/system/app/SecurityAdd/");
delete_recursive("/system/app/TrafficControl/");
delete_recursive("/system/app/VirtualSim/");
delete_recursive("/system/app/XiaomiVip/");
delete_recursive("/system/app/XMPass/");

# Apps Already Removed in Xiaomi.eu
#delete_recursive("/system/app/BasicDreams/");
#delete_recursive("/system/app/Galaxy4/");
#delete_recursive("/system/app/GameCenter/");
#delete_recursive("/system/app/GooglePinyinIME/");
#delete_recursive("/system/app/HoloSpiralWallpaper/");
#delete_recursive("/system/app/LiveWallpapers/");
#delete_recursive("/system/app/MiuiSuperMarket/");
#delete_recursive("/system/app/NoiseField/");
#delete_recursive("/system/app/PaymentService/");
#delete_recursive("/system/app/PhaseBeam/");
#delete_recursive("/system/app/PhotoTable/");
#delete_recursive("/system/app/SystemAdSolution/");
#delete_recursive("/system/app/VisualizationWallpapers/");
#delete_recursive("/system/app/VirtualSim/");
#delete_recursive("/system/app/VoiceAssist/");
#delete_recursive("/system/priv-app/MiGameCenterSDKService/");

# Other Garbage
delete_recursive("/system/bin/qmi-framework-tests/");
delete("/system/etc/fake_cdrom_install.iso");
delete("/system/media/audio/create_symlink_for_audio-timestamp");

# Google Apps
delete("/system/etc/permissions/com.google.android.dialer.support.xml");
#delete("/system/etc/permissions/com.google.android.maps.xml"); # Careful! Also present in stock
delete("/system/etc/permissions/com.google.android.media.effects.xml");
delete("/system/etc/permissions/com.google.widevine.software.drm.xml");
delete("/system/etc/preferred-apps/google.xml");
delete("/system/etc/preferred-apps/");
delete("/system/etc/sysconfig/google.xml");
delete("/system/etc/sysconfig/google_build.xml");
delete("/system/etc/sysconfig/");
delete("/system/framework/com.google.android.dialer.support.jar");
#delete("/system/framework/com.google.android.maps.jar"); # Careful! Also present in stock
delete("/system/framework/com.google.android.media.effects.jar");
delete("/system/framework/com.google.widevine.software.drm.jar");
delete_recursive("/system/app/CloudPrint2/");
delete_recursive("/system/app/GoogleCalendarSyncAdapter/");
delete_recursive("/system/app/GoogleContactsSyncAdapter/");
delete_recursive("/system/priv-app/GoogleBackupTransport/");
delete_recursive("/system/priv-app/GoogleFeedback/");
delete_recursive("/system/priv-app/GoogleLoginService/");
delete_recursive("/system/priv-app/GoogleOneTimeInitializer/");
delete_recursive("/system/priv-app/GooglePartnerSetup/");
delete_recursive("/system/priv-app/GoogleServicesFramework/");
delete_recursive("/system/priv-app/Phonesky/");
delete_recursive("/system/priv-app/PrebuiltGmsCore/");

# Fonts
delete("/system/fonts/AndroidEmoji.ttf");
delete("/system/fonts/DroidNaskh-Regular-SystemUI.ttf");
delete("/system/fonts/NotoSansHebrewUI-Bold.ttf"); # Just a copy, there is UI variant for Hebrew
delete("/system/fonts/NotoSansHebrewUI-Regular.ttf"); # Just a copy, there is no UI variant for Hebrew

# Default Audio Resources (Only Remove If Replacing Your Own)
#delete_recursive("/system/media/audio/alarms/");
#delete_recursive("/system/media/audio/notifications/");
#delete_recursive("/system/media/audio/ringtones/");

# Default Visual Resources (Only Remove If Replacing Your Own)
#delete_recursive("/system/media/lockscreen/");
delete_recursive("/system/media/theme/miui_mod_icons/");
#delete_recursive("/system/media/wallpaper/");

# Extract

ui_print("System: Extracting items...");
package_extract_dir("system", "/system") || abort("System: Extraction failed!");

# Set Metadata

ui_print("System: Setting permissions...");
set_metadata_recursive("/system/app/",
"uid", 0, "gid", 0, "dmode", 0755, "fmode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");
set_metadata("/system/build.prop", "uid", 0, "gid", 0, "mode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");
set_metadata("/system/etc/gps.conf", "uid", 0, "gid", 0, "mode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");
set_metadata("/system/etc/init.qcom.post_boot.sh", "uid", 0, "gid", 0, "mode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");
set_metadata("/system/etc/mcd_default.conf", "uid", 0, "gid", 0, "mode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");
set_metadata("/system/etc/spn-conf.xml", "uid", 0, "gid", 0, "mode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");
set_metadata("/system/etc/system_fonts.xml", "uid", 0, "gid", 0, "mode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");
set_metadata_recursive("/system/fonts/",
"uid", 0, "gid", 0, "dmode", 0755, "fmode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");
set_metadata_recursive("/system/media/",
"uid", 0, "gid", 0, "dmode", 0755, "fmode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");
set_metadata_recursive("/system/priv-app/",
"uid", 0, "gid", 0, "dmode", 0755, "fmode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");
set_metadata_recursive("/system/usr/",
"uid", 0, "gid", 0, "dmode", 0755, "fmode", 0644,
"capabilities", 0x0, "selabel", "u:object_r:system_file:s0");

########
# DATA #
########

# Delete

ui_print("Data: Deleting items...");

delete_recursive("/data/evt-test/");
delete_recursive("/data/media/MIUI/");
delete_recursive("/data/miui/app/");
delete_recursive("/data/miui/cust/");
delete_recursive("/data/miui/gallery/");
delete_recursive("/data/miui/mms/");
delete_recursive("/data/miui/videoplugins/");
delete_recursive("/data/miui/yellowpage/");
delete_recursive("/data/qmi-framework-tests/");

# Already Removed in Xiaomi.eu
#delete("/data/miui/app/vanward_applist");
#delete("/data/miui/cdrom_install.iso");
#delete("/data/miui/cust_variant");

# MIUI Update Binary

ui_print("Data: Executing MIUI Update Binary...");
package_extract_file("META-INF/com/miui/miui_update", "/cache/miui_update");
set_metadata("/cache/miui_update", "uid", 0, "gid", 0, "mode", 0555, "capabilities", 0x0);
run_program("/cache/miui_update");
delete("/cache/miui_update");

# Extract

ui_print("Data: Extracting items...");
package_extract_dir("data", "/data") || abort("Data: Extraction failed!");

# Set Metadata

ui_print("Data: Setting permissions...");
set_metadata_recursive("/data/miui", "uid", 1000, "gid", 1000, "dmode", 0755, "fmode", 0644,
"capabilities", 0x0);

#########
# CACHE #
#########

# Wipe Cache

#ui_print("Wiping cache...");
#delete_recursive("/cache/");

# Wipe Dalvik Cache

ui_print("Wiping Dalvik cache...");
delete_recursive("/data/dalvik-cache/");

# Unmount

ui_print("Unmounting filesystems...");
unmount("/system");
#unmount("/cache");
unmount("/data");

# Finish

set_progress(1.000000);
ui_print("Finished.");

When reusing the above script, please retain the attribution notice:

Aqq123 @ XDA, http://xdaforums.com/showthread.php?t=3286972

Thank you!

The Update ZIP Archive

Next, we'll create a collection of all the changed files we want installed on top of the original installation. The collection will have the following structure:

./data/

Everything in here will be unpacked into the /data partition.

./system/

Everything in here will be unpacked into the /system partition.

./boot-patched.img

The modified boot image that will be flashed into the boot partition.

./META-INF/com/android/metadata

./META-INF/com/android/otacert

Essentially redundant files that I'm keeping out of sentiment.

./META-INF/com/google/android/update-binary

./META-INF/com/miui/miui_update

Helper binaries that are taken from the latest version of the China Developer ROM installer.

./META-INF/com/google/android/update

r

-script

This is where we put the above Edify script. Note that the script is "update

r

", whereas the binary is "update."

All the stuff needs to be collected together and packed as one ZIP archive, which then becomes a flashable ZIP from inside the TWRP recovery or with adb sideload aqq123patch.zip, immediately after flashing the full MIUI image.

The Patch Builder Script

To automate things further, here is a simple batch script called build_patch.bat that rebuilds the patch file:

@echo off
if exist aqq123patch_leo*.zip del /f /q aqq123patch_leo*.zip
zip -r aqq123patch_leo.zip . -x *.bat *.zip

rem To generate a signed version, using signapk and your own key
rem change the above name to aqq123patch_leo_unsigned.zip
rem and uncomment the following two lines:

rem call signapkw aqq123patch_leo_unsigned.zip aqq123patch_leo.zip
rem del /f /q aqq123patch_leo_unsigned.zip

The script, also included in the attached archive, allows you to generate a signed archive as well, which is what I do, however you would need to have the signapkw.bat wrapper set up with your own key. For example:

@echo off
set PATH_SAVE=%PATH%
set PATH=%CD%;%PATH%;
java -jar "signapk.jar" -w "signapk.x509.pem" "signapk.pk8" %1 %2 %3 %4 %5 %6 %7 %8 %9
set PATH=%PATH_SAVE%
set PATH_SAVE=

The Complete Patch

For your convenience, all the stuff is attached to this post as a single archive. Note however that due to the 20 MiB attachment size constraint, I needed to leave out a couple of larger files, which you need to manually download from the attachments to previous posts and put into the appropriate directories. These are:

boot-patched.img

The modified boot partition image.

system/app/XiaomiServiceFramework/XiaomiServiceFramework.apk

system/priv-app/Contacts/Contacts.apk

system/priv-app/TeleService/TeleService.apk

The patched APKs.

system/fonts/*

system/usr/icu/icudt53l.dat

system/usr/share/zoneinfo/tzdata

Updated fonts, Unicode database and timezone data.

system/media/theme/miui_mod_icons/*

The merged collection of MIUI-themed app icons from MIUI Pro, Xiaomi.eu and the stock MIUI along with some obscure icons I designed on my own.

You are of course free to leave out any files you don't want included or put some more of your own. For example, I also install my own set of ringtones and wallpapers while removing the default ones this way. Note the following though:

If you don't want to flash the boot-patched.img, you need to edit the Edify script above (which I encourage you to do anyway).

If you put anything into other directories than the ones that are mentioned throughout this guide, you might need to adjust the metadata (ownership, permissions and SELinux labels).

If making the above changes, you might also want tocopy the ./file_contexts from the stock installer ZIP. I am not including it here, as with the way I use this script, it's not necessary (although you will see a warning about it in TWRP, just ignore it).