blob: bff7c796f23dfa11106d4bdfa834f9a5a2cc23f6 [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 Scottc0fdde92010-02-25 14:40:11 -050033import android.graphics.Rect;
Patrick Scotte09761e2009-03-24 20:43:37 -070034import android.graphics.RectF;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.net.Uri;
36import android.os.Bundle;
37import android.os.Handler;
38import android.os.Message;
39import android.os.ServiceManager;
40import android.provider.Browser;
41import android.text.IClipboard;
42import android.util.Log;
43import android.view.ContextMenu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.view.Menu;
45import android.view.MenuInflater;
46import android.view.MenuItem;
47import android.view.View;
48import android.view.ViewGroup;
49import android.view.ContextMenu.ContextMenuInfo;
Patrick Scottc1cf63a2010-03-09 16:02:08 -050050import android.webkit.WebIconDatabase.IconListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import 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
Ben Murdoch328ea872009-09-16 13:33:29 +010056/*package*/ enum BookmarkViewMode { NONE, GRID, LIST }
The Android Open Source Project0c908882009-03-03 19:32:16 -080057/**
58 * View showing the user's bookmarks in the browser.
59 */
60public class BrowserBookmarksPage extends Activity implements
61 View.OnCreateContextMenuListener {
62
Ben Murdoch328ea872009-09-16 13:33:29 +010063 private BookmarkViewMode mViewMode = BookmarkViewMode.NONE;
Leon Scroggins89c6d362009-07-15 16:54:37 -040064 private GridView mGridPage;
Leon Scrogginsea002572009-11-24 15:21:18 -050065 private ListView mVerticalList;
The Android Open Source Project0c908882009-03-03 19:32:16 -080066 private BrowserBookmarksAdapter mBookmarksAdapter;
67 private static final int BOOKMARKS_SAVE = 1;
Leon Scroggins190095d2009-08-17 17:01:38 -040068 private boolean mDisableNewWindow;
The Android Open Source Project0c908882009-03-03 19:32:16 -080069 private BookmarkItem mContextHeader;
70 private AddNewBookmark mAddHeader;
71 private boolean mCanceled = false;
72 private boolean mCreateShortcut;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040073 private boolean mMostVisited;
74 private View mEmptyView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080075 // XXX: There is no public string defining this intent so if Home changes
76 // the value, we have to update this string.
77 private static final String INSTALL_SHORTCUT =
78 "com.android.launcher.action.INSTALL_SHORTCUT";
79
80 private final static String LOGTAG = "browser";
Ben Murdoch328ea872009-09-16 13:33:29 +010081 private final static String PREF_BOOKMARK_VIEW_MODE = "pref_bookmark_view_mode";
82 private final static String PREF_MOST_VISITED_VIEW_MODE = "pref_most_visited_view_mode";
The Android Open Source Project0c908882009-03-03 19:32:16 -080083
84 @Override
85 public boolean onContextItemSelected(MenuItem item) {
86 // It is possible that the view has been canceled when we get to
87 // this point as back has a higher priority
88 if (mCanceled) {
89 return true;
90 }
91 AdapterView.AdapterContextMenuInfo i =
92 (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
93 // If we have no menu info, we can't tell which item was selected.
94 if (i == null) {
95 return true;
96 }
97
98 switch (item.getItemId()) {
99 case R.id.new_context_menu_id:
100 saveCurrentPage();
101 break;
102 case R.id.open_context_menu_id:
103 loadUrl(i.position);
104 break;
105 case R.id.edit_context_menu_id:
106 editBookmark(i.position);
107 break;
108 case R.id.shortcut_context_menu_id:
Patrick Scott3918d442009-08-04 13:22:29 -0400109 final Intent send = createShortcutIntent(i.position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 send.setAction(INSTALL_SHORTCUT);
111 sendBroadcast(send);
112 break;
113 case R.id.delete_context_menu_id:
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400114 if (mMostVisited) {
115 Browser.deleteFromHistory(getContentResolver(),
116 getUrl(i.position));
117 refreshList();
118 } else {
119 displayRemoveBookmarkDialog(i.position);
120 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 break;
122 case R.id.new_window_context_menu_id:
123 openInNewWindow(i.position);
124 break;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400125 case R.id.share_link_context_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -0500126 BrowserActivity.sharePage(BrowserBookmarksPage.this,
127 mBookmarksAdapter.getTitle(i.position), getUrl(i.position),
128 getFavicon(i.position),
129 mBookmarksAdapter.getScreenshot(i.position));
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 Scrogginsea002572009-11-24 15:21:18 -0500243 mHandler.obtainMessage(CREATE_ADAPTER).sendToTarget();
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400244
245 setContentView(R.layout.empty_history);
246 mEmptyView = findViewById(R.id.empty_view);
247 mEmptyView.setVisibility(View.GONE);
248
Ben Murdoch328ea872009-09-16 13:33:29 +0100249 SharedPreferences p = getPreferences(MODE_PRIVATE);
250
251 // See if the user has set a preference for the view mode of their
252 // bookmarks. Otherwise default to grid mode.
253 BookmarkViewMode preference = BookmarkViewMode.NONE;
254 if (mMostVisited) {
Leon Scrogginsb3968bb2009-10-16 09:04:16 -0400255 // For the most visited page, only use list mode.
256 preference = BookmarkViewMode.LIST;
Ben Murdoch328ea872009-09-16 13:33:29 +0100257 } else {
258 preference = BookmarkViewMode.values()[p.getInt(
259 PREF_BOOKMARK_VIEW_MODE, BookmarkViewMode.GRID.ordinal())];
260 }
261 switchViewMode(preference);
Leon Scroggins892df312009-07-14 14:48:02 -0400262 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263
Leon Scroggins892df312009-07-14 14:48:02 -0400264 /**
265 * Set the ContentView to be either the grid of thumbnails or the vertical
Ben Murdoch328ea872009-09-16 13:33:29 +0100266 * list.
Leon Scroggins892df312009-07-14 14:48:02 -0400267 */
Leon Scrogginsea002572009-11-24 15:21:18 -0500268 private void switchViewMode(BookmarkViewMode viewMode) {
269 if (mViewMode == viewMode) {
Ben Murdoch328ea872009-09-16 13:33:29 +0100270 return;
271 }
272
Leon Scrogginsea002572009-11-24 15:21:18 -0500273 mViewMode = viewMode;
Ben Murdoch328ea872009-09-16 13:33:29 +0100274
275 // Update the preferences to make the new view mode sticky.
276 Editor ed = getPreferences(MODE_PRIVATE).edit();
277 if (mMostVisited) {
278 ed.putInt(PREF_MOST_VISITED_VIEW_MODE, mViewMode.ordinal());
279 } else {
280 ed.putInt(PREF_BOOKMARK_VIEW_MODE, mViewMode.ordinal());
281 }
282 ed.commit();
283
Leon Scrogginsea002572009-11-24 15:21:18 -0500284 if (mBookmarksAdapter != null) {
285 mBookmarksAdapter.switchViewMode(viewMode);
286 }
Ben Murdoch328ea872009-09-16 13:33:29 +0100287 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400288 if (mGridPage == null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -0400289 mGridPage = new GridView(this);
Leon Scrogginsea002572009-11-24 15:21:18 -0500290 if (mBookmarksAdapter != null) {
291 mGridPage.setAdapter(mBookmarksAdapter);
292 }
Leon Scroggins892df312009-07-14 14:48:02 -0400293 mGridPage.setOnItemClickListener(mListener);
Leon Scroggins89c6d362009-07-15 16:54:37 -0400294 mGridPage.setNumColumns(GridView.AUTO_FIT);
Leon Scrogginsf8551612009-09-24 16:06:02 -0400295 mGridPage.setColumnWidth(
296 BrowserActivity.getDesiredThumbnailWidth(this));
Leon Scroggins89c6d362009-07-15 16:54:37 -0400297 mGridPage.setFocusable(true);
298 mGridPage.setFocusableInTouchMode(true);
299 mGridPage.setSelector(android.R.drawable.gallery_thumb);
Leon Scrogginsf8551612009-09-24 16:06:02 -0400300 float density = getResources().getDisplayMetrics().density;
301 mGridPage.setVerticalSpacing((int) (14 * density));
302 mGridPage.setHorizontalSpacing((int) (8 * density));
303 mGridPage.setStretchMode(GridView.STRETCH_SPACING);
Leon Scrogginsbbe6d5b2009-09-28 12:01:00 -0400304 mGridPage.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
Leon Scrogginsf8551612009-09-24 16:06:02 -0400305 mGridPage.setDrawSelectorOnTop(true);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400306 if (mMostVisited) {
307 mGridPage.setEmptyView(mEmptyView);
308 }
Leon Scroggins892df312009-07-14 14:48:02 -0400309 if (!mCreateShortcut) {
310 mGridPage.setOnCreateContextMenuListener(this);
311 }
312 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400313 addContentView(mGridPage, FULL_SCREEN_PARAMS);
314 if (mVerticalList != null) {
315 ViewGroup parent = (ViewGroup) mVerticalList.getParent();
316 if (parent != null) {
317 parent.removeView(mVerticalList);
318 }
319 }
Leon Scroggins892df312009-07-14 14:48:02 -0400320 } else {
321 if (null == mVerticalList) {
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400322 ListView listView = new ListView(this);
Leon Scrogginsea002572009-11-24 15:21:18 -0500323 if (mBookmarksAdapter != null) {
324 listView.setAdapter(mBookmarksAdapter);
325 }
Leon Scroggins892df312009-07-14 14:48:02 -0400326 listView.setDrawSelectorOnTop(false);
327 listView.setVerticalScrollBarEnabled(true);
328 listView.setOnItemClickListener(mListener);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400329 if (mMostVisited) {
330 listView.setEmptyView(mEmptyView);
331 }
Leon Scroggins892df312009-07-14 14:48:02 -0400332 if (!mCreateShortcut) {
333 listView.setOnCreateContextMenuListener(this);
334 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400335 mVerticalList = listView;
Leon Scroggins892df312009-07-14 14:48:02 -0400336 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400337 addContentView(mVerticalList, FULL_SCREEN_PARAMS);
338 if (mGridPage != null) {
339 ViewGroup parent = (ViewGroup) mGridPage.getParent();
340 if (parent != null) {
341 parent.removeView(mGridPage);
342 }
343 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400344 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800345 }
346
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400347 private static final ViewGroup.LayoutParams FULL_SCREEN_PARAMS
348 = new ViewGroup.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -0800349 ViewGroup.LayoutParams.MATCH_PARENT,
350 ViewGroup.LayoutParams.MATCH_PARENT);
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400351
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 private static final int SAVE_CURRENT_PAGE = 1000;
Leon Scrogginsea002572009-11-24 15:21:18 -0500353 private static final int CREATE_ADAPTER = 1001;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800354 private final Handler mHandler = new Handler() {
355 @Override
356 public void handleMessage(Message msg) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500357 switch (msg.what) {
358 case SAVE_CURRENT_PAGE:
359 saveCurrentPage();
360 break;
361 case CREATE_ADAPTER:
362 Intent intent = getIntent();
363 mBookmarksAdapter = new BrowserBookmarksAdapter(
364 BrowserBookmarksPage.this,
365 intent.getStringExtra("url"),
366 intent.getStringExtra("title"),
367 (Bitmap) intent.getParcelableExtra("thumbnail"),
368 mCreateShortcut,
369 mMostVisited);
370 mBookmarksAdapter.switchViewMode(mViewMode);
371 if (mGridPage != null) {
372 mGridPage.setAdapter(mBookmarksAdapter);
373 }
374 if (mVerticalList != null) {
375 mVerticalList.setAdapter(mBookmarksAdapter);
376 }
Patrick Scottc1cf63a2010-03-09 16:02:08 -0500377 // Add our own listener in case there are favicons that
378 // have yet to be loaded.
379 if (mMostVisited) {
380 IconListener listener = new IconListener() {
381 public void onReceivedIcon(String url,
382 Bitmap icon) {
383 if (mGridPage != null) {
384 mGridPage.setAdapter(mBookmarksAdapter);
385 }
386 if (mVerticalList != null) {
387 mVerticalList.setAdapter(mBookmarksAdapter);
388 }
389 }
390 };
391 CombinedBookmarkHistoryActivity.getIconListenerSet()
392 .addListener(listener);
393 }
Leon Scrogginsea002572009-11-24 15:21:18 -0500394 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800395 }
396 }
397 };
398
399 private AdapterView.OnItemClickListener mListener = new AdapterView.OnItemClickListener() {
400 public void onItemClick(AdapterView parent, View v, int position, long id) {
401 // It is possible that the view has been canceled when we get to
402 // this point as back has a higher priority
403 if (mCanceled) {
Leon Scroggins892df312009-07-14 14:48:02 -0400404 android.util.Log.e(LOGTAG, "item clicked when dismissing");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800405 return;
406 }
407 if (!mCreateShortcut) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400408 if (0 == position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800409 // XXX: Work-around for a framework issue.
410 mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE);
411 } else {
412 loadUrl(position);
413 }
414 } else {
Patrick Scott3918d442009-08-04 13:22:29 -0400415 final Intent intent = createShortcutIntent(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800416 setResultToParent(RESULT_OK, intent);
417 finish();
418 }
419 }
420 };
421
Patrick Scott3918d442009-08-04 13:22:29 -0400422 private Intent createShortcutIntent(int position) {
423 String url = getUrl(position);
424 String title = getBookmarkTitle(position);
425 Bitmap touchIcon = getTouchIcon(position);
426
The Android Open Source Project0c908882009-03-03 19:32:16 -0800427 final Intent i = new Intent();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700428 final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW,
429 Uri.parse(url));
430 long urlHash = url.hashCode();
431 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
432 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID,
433 Long.toString(uniqueId));
434 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800435 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Patrick Scott3918d442009-08-04 13:22:29 -0400436 // Use the apple-touch-icon if available
437 if (touchIcon != null) {
438 // Make a copy so we can modify the pixels.
439 Bitmap copy = touchIcon.copy(Bitmap.Config.ARGB_8888, true);
Patrick Scotte09761e2009-03-24 20:43:37 -0700440 Canvas canvas = new Canvas(copy);
441
Patrick Scott3918d442009-08-04 13:22:29 -0400442 // Construct a path from a round rect. This will allow drawing with
443 // an inverse fill so we can punch a hole using the round rect.
444 Path path = new Path();
445 path.setFillType(Path.FillType.INVERSE_WINDING);
Patrick Scott59ce8302009-09-18 16:29:38 -0400446 RectF rect = new RectF(0, 0, touchIcon.getWidth(),
447 touchIcon.getHeight());
448 rect.inset(1, 1);
449 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
Patrick Scotte09761e2009-03-24 20:43:37 -0700450
Patrick Scott3918d442009-08-04 13:22:29 -0400451 // Construct a paint that clears the outside of the rectangle and
452 // draw.
453 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
454 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
455 canvas.drawPath(path, paint);
Patrick Scotte09761e2009-03-24 20:43:37 -0700456
Patrick Scotte09761e2009-03-24 20:43:37 -0700457 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
Patrick Scott3918d442009-08-04 13:22:29 -0400458 } else {
459 Bitmap favicon = getFavicon(position);
460 if (favicon == null) {
461 i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
462 Intent.ShortcutIconResource.fromContext(
463 BrowserBookmarksPage.this,
464 R.drawable.ic_launcher_shortcut_browser_bookmark));
465 } else {
466 Bitmap icon = BitmapFactory.decodeResource(getResources(),
Patrick Scottc0fdde92010-02-25 14:40:11 -0500467 R.drawable.ic_launcher_shortcut_browser_bookmark_icon);
Patrick Scott3918d442009-08-04 13:22:29 -0400468
469 // Make a copy of the regular icon so we can modify the pixels.
470 Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true);
471 Canvas canvas = new Canvas(copy);
472
473 // Make a Paint for the white background rectangle and for
474 // filtering the favicon.
475 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG
476 | Paint.FILTER_BITMAP_FLAG);
477 p.setStyle(Paint.Style.FILL_AND_STROKE);
478 p.setColor(Color.WHITE);
479
Patrick Scottc0fdde92010-02-25 14:40:11 -0500480 final float density =
481 getResources().getDisplayMetrics().density;
Patrick Scott3918d442009-08-04 13:22:29 -0400482 // Create a rectangle that is slightly wider than the favicon
Patrick Scott4aff3aa2009-10-05 09:32:46 -0400483 final float iconSize = 16 * density; // 16x16 favicon
Patrick Scottc0fdde92010-02-25 14:40:11 -0500484 final float padding = 2 * density; // white padding around icon
Patrick Scott3918d442009-08-04 13:22:29 -0400485 final float rectSize = iconSize + 2 * padding;
Patrick Scottc0fdde92010-02-25 14:40:11 -0500486
487 final Rect iconBounds =
488 new Rect(0, 0, icon.getWidth(), icon.getHeight());
489 final float x = iconBounds.exactCenterX() - (rectSize / 2);
490 // Note: Subtract 2 dip from the y position since the box is
491 // slightly higher than center. Use padding since it is already
492 // 2 * density.
493 final float y = iconBounds.exactCenterY() - (rectSize / 2)
494 - padding;
495 RectF r = new RectF(x, y, x + rectSize, y + rectSize);
Patrick Scott3918d442009-08-04 13:22:29 -0400496
497 // Draw a white rounded rectangle behind the favicon
498 canvas.drawRoundRect(r, 2, 2, p);
499
500 // Draw the favicon in the same rectangle as the rounded
501 // rectangle but inset by the padding
502 // (results in a 16x16 favicon).
503 r.inset(padding, padding);
504 canvas.drawBitmap(favicon, null, r, p);
505 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
506 }
Patrick Scotte09761e2009-03-24 20:43:37 -0700507 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800508 // Do not allow duplicate items
509 i.putExtra("duplicate", false);
510 return i;
511 }
512
513 private void saveCurrentPage() {
514 Intent i = new Intent(BrowserBookmarksPage.this,
515 AddBookmarkPage.class);
516 i.putExtras(getIntent());
517 startActivityForResult(i, BOOKMARKS_SAVE);
518 }
519
520 private void loadUrl(int position) {
521 Intent intent = (new Intent()).setAction(getUrl(position));
522 setResultToParent(RESULT_OK, intent);
523 finish();
524 }
525
526 @Override
527 public boolean onCreateOptionsMenu(Menu menu) {
528 boolean result = super.onCreateOptionsMenu(menu);
Leon Scrogginsb3968bb2009-10-16 09:04:16 -0400529 if (!mCreateShortcut && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 MenuInflater inflater = getMenuInflater();
531 inflater.inflate(R.menu.bookmarks, menu);
532 return true;
533 }
534 return result;
535 }
536
537 @Override
Leon Scroggins0c786502009-08-04 16:04:55 -0400538 public boolean onPrepareOptionsMenu(Menu menu) {
Leon Scroggins8382d992009-08-19 11:25:14 -0400539 boolean result = super.onPrepareOptionsMenu(menu);
Leon Scrogginsea002572009-11-24 15:21:18 -0500540 if (mCreateShortcut || mMostVisited || mBookmarksAdapter == null
Leon Scrogginsb3968bb2009-10-16 09:04:16 -0400541 || mBookmarksAdapter.getCount() == 0) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400542 // No need to show the menu if there are no items.
Leon Scroggins8382d992009-08-19 11:25:14 -0400543 return result;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400544 }
Leon Scrogginsfdd10d72009-09-25 13:01:45 -0400545 MenuItem switchItem = menu.findItem(R.id.switch_mode_menu_id);
546 int titleResId;
547 int iconResId;
548 if (mViewMode == BookmarkViewMode.GRID) {
549 titleResId = R.string.switch_to_list;
550 iconResId = R.drawable.ic_menu_list;
551 } else {
552 titleResId = R.string.switch_to_thumbnails;
553 iconResId = R.drawable.ic_menu_thumbnail;
554 }
555 switchItem.setTitle(titleResId);
556 switchItem.setIcon(iconResId);
Leon Scroggins0c786502009-08-04 16:04:55 -0400557 return true;
558 }
559
560 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800561 public boolean onOptionsItemSelected(MenuItem item) {
562 switch (item.getItemId()) {
Leon Scroggins892df312009-07-14 14:48:02 -0400563 case R.id.new_context_menu_id:
564 saveCurrentPage();
565 break;
566
567 case R.id.switch_mode_menu_id:
Ben Murdoch328ea872009-09-16 13:33:29 +0100568 if (mViewMode == BookmarkViewMode.GRID) {
569 switchViewMode(BookmarkViewMode.LIST);
570 } else {
571 switchViewMode(BookmarkViewMode.GRID);
572 }
Leon Scroggins892df312009-07-14 14:48:02 -0400573 break;
574
575 default:
576 return super.onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800577 }
578 return true;
579 }
580
581 private void openInNewWindow(int position) {
582 Bundle b = new Bundle();
583 b.putBoolean("new_window", true);
584 setResultToParent(RESULT_OK,
585 (new Intent()).setAction(getUrl(position)).putExtras(b));
586
587 finish();
588 }
589
590
591 private void editBookmark(int position) {
592 Intent intent = new Intent(BrowserBookmarksPage.this,
593 AddBookmarkPage.class);
594 intent.putExtra("bookmark", getRow(position));
595 startActivityForResult(intent, BOOKMARKS_SAVE);
596 }
597
598 @Override
599 protected void onActivityResult(int requestCode, int resultCode,
600 Intent data) {
601 switch(requestCode) {
602 case BOOKMARKS_SAVE:
603 if (resultCode == RESULT_OK) {
604 Bundle extras;
605 if (data != null && (extras = data.getExtras()) != null) {
606 // If there are extras, then we need to save
607 // the edited bookmark. This is done in updateRow()
608 String title = extras.getString("title");
609 String url = extras.getString("url");
610 if (title != null && url != null) {
611 mBookmarksAdapter.updateRow(extras);
612 }
613 } else {
614 // extras == null then a new bookmark was added to
615 // the database.
616 refreshList();
617 }
618 }
619 break;
620 default:
621 break;
622 }
623 }
624
625 private void displayRemoveBookmarkDialog(int position) {
626 // Put up a dialog asking if the user really wants to
627 // delete the bookmark
628 final int deletePos = position;
629 new AlertDialog.Builder(this)
630 .setTitle(R.string.delete_bookmark)
631 .setIcon(android.R.drawable.ic_dialog_alert)
632 .setMessage(getText(R.string.delete_bookmark_warning).toString().replace(
633 "%s", getBookmarkTitle(deletePos)))
634 .setPositiveButton(R.string.ok,
635 new DialogInterface.OnClickListener() {
636 public void onClick(DialogInterface dialog, int whichButton) {
637 deleteBookmark(deletePos);
638 }
639 })
640 .setNegativeButton(R.string.cancel, null)
641 .show();
642 }
643
644 /**
645 * Refresh the shown list after the database has changed.
646 */
Leon Scroggins892df312009-07-14 14:48:02 -0400647 private void refreshList() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800648 mBookmarksAdapter.refreshList();
649 }
650
651 /**
652 * Return a hashmap representing the currently highlighted row.
653 */
654 public Bundle getRow(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500655 return mBookmarksAdapter == null ? null
656 : mBookmarksAdapter.getRow(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800657 }
658
659 /**
660 * Return the url of the currently highlighted row.
661 */
662 public String getUrl(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500663 return mBookmarksAdapter == null ? null
664 : mBookmarksAdapter.getUrl(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800665 }
666
Patrick Scotte09761e2009-03-24 20:43:37 -0700667 /**
668 * Return the favicon of the currently highlighted row.
669 */
670 public Bitmap getFavicon(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500671 return mBookmarksAdapter == null ? null
672 : mBookmarksAdapter.getFavicon(position);
Patrick Scotte09761e2009-03-24 20:43:37 -0700673 }
674
Patrick Scott3918d442009-08-04 13:22:29 -0400675 private Bitmap getTouchIcon(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500676 return mBookmarksAdapter == null ? null
677 : mBookmarksAdapter.getTouchIcon(position);
Patrick Scott3918d442009-08-04 13:22:29 -0400678 }
679
The Android Open Source Project0c908882009-03-03 19:32:16 -0800680 private void copy(CharSequence text) {
681 try {
682 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
683 if (clip != null) {
684 clip.setClipboardText(text);
685 }
686 } catch (android.os.RemoteException e) {
687 Log.e(LOGTAG, "Copy failed", e);
688 }
689 }
690
691 public String getBookmarkTitle(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500692 return mBookmarksAdapter == null ? null
693 : mBookmarksAdapter.getTitle(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800694 }
695
696 /**
697 * Delete the currently highlighted row.
698 */
699 public void deleteBookmark(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500700 if (mBookmarksAdapter == null) return;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800701 mBookmarksAdapter.deleteRow(position);
702 }
Grace Kloba5942df02009-09-18 11:48:29 -0700703
704 @Override
705 public void onBackPressed() {
706 setResultToParent(RESULT_CANCELED, null);
707 mCanceled = true;
708 super.onBackPressed();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800709 }
710
Leon Scrogginsfde97462010-01-11 13:06:21 -0500711 // This Activity is generally a sub-Activity of
712 // CombinedBookmarkHistoryActivity. In that situation, we need to pass our
713 // result code up to our parent. However, if someone calls this Activity
714 // directly, then this has no parent, and it needs to set it on itself.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800715 private void setResultToParent(int resultCode, Intent data) {
Leon Scrogginsfde97462010-01-11 13:06:21 -0500716 Activity parent = getParent();
717 if (parent == null) {
718 setResult(resultCode, data);
719 } else {
720 ((CombinedBookmarkHistoryActivity) parent).setResultFromChild(
721 resultCode, data);
722 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800723 }
724}