接触Android框架层也有一段时间了,但是基本上都没有对遇到的问题写总结,当遇到同样的问题也只能凭着模糊的记忆去解决。打算从今天开始每天抽点时间对工作中遇到的问题进行总结。
今天要说的是为Android添加一个重启按钮,在Nine的客户需求中需要添加这项功能,在长按电源键弹出的菜单中没有重启选项,谨以此文记录自己添加这个功能的过程:
修改涉及到的文件如下:
frameworks\base\core\java\android\view\WindowManagerPolicy.java
frameworks\base\core\res\res\values\strings.xml
frameworks\base\core\res\res\values-zh-rCN\strings.xml
frameworks\base\policy\src\com\android\internal\policy\impl\GlobalActions.java
frameworks\base\services\java\com\android\server\wm\windowManagerService.java
frameworks\base\services\java\com\android\server\power\ShutdownThread.java
frameworks\base\core\res\res\values\symbols.xml
首先找到长按电源键弹出的对话框,在 frameworks\base\policy\src\com\android\internal\policy\impl\GlobalActions.java文件中
/**
* Create the global actions dialog.
* @return A new dialog.
*/
private GlobalActionsDialog createDialog() {
...
...
mItems = new ArrayList<Action>();
// first: power off
mItems.add(
new SinglePressAction(
com.android.internal.R.drawable.ic_lock_power_off,
R.string.global_action_power_off) {
public void onPress() {
// shutdown by making sure radio and power are handled accordingly.
mWindowManagerFuncs.shutdown(true);
}
public boolean onLongPress() {
mWindowManagerFuncs.rebootSafeMode(true);
return true;
}
public boolean showDuringKeyguard() {
return true;
}
public boolean showBeforeProvisioning() {
return true;
}
});
//xiaoyuguang 20130616 add reboot item
mItems.add(
new SinglePressAction(
com.android.internal.R.drawable.ic_lock_power_off,
R.string.global_action_reboot) {
public void onPress() {
// reboot
mWindowManagerFuncs.reboot(true);
}
public boolean onLongPress() {
return true;
}
public boolean showDuringKeyguard() {
return true;
}
public boolean showBeforeProvisioning() {
return true;
}
});
//end xiaoyuguang 20130616
...
...}
在GlobalActionsDialog方法可以看 mItems.add这个方法是添加菜单选项的,该菜单的添加的第一个选项就是关机选项。可以仿照关机的Item添加一个重启的选项,如上面的代码所示;这样就解决了在长按的电源键弹出的对话框中添加一个重启选项了。当然这仅仅是添加一个显示而已,接下来就为这个选项添加逻辑控制代码了。
在上面的代码中使用的mWindowManagerFuncs.reboot方法和R.string.global_action_reboot资源(资源的添加放到最后说),默认是不存在的,所以需要在自己手动添加。
首先在找到WindowManagerFuncs这个所在的位置,在frameworks\base\core\java\android\view\WindowManagerPolicy.java中
/**
* Interface for calling back in to the window manager that is private
* between it and the policy.
*/
public interface WindowManagerFuncs {
...
...
/**
* Switch the keyboard layout for the given device.
* Direction should be +1 or -1 to go to the next or previous keyboard layout.
*/
public void switchKeyboardLayout(int deviceId, int direction);
public void shutdown(boolean confirm);
public void rebootSafeMode(boolean confirm)
//xiaoyuguang
public void reboot(boolean confirm);
}
添加reboot方法。
但这只是添加接口而已,它的具体实现在呢?找了许久在
frameworks\base\services\java\com\android\server\wm\windowManagerService.java中找到了这个接口的实现。
/** {@hide} */
public class WindowManagerService extends IWindowManager.Stub
implements Watchdog.Monitor, WindowManagerPolicy.WindowManagerFuncs,
DisplayManagerService.WindowManagerFuncs, DisplayManager.DisplayListener {
...
...
// Called by window manager policy. Not exposed externally.
@Override
public void shutdown(boolean confirm) {
ShutdownThread.shutdown(mContext, confirm);
}
//xiaoyuguang 20130616 add reboot
@Override
public void reboot(boolean confirm){
ShutdownThread.reboot(mContext, null, confirm);
}
//end xiaoyuguang
// Called by window manager policy. Not exposed externally.
@Override
public void rebootSafeMode(boolean confirm) {
ShutdownThread.rebootSafeMode(mContext, confirm);
}
...
...}
同样在仿照关机的原理添加reboot的具体实现代码,既然在ShutdownThread这个类中提供了shutdown和rebootSafeMode的方法,那按理也应该有reboot的方法,或者类似reboot的方法。找到Shutdown.java文件,在frameworks\base\services\java\com\android\server\power\ShutdownThread.java中,
public final class ShutdownThread extends Thread {
...
...
/**
* Request a clean shutdown, waiting for subsystems to clean up their
* state etc. Must be called from a Looper thread in which its UI
* is shown.
*
* @param context Context used to display the shutdown progress dialog.
* @param confirm true if user confirmation is needed before shutting down.
*/
public static void shutdown(final Context context, boolean confirm) {
...
}
/**
* Request a clean shutdown, waiting for subsystems to clean up their
* state etc. Must be called from a Looper thread in which its UI
* is shown.
*
* @param context Context used to display the shutdown progress dialog.
* @param reason code to pass to the kernel (e.g. "recovery"), or null.
* @param confirm true if user confirmation is needed before shutting down.
*/
public static void reboot(final Context context, String reason, boolean confirm) {
mReboot = true;
mRebootSafeMode = false;
mRebootReason = reason;
Log.d(TAG, "reboot");
shutdownInner(context, confirm);
}
...
...
}
其中提供了一个静态的reboot方法,所以在windowManagerService.java中的reboot实现中直接调用ShutdownThread中reboot即可。
public static void reboot(final Context context, String reason, boolean confirm);有三个参数,后两个参数解释如下: reason 如果值为是null,正常重启;如果是recovery,系统重启进入recovery mode ;confirm为true显示关机提示框,需要用户【确认】;false不显示提示框,直接关机。
到此重启功能基本上可以使用了(除资源还没有添加之外),但是此时选择重启选项时,其提示还是不够关机的提示,所以还要修改选择“重启”时的对话框的提示。
在frameworks\base\services\java\com\android\server\power\ShutdownThread.java中
static void shutdownInner(final Context context, boolean confirm) {
...
...
final int resourceId = mRebootSafeMode
? com.android.internal.R.string.reboot_safemode_confirm
: (longPressBehavior == 2
? com.android.internal.R.string.shutdown_confirm_question
: com.android.internal.R.string.shutdown_confirm);
...
...
sConfirmDialog = new AlertDialog.Builder(context)
.setTitle(mRebootSafeMode
? com.android.internal.R.string.reboot_safemode_title
: com.android.internal.R.string.power_off)
.setMessage(resourceId)
...
...
}
修改如下:
final int resourceId = mReboot
? com.android.internal.R.string.reboot_confirm
: (mRebootSafeMode
? com.android.internal.R.string.reboot_safemode_confirm
: (longPressBehavior == 2
? com.android.internal.R.string.shutdown_confirm_question
: com.android.internal.R.string.shutdown_confirm));
...
...
sConfirmDialog = new AlertDialog.Builder(context)
.setTitle(mReboot
? com.android.internal.R.string.global_action_reboot
: (mRebootSafeMode
? com.android.internal.R.string.reboot_safemode_title
: com.android.internal.R.string.power_off))
.setMessage(resourceId)
至此关于代码部分的改动全部完成,接下就添加需要添加使用到的资源了,就是其中使用的字符串,为了简单起见就添加了英文和简体中文:
在对应的资源文件中添加:
frameworks\base\core\res\res\values\strings.xml
<!-- xiaoyuguang 20130616 -->
<!-- label for item that turns reboot in device options dialog -->
<string name="global_action_reboot">Reboot</string>
<string name="reboot_confirm"> Do you want to reboot your device?</string>
<!-- end xiaoyuguang 20130616 -->
frameworks\base\core\res\res\values-zh-rCN\strings.xml
<!-- xiaoyuguang 20130616 -->
<string name="global_action_reboot">重启</string>
<string name="reboot_confirm">您要重新启动您的设备吗?</string>
<!-- end xiaoyuguang 20130616-->
现在已经添加了好这些资源,但是现在还不能使用,此时编译会出现找不到该资源的错误,还需要在 frameworks\base\core\res\res\values\symbols.xml (在4.1中是在 frameworks\base\core\res\res\values\public.xml) 文件中进行资源声明:
<java-symbol type="string" name="global_action_reboot" />
<java-symbol type="string" name="reboot_confirm" />
至此重启功能添加完毕,在编译前执行./mk -t update-api (因为新添加了接口)。
当然这仅仅是从框架层上进行修改,其底层的具体实现暂时还未探讨,有时间在去看看了。