FileDocCategorySizeDatePackage
BookmarkPicker.javaAPI DocAndroid 1.5 API11969Wed May 06 22:42:48 BST 2009com.android.settings.quicklaunch

BookmarkPicker

public class BookmarkPicker extends android.app.ListActivity implements SimpleAdapter.ViewBinder
Activity to pick a bookmark that will be returned to the caller.

Currently, bookmarks are either:

  • Activities that are in the launcher
  • Activities that are within an app that is capable of being launched with the {@link Intent#ACTION_CREATE_SHORTCUT}.
  • Fields Summary
    private static final String
    TAG
    public static final String
    EXTRA_TITLE
    Extra in the returned intent from this activity.
    public static final String
    EXTRA_SHORTCUT
    Extra that should be provided, and will be returned.
    private static final int
    REQUEST_CREATE_SHORTCUT
    The request code for the screen to create a bookmark that is WITHIN an application. For example, Gmail can return a bookmark for the inbox folder.
    private static android.content.Intent
    sLaunchIntent
    Intent used to get all the activities that are launch-able
    private static android.content.Intent
    sShortcutIntent
    Intent used to get all the activities that are {@link #REQUEST_CREATE_SHORTCUT}-able
    private List
    mResolveList
    List of ResolveInfo for activities that we can bookmark (either directly to the activity, or by launching the activity and it returning a bookmark WITHIN that application).
    private static final String
    KEY_TITLE
    private static final String
    KEY_RESOLVE_INFO
    private static final String[]
    sKeys
    private static final int[]
    sResourceIds
    private android.widget.SimpleAdapter
    mMyAdapter
    private static final int
    DISPLAY_MODE_LAUNCH
    Display those activities that are launch-able
    private static final int
    DISPLAY_MODE_SHORTCUT
    Display those activities that are able to have bookmarks WITHIN the application
    private int
    mDisplayMode
    private android.os.Handler
    mUiHandler
    Constructors Summary
    Methods Summary
    private android.widget.SimpleAdaptercreateResolveAdapter(java.util.List list)

            SimpleAdapter adapter = new SimpleAdapter(this, list,
                    R.layout.bookmark_picker_item, sKeys, sResourceIds);
            adapter.setViewBinder(this);
            return adapter;
        
    private voidensureIntents()

            if (sLaunchIntent == null) {
                sLaunchIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
                sShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
            }
        
    private voidfillAdapterList(java.util.List list, java.util.List resolveList)

            list.clear();
            int resolveListSize = resolveList.size();
            for (int i = 0; i < resolveListSize; i++) {
                ResolveInfo info = resolveList.get(i);
                /*
                 * Simple adapter craziness. For each item, we need to create a map
                 * from a key to its value (the value can be any object--the view
                 * binder will take care of filling the View with a representation
                 * of that object).
                 */
                Map<String, Object> map = new TreeMap<String, Object>();
                map.put(KEY_TITLE, getResolveInfoTitle(info));
                map.put(KEY_RESOLVE_INFO, info);
                list.add(map);
            }
        
    private voidfillResolveList(java.util.List list)
    Gets all activities matching our current display mode.

    param
    list The list to fill.

            ensureIntents();
            PackageManager pm = getPackageManager();
            list.clear();
            
            if (mDisplayMode == DISPLAY_MODE_LAUNCH) {
                list.addAll(pm.queryIntentActivities(sLaunchIntent, 0));
            } else if (mDisplayMode == DISPLAY_MODE_SHORTCUT) {
                list.addAll(pm.queryIntentActivities(sShortcutIntent, 0)); 
            }
        
    private voidfinish(android.content.Intent intent, java.lang.String title)
    Finishes the activity and returns the given data.

            // Give back what was given to us (it will have the shortcut, for example)
            intent.putExtras(getIntent());
            // Put our information
            intent.putExtra(EXTRA_TITLE, title);
            setResult(RESULT_OK, intent);
            finish();
        
    private static android.content.IntentgetIntentForResolveInfo(android.content.pm.ResolveInfo info, java.lang.String action)

            Intent intent = new Intent(action);
            ActivityInfo ai = info.activityInfo;
            intent.setClassName(ai.packageName, ai.name);
            return intent;
        
    private java.lang.StringgetResolveInfoTitle(android.content.pm.ResolveInfo info)
    Get the title for a resolve info.

            CharSequence label = info.loadLabel(getPackageManager());
            if (label == null) label = info.activityInfo.name;
            return label != null ? label.toString() : null;
        
    protected voidonActivityResult(int requestCode, int resultCode, android.content.Intent data)

            if (resultCode != RESULT_OK) {
                return;
            }
            
            switch (requestCode) {
                
                case REQUEST_CREATE_SHORTCUT:
                    if (data != null) {
                        finish((Intent) data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT),
                                data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME));
                    }
                    break;
                    
                default:
                    super.onActivityResult(requestCode, resultCode, data);
                    break;
            }
        
    protected voidonCreate(android.os.Bundle savedInstanceState)

        
        
            
            super.onCreate(savedInstanceState);
    
            updateListAndAdapter();
        
    public booleanonCreateOptionsMenu(android.view.Menu menu)

            menu.add(0, DISPLAY_MODE_LAUNCH, 0, R.string.quick_launch_display_mode_applications)
                    .setIcon(com.android.internal.R.drawable.ic_menu_archive);
            menu.add(0, DISPLAY_MODE_SHORTCUT, 0, R.string.quick_launch_display_mode_shortcuts)
                    .setIcon(com.android.internal.R.drawable.ic_menu_goto);
            return true;
        
    protected voidonListItemClick(android.widget.ListView l, android.view.View v, int position, long id)

            if (position >= mResolveList.size()) return;
    
            ResolveInfo info = mResolveList.get(position);
            
            switch (mDisplayMode) {
    
                case DISPLAY_MODE_LAUNCH: 
                    // We can go ahead and return the clicked info's intent
                    Intent intent = getIntentForResolveInfo(info, Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    finish(intent, getResolveInfoTitle(info));
                    break;
    
                case DISPLAY_MODE_SHORTCUT:
                    // Start the shortcut activity so the user can pick the actual intent
                    // (example: Gmail's shortcut activity shows a list of mailboxes)
                    startShortcutActivity(info);
                    break;
            }
            
        
    public booleanonOptionsItemSelected(android.view.MenuItem item)

            
            switch (item.getItemId()) {
    
                case DISPLAY_MODE_LAUNCH: 
                    mDisplayMode = DISPLAY_MODE_LAUNCH;
                    break;
                
                case DISPLAY_MODE_SHORTCUT:
                    mDisplayMode = DISPLAY_MODE_SHORTCUT;
                    break;
                
                default:
                    return false;
            }
            
            updateListAndAdapter();
            return true;
        
    public booleanonPrepareOptionsMenu(android.view.Menu menu)

            menu.findItem(DISPLAY_MODE_LAUNCH).setVisible(mDisplayMode != DISPLAY_MODE_LAUNCH);
            menu.findItem(DISPLAY_MODE_SHORTCUT).setVisible(mDisplayMode != DISPLAY_MODE_SHORTCUT);
            return true;
        
    public booleansetViewValue(android.view.View view, java.lang.Object data, java.lang.String textRepresentation)
    {@inheritDoc}

            if (view.getId() == R.id.icon) {
                Drawable icon = ((ResolveInfo) data).loadIcon(getPackageManager());
                if (icon != null) {
                    ((ImageView) view).setImageDrawable(icon);
                }
                return true;
            } else {
                return false;
            }
        
    private voidstartShortcutActivity(android.content.pm.ResolveInfo info)
    Starts an activity to get a shortcut.

    For example, Gmail has an activity that lists the available labels. It returns a shortcut intent for going directly to this label.

            Intent intent = getIntentForResolveInfo(info, Intent.ACTION_CREATE_SHORTCUT);
            startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
            
            // Will get a callback to onActivityResult
        
    private voidupdateAdapterToUseNewLists(java.util.ArrayList newAdapterList, java.util.ArrayList newResolveList)

            // Post this back on the UI thread
            mUiHandler.post(new Runnable() {
                public void run() {
                    /*
                     * SimpleAdapter does not support changing the lists after it
                     * has been created. We just create a new instance.
                     */
                    mMyAdapter = createResolveAdapter(newAdapterList);
                    mResolveList = newResolveList;
                    setListAdapter(mMyAdapter);
                }
            });
        
    private voidupdateListAndAdapter()
    This should be called from the UI thread.

            // Get the activities in a separate thread
            new Thread("data updater") {
                @Override
                public void run() {
                    synchronized (BookmarkPicker.this) {
                        /*
                         * Don't touch any of the lists that are being used by the
                         * adapter in this thread!
                         */
                        ArrayList<ResolveInfo> newResolveList = new ArrayList<ResolveInfo>();
                        ArrayList<Map<String, ?>> newAdapterList = new ArrayList<Map<String, ?>>();
    
                        fillResolveList(newResolveList);
                        Collections.sort(newResolveList,
                                new ResolveInfo.DisplayNameComparator(getPackageManager()));
                        
                        fillAdapterList(newAdapterList, newResolveList);
                        
                        updateAdapterToUseNewLists(newAdapterList, newResolveList);
                    }
                }
            }.start();