blob: 17a362b5171d7d302e2f3f0287a267431acf879b [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:
Andrei Popescu10fdba82009-09-24 13:25:47 +0100128 Browser.sendString(BrowserBookmarksPage.this, getUrl(i.position),
129 getText(R.string.choosertitle_sharevia).toString());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130 break;
131 case R.id.copy_url_context_menu_id:
132 copy(getUrl(i.position));
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400133 break;
134 case R.id.homepage_context_menu_id:
135 BrowserSettings.getInstance().setHomePage(this,
136 getUrl(i.position));
137 Toast.makeText(this, R.string.homepage_set,
138 Toast.LENGTH_LONG).show();
139 break;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400140 // Only for the Most visited page
141 case R.id.save_to_bookmarks_menu_id:
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400142 boolean isBookmark;
143 String name;
144 String url;
Ben Murdoch328ea872009-09-16 13:33:29 +0100145 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400146 isBookmark = mBookmarksAdapter.getIsBookmark(i.position);
147 name = mBookmarksAdapter.getTitle(i.position);
148 url = mBookmarksAdapter.getUrl(i.position);
149 } else {
150 HistoryItem historyItem = ((HistoryItem) i.targetView);
151 isBookmark = historyItem.isBookmark();
152 name = historyItem.getName();
153 url = historyItem.getUrl();
154 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400155 // If the site is bookmarked, the item becomes remove from
156 // bookmarks.
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400157 if (isBookmark) {
Andrei Popescuc9526192009-09-23 15:52:16 +0100158 Bookmarks.removeFromBookmarks(this, getContentResolver(), url, name);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400159 } else {
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400160 Browser.saveBookmark(this, name, url);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400161 }
162 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800163 default:
164 return super.onContextItemSelected(item);
165 }
166 return true;
167 }
168
169 @Override
170 public void onCreateContextMenu(ContextMenu menu, View v,
171 ContextMenuInfo menuInfo) {
172 AdapterView.AdapterContextMenuInfo i =
173 (AdapterView.AdapterContextMenuInfo) menuInfo;
174
175 MenuInflater inflater = getMenuInflater();
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400176 if (mMostVisited) {
177 inflater.inflate(R.menu.historycontext, menu);
178 } else {
179 inflater.inflate(R.menu.bookmarkscontext, menu);
180 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400182 if (0 == i.position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 menu.setGroupVisible(R.id.CONTEXT_MENU, false);
184 if (mAddHeader == null) {
185 mAddHeader = new AddNewBookmark(BrowserBookmarksPage.this);
186 } else if (mAddHeader.getParent() != null) {
187 ((ViewGroup) mAddHeader.getParent()).
188 removeView(mAddHeader);
189 }
Leon Scroggins892df312009-07-14 14:48:02 -0400190 mAddHeader.setUrl(getIntent().getStringExtra("url"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 menu.setHeaderView(mAddHeader);
192 return;
193 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400194 if (mMostVisited) {
Ben Murdoch328ea872009-09-16 13:33:29 +0100195 if ((mViewMode == BookmarkViewMode.LIST
196 && ((HistoryItem) i.targetView).isBookmark())
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400197 || mBookmarksAdapter.getIsBookmark(i.position)) {
198 MenuItem item = menu.findItem(
199 R.id.save_to_bookmarks_menu_id);
200 item.setTitle(R.string.remove_from_bookmarks);
201 }
202 } else {
203 // The historycontext menu has no ADD_MENU group.
204 menu.setGroupVisible(R.id.ADD_MENU, false);
205 }
Leon Scroggins190095d2009-08-17 17:01:38 -0400206 if (mDisableNewWindow) {
Leon Scroggins892df312009-07-14 14:48:02 -0400207 menu.findItem(R.id.new_window_context_menu_id).setVisible(
208 false);
209 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800210 if (mContextHeader == null) {
211 mContextHeader = new BookmarkItem(BrowserBookmarksPage.this);
212 } else if (mContextHeader.getParent() != null) {
213 ((ViewGroup) mContextHeader.getParent()).
214 removeView(mContextHeader);
215 }
Ben Murdoch328ea872009-09-16 13:33:29 +0100216 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400217 mBookmarksAdapter.populateBookmarkItem(mContextHeader,
218 i.position);
219 } else {
220 BookmarkItem b = (BookmarkItem) i.targetView;
221 b.copyTo(mContextHeader);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 }
Leon Scroggins892df312009-07-14 14:48:02 -0400223 menu.setHeaderView(mContextHeader);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800224 }
225
226 /**
227 * Create a new BrowserBookmarksPage.
228 */
229 @Override
230 protected void onCreate(Bundle icicle) {
231 super.onCreate(icicle);
232
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
234 mCreateShortcut = true;
235 }
Leon Scroggins190095d2009-08-17 17:01:38 -0400236 mDisableNewWindow = getIntent().getBooleanExtra("disable_new_window",
237 false);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400238 mMostVisited = getIntent().getBooleanExtra("mostVisited", false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800239
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400240 if (mCreateShortcut) {
241 setTitle(R.string.browser_bookmarks_page_bookmarks_text);
242 }
Leon Scroggins892df312009-07-14 14:48:02 -0400243 mBookmarksAdapter = new BrowserBookmarksAdapter(this,
Leon Scroggins89c6d362009-07-15 16:54:37 -0400244 getIntent().getStringExtra("url"),
Ben Murdochdcc2b6f2009-09-21 14:29:20 +0100245 getIntent().getStringExtra("title"),
246 (Bitmap) getIntent().getParcelableExtra("thumbnail"),
247 mCreateShortcut,
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400248 mMostVisited);
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400249
250 setContentView(R.layout.empty_history);
251 mEmptyView = findViewById(R.id.empty_view);
252 mEmptyView.setVisibility(View.GONE);
253
Ben Murdoch328ea872009-09-16 13:33:29 +0100254 SharedPreferences p = getPreferences(MODE_PRIVATE);
255
256 // See if the user has set a preference for the view mode of their
257 // bookmarks. Otherwise default to grid mode.
258 BookmarkViewMode preference = BookmarkViewMode.NONE;
259 if (mMostVisited) {
260 preference = BookmarkViewMode.values()[p.getInt(
261 PREF_MOST_VISITED_VIEW_MODE,
262 BookmarkViewMode.GRID.ordinal())];
263 } else {
264 preference = BookmarkViewMode.values()[p.getInt(
265 PREF_BOOKMARK_VIEW_MODE, BookmarkViewMode.GRID.ordinal())];
266 }
267 switchViewMode(preference);
Leon Scroggins892df312009-07-14 14:48:02 -0400268 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800269
Leon Scroggins892df312009-07-14 14:48:02 -0400270 /**
271 * Set the ContentView to be either the grid of thumbnails or the vertical
Ben Murdoch328ea872009-09-16 13:33:29 +0100272 * list.
Leon Scroggins892df312009-07-14 14:48:02 -0400273 */
Ben Murdoch328ea872009-09-16 13:33:29 +0100274 private void switchViewMode(BookmarkViewMode gridMode) {
275 if (mViewMode == gridMode) {
276 return;
277 }
278
279 mViewMode = gridMode;
280
281 // Update the preferences to make the new view mode sticky.
282 Editor ed = getPreferences(MODE_PRIVATE).edit();
283 if (mMostVisited) {
284 ed.putInt(PREF_MOST_VISITED_VIEW_MODE, mViewMode.ordinal());
285 } else {
286 ed.putInt(PREF_BOOKMARK_VIEW_MODE, mViewMode.ordinal());
287 }
288 ed.commit();
289
Leon Scroggins892df312009-07-14 14:48:02 -0400290 mBookmarksAdapter.switchViewMode(gridMode);
Ben Murdoch328ea872009-09-16 13:33:29 +0100291 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400292 if (mGridPage == null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -0400293 mGridPage = new GridView(this);
294 mGridPage.setAdapter(mBookmarksAdapter);
Leon Scroggins892df312009-07-14 14:48:02 -0400295 mGridPage.setOnItemClickListener(mListener);
Leon Scroggins89c6d362009-07-15 16:54:37 -0400296 mGridPage.setNumColumns(GridView.AUTO_FIT);
Leon Scroggins06ec5f22009-09-17 12:46:04 -0400297 mGridPage.setColumnWidth(BrowserActivity.THUMBNAIL_WIDTH);
Leon Scroggins89c6d362009-07-15 16:54:37 -0400298 mGridPage.setFocusable(true);
299 mGridPage.setFocusableInTouchMode(true);
300 mGridPage.setSelector(android.R.drawable.gallery_thumb);
301 mGridPage.setVerticalSpacing(10);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400302 if (mMostVisited) {
303 mGridPage.setEmptyView(mEmptyView);
304 }
Leon Scroggins892df312009-07-14 14:48:02 -0400305 if (!mCreateShortcut) {
306 mGridPage.setOnCreateContextMenuListener(this);
307 }
308 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400309 addContentView(mGridPage, FULL_SCREEN_PARAMS);
310 if (mVerticalList != null) {
311 ViewGroup parent = (ViewGroup) mVerticalList.getParent();
312 if (parent != null) {
313 parent.removeView(mVerticalList);
314 }
315 }
Leon Scroggins892df312009-07-14 14:48:02 -0400316 } else {
317 if (null == mVerticalList) {
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400318 ListView listView = new ListView(this);
Leon Scroggins892df312009-07-14 14:48:02 -0400319 listView.setAdapter(mBookmarksAdapter);
320 listView.setDrawSelectorOnTop(false);
321 listView.setVerticalScrollBarEnabled(true);
322 listView.setOnItemClickListener(mListener);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400323 if (mMostVisited) {
324 listView.setEmptyView(mEmptyView);
325 }
Leon Scroggins892df312009-07-14 14:48:02 -0400326 if (!mCreateShortcut) {
327 listView.setOnCreateContextMenuListener(this);
328 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400329 mVerticalList = listView;
Leon Scroggins892df312009-07-14 14:48:02 -0400330 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400331 addContentView(mVerticalList, FULL_SCREEN_PARAMS);
332 if (mGridPage != null) {
333 ViewGroup parent = (ViewGroup) mGridPage.getParent();
334 if (parent != null) {
335 parent.removeView(mGridPage);
336 }
337 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400338 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800339 }
340
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400341 private static final ViewGroup.LayoutParams FULL_SCREEN_PARAMS
342 = new ViewGroup.LayoutParams(
343 ViewGroup.LayoutParams.FILL_PARENT,
344 ViewGroup.LayoutParams.FILL_PARENT);
345
The Android Open Source Project0c908882009-03-03 19:32:16 -0800346 private static final int SAVE_CURRENT_PAGE = 1000;
347 private final Handler mHandler = new Handler() {
348 @Override
349 public void handleMessage(Message msg) {
350 if (msg.what == SAVE_CURRENT_PAGE) {
351 saveCurrentPage();
352 }
353 }
354 };
355
356 private AdapterView.OnItemClickListener mListener = new AdapterView.OnItemClickListener() {
357 public void onItemClick(AdapterView parent, View v, int position, long id) {
358 // It is possible that the view has been canceled when we get to
359 // this point as back has a higher priority
360 if (mCanceled) {
Leon Scroggins892df312009-07-14 14:48:02 -0400361 android.util.Log.e(LOGTAG, "item clicked when dismissing");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362 return;
363 }
364 if (!mCreateShortcut) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400365 if (0 == position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800366 // XXX: Work-around for a framework issue.
367 mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE);
368 } else {
369 loadUrl(position);
370 }
371 } else {
Patrick Scott3918d442009-08-04 13:22:29 -0400372 final Intent intent = createShortcutIntent(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800373 setResultToParent(RESULT_OK, intent);
374 finish();
375 }
376 }
377 };
378
Patrick Scott3918d442009-08-04 13:22:29 -0400379 private Intent createShortcutIntent(int position) {
380 String url = getUrl(position);
381 String title = getBookmarkTitle(position);
382 Bitmap touchIcon = getTouchIcon(position);
383
The Android Open Source Project0c908882009-03-03 19:32:16 -0800384 final Intent i = new Intent();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700385 final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW,
386 Uri.parse(url));
387 long urlHash = url.hashCode();
388 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
389 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID,
390 Long.toString(uniqueId));
391 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800392 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Patrick Scott3918d442009-08-04 13:22:29 -0400393 // Use the apple-touch-icon if available
394 if (touchIcon != null) {
395 // Make a copy so we can modify the pixels.
396 Bitmap copy = touchIcon.copy(Bitmap.Config.ARGB_8888, true);
Patrick Scotte09761e2009-03-24 20:43:37 -0700397 Canvas canvas = new Canvas(copy);
398
Patrick Scott3918d442009-08-04 13:22:29 -0400399 // Construct a path from a round rect. This will allow drawing with
400 // an inverse fill so we can punch a hole using the round rect.
401 Path path = new Path();
402 path.setFillType(Path.FillType.INVERSE_WINDING);
Patrick Scott59ce8302009-09-18 16:29:38 -0400403 RectF rect = new RectF(0, 0, touchIcon.getWidth(),
404 touchIcon.getHeight());
405 rect.inset(1, 1);
406 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
Patrick Scotte09761e2009-03-24 20:43:37 -0700407
Patrick Scott3918d442009-08-04 13:22:29 -0400408 // Construct a paint that clears the outside of the rectangle and
409 // draw.
410 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
411 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
412 canvas.drawPath(path, paint);
Patrick Scotte09761e2009-03-24 20:43:37 -0700413
Patrick Scotte09761e2009-03-24 20:43:37 -0700414 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
Patrick Scott3918d442009-08-04 13:22:29 -0400415 } else {
416 Bitmap favicon = getFavicon(position);
417 if (favicon == null) {
418 i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
419 Intent.ShortcutIconResource.fromContext(
420 BrowserBookmarksPage.this,
421 R.drawable.ic_launcher_shortcut_browser_bookmark));
422 } else {
423 Bitmap icon = BitmapFactory.decodeResource(getResources(),
424 R.drawable.ic_launcher_shortcut_browser_bookmark);
425
426 // Make a copy of the regular icon so we can modify the pixels.
427 Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true);
428 Canvas canvas = new Canvas(copy);
429
430 // Make a Paint for the white background rectangle and for
431 // filtering the favicon.
432 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG
433 | Paint.FILTER_BITMAP_FLAG);
434 p.setStyle(Paint.Style.FILL_AND_STROKE);
435 p.setColor(Color.WHITE);
436
437 // Create a rectangle that is slightly wider than the favicon
438 final float iconSize = 16; // 16x16 favicon
439 final float padding = 2; // white padding around icon
440 final float rectSize = iconSize + 2 * padding;
441 final float y = icon.getHeight() - rectSize;
442 RectF r = new RectF(0, y, rectSize, y + rectSize);
443
444 // Draw a white rounded rectangle behind the favicon
445 canvas.drawRoundRect(r, 2, 2, p);
446
447 // Draw the favicon in the same rectangle as the rounded
448 // rectangle but inset by the padding
449 // (results in a 16x16 favicon).
450 r.inset(padding, padding);
451 canvas.drawBitmap(favicon, null, r, p);
452 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
453 }
Patrick Scotte09761e2009-03-24 20:43:37 -0700454 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 // Do not allow duplicate items
456 i.putExtra("duplicate", false);
457 return i;
458 }
459
460 private void saveCurrentPage() {
461 Intent i = new Intent(BrowserBookmarksPage.this,
462 AddBookmarkPage.class);
463 i.putExtras(getIntent());
464 startActivityForResult(i, BOOKMARKS_SAVE);
465 }
466
467 private void loadUrl(int position) {
468 Intent intent = (new Intent()).setAction(getUrl(position));
469 setResultToParent(RESULT_OK, intent);
470 finish();
471 }
472
473 @Override
474 public boolean onCreateOptionsMenu(Menu menu) {
475 boolean result = super.onCreateOptionsMenu(menu);
476 if (!mCreateShortcut) {
477 MenuInflater inflater = getMenuInflater();
478 inflater.inflate(R.menu.bookmarks, menu);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400479 // Most visited page does not have an option to bookmark the last
480 // viewed page.
481 menu.findItem(R.id.new_context_menu_id).setVisible(!mMostVisited);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800482 return true;
483 }
484 return result;
485 }
486
487 @Override
Leon Scroggins0c786502009-08-04 16:04:55 -0400488 public boolean onPrepareOptionsMenu(Menu menu) {
Leon Scroggins8382d992009-08-19 11:25:14 -0400489 boolean result = super.onPrepareOptionsMenu(menu);
490 if (mCreateShortcut || mBookmarksAdapter.getCount() == 0) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400491 // No need to show the menu if there are no items.
Leon Scroggins8382d992009-08-19 11:25:14 -0400492 return result;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400493 }
Leon Scroggins0c786502009-08-04 16:04:55 -0400494 menu.findItem(R.id.switch_mode_menu_id).setTitle(
Ben Murdoch328ea872009-09-16 13:33:29 +0100495 mViewMode == BookmarkViewMode.GRID ? R.string.switch_to_list
Leon Scroggins0c786502009-08-04 16:04:55 -0400496 : R.string.switch_to_thumbnails);
497 return true;
498 }
499
500 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800501 public boolean onOptionsItemSelected(MenuItem item) {
502 switch (item.getItemId()) {
Leon Scroggins892df312009-07-14 14:48:02 -0400503 case R.id.new_context_menu_id:
504 saveCurrentPage();
505 break;
506
507 case R.id.switch_mode_menu_id:
Ben Murdoch328ea872009-09-16 13:33:29 +0100508 if (mViewMode == BookmarkViewMode.GRID) {
509 switchViewMode(BookmarkViewMode.LIST);
510 } else {
511 switchViewMode(BookmarkViewMode.GRID);
512 }
Leon Scroggins892df312009-07-14 14:48:02 -0400513 break;
514
515 default:
516 return super.onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800517 }
518 return true;
519 }
520
521 private void openInNewWindow(int position) {
522 Bundle b = new Bundle();
523 b.putBoolean("new_window", true);
524 setResultToParent(RESULT_OK,
525 (new Intent()).setAction(getUrl(position)).putExtras(b));
526
527 finish();
528 }
529
530
531 private void editBookmark(int position) {
532 Intent intent = new Intent(BrowserBookmarksPage.this,
533 AddBookmarkPage.class);
534 intent.putExtra("bookmark", getRow(position));
535 startActivityForResult(intent, BOOKMARKS_SAVE);
536 }
537
538 @Override
539 protected void onActivityResult(int requestCode, int resultCode,
540 Intent data) {
541 switch(requestCode) {
542 case BOOKMARKS_SAVE:
543 if (resultCode == RESULT_OK) {
544 Bundle extras;
545 if (data != null && (extras = data.getExtras()) != null) {
546 // If there are extras, then we need to save
547 // the edited bookmark. This is done in updateRow()
548 String title = extras.getString("title");
549 String url = extras.getString("url");
550 if (title != null && url != null) {
551 mBookmarksAdapter.updateRow(extras);
552 }
553 } else {
554 // extras == null then a new bookmark was added to
555 // the database.
556 refreshList();
557 }
558 }
559 break;
560 default:
561 break;
562 }
563 }
564
565 private void displayRemoveBookmarkDialog(int position) {
566 // Put up a dialog asking if the user really wants to
567 // delete the bookmark
568 final int deletePos = position;
569 new AlertDialog.Builder(this)
570 .setTitle(R.string.delete_bookmark)
571 .setIcon(android.R.drawable.ic_dialog_alert)
572 .setMessage(getText(R.string.delete_bookmark_warning).toString().replace(
573 "%s", getBookmarkTitle(deletePos)))
574 .setPositiveButton(R.string.ok,
575 new DialogInterface.OnClickListener() {
576 public void onClick(DialogInterface dialog, int whichButton) {
577 deleteBookmark(deletePos);
578 }
579 })
580 .setNegativeButton(R.string.cancel, null)
581 .show();
582 }
583
584 /**
585 * Refresh the shown list after the database has changed.
586 */
Leon Scroggins892df312009-07-14 14:48:02 -0400587 private void refreshList() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800588 mBookmarksAdapter.refreshList();
589 }
590
591 /**
592 * Return a hashmap representing the currently highlighted row.
593 */
594 public Bundle getRow(int position) {
595 return mBookmarksAdapter.getRow(position);
596 }
597
598 /**
599 * Return the url of the currently highlighted row.
600 */
601 public String getUrl(int position) {
602 return mBookmarksAdapter.getUrl(position);
603 }
604
Patrick Scotte09761e2009-03-24 20:43:37 -0700605 /**
606 * Return the favicon of the currently highlighted row.
607 */
608 public Bitmap getFavicon(int position) {
609 return mBookmarksAdapter.getFavicon(position);
610 }
611
Patrick Scott3918d442009-08-04 13:22:29 -0400612 private Bitmap getTouchIcon(int position) {
613 return mBookmarksAdapter.getTouchIcon(position);
614 }
615
The Android Open Source Project0c908882009-03-03 19:32:16 -0800616 private void copy(CharSequence text) {
617 try {
618 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
619 if (clip != null) {
620 clip.setClipboardText(text);
621 }
622 } catch (android.os.RemoteException e) {
623 Log.e(LOGTAG, "Copy failed", e);
624 }
625 }
626
627 public String getBookmarkTitle(int position) {
628 return mBookmarksAdapter.getTitle(position);
629 }
630
631 /**
632 * Delete the currently highlighted row.
633 */
634 public void deleteBookmark(int position) {
635 mBookmarksAdapter.deleteRow(position);
636 }
Grace Kloba5942df02009-09-18 11:48:29 -0700637
638 @Override
639 public void onBackPressed() {
640 setResultToParent(RESULT_CANCELED, null);
641 mCanceled = true;
642 super.onBackPressed();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 }
644
645 // This Activity is generally a sub-Activity of CombinedHistoryActivity. In
646 // that situation, we need to pass our result code up to our parent.
647 // However, if someone calls this Activity directly, then this has no
648 // parent, and it needs to set it on itself.
649 private void setResultToParent(int resultCode, Intent data) {
650 Activity a = getParent() == null ? this : getParent();
651 a.setResult(resultCode, data);
652 }
653}