|
【Android定制】定制设备重启功能
发表于 2014-11-11 17:29:38
浏览:13734
|
回复:1
打印
只看该作者
[复制链接]
楼主
本帖最后由 ZZP 于 2014-11-11 17:37 编辑
定制设备【重启】功能,需要确认两个问题:
1、关机对话框代码路径,如何添加【重启】选项
2、设备重启执行方式
带着以上两个问题,分别在Android源代码中进行跟踪消化:
针对问题1:
在源码frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java 中,里边定义了【关机】【静音】【飞行模式】等全局功能选项,其被实例化使用位于同目录下的PhoneWindowManager的showGlobalActionsDialog方法中,该方法被调用正是响应Power按键长按事件(mPowerLongPress)。
添加【重启】选项直接在GlobalActions中,参照【关机】选项代码格式添加即可。GlobalActions中添加代码:
+ mItems.add(
+ new SinglePressAction(
+ com.android.internal.R.drawable.ic_lock_power_off,
+ R.string.factorytest_reboot) {
+
+ public void onPress() {
+ // mWindowManagerFuncs.reboot(true);
+ // reboot
+ mWindowManagerFuncs.reboot(true);
+ }
+
+ public boolean onLongPress() {
+ return true;
+ }
+
+ public boolean showDuringKeyguard() {
+ return true;
+ }
+
+ public boolean showBeforeProvisioning() {
+ return true;
+ }
+ });
针对问题2:
回到GlobalActions中,先了解到【关机】功能是执行代码【 mWindowManagerFuncs.shutdown(true);】,接下来进一步跟踪代码WindowManagerFuncs类,发现其为接口WindowManagerPolicy(路径【frameworks/base/core/java/android/view/WindowManagerPolicy.java】)中定义,其接口实现类在WindowManagerService(路径【frameworks/base/services/java/com/android/server/wm/WindowManagerService.java】,简称WMS)中实现。
在WMS中,shutdown方法和rebootSafeMode方法的实现类如下:
- @Override
- public void shutdown(boolean confirm) {
- ShutdownThread.shutdown(mContext, confirm);
- }
- // Called by window manager policy. Not exposed externally.
- @Override
- public void rebootSafeMode(boolean confirm) {
- ShutdownThread.rebootSafeMode(mContext, confirm);
- }
复制代码
在这里了解到这些方法实际上是已经封装在ShutdownThread(路径【frameworks/base/services/java/com/android/server/power/ShutdownThread.java】)中,进一步查阅代码发现ShutdownThread类除了上边提到的关机和重启安全模式方法外,还有封装了重启(reboot)方法:
- public static void reboot(final Context context, String reason, boolean confirm) {
- mReboot = true;
- mRebootSafeMode = false;
- mRebootReason = reason;
- shutdownInner(context, confirm);
- }
复制代码
显而易见,这里可以用同样的方式将reboot方法添加到WindowManagerFuncs接口中,与关机方法一样进一步在WMS中进行引用封装,具体代码添加如下:
接口WindowManagerPolicy.java中修改:
public interface WindowManagerFuncs {
...
public void shutdown(boolean confirm);
public void rebootSafeMode(boolean confirm);
+ public void reboot(boolean confirm);
+ public void reboot(String reason, boolean confirm);
...
}
WMS中实现重启方法:
+ // Called by window manager policy. Not exposed externally.
+ @Override
+ public void reboot(boolean confirm) {
+ reboot(null, confirm);
+ }
+
+ // Called by window manager policy. Not exposed externally.
+ @Override
+ public void reboot(String reason,boolean confirm) {
+ ShutdownThread.reboot(mContext, reason, confirm);
+ }
|
|