一、首先跟踪代码发现:
grep -Rns "shutdownanimation" frameworks/base/
frameworks/base/services/java/com/android/server/power/ShutdownThread.java:380: int shutdownanimation = SystemProperties.getInt("persist.sys.shutdownanimation", 0);
frameworks/base/services/java/com/android/server/power/ShutdownThread.java:381: if (mBootFastEnable || shutdownanimation != 1) pd.show();
frameworks/base/services/java/com/android/server/power/ShutdownThread.java:447: int shutdownanimation = SystemProperties.getInt("persist.sys.shutdownanimation", 0);
frameworks/base/services/java/com/android/server/power/ShutdownThread.java:450: if (shutdownanimation == 1) {
frameworks/base/services/java/com/android/server/power/ShutdownThread.java:568: if (shutdownanimation == 1) {
frameworks/base/cmds/bootanimation/BootAnimation.cpp:57:#define SYSTEM_SHUTDOWNANIMATION_FILE "/system/media/shutdownanimation.zip"
A33已经添加了关机动画相关代码,我们进一步跟踪代码发现:
frameworks/base/services/java/com/android/server/power/ShutdownThread.java
public void run() {
BroadcastReceiver br = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
// We don't allow apps to cancel this, so ignore the result.
actionDone();
}
};
if(!mBootFastEnable){
int shutdownanimation = SystemProperties.getInt("persist.sys.shutdownanimation", 0);
int value = Settings.System.getIntForUser(mContext.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
int rotation = Surface.ROTATION_0;
if (shutdownanimation == 1) {
IWindowManager mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
if (mWindowManager != null) {
try{
rotation = mWindowManager.getRotation();
mWindowManager.freezeRotation(Surface.ROTATION_0);
mWindowManager.updateRotation(true, true);
} catch (RemoteException e) {
e.printStackTrace();
}
}
persist.sys.shutdownanimation 是关键;可以在系统build.prop中加入 persist.sys.shutdownanimation=1,可以实现关机动画效果;
二、测试发现添加关机动画,关机音乐出现且和开机音乐一样(一般情况二者是不同的)?
\android\frameworks\base\cmds\bootanimation\bootanimation_main.cpp 代码中
if (!noBootAnimation) {
sp<ProcessState> proc(ProcessState::self());
ProcessState::self()->startThreadPool();
// create the boot animation object
sp<BootAnimation> boot = new BootAnimation();
boot->playBootMusic("/system/media/boot.wav");
IPCThreadState::self()->joinThreadPool();
}
此处要作一个处理:
if (!noBootAnimation) {
sp<ProcessState> proc(ProcessState::self());
ProcessState::self()->startThreadPool();
// create the boot animation object
sp<BootAnimation> boot = new BootAnimation();
// Await add
char shutdown[PROPERTY_VALUE_MAX];
property_get("sys.shutdown_animation", shutdown, "boot");
char* animation;
if (strcmp(shutdown, "shutdown")) {
boot->playBootMusic("/system/media/boot.wav");
} else {
boot->playBootMusic("/system/media/shutdownboot.wav");
}
IPCThreadState::self()->joinThreadPool();
}
三、以上二步实现了关机动画及关机音乐,但有一个小问题:我们在竖屏关机的时候发现关机瞬间有横屏现象?
1、在ShutdownThread.java 中只是对方向作了一个简单的处理
if (shutdownanimation == 1) {
IWindowManager mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
if (mWindowManager != null) {
try{
rotation = mWindowManager.getRotation();
mWindowManager.freezeRotation(Surface.ROTATION_0);
mWindowManager.updateRotation(true, true);
} catch (RemoteException e) {
e.printStackTrace();
}
}
if (rotation != Surface.ROTATION_0) {
SystemClock.sleep(600);
} else {
SystemClock.sleep(200);
}
2、在\android\frameworks\base\services\java\com\android\server\wm\WindowManagerService.java中我们加入:
Intent it = new Intent(Intent.ACTION_UI_BOOT_FINISH);
mContext.sendBroadcast(it);
// Await add
if(SystemProperties.get("persist.sys.rotation", "false").equals("true")){
Settings.System.putInt(mContext.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
}
mPolicy.enableScreenAfterBoot();
// Make sure the last requested orientation has been applied.
updateRotationUnchecked(false, false);
rotation_boostuperf_enable = true;
3、此时还需要在ShutdownThread.java中作一个判断:
if (rotation != Surface.ROTATION_0) {
SystemClock.sleep(600);
} else {
SystemClock.sleep(200);
}
// Await add
if( value == 1){
SystemProperties.set("persist.sys.rotation", "true");
} else {
SystemProperties.set("persist.sys.rotation", "false");
}
SystemProperties.set("sys.shutdown_animation", "shutdown");
SystemProperties.set("service.bootanim.exit", "0");
SystemProperties.set("ctl.start", "bootanim");
这样竖屏关机的时候横屏的问题就可以解决了