blob: b91e322855ee656f6bec8fcff8d30702a5f6228c [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;
Ben Murdoch328ea872009-09-16 13:33:29 +010023import android.content.SharedPreferences;
24import android.content.SharedPreferences.Editor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.graphics.Bitmap;
Patrick Scotte09761e2009-03-24 20:43:37 -070026import android.graphics.BitmapFactory;
27import android.graphics.Canvas;
28import android.graphics.Color;
29import android.graphics.Paint;
Patrick Scott3918d442009-08-04 13:22:29 -040030import android.graphics.Path;
31import android.graphics.PorterDuff;
32import android.graphics.PorterDuffXfermode;
Patrick Scotte09761e2009-03-24 20:43:37 -070033import android.graphics.RectF;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.net.Uri;
35import android.os.Bundle;
36import android.os.Handler;
37import android.os.Message;
38import android.os.ServiceManager;
39import android.provider.Browser;
40import android.text.IClipboard;
41import android.util.Log;
42import android.view.ContextMenu;
43import android.view.KeyEvent;
Leon Scroggins892df312009-07-14 14:48:02 -040044import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.view.Menu;
46import android.view.MenuInflater;
47import android.view.MenuItem;
48import android.view.View;
49import android.view.ViewGroup;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040050import android.view.ViewGroup.LayoutParams;
51import android.view.ViewStub;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052import android.view.ContextMenu.ContextMenuInfo;
53import android.widget.AdapterView;
Leon Scroggins89c6d362009-07-15 16:54:37 -040054import android.widget.GridView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055import android.widget.ListView;
Leon Scrogginsfeb941d2009-05-28 17:27:38 -040056import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080057
Ben Murdoch328ea872009-09-16 13:33:29 +010058/*package*/ enum BookmarkViewMode { NONE, GRID, LIST }
The Android Open Source Project0c908882009-03-03 19:32:16 -080059/**
60 * View showing the user's bookmarks in the browser.
61 */
62public class BrowserBookmarksPage extends Activity implements
63 View.OnCreateContextMenuListener {
64
Ben Murdoch328ea872009-09-16 13:33:29 +010065 private BookmarkViewMode mViewMode = BookmarkViewMode.NONE;
Leon Scroggins89c6d362009-07-15 16:54:37 -040066 private GridView mGridPage;
Leon Scroggins892df312009-07-14 14:48:02 -040067 private View mVerticalList;
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 private BrowserBookmarksAdapter mBookmarksAdapter;
69 private static final int BOOKMARKS_SAVE = 1;
Leon Scroggins190095d2009-08-17 17:01:38 -040070 private boolean mDisableNewWindow;
The Android Open Source Project0c908882009-03-03 19:32:16 -080071 private BookmarkItem mContextHeader;
72 private AddNewBookmark mAddHeader;
73 private boolean mCanceled = false;
74 private boolean mCreateShortcut;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040075 private boolean mMostVisited;
76 private View mEmptyView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080077 // XXX: There is no public string defining this intent so if Home changes
78 // the value, we have to update this string.
79 private static final String INSTALL_SHORTCUT =
80 "com.android.launcher.action.INSTALL_SHORTCUT";
81
82 private final static String LOGTAG = "browser";
Ben Murdoch328ea872009-09-16 13:33:29 +010083 private final static String PREF_BOOKMARK_VIEW_MODE = "pref_bookmark_view_mode";
84 private final static String PREF_MOST_VISITED_VIEW_MODE = "pref_most_visited_view_mode";
The Android Open Source Project0c908882009-03-03 19:32:16 -080085
86 @Override
87 public boolean onContextItemSelected(MenuItem item) {
88 // It is possible that the view has been canceled when we get to
89 // this point as back has a higher priority
90 if (mCanceled) {
91 return true;
92 }
93 AdapterView.AdapterContextMenuInfo i =
94 (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
95 // If we have no menu info, we can't tell which item was selected.
96 if (i == null) {
97 return true;
98 }
99
100 switch (item.getItemId()) {
101 case R.id.new_context_menu_id:
102 saveCurrentPage();
103 break;
104 case R.id.open_context_menu_id:
105 loadUrl(i.position);
106 break;
107 case R.id.edit_context_menu_id:
108 editBookmark(i.position);
109 break;
110 case R.id.shortcut_context_menu_id:
Patrick Scott3918d442009-08-04 13:22:29 -0400111 final Intent send = createShortcutIntent(i.position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112 send.setAction(INSTALL_SHORTCUT);
113 sendBroadcast(send);
114 break;
115 case R.id.delete_context_menu_id:
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400116 if (mMostVisited) {
117 Browser.deleteFromHistory(getContentResolver(),
118 getUrl(i.position));
119 refreshList();
120 } else {
121 displayRemoveBookmarkDialog(i.position);
122 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123 break;
124 case R.id.new_window_context_menu_id:
125 openInNewWindow(i.position);
126 break;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400127 case R.id.share_link_context_menu_id:
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128 Browser.sendString(BrowserBookmarksPage.this, getUrl(i.position));
129 break;
130 case R.id.copy_url_context_menu_id:
131 copy(getUrl(i.position));
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400132 break;
133 case R.id.homepage_context_menu_id:
134 BrowserSettings.getInstance().setHomePage(this,
135 getUrl(i.position));
136 Toast.makeText(this, R.string.homepage_set,
137 Toast.LENGTH_LONG).show();
138 break;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400139 // Only for the Most visited page
140 case R.id.save_to_bookmarks_menu_id:
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400141 boolean isBookmark;
142 String name;
143 String url;
Ben Murdoch328ea872009-09-16 13:33:29 +0100144 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400145 isBookmark = mBookmarksAdapter.getIsBookmark(i.position);
146 name = mBookmarksAdapter.getTitle(i.position);
147 url = mBookmarksAdapter.getUrl(i.position);
148 } else {
149 HistoryItem historyItem = ((HistoryItem) i.targetView);
150 isBookmark = historyItem.isBookmark();
151 name = historyItem.getName();
152 url = historyItem.getUrl();
153 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400154 // If the site is bookmarked, the item becomes remove from
155 // bookmarks.
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400156 if (isBookmark) {
157 Bookmarks.removeFromBookmarks(this, getContentResolver(), url);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400158 } else {
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400159 Browser.saveBookmark(this, name, url);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400160 }
161 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162 default:
163 return super.onContextItemSelected(item);
164 }
165 return true;
166 }
167
168 @Override
169 public void onCreateContextMenu(ContextMenu menu, View v,
170 ContextMenuInfo menuInfo) {
171 AdapterView.AdapterContextMenuInfo i =
172 (AdapterView.AdapterContextMenuInfo) menuInfo;
173
174 MenuInflater inflater = getMenuInflater();
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400175 if (mMostVisited) {
176 inflater.inflate(R.menu.historycontext, menu);
177 } else {
178 inflater.inflate(R.menu.bookmarkscontext, menu);
179 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400181 if (0 == i.position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 menu.setGroupVisible(R.id.CONTEXT_MENU, false);
183 if (mAddHeader == null) {
184 mAddHeader = new AddNewBookmark(BrowserBookmarksPage.this);
185 } else if (mAddHeader.getParent() != null) {
186 ((ViewGroup) mAddHeader.getParent()).
187 removeView(mAddHeader);
188 }
Leon Scroggins892df312009-07-14 14:48:02 -0400189 mAddHeader.setUrl(getIntent().getStringExtra("url"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 menu.setHeaderView(mAddHeader);
191 return;
192 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400193 if (mMostVisited) {
Ben Murdoch328ea872009-09-16 13:33:29 +0100194 if ((mViewMode == BookmarkViewMode.LIST
195 && ((HistoryItem) i.targetView).isBookmark())
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400196 || mBookmarksAdapter.getIsBookmark(i.position)) {
197 MenuItem item = menu.findItem(
198 R.id.save_to_bookmarks_menu_id);
199 item.setTitle(R.string.remove_from_bookmarks);
200 }
201 } else {
202 // The historycontext menu has no ADD_MENU group.
203 menu.setGroupVisible(R.id.ADD_MENU, false);
204 }
Leon Scroggins190095d2009-08-17 17:01:38 -0400205 if (mDisableNewWindow) {
Leon Scroggins892df312009-07-14 14:48:02 -0400206 menu.findItem(R.id.new_window_context_menu_id).setVisible(
207 false);
208 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 if (mContextHeader == null) {
210 mContextHeader = new BookmarkItem(BrowserBookmarksPage.this);
211 } else if (mContextHeader.getParent() != null) {
212 ((ViewGroup) mContextHeader.getParent()).
213 removeView(mContextHeader);
214 }
Ben Murdoch328ea872009-09-16 13:33:29 +0100215 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400216 mBookmarksAdapter.populateBookmarkItem(mContextHeader,
217 i.position);
218 } else {
219 BookmarkItem b = (BookmarkItem) i.targetView;
220 b.copyTo(mContextHeader);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800221 }
Leon Scroggins892df312009-07-14 14:48:02 -0400222 menu.setHeaderView(mContextHeader);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800223 }
224
225 /**
226 * Create a new BrowserBookmarksPage.
227 */
228 @Override
229 protected void onCreate(Bundle icicle) {
230 super.onCreate(icicle);
231
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232 if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
233 mCreateShortcut = true;
234 }
Leon Scroggins190095d2009-08-17 17:01:38 -0400235 mDisableNewWindow = getIntent().getBooleanExtra("disable_new_window",
236 false);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400237 mMostVisited = getIntent().getBooleanExtra("mostVisited", false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400239 if (mCreateShortcut) {
240 setTitle(R.string.browser_bookmarks_page_bookmarks_text);
241 }
Leon Scroggins892df312009-07-14 14:48:02 -0400242 mBookmarksAdapter = new BrowserBookmarksAdapter(this,
Leon Scroggins89c6d362009-07-15 16:54:37 -0400243 getIntent().getStringExtra("url"),
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400244 getIntent().getStringExtra("title"), mCreateShortcut,
245 mMostVisited);
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400246
247 setContentView(R.layout.empty_history);
248 mEmptyView = findViewById(R.id.empty_view);
249 mEmptyView.setVisibility(View.GONE);
250
Ben Murdoch328ea872009-09-16 13:33:29 +0100251 SharedPreferences p = getPreferences(MODE_PRIVATE);
252
253 // See if the user has set a preference for the view mode of their
254 // bookmarks. Otherwise default to grid mode.
255 BookmarkViewMode preference = BookmarkViewMode.NONE;
256 if (mMostVisited) {
257 preference = BookmarkViewMode.values()[p.getInt(
258 PREF_MOST_VISITED_VIEW_MODE,
259 BookmarkViewMode.GRID.ordinal())];
260 } else {
261 preference = BookmarkViewMode.values()[p.getInt(
262 PREF_BOOKMARK_VIEW_MODE, BookmarkViewMode.GRID.ordinal())];
263 }
264 switchViewMode(preference);
Leon Scroggins892df312009-07-14 14:48:02 -0400265 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800266
Leon Scroggins892df312009-07-14 14:48:02 -0400267 /**
268 * Set the ContentView to be either the grid of thumbnails or the vertical
Ben Murdoch328ea872009-09-16 13:33:29 +0100269 * list.
Leon Scroggins892df312009-07-14 14:48:02 -0400270 */
Ben Murdoch328ea872009-09-16 13:33:29 +0100271 private void switchViewMode(BookmarkViewMode gridMode) {
272 if (mViewMode == gridMode) {
273 return;
274 }
275
276 mViewMode = gridMode;
277
278 // Update the preferences to make the new view mode sticky.
279 Editor ed = getPreferences(MODE_PRIVATE).edit();
280 if (mMostVisited) {
281 ed.putInt(PREF_MOST_VISITED_VIEW_MODE, mViewMode.ordinal());
282 } else {
283 ed.putInt(PREF_BOOKMARK_VIEW_MODE, mViewMode.ordinal());
284 }
285 ed.commit();
286
Leon Scroggins892df312009-07-14 14:48:02 -0400287 mBookmarksAdapter.switchViewMode(gridMode);
Ben Murdoch328ea872009-09-16 13:33:29 +0100288 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400289 if (mGridPage == null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -0400290 mGridPage = new GridView(this);
291 mGridPage.setAdapter(mBookmarksAdapter);
Leon Scroggins892df312009-07-14 14:48:02 -0400292 mGridPage.setOnItemClickListener(mListener);
Leon Scroggins89c6d362009-07-15 16:54:37 -0400293 mGridPage.setNumColumns(GridView.AUTO_FIT);
294 // Keep this in sync with bookmark_thumb and
295 // BrowserActivity.updateScreenshot
296 mGridPage.setColumnWidth(100);
297 mGridPage.setFocusable(true);
298 mGridPage.setFocusableInTouchMode(true);
299 mGridPage.setSelector(android.R.drawable.gallery_thumb);
300 mGridPage.setVerticalSpacing(10);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400301 if (mMostVisited) {
302 mGridPage.setEmptyView(mEmptyView);
303 }
Leon Scroggins892df312009-07-14 14:48:02 -0400304 if (!mCreateShortcut) {
305 mGridPage.setOnCreateContextMenuListener(this);
306 }
307 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400308 addContentView(mGridPage, FULL_SCREEN_PARAMS);
309 if (mVerticalList != null) {
310 ViewGroup parent = (ViewGroup) mVerticalList.getParent();
311 if (parent != null) {
312 parent.removeView(mVerticalList);
313 }
314 }
Leon Scroggins892df312009-07-14 14:48:02 -0400315 } else {
316 if (null == mVerticalList) {
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400317 ListView listView = new ListView(this);
Leon Scroggins892df312009-07-14 14:48:02 -0400318 listView.setAdapter(mBookmarksAdapter);
319 listView.setDrawSelectorOnTop(false);
320 listView.setVerticalScrollBarEnabled(true);
321 listView.setOnItemClickListener(mListener);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400322 if (mMostVisited) {
323 listView.setEmptyView(mEmptyView);
324 }
Leon Scroggins892df312009-07-14 14:48:02 -0400325 if (!mCreateShortcut) {
326 listView.setOnCreateContextMenuListener(this);
327 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400328 mVerticalList = listView;
Leon Scroggins892df312009-07-14 14:48:02 -0400329 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400330 addContentView(mVerticalList, FULL_SCREEN_PARAMS);
331 if (mGridPage != null) {
332 ViewGroup parent = (ViewGroup) mGridPage.getParent();
333 if (parent != null) {
334 parent.removeView(mGridPage);
335 }
336 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400337 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800338 }
339
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400340 private static final ViewGroup.LayoutParams FULL_SCREEN_PARAMS
341 = new ViewGroup.LayoutParams(
342 ViewGroup.LayoutParams.FILL_PARENT,
343 ViewGroup.LayoutParams.FILL_PARENT);
344
The Android Open Source Project0c908882009-03-03 19:32:16 -0800345 private static final int SAVE_CURRENT_PAGE = 1000;
346 private final Handler mHandler = new Handler() {
347 @Override
348 public void handleMessage(Message msg) {
349 if (msg.what == SAVE_CURRENT_PAGE) {
350 saveCurrentPage();
351 }
352 }
353 };
354
355 private AdapterView.OnItemClickListener mListener = new AdapterView.OnItemClickListener() {
356 public void onItemClick(AdapterView parent, View v, int position, long id) {
357 // It is possible that the view has been canceled when we get to
358 // this point as back has a higher priority
359 if (mCanceled) {
Leon Scroggins892df312009-07-14 14:48:02 -0400360 android.util.Log.e(LOGTAG, "item clicked when dismissing");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361 return;
362 }
363 if (!mCreateShortcut) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400364 if (0 == position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800365 // XXX: Work-around for a framework issue.
366 mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE);
367 } else {
368 loadUrl(position);
369 }
370 } else {
Patrick Scott3918d442009-08-04 13:22:29 -0400371 final Intent intent = createShortcutIntent(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800372 setResultToParent(RESULT_OK, intent);
373 finish();
374 }
375 }
376 };
377
Patrick Scott3918d442009-08-04 13:22:29 -0400378 private Intent createShortcutIntent(int position) {
379 String url = getUrl(position);
380 String title = getBookmarkTitle(position);
381 Bitmap touchIcon = getTouchIcon(position);
382
The Android Open Source Project0c908882009-03-03 19:32:16 -0800383 final Intent i = new Intent();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700384 final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW,
385 Uri.parse(url));
386 long urlHash = url.hashCode();
387 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
388 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID,
389 Long.toString(uniqueId));
390 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800391 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Patrick Scott3918d442009-08-04 13:22:29 -0400392 // Use the apple-touch-icon if available
393 if (touchIcon != null) {
394 // Make a copy so we can modify the pixels.
395 Bitmap copy = touchIcon.copy(Bitmap.Config.ARGB_8888, true);
Patrick Scotte09761e2009-03-24 20:43:37 -0700396 Canvas canvas = new Canvas(copy);
397
Patrick Scott3918d442009-08-04 13:22:29 -0400398 // Construct a path from a round rect. This will allow drawing with
399 // an inverse fill so we can punch a hole using the round rect.
400 Path path = new Path();
401 path.setFillType(Path.FillType.INVERSE_WINDING);
402 path.addRoundRect(new RectF(0, 0, touchIcon.getWidth(),
403 touchIcon.getHeight()), 8f, 8f, Path.Direction.CW);
Patrick Scotte09761e2009-03-24 20:43:37 -0700404
Patrick Scott3918d442009-08-04 13:22:29 -0400405 // Construct a paint that clears the outside of the rectangle and
406 // draw.
407 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
408 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
409 canvas.drawPath(path, paint);
Patrick Scotte09761e2009-03-24 20:43:37 -0700410
Patrick Scotte09761e2009-03-24 20:43:37 -0700411 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
Patrick Scott3918d442009-08-04 13:22:29 -0400412 } else {
413 Bitmap favicon = getFavicon(position);
414 if (favicon == null) {
415 i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
416 Intent.ShortcutIconResource.fromContext(
417 BrowserBookmarksPage.this,
418 R.drawable.ic_launcher_shortcut_browser_bookmark));
419 } else {
420 Bitmap icon = BitmapFactory.decodeResource(getResources(),
421 R.drawable.ic_launcher_shortcut_browser_bookmark);
422
423 // Make a copy of the regular icon so we can modify the pixels.
424 Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true);
425 Canvas canvas = new Canvas(copy);
426
427 // Make a Paint for the white background rectangle and for
428 // filtering the favicon.
429 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG
430 | Paint.FILTER_BITMAP_FLAG);
431 p.setStyle(Paint.Style.FILL_AND_STROKE);
432 p.setColor(Color.WHITE);
433
434 // Create a rectangle that is slightly wider than the favicon
435 final float iconSize = 16; // 16x16 favicon
436 final float padding = 2; // white padding around icon
437 final float rectSize = iconSize + 2 * padding;
438 final float y = icon.getHeight() - rectSize;
439 RectF r = new RectF(0, y, rectSize, y + rectSize);
440
441 // Draw a white rounded rectangle behind the favicon
442 canvas.drawRoundRect(r, 2, 2, p);
443
444 // Draw the favicon in the same rectangle as the rounded
445 // rectangle but inset by the padding
446 // (results in a 16x16 favicon).
447 r.inset(padding, padding);
448 canvas.drawBitmap(favicon, null, r, p);
449 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
450 }
Patrick Scotte09761e2009-03-24 20:43:37 -0700451 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 // Do not allow duplicate items
453 i.putExtra("duplicate", false);
454 return i;
455 }
456
457 private void saveCurrentPage() {
458 Intent i = new Intent(BrowserBookmarksPage.this,
459 AddBookmarkPage.class);
460 i.putExtras(getIntent());
461 startActivityForResult(i, BOOKMARKS_SAVE);
462 }
463
464 private void loadUrl(int position) {
465 Intent intent = (new Intent()).setAction(getUrl(position));
466 setResultToParent(RESULT_OK, intent);
467 finish();
468 }
469
470 @Override
471 public boolean onCreateOptionsMenu(Menu menu) {
472 boolean result = super.onCreateOptionsMenu(menu);
473 if (!mCreateShortcut) {
474 MenuInflater inflater = getMenuInflater();
475 inflater.inflate(R.menu.bookmarks, menu);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400476 // Most visited page does not have an option to bookmark the last
477 // viewed page.
478 menu.findItem(R.id.new_context_menu_id).setVisible(!mMostVisited);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800479 return true;
480 }
481 return result;
482 }
483
484 @Override
Leon Scroggins0c786502009-08-04 16:04:55 -0400485 public boolean onPrepareOptionsMenu(Menu menu) {
Leon Scroggins8382d992009-08-19 11:25:14 -0400486 boolean result = super.onPrepareOptionsMenu(menu);
487 if (mCreateShortcut || mBookmarksAdapter.getCount() == 0) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400488 // No need to show the menu if there are no items.
Leon Scroggins8382d992009-08-19 11:25:14 -0400489 return result;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400490 }
Leon Scroggins0c786502009-08-04 16:04:55 -0400491 menu.findItem(R.id.switch_mode_menu_id).setTitle(
Ben Murdoch328ea872009-09-16 13:33:29 +0100492 mViewMode == BookmarkViewMode.GRID ? R.string.switch_to_list
Leon Scroggins0c786502009-08-04 16:04:55 -0400493 : R.string.switch_to_thumbnails);
494 return true;
495 }
496
497 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 public boolean onOptionsItemSelected(MenuItem item) {
499 switch (item.getItemId()) {
Leon Scroggins892df312009-07-14 14:48:02 -0400500 case R.id.new_context_menu_id:
501 saveCurrentPage();
502 break;
503
504 case R.id.switch_mode_menu_id:
Ben Murdoch328ea872009-09-16 13:33:29 +0100505 if (mViewMode == BookmarkViewMode.GRID) {
506 switchViewMode(BookmarkViewMode.LIST);
507 } else {
508 switchViewMode(BookmarkViewMode.GRID);
509 }
Leon Scroggins892df312009-07-14 14:48:02 -0400510 break;
511
512 default:
513 return super.onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800514 }
515 return true;
516 }
517
518 private void openInNewWindow(int position) {
519 Bundle b = new Bundle();
520 b.putBoolean("new_window", true);
521 setResultToParent(RESULT_OK,
522 (new Intent()).setAction(getUrl(position)).putExtras(b));
523
524 finish();
525 }
526
527
528 private void editBookmark(int position) {
529 Intent intent = new Intent(BrowserBookmarksPage.this,
530 AddBookmarkPage.class);
531 intent.putExtra("bookmark", getRow(position));
532 startActivityForResult(intent, BOOKMARKS_SAVE);
533 }
534
535 @Override
536 protected void onActivityResult(int requestCode, int resultCode,
537 Intent data) {
538 switch(requestCode) {
539 case BOOKMARKS_SAVE:
540 if (resultCode == RESULT_OK) {
541 Bundle extras;
542 if (data != null && (extras = data.getExtras()) != null) {
543 // If there are extras, then we need to save
544 // the edited bookmark. This is done in updateRow()
545 String title = extras.getString("title");
546 String url = extras.getString("url");
547 if (title != null && url != null) {
548 mBookmarksAdapter.updateRow(extras);
549 }
550 } else {
551 // extras == null then a new bookmark was added to
552 // the database.
553 refreshList();
554 }
555 }
556 break;
557 default:
558 break;
559 }
560 }
561
562 private void displayRemoveBookmarkDialog(int position) {
563 // Put up a dialog asking if the user really wants to
564 // delete the bookmark
565 final int deletePos = position;
566 new AlertDialog.Builder(this)
567 .setTitle(R.string.delete_bookmark)
568 .setIcon(android.R.drawable.ic_dialog_alert)
569 .setMessage(getText(R.string.delete_bookmark_warning).toString().replace(
570 "%s", getBookmarkTitle(deletePos)))
571 .setPositiveButton(R.string.ok,
572 new DialogInterface.OnClickListener() {
573 public void onClick(DialogInterface dialog, int whichButton) {
574 deleteBookmark(deletePos);
575 }
576 })
577 .setNegativeButton(R.string.cancel, null)
578 .show();
579 }
580
581 /**
582 * Refresh the shown list after the database has changed.
583 */
Leon Scroggins892df312009-07-14 14:48:02 -0400584 private void refreshList() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800585 mBookmarksAdapter.refreshList();
586 }
587
588 /**
589 * Return a hashmap representing the currently highlighted row.
590 */
591 public Bundle getRow(int position) {
592 return mBookmarksAdapter.getRow(position);
593 }
594
595 /**
596 * Return the url of the currently highlighted row.
597 */
598 public String getUrl(int position) {
599 return mBookmarksAdapter.getUrl(position);
600 }
601
Patrick Scotte09761e2009-03-24 20:43:37 -0700602 /**
603 * Return the favicon of the currently highlighted row.
604 */
605 public Bitmap getFavicon(int position) {
606 return mBookmarksAdapter.getFavicon(position);
607 }
608
Patrick Scott3918d442009-08-04 13:22:29 -0400609 private Bitmap getTouchIcon(int position) {
610 return mBookmarksAdapter.getTouchIcon(position);
611 }
612
The Android Open Source Project0c908882009-03-03 19:32:16 -0800613 private void copy(CharSequence text) {
614 try {
615 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
616 if (clip != null) {
617 clip.setClipboardText(text);
618 }
619 } catch (android.os.RemoteException e) {
620 Log.e(LOGTAG, "Copy failed", e);
621 }
622 }
623
624 public String getBookmarkTitle(int position) {
625 return mBookmarksAdapter.getTitle(position);
626 }
627
628 /**
629 * Delete the currently highlighted row.
630 */
631 public void deleteBookmark(int position) {
632 mBookmarksAdapter.deleteRow(position);
633 }
Grace Kloba5942df02009-09-18 11:48:29 -0700634
635 @Override
636 public void onBackPressed() {
637 setResultToParent(RESULT_CANCELED, null);
638 mCanceled = true;
639 super.onBackPressed();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800640 }
641
642 // This Activity is generally a sub-Activity of CombinedHistoryActivity. In
643 // that situation, we need to pass our result code up to our parent.
644 // However, if someone calls this Activity directly, then this has no
645 // parent, and it needs to set it on itself.
646 private void setResultToParent(int resultCode, Intent data) {
647 Activity a = getParent() == null ? this : getParent();
648 a.setResult(resultCode, data);
649 }
650}