去掉快捷键拉起本地浏览器功能,解决学校需求,打开终端防修改后响应快捷键win+b拉起了浏览器问题
需求:按菜单键后,在任何应用界面弹出指定菜单
实现方案1:
(1)配置./device/google/atv/overlay/frameworks/base/core/res/res/xml/global_keys.xml,写上自己需要配置keycode和componet
(2)写一个静态广播,并过action="android.intent.action.GLOBAL_BUTTON".
<receiver android:name=".receiver.SWGlobalKeyReceiver">
<intent-filter>
<action android:name="android.intent.action.GLOBAL_BUTTON" />
</intent-filter>
</receiver>
(3)GlobalKeyManager中对全局键的处理/frameworks/base/services/core/java/com/android/server/policy/GlobalKeyManager.java
1)解析global_keys.xml,开机起来就是进行解析
private void loadGlobalKeys(Context context) {
XmlResourceParser parser = null;
try {
parser = context.getResources().getXml(com.android.internal.R.xml.global_keys);
XmlUtils.beginDocument(parser, TAG_GLOBAL_KEYS);
int version = parser.getAttributeIntValue(null, ATTR_VERSION, 0);
if (GLOBAL_KEY_FILE_VERSION == version) {
while (true) {
XmlUtils.nextElement(parser);
String element = parser.getName();
if (element == null) {
break;
}
if (TAG_KEY.equals(element)) {
String keyCodeName = parser.getAttributeValue(null, ATTR_KEY_CODE);
String componentName = parser.getAttributeValue(null, ATTR_COMPONENT);
int keyCode = KeyEvent.keyCodeFromString(keyCodeName);
if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
mKeyMapping.put(keyCode, ComponentName.unflattenFromString(
componentName));
}
}
}
}
} catch (Resources.NotFoundException e) {
Log.w(TAG, "global keys file not found", e);
} catch (XmlPullParserException e) {
Log.w(TAG, "XML parser exception reading global keys file", e);
} catch (IOException e) {
Log.w(TAG, "I/O exception reading global keys file", e);
} finally {
if (parser != null) {
parser.close();
}
}
}
2)按键触发后调用handleGlobalKey发送响应广播给comptent
boolean handleGlobalKey(Context context, int keyCode, KeyEvent event) {
if (mKeyMapping.size() > 0) {
ComponentName component = mKeyMapping.get(keyCode);
if (component != null) {
Intent intent = new Intent(Intent.ACTION_GLOBAL_BUTTON)
.setComponent(component)
.putExtra(Intent.EXTRA_KEY_EVENT, event);
context.sendBroadcastAsUser(intent, UserHandle.CURRENT, null);
return true;
}
}
return false;
}
实现方案2:
在/home/zhangyulong/wgd/Nova/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
对按键进行接收,并发送全局广播处理。
/** {@inheritDoc} */
@Override
public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
final boolean keyguardOn = keyguardOn();
final int keyCode = event.getKeyCode();
final int repeatCount = event.getRepeatCount();
final int metaState = event.getMetaState();
final int flags = event.getFlags();
final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
final boolean up = event.getAction() == KeyEvent.ACTION_UP;
final boolean canceled = event.isCanceled();
if (DEBUG_INPUT) {
Log.d(TAG, "interceptKeyTi keyCode=" + keyCode + " down=" + down + " repeatCount="
+ repeatCount + " keyguardOn=" + keyguardOn + " mHomePressed=" + mHomePressed
+ " canceled=" + canceled);
}
//ADD BY wgd
if(down) {
downCount = repeatCount;
}
Log.d(TAG,"KEY_PRESS_DOWN==downCount==" + downCount);
try{
if(up && downCount==0) {
downCount =-1;
String action = "net.sunniwell.action.KEY_PRESS_DOWN";
Intent keypressDown = new Intent(action);
keypressDown.putExtra("keyCode", event.getKeyCode());
mContext.sendBroadcast(keypressDown);
Log.d(TAG,"send broadcast key press down");
}
}catch(Exception e){
Log.d(TAG,"KEY_PRESS_DOWN==downCount==Exception==" + e);
e.printStackTrace();
}
if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) {
try{
Log.d(TAG, "FLAG_LONG_PRESS=="+event.getFlags());
if(down){
String actionLong = "net.sunniwell.action.KEY_PRESS_DOWN_LONG";
Intent longKeypressDown = new Intent(actionLong);
longKeypressDown.putExtra("keyCode", event.getKeyCode());
mContext.sendBroadcast(longKeypressDown);
Log.d(TAG,"send broadcast key long press down");
}
}catch(Exception e){
e.printStackTrace();
}
}
// If we think we might have a volume down & power key chord on the way
// but we're not sure, then tell the dispatcher to wait a little while and
// try again later before dispatching.
if (mScreenshotChordEnabled && (flags & KeyEvent.FLAG_FALLBACK) == 0) {
if (mScreenshotChordVolumeDownKeyTriggered && !mScreenshotChordPowerKeyTriggered) {
final long now = SystemClock.uptimeMillis();
final long timeoutTime = mScreenshotChordVolumeDownKeyTime
+ SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS;
if (now < timeoutTime) {
return timeoutTime - now;
}
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
&& mScreenshotChordVolumeDownKeyConsumed) {
if (!down) {
mScreenshotChordVolumeDownKeyConsumed = false;
}
return -1;
}
}
// If an accessibility shortcut might be partially complete, hold off dispatching until we
// know if it is complete or not
if (mAccessibilityShortcutController.isAccessibilityShortcutAvailable(false)
&& (flags & KeyEvent.FLAG_FALLBACK) == 0) {
if (mScreenshotChordVolumeDownKeyTriggered ^ mA11yShortcutChordVolumeUpKeyTriggered) {
final long now = SystemClock.uptimeMillis();
final long timeoutTime = (mScreenshotChordVolumeDownKeyTriggered
? mScreenshotChordVolumeDownKeyTime : mA11yShortcutChordVolumeUpKeyTime)
+ SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS;
if (now < timeoutTime) {
return timeoutTime - now;
}
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && mScreenshotChordVolumeDownKeyConsumed) {
if (!down) {
mScreenshotChordVolumeDownKeyConsumed = false;
}
return -1;
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mA11yShortcutChordVolumeUpKeyConsumed) {
if (!down) {
mA11yShortcutChordVolumeUpKeyConsumed = false;
}
return -1;
}
}
// Cancel any pending meta actions if we see any other keys being pressed between the down
// of the meta key and its corresponding up.
if (mPendingMetaAction && !KeyEvent.isMetaKey(keyCode)) {
mPendingMetaAction = false;
}
// Any key that is not Alt or Meta cancels Caps Lock combo tracking.
if (mPendingCapsLockToggle && !KeyEvent.isMetaKey(keyCode) && !KeyEvent.isAltKey(keyCode)) {
mPendingCapsLockToggle = false;
}
// First we always handle the home key here, so applications
// can never break it, although if keyguard is on, we do let
// it handle it, because that gives us the correct 5 second
// timeout.
if (keyCode == KeyEvent.KEYCODE_HOME) {
// If we have released the home key, and didn't do anything else
// while it was pressed, then it is time to go home!
if (!down) {
cancelPreloadRecentApps();
if (mHasFeatureLeanback) {
// Clear flags
mAccessibilityTvKey2Pressed = down;
}
mHomePressed = false;
if (mHomeConsumed) {
mHomeConsumed = false;
return -1;
}
if (canceled) {
Log.i(TAG, "Ignoring HOME; event canceled.");
return -1;
}
// Delay handling home if a double-tap is possible.
if (mDoubleTapOnHomeBehavior != DOUBLE_TAP_HOME_NOTHING) {
mHandler.removeCallbacks(mHomeDoubleTapTimeoutRunnable); // just in case
mHomeDoubleTapPending = true;
mHandler.postDelayed(mHomeDoubleTapTimeoutRunnable,
ViewConfiguration.getDoubleTapTimeout());
return -1;
}
handleShortPressOnHome();
return -1;
}
// If a system window has focus, then it doesn't make sense
// right now to interact with applications.
WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
if (attrs != null) {
final int type = attrs.type;
if (type == TYPE_KEYGUARD_DIALOG
|| (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
// the "app" is keyguard, so give it the key
return 0;
}
final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
for (int i=0; i<typeCount; i++) {
if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
// don't do anything, but also don't pass it to the app
return -1;
}
}
}
// Remember that home is pressed and handle special actions.
if (repeatCount == 0) {
mHomePressed = true;
if (mHomeDoubleTapPending) {
mHomeDoubleTapPending = false;
mHandler.removeCallbacks(mHomeDoubleTapTimeoutRunnable);
handleDoubleTapOnHome();
} else if (mDoubleTapOnHomeBehavior == DOUBLE_TAP_HOME_RECENT_SYSTEM_UI) {
preloadRecentApps();
}
} else if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) {
if (mHasFeatureLeanback) {
mAccessibilityTvKey2Pressed = down;
if (interceptAccessibilityGestureTv()) {
return -1;
}
}
if (!keyguardOn) {
handleLongPressOnHome(event.getDeviceId());
}
}
return -1;
} else if (keyCode == KeyEvent.KEYCODE_MENU) {
// Hijack modified menu keys for debugging features
final int chordBug = KeyEvent.META_SHIFT_ON;
if (down && repeatCount == 0) {
if (mEnableShiftMenuBugReports && (metaState & chordBug) == chordBug) {
Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT,
null, null, null, 0, null, null);
return -1;
}
}
} else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
if (down) {
if (repeatCount == 0) {
mSearchKeyShortcutPending = true;
mConsumeSearchKeyUp = false;
}
} else {
mSearchKeyShortcutPending = false;
if (mConsumeSearchKeyUp) {
mConsumeSearchKeyUp = false;
return -1;
}
}
return 0;
} else if (keyCode == KeyEvent.KEYCODE_APP_SWITCH) {
if (!keyguardOn) {
if (down && repeatCount == 0) {
preloadRecentApps();
} else if (!down) {
toggleRecentApps();
}
}
return -1;
} else if (keyCode == KeyEvent.KEYCODE_N && event.isMetaPressed()) {
if (down) {
IStatusBarService service = getStatusBarService();
if (service != null) {
try {
service.expandNotificationsPanel();
} catch (RemoteException e) {
// do nothing.
}
}
}
} else if (keyCode == KeyEvent.KEYCODE_S && event.isMetaPressed()
&& event.isCtrlPressed()) {
if (down && repeatCount == 0) {
int type = event.isShiftPressed() ? TAKE_SCREENSHOT_SELECTED_REGION
: TAKE_SCREENSHOT_FULLSCREEN;
mScreenshotRunnable.setScreenshotType(type);
mHandler.post(mScreenshotRunnable);
return -1;
}
} else if (keyCode == KeyEvent.KEYCODE_SLASH && event.isMetaPressed()) {
if (down && repeatCount == 0 && !isKeyguardLocked()) {
toggleKeyboardShortcutsMenu(event.getDeviceId());
}
} else if (keyCode == KeyEvent.KEYCODE_ASSIST) {
if (down) {
if (repeatCount == 0) {
mAssistKeyLongPressed = false;
} else if (repeatCount == 1) {
mAssistKeyLongPressed = true;
if (!keyguardOn) {
launchAssistLongPressAction();
}
}
} else {
if (mAssistKeyLongPressed) {
mAssistKeyLongPressed = false;
} else {
if (!keyguardOn) {
launchAssistAction(null, event.getDeviceId());
}
}
}
return -1;
} else if (keyCode == KeyEvent.KEYCODE_VOICE_ASSIST) {
if (!down) {
Intent voiceIntent;
if (!keyguardOn) {
voiceIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
} else {
IDeviceIdleController dic = IDeviceIdleController.Stub.asInterface(
ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER));
if (dic != null) {
try {
dic.exitIdle("voice-search");
} catch (RemoteException e) {
}
}
voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE, true);
}
startActivityAsUser(voiceIntent, UserHandle.CURRENT_OR_SELF);
}
} else if (keyCode == KeyEvent.KEYCODE_SYSRQ) {
if (down && repeatCount == 0) {
mScreenshotRunnable.setScreenshotType(TAKE_SCREENSHOT_FULLSCREEN);
mHandler.post(mScreenshotRunnable);
}
return -1;
} else if (keyCode == KeyEvent.KEYCODE_BRIGHTNESS_UP
|| keyCode == KeyEvent.KEYCODE_BRIGHTNESS_DOWN) {
if (down) {
int direction = keyCode == KeyEvent.KEYCODE_BRIGHTNESS_UP ? 1 : -1;
// Disable autobrightness if it's on
int auto = Settings.System.getIntForUser(
mContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL,
UserHandle.USER_CURRENT_OR_SELF);
if (auto != 0) {
Settings.System.putIntForUser(mContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL,
UserHandle.USER_CURRENT_OR_SELF);
}
int min = mPowerManager.getMinimumScreenBrightnessSetting();
int max = mPowerManager.getMaximumScreenBrightnessSetting();
int step = (max - min + BRIGHTNESS_STEPS - 1) / BRIGHTNESS_STEPS * direction;
int brightness = Settings.System.getIntForUser(mContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS,
mPowerManager.getDefaultScreenBrightnessSetting(),
UserHandle.USER_CURRENT_OR_SELF);
brightness += step;
// Make sure we don't go beyond the limits.
brightness = Math.min(max, brightness);
brightness = Math.max(min, brightness);
Settings.System.putIntForUser(mContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, brightness,
UserHandle.USER_CURRENT_OR_SELF);
startActivityAsUser(new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG),
UserHandle.CURRENT_OR_SELF);
}
return -1;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP
|| keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
if (mUseTvRouting || mHandleVolumeKeysInWM) {
// On TVs or when the configuration is enabled, volume keys never
// go to the foreground app.
dispatchDirectAudioEvent(event);
return -1;
}
// If the device is in Vr mode, drop the volume keys and don't
// forward it to the application/dispatch the audio event.
if (mPersistentVrModeEnabled) {
return -1;
}
} else if (keyCode == KeyEvent.KEYCODE_TAB && event.isMetaPressed()) {
// Pass through keyboard navigation keys.
return 0;
} else if (mHasFeatureLeanback && interceptBugreportGestureTv(keyCode, down)) {
return -1;
} else if (mHasFeatureLeanback && keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
mAccessibilityTvKey1Pressed = down;
if (interceptAccessibilityGestureTv()) {
return -1;
}
}
// Toggle Caps Lock on META-ALT.
boolean actionTriggered = false;
if (KeyEvent.isModifierKey(keyCode)) {
if (!mPendingCapsLockToggle) {
// Start tracking meta state for combo.
mInitialMetaState = mMetaState;
mPendingCapsLockToggle = true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {
int altOnMask = mMetaState & KeyEvent.META_ALT_MASK;
int metaOnMask = mMetaState & KeyEvent.META_META_MASK;
// Check for Caps Lock toggle
if ((metaOnMask != 0) && (altOnMask != 0)) {
// Check if nothing else is pressed
if (mInitialMetaState == (mMetaState ^ (altOnMask | metaOnMask))) {
// Handle Caps Lock Toggle
mInputManagerInternal.toggleCapsLock(event.getDeviceId());
actionTriggered = true;
}
}
// Always stop tracking when key goes up.
mPendingCapsLockToggle = false;
}
}
// Store current meta state to be able to evaluate it later.
mMetaState = metaState;
if (actionTriggered) {
return -1;
}
if (KeyEvent.isMetaKey(keyCode)) {
if (down) {
mPendingMetaAction = true;
} else if (mPendingMetaAction) {
launchAssistAction(Intent.EXTRA_ASSIST_INPUT_HINT_KEYBOARD, event.getDeviceId());
}
return -1;
}
// Shortcuts are invoked through Search+key, so intercept those here
// Any printing key that is chorded with Search should be consumed
// even if no shortcut was invoked. This prevents text from being
// inadvertently inserted when using a keyboard that has built-in macro
// shortcut keys (that emit Search+x) and some of them are not registered.
if (mSearchKeyShortcutPending) {
final KeyCharacterMap kcm = event.getKeyCharacterMap();
if (kcm.isPrintingKey(keyCode)) {
mConsumeSearchKeyUp = true;
mSearchKeyShortcutPending = false;
if (down && repeatCount == 0 && !keyguardOn) {
Intent shortcutIntent = mShortcutManager.getIntent(kcm, keyCode, metaState);
if (shortcutIntent != null) {
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivityAsUser(shortcutIntent, UserHandle.CURRENT);
dismissKeyboardShortcutsMenu();
} catch (ActivityNotFoundException ex) {
Slog.w(TAG, "Dropping shortcut key combination because "
+ "the activity to which it is registered was not found: "
+ "SEARCH+" + KeyEvent.keyCodeToString(keyCode), ex);
}
} else {
Slog.i(TAG, "Dropping unregistered shortcut key combination: "
+ "SEARCH+" + KeyEvent.keyCodeToString(keyCode));
}
}
return -1;
}
}