Android4.4添加重启功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/asdf1225/article/details/35558743
需要改动的文件:
frameworks\base\core\res\res\values\strings.xml
frameworks\base\core\res\res\values-zh-rCN\strings.xml
frameworks\base\core\res\res\values\symbols.xml
frameworks\base\policy\src\com\android\internal\policy\impl\GlobalActions.java
frameworks\base\core\java\android\view\WindowManagerPolicy.java
frameworks\base\services\java\com\android\server\wm\windowManagerService.java
frameworks\base\services\java\com\android\server\power\ShutdownThread.java

首先在长按电源键的列表中添加重启按钮,在frameworks\base\policy\src\com\android\internal\policy\impl\GlobalActions.java文件中添加如下
        // 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.
                    Log.i(TAG, "[SHTDWN] onPress, request a clean shutdown");
                    mWindowManagerFuncs.shutdown(true);
                }

                public boolean onLongPress() {
                    Log.i(TAG, "[SHTDWN] onLongPress, request a reboot safe mode");
                    mWindowManagerFuncs.rebootSafeMode(true);
                    return true;
                }

                public boolean showDuringKeyguard() {
                    return true;
                }

                public boolean showBeforeProvisioning() {
                    return true;
                }
            });

	// add reboot
	mItems.add(new SinglePressAction(
			com.android.internal.R.drawable.ic_lock_power_off,
			R.string.global_action_reboot) {

		public void onPress() {
			Log.v(TAG, "[REBOOT] onPress, request a reboot without safe mode");
			mWindowManagerFuncs.reboot(true);
		}

		public boolean showDuringKeyguard() {
			return true;
		}

		public boolean showBeforeProvisioning() {
			return true;
		}
	});
<pre name="code" class="java">	// add reboot end
// one more thing: optionally add a list of users to switch to if (SystemProperties.getBoolean("fw.power_user_switcher", false)) { addUsersToMenu(mItems); } mAdapter = new MyAdapter(); AlertParams params = new AlertParams(mContext); params.mAdapter = mAdapter; params.mOnClickListener = this; params.mForceInverseBackground = true; GlobalActionsDialog dialog = new GlobalActionsDialog(mContext, params); dialog.setCanceledOnTouchOutside(false); // Handled by the custom class. dialog.getListView().setItemsCanFocus(true); dialog.getListView().setLongClickable(true); dialog.getListView().setOnItemLongClickListener( new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { return mAdapter.getItem(position).onLongPress(); } }); dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); dialog.setOnDismissListener(this); return dialog; }

在GlobalActionsDialog方法可以看 mItems.add这个方法是添加菜单选项的,该菜单的添加的第一个选项就是关机选项。可以仿照关机的Item添加一个重启的选项,如上面的代码所示;这样就解决了在长按的电源键弹出的对话框中添加一个重启选项了。当然这仅仅是添加一个显示而已,接下来就为这个选项添加逻辑控制代码了。首先在找到WindowManagerFuncs这个所在的位置,在frameworks\base\core\java\android\view\WindowManagerPolicy.java中
        /**
         * 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);
        // add reboot
        public void reboot(boolean confirm);

        /**
         * Return the window manager lock needed to correctly call "Lw" methods.
         */
        public Object getWindowManagerLock();

        /** Register a system listener for touch events */
        void registerPointerEventListener(PointerEventListener listener);

        /** Unregister a system listener for touch events */
        void unregisterPointerEventListener(PointerEventListener listener);

添加reboot方法。但这只是添加接口而已,它的具体实现在frameworks\base\services\java\com\android\server\wm\windowManagerService.java中
    // Called by window manager policy.  Not exposed externally.
    @Override
    public void shutdown(boolean confirm) {
        ShutdownThread.shutdown(mContext, confirm);
    }
<pre name="code" class="java">    //add reboot
    @Override
    public void reboot(boolean confirm){  
	Log.v(TAG, "reboot");
        ShutdownThread.reboot(mContext,null,confirm);   
    }

    // Called by window manager policy.  Not exposed externally.
    @Override
    public void rebootSafeMode(boolean confirm) {
        ShutdownThread.rebootSafeMode(mContext, confirm);
    }

同样在仿照关机的原理添加reboot的具体实现代码,既然在ShutdownThread这个类中提供了shutdown和rebootSafeMode的方法,那按理也应该有reboot的方法,或者类似reboot的方法。在frameworks\base\services\java\com\android\server\power\ShutdownThread.java中
    /**
     * 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.i(TAG, "[SHTDWN] reboot, reboot requested"
            + " reason=" + (reason != null ? reason : "null")
            + " confirm=" + (confirm ? "yes" : "no"));
        shutdownInner(context, confirm);
    }

    /**
     * Request a reboot into safe mode.  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 rebootSafeMode(final Context context, boolean confirm) {
        mReboot = true;
        mRebootSafeMode = true;
        mRebootReason = null;
        Log.i(TAG, "[SHTDWN] reboot, rebootSafeMode requested"
            + " reason=null"
            + " confirm=" + (confirm ? "yes" : "no"));
        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中
        final int longPressBehavior = context.getResources().getInteger(
                        com.android.internal.R.integer.config_longPressOnPowerBehavior);
        // reboot confirmation dialog
        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));

        Log.d(TAG, "Notifying thread to start shutdown longPressBehavior=" + longPressBehavior);
        // dumpStack to log the caller name
        Log.e(TAG, "[SHTDWN] My call stack is:");
        Thread.dumpStack();
修改为:
        final int longPressBehavior = context.getResources().getInteger(
                        com.android.internal.R.integer.config_longPressOnPowerBehavior);
        // change for reboot
		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));
		// end

        Log.d(TAG, "Notifying thread to start shutdown longPressBehavior=" + longPressBehavior);
        // dumpStack to log the caller name
        Log.e(TAG, "[SHTDWN] My call stack is:");
        Thread.dumpStack();

以及添加正在关机的判断将下面的代码
        if (confirm) {
            final CloseDialogReceiver closer = new CloseDialogReceiver(context);
            if (sConfirmDialog != null) {
                sConfirmDialog.dismiss();
            }
        // reboot confirmation dialog
        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)

                    .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            beginShutdownSequence(context);
                            Log.i(TAG, "[SHTDWN] shutdown, confirm=YES");
                        }
                    })

修改为:
 if (confirm) {
            final CloseDialogReceiver closer = new CloseDialogReceiver(context);
            if (sConfirmDialog != null) {
                sConfirmDialog.dismiss();
            }
            // change for reboot
			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)
	    // end
                    .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            beginShutdownSequence(context);
                            Log.i(TAG, "[SHTDWN] shutdown, confirm=YES");
                        }
                    })

至此关于代码部分的改动全部完成,接下就添加需要添加使用到的资源了,就是其中使用的字符串,为了简单起见就添加了英文和简体中文:
在对应的资源文件中添加:
frameworks\base\core\res\res\values\strings.xml
<!-- reboot -->
<string name="global_action_reboot">Reboot</string>
<string name="reboot_confirm"> Do you want to reboot your device?</string>

frameworks\base\core\res\res\values-zh-rCN\strings.xml
<string name="global_action_reboot">重启</string>
<string name="reboot_confirm">您要重新启动您的设备吗?</string>

现在已经添加了好这些资源,但是现在还不能使用,此时编译会出现找不到该资源的错误,还需要在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 (因为新添加了接口)。当然这仅仅是从框架层上进行修改。
Install: out/target/product/MCT02A_1/system/framework/services.jar
Install: out/target/product/MCT02A_1/system/framework/framework-res.apk
Install: out/target/product/MCT02A_1/system/framework/android.policy.jar
Install: out/target/product/MCT02A_1/system/framework/framework2.jar
Install: out/target/product/MCT02A_1/system/framework/framework.jar

展开阅读全文

没有更多推荐了,返回首页