首先从PhoneWindowsManager.java看起,这里会调用
private void interceptPowerKeyDown(boolean handled) {
mPowerKeyHandled = handled;
if (!handled) {
mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
}
}
从而进入mPowerLongPress,接着看mPowerLongPress,在这里调用了showGlobalActionsDialog()方法,
private final Runnable mPowerLongPress = new Runnable() {
@Override
public void run() {
// The context isn't read
if (mLongPressOnPowerBehavior < 0) {
mLongPressOnPowerBehavior = mContext.getResources().getInteger(
com.android.internal.R.integer.config_longPressOnPowerBehavior);
}
...
switch (resolvedBehavior) {
case LONG_PRESS_POWER_NOTHING:
break;
case LONG_PRESS_POWER_GLOBAL_ACTIONS:
mPowerKeyHandled = true;
if (!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false)) {
performAuditoryFeedbackForAccessibilityIfNeed();
}
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
showGlobalActionsDialog();
break;
...
}
}
};
showGlobalActionsDialog方法中又调用了mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());
void showGlobalActionsDialog() {
if (mGlobalActions == null) {
mGlobalActions = new GlobalActions(mContext, mWindowManagerFuncs);
}
final boolean keyguardShowing = keyguardIsShowingTq();
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.userActivity();
}
}
这个方法,至此我们看到了长按关机键弹框的关键类GlobalActions.java,弹框中所有的数据都是在GlobalActionsDialog createDialog()这个方法被创建的,
GlobalActionsDialog方法可以看 mItems.add这个方法是添加菜单选项的,该菜单的添加的第一个选项就是关机选项。
以添加飞行模式为例 mItems.add(mAirplaneModeOn);
这里的mAirplaneModeOn是一个实现了Action的一个对象,我们可以仿照飞行模式添加一个提示语的选项(项目中的需求),
我们只需要定义一个类实现Action,class PromptAction implements Action ,在create()方法中,绑定自己的布局,
private class PromptAction implements Action{
public CharSequence getLabelForAccessibility(Context context) {
return null;
}
@Override
public View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
View view=inflater.inflate(R.layout.promt_action_layout,parent,false);
return view;
}
@Override
public void onPress() {
}
@Override
public boolean showDuringKeyguard() {
return false;
}
@Override
public boolean showBeforeProvisioning() {
return false;
}
@Override
public boolean isEnabled() {
return false;
}
}
需要注意的是,实现Action的一个方法
@Override
public boolean showDuringKeyguard() {
return false;
}
这里的返回值决定此项是否会出现在锁屏界面的弹框里。之后我们在添加飞行模式的地方,加上如下代码:
else if(GLOBAL_ACTION_KEY_PROMPT.equals(actionKey)){
mItems.add(new PromptAction());
}
最后,在frameworks/base/core/res/res/values/config.xml中config_globalActionsList string-array中添加prompt
<string-array translatable="false" name="config_globalActionsList">
<item>power</item>
<item>reboot</item>
<item>airplane</item>
<item>screenshot</item>
<item>data_connection</item>
<item>silent</item>
<item>bugreport</item>
<item>users</item>
<item>prompt</item>
</string-array>
这就实现了在长按关机弹框中添加提示语的功能。