The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.content.DialogInterface; |
| 22 | import android.content.Intent; |
| 23 | import android.graphics.Bitmap; |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 24 | import android.graphics.BitmapFactory; |
| 25 | import android.graphics.Canvas; |
| 26 | import android.graphics.Color; |
| 27 | import android.graphics.Paint; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 28 | import android.graphics.Path; |
| 29 | import android.graphics.PorterDuff; |
| 30 | import android.graphics.PorterDuffXfermode; |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 31 | import android.graphics.RectF; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 32 | import android.net.Uri; |
| 33 | import android.os.Bundle; |
| 34 | import android.os.Handler; |
| 35 | import android.os.Message; |
| 36 | import android.os.ServiceManager; |
| 37 | import android.provider.Browser; |
| 38 | import android.text.IClipboard; |
| 39 | import android.util.Log; |
| 40 | import android.view.ContextMenu; |
| 41 | import android.view.KeyEvent; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 42 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 43 | import android.view.Menu; |
| 44 | import android.view.MenuInflater; |
| 45 | import android.view.MenuItem; |
| 46 | import android.view.View; |
| 47 | import android.view.ViewGroup; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 48 | import android.view.ViewGroup.LayoutParams; |
| 49 | import android.view.ViewStub; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | import android.view.ContextMenu.ContextMenuInfo; |
| 51 | import android.widget.AdapterView; |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 52 | import android.widget.GridView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 53 | import android.widget.ListView; |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 54 | import android.widget.Toast; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * View showing the user's bookmarks in the browser. |
| 58 | */ |
| 59 | public class BrowserBookmarksPage extends Activity implements |
| 60 | View.OnCreateContextMenuListener { |
| 61 | |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 62 | private boolean mGridMode; |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 63 | private GridView mGridPage; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 64 | private View mVerticalList; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 65 | private BrowserBookmarksAdapter mBookmarksAdapter; |
| 66 | private static final int BOOKMARKS_SAVE = 1; |
| 67 | private boolean mMaxTabsOpen; |
| 68 | private BookmarkItem mContextHeader; |
| 69 | private AddNewBookmark mAddHeader; |
| 70 | private boolean mCanceled = false; |
| 71 | private boolean mCreateShortcut; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 72 | private boolean mMostVisited; |
| 73 | private View mEmptyView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 74 | // 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 Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 107 | final Intent send = createShortcutIntent(i.position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 108 | send.setAction(INSTALL_SHORTCUT); |
| 109 | sendBroadcast(send); |
| 110 | break; |
| 111 | case R.id.delete_context_menu_id: |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 112 | if (mMostVisited) { |
| 113 | Browser.deleteFromHistory(getContentResolver(), |
| 114 | getUrl(i.position)); |
| 115 | refreshList(); |
| 116 | } else { |
| 117 | displayRemoveBookmarkDialog(i.position); |
| 118 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 119 | break; |
| 120 | case R.id.new_window_context_menu_id: |
| 121 | openInNewWindow(i.position); |
| 122 | break; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 123 | case R.id.share_link_context_menu_id: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 124 | Browser.sendString(BrowserBookmarksPage.this, getUrl(i.position)); |
| 125 | break; |
| 126 | case R.id.copy_url_context_menu_id: |
| 127 | copy(getUrl(i.position)); |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 128 | 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 Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 135 | // Only for the Most visited page |
| 136 | case R.id.save_to_bookmarks_menu_id: |
Leon Scroggins | c1f5759 | 2009-08-14 14:16:10 -0400 | [diff] [blame^] | 137 | 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 Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 150 | // If the site is bookmarked, the item becomes remove from |
| 151 | // bookmarks. |
Leon Scroggins | c1f5759 | 2009-08-14 14:16:10 -0400 | [diff] [blame^] | 152 | if (isBookmark) { |
| 153 | Bookmarks.removeFromBookmarks(this, getContentResolver(), url); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 154 | } else { |
Leon Scroggins | c1f5759 | 2009-08-14 14:16:10 -0400 | [diff] [blame^] | 155 | Browser.saveBookmark(this, name, url); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 156 | } |
| 157 | break; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 158 | 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 Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 171 | if (mMostVisited) { |
| 172 | inflater.inflate(R.menu.historycontext, menu); |
| 173 | } else { |
| 174 | inflater.inflate(R.menu.bookmarkscontext, menu); |
| 175 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 176 | |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 177 | if (0 == i.position && !mMostVisited) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 178 | 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 Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 185 | mAddHeader.setUrl(getIntent().getStringExtra("url")); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 186 | menu.setHeaderView(mAddHeader); |
| 187 | return; |
| 188 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 189 | 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 Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 200 | if (mMaxTabsOpen) { |
| 201 | menu.findItem(R.id.new_window_context_menu_id).setVisible( |
| 202 | false); |
| 203 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 204 | if (mContextHeader == null) { |
| 205 | mContextHeader = new BookmarkItem(BrowserBookmarksPage.this); |
| 206 | } else if (mContextHeader.getParent() != null) { |
| 207 | ((ViewGroup) mContextHeader.getParent()). |
| 208 | removeView(mContextHeader); |
| 209 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 210 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 216 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 217 | menu.setHeaderView(mContextHeader); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 218 | } |
| 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 227 | if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) { |
| 228 | mCreateShortcut = true; |
| 229 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 230 | mMaxTabsOpen = getIntent().getBooleanExtra("maxTabsOpen", false); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 231 | mMostVisited = getIntent().getBooleanExtra("mostVisited", false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 232 | |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 233 | if (mCreateShortcut) { |
| 234 | setTitle(R.string.browser_bookmarks_page_bookmarks_text); |
| 235 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 236 | mBookmarksAdapter = new BrowserBookmarksAdapter(this, |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 237 | getIntent().getStringExtra("url"), |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 238 | getIntent().getStringExtra("title"), mCreateShortcut, |
| 239 | mMostVisited); |
| 240 | if (mMostVisited) { |
| 241 | mEmptyView = new ViewStub(this, R.layout.empty_history); |
| 242 | } |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 243 | switchViewMode(true); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 244 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 245 | |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 246 | /** |
| 247 | * Set the ContentView to be either the grid of thumbnails or the vertical |
| 248 | * list. Pass true to set it to the grid. |
| 249 | */ |
| 250 | private void switchViewMode(boolean gridMode) { |
| 251 | mGridMode = gridMode; |
| 252 | mBookmarksAdapter.switchViewMode(gridMode); |
| 253 | if (mGridMode) { |
| 254 | if (mGridPage == null) { |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 255 | mGridPage = new GridView(this); |
| 256 | mGridPage.setAdapter(mBookmarksAdapter); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 257 | mGridPage.setOnItemClickListener(mListener); |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 258 | mGridPage.setNumColumns(GridView.AUTO_FIT); |
| 259 | // Keep this in sync with bookmark_thumb and |
| 260 | // BrowserActivity.updateScreenshot |
| 261 | mGridPage.setColumnWidth(100); |
| 262 | mGridPage.setFocusable(true); |
| 263 | mGridPage.setFocusableInTouchMode(true); |
| 264 | mGridPage.setSelector(android.R.drawable.gallery_thumb); |
| 265 | mGridPage.setVerticalSpacing(10); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 266 | if (mMostVisited) { |
| 267 | mGridPage.setEmptyView(mEmptyView); |
| 268 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 269 | if (!mCreateShortcut) { |
| 270 | mGridPage.setOnCreateContextMenuListener(this); |
| 271 | } |
| 272 | } |
| 273 | setContentView(mGridPage); |
| 274 | } else { |
| 275 | if (null == mVerticalList) { |
| 276 | LayoutInflater factory = LayoutInflater.from(this); |
| 277 | mVerticalList = factory.inflate(R.layout.browser_bookmarks_page, |
| 278 | null); |
| 279 | |
| 280 | ListView listView |
| 281 | = (ListView) mVerticalList.findViewById(R.id.list); |
| 282 | listView.setAdapter(mBookmarksAdapter); |
| 283 | listView.setDrawSelectorOnTop(false); |
| 284 | listView.setVerticalScrollBarEnabled(true); |
| 285 | listView.setOnItemClickListener(mListener); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 286 | if (mMostVisited) { |
| 287 | listView.setEmptyView(mEmptyView); |
| 288 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 289 | |
| 290 | if (!mCreateShortcut) { |
| 291 | listView.setOnCreateContextMenuListener(this); |
| 292 | } |
| 293 | } |
| 294 | setContentView(mVerticalList); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 295 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 296 | if (mMostVisited) { |
| 297 | addContentView(mEmptyView, new LayoutParams( |
| 298 | LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); |
| 299 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | private static final int SAVE_CURRENT_PAGE = 1000; |
| 303 | private final Handler mHandler = new Handler() { |
| 304 | @Override |
| 305 | public void handleMessage(Message msg) { |
| 306 | if (msg.what == SAVE_CURRENT_PAGE) { |
| 307 | saveCurrentPage(); |
| 308 | } |
| 309 | } |
| 310 | }; |
| 311 | |
| 312 | private AdapterView.OnItemClickListener mListener = new AdapterView.OnItemClickListener() { |
| 313 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
| 314 | // It is possible that the view has been canceled when we get to |
| 315 | // this point as back has a higher priority |
| 316 | if (mCanceled) { |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 317 | android.util.Log.e(LOGTAG, "item clicked when dismissing"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | if (!mCreateShortcut) { |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 321 | if (0 == position && !mMostVisited) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 322 | // XXX: Work-around for a framework issue. |
| 323 | mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE); |
| 324 | } else { |
| 325 | loadUrl(position); |
| 326 | } |
| 327 | } else { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 328 | final Intent intent = createShortcutIntent(position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 329 | setResultToParent(RESULT_OK, intent); |
| 330 | finish(); |
| 331 | } |
| 332 | } |
| 333 | }; |
| 334 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 335 | private Intent createShortcutIntent(int position) { |
| 336 | String url = getUrl(position); |
| 337 | String title = getBookmarkTitle(position); |
| 338 | Bitmap touchIcon = getTouchIcon(position); |
| 339 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 340 | final Intent i = new Intent(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 341 | final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, |
| 342 | Uri.parse(url)); |
| 343 | long urlHash = url.hashCode(); |
| 344 | long uniqueId = (urlHash << 32) | shortcutIntent.hashCode(); |
| 345 | shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, |
| 346 | Long.toString(uniqueId)); |
| 347 | i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 348 | i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 349 | // Use the apple-touch-icon if available |
| 350 | if (touchIcon != null) { |
| 351 | // Make a copy so we can modify the pixels. |
| 352 | Bitmap copy = touchIcon.copy(Bitmap.Config.ARGB_8888, true); |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 353 | Canvas canvas = new Canvas(copy); |
| 354 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 355 | // Construct a path from a round rect. This will allow drawing with |
| 356 | // an inverse fill so we can punch a hole using the round rect. |
| 357 | Path path = new Path(); |
| 358 | path.setFillType(Path.FillType.INVERSE_WINDING); |
| 359 | path.addRoundRect(new RectF(0, 0, touchIcon.getWidth(), |
| 360 | touchIcon.getHeight()), 8f, 8f, Path.Direction.CW); |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 361 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 362 | // Construct a paint that clears the outside of the rectangle and |
| 363 | // draw. |
| 364 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 365 | paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); |
| 366 | canvas.drawPath(path, paint); |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 367 | |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 368 | i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 369 | } else { |
| 370 | Bitmap favicon = getFavicon(position); |
| 371 | if (favicon == null) { |
| 372 | i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, |
| 373 | Intent.ShortcutIconResource.fromContext( |
| 374 | BrowserBookmarksPage.this, |
| 375 | R.drawable.ic_launcher_shortcut_browser_bookmark)); |
| 376 | } else { |
| 377 | Bitmap icon = BitmapFactory.decodeResource(getResources(), |
| 378 | R.drawable.ic_launcher_shortcut_browser_bookmark); |
| 379 | |
| 380 | // Make a copy of the regular icon so we can modify the pixels. |
| 381 | Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true); |
| 382 | Canvas canvas = new Canvas(copy); |
| 383 | |
| 384 | // Make a Paint for the white background rectangle and for |
| 385 | // filtering the favicon. |
| 386 | Paint p = new Paint(Paint.ANTI_ALIAS_FLAG |
| 387 | | Paint.FILTER_BITMAP_FLAG); |
| 388 | p.setStyle(Paint.Style.FILL_AND_STROKE); |
| 389 | p.setColor(Color.WHITE); |
| 390 | |
| 391 | // Create a rectangle that is slightly wider than the favicon |
| 392 | final float iconSize = 16; // 16x16 favicon |
| 393 | final float padding = 2; // white padding around icon |
| 394 | final float rectSize = iconSize + 2 * padding; |
| 395 | final float y = icon.getHeight() - rectSize; |
| 396 | RectF r = new RectF(0, y, rectSize, y + rectSize); |
| 397 | |
| 398 | // Draw a white rounded rectangle behind the favicon |
| 399 | canvas.drawRoundRect(r, 2, 2, p); |
| 400 | |
| 401 | // Draw the favicon in the same rectangle as the rounded |
| 402 | // rectangle but inset by the padding |
| 403 | // (results in a 16x16 favicon). |
| 404 | r.inset(padding, padding); |
| 405 | canvas.drawBitmap(favicon, null, r, p); |
| 406 | i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy); |
| 407 | } |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 408 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 409 | // Do not allow duplicate items |
| 410 | i.putExtra("duplicate", false); |
| 411 | return i; |
| 412 | } |
| 413 | |
| 414 | private void saveCurrentPage() { |
| 415 | Intent i = new Intent(BrowserBookmarksPage.this, |
| 416 | AddBookmarkPage.class); |
| 417 | i.putExtras(getIntent()); |
| 418 | startActivityForResult(i, BOOKMARKS_SAVE); |
| 419 | } |
| 420 | |
| 421 | private void loadUrl(int position) { |
| 422 | Intent intent = (new Intent()).setAction(getUrl(position)); |
| 423 | setResultToParent(RESULT_OK, intent); |
| 424 | finish(); |
| 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public boolean onCreateOptionsMenu(Menu menu) { |
| 429 | boolean result = super.onCreateOptionsMenu(menu); |
| 430 | if (!mCreateShortcut) { |
| 431 | MenuInflater inflater = getMenuInflater(); |
| 432 | inflater.inflate(R.menu.bookmarks, menu); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 433 | // Most visited page does not have an option to bookmark the last |
| 434 | // viewed page. |
| 435 | menu.findItem(R.id.new_context_menu_id).setVisible(!mMostVisited); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | return result; |
| 439 | } |
| 440 | |
| 441 | @Override |
Leon Scroggins | 0c78650 | 2009-08-04 16:04:55 -0400 | [diff] [blame] | 442 | public boolean onPrepareOptionsMenu(Menu menu) { |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 443 | if (mBookmarksAdapter.getCount() == 0) { |
| 444 | // No need to show the menu if there are no items. |
| 445 | return false; |
| 446 | } |
Leon Scroggins | 0c78650 | 2009-08-04 16:04:55 -0400 | [diff] [blame] | 447 | menu.findItem(R.id.switch_mode_menu_id).setTitle( |
| 448 | mGridMode ? R.string.switch_to_list |
| 449 | : R.string.switch_to_thumbnails); |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | @Override |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 454 | public boolean onOptionsItemSelected(MenuItem item) { |
| 455 | switch (item.getItemId()) { |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 456 | case R.id.new_context_menu_id: |
| 457 | saveCurrentPage(); |
| 458 | break; |
| 459 | |
| 460 | case R.id.switch_mode_menu_id: |
| 461 | switchViewMode(!mGridMode); |
| 462 | break; |
| 463 | |
| 464 | default: |
| 465 | return super.onOptionsItemSelected(item); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 466 | } |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | private void openInNewWindow(int position) { |
| 471 | Bundle b = new Bundle(); |
| 472 | b.putBoolean("new_window", true); |
| 473 | setResultToParent(RESULT_OK, |
| 474 | (new Intent()).setAction(getUrl(position)).putExtras(b)); |
| 475 | |
| 476 | finish(); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | private void editBookmark(int position) { |
| 481 | Intent intent = new Intent(BrowserBookmarksPage.this, |
| 482 | AddBookmarkPage.class); |
| 483 | intent.putExtra("bookmark", getRow(position)); |
| 484 | startActivityForResult(intent, BOOKMARKS_SAVE); |
| 485 | } |
| 486 | |
| 487 | @Override |
| 488 | protected void onActivityResult(int requestCode, int resultCode, |
| 489 | Intent data) { |
| 490 | switch(requestCode) { |
| 491 | case BOOKMARKS_SAVE: |
| 492 | if (resultCode == RESULT_OK) { |
| 493 | Bundle extras; |
| 494 | if (data != null && (extras = data.getExtras()) != null) { |
| 495 | // If there are extras, then we need to save |
| 496 | // the edited bookmark. This is done in updateRow() |
| 497 | String title = extras.getString("title"); |
| 498 | String url = extras.getString("url"); |
| 499 | if (title != null && url != null) { |
| 500 | mBookmarksAdapter.updateRow(extras); |
| 501 | } |
| 502 | } else { |
| 503 | // extras == null then a new bookmark was added to |
| 504 | // the database. |
| 505 | refreshList(); |
| 506 | } |
| 507 | } |
| 508 | break; |
| 509 | default: |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private void displayRemoveBookmarkDialog(int position) { |
| 515 | // Put up a dialog asking if the user really wants to |
| 516 | // delete the bookmark |
| 517 | final int deletePos = position; |
| 518 | new AlertDialog.Builder(this) |
| 519 | .setTitle(R.string.delete_bookmark) |
| 520 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 521 | .setMessage(getText(R.string.delete_bookmark_warning).toString().replace( |
| 522 | "%s", getBookmarkTitle(deletePos))) |
| 523 | .setPositiveButton(R.string.ok, |
| 524 | new DialogInterface.OnClickListener() { |
| 525 | public void onClick(DialogInterface dialog, int whichButton) { |
| 526 | deleteBookmark(deletePos); |
| 527 | } |
| 528 | }) |
| 529 | .setNegativeButton(R.string.cancel, null) |
| 530 | .show(); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Refresh the shown list after the database has changed. |
| 535 | */ |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 536 | private void refreshList() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 537 | mBookmarksAdapter.refreshList(); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Return a hashmap representing the currently highlighted row. |
| 542 | */ |
| 543 | public Bundle getRow(int position) { |
| 544 | return mBookmarksAdapter.getRow(position); |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Return the url of the currently highlighted row. |
| 549 | */ |
| 550 | public String getUrl(int position) { |
| 551 | return mBookmarksAdapter.getUrl(position); |
| 552 | } |
| 553 | |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 554 | /** |
| 555 | * Return the favicon of the currently highlighted row. |
| 556 | */ |
| 557 | public Bitmap getFavicon(int position) { |
| 558 | return mBookmarksAdapter.getFavicon(position); |
| 559 | } |
| 560 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 561 | private Bitmap getTouchIcon(int position) { |
| 562 | return mBookmarksAdapter.getTouchIcon(position); |
| 563 | } |
| 564 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 565 | private void copy(CharSequence text) { |
| 566 | try { |
| 567 | IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard")); |
| 568 | if (clip != null) { |
| 569 | clip.setClipboardText(text); |
| 570 | } |
| 571 | } catch (android.os.RemoteException e) { |
| 572 | Log.e(LOGTAG, "Copy failed", e); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | public String getBookmarkTitle(int position) { |
| 577 | return mBookmarksAdapter.getTitle(position); |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Delete the currently highlighted row. |
| 582 | */ |
| 583 | public void deleteBookmark(int position) { |
| 584 | mBookmarksAdapter.deleteRow(position); |
| 585 | } |
| 586 | |
| 587 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 588 | if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.isDown()) { |
| 589 | setResultToParent(RESULT_CANCELED, null); |
| 590 | mCanceled = true; |
| 591 | } |
| 592 | return super.dispatchKeyEvent(event); |
| 593 | } |
| 594 | |
| 595 | // This Activity is generally a sub-Activity of CombinedHistoryActivity. In |
| 596 | // that situation, we need to pass our result code up to our parent. |
| 597 | // However, if someone calls this Activity directly, then this has no |
| 598 | // parent, and it needs to set it on itself. |
| 599 | private void setResultToParent(int resultCode, Intent data) { |
| 600 | Activity a = getParent() == null ? this : getParent(); |
| 601 | a.setResult(resultCode, data); |
| 602 | } |
| 603 | } |