Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1373607
  • 博文数量: 478
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4833
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-28 11:12
文章分类

全部博文(478)

文章存档

2019年(1)

2018年(27)

2017年(21)

2016年(171)

2015年(258)

我的朋友

分类: Android平台

2015-07-28 23:19:38

http://blog.chinaunix.net/uid-21515470-id-1825152.html
在Android系统中,长按Power键默认会弹出对话框,可以你选择“静音”,“关机”,“重启”等功能。
弹出对话框的代码位于: 

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方法将会显示上面提到的显示“静音”,“关机”,“重启”等选项的对话框。 

继续跟踪showGlobalActionsDialog方法,代码如下:

void showGlobalActionsDialog() {
        if (mGlobalActions == null) {
            mGlobalActions = new GlobalActions(mContext);
        }
        final boolean keyguardShowing = mKeyguardMediator.isShowingAndNotHidden();
        mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());
        if (keyguardShowing) {
            // since it took two seconds of long press to bring this up,

            // poke the wake lock so they have some time to see the dialog.

            mKeyguardMediator.pokeWakelock();
        }
    }


GlobalActions相关的代码位于:

frameworks/policies/base/phone/com/android/internal/policy/impl/GlobalActions.java


其中,showDialog的代码如下:

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();
    }


showDialog中会调用createDialog方法,以创建相应的对话框。createDialog方法中,有如下代码: 

mItems = Lists.newArrayList(
                // silent mode

                mSilentModeToggle,
                // next: airplane mode

                //mAirplaneModeOn,

                // next: reboot

                new SinglePressAction(com.android.internal.R.drawable.ic_lock_power_off, R.string.global_action_reboot){

                    public void onPress() {
                        ShutdownThread.reboot(mContext, true);
                    }

                    public boolean showDuringKeyguard() {
                        return true;
                    }

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

                // 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;
                    }
                });


从代码中我们可以看出,如果选择上述对话框的“关机”选项之后,将会调用ShutdownThread的shutdown方法来关机,shutdown方法的第二个参数表示是否弹出询问对话框。 如果选择上述对话框的“重启”选项之后,将会调用ShutdownThread的reboot方法来重启,reboot方法的第二个参数表示是否弹出询问对话框。 

createDialog方法中还有如下代码: 

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);


其中,“dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                    WindowManager.LayoutParams.FLAG_BLUR_BEHIND);”语句的作用是添加相应的特效:即弹出对话框的时候,让对话框后面的背景模糊显示。如果不需要这样的特效,可以将相关语句注释掉即可。
其他特效:
1)FLAG_DIM_BEHIND:会让背景的亮度变暗。

再来看看ShutdownThread,其定义的源文件在:

frameworks/base/core/java/com/android/internal/app/ShutdownThread.java


如果选择上述对话框的“关机”选项之后,将会调用ShutdownThread的shutdown方法来关机。 如果选择上述对话框的“重启”选项之后,将会调用ShutdownThread的reboot方法来重启。shutdown方法和reboot方法的定义如下:

public static void shutdown(final Context context, boolean confirm) {
        shutdown(context, confirm, false);
    }

    public static void reboot(final Context context, boolean confirm) {
        shutdown(context, confirm, true);
    }


它们都会调用到public static void shutdown(final Context context, boolean confirm, final boolean reboot)
阅读(850) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~