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