blob: 6af14e839ca70b6de79e4545b02ca39b00e1fe9d [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.content.DialogInterface;
22import android.content.Intent;
Ben Murdoch328ea872009-09-16 13:33:29 +010023import android.content.SharedPreferences;
24import android.content.SharedPreferences.Editor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.graphics.Bitmap;
Patrick Scottf49ecd62010-04-09 12:43:26 -040026import android.os.AsyncTask;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import android.os.Bundle;
28import android.os.Handler;
29import android.os.Message;
30import android.os.ServiceManager;
31import android.provider.Browser;
32import android.text.IClipboard;
33import android.util.Log;
34import android.view.ContextMenu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.view.Menu;
36import android.view.MenuInflater;
37import android.view.MenuItem;
38import android.view.View;
39import android.view.ViewGroup;
40import android.view.ContextMenu.ContextMenuInfo;
Patrick Scottc1cf63a2010-03-09 16:02:08 -050041import android.webkit.WebIconDatabase.IconListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import android.widget.AdapterView;
Leon Scroggins89c6d362009-07-15 16:54:37 -040043import android.widget.GridView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.widget.ListView;
Leon Scrogginsfeb941d2009-05-28 17:27:38 -040045import android.widget.Toast;
Michael Kolbfe251992010-07-08 15:41:55 -070046import android.widget.AdapterView.OnItemClickListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047
Ben Murdoch328ea872009-09-16 13:33:29 +010048/*package*/ enum BookmarkViewMode { NONE, GRID, LIST }
The Android Open Source Project0c908882009-03-03 19:32:16 -080049/**
50 * View showing the user's bookmarks in the browser.
51 */
Nicolas Catania095292f2010-03-15 09:00:14 -070052public class BrowserBookmarksPage extends Activity implements
The Android Open Source Project0c908882009-03-03 19:32:16 -080053 View.OnCreateContextMenuListener {
54
Ben Murdoch328ea872009-09-16 13:33:29 +010055 private BookmarkViewMode mViewMode = BookmarkViewMode.NONE;
Leon Scroggins89c6d362009-07-15 16:54:37 -040056 private GridView mGridPage;
Leon Scrogginsea002572009-11-24 15:21:18 -050057 private ListView mVerticalList;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058 private BrowserBookmarksAdapter mBookmarksAdapter;
59 private static final int BOOKMARKS_SAVE = 1;
Leon Scroggins190095d2009-08-17 17:01:38 -040060 private boolean mDisableNewWindow;
The Android Open Source Project0c908882009-03-03 19:32:16 -080061 private BookmarkItem mContextHeader;
62 private AddNewBookmark mAddHeader;
63 private boolean mCanceled = false;
64 private boolean mCreateShortcut;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040065 private boolean mMostVisited;
66 private View mEmptyView;
Patrick Scott152794b2010-03-30 13:57:29 -040067 private int mIconSize;
Nicolas Catania095292f2010-03-15 09:00:14 -070068
The Android Open Source Project0c908882009-03-03 19:32:16 -080069 private final static String LOGTAG = "browser";
Ben Murdoch328ea872009-09-16 13:33:29 +010070 private final static String PREF_BOOKMARK_VIEW_MODE = "pref_bookmark_view_mode";
71 private final static String PREF_MOST_VISITED_VIEW_MODE = "pref_most_visited_view_mode";
The Android Open Source Project0c908882009-03-03 19:32:16 -080072
73 @Override
74 public boolean onContextItemSelected(MenuItem item) {
75 // It is possible that the view has been canceled when we get to
Nicolas Catania095292f2010-03-15 09:00:14 -070076 // this point as back has a higher priority
The Android Open Source Project0c908882009-03-03 19:32:16 -080077 if (mCanceled) {
78 return true;
79 }
Nicolas Catania095292f2010-03-15 09:00:14 -070080 AdapterView.AdapterContextMenuInfo i =
The Android Open Source Project0c908882009-03-03 19:32:16 -080081 (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
82 // If we have no menu info, we can't tell which item was selected.
83 if (i == null) {
84 return true;
85 }
Nicolas Catania095292f2010-03-15 09:00:14 -070086
The Android Open Source Project0c908882009-03-03 19:32:16 -080087 switch (item.getItemId()) {
88 case R.id.new_context_menu_id:
89 saveCurrentPage();
90 break;
91 case R.id.open_context_menu_id:
92 loadUrl(i.position);
93 break;
94 case R.id.edit_context_menu_id:
95 editBookmark(i.position);
96 break;
97 case R.id.shortcut_context_menu_id:
Ben Murdoch90b40262010-06-30 13:33:28 +010098 sendBroadcast(createShortcutIntent(i.position));
The Android Open Source Project0c908882009-03-03 19:32:16 -080099 break;
100 case R.id.delete_context_menu_id:
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400101 if (mMostVisited) {
102 Browser.deleteFromHistory(getContentResolver(),
103 getUrl(i.position));
104 refreshList();
105 } else {
106 displayRemoveBookmarkDialog(i.position);
107 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108 break;
109 case R.id.new_window_context_menu_id:
110 openInNewWindow(i.position);
111 break;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400112 case R.id.share_link_context_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -0500113 BrowserActivity.sharePage(BrowserBookmarksPage.this,
114 mBookmarksAdapter.getTitle(i.position), getUrl(i.position),
115 getFavicon(i.position),
116 mBookmarksAdapter.getScreenshot(i.position));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117 break;
118 case R.id.copy_url_context_menu_id:
119 copy(getUrl(i.position));
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400120 break;
121 case R.id.homepage_context_menu_id:
122 BrowserSettings.getInstance().setHomePage(this,
123 getUrl(i.position));
124 Toast.makeText(this, R.string.homepage_set,
125 Toast.LENGTH_LONG).show();
126 break;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400127 // Only for the Most visited page
128 case R.id.save_to_bookmarks_menu_id:
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400129 boolean isBookmark;
130 String name;
131 String url;
Ben Murdoch328ea872009-09-16 13:33:29 +0100132 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400133 isBookmark = mBookmarksAdapter.getIsBookmark(i.position);
134 name = mBookmarksAdapter.getTitle(i.position);
135 url = mBookmarksAdapter.getUrl(i.position);
136 } else {
137 HistoryItem historyItem = ((HistoryItem) i.targetView);
138 isBookmark = historyItem.isBookmark();
139 name = historyItem.getName();
140 url = historyItem.getUrl();
141 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400142 // If the site is bookmarked, the item becomes remove from
143 // bookmarks.
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400144 if (isBookmark) {
Andrei Popescuc9526192009-09-23 15:52:16 +0100145 Bookmarks.removeFromBookmarks(this, getContentResolver(), url, name);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400146 } else {
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400147 Browser.saveBookmark(this, name, url);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400148 }
149 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150 default:
151 return super.onContextItemSelected(item);
152 }
153 return true;
154 }
155
156 @Override
157 public void onCreateContextMenu(ContextMenu menu, View v,
158 ContextMenuInfo menuInfo) {
Nicolas Catania095292f2010-03-15 09:00:14 -0700159 AdapterView.AdapterContextMenuInfo i =
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160 (AdapterView.AdapterContextMenuInfo) menuInfo;
161
162 MenuInflater inflater = getMenuInflater();
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400163 if (mMostVisited) {
164 inflater.inflate(R.menu.historycontext, menu);
165 } else {
166 inflater.inflate(R.menu.bookmarkscontext, menu);
167 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800168
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400169 if (0 == i.position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 menu.setGroupVisible(R.id.CONTEXT_MENU, false);
171 if (mAddHeader == null) {
172 mAddHeader = new AddNewBookmark(BrowserBookmarksPage.this);
173 } else if (mAddHeader.getParent() != null) {
174 ((ViewGroup) mAddHeader.getParent()).
175 removeView(mAddHeader);
176 }
Leon Scroggins892df312009-07-14 14:48:02 -0400177 mAddHeader.setUrl(getIntent().getStringExtra("url"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178 menu.setHeaderView(mAddHeader);
179 return;
180 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400181 if (mMostVisited) {
Ben Murdoch328ea872009-09-16 13:33:29 +0100182 if ((mViewMode == BookmarkViewMode.LIST
183 && ((HistoryItem) i.targetView).isBookmark())
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400184 || mBookmarksAdapter.getIsBookmark(i.position)) {
185 MenuItem item = menu.findItem(
186 R.id.save_to_bookmarks_menu_id);
187 item.setTitle(R.string.remove_from_bookmarks);
188 }
189 } else {
190 // The historycontext menu has no ADD_MENU group.
191 menu.setGroupVisible(R.id.ADD_MENU, false);
192 }
Leon Scroggins190095d2009-08-17 17:01:38 -0400193 if (mDisableNewWindow) {
Leon Scroggins892df312009-07-14 14:48:02 -0400194 menu.findItem(R.id.new_window_context_menu_id).setVisible(
195 false);
196 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197 if (mContextHeader == null) {
198 mContextHeader = new BookmarkItem(BrowserBookmarksPage.this);
199 } else if (mContextHeader.getParent() != null) {
200 ((ViewGroup) mContextHeader.getParent()).
201 removeView(mContextHeader);
202 }
Ben Murdoch328ea872009-09-16 13:33:29 +0100203 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400204 mBookmarksAdapter.populateBookmarkItem(mContextHeader,
205 i.position);
206 } else {
207 BookmarkItem b = (BookmarkItem) i.targetView;
208 b.copyTo(mContextHeader);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 }
Leon Scroggins892df312009-07-14 14:48:02 -0400210 menu.setHeaderView(mContextHeader);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800211 }
212
213 /**
214 * Create a new BrowserBookmarksPage.
Nicolas Catania095292f2010-03-15 09:00:14 -0700215 */
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 @Override
217 protected void onCreate(Bundle icicle) {
218 super.onCreate(icicle);
219
Patrick Scott152794b2010-03-30 13:57:29 -0400220 // Grab the app icon size as a resource.
221 mIconSize = getResources().getDimensionPixelSize(
222 android.R.dimen.app_icon_size);
223
Patrick Scottf49ecd62010-04-09 12:43:26 -0400224 Intent intent = getIntent();
225 if (Intent.ACTION_CREATE_SHORTCUT.equals(intent.getAction())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800226 mCreateShortcut = true;
227 }
Patrick Scottf49ecd62010-04-09 12:43:26 -0400228 mDisableNewWindow = intent.getBooleanExtra("disable_new_window",
Leon Scroggins190095d2009-08-17 17:01:38 -0400229 false);
Patrick Scottf49ecd62010-04-09 12:43:26 -0400230 mMostVisited = intent.getBooleanExtra("mostVisited", false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800231
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400232 if (mCreateShortcut) {
233 setTitle(R.string.browser_bookmarks_page_bookmarks_text);
234 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400235
236 setContentView(R.layout.empty_history);
237 mEmptyView = findViewById(R.id.empty_view);
238 mEmptyView.setVisibility(View.GONE);
239
Ben Murdoch328ea872009-09-16 13:33:29 +0100240 SharedPreferences p = getPreferences(MODE_PRIVATE);
241
242 // See if the user has set a preference for the view mode of their
243 // bookmarks. Otherwise default to grid mode.
244 BookmarkViewMode preference = BookmarkViewMode.NONE;
245 if (mMostVisited) {
Leon Scrogginsb3968bb2009-10-16 09:04:16 -0400246 // For the most visited page, only use list mode.
247 preference = BookmarkViewMode.LIST;
Ben Murdoch328ea872009-09-16 13:33:29 +0100248 } else {
249 preference = BookmarkViewMode.values()[p.getInt(
250 PREF_BOOKMARK_VIEW_MODE, BookmarkViewMode.GRID.ordinal())];
251 }
252 switchViewMode(preference);
Patrick Scottf49ecd62010-04-09 12:43:26 -0400253
254 final boolean createShortcut = mCreateShortcut;
255 final boolean mostVisited = mMostVisited;
256 final String url = intent.getStringExtra("url");
257 final String title = intent.getStringExtra("title");
258 final Bitmap thumbnail =
259 (Bitmap) intent.getParcelableExtra("thumbnail");
260 new AsyncTask<Void, Void, Void>() {
261 @Override
262 protected Void doInBackground(Void... unused) {
Michael Kolbfe251992010-07-08 15:41:55 -0700263 BrowserBookmarksAdapter adapter =
264 new BrowserBookmarksAdapter(
265 BrowserBookmarksPage.this,
266 url,
267 title,
268 thumbnail,
269 createShortcut,
270 mostVisited);
Patrick Scottf49ecd62010-04-09 12:43:26 -0400271 mHandler.obtainMessage(ADAPTER_CREATED, adapter).sendToTarget();
272 return null;
273 }
274 }.execute();
Leon Scroggins892df312009-07-14 14:48:02 -0400275 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276
Nicolas Catania095292f2010-03-15 09:00:14 -0700277 @Override
278 protected void onDestroy() {
279 mHandler.removeCallbacksAndMessages(null);
280 super.onDestroy();
281 }
282
Leon Scroggins892df312009-07-14 14:48:02 -0400283 /**
284 * Set the ContentView to be either the grid of thumbnails or the vertical
Ben Murdoch328ea872009-09-16 13:33:29 +0100285 * list.
Leon Scroggins892df312009-07-14 14:48:02 -0400286 */
Leon Scrogginsea002572009-11-24 15:21:18 -0500287 private void switchViewMode(BookmarkViewMode viewMode) {
288 if (mViewMode == viewMode) {
Ben Murdoch328ea872009-09-16 13:33:29 +0100289 return;
290 }
291
Leon Scrogginsea002572009-11-24 15:21:18 -0500292 mViewMode = viewMode;
Ben Murdoch328ea872009-09-16 13:33:29 +0100293
294 // Update the preferences to make the new view mode sticky.
295 Editor ed = getPreferences(MODE_PRIVATE).edit();
296 if (mMostVisited) {
297 ed.putInt(PREF_MOST_VISITED_VIEW_MODE, mViewMode.ordinal());
298 } else {
299 ed.putInt(PREF_BOOKMARK_VIEW_MODE, mViewMode.ordinal());
300 }
301 ed.commit();
302
Leon Scrogginsea002572009-11-24 15:21:18 -0500303 if (mBookmarksAdapter != null) {
304 mBookmarksAdapter.switchViewMode(viewMode);
305 }
Ben Murdoch328ea872009-09-16 13:33:29 +0100306 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400307 if (mGridPage == null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -0400308 mGridPage = new GridView(this);
Leon Scrogginsea002572009-11-24 15:21:18 -0500309 if (mBookmarksAdapter != null) {
310 mGridPage.setAdapter(mBookmarksAdapter);
311 }
Leon Scroggins892df312009-07-14 14:48:02 -0400312 mGridPage.setOnItemClickListener(mListener);
Leon Scroggins89c6d362009-07-15 16:54:37 -0400313 mGridPage.setNumColumns(GridView.AUTO_FIT);
Leon Scrogginsf8551612009-09-24 16:06:02 -0400314 mGridPage.setColumnWidth(
315 BrowserActivity.getDesiredThumbnailWidth(this));
Leon Scroggins89c6d362009-07-15 16:54:37 -0400316 mGridPage.setFocusable(true);
317 mGridPage.setFocusableInTouchMode(true);
318 mGridPage.setSelector(android.R.drawable.gallery_thumb);
Leon Scrogginsf8551612009-09-24 16:06:02 -0400319 float density = getResources().getDisplayMetrics().density;
320 mGridPage.setVerticalSpacing((int) (14 * density));
321 mGridPage.setHorizontalSpacing((int) (8 * density));
322 mGridPage.setStretchMode(GridView.STRETCH_SPACING);
Leon Scrogginsbbe6d5b2009-09-28 12:01:00 -0400323 mGridPage.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
Leon Scrogginsf8551612009-09-24 16:06:02 -0400324 mGridPage.setDrawSelectorOnTop(true);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400325 if (mMostVisited) {
326 mGridPage.setEmptyView(mEmptyView);
327 }
Leon Scroggins892df312009-07-14 14:48:02 -0400328 if (!mCreateShortcut) {
329 mGridPage.setOnCreateContextMenuListener(this);
330 }
331 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400332 addContentView(mGridPage, FULL_SCREEN_PARAMS);
333 if (mVerticalList != null) {
334 ViewGroup parent = (ViewGroup) mVerticalList.getParent();
335 if (parent != null) {
336 parent.removeView(mVerticalList);
337 }
338 }
Leon Scroggins892df312009-07-14 14:48:02 -0400339 } else {
340 if (null == mVerticalList) {
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400341 ListView listView = new ListView(this);
Leon Scrogginsea002572009-11-24 15:21:18 -0500342 if (mBookmarksAdapter != null) {
343 listView.setAdapter(mBookmarksAdapter);
344 }
Leon Scroggins892df312009-07-14 14:48:02 -0400345 listView.setDrawSelectorOnTop(false);
346 listView.setVerticalScrollBarEnabled(true);
Michael Kolbfe251992010-07-08 15:41:55 -0700347 listView.setOnItemClickListener(mListListener);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400348 if (mMostVisited) {
349 listView.setEmptyView(mEmptyView);
350 }
Leon Scroggins892df312009-07-14 14:48:02 -0400351 if (!mCreateShortcut) {
352 listView.setOnCreateContextMenuListener(this);
353 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400354 mVerticalList = listView;
Leon Scroggins892df312009-07-14 14:48:02 -0400355 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400356 addContentView(mVerticalList, FULL_SCREEN_PARAMS);
357 if (mGridPage != null) {
358 ViewGroup parent = (ViewGroup) mGridPage.getParent();
359 if (parent != null) {
360 parent.removeView(mGridPage);
361 }
362 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400363 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800364 }
365
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400366 private static final ViewGroup.LayoutParams FULL_SCREEN_PARAMS
367 = new ViewGroup.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -0800368 ViewGroup.LayoutParams.MATCH_PARENT,
369 ViewGroup.LayoutParams.MATCH_PARENT);
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400370
The Android Open Source Project0c908882009-03-03 19:32:16 -0800371 private static final int SAVE_CURRENT_PAGE = 1000;
Patrick Scottf49ecd62010-04-09 12:43:26 -0400372 private static final int ADAPTER_CREATED = 1001;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800373 private final Handler mHandler = new Handler() {
374 @Override
375 public void handleMessage(Message msg) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500376 switch (msg.what) {
377 case SAVE_CURRENT_PAGE:
378 saveCurrentPage();
379 break;
Patrick Scottf49ecd62010-04-09 12:43:26 -0400380 case ADAPTER_CREATED:
381 mBookmarksAdapter = (BrowserBookmarksAdapter) msg.obj;
Leon Scrogginsea002572009-11-24 15:21:18 -0500382 mBookmarksAdapter.switchViewMode(mViewMode);
383 if (mGridPage != null) {
384 mGridPage.setAdapter(mBookmarksAdapter);
385 }
386 if (mVerticalList != null) {
387 mVerticalList.setAdapter(mBookmarksAdapter);
388 }
Patrick Scottc1cf63a2010-03-09 16:02:08 -0500389 // Add our own listener in case there are favicons that
390 // have yet to be loaded.
391 if (mMostVisited) {
392 IconListener listener = new IconListener() {
393 public void onReceivedIcon(String url,
394 Bitmap icon) {
395 if (mGridPage != null) {
396 mGridPage.setAdapter(mBookmarksAdapter);
397 }
398 if (mVerticalList != null) {
399 mVerticalList.setAdapter(mBookmarksAdapter);
400 }
401 }
402 };
403 CombinedBookmarkHistoryActivity.getIconListenerSet()
404 .addListener(listener);
405 }
Leon Scrogginsea002572009-11-24 15:21:18 -0500406 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800407 }
408 }
409 };
410
Michael Kolbfe251992010-07-08 15:41:55 -0700411 private OnItemClickListener mListener = new OnItemClickListener() {
412 public void onItemClick(AdapterView parent, View v, int position, long id) {
413 // It is possible that the view has been canceled when we get to
414 // this point as back has a higher priority
415 if (mCanceled) {
416 android.util.Log.e(LOGTAG, "item clicked when dismissing");
417 return;
418 }
419 if (!mCreateShortcut) {
420 if (0 == position && !mMostVisited) {
421 // XXX: Work-around for a framework issue.
422 mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE);
423 } else {
424 loadUrl(position);
425 }
426 } else {
427 setResultToParent(RESULT_OK, createShortcutIntent(position));
428 finish();
429 }
430 }
431 };
432
433 private OnItemClickListener mListListener = new OnItemClickListener() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800434 public void onItemClick(AdapterView parent, View v, int position, long id) {
435 // It is possible that the view has been canceled when we get to
Nicolas Catania095292f2010-03-15 09:00:14 -0700436 // this point as back has a higher priority
The Android Open Source Project0c908882009-03-03 19:32:16 -0800437 if (mCanceled) {
Leon Scroggins892df312009-07-14 14:48:02 -0400438 android.util.Log.e(LOGTAG, "item clicked when dismissing");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800439 return;
440 }
441 if (!mCreateShortcut) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400442 if (0 == position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800443 // XXX: Work-around for a framework issue.
444 mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE);
445 } else {
446 loadUrl(position);
447 }
448 } else {
Ben Murdoch90b40262010-06-30 13:33:28 +0100449 setResultToParent(RESULT_OK, createShortcutIntent(position));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800450 finish();
451 }
452 }
453 };
454
Patrick Scott3918d442009-08-04 13:22:29 -0400455 private Intent createShortcutIntent(int position) {
456 String url = getUrl(position);
457 String title = getBookmarkTitle(position);
458 Bitmap touchIcon = getTouchIcon(position);
Ben Murdoch90b40262010-06-30 13:33:28 +0100459 Bitmap favicon = getFavicon(position);
460 return BookmarkUtils.createAddToHomeIntent(this, url, title, touchIcon, favicon);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 }
462
463 private void saveCurrentPage() {
464 Intent i = new Intent(BrowserBookmarksPage.this,
465 AddBookmarkPage.class);
466 i.putExtras(getIntent());
467 startActivityForResult(i, BOOKMARKS_SAVE);
468 }
469
470 private void loadUrl(int position) {
471 Intent intent = (new Intent()).setAction(getUrl(position));
472 setResultToParent(RESULT_OK, intent);
473 finish();
474 }
475
476 @Override
477 public boolean onCreateOptionsMenu(Menu menu) {
478 boolean result = super.onCreateOptionsMenu(menu);
Leon Scrogginsb3968bb2009-10-16 09:04:16 -0400479 if (!mCreateShortcut && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800480 MenuInflater inflater = getMenuInflater();
481 inflater.inflate(R.menu.bookmarks, menu);
482 return true;
483 }
484 return result;
485 }
486
487 @Override
Leon Scroggins0c786502009-08-04 16:04:55 -0400488 public boolean onPrepareOptionsMenu(Menu menu) {
Leon Scroggins8382d992009-08-19 11:25:14 -0400489 boolean result = super.onPrepareOptionsMenu(menu);
Leon Scrogginsea002572009-11-24 15:21:18 -0500490 if (mCreateShortcut || mMostVisited || mBookmarksAdapter == null
Leon Scrogginsb3968bb2009-10-16 09:04:16 -0400491 || mBookmarksAdapter.getCount() == 0) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400492 // No need to show the menu if there are no items.
Leon Scroggins8382d992009-08-19 11:25:14 -0400493 return result;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400494 }
Leon Scrogginsfdd10d72009-09-25 13:01:45 -0400495 MenuItem switchItem = menu.findItem(R.id.switch_mode_menu_id);
496 int titleResId;
497 int iconResId;
498 if (mViewMode == BookmarkViewMode.GRID) {
499 titleResId = R.string.switch_to_list;
500 iconResId = R.drawable.ic_menu_list;
501 } else {
502 titleResId = R.string.switch_to_thumbnails;
503 iconResId = R.drawable.ic_menu_thumbnail;
504 }
505 switchItem.setTitle(titleResId);
506 switchItem.setIcon(iconResId);
Leon Scroggins0c786502009-08-04 16:04:55 -0400507 return true;
508 }
509
510 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800511 public boolean onOptionsItemSelected(MenuItem item) {
512 switch (item.getItemId()) {
Leon Scroggins892df312009-07-14 14:48:02 -0400513 case R.id.new_context_menu_id:
514 saveCurrentPage();
515 break;
516
517 case R.id.switch_mode_menu_id:
Ben Murdoch328ea872009-09-16 13:33:29 +0100518 if (mViewMode == BookmarkViewMode.GRID) {
519 switchViewMode(BookmarkViewMode.LIST);
520 } else {
521 switchViewMode(BookmarkViewMode.GRID);
522 }
Leon Scroggins892df312009-07-14 14:48:02 -0400523 break;
524
525 default:
526 return super.onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800527 }
528 return true;
529 }
530
531 private void openInNewWindow(int position) {
532 Bundle b = new Bundle();
533 b.putBoolean("new_window", true);
534 setResultToParent(RESULT_OK,
535 (new Intent()).setAction(getUrl(position)).putExtras(b));
536
537 finish();
538 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700539
The Android Open Source Project0c908882009-03-03 19:32:16 -0800540
541 private void editBookmark(int position) {
Nicolas Catania095292f2010-03-15 09:00:14 -0700542 Intent intent = new Intent(BrowserBookmarksPage.this,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800543 AddBookmarkPage.class);
544 intent.putExtra("bookmark", getRow(position));
545 startActivityForResult(intent, BOOKMARKS_SAVE);
546 }
547
548 @Override
549 protected void onActivityResult(int requestCode, int resultCode,
550 Intent data) {
551 switch(requestCode) {
552 case BOOKMARKS_SAVE:
553 if (resultCode == RESULT_OK) {
554 Bundle extras;
555 if (data != null && (extras = data.getExtras()) != null) {
556 // If there are extras, then we need to save
557 // the edited bookmark. This is done in updateRow()
558 String title = extras.getString("title");
559 String url = extras.getString("url");
560 if (title != null && url != null) {
561 mBookmarksAdapter.updateRow(extras);
562 }
563 } else {
564 // extras == null then a new bookmark was added to
565 // the database.
566 refreshList();
567 }
568 }
569 break;
570 default:
571 break;
572 }
573 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700574
The Android Open Source Project0c908882009-03-03 19:32:16 -0800575 private void displayRemoveBookmarkDialog(int position) {
576 // Put up a dialog asking if the user really wants to
577 // delete the bookmark
578 final int deletePos = position;
579 new AlertDialog.Builder(this)
580 .setTitle(R.string.delete_bookmark)
581 .setIcon(android.R.drawable.ic_dialog_alert)
582 .setMessage(getText(R.string.delete_bookmark_warning).toString().replace(
583 "%s", getBookmarkTitle(deletePos)))
Nicolas Catania095292f2010-03-15 09:00:14 -0700584 .setPositiveButton(R.string.ok,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800585 new DialogInterface.OnClickListener() {
586 public void onClick(DialogInterface dialog, int whichButton) {
587 deleteBookmark(deletePos);
588 }
589 })
590 .setNegativeButton(R.string.cancel, null)
591 .show();
592 }
593
594 /**
595 * Refresh the shown list after the database has changed.
596 */
Leon Scroggins892df312009-07-14 14:48:02 -0400597 private void refreshList() {
Grace Kloba0b82a6f2010-04-20 13:46:42 -0700598 if (mBookmarksAdapter == null) return;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800599 mBookmarksAdapter.refreshList();
600 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700601
The Android Open Source Project0c908882009-03-03 19:32:16 -0800602 /**
603 * Return a hashmap representing the currently highlighted row.
604 */
605 public Bundle getRow(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500606 return mBookmarksAdapter == null ? null
607 : mBookmarksAdapter.getRow(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800608 }
609
610 /**
611 * Return the url of the currently highlighted row.
612 */
613 public String getUrl(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500614 return mBookmarksAdapter == null ? null
615 : mBookmarksAdapter.getUrl(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800616 }
617
Patrick Scotte09761e2009-03-24 20:43:37 -0700618 /**
619 * Return the favicon of the currently highlighted row.
620 */
621 public Bitmap getFavicon(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500622 return mBookmarksAdapter == null ? null
623 : mBookmarksAdapter.getFavicon(position);
Patrick Scotte09761e2009-03-24 20:43:37 -0700624 }
625
Patrick Scott3918d442009-08-04 13:22:29 -0400626 private Bitmap getTouchIcon(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500627 return mBookmarksAdapter == null ? null
628 : mBookmarksAdapter.getTouchIcon(position);
Patrick Scott3918d442009-08-04 13:22:29 -0400629 }
630
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631 private void copy(CharSequence text) {
632 try {
633 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
634 if (clip != null) {
635 clip.setClipboardText(text);
636 }
637 } catch (android.os.RemoteException e) {
638 Log.e(LOGTAG, "Copy failed", e);
639 }
640 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700641
The Android Open Source Project0c908882009-03-03 19:32:16 -0800642 public String getBookmarkTitle(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500643 return mBookmarksAdapter == null ? null
644 : mBookmarksAdapter.getTitle(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800645 }
646
647 /**
648 * Delete the currently highlighted row.
649 */
650 public void deleteBookmark(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500651 if (mBookmarksAdapter == null) return;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800652 mBookmarksAdapter.deleteRow(position);
653 }
Grace Kloba5942df02009-09-18 11:48:29 -0700654
655 @Override
656 public void onBackPressed() {
657 setResultToParent(RESULT_CANCELED, null);
658 mCanceled = true;
659 super.onBackPressed();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800660 }
661
Leon Scrogginsfde97462010-01-11 13:06:21 -0500662 // This Activity is generally a sub-Activity of
663 // CombinedBookmarkHistoryActivity. In that situation, we need to pass our
664 // result code up to our parent. However, if someone calls this Activity
665 // directly, then this has no parent, and it needs to set it on itself.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800666 private void setResultToParent(int resultCode, Intent data) {
Leon Scrogginsfde97462010-01-11 13:06:21 -0500667 Activity parent = getParent();
668 if (parent == null) {
669 setResult(resultCode, data);
670 } else {
671 ((CombinedBookmarkHistoryActivity) parent).setResultFromChild(
672 resultCode, data);
673 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 }
Michael Kolbfe251992010-07-08 15:41:55 -0700675
The Android Open Source Project0c908882009-03-03 19:32:16 -0800676}