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