Android12 应用更新开机动画
项目selinux是打开的,会涉及到权限问题,所以实在具有系统签名的应用中实现。
将动画资源包放到"/data/local/traces"
目录
- system/core/rootdir/init.rc
# For security reasons, /data/local/tmp should always be empty.
# Do not place files or directories in /data/local/tmp
mkdir /data/local/tmp 0771 shell shell
mkdir /data/local/traces 0777 shell shell
mkdir /data/data 0771 system system encryption=None
mkdir /data/app-private 0771 system system encryption=Require
在BootAnimation.cpp
中添加路径
- /frameworks/base/cmds/bootanimation/BootAnimation.cpp
+static const char CUSTOM_BOOTANIMATION_FILE[] = "/data/local/traces/bootanimation.zip";
static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
@@ -583,7 +584,7 @@ void BootAnimation::findBootAnimationFile() {
}
const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
// 将添加的路径放在第一位
- static const std::vector<std::string> bootFiles = {
+ static const std::vector<std::string> bootFiles = {CUSTOM_BOOTANIMATION_FILE,
APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
};
...
// 获取可用开机动画路径
bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
for (const std::string& f : files) {
if (access(f.c_str(), R_OK) == 0) {
mZipFileName = f.c_str();
return true;
}
}
return false;
}
添加权限
- /device/mediatek/sepolicy/basic/non_plat/bootanim.te
+allow bootanim trace_data_file:dir { search read };
+allow bootanim trace_data_file:file { read open map };
- /device/mediatek/sepolicy/basic/non_plat/system_app.te
+allow system_app trace_data_file:dir { search read create write add_name remove_name };
+allow system_app trace_data_file:file { read create write open getattr setattr unlink };
复制zip包到指定路径
private static final String DEST_PATH = "/data/local/traces/bootanimation.zip";
public boolean setSystemBootAnimation(String path) {
File file = new File(path);
if (file.exists()){
try {
File destFile = new File(DEST_PATH);
Runtime.getRuntime().exec("chmod 777" + DEST_PATH);
if (destFile.exists()){
destFile.delete();
}
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(DEST_PATH);
byte[] buffer = new byte[1024];
int byteRead;
while (-1 != (byteRead = fis.read(buffer))){
fos.write(buffer, 0, byteRead);
}
fis.close();
fos.flush();
fos.close();
} catch (IOException e){
e.printStackTrace();
return false;
}
}
File nFile = new File(DEST_PATH);
if (nFile.exists()){
try {
Files.setPosixFilePermissions(nFile.toPath(),
PosixFilePermissions.fromString("rw-rw-rw-"));
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
需要系统签名,包在data目录下,恢复出厂设置会被清除。
如果项目的selinux是关闭的,也可以自己创建其他路径
- system/core/rootdir/init.rc
mkdir /data/local/animation 0777 shell shell