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

ShortcutManager

public class ShortcutManager extends android.database.ContentObserver
Manages quick launch shortcuts by:
  • Keeping the local copy in sync with the database (this is an observer)
  • Returning a shortcut-matching intent to clients
  • Fields Summary
    private static final String
    TAG
    private static final int
    COLUMN_SHORTCUT
    private static final int
    COLUMN_INTENT
    private static final String[]
    sProjection
    private android.content.Context
    mContext
    private android.database.Cursor
    mCursor
    private android.util.SparseArray
    mShortcutIntents
    Map of a shortcut to its intent.
    Constructors Summary
    public ShortcutManager(android.content.Context context, android.os.Handler handler)

        
             
            super(handler);
            
            mContext = context;
            mShortcutIntents = new SparseArray<Intent>();
        
    Methods Summary
    public android.content.IntentgetIntent(android.view.KeyCharacterMap kcm, int keyCode, int metaState)
    Gets the shortcut intent for a given keycode+modifier. Make sure you strip whatever modifier is used for invoking shortcuts (for example, if 'Sym+A' should invoke a shortcut on 'A', you should strip the 'Sym' bit from the modifiers before calling this method.

    This will first try an exact match (with modifiers), and then try a match without modifiers (primary character on a key).

    param
    kcm The key character map of the device on which the key was pressed.
    param
    keyCode The key code.
    param
    metaState The meta state, omitting any modifiers that were used to invoke the shortcut.
    return
    The intent that matches the shortcut, or null if not found.

            Intent intent = null;
    
            // First try the exact keycode (with modifiers).
            int shortcut = kcm.get(keyCode, metaState);
            if (shortcut != 0) {
                intent = mShortcutIntents.get(shortcut);
            }
    
            // Next try the primary character on that key.
            if (intent == null) {
                shortcut = Character.toLowerCase(kcm.getDisplayLabel(keyCode));
                if (shortcut != 0) {
                    intent = mShortcutIntents.get(shortcut);
                }
            }
    
            return intent;
        
    public voidobserve()
    Observes the provider of shortcut+intents

            mCursor = mContext.getContentResolver().query(
                    Settings.Bookmarks.CONTENT_URI, sProjection, null, null, null);
            mCursor.registerContentObserver(this);
            updateShortcuts();
        
    public voidonChange(boolean selfChange)

            updateShortcuts();
        
    private voidupdateShortcuts()

            Cursor c = mCursor;
            if (!c.requery()) {
                Log.e(TAG, "ShortcutObserver could not re-query shortcuts.");
                return;
            }
    
            mShortcutIntents.clear();
            while (c.moveToNext()) {
                int shortcut = c.getInt(COLUMN_SHORTCUT);
                if (shortcut == 0) continue;
                String intentURI = c.getString(COLUMN_INTENT);
                Intent intent = null;
                try {
                    intent = Intent.getIntent(intentURI);
                } catch (URISyntaxException e) {
                    Log.w(TAG, "Intent URI for shortcut invalid.", e);
                }
                if (intent == null) continue;
                mShortcutIntents.put(shortcut, intent);
            }