FileDocCategorySizeDatePackage
ShortcutManager.javaAPI DocAndroid 1.5 API4339Wed May 06 22:42:06 BST 2009com.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(int keyCode, int modifiers)
    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
    keyCode The keycode of the key pushed.
    param
    modifiers The modifiers without any that are used for chording to invoke a shortcut.
    return
    The intent that matches the shortcut, or null if not found.

            KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.BUILT_IN_KEYBOARD);
            // First try the exact keycode (with modifiers)
            int shortcut = kcm.get(keyCode, modifiers);
            Intent intent = shortcut != 0 ? mShortcutIntents.get(shortcut) : null; 
            if (intent != null) return intent;
            
            // Next try the keycode without modifiers (the primary character on that key)
            shortcut = Character.toLowerCase(kcm.get(keyCode, 0));
            return shortcut != 0 ? mShortcutIntents.get(shortcut) : null;
        
    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);
            }