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; |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 23 | import android.content.SharedPreferences; |
| 24 | import android.content.SharedPreferences.Editor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | import android.graphics.Bitmap; |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 26 | import android.graphics.BitmapFactory; |
| 27 | import android.graphics.Canvas; |
| 28 | import android.graphics.Color; |
| 29 | import android.graphics.Paint; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 30 | import android.graphics.Path; |
| 31 | import android.graphics.PorterDuff; |
| 32 | import android.graphics.PorterDuffXfermode; |
Patrick Scott | c0fdde9 | 2010-02-25 14:40:11 -0500 | [diff] [blame] | 33 | import android.graphics.Rect; |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 34 | import android.graphics.RectF; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 35 | import android.net.Uri; |
Patrick Scott | f49ecd6 | 2010-04-09 12:43:26 -0400 | [diff] [blame] | 36 | import android.os.AsyncTask; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 37 | import android.os.Bundle; |
| 38 | import android.os.Handler; |
| 39 | import android.os.Message; |
| 40 | import android.os.ServiceManager; |
| 41 | import android.provider.Browser; |
| 42 | import android.text.IClipboard; |
| 43 | import android.util.Log; |
| 44 | import android.view.ContextMenu; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 45 | import android.view.Menu; |
| 46 | import android.view.MenuInflater; |
| 47 | import android.view.MenuItem; |
| 48 | import android.view.View; |
| 49 | import android.view.ViewGroup; |
| 50 | import android.view.ContextMenu.ContextMenuInfo; |
Patrick Scott | c1cf63a | 2010-03-09 16:02:08 -0500 | [diff] [blame] | 51 | import android.webkit.WebIconDatabase.IconListener; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 52 | import android.widget.AdapterView; |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 53 | import android.widget.GridView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | import android.widget.ListView; |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 55 | import android.widget.Toast; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 56 | |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 57 | /*package*/ enum BookmarkViewMode { NONE, GRID, LIST } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 58 | /** |
| 59 | * View showing the user's bookmarks in the browser. |
| 60 | */ |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 61 | public class BrowserBookmarksPage extends Activity implements |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 62 | View.OnCreateContextMenuListener { |
| 63 | |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 64 | private BookmarkViewMode mViewMode = BookmarkViewMode.NONE; |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 65 | private GridView mGridPage; |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 66 | private ListView mVerticalList; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 67 | private BrowserBookmarksAdapter mBookmarksAdapter; |
| 68 | private static final int BOOKMARKS_SAVE = 1; |
Leon Scroggins | 190095d | 2009-08-17 17:01:38 -0400 | [diff] [blame] | 69 | private boolean mDisableNewWindow; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 70 | private BookmarkItem mContextHeader; |
| 71 | private AddNewBookmark mAddHeader; |
| 72 | private boolean mCanceled = false; |
| 73 | private boolean mCreateShortcut; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 74 | private boolean mMostVisited; |
| 75 | private View mEmptyView; |
Patrick Scott | 152794b | 2010-03-30 13:57:29 -0400 | [diff] [blame] | 76 | private int mIconSize; |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 77 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 78 | private final static String LOGTAG = "browser"; |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 79 | private final static String PREF_BOOKMARK_VIEW_MODE = "pref_bookmark_view_mode"; |
| 80 | private final static String PREF_MOST_VISITED_VIEW_MODE = "pref_most_visited_view_mode"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | public boolean onContextItemSelected(MenuItem item) { |
| 84 | // It is possible that the view has been canceled when we get to |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 85 | // this point as back has a higher priority |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 86 | if (mCanceled) { |
| 87 | return true; |
| 88 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 89 | AdapterView.AdapterContextMenuInfo i = |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 95 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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: |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame^] | 107 | sendBroadcast(createShortcutIntent(i.position)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 108 | break; |
| 109 | case R.id.delete_context_menu_id: |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 110 | if (mMostVisited) { |
| 111 | Browser.deleteFromHistory(getContentResolver(), |
| 112 | getUrl(i.position)); |
| 113 | refreshList(); |
| 114 | } else { |
| 115 | displayRemoveBookmarkDialog(i.position); |
| 116 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 117 | break; |
| 118 | case R.id.new_window_context_menu_id: |
| 119 | openInNewWindow(i.position); |
| 120 | break; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 121 | case R.id.share_link_context_menu_id: |
Leon Scroggins | 96afcb1 | 2009-12-10 12:35:56 -0500 | [diff] [blame] | 122 | BrowserActivity.sharePage(BrowserBookmarksPage.this, |
| 123 | mBookmarksAdapter.getTitle(i.position), getUrl(i.position), |
| 124 | getFavicon(i.position), |
| 125 | mBookmarksAdapter.getScreenshot(i.position)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 126 | break; |
| 127 | case R.id.copy_url_context_menu_id: |
| 128 | copy(getUrl(i.position)); |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 129 | break; |
| 130 | case R.id.homepage_context_menu_id: |
| 131 | BrowserSettings.getInstance().setHomePage(this, |
| 132 | getUrl(i.position)); |
| 133 | Toast.makeText(this, R.string.homepage_set, |
| 134 | Toast.LENGTH_LONG).show(); |
| 135 | break; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 136 | // Only for the Most visited page |
| 137 | case R.id.save_to_bookmarks_menu_id: |
Leon Scroggins | c1f5759 | 2009-08-14 14:16:10 -0400 | [diff] [blame] | 138 | boolean isBookmark; |
| 139 | String name; |
| 140 | String url; |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 141 | if (mViewMode == BookmarkViewMode.GRID) { |
Leon Scroggins | c1f5759 | 2009-08-14 14:16:10 -0400 | [diff] [blame] | 142 | isBookmark = mBookmarksAdapter.getIsBookmark(i.position); |
| 143 | name = mBookmarksAdapter.getTitle(i.position); |
| 144 | url = mBookmarksAdapter.getUrl(i.position); |
| 145 | } else { |
| 146 | HistoryItem historyItem = ((HistoryItem) i.targetView); |
| 147 | isBookmark = historyItem.isBookmark(); |
| 148 | name = historyItem.getName(); |
| 149 | url = historyItem.getUrl(); |
| 150 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 151 | // If the site is bookmarked, the item becomes remove from |
| 152 | // bookmarks. |
Leon Scroggins | c1f5759 | 2009-08-14 14:16:10 -0400 | [diff] [blame] | 153 | if (isBookmark) { |
Andrei Popescu | c952619 | 2009-09-23 15:52:16 +0100 | [diff] [blame] | 154 | Bookmarks.removeFromBookmarks(this, getContentResolver(), url, name); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 155 | } else { |
Leon Scroggins | c1f5759 | 2009-08-14 14:16:10 -0400 | [diff] [blame] | 156 | Browser.saveBookmark(this, name, url); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 157 | } |
| 158 | break; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 159 | default: |
| 160 | return super.onContextItemSelected(item); |
| 161 | } |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void onCreateContextMenu(ContextMenu menu, View v, |
| 167 | ContextMenuInfo menuInfo) { |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 168 | AdapterView.AdapterContextMenuInfo i = |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 169 | (AdapterView.AdapterContextMenuInfo) menuInfo; |
| 170 | |
| 171 | MenuInflater inflater = getMenuInflater(); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 172 | if (mMostVisited) { |
| 173 | inflater.inflate(R.menu.historycontext, menu); |
| 174 | } else { |
| 175 | inflater.inflate(R.menu.bookmarkscontext, menu); |
| 176 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 177 | |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 178 | if (0 == i.position && !mMostVisited) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 179 | menu.setGroupVisible(R.id.CONTEXT_MENU, false); |
| 180 | if (mAddHeader == null) { |
| 181 | mAddHeader = new AddNewBookmark(BrowserBookmarksPage.this); |
| 182 | } else if (mAddHeader.getParent() != null) { |
| 183 | ((ViewGroup) mAddHeader.getParent()). |
| 184 | removeView(mAddHeader); |
| 185 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 186 | mAddHeader.setUrl(getIntent().getStringExtra("url")); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 187 | menu.setHeaderView(mAddHeader); |
| 188 | return; |
| 189 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 190 | if (mMostVisited) { |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 191 | if ((mViewMode == BookmarkViewMode.LIST |
| 192 | && ((HistoryItem) i.targetView).isBookmark()) |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 193 | || mBookmarksAdapter.getIsBookmark(i.position)) { |
| 194 | MenuItem item = menu.findItem( |
| 195 | R.id.save_to_bookmarks_menu_id); |
| 196 | item.setTitle(R.string.remove_from_bookmarks); |
| 197 | } |
| 198 | } else { |
| 199 | // The historycontext menu has no ADD_MENU group. |
| 200 | menu.setGroupVisible(R.id.ADD_MENU, false); |
| 201 | } |
Leon Scroggins | 190095d | 2009-08-17 17:01:38 -0400 | [diff] [blame] | 202 | if (mDisableNewWindow) { |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 203 | menu.findItem(R.id.new_window_context_menu_id).setVisible( |
| 204 | false); |
| 205 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 206 | if (mContextHeader == null) { |
| 207 | mContextHeader = new BookmarkItem(BrowserBookmarksPage.this); |
| 208 | } else if (mContextHeader.getParent() != null) { |
| 209 | ((ViewGroup) mContextHeader.getParent()). |
| 210 | removeView(mContextHeader); |
| 211 | } |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 212 | if (mViewMode == BookmarkViewMode.GRID) { |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 213 | mBookmarksAdapter.populateBookmarkItem(mContextHeader, |
| 214 | i.position); |
| 215 | } else { |
| 216 | BookmarkItem b = (BookmarkItem) i.targetView; |
| 217 | b.copyTo(mContextHeader); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 218 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 219 | menu.setHeaderView(mContextHeader); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Create a new BrowserBookmarksPage. |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 224 | */ |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 225 | @Override |
| 226 | protected void onCreate(Bundle icicle) { |
| 227 | super.onCreate(icicle); |
| 228 | |
Patrick Scott | 152794b | 2010-03-30 13:57:29 -0400 | [diff] [blame] | 229 | // Grab the app icon size as a resource. |
| 230 | mIconSize = getResources().getDimensionPixelSize( |
| 231 | android.R.dimen.app_icon_size); |
| 232 | |
Patrick Scott | f49ecd6 | 2010-04-09 12:43:26 -0400 | [diff] [blame] | 233 | Intent intent = getIntent(); |
| 234 | if (Intent.ACTION_CREATE_SHORTCUT.equals(intent.getAction())) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 235 | mCreateShortcut = true; |
| 236 | } |
Patrick Scott | f49ecd6 | 2010-04-09 12:43:26 -0400 | [diff] [blame] | 237 | mDisableNewWindow = intent.getBooleanExtra("disable_new_window", |
Leon Scroggins | 190095d | 2009-08-17 17:01:38 -0400 | [diff] [blame] | 238 | false); |
Patrick Scott | f49ecd6 | 2010-04-09 12:43:26 -0400 | [diff] [blame] | 239 | mMostVisited = intent.getBooleanExtra("mostVisited", false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 240 | |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 241 | if (mCreateShortcut) { |
| 242 | setTitle(R.string.browser_bookmarks_page_bookmarks_text); |
| 243 | } |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 244 | |
| 245 | setContentView(R.layout.empty_history); |
| 246 | mEmptyView = findViewById(R.id.empty_view); |
| 247 | mEmptyView.setVisibility(View.GONE); |
| 248 | |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 249 | SharedPreferences p = getPreferences(MODE_PRIVATE); |
| 250 | |
| 251 | // See if the user has set a preference for the view mode of their |
| 252 | // bookmarks. Otherwise default to grid mode. |
| 253 | BookmarkViewMode preference = BookmarkViewMode.NONE; |
| 254 | if (mMostVisited) { |
Leon Scroggins | b3968bb | 2009-10-16 09:04:16 -0400 | [diff] [blame] | 255 | // For the most visited page, only use list mode. |
| 256 | preference = BookmarkViewMode.LIST; |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 257 | } else { |
| 258 | preference = BookmarkViewMode.values()[p.getInt( |
| 259 | PREF_BOOKMARK_VIEW_MODE, BookmarkViewMode.GRID.ordinal())]; |
| 260 | } |
| 261 | switchViewMode(preference); |
Patrick Scott | f49ecd6 | 2010-04-09 12:43:26 -0400 | [diff] [blame] | 262 | |
| 263 | final boolean createShortcut = mCreateShortcut; |
| 264 | final boolean mostVisited = mMostVisited; |
| 265 | final String url = intent.getStringExtra("url"); |
| 266 | final String title = intent.getStringExtra("title"); |
| 267 | final Bitmap thumbnail = |
| 268 | (Bitmap) intent.getParcelableExtra("thumbnail"); |
| 269 | new AsyncTask<Void, Void, Void>() { |
| 270 | @Override |
| 271 | protected Void doInBackground(Void... unused) { |
| 272 | BrowserBookmarksAdapter adapter = new BrowserBookmarksAdapter( |
| 273 | BrowserBookmarksPage.this, |
| 274 | url, |
| 275 | title, |
| 276 | thumbnail, |
| 277 | createShortcut, |
| 278 | mostVisited); |
| 279 | mHandler.obtainMessage(ADAPTER_CREATED, adapter).sendToTarget(); |
| 280 | return null; |
| 281 | } |
| 282 | }.execute(); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 283 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 284 | |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 285 | @Override |
| 286 | protected void onDestroy() { |
| 287 | mHandler.removeCallbacksAndMessages(null); |
| 288 | super.onDestroy(); |
| 289 | } |
| 290 | |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 291 | /** |
| 292 | * Set the ContentView to be either the grid of thumbnails or the vertical |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 293 | * list. |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 294 | */ |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 295 | private void switchViewMode(BookmarkViewMode viewMode) { |
| 296 | if (mViewMode == viewMode) { |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 300 | mViewMode = viewMode; |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 301 | |
| 302 | // Update the preferences to make the new view mode sticky. |
| 303 | Editor ed = getPreferences(MODE_PRIVATE).edit(); |
| 304 | if (mMostVisited) { |
| 305 | ed.putInt(PREF_MOST_VISITED_VIEW_MODE, mViewMode.ordinal()); |
| 306 | } else { |
| 307 | ed.putInt(PREF_BOOKMARK_VIEW_MODE, mViewMode.ordinal()); |
| 308 | } |
| 309 | ed.commit(); |
| 310 | |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 311 | if (mBookmarksAdapter != null) { |
| 312 | mBookmarksAdapter.switchViewMode(viewMode); |
| 313 | } |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 314 | if (mViewMode == BookmarkViewMode.GRID) { |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 315 | if (mGridPage == null) { |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 316 | mGridPage = new GridView(this); |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 317 | if (mBookmarksAdapter != null) { |
| 318 | mGridPage.setAdapter(mBookmarksAdapter); |
| 319 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 320 | mGridPage.setOnItemClickListener(mListener); |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 321 | mGridPage.setNumColumns(GridView.AUTO_FIT); |
Leon Scroggins | f855161 | 2009-09-24 16:06:02 -0400 | [diff] [blame] | 322 | mGridPage.setColumnWidth( |
| 323 | BrowserActivity.getDesiredThumbnailWidth(this)); |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 324 | mGridPage.setFocusable(true); |
| 325 | mGridPage.setFocusableInTouchMode(true); |
| 326 | mGridPage.setSelector(android.R.drawable.gallery_thumb); |
Leon Scroggins | f855161 | 2009-09-24 16:06:02 -0400 | [diff] [blame] | 327 | float density = getResources().getDisplayMetrics().density; |
| 328 | mGridPage.setVerticalSpacing((int) (14 * density)); |
| 329 | mGridPage.setHorizontalSpacing((int) (8 * density)); |
| 330 | mGridPage.setStretchMode(GridView.STRETCH_SPACING); |
Leon Scroggins | bbe6d5b | 2009-09-28 12:01:00 -0400 | [diff] [blame] | 331 | mGridPage.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET); |
Leon Scroggins | f855161 | 2009-09-24 16:06:02 -0400 | [diff] [blame] | 332 | mGridPage.setDrawSelectorOnTop(true); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 333 | if (mMostVisited) { |
| 334 | mGridPage.setEmptyView(mEmptyView); |
| 335 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 336 | if (!mCreateShortcut) { |
| 337 | mGridPage.setOnCreateContextMenuListener(this); |
| 338 | } |
| 339 | } |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 340 | addContentView(mGridPage, FULL_SCREEN_PARAMS); |
| 341 | if (mVerticalList != null) { |
| 342 | ViewGroup parent = (ViewGroup) mVerticalList.getParent(); |
| 343 | if (parent != null) { |
| 344 | parent.removeView(mVerticalList); |
| 345 | } |
| 346 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 347 | } else { |
| 348 | if (null == mVerticalList) { |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 349 | ListView listView = new ListView(this); |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 350 | if (mBookmarksAdapter != null) { |
| 351 | listView.setAdapter(mBookmarksAdapter); |
| 352 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 353 | listView.setDrawSelectorOnTop(false); |
| 354 | listView.setVerticalScrollBarEnabled(true); |
| 355 | listView.setOnItemClickListener(mListener); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 356 | if (mMostVisited) { |
| 357 | listView.setEmptyView(mEmptyView); |
| 358 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 359 | if (!mCreateShortcut) { |
| 360 | listView.setOnCreateContextMenuListener(this); |
| 361 | } |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 362 | mVerticalList = listView; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 363 | } |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 364 | addContentView(mVerticalList, FULL_SCREEN_PARAMS); |
| 365 | if (mGridPage != null) { |
| 366 | ViewGroup parent = (ViewGroup) mGridPage.getParent(); |
| 367 | if (parent != null) { |
| 368 | parent.removeView(mGridPage); |
| 369 | } |
| 370 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 371 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 374 | private static final ViewGroup.LayoutParams FULL_SCREEN_PARAMS |
| 375 | = new ViewGroup.LayoutParams( |
Romain Guy | 15b8ec6 | 2010-01-08 15:06:43 -0800 | [diff] [blame] | 376 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 377 | ViewGroup.LayoutParams.MATCH_PARENT); |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 378 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 379 | private static final int SAVE_CURRENT_PAGE = 1000; |
Patrick Scott | f49ecd6 | 2010-04-09 12:43:26 -0400 | [diff] [blame] | 380 | private static final int ADAPTER_CREATED = 1001; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 381 | private final Handler mHandler = new Handler() { |
| 382 | @Override |
| 383 | public void handleMessage(Message msg) { |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 384 | switch (msg.what) { |
| 385 | case SAVE_CURRENT_PAGE: |
| 386 | saveCurrentPage(); |
| 387 | break; |
Patrick Scott | f49ecd6 | 2010-04-09 12:43:26 -0400 | [diff] [blame] | 388 | case ADAPTER_CREATED: |
| 389 | mBookmarksAdapter = (BrowserBookmarksAdapter) msg.obj; |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 390 | mBookmarksAdapter.switchViewMode(mViewMode); |
| 391 | if (mGridPage != null) { |
| 392 | mGridPage.setAdapter(mBookmarksAdapter); |
| 393 | } |
| 394 | if (mVerticalList != null) { |
| 395 | mVerticalList.setAdapter(mBookmarksAdapter); |
| 396 | } |
Patrick Scott | c1cf63a | 2010-03-09 16:02:08 -0500 | [diff] [blame] | 397 | // Add our own listener in case there are favicons that |
| 398 | // have yet to be loaded. |
| 399 | if (mMostVisited) { |
| 400 | IconListener listener = new IconListener() { |
| 401 | public void onReceivedIcon(String url, |
| 402 | Bitmap icon) { |
| 403 | if (mGridPage != null) { |
| 404 | mGridPage.setAdapter(mBookmarksAdapter); |
| 405 | } |
| 406 | if (mVerticalList != null) { |
| 407 | mVerticalList.setAdapter(mBookmarksAdapter); |
| 408 | } |
| 409 | } |
| 410 | }; |
| 411 | CombinedBookmarkHistoryActivity.getIconListenerSet() |
| 412 | .addListener(listener); |
| 413 | } |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 414 | break; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | }; |
| 418 | |
| 419 | private AdapterView.OnItemClickListener mListener = new AdapterView.OnItemClickListener() { |
| 420 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
| 421 | // It is possible that the view has been canceled when we get to |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 422 | // this point as back has a higher priority |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 423 | if (mCanceled) { |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 424 | android.util.Log.e(LOGTAG, "item clicked when dismissing"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | if (!mCreateShortcut) { |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 428 | if (0 == position && !mMostVisited) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 429 | // XXX: Work-around for a framework issue. |
| 430 | mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE); |
| 431 | } else { |
| 432 | loadUrl(position); |
| 433 | } |
| 434 | } else { |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame^] | 435 | setResultToParent(RESULT_OK, createShortcutIntent(position)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 436 | finish(); |
| 437 | } |
| 438 | } |
| 439 | }; |
| 440 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 441 | private Intent createShortcutIntent(int position) { |
| 442 | String url = getUrl(position); |
| 443 | String title = getBookmarkTitle(position); |
| 444 | Bitmap touchIcon = getTouchIcon(position); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame^] | 445 | Bitmap favicon = getFavicon(position); |
| 446 | return BookmarkUtils.createAddToHomeIntent(this, url, title, touchIcon, favicon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | private void saveCurrentPage() { |
| 450 | Intent i = new Intent(BrowserBookmarksPage.this, |
| 451 | AddBookmarkPage.class); |
| 452 | i.putExtras(getIntent()); |
| 453 | startActivityForResult(i, BOOKMARKS_SAVE); |
| 454 | } |
| 455 | |
| 456 | private void loadUrl(int position) { |
| 457 | Intent intent = (new Intent()).setAction(getUrl(position)); |
| 458 | setResultToParent(RESULT_OK, intent); |
| 459 | finish(); |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | public boolean onCreateOptionsMenu(Menu menu) { |
| 464 | boolean result = super.onCreateOptionsMenu(menu); |
Leon Scroggins | b3968bb | 2009-10-16 09:04:16 -0400 | [diff] [blame] | 465 | if (!mCreateShortcut && !mMostVisited) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 466 | MenuInflater inflater = getMenuInflater(); |
| 467 | inflater.inflate(R.menu.bookmarks, menu); |
| 468 | return true; |
| 469 | } |
| 470 | return result; |
| 471 | } |
| 472 | |
| 473 | @Override |
Leon Scroggins | 0c78650 | 2009-08-04 16:04:55 -0400 | [diff] [blame] | 474 | public boolean onPrepareOptionsMenu(Menu menu) { |
Leon Scroggins | 8382d99 | 2009-08-19 11:25:14 -0400 | [diff] [blame] | 475 | boolean result = super.onPrepareOptionsMenu(menu); |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 476 | if (mCreateShortcut || mMostVisited || mBookmarksAdapter == null |
Leon Scroggins | b3968bb | 2009-10-16 09:04:16 -0400 | [diff] [blame] | 477 | || mBookmarksAdapter.getCount() == 0) { |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 478 | // No need to show the menu if there are no items. |
Leon Scroggins | 8382d99 | 2009-08-19 11:25:14 -0400 | [diff] [blame] | 479 | return result; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 480 | } |
Leon Scroggins | fdd10d7 | 2009-09-25 13:01:45 -0400 | [diff] [blame] | 481 | MenuItem switchItem = menu.findItem(R.id.switch_mode_menu_id); |
| 482 | int titleResId; |
| 483 | int iconResId; |
| 484 | if (mViewMode == BookmarkViewMode.GRID) { |
| 485 | titleResId = R.string.switch_to_list; |
| 486 | iconResId = R.drawable.ic_menu_list; |
| 487 | } else { |
| 488 | titleResId = R.string.switch_to_thumbnails; |
| 489 | iconResId = R.drawable.ic_menu_thumbnail; |
| 490 | } |
| 491 | switchItem.setTitle(titleResId); |
| 492 | switchItem.setIcon(iconResId); |
Leon Scroggins | 0c78650 | 2009-08-04 16:04:55 -0400 | [diff] [blame] | 493 | return true; |
| 494 | } |
| 495 | |
| 496 | @Override |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 497 | public boolean onOptionsItemSelected(MenuItem item) { |
| 498 | switch (item.getItemId()) { |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 499 | case R.id.new_context_menu_id: |
| 500 | saveCurrentPage(); |
| 501 | break; |
| 502 | |
| 503 | case R.id.switch_mode_menu_id: |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 504 | if (mViewMode == BookmarkViewMode.GRID) { |
| 505 | switchViewMode(BookmarkViewMode.LIST); |
| 506 | } else { |
| 507 | switchViewMode(BookmarkViewMode.GRID); |
| 508 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 509 | break; |
| 510 | |
| 511 | default: |
| 512 | return super.onOptionsItemSelected(item); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 513 | } |
| 514 | return true; |
| 515 | } |
| 516 | |
| 517 | private void openInNewWindow(int position) { |
| 518 | Bundle b = new Bundle(); |
| 519 | b.putBoolean("new_window", true); |
| 520 | setResultToParent(RESULT_OK, |
| 521 | (new Intent()).setAction(getUrl(position)).putExtras(b)); |
| 522 | |
| 523 | finish(); |
| 524 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 525 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 526 | |
| 527 | private void editBookmark(int position) { |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 528 | Intent intent = new Intent(BrowserBookmarksPage.this, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 529 | AddBookmarkPage.class); |
| 530 | intent.putExtra("bookmark", getRow(position)); |
| 531 | startActivityForResult(intent, BOOKMARKS_SAVE); |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | protected void onActivityResult(int requestCode, int resultCode, |
| 536 | Intent data) { |
| 537 | switch(requestCode) { |
| 538 | case BOOKMARKS_SAVE: |
| 539 | if (resultCode == RESULT_OK) { |
| 540 | Bundle extras; |
| 541 | if (data != null && (extras = data.getExtras()) != null) { |
| 542 | // If there are extras, then we need to save |
| 543 | // the edited bookmark. This is done in updateRow() |
| 544 | String title = extras.getString("title"); |
| 545 | String url = extras.getString("url"); |
| 546 | if (title != null && url != null) { |
| 547 | mBookmarksAdapter.updateRow(extras); |
| 548 | } |
| 549 | } else { |
| 550 | // extras == null then a new bookmark was added to |
| 551 | // the database. |
| 552 | refreshList(); |
| 553 | } |
| 554 | } |
| 555 | break; |
| 556 | default: |
| 557 | break; |
| 558 | } |
| 559 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 560 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 561 | private void displayRemoveBookmarkDialog(int position) { |
| 562 | // Put up a dialog asking if the user really wants to |
| 563 | // delete the bookmark |
| 564 | final int deletePos = position; |
| 565 | new AlertDialog.Builder(this) |
| 566 | .setTitle(R.string.delete_bookmark) |
| 567 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 568 | .setMessage(getText(R.string.delete_bookmark_warning).toString().replace( |
| 569 | "%s", getBookmarkTitle(deletePos))) |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 570 | .setPositiveButton(R.string.ok, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 571 | new DialogInterface.OnClickListener() { |
| 572 | public void onClick(DialogInterface dialog, int whichButton) { |
| 573 | deleteBookmark(deletePos); |
| 574 | } |
| 575 | }) |
| 576 | .setNegativeButton(R.string.cancel, null) |
| 577 | .show(); |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Refresh the shown list after the database has changed. |
| 582 | */ |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 583 | private void refreshList() { |
Grace Kloba | 0b82a6f | 2010-04-20 13:46:42 -0700 | [diff] [blame] | 584 | if (mBookmarksAdapter == null) return; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 585 | mBookmarksAdapter.refreshList(); |
| 586 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 587 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 588 | /** |
| 589 | * Return a hashmap representing the currently highlighted row. |
| 590 | */ |
| 591 | public Bundle getRow(int position) { |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 592 | return mBookmarksAdapter == null ? null |
| 593 | : mBookmarksAdapter.getRow(position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Return the url of the currently highlighted row. |
| 598 | */ |
| 599 | public String getUrl(int position) { |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 600 | return mBookmarksAdapter == null ? null |
| 601 | : mBookmarksAdapter.getUrl(position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 604 | /** |
| 605 | * Return the favicon of the currently highlighted row. |
| 606 | */ |
| 607 | public Bitmap getFavicon(int position) { |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 608 | return mBookmarksAdapter == null ? null |
| 609 | : mBookmarksAdapter.getFavicon(position); |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 612 | private Bitmap getTouchIcon(int position) { |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 613 | return mBookmarksAdapter == null ? null |
| 614 | : mBookmarksAdapter.getTouchIcon(position); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 615 | } |
| 616 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 617 | private void copy(CharSequence text) { |
| 618 | try { |
| 619 | IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard")); |
| 620 | if (clip != null) { |
| 621 | clip.setClipboardText(text); |
| 622 | } |
| 623 | } catch (android.os.RemoteException e) { |
| 624 | Log.e(LOGTAG, "Copy failed", e); |
| 625 | } |
| 626 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 627 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 628 | public String getBookmarkTitle(int position) { |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 629 | return mBookmarksAdapter == null ? null |
| 630 | : mBookmarksAdapter.getTitle(position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Delete the currently highlighted row. |
| 635 | */ |
| 636 | public void deleteBookmark(int position) { |
Leon Scroggins | ea00257 | 2009-11-24 15:21:18 -0500 | [diff] [blame] | 637 | if (mBookmarksAdapter == null) return; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 638 | mBookmarksAdapter.deleteRow(position); |
| 639 | } |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 640 | |
| 641 | @Override |
| 642 | public void onBackPressed() { |
| 643 | setResultToParent(RESULT_CANCELED, null); |
| 644 | mCanceled = true; |
| 645 | super.onBackPressed(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Leon Scroggins | fde9746 | 2010-01-11 13:06:21 -0500 | [diff] [blame] | 648 | // This Activity is generally a sub-Activity of |
| 649 | // CombinedBookmarkHistoryActivity. In that situation, we need to pass our |
| 650 | // result code up to our parent. However, if someone calls this Activity |
| 651 | // directly, then this has no parent, and it needs to set it on itself. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 652 | private void setResultToParent(int resultCode, Intent data) { |
Leon Scroggins | fde9746 | 2010-01-11 13:06:21 -0500 | [diff] [blame] | 653 | Activity parent = getParent(); |
| 654 | if (parent == null) { |
| 655 | setResult(resultCode, data); |
| 656 | } else { |
| 657 | ((CombinedBookmarkHistoryActivity) parent).setResultFromChild( |
| 658 | resultCode, data); |
| 659 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 660 | } |
| 661 | } |