blob: f070546b6553e100e5bb4a458b475b19c0455d38 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.content.DialogInterface;
22import android.content.Intent;
23import android.graphics.Bitmap;
Patrick Scotte09761e2009-03-24 20:43:37 -070024import android.graphics.BitmapFactory;
25import android.graphics.Canvas;
26import android.graphics.Color;
27import android.graphics.Paint;
Patrick Scott3918d442009-08-04 13:22:29 -040028import android.graphics.Path;
29import android.graphics.PorterDuff;
30import android.graphics.PorterDuffXfermode;
Patrick Scotte09761e2009-03-24 20:43:37 -070031import android.graphics.RectF;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.net.Uri;
33import android.os.Bundle;
34import android.os.Handler;
35import android.os.Message;
36import android.os.ServiceManager;
37import android.provider.Browser;
38import android.text.IClipboard;
39import android.util.Log;
40import android.view.ContextMenu;
41import android.view.KeyEvent;
Leon Scroggins892df312009-07-14 14:48:02 -040042import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.view.Menu;
44import android.view.MenuInflater;
45import android.view.MenuItem;
46import android.view.View;
47import android.view.ViewGroup;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040048import android.view.ViewGroup.LayoutParams;
49import android.view.ViewStub;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050import android.view.ContextMenu.ContextMenuInfo;
51import android.widget.AdapterView;
Leon Scroggins89c6d362009-07-15 16:54:37 -040052import android.widget.GridView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053import android.widget.ListView;
Leon Scrogginsfeb941d2009-05-28 17:27:38 -040054import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055
56/**
57 * View showing the user's bookmarks in the browser.
58 */
59public class BrowserBookmarksPage extends Activity implements
60 View.OnCreateContextMenuListener {
61
Leon Scroggins892df312009-07-14 14:48:02 -040062 private boolean mGridMode;
Leon Scroggins89c6d362009-07-15 16:54:37 -040063 private GridView mGridPage;
Leon Scroggins892df312009-07-14 14:48:02 -040064 private View mVerticalList;
The Android Open Source Project0c908882009-03-03 19:32:16 -080065 private BrowserBookmarksAdapter mBookmarksAdapter;
66 private static final int BOOKMARKS_SAVE = 1;
67 private boolean mMaxTabsOpen;
68 private BookmarkItem mContextHeader;
69 private AddNewBookmark mAddHeader;
70 private boolean mCanceled = false;
71 private boolean mCreateShortcut;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040072 private boolean mMostVisited;
73 private View mEmptyView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080074 // XXX: There is no public string defining this intent so if Home changes
75 // the value, we have to update this string.
76 private static final String INSTALL_SHORTCUT =
77 "com.android.launcher.action.INSTALL_SHORTCUT";
78
79 private final static String LOGTAG = "browser";
80
81
82 @Override
83 public boolean onContextItemSelected(MenuItem item) {
84 // It is possible that the view has been canceled when we get to
85 // this point as back has a higher priority
86 if (mCanceled) {
87 return true;
88 }
89 AdapterView.AdapterContextMenuInfo i =
90 (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
91 // If we have no menu info, we can't tell which item was selected.
92 if (i == null) {
93 return true;
94 }
95
96 switch (item.getItemId()) {
97 case R.id.new_context_menu_id:
98 saveCurrentPage();
99 break;
100 case R.id.open_context_menu_id:
101 loadUrl(i.position);
102 break;
103 case R.id.edit_context_menu_id:
104 editBookmark(i.position);
105 break;
106 case R.id.shortcut_context_menu_id:
Patrick Scott3918d442009-08-04 13:22:29 -0400107 final Intent send = createShortcutIntent(i.position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108 send.setAction(INSTALL_SHORTCUT);
109 sendBroadcast(send);
110 break;
111 case R.id.delete_context_menu_id:
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400112 if (mMostVisited) {
113 Browser.deleteFromHistory(getContentResolver(),
114 getUrl(i.position));
115 refreshList();
116 } else {
117 displayRemoveBookmarkDialog(i.position);
118 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 break;
120 case R.id.new_window_context_menu_id:
121 openInNewWindow(i.position);
122 break;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400123 case R.id.share_link_context_menu_id:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124 Browser.sendString(BrowserBookmarksPage.this, getUrl(i.position));
125 break;
126 case R.id.copy_url_context_menu_id:
127 copy(getUrl(i.position));
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400128 break;
129 case R.id.homepage_context_menu_id:
130 BrowserSettings.getInstance().setHomePage(this,
131 getUrl(i.position));
132 Toast.makeText(this, R.string.homepage_set,
133 Toast.LENGTH_LONG).show();
134 break;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400135 // Only for the Most visited page
136 case R.id.save_to_bookmarks_menu_id:
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400137 boolean isBookmark;
138 String name;
139 String url;
140 if (mGridMode) {
141 isBookmark = mBookmarksAdapter.getIsBookmark(i.position);
142 name = mBookmarksAdapter.getTitle(i.position);
143 url = mBookmarksAdapter.getUrl(i.position);
144 } else {
145 HistoryItem historyItem = ((HistoryItem) i.targetView);
146 isBookmark = historyItem.isBookmark();
147 name = historyItem.getName();
148 url = historyItem.getUrl();
149 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400150 // If the site is bookmarked, the item becomes remove from
151 // bookmarks.
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400152 if (isBookmark) {
153 Bookmarks.removeFromBookmarks(this, getContentResolver(), url);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400154 } else {
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400155 Browser.saveBookmark(this, name, url);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400156 }
157 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 default:
159 return super.onContextItemSelected(item);
160 }
161 return true;
162 }
163
164 @Override
165 public void onCreateContextMenu(ContextMenu menu, View v,
166 ContextMenuInfo menuInfo) {
167 AdapterView.AdapterContextMenuInfo i =
168 (AdapterView.AdapterContextMenuInfo) menuInfo;
169
170 MenuInflater inflater = getMenuInflater();
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400171 if (mMostVisited) {
172 inflater.inflate(R.menu.historycontext, menu);
173 } else {
174 inflater.inflate(R.menu.bookmarkscontext, menu);
175 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800176
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400177 if (0 == i.position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178 menu.setGroupVisible(R.id.CONTEXT_MENU, false);
179 if (mAddHeader == null) {
180 mAddHeader = new AddNewBookmark(BrowserBookmarksPage.this);
181 } else if (mAddHeader.getParent() != null) {
182 ((ViewGroup) mAddHeader.getParent()).
183 removeView(mAddHeader);
184 }
Leon Scroggins892df312009-07-14 14:48:02 -0400185 mAddHeader.setUrl(getIntent().getStringExtra("url"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 menu.setHeaderView(mAddHeader);
187 return;
188 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400189 if (mMostVisited) {
190 if ((!mGridMode && ((HistoryItem) i.targetView).isBookmark())
191 || mBookmarksAdapter.getIsBookmark(i.position)) {
192 MenuItem item = menu.findItem(
193 R.id.save_to_bookmarks_menu_id);
194 item.setTitle(R.string.remove_from_bookmarks);
195 }
196 } else {
197 // The historycontext menu has no ADD_MENU group.
198 menu.setGroupVisible(R.id.ADD_MENU, false);
199 }
Leon Scroggins892df312009-07-14 14:48:02 -0400200 if (mMaxTabsOpen) {
201 menu.findItem(R.id.new_window_context_menu_id).setVisible(
202 false);
203 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204 if (mContextHeader == null) {
205 mContextHeader = new BookmarkItem(BrowserBookmarksPage.this);
206 } else if (mContextHeader.getParent() != null) {
207 ((ViewGroup) mContextHeader.getParent()).
208 removeView(mContextHeader);
209 }
Leon Scroggins892df312009-07-14 14:48:02 -0400210 if (mGridMode) {
211 mBookmarksAdapter.populateBookmarkItem(mContextHeader,
212 i.position);
213 } else {
214 BookmarkItem b = (BookmarkItem) i.targetView;
215 b.copyTo(mContextHeader);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 }
Leon Scroggins892df312009-07-14 14:48:02 -0400217 menu.setHeaderView(mContextHeader);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800218 }
219
220 /**
221 * Create a new BrowserBookmarksPage.
222 */
223 @Override
224 protected void onCreate(Bundle icicle) {
225 super.onCreate(icicle);
226
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
228 mCreateShortcut = true;
229 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 mMaxTabsOpen = getIntent().getBooleanExtra("maxTabsOpen", false);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400231 mMostVisited = getIntent().getBooleanExtra("mostVisited", false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400233 if (mCreateShortcut) {
234 setTitle(R.string.browser_bookmarks_page_bookmarks_text);
235 }
Leon Scroggins892df312009-07-14 14:48:02 -0400236 mBookmarksAdapter = new BrowserBookmarksAdapter(this,
Leon Scroggins89c6d362009-07-15 16:54:37 -0400237 getIntent().getStringExtra("url"),
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400238 getIntent().getStringExtra("title"), mCreateShortcut,
239 mMostVisited);
240 if (mMostVisited) {
241 mEmptyView = new ViewStub(this, R.layout.empty_history);
242 }
Leon Scroggins89c6d362009-07-15 16:54:37 -0400243 switchViewMode(true);
Leon Scroggins892df312009-07-14 14:48:02 -0400244 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800245
Leon Scroggins892df312009-07-14 14:48:02 -0400246 /**
247 * Set the ContentView to be either the grid of thumbnails or the vertical
248 * list. Pass true to set it to the grid.
249 */
250 private void switchViewMode(boolean gridMode) {
251 mGridMode = gridMode;
252 mBookmarksAdapter.switchViewMode(gridMode);
253 if (mGridMode) {
254 if (mGridPage == null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -0400255 mGridPage = new GridView(this);
256 mGridPage.setAdapter(mBookmarksAdapter);
Leon Scroggins892df312009-07-14 14:48:02 -0400257 mGridPage.setOnItemClickListener(mListener);
Leon Scroggins89c6d362009-07-15 16:54:37 -0400258 mGridPage.setNumColumns(GridView.AUTO_FIT);
259 // Keep this in sync with bookmark_thumb and
260 // BrowserActivity.updateScreenshot
261 mGridPage.setColumnWidth(100);
262 mGridPage.setFocusable(true);
263 mGridPage.setFocusableInTouchMode(true);
264 mGridPage.setSelector(android.R.drawable.gallery_thumb);
265 mGridPage.setVerticalSpacing(10);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400266 if (mMostVisited) {
267 mGridPage.setEmptyView(mEmptyView);
268 }
Leon Scroggins892df312009-07-14 14:48:02 -0400269 if (!mCreateShortcut) {
270 mGridPage.setOnCreateContextMenuListener(this);
271 }
272 }
273 setContentView(mGridPage);
274 } else {
275 if (null == mVerticalList) {
276 LayoutInflater factory = LayoutInflater.from(this);
277 mVerticalList = factory.inflate(R.layout.browser_bookmarks_page,
278 null);
279
280 ListView listView
281 = (ListView) mVerticalList.findViewById(R.id.list);
282 listView.setAdapter(mBookmarksAdapter);
283 listView.setDrawSelectorOnTop(false);
284 listView.setVerticalScrollBarEnabled(true);
285 listView.setOnItemClickListener(mListener);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400286 if (mMostVisited) {
287 listView.setEmptyView(mEmptyView);
288 }
Leon Scroggins892df312009-07-14 14:48:02 -0400289
290 if (!mCreateShortcut) {
291 listView.setOnCreateContextMenuListener(this);
292 }
293 }
294 setContentView(mVerticalList);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800295 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400296 if (mMostVisited) {
297 addContentView(mEmptyView, new LayoutParams(
298 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
299 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800300 }
301
302 private static final int SAVE_CURRENT_PAGE = 1000;
303 private final Handler mHandler = new Handler() {
304 @Override
305 public void handleMessage(Message msg) {
306 if (msg.what == SAVE_CURRENT_PAGE) {
307 saveCurrentPage();
308 }
309 }
310 };
311
312 private AdapterView.OnItemClickListener mListener = new AdapterView.OnItemClickListener() {
313 public void onItemClick(AdapterView parent, View v, int position, long id) {
314 // It is possible that the view has been canceled when we get to
315 // this point as back has a higher priority
316 if (mCanceled) {
Leon Scroggins892df312009-07-14 14:48:02 -0400317 android.util.Log.e(LOGTAG, "item clicked when dismissing");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800318 return;
319 }
320 if (!mCreateShortcut) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400321 if (0 == position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800322 // XXX: Work-around for a framework issue.
323 mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE);
324 } else {
325 loadUrl(position);
326 }
327 } else {
Patrick Scott3918d442009-08-04 13:22:29 -0400328 final Intent intent = createShortcutIntent(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800329 setResultToParent(RESULT_OK, intent);
330 finish();
331 }
332 }
333 };
334
Patrick Scott3918d442009-08-04 13:22:29 -0400335 private Intent createShortcutIntent(int position) {
336 String url = getUrl(position);
337 String title = getBookmarkTitle(position);
338 Bitmap touchIcon = getTouchIcon(position);
339
The Android Open Source Project0c908882009-03-03 19:32:16 -0800340 final Intent i = new Intent();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700341 final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW,
342 Uri.parse(url));
343 long urlHash = url.hashCode();
344 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
345 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID,
346 Long.toString(uniqueId));
347 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800348 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Patrick Scott3918d442009-08-04 13:22:29 -0400349 // Use the apple-touch-icon if available
350 if (touchIcon != null) {
351 // Make a copy so we can modify the pixels.
352 Bitmap copy = touchIcon.copy(Bitmap.Config.ARGB_8888, true);
Patrick Scotte09761e2009-03-24 20:43:37 -0700353 Canvas canvas = new Canvas(copy);
354
Patrick Scott3918d442009-08-04 13:22:29 -0400355 // Construct a path from a round rect. This will allow drawing with
356 // an inverse fill so we can punch a hole using the round rect.
357 Path path = new Path();
358 path.setFillType(Path.FillType.INVERSE_WINDING);
359 path.addRoundRect(new RectF(0, 0, touchIcon.getWidth(),
360 touchIcon.getHeight()), 8f, 8f, Path.Direction.CW);
Patrick Scotte09761e2009-03-24 20:43:37 -0700361
Patrick Scott3918d442009-08-04 13:22:29 -0400362 // Construct a paint that clears the outside of the rectangle and
363 // draw.
364 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
365 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
366 canvas.drawPath(path, paint);
Patrick Scotte09761e2009-03-24 20:43:37 -0700367
Patrick Scotte09761e2009-03-24 20:43:37 -0700368 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
Patrick Scott3918d442009-08-04 13:22:29 -0400369 } else {
370 Bitmap favicon = getFavicon(position);
371 if (favicon == null) {
372 i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
373 Intent.ShortcutIconResource.fromContext(
374 BrowserBookmarksPage.this,
375 R.drawable.ic_launcher_shortcut_browser_bookmark));
376 } else {
377 Bitmap icon = BitmapFactory.decodeResource(getResources(),
378 R.drawable.ic_launcher_shortcut_browser_bookmark);
379
380 // Make a copy of the regular icon so we can modify the pixels.
381 Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true);
382 Canvas canvas = new Canvas(copy);
383
384 // Make a Paint for the white background rectangle and for
385 // filtering the favicon.
386 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG
387 | Paint.FILTER_BITMAP_FLAG);
388 p.setStyle(Paint.Style.FILL_AND_STROKE);
389 p.setColor(Color.WHITE);
390
391 // Create a rectangle that is slightly wider than the favicon
392 final float iconSize = 16; // 16x16 favicon
393 final float padding = 2; // white padding around icon
394 final float rectSize = iconSize + 2 * padding;
395 final float y = icon.getHeight() - rectSize;
396 RectF r = new RectF(0, y, rectSize, y + rectSize);
397
398 // Draw a white rounded rectangle behind the favicon
399 canvas.drawRoundRect(r, 2, 2, p);
400
401 // Draw the favicon in the same rectangle as the rounded
402 // rectangle but inset by the padding
403 // (results in a 16x16 favicon).
404 r.inset(padding, padding);
405 canvas.drawBitmap(favicon, null, r, p);
406 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
407 }
Patrick Scotte09761e2009-03-24 20:43:37 -0700408 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800409 // Do not allow duplicate items
410 i.putExtra("duplicate", false);
411 return i;
412 }
413
414 private void saveCurrentPage() {
415 Intent i = new Intent(BrowserBookmarksPage.this,
416 AddBookmarkPage.class);
417 i.putExtras(getIntent());
418 startActivityForResult(i, BOOKMARKS_SAVE);
419 }
420
421 private void loadUrl(int position) {
422 Intent intent = (new Intent()).setAction(getUrl(position));
423 setResultToParent(RESULT_OK, intent);
424 finish();
425 }
426
427 @Override
428 public boolean onCreateOptionsMenu(Menu menu) {
429 boolean result = super.onCreateOptionsMenu(menu);
430 if (!mCreateShortcut) {
431 MenuInflater inflater = getMenuInflater();
432 inflater.inflate(R.menu.bookmarks, menu);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400433 // Most visited page does not have an option to bookmark the last
434 // viewed page.
435 menu.findItem(R.id.new_context_menu_id).setVisible(!mMostVisited);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800436 return true;
437 }
438 return result;
439 }
440
441 @Override
Leon Scroggins0c786502009-08-04 16:04:55 -0400442 public boolean onPrepareOptionsMenu(Menu menu) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400443 if (mBookmarksAdapter.getCount() == 0) {
444 // No need to show the menu if there are no items.
445 return false;
446 }
Leon Scroggins0c786502009-08-04 16:04:55 -0400447 menu.findItem(R.id.switch_mode_menu_id).setTitle(
448 mGridMode ? R.string.switch_to_list
449 : R.string.switch_to_thumbnails);
450 return true;
451 }
452
453 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800454 public boolean onOptionsItemSelected(MenuItem item) {
455 switch (item.getItemId()) {
Leon Scroggins892df312009-07-14 14:48:02 -0400456 case R.id.new_context_menu_id:
457 saveCurrentPage();
458 break;
459
460 case R.id.switch_mode_menu_id:
461 switchViewMode(!mGridMode);
462 break;
463
464 default:
465 return super.onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 }
467 return true;
468 }
469
470 private void openInNewWindow(int position) {
471 Bundle b = new Bundle();
472 b.putBoolean("new_window", true);
473 setResultToParent(RESULT_OK,
474 (new Intent()).setAction(getUrl(position)).putExtras(b));
475
476 finish();
477 }
478
479
480 private void editBookmark(int position) {
481 Intent intent = new Intent(BrowserBookmarksPage.this,
482 AddBookmarkPage.class);
483 intent.putExtra("bookmark", getRow(position));
484 startActivityForResult(intent, BOOKMARKS_SAVE);
485 }
486
487 @Override
488 protected void onActivityResult(int requestCode, int resultCode,
489 Intent data) {
490 switch(requestCode) {
491 case BOOKMARKS_SAVE:
492 if (resultCode == RESULT_OK) {
493 Bundle extras;
494 if (data != null && (extras = data.getExtras()) != null) {
495 // If there are extras, then we need to save
496 // the edited bookmark. This is done in updateRow()
497 String title = extras.getString("title");
498 String url = extras.getString("url");
499 if (title != null && url != null) {
500 mBookmarksAdapter.updateRow(extras);
501 }
502 } else {
503 // extras == null then a new bookmark was added to
504 // the database.
505 refreshList();
506 }
507 }
508 break;
509 default:
510 break;
511 }
512 }
513
514 private void displayRemoveBookmarkDialog(int position) {
515 // Put up a dialog asking if the user really wants to
516 // delete the bookmark
517 final int deletePos = position;
518 new AlertDialog.Builder(this)
519 .setTitle(R.string.delete_bookmark)
520 .setIcon(android.R.drawable.ic_dialog_alert)
521 .setMessage(getText(R.string.delete_bookmark_warning).toString().replace(
522 "%s", getBookmarkTitle(deletePos)))
523 .setPositiveButton(R.string.ok,
524 new DialogInterface.OnClickListener() {
525 public void onClick(DialogInterface dialog, int whichButton) {
526 deleteBookmark(deletePos);
527 }
528 })
529 .setNegativeButton(R.string.cancel, null)
530 .show();
531 }
532
533 /**
534 * Refresh the shown list after the database has changed.
535 */
Leon Scroggins892df312009-07-14 14:48:02 -0400536 private void refreshList() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800537 mBookmarksAdapter.refreshList();
538 }
539
540 /**
541 * Return a hashmap representing the currently highlighted row.
542 */
543 public Bundle getRow(int position) {
544 return mBookmarksAdapter.getRow(position);
545 }
546
547 /**
548 * Return the url of the currently highlighted row.
549 */
550 public String getUrl(int position) {
551 return mBookmarksAdapter.getUrl(position);
552 }
553
Patrick Scotte09761e2009-03-24 20:43:37 -0700554 /**
555 * Return the favicon of the currently highlighted row.
556 */
557 public Bitmap getFavicon(int position) {
558 return mBookmarksAdapter.getFavicon(position);
559 }
560
Patrick Scott3918d442009-08-04 13:22:29 -0400561 private Bitmap getTouchIcon(int position) {
562 return mBookmarksAdapter.getTouchIcon(position);
563 }
564
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 private void copy(CharSequence text) {
566 try {
567 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
568 if (clip != null) {
569 clip.setClipboardText(text);
570 }
571 } catch (android.os.RemoteException e) {
572 Log.e(LOGTAG, "Copy failed", e);
573 }
574 }
575
576 public String getBookmarkTitle(int position) {
577 return mBookmarksAdapter.getTitle(position);
578 }
579
580 /**
581 * Delete the currently highlighted row.
582 */
583 public void deleteBookmark(int position) {
584 mBookmarksAdapter.deleteRow(position);
585 }
586
587 public boolean dispatchKeyEvent(KeyEvent event) {
588 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.isDown()) {
589 setResultToParent(RESULT_CANCELED, null);
590 mCanceled = true;
591 }
592 return super.dispatchKeyEvent(event);
593 }
594
595 // This Activity is generally a sub-Activity of CombinedHistoryActivity. In
596 // that situation, we need to pass our result code up to our parent.
597 // However, if someone calls this Activity directly, then this has no
598 // parent, and it needs to set it on itself.
599 private void setResultToParent(int resultCode, Intent data) {
600 Activity a = getParent() == null ? this : getParent();
601 a.setResult(resultCode, data);
602 }
603}