[置顶] android源码定制之初探--定制android关机界面

LouisWang

转载请注明出处:http://blog.csdn.net/louiswangbing/article/details/6688240

上一篇文章中讲到android源码定制要点,说了个大概的方法和方向,现在,就来实战一下。

在Android系统中,长按Power键默认会弹出对话框让你选择“飞行模式”,“静音”,“关机”等功能。如下图所示:

[置顶] android源码定制之初探--定制android关机界面_第1张图片

但这些功能都对Android-x86和其他终端产品就没什么必要了。本文就简单介绍下如何定制关机界面。

我的目标是长按Power键,将会关机,弹出“设备将要关机”选择对话框。如果可以选择“是”关机,和“否”返回系统。 

按照android源码定制要点中提到的,首先你要对整个系统有全面的了解,找到弹出原来这个选择框的代码,它在这里:

frameworks\policies\base\phone\com\android\internal\policy\impl\PhoneWindowManager.java  


显示对话框调用的代码如下:

    Runnable mPowerLongPress = new Runnable() {  
        public void run() {  
            mShouldTurnOffOnKeyUp = false;  
            performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);  
            sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);  
            showGlobalActionsDialog();  
        }  
    };  

调用showGlobalActionsDialog方法之后将会聚到有“飞行模式”、“静音”、“关机”等选项的对话框。

找到这里,我们就知道该做什么了!干掉它,换成我们想要的关机代码,就大功告成了!既然这样,事不宜迟,让我们赶快到showGloabalActionDialog方法中看看关机的部分在哪里!

showGlobalActionsDialog的实现部分在这里:

frameworks\policies\base\phone\com\android\internal\policy\impl\GlobalAction.java

我们进去看看:

    public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
        mKeyguardShowing = keyguardShowing;
        mDeviceProvisioned = isDeviceProvisioned;
        if (mDialog == null) {
            mStatusBar = (StatusBarManager)mContext.getSystemService(Context.STATUS_BAR_SERVICE);
            mDialog = createDialog();
        }
        prepareDialog();

        mStatusBar.disable(StatusBarManager.DISABLE_EXPAND);
        mDialog.show();
    }

我们可以很清楚的看到,这里新建了一个mDialog,然后prepare接着就show了它,那么,这个mDialog就是关键了,看看它是怎么被createDialog创建出来的吧,仍然在这个文件中:

    /**
     * Create the global actions dialog.
     * @return A new dialog.
     */
    private AlertDialog createDialog() {
        mSilentModeToggle = new ToggleAction(
                R.drawable.ic_lock_silent_mode,
                R.drawable.ic_lock_silent_mode_off,
                R.string.global_action_toggle_silent_mode,
                R.string.global_action_silent_mode_on_status,
                R.string.global_action_silent_mode_off_status) {

            void willCreate() {
                // XXX: FIXME: switch to ic_lock_vibrate_mode when available
                mEnabledIconResId = (Settings.System.getInt(mContext.getContentResolver(),
                        Settings.System.VIBRATE_IN_SILENT, 1) == 1)
                    ? R.drawable.ic_lock_silent_mode_vibrate
                    : R.drawable.ic_lock_silent_mode;
            }

            void onToggle(boolean on) {
                if (on) {
                    mAudioManager.setRingerMode((Settings.System.getInt(mContext.getContentResolver(),
                        Settings.System.VIBRATE_IN_SILENT, 1) == 1)
                        ? AudioManager.RINGER_MODE_VIBRATE
                        : AudioManager.RINGER_MODE_SILENT);
                } else {
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                }
            }

            public boolean showDuringKeyguard() {
                return true;
            }

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

        mAirplaneModeOn = new ToggleAction(
                R.drawable.ic_lock_airplane_mode,
                R.drawable.ic_lock_airplane_mode_off,
                R.string.global_actions_toggle_airplane_mode,
                R.string.global_actions_airplane_mode_on_status,
                R.string.global_actions_airplane_mode_off_status) {

            void onToggle(boolean on) {
                if (Boolean.parseBoolean(
                        SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
                    mIsWaitingForEcmExit = true;
                    // Launch ECM exit dialog
                    Intent ecmDialogIntent =
                            new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
                    ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(ecmDialogIntent);
                } else {
                    changeAirplaneModeSystemSetting(on);
                }
            }

            @Override
            protected void changeStateFromPress(boolean buttonOn) {
                // In ECM mode airplane state cannot be changed
                if (!(Boolean.parseBoolean(
                        SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
                    mState = buttonOn ? State.TurningOn : State.TurningOff;
                    mAirplaneState = mState;
                }
            }

            public boolean showDuringKeyguard() {
                return true;
            }

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

        mItems = Lists.newArrayList(
                // silent mode
                mSilentModeToggle,
                // next: airplane mode
                mAirplaneModeOn,
                // last: power off
                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. ShutdownThread.shutdown(mContext, true); }

                    public boolean showDuringKeyguard() {
                        return true;
                    }

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

        mAdapter = new MyAdapter();

        final AlertDialog.Builder ab = new AlertDialog.Builder(mContext);

        ab.setAdapter(mAdapter, this)
                .setInverseBackgroundForced(true)
                .setTitle(R.string.global_actions);

        final AlertDialog dialog = ab.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
        if (!mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_sf_slowBlur)) {
            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                    WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        }

        dialog.setOnDismissListener(this);

        return dialog;
    }

看看我们发现了什么!!蓝色的部分就是关机调用的函数了!!shutdown方法的第二个参数标识是否弹出询问对话框。你可以选择需要(true)或者不需要(false)。这里我保守一点,还是选个true吧,万一不小心按到关机键呢,呵呵。。。

也就是说,只要我们用

ShutdownThread.shutdown(mContext, true);

替换掉前面的

showGlobalActionsDialog();  

就可以大功告成了!还等什么!我们修改

frameworks\policies\base\phone\com\android\internal\policy\impl\PhoneWindowManager.java  

的源代码如下:

    Runnable mPowerLongPress = new Runnable() {
        public void run() {
            mShouldTurnOffOnKeyUp = false;
            performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
            sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
            //showGlobalActionsDialog();
            ShutdownThread.shutdown(mContext, true); 
        }
    };

好了,大功告成了!!

是不是就这样完了呢?发现编译不过。。。

细节很重要!!

原来ShutdownThread.shutdown(mContext, true)的引用包没加进来!!幸好有gcc。。。

import com.android.internal.app.ShutdownThread;

将上面这个包加到
frameworks\policies\base\phone\com\android\internal\policy\impl\PhoneWindowManager.java  

中,再次编译,通过,YES!

看看我们的战果吧:

[置顶] android源码定制之初探--定制android关机界面_第2张图片

是不是感觉到源码定制的快感和成就感了呢?

这仅仅只是个开始,好戏还在后头呢!!哈哈


   

你可能感兴趣的:(android,null,System,dialog,action,frameworks)