summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/BrowserBookmarksPage.java
blob: a36bd08f9002a7c4f1861bb1bc9b62681c65c57d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.browser;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BrowserContract;
import android.provider.BrowserContract.Accounts;
import android.text.TextUtils;
import android.util.Pair;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebIconDatabase.IconListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Stack;

/**
 *  View showing the user's bookmarks in the browser.
 */
public class BrowserBookmarksPage extends Fragment implements View.OnCreateContextMenuListener,
        LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener, IconListener, OnClickListener,
        OnItemSelectedListener {

    static final int BOOKMARKS_SAVE = 1;
    static final String LOGTAG = "browser";

    static final int LOADER_BOOKMARKS = 1;
    static final int LOADER_ACCOUNTS = 2;
    static final int LOADER_ACCOUNTS_THEN_BOOKMARKS = 3;

    static final String EXTRA_SHORTCUT = "create_shortcut";
    static final String EXTRA_DISABLE_WINDOW = "disable_new_window";

    static final String ACCOUNT_NAME_UNSYNCED = "Unsynced";
    
    public static final String PREF_ACCOUNT_TYPE = "acct_type";
    public static final String PREF_ACCOUNT_NAME = "acct_name";

    static final String DEFAULT_ACCOUNT = "local";

    BookmarksHistoryCallbacks mCallbacks;
    GridView mGrid;
    Spinner mAccountSelector;
    BrowserBookmarksAdapter mAdapter;
    boolean mDisableNewWindow;
    BookmarkItem mContextHeader;
    boolean mCanceled = false;
    boolean mCreateShortcut;
    View mEmptyView;
    View mContentView;
    Stack<Pair<String, Uri>> mFolderStack = new Stack<Pair<String, Uri>>();
    Button mUpButton;

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        switch (id) {
            case LOADER_BOOKMARKS: {
                String accountType = null;
                String accountName = null;
                if (args != null) {
                    accountType = args.getString(BookmarksLoader.ARG_ACCOUNT_TYPE);
                    accountName = args.getString(BookmarksLoader.ARG_ACCOUNT_NAME);
                }
                return new BookmarksLoader(getActivity(), accountType, accountName);
            }

            case LOADER_ACCOUNTS:
            case LOADER_ACCOUNTS_THEN_BOOKMARKS: {
                return new CursorLoader(getActivity(), Accounts.CONTENT_URI,
                        new String[] { Accounts.ACCOUNT_TYPE, Accounts.ACCOUNT_NAME }, null, null,
                        null);
            }
        }
        throw new UnsupportedOperationException("Unknown loader id " + id);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        switch (loader.getId()) {
            case LOADER_BOOKMARKS: {
                // Set the visibility of the empty vs. content views
                if (cursor == null || cursor.getCount() == 0) {
                    mEmptyView.setVisibility(View.VISIBLE);
                    mGrid.setVisibility(View.GONE);
                } else {
                    mEmptyView.setVisibility(View.GONE);
                    mGrid.setVisibility(View.VISIBLE);
                }

                // Fill in the "up" button if needed
                BookmarksLoader bl = (BookmarksLoader) loader;
                String path = bl.getUri().getPath();
                boolean rootFolder =
                        BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER.getPath().equals(path);
                if (rootFolder) {
                    mUpButton.setText(R.string.defaultBookmarksUpButton);
                    mUpButton.setEnabled(false);
                } else {
                    mUpButton.setText(mFolderStack.peek().first);
                    mUpButton.setEnabled(true);
                }
                mUpButton.setVisibility(View.VISIBLE);

                // Give the new data to the adapter
                mAdapter.changeCursor(cursor);

                break;
            }

            case LOADER_ACCOUNTS:
            case LOADER_ACCOUNTS_THEN_BOOKMARKS: {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
                        getActivity());
                String storedAccountType = prefs.getString(PREF_ACCOUNT_TYPE, null);
                String storedAccountName = prefs.getString(PREF_ACCOUNT_NAME, null);
                String accountType =
                        TextUtils.isEmpty(storedAccountType) ? DEFAULT_ACCOUNT : storedAccountType;
                String accountName =
                        TextUtils.isEmpty(storedAccountName) ? DEFAULT_ACCOUNT : storedAccountName;

                Bundle args = null;
                if (cursor == null || !cursor.moveToFirst()) {
                    // No accounts, set the prefs to the default
                    accountType = DEFAULT_ACCOUNT;
                    accountName = DEFAULT_ACCOUNT;
                    mAccountSelector.setVisibility(View.GONE);
                } else {
                    int accountPosition = -1;

                    if (!DEFAULT_ACCOUNT.equals(accountType) &&
                            !DEFAULT_ACCOUNT.equals(accountName)) {
                        // Check to see if the account in prefs still exists
                        cursor.moveToFirst();
                        do {
                            if (accountType.equals(cursor.getString(0))
                                    && accountName.equals(cursor.getString(1))) {
                                accountPosition = cursor.getPosition();
                                break;
                            }
                        } while (cursor.moveToNext());
                    }

                    if (accountPosition == -1) {
                        if ((DEFAULT_ACCOUNT.equals(accountType)
                                && DEFAULT_ACCOUNT.equals(accountName))) {
                            // The "unsynced" account is selected
                            accountPosition = cursor.getCount();
                        } else {
                            // No account is set in prefs and there is at least one,
                            // so pick the first one as the default
                            cursor.moveToFirst();
                            accountType = cursor.getString(0);
                            accountName = cursor.getString(1);
                            accountPosition = 0;
                        }
                    }

                    args = new Bundle();
                    args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
                    args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);

                    // Add in the sync accounts
                    ArrayList<String> accounts = new ArrayList<String>();
                    cursor.moveToFirst();
                    do {
                        accounts.add(cursor.getString(1));
                    } while (cursor.moveToNext());

                    // STOPSHIP: Add in the "unsynced" account temporarily until we
                    // have support for migrated unsynced bookmarks into sync accounts.
                    accounts.add(ACCOUNT_NAME_UNSYNCED);

                    mAccountSelector.setAdapter(new ArrayAdapter<String>(getActivity(),
                            android.R.layout.simple_list_item_1, android.R.id.text1, accounts));
                    mAccountSelector.setVisibility(View.VISIBLE);
                    mAccountSelector.setSelection(accountPosition);
                }
                if (!accountType.equals(storedAccountType)
                        || !accountName.equals(storedAccountName)) {
                    prefs.edit()
                            .putString(PREF_ACCOUNT_TYPE, accountType)
                            .putString(PREF_ACCOUNT_NAME, accountName)
                            .apply();
                }
                if (loader.getId() == LOADER_ACCOUNTS_THEN_BOOKMARKS) {
                    getLoaderManager().initLoader(LOADER_BOOKMARKS, args, this);
                }

                break;
            }
        }
    }

    @Override
    public void onClick(View view) {
        if (view == mUpButton) {
            Pair<String, Uri> pair = mFolderStack.pop();
            BookmarksLoader loader =
                    (BookmarksLoader) ((Loader) getLoaderManager().getLoader(LOADER_BOOKMARKS));
            loader.setUri(pair.second);
            loader.forceLoad();
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        final Activity activity = getActivity();
        // 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.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:
            activity.sendBroadcast(createShortcutIntent(i.position));
            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.share_link_context_menu_id: {
            Cursor cursor = (Cursor) mAdapter.getItem(i.position);
            BrowserActivity.sharePage(activity,
                    cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE),
                    cursor.getString(BookmarksLoader.COLUMN_INDEX_URL),
                    getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON),
                    getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_THUMBNAIL));
            break;
        }
        case R.id.copy_url_context_menu_id:
            copy(getUrl(i.position));
            break;
        case R.id.homepage_context_menu_id: {
            BrowserSettings.getInstance().setHomePage(activity, getUrl(i.position));
            Toast.makeText(activity, R.string.homepage_set, Toast.LENGTH_LONG).show();
            break;
        }
        // Only for the Most visited page
        case R.id.save_to_bookmarks_menu_id: {
            Cursor cursor = (Cursor) mAdapter.getItem(i.position);
            String name = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
            String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
            // If the site is bookmarked, the item becomes remove from
            // bookmarks.
            Bookmarks.removeFromBookmarks(activity, activity.getContentResolver(), url, name);
            break;
        }
        default:
            return super.onContextItemSelected(item);
        }
        return true;
    }

    Bitmap getBitmap(Cursor cursor, int columnIndex) {
        byte[] data = cursor.getBlob(columnIndex);
        if (data == null) {
            return null;
        }
        return BitmapFactory.decodeByteArray(data, 0, data.length);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

        final Activity activity = getActivity();
        MenuInflater inflater = activity.getMenuInflater();
        inflater.inflate(R.menu.bookmarkscontext, menu);

        if (mDisableNewWindow) {
            menu.findItem(R.id.new_window_context_menu_id).setVisible(false);
        }

        if (mContextHeader == null) {
            mContextHeader = new BookmarkItem(activity);
        } else if (mContextHeader.getParent() != null) {
            ((ViewGroup) mContextHeader.getParent()).removeView(mContextHeader);
        }

        populateBookmarkItem(mAdapter, mContextHeader, info.position);

        menu.setHeaderView(mContextHeader);
    }

    private void populateBookmarkItem(BrowserBookmarksAdapter adapter, BookmarkItem item,
            int position) {
        Cursor cursor = (Cursor) mAdapter.getItem(position);
        String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
        item.setUrl(url);
        item.setName(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
        Bitmap bitmap = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON);
        if (bitmap == null) {
            bitmap = CombinedBookmarkHistoryActivity.getIconListenerSet().getFavicon(url);
        }
        item.setFavicon(bitmap);
    }

    /**
     *  Create a new BrowserBookmarksPage.
     */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Bundle args = getArguments();
        mCreateShortcut = args == null ? false : args.getBoolean("create_shortcut", false);
        mDisableNewWindow = args == null ? false : args.getBoolean("disable_new_window", false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallbacks = (BookmarksHistoryCallbacks) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Context context = getActivity();

        View root = inflater.inflate(R.layout.bookmarks, container, false);
        mEmptyView = root.findViewById(android.R.id.empty);
        mContentView = root.findViewById(android.R.id.content);

        mGrid = (GridView) root.findViewById(R.id.grid);
        mGrid.setOnItemClickListener(this);
        mGrid.setColumnWidth(BrowserActivity.getDesiredThumbnailWidth(getActivity()));
        if (!mCreateShortcut) {
            mGrid.setOnCreateContextMenuListener(this);
        }

        mAccountSelector = (Spinner) root.findViewById(R.id.accounts);
        mAccountSelector.setOnItemSelectedListener(this);
        mAccountSelector.setVisibility(View.INVISIBLE);

        mUpButton = (Button) root.findViewById(R.id.up);
        mUpButton.setEnabled(false);
        mUpButton.setOnClickListener(this);
        mUpButton.setVisibility(View.GONE);

        mAdapter = new BrowserBookmarksAdapter(getActivity());
        mGrid.setAdapter(mAdapter);

        // Start the loaders
        LoaderManager lm = getLoaderManager();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String accountType = prefs.getString(PREF_ACCOUNT_TYPE, null);
        String accountName = prefs.getString(PREF_ACCOUNT_NAME, null);
        if (!TextUtils.isEmpty(accountType) && !TextUtils.isEmpty(accountName)) {
            // There is an account set, load up that one
            Bundle args = null;
            if (!DEFAULT_ACCOUNT.equals(accountType) && !DEFAULT_ACCOUNT.equals(accountName)) {
                args = new Bundle();
                args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
                args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
            }
            lm.restartLoader(LOADER_BOOKMARKS, args, this);
            lm.restartLoader(LOADER_ACCOUNTS, null, this);
        } else {
            // No account set, load them first
            lm.restartLoader(LOADER_ACCOUNTS_THEN_BOOKMARKS, null, this);
        }

        // Add our own listener in case there are favicons that have yet to be loaded.
        CombinedBookmarkHistoryActivity.getIconListenerSet().addListener(this);

        return root;
    }

    @Override
    public void onReceivedIcon(String url, Bitmap icon) {
        // A new favicon has been loaded, so let anything attached to the adapter know about it
        // so new icons will be loaded.
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        // It is possible that the view has been canceled when we get to
        // this point as back has a higher priority
        if (mCanceled) {
            android.util.Log.e(LOGTAG, "item clicked when dismissing");
            return;
        }

        if (mCreateShortcut) {
            Intent intent = createShortcutIntent(position);
            // the activity handles the intent in startActivityFromFragment
            startActivity(intent);
            return;
        }

        Cursor cursor = (Cursor) mAdapter.getItem(position);
        boolean isFolder = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
        if (!isFolder) {
            mCallbacks.onUrlSelected(getUrl(position), false);
        } else {
            String title;
            if (mFolderStack.size() != 0) {
                title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
            } else {
                // TODO localize
                title = "Bookmarks";
            }
            LoaderManager manager = getLoaderManager();
            BookmarksLoader loader =
                    (BookmarksLoader) ((Loader) manager.getLoader(LOADER_BOOKMARKS));
            mFolderStack.push(new Pair(title, loader.getUri()));
            Uri uri = ContentUris.withAppendedId(
                    BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, id);
            loader.setUri(uri);
            loader.forceLoad();
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Adapter adapter = parent.getAdapter();
        String accountType = "com.google";
        String accountName = adapter.getItem(position).toString();

        Bundle args = null;
        if (ACCOUNT_NAME_UNSYNCED.equals(accountName)) {
            accountType = DEFAULT_ACCOUNT;
            accountName = DEFAULT_ACCOUNT;
        } else {
            args = new Bundle();
            args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
            args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
        }

        // Remember the selection for later
        PreferenceManager.getDefaultSharedPreferences(getActivity()).edit()
                .putString(PREF_ACCOUNT_TYPE, accountType)
                .putString(PREF_ACCOUNT_NAME, accountName)
                .apply();

        getLoaderManager().restartLoader(LOADER_BOOKMARKS, args, this);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Do nothing
    }

    private Intent createShortcutIntent(int position) {
        Cursor cursor = (Cursor) mAdapter.getItem(position);
        String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
        String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
        Bitmap touchIcon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_TOUCH_ICON);
        Bitmap favicon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON);
        return BookmarkUtils.createAddToHomeIntent(getActivity(), url, title, touchIcon, favicon);
    }

    private void loadUrl(int position) {
        mCallbacks.onUrlSelected(getUrl(position), false);
    }

    private void openInNewWindow(int position) {
        mCallbacks.onUrlSelected(getUrl(position), true);
    }

    private void editBookmark(int position) {
        Intent intent = new Intent(getActivity(), AddBookmarkPage.class);
        Cursor cursor = (Cursor) mAdapter.getItem(position);
        Bundle item = new Bundle();
        item.putString(BrowserContract.Bookmarks.TITLE,
                cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
        item.putString(BrowserContract.Bookmarks.URL,
                cursor.getString(BookmarksLoader.COLUMN_INDEX_URL));
        byte[] data = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON);
        if (data != null) {
            item.putParcelable(BrowserContract.Bookmarks.FAVICON,
                    BitmapFactory.decodeByteArray(data, 0, data.length));
        }
        item.putInt("id", cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID));
        item.putLong(BrowserContract.Bookmarks.PARENT,
                cursor.getLong(BookmarksLoader.COLUMN_INDEX_PARENT));
        intent.putExtra("bookmark", item);
        startActivityForResult(intent, BOOKMARKS_SAVE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case BOOKMARKS_SAVE:
                if (resultCode == Activity.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) {
                            updateRow(extras);
                        }
                    }
                }
                break;
        }
    }

    /**
     *  Update a row in the database with new information.
     *  @param map  Bundle storing id, title and url of new information
     */
    public void updateRow(Bundle map) {

        // Find the record
        int id = map.getInt("id");
        int position = -1;
        Cursor cursor = mAdapter.getCursor();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID) == id) {
                position = cursor.getPosition();
                break;
            }
        }
        if (position < 0) {
            return;
        }

        cursor.moveToPosition(position);
        ContentValues values = new ContentValues();
        String title = map.getString(BrowserContract.Bookmarks.TITLE);
        if (!title.equals(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE))) {
            values.put(BrowserContract.Bookmarks.TITLE, title);
        }
        String url = map.getString(BrowserContract.Bookmarks.URL);
        if (!url.equals(cursor.getString(BookmarksLoader.COLUMN_INDEX_URL))) {
            values.put(BrowserContract.Bookmarks.URL, url);
        }

        if (map.getBoolean("invalidateThumbnail") == true) {
            values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
        }

        if (values.size() > 0) {
            getActivity().getContentResolver().update(
                    ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id),
                    values, null, null);
        }
    }

    private void displayRemoveBookmarkDialog(final int position) {
        // Put up a dialog asking if the user really wants to
        // delete the bookmark
        Cursor cursor = (Cursor) mAdapter.getItem(position);
        Context context = getActivity();
        final ContentResolver resolver = context.getContentResolver();
        final Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI,
                cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID));

        new AlertDialog.Builder(context)
                .setTitle(R.string.delete_bookmark)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setMessage(context.getString(R.string.delete_bookmark_warning,
                        cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)))
                .setPositiveButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                resolver.delete(uri, null, null);
                            }
                        })
                .setNegativeButton(R.string.cancel, null)
                .show();
    }

    private String getUrl(int position) {
        Cursor cursor = (Cursor) mAdapter.getItem(position);
        return cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
    }

    private void copy(CharSequence text) {
        ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(
                Context.CLIPBOARD_SERVICE);
        cm.setPrimaryClip(ClipData.newRawUri(null, null, Uri.parse(text.toString())));
    }
}