blob: cf2dbc01429d708736a55a160328d03d74041c0a [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;
Leon Scroggins190095d2009-08-17 17:01:38 -040067 private boolean mDisableNewWindow;
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 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 Scroggins190095d2009-08-17 17:01:38 -0400200 if (mDisableNewWindow) {
Leon Scroggins892df312009-07-14 14:48:02 -0400201 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 }
Leon Scroggins190095d2009-08-17 17:01:38 -0400230 mDisableNewWindow = getIntent().getBooleanExtra("disable_new_window",
231 false);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400232 mMostVisited = getIntent().getBooleanExtra("mostVisited", false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400234 if (mCreateShortcut) {
235 setTitle(R.string.browser_bookmarks_page_bookmarks_text);
236 }
Leon Scroggins892df312009-07-14 14:48:02 -0400237 mBookmarksAdapter = new BrowserBookmarksAdapter(this,
Leon Scroggins89c6d362009-07-15 16:54:37 -0400238 getIntent().getStringExtra("url"),
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400239 getIntent().getStringExtra("title"), mCreateShortcut,
240 mMostVisited);
241 if (mMostVisited) {
242 mEmptyView = new ViewStub(this, R.layout.empty_history);
243 }
Leon Scroggins89c6d362009-07-15 16:54:37 -0400244 switchViewMode(true);
Leon Scroggins892df312009-07-14 14:48:02 -0400245 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246
Leon Scroggins892df312009-07-14 14:48:02 -0400247 /**
248 * Set the ContentView to be either the grid of thumbnails or the vertical
249 * list. Pass true to set it to the grid.
250 */
251 private void switchViewMode(boolean gridMode) {
252 mGridMode = gridMode;
253 mBookmarksAdapter.switchViewMode(gridMode);
254 if (mGridMode) {
255 if (mGridPage == null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -0400256 mGridPage = new GridView(this);
257 mGridPage.setAdapter(mBookmarksAdapter);
Leon Scroggins892df312009-07-14 14:48:02 -0400258 mGridPage.setOnItemClickListener(mListener);
Leon Scroggins89c6d362009-07-15 16:54:37 -0400259 mGridPage.setNumColumns(GridView.AUTO_FIT);
260 // Keep this in sync with bookmark_thumb and
261 // BrowserActivity.updateScreenshot
262 mGridPage.setColumnWidth(100);
263 mGridPage.setFocusable(true);
264 mGridPage.setFocusableInTouchMode(true);
265 mGridPage.setSelector(android.R.drawable.gallery_thumb);
266 mGridPage.setVerticalSpacing(10);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400267 if (mMostVisited) {
268 mGridPage.setEmptyView(mEmptyView);
269 }
Leon Scroggins892df312009-07-14 14:48:02 -0400270 if (!mCreateShortcut) {
271 mGridPage.setOnCreateContextMenuListener(this);
272 }
273 }
274 setContentView(mGridPage);
275 } else {
276 if (null == mVerticalList) {
277 LayoutInflater factory = LayoutInflater.from(this);
278 mVerticalList = factory.inflate(R.layout.browser_bookmarks_page,
279 null);
280
281 ListView listView
282 = (ListView) mVerticalList.findViewById(R.id.list);
283 listView.setAdapter(mBookmarksAdapter);
284 listView.setDrawSelectorOnTop(false);
285 listView.setVerticalScrollBarEnabled(true);
286 listView.setOnItemClickListener(mListener);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400287 if (mMostVisited) {
288 listView.setEmptyView(mEmptyView);
289 }
Leon Scroggins892df312009-07-14 14:48:02 -0400290
291 if (!mCreateShortcut) {
292 listView.setOnCreateContextMenuListener(this);
293 }
294 }
295 setContentView(mVerticalList);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800296 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400297 if (mMostVisited) {
298 addContentView(mEmptyView, new LayoutParams(
299 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
300 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800301 }
302
303 private static final int SAVE_CURRENT_PAGE = 1000;
304 private final Handler mHandler = new Handler() {
305 @Override
306 public void handleMessage(Message msg) {
307 if (msg.what == SAVE_CURRENT_PAGE) {
308 saveCurrentPage();
309 }
310 }
311 };
312
313 private AdapterView.OnItemClickListener mListener = new AdapterView.OnItemClickListener() {
314 public void onItemClick(AdapterView parent, View v, int position, long id) {
315 // It is possible that the view has been canceled when we get to
316 // this point as back has a higher priority
317 if (mCanceled) {
Leon Scroggins892df312009-07-14 14:48:02 -0400318 android.util.Log.e(LOGTAG, "item clicked when dismissing");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800319 return;
320 }
321 if (!mCreateShortcut) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400322 if (0 == position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800323 // XXX: Work-around for a framework issue.
324 mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE);
325 } else {
326 loadUrl(position);
327 }
328 } else {
Patrick Scott3918d442009-08-04 13:22:29 -0400329 final Intent intent = createShortcutIntent(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800330 setResultToParent(RESULT_OK, intent);
331 finish();
332 }
333 }
334 };
335
Patrick Scott3918d442009-08-04 13:22:29 -0400336 private Intent createShortcutIntent(int position) {
337 String url = getUrl(position);
338 String title = getBookmarkTitle(position);
339 Bitmap touchIcon = getTouchIcon(position);
340
The Android Open Source Project0c908882009-03-03 19:32:16 -0800341 final Intent i = new Intent();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700342 final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW,
343 Uri.parse(url));
344 long urlHash = url.hashCode();
345 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
346 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID,
347 Long.toString(uniqueId));
348 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800349 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Patrick Scott3918d442009-08-04 13:22:29 -0400350 // Use the apple-touch-icon if available
351 if (touchIcon != null) {
352 // Make a copy so we can modify the pixels.
353 Bitmap copy = touchIcon.copy(Bitmap.Config.ARGB_8888, true);
Patrick Scotte09761e2009-03-24 20:43:37 -0700354 Canvas canvas = new Canvas(copy);
355
Patrick Scott3918d442009-08-04 13:22:29 -0400356 // Construct a path from a round rect. This will allow drawing with
357 // an inverse fill so we can punch a hole using the round rect.
358 Path path = new Path();
359 path.setFillType(Path.FillType.INVERSE_WINDING);
360 path.addRoundRect(new RectF(0, 0, touchIcon.getWidth(),
361 touchIcon.getHeight()), 8f, 8f, Path.Direction.CW);
Patrick Scotte09761e2009-03-24 20:43:37 -0700362
Patrick Scott3918d442009-08-04 13:22:29 -0400363 // Construct a paint that clears the outside of the rectangle and
364 // draw.
365 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
366 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
367 canvas.drawPath(path, paint);
Patrick Scotte09761e2009-03-24 20:43:37 -0700368
Patrick Scotte09761e2009-03-24 20:43:37 -0700369 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
Patrick Scott3918d442009-08-04 13:22:29 -0400370 } else {
371 Bitmap favicon = getFavicon(position);
372 if (favicon == null) {
373 i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
374 Intent.ShortcutIconResource.fromContext(
375 BrowserBookmarksPage.this,
376 R.drawable.ic_launcher_shortcut_browser_bookmark));
377 } else {
378 Bitmap icon = BitmapFactory.decodeResource(getResources(),
379 R.drawable.ic_launcher_shortcut_browser_bookmark);
380
381 // Make a copy of the regular icon so we can modify the pixels.
382 Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true);
383 Canvas canvas = new Canvas(copy);
384
385 // Make a Paint for the white background rectangle and for
386 // filtering the favicon.
387 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG
388 | Paint.FILTER_BITMAP_FLAG);
389 p.setStyle(Paint.Style.FILL_AND_STROKE);
390 p.setColor(Color.WHITE);
391
392 // Create a rectangle that is slightly wider than the favicon
393 final float iconSize = 16; // 16x16 favicon
394 final float padding = 2; // white padding around icon
395 final float rectSize = iconSize + 2 * padding;
396 final float y = icon.getHeight() - rectSize;
397 RectF r = new RectF(0, y, rectSize, y + rectSize);
398
399 // Draw a white rounded rectangle behind the favicon
400 canvas.drawRoundRect(r, 2, 2, p);
401
402 // Draw the favicon in the same rectangle as the rounded
403 // rectangle but inset by the padding
404 // (results in a 16x16 favicon).
405 r.inset(padding, padding);
406 canvas.drawBitmap(favicon, null, r, p);
407 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
408 }
Patrick Scotte09761e2009-03-24 20:43:37 -0700409 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800410 // Do not allow duplicate items
411 i.putExtra("duplicate", false);
412 return i;
413 }
414
415 private void saveCurrentPage() {
416 Intent i = new Intent(BrowserBookmarksPage.this,
417 AddBookmarkPage.class);
418 i.putExtras(getIntent());
419 startActivityForResult(i, BOOKMARKS_SAVE);
420 }
421
422 private void loadUrl(int position) {
423 Intent intent = (new Intent()).setAction(getUrl(position));
424 setResultToParent(RESULT_OK, intent);
425 finish();
426 }
427
428 @Override
429 public boolean onCreateOptionsMenu(Menu menu) {
430 boolean result = super.onCreateOptionsMenu(menu);
431 if (!mCreateShortcut) {
432 MenuInflater inflater = getMenuInflater();
433 inflater.inflate(R.menu.bookmarks, menu);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400434 // Most visited page does not have an option to bookmark the last
435 // viewed page.
436 menu.findItem(R.id.new_context_menu_id).setVisible(!mMostVisited);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800437 return true;
438 }
439 return result;
440 }
441
442 @Override
Leon Scroggins0c786502009-08-04 16:04:55 -0400443 public boolean onPrepareOptionsMenu(Menu menu) {
Leon Scroggins8382d992009-08-19 11:25:14 -0400444 boolean result = super.onPrepareOptionsMenu(menu);
445 if (mCreateShortcut || mBookmarksAdapter.getCount() == 0) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400446 // No need to show the menu if there are no items.
Leon Scroggins8382d992009-08-19 11:25:14 -0400447 return result;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400448 }
Leon Scroggins0c786502009-08-04 16:04:55 -0400449 menu.findItem(R.id.switch_mode_menu_id).setTitle(
450 mGridMode ? R.string.switch_to_list
451 : R.string.switch_to_thumbnails);
452 return true;
453 }
454
455 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456 public boolean onOptionsItemSelected(MenuItem item) {
457 switch (item.getItemId()) {
Leon Scroggins892df312009-07-14 14:48:02 -0400458 case R.id.new_context_menu_id:
459 saveCurrentPage();
460 break;
461
462 case R.id.switch_mode_menu_id:
463 switchViewMode(!mGridMode);
464 break;
465
466 default:
467 return super.onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468 }
469 return true;
470 }
471
472 private void openInNewWindow(int position) {
473 Bundle b = new Bundle();
474 b.putBoolean("new_window", true);
475 setResultToParent(RESULT_OK,
476 (new Intent()).setAction(getUrl(position)).putExtras(b));
477
478 finish();
479 }
480
481
482 private void editBookmark(int position) {
483 Intent intent = new Intent(BrowserBookmarksPage.this,
484 AddBookmarkPage.class);
485 intent.putExtra("bookmark", getRow(position));
486 startActivityForResult(intent, BOOKMARKS_SAVE);
487 }
488
489 @Override
490 protected void onActivityResult(int requestCode, int resultCode,
491 Intent data) {
492 switch(requestCode) {
493 case BOOKMARKS_SAVE:
494 if (resultCode == RESULT_OK) {
495 Bundle extras;
496 if (data != null && (extras = data.getExtras()) != null) {
497 // If there are extras, then we need to save
498 // the edited bookmark. This is done in updateRow()
499 String title = extras.getString("title");
500 String url = extras.getString("url");
501 if (title != null && url != null) {
502 mBookmarksAdapter.updateRow(extras);
503 }
504 } else {
505 // extras == null then a new bookmark was added to
506 // the database.
507 refreshList();
508 }
509 }
510 break;
511 default:
512 break;
513 }
514 }
515
516 private void displayRemoveBookmarkDialog(int position) {
517 // Put up a dialog asking if the user really wants to
518 // delete the bookmark
519 final int deletePos = position;
520 new AlertDialog.Builder(this)
521 .setTitle(R.string.delete_bookmark)
522 .setIcon(android.R.drawable.ic_dialog_alert)
523 .setMessage(getText(R.string.delete_bookmark_warning).toString().replace(
524 "%s", getBookmarkTitle(deletePos)))
525 .setPositiveButton(R.string.ok,
526 new DialogInterface.OnClickListener() {
527 public void onClick(DialogInterface dialog, int whichButton) {
528 deleteBookmark(deletePos);
529 }
530 })
531 .setNegativeButton(R.string.cancel, null)
532 .show();
533 }
534
535 /**
536 * Refresh the shown list after the database has changed.
537 */
Leon Scroggins892df312009-07-14 14:48:02 -0400538 private void refreshList() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800539 mBookmarksAdapter.refreshList();
540 }
541
542 /**
543 * Return a hashmap representing the currently highlighted row.
544 */
545 public Bundle getRow(int position) {
546 return mBookmarksAdapter.getRow(position);
547 }
548
549 /**
550 * Return the url of the currently highlighted row.
551 */
552 public String getUrl(int position) {
553 return mBookmarksAdapter.getUrl(position);
554 }
555
Patrick Scotte09761e2009-03-24 20:43:37 -0700556 /**
557 * Return the favicon of the currently highlighted row.
558 */
559 public Bitmap getFavicon(int position) {
560 return mBookmarksAdapter.getFavicon(position);
561 }
562
Patrick Scott3918d442009-08-04 13:22:29 -0400563 private Bitmap getTouchIcon(int position) {
564 return mBookmarksAdapter.getTouchIcon(position);
565 }
566
The Android Open Source Project0c908882009-03-03 19:32:16 -0800567 private void copy(CharSequence text) {
568 try {
569 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
570 if (clip != null) {
571 clip.setClipboardText(text);
572 }
573 } catch (android.os.RemoteException e) {
574 Log.e(LOGTAG, "Copy failed", e);
575 }
576 }
577
578 public String getBookmarkTitle(int position) {
579 return mBookmarksAdapter.getTitle(position);
580 }
581
582 /**
583 * Delete the currently highlighted row.
584 */
585 public void deleteBookmark(int position) {
586 mBookmarksAdapter.deleteRow(position);
587 }
588
589 public boolean dispatchKeyEvent(KeyEvent event) {
590 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.isDown()) {
591 setResultToParent(RESULT_CANCELED, null);
592 mCanceled = true;
593 }
594 return super.dispatchKeyEvent(event);
595 }
596
597 // This Activity is generally a sub-Activity of CombinedHistoryActivity. In
598 // that situation, we need to pass our result code up to our parent.
599 // However, if someone calls this Activity directly, then this has no
600 // parent, and it needs to set it on itself.
601 private void setResultToParent(int resultCode, Intent data) {
602 Activity a = getParent() == null ? this : getParent();
603 a.setResult(resultCode, data);
604 }
605}