FileDocCategorySizeDatePackage
GlobalKeyManager.javaAPI DocAndroid 5.1 API5492Thu Mar 12 22:22:42 GMT 2015com.android.internal.policy.impl

GlobalKeyManager

public final class GlobalKeyManager extends Object
Stores a mapping of global keys.

A global key will NOT go to the foreground application and instead only ever be sent via targeted broadcast to the specified component. The action of the intent will be {@link Intent#ACTION_GLOBAL_BUTTON} and the KeyEvent will be included in the intent with {@link Intent#EXTRA_KEY_EVENT}.

Fields Summary
private static final String
TAG
private static final String
TAG_GLOBAL_KEYS
private static final String
ATTR_VERSION
private static final String
TAG_KEY
private static final String
ATTR_KEY_CODE
private static final String
ATTR_COMPONENT
private static final int
GLOBAL_KEY_FILE_VERSION
private android.util.SparseArray
mKeyMapping
Constructors Summary
public GlobalKeyManager(android.content.Context context)


       
        mKeyMapping = new SparseArray<ComponentName>();
        loadGlobalKeys(context);
    
Methods Summary
public voiddump(java.lang.String prefix, java.io.PrintWriter pw)

        final int numKeys = mKeyMapping.size();
        if (numKeys == 0) {
            pw.print(prefix); pw.println("mKeyMapping.size=0");
            return;
        }
        pw.print(prefix); pw.println("mKeyMapping={");
        for (int i = 0; i < numKeys; ++i) {
            pw.print("  ");
            pw.print(prefix);
            pw.print(KeyEvent.keyCodeToString(mKeyMapping.keyAt(i)));
            pw.print("=");
            pw.println(mKeyMapping.valueAt(i).flattenToString());
        }
        pw.print(prefix); pw.println("}");
    
booleanhandleGlobalKey(android.content.Context context, int keyCode, android.view.KeyEvent event)
Broadcasts an intent if the keycode is part of the global key mapping.

param
context context used to broadcast the event
param
keyCode keyCode which triggered this function
param
event keyEvent which trigged this function
return
{@code true} if this was handled

        if (mKeyMapping.size() > 0) {
            ComponentName component = mKeyMapping.get(keyCode);
            if (component != null) {
                Intent intent = new Intent(Intent.ACTION_GLOBAL_BUTTON)
                        .setComponent(component)
                        .setFlags(Intent.FLAG_RECEIVER_FOREGROUND)
                        .putExtra(Intent.EXTRA_KEY_EVENT, event);
                context.sendBroadcastAsUser(intent, UserHandle.CURRENT, null);
                return true;
            }
        }
        return false;
    
private voidloadGlobalKeys(android.content.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();
            }
        }
    
booleanshouldHandleGlobalKey(int keyCode, android.view.KeyEvent event)
Returns {@code true} if the key will be handled globally.

        return mKeyMapping.get(keyCode) != null;