FileDocCategorySizeDatePackage
BrowserBookmarksPage.javaAPI DocAndroid 1.5 API16045Wed May 06 22:42:42 BST 2009com.android.browser

BrowserBookmarksPage

public class BrowserBookmarksPage extends android.app.Activity implements View.OnCreateContextMenuListener
View showing the user's bookmarks in the browser.

Fields Summary
private BrowserBookmarksAdapter
mBookmarksAdapter
private static final int
BOOKMARKS_SAVE
private boolean
mMaxTabsOpen
private BookmarkItem
mContextHeader
private AddNewBookmark
mAddHeader
private boolean
mCanceled
private boolean
mCreateShortcut
private static final String
INSTALL_SHORTCUT
private static final String
LOGTAG
private static final int
SAVE_CURRENT_PAGE
private final android.os.Handler
mHandler
private AdapterView.OnItemClickListener
mListener
Constructors Summary
Methods Summary
private voidcopy(java.lang.CharSequence text)

        try {
            IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
            if (clip != null) {
                clip.setClipboardText(text);
            }
        } catch (android.os.RemoteException e) {
            Log.e(LOGTAG, "Copy failed", e);
        }
    
private android.content.IntentcreateShortcutIntent(java.lang.String url, java.lang.String title, android.graphics.Bitmap favicon)


         
              
        final Intent i = new Intent();
        final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW,
                Uri.parse(url));
        long urlHash = url.hashCode();
        long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
        shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID,
                Long.toString(uniqueId));
        i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
        if (favicon == null) {
            i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(
                            BrowserBookmarksPage.this,
                            R.drawable.ic_launcher_shortcut_browser_bookmark));
        } else {
            Bitmap icon = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_launcher_shortcut_browser_bookmark);

            // Make a copy of the regular icon so we can modify the pixels.
            Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true);
            Canvas canvas = new Canvas(copy);

            // Make a Paint for the white background rectangle and for
            // filtering the favicon.
            Paint p = new Paint(Paint.ANTI_ALIAS_FLAG
                    | Paint.FILTER_BITMAP_FLAG);
            p.setStyle(Paint.Style.FILL_AND_STROKE);
            p.setColor(Color.WHITE);

            // Create a rectangle that is slightly wider than the favicon
            final float iconSize = 16; // 16x16 favicon
            final float padding = 2;   // white padding around icon
            final float rectSize = iconSize + 2 * padding;
            final float y = icon.getHeight() - rectSize;
            RectF r = new RectF(0, y, rectSize, y + rectSize);

            // Draw a white rounded rectangle behind the favicon
            canvas.drawRoundRect(r, 2, 2, p);

            // Draw the favicon in the same rectangle as the rounded rectangle
            // but inset by the padding (results in a 16x16 favicon).
            r.inset(padding, padding);
            canvas.drawBitmap(favicon, null, r, p);
            i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
        }
        // Do not allow duplicate items
        i.putExtra("duplicate", false);
        return i;
    
public voiddeleteBookmark(int position)
Delete the currently highlighted row.

        mBookmarksAdapter.deleteRow(position);
    
public booleandispatchKeyEvent(android.view.KeyEvent event)

    
        if (event.getKeyCode() ==  KeyEvent.KEYCODE_BACK && event.isDown()) {
            setResultToParent(RESULT_CANCELED, null);
            mCanceled = true;
        }
        return super.dispatchKeyEvent(event);
    
private voiddisplayRemoveBookmarkDialog(int position)

        // Put up a dialog asking if the user really wants to
        // delete the bookmark
        final int deletePos = position;
        new AlertDialog.Builder(this)
                .setTitle(R.string.delete_bookmark)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setMessage(getText(R.string.delete_bookmark_warning).toString().replace(
                        "%s", getBookmarkTitle(deletePos)))
                .setPositiveButton(R.string.ok, 
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                deleteBookmark(deletePos);
                            }
                        })
                .setNegativeButton(R.string.cancel, null)
                .show();
    
private voideditBookmark(int position)

        Intent intent = new Intent(BrowserBookmarksPage.this, 
            AddBookmarkPage.class);
        intent.putExtra("bookmark", getRow(position));
        startActivityForResult(intent, BOOKMARKS_SAVE);
    
public java.lang.StringgetBookmarkTitle(int position)

        return mBookmarksAdapter.getTitle(position);
    
public android.graphics.BitmapgetFavicon(int position)
Return the favicon of the currently highlighted row.

        return mBookmarksAdapter.getFavicon(position);
    
public android.os.BundlegetRow(int position)
Return a hashmap representing the currently highlighted row.

        return mBookmarksAdapter.getRow(position);
    
public java.lang.StringgetUrl(int position)
Return the url of the currently highlighted row.

        return mBookmarksAdapter.getUrl(position);
    
private voidloadUrl(int position)

        Intent intent = (new Intent()).setAction(getUrl(position));
        setResultToParent(RESULT_OK, intent);
        finish();
    
protected voidonActivityResult(int requestCode, int resultCode, android.content.Intent data)

        switch(requestCode) {
            case BOOKMARKS_SAVE:
                if (resultCode == RESULT_OK) {
                    Bundle extras;
                    if (data != null && (extras = data.getExtras()) != null) {
                        // If there are extras, then we need to save
                        // the edited bookmark. This is done in updateRow()
                        String title = extras.getString("title");
                        String url = extras.getString("url");
                        if (title != null && url != null) {
                            mBookmarksAdapter.updateRow(extras);
                        }
                    } else {
                        // extras == null then a new bookmark was added to
                        // the database.
                        refreshList();
                    }
                }
                break;
            default:
                break;
        }
    
public booleanonContextItemSelected(android.view.MenuItem item)



    
        
        // It is possible that the view has been canceled when we get to
        // this point as back has a higher priority 
        if (mCanceled) {
            return true;
        }
        AdapterView.AdapterContextMenuInfo i = 
            (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        // If we have no menu info, we can't tell which item was selected.
        if (i == null) {
            return true;
        }
        
        switch (item.getItemId()) {
        case R.id.new_context_menu_id:
            saveCurrentPage();
            break;
        case R.id.open_context_menu_id:
            loadUrl(i.position);
            break;
        case R.id.edit_context_menu_id:
            editBookmark(i.position);
            break;
        case R.id.shortcut_context_menu_id:
            final Intent send = createShortcutIntent(getUrl(i.position),
                    getBookmarkTitle(i.position), getFavicon(i.position));
            send.setAction(INSTALL_SHORTCUT);
            sendBroadcast(send);
            break;
        case R.id.delete_context_menu_id:
            displayRemoveBookmarkDialog(i.position);
            break;
        case R.id.new_window_context_menu_id:
            openInNewWindow(i.position);
            break;
        case R.id.send_context_menu_id:
            Browser.sendString(BrowserBookmarksPage.this, getUrl(i.position));
            break;
        case R.id.copy_url_context_menu_id:
            copy(getUrl(i.position));
            
        default:
            return super.onContextItemSelected(item);
        }
        return true;
    
protected voidonCreate(android.os.Bundle icicle)
Create a new BrowserBookmarksPage.

        super.onCreate(icicle);

        setContentView(R.layout.browser_bookmarks_page);
        setTitle(R.string.browser_bookmarks_page_bookmarks_text);

        if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
            mCreateShortcut = true;
        }

        mBookmarksAdapter = new BrowserBookmarksAdapter(this, 
                getIntent().getStringExtra("url"), mCreateShortcut);
        mMaxTabsOpen = getIntent().getBooleanExtra("maxTabsOpen", false);

        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(mBookmarksAdapter);
        listView.setDrawSelectorOnTop(false);
        listView.setVerticalScrollBarEnabled(true);
        listView.setOnItemClickListener(mListener);

        if (!mCreateShortcut) {
            listView.setOnCreateContextMenuListener(this);
        }
    
public voidonCreateContextMenu(android.view.ContextMenu menu, android.view.View v, android.view.ContextMenu.ContextMenuInfo menuInfo)

            AdapterView.AdapterContextMenuInfo i = 
                    (AdapterView.AdapterContextMenuInfo) menuInfo;

            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.bookmarkscontext, menu);

            if (0 == i.position) {
                menu.setGroupVisible(R.id.CONTEXT_MENU, false);
                if (mAddHeader == null) {
                    mAddHeader = new AddNewBookmark(BrowserBookmarksPage.this);
                } else if (mAddHeader.getParent() != null) {
                    ((ViewGroup) mAddHeader.getParent()).
                            removeView(mAddHeader);
                }
                ((AddNewBookmark) i.targetView).copyTo(mAddHeader);
                menu.setHeaderView(mAddHeader);
                return;
            }
            menu.setGroupVisible(R.id.ADD_MENU, false);
            BookmarkItem b = (BookmarkItem) i.targetView;
            if (mContextHeader == null) {
                mContextHeader = new BookmarkItem(BrowserBookmarksPage.this);
            } else if (mContextHeader.getParent() != null) {
                ((ViewGroup) mContextHeader.getParent()).
                        removeView(mContextHeader);
            }
            b.copyTo(mContextHeader);
            menu.setHeaderView(mContextHeader);

            if (mMaxTabsOpen) {
                menu.findItem(R.id.new_window_context_menu_id).setVisible(
                        false);
            }
        
public booleanonCreateOptionsMenu(android.view.Menu menu)

        boolean result = super.onCreateOptionsMenu(menu);
        if (!mCreateShortcut) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.bookmarks, menu);
            return true;
        }
        return result;
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        switch (item.getItemId()) {
            case R.id.new_context_menu_id:
                saveCurrentPage();
                break;
                
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    
private voidopenInNewWindow(int position)

        Bundle b = new Bundle();
        b.putBoolean("new_window", true);
        setResultToParent(RESULT_OK,
                (new Intent()).setAction(getUrl(position)).putExtras(b));

        finish();
    
public voidrefreshList()
Refresh the shown list after the database has changed.

        mBookmarksAdapter.refreshList();
    
private voidsaveCurrentPage()

        Intent i = new Intent(BrowserBookmarksPage.this,
                AddBookmarkPage.class);
        i.putExtras(getIntent());
        startActivityForResult(i, BOOKMARKS_SAVE);
    
private voidsetResultToParent(int resultCode, android.content.Intent data)

        Activity a = getParent() == null ? this : getParent();
        a.setResult(resultCode, data);