blob: de4d11339d930707e525ec3503faaf10b0d55a61 [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 Scotte09761e2009-03-24 20:43:37 -070026import android.graphics.BitmapFactory;
27import android.graphics.Canvas;
28import android.graphics.Color;
29import android.graphics.Paint;
Patrick Scott3918d442009-08-04 13:22:29 -040030import android.graphics.Path;
31import android.graphics.PorterDuff;
32import android.graphics.PorterDuffXfermode;
Patrick Scotte09761e2009-03-24 20:43:37 -070033import android.graphics.RectF;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.net.Uri;
35import android.os.Bundle;
36import android.os.Handler;
37import android.os.Message;
38import android.os.ServiceManager;
39import android.provider.Browser;
40import android.text.IClipboard;
41import android.util.Log;
42import android.view.ContextMenu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.view.Menu;
44import android.view.MenuInflater;
45import android.view.MenuItem;
46import android.view.View;
47import android.view.ViewGroup;
48import android.view.ContextMenu.ContextMenuInfo;
49import android.widget.AdapterView;
Leon Scroggins89c6d362009-07-15 16:54:37 -040050import android.widget.GridView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.widget.ListView;
Leon Scrogginsfeb941d2009-05-28 17:27:38 -040052import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053
Ben Murdoch328ea872009-09-16 13:33:29 +010054/*package*/ enum BookmarkViewMode { NONE, GRID, LIST }
The Android Open Source Project0c908882009-03-03 19:32:16 -080055/**
56 * View showing the user's bookmarks in the browser.
57 */
58public class BrowserBookmarksPage extends Activity implements
59 View.OnCreateContextMenuListener {
60
Ben Murdoch328ea872009-09-16 13:33:29 +010061 private BookmarkViewMode mViewMode = BookmarkViewMode.NONE;
Leon Scroggins89c6d362009-07-15 16:54:37 -040062 private GridView mGridPage;
Leon Scrogginsea002572009-11-24 15:21:18 -050063 private ListView mVerticalList;
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 private BrowserBookmarksAdapter mBookmarksAdapter;
65 private static final int BOOKMARKS_SAVE = 1;
Leon Scroggins190095d2009-08-17 17:01:38 -040066 private boolean mDisableNewWindow;
The Android Open Source Project0c908882009-03-03 19:32:16 -080067 private BookmarkItem mContextHeader;
68 private AddNewBookmark mAddHeader;
69 private boolean mCanceled = false;
70 private boolean mCreateShortcut;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040071 private boolean mMostVisited;
72 private View mEmptyView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 // XXX: There is no public string defining this intent so if Home changes
74 // the value, we have to update this string.
75 private static final String INSTALL_SHORTCUT =
76 "com.android.launcher.action.INSTALL_SHORTCUT";
77
78 private final static String LOGTAG = "browser";
Ben Murdoch328ea872009-09-16 13:33:29 +010079 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 Project0c908882009-03-03 19:32:16 -080081
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 Scott3918d442009-08-04 13:22:29 -0400107 final Intent send = createShortcutIntent(i.position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108 send.setAction(INSTALL_SHORTCUT);
109 sendBroadcast(send);
110 break;
111 case R.id.delete_context_menu_id:
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400112 if (mMostVisited) {
113 Browser.deleteFromHistory(getContentResolver(),
114 getUrl(i.position));
115 refreshList();
116 } else {
117 displayRemoveBookmarkDialog(i.position);
118 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 break;
120 case R.id.new_window_context_menu_id:
121 openInNewWindow(i.position);
122 break;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400123 case R.id.share_link_context_menu_id:
Andrei Popescu10fdba82009-09-24 13:25:47 +0100124 Browser.sendString(BrowserBookmarksPage.this, getUrl(i.position),
125 getText(R.string.choosertitle_sharevia).toString());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126 break;
127 case R.id.copy_url_context_menu_id:
128 copy(getUrl(i.position));
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400129 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 Scrogginsa5d669e2009-08-05 14:07:58 -0400136 // Only for the Most visited page
137 case R.id.save_to_bookmarks_menu_id:
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400138 boolean isBookmark;
139 String name;
140 String url;
Ben Murdoch328ea872009-09-16 13:33:29 +0100141 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400142 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 Scrogginsa5d669e2009-08-05 14:07:58 -0400151 // If the site is bookmarked, the item becomes remove from
152 // bookmarks.
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400153 if (isBookmark) {
Andrei Popescuc9526192009-09-23 15:52:16 +0100154 Bookmarks.removeFromBookmarks(this, getContentResolver(), url, name);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400155 } else {
Leon Scrogginsc1f57592009-08-14 14:16:10 -0400156 Browser.saveBookmark(this, name, url);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400157 }
158 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800159 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) {
168 AdapterView.AdapterContextMenuInfo i =
169 (AdapterView.AdapterContextMenuInfo) menuInfo;
170
171 MenuInflater inflater = getMenuInflater();
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400172 if (mMostVisited) {
173 inflater.inflate(R.menu.historycontext, menu);
174 } else {
175 inflater.inflate(R.menu.bookmarkscontext, menu);
176 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400178 if (0 == i.position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 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 Scroggins892df312009-07-14 14:48:02 -0400186 mAddHeader.setUrl(getIntent().getStringExtra("url"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187 menu.setHeaderView(mAddHeader);
188 return;
189 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400190 if (mMostVisited) {
Ben Murdoch328ea872009-09-16 13:33:29 +0100191 if ((mViewMode == BookmarkViewMode.LIST
192 && ((HistoryItem) i.targetView).isBookmark())
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400193 || 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 Scroggins190095d2009-08-17 17:01:38 -0400202 if (mDisableNewWindow) {
Leon Scroggins892df312009-07-14 14:48:02 -0400203 menu.findItem(R.id.new_window_context_menu_id).setVisible(
204 false);
205 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 if (mContextHeader == null) {
207 mContextHeader = new BookmarkItem(BrowserBookmarksPage.this);
208 } else if (mContextHeader.getParent() != null) {
209 ((ViewGroup) mContextHeader.getParent()).
210 removeView(mContextHeader);
211 }
Ben Murdoch328ea872009-09-16 13:33:29 +0100212 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400213 mBookmarksAdapter.populateBookmarkItem(mContextHeader,
214 i.position);
215 } else {
216 BookmarkItem b = (BookmarkItem) i.targetView;
217 b.copyTo(mContextHeader);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800218 }
Leon Scroggins892df312009-07-14 14:48:02 -0400219 menu.setHeaderView(mContextHeader);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800220 }
221
222 /**
223 * Create a new BrowserBookmarksPage.
224 */
225 @Override
226 protected void onCreate(Bundle icicle) {
227 super.onCreate(icicle);
228
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
230 mCreateShortcut = true;
231 }
Leon Scroggins190095d2009-08-17 17:01:38 -0400232 mDisableNewWindow = getIntent().getBooleanExtra("disable_new_window",
233 false);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400234 mMostVisited = getIntent().getBooleanExtra("mostVisited", false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400236 if (mCreateShortcut) {
237 setTitle(R.string.browser_bookmarks_page_bookmarks_text);
238 }
Leon Scrogginsea002572009-11-24 15:21:18 -0500239 mHandler.obtainMessage(CREATE_ADAPTER).sendToTarget();
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400240
241 setContentView(R.layout.empty_history);
242 mEmptyView = findViewById(R.id.empty_view);
243 mEmptyView.setVisibility(View.GONE);
244
Ben Murdoch328ea872009-09-16 13:33:29 +0100245 SharedPreferences p = getPreferences(MODE_PRIVATE);
246
247 // See if the user has set a preference for the view mode of their
248 // bookmarks. Otherwise default to grid mode.
249 BookmarkViewMode preference = BookmarkViewMode.NONE;
250 if (mMostVisited) {
Leon Scrogginsb3968bb2009-10-16 09:04:16 -0400251 // For the most visited page, only use list mode.
252 preference = BookmarkViewMode.LIST;
Ben Murdoch328ea872009-09-16 13:33:29 +0100253 } else {
254 preference = BookmarkViewMode.values()[p.getInt(
255 PREF_BOOKMARK_VIEW_MODE, BookmarkViewMode.GRID.ordinal())];
256 }
257 switchViewMode(preference);
Leon Scroggins892df312009-07-14 14:48:02 -0400258 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259
Leon Scroggins892df312009-07-14 14:48:02 -0400260 /**
261 * Set the ContentView to be either the grid of thumbnails or the vertical
Ben Murdoch328ea872009-09-16 13:33:29 +0100262 * list.
Leon Scroggins892df312009-07-14 14:48:02 -0400263 */
Leon Scrogginsea002572009-11-24 15:21:18 -0500264 private void switchViewMode(BookmarkViewMode viewMode) {
265 if (mViewMode == viewMode) {
Ben Murdoch328ea872009-09-16 13:33:29 +0100266 return;
267 }
268
Leon Scrogginsea002572009-11-24 15:21:18 -0500269 mViewMode = viewMode;
Ben Murdoch328ea872009-09-16 13:33:29 +0100270
271 // Update the preferences to make the new view mode sticky.
272 Editor ed = getPreferences(MODE_PRIVATE).edit();
273 if (mMostVisited) {
274 ed.putInt(PREF_MOST_VISITED_VIEW_MODE, mViewMode.ordinal());
275 } else {
276 ed.putInt(PREF_BOOKMARK_VIEW_MODE, mViewMode.ordinal());
277 }
278 ed.commit();
279
Leon Scrogginsea002572009-11-24 15:21:18 -0500280 if (mBookmarksAdapter != null) {
281 mBookmarksAdapter.switchViewMode(viewMode);
282 }
Ben Murdoch328ea872009-09-16 13:33:29 +0100283 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400284 if (mGridPage == null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -0400285 mGridPage = new GridView(this);
Leon Scrogginsea002572009-11-24 15:21:18 -0500286 if (mBookmarksAdapter != null) {
287 mGridPage.setAdapter(mBookmarksAdapter);
288 }
Leon Scroggins892df312009-07-14 14:48:02 -0400289 mGridPage.setOnItemClickListener(mListener);
Leon Scroggins89c6d362009-07-15 16:54:37 -0400290 mGridPage.setNumColumns(GridView.AUTO_FIT);
Leon Scrogginsf8551612009-09-24 16:06:02 -0400291 mGridPage.setColumnWidth(
292 BrowserActivity.getDesiredThumbnailWidth(this));
Leon Scroggins89c6d362009-07-15 16:54:37 -0400293 mGridPage.setFocusable(true);
294 mGridPage.setFocusableInTouchMode(true);
295 mGridPage.setSelector(android.R.drawable.gallery_thumb);
Leon Scrogginsf8551612009-09-24 16:06:02 -0400296 float density = getResources().getDisplayMetrics().density;
297 mGridPage.setVerticalSpacing((int) (14 * density));
298 mGridPage.setHorizontalSpacing((int) (8 * density));
299 mGridPage.setStretchMode(GridView.STRETCH_SPACING);
Leon Scrogginsbbe6d5b2009-09-28 12:01:00 -0400300 mGridPage.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
Leon Scrogginsf8551612009-09-24 16:06:02 -0400301 mGridPage.setDrawSelectorOnTop(true);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400302 if (mMostVisited) {
303 mGridPage.setEmptyView(mEmptyView);
304 }
Leon Scroggins892df312009-07-14 14:48:02 -0400305 if (!mCreateShortcut) {
306 mGridPage.setOnCreateContextMenuListener(this);
307 }
308 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400309 addContentView(mGridPage, FULL_SCREEN_PARAMS);
310 if (mVerticalList != null) {
311 ViewGroup parent = (ViewGroup) mVerticalList.getParent();
312 if (parent != null) {
313 parent.removeView(mVerticalList);
314 }
315 }
Leon Scroggins892df312009-07-14 14:48:02 -0400316 } else {
317 if (null == mVerticalList) {
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400318 ListView listView = new ListView(this);
Leon Scrogginsea002572009-11-24 15:21:18 -0500319 if (mBookmarksAdapter != null) {
320 listView.setAdapter(mBookmarksAdapter);
321 }
Leon Scroggins892df312009-07-14 14:48:02 -0400322 listView.setDrawSelectorOnTop(false);
323 listView.setVerticalScrollBarEnabled(true);
324 listView.setOnItemClickListener(mListener);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400325 if (mMostVisited) {
326 listView.setEmptyView(mEmptyView);
327 }
Leon Scroggins892df312009-07-14 14:48:02 -0400328 if (!mCreateShortcut) {
329 listView.setOnCreateContextMenuListener(this);
330 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400331 mVerticalList = listView;
Leon Scroggins892df312009-07-14 14:48:02 -0400332 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400333 addContentView(mVerticalList, FULL_SCREEN_PARAMS);
334 if (mGridPage != null) {
335 ViewGroup parent = (ViewGroup) mGridPage.getParent();
336 if (parent != null) {
337 parent.removeView(mGridPage);
338 }
339 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400340 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800341 }
342
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400343 private static final ViewGroup.LayoutParams FULL_SCREEN_PARAMS
344 = new ViewGroup.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -0800345 ViewGroup.LayoutParams.MATCH_PARENT,
346 ViewGroup.LayoutParams.MATCH_PARENT);
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400347
The Android Open Source Project0c908882009-03-03 19:32:16 -0800348 private static final int SAVE_CURRENT_PAGE = 1000;
Leon Scrogginsea002572009-11-24 15:21:18 -0500349 private static final int CREATE_ADAPTER = 1001;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800350 private final Handler mHandler = new Handler() {
351 @Override
352 public void handleMessage(Message msg) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500353 switch (msg.what) {
354 case SAVE_CURRENT_PAGE:
355 saveCurrentPage();
356 break;
357 case CREATE_ADAPTER:
358 Intent intent = getIntent();
359 mBookmarksAdapter = new BrowserBookmarksAdapter(
360 BrowserBookmarksPage.this,
361 intent.getStringExtra("url"),
362 intent.getStringExtra("title"),
363 (Bitmap) intent.getParcelableExtra("thumbnail"),
364 mCreateShortcut,
365 mMostVisited);
366 mBookmarksAdapter.switchViewMode(mViewMode);
367 if (mGridPage != null) {
368 mGridPage.setAdapter(mBookmarksAdapter);
369 }
370 if (mVerticalList != null) {
371 mVerticalList.setAdapter(mBookmarksAdapter);
372 }
373 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800374 }
375 }
376 };
377
378 private AdapterView.OnItemClickListener mListener = new AdapterView.OnItemClickListener() {
379 public void onItemClick(AdapterView parent, View v, int position, long id) {
380 // It is possible that the view has been canceled when we get to
381 // this point as back has a higher priority
382 if (mCanceled) {
Leon Scroggins892df312009-07-14 14:48:02 -0400383 android.util.Log.e(LOGTAG, "item clicked when dismissing");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800384 return;
385 }
386 if (!mCreateShortcut) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400387 if (0 == position && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800388 // XXX: Work-around for a framework issue.
389 mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE);
390 } else {
391 loadUrl(position);
392 }
393 } else {
Patrick Scott3918d442009-08-04 13:22:29 -0400394 final Intent intent = createShortcutIntent(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800395 setResultToParent(RESULT_OK, intent);
396 finish();
397 }
398 }
399 };
400
Patrick Scott3918d442009-08-04 13:22:29 -0400401 private Intent createShortcutIntent(int position) {
402 String url = getUrl(position);
403 String title = getBookmarkTitle(position);
404 Bitmap touchIcon = getTouchIcon(position);
405
The Android Open Source Project0c908882009-03-03 19:32:16 -0800406 final Intent i = new Intent();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700407 final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW,
408 Uri.parse(url));
409 long urlHash = url.hashCode();
410 long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
411 shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID,
412 Long.toString(uniqueId));
413 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800414 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Patrick Scott3918d442009-08-04 13:22:29 -0400415 // Use the apple-touch-icon if available
416 if (touchIcon != null) {
417 // Make a copy so we can modify the pixels.
418 Bitmap copy = touchIcon.copy(Bitmap.Config.ARGB_8888, true);
Patrick Scotte09761e2009-03-24 20:43:37 -0700419 Canvas canvas = new Canvas(copy);
420
Patrick Scott3918d442009-08-04 13:22:29 -0400421 // Construct a path from a round rect. This will allow drawing with
422 // an inverse fill so we can punch a hole using the round rect.
423 Path path = new Path();
424 path.setFillType(Path.FillType.INVERSE_WINDING);
Patrick Scott59ce8302009-09-18 16:29:38 -0400425 RectF rect = new RectF(0, 0, touchIcon.getWidth(),
426 touchIcon.getHeight());
427 rect.inset(1, 1);
428 path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
Patrick Scotte09761e2009-03-24 20:43:37 -0700429
Patrick Scott3918d442009-08-04 13:22:29 -0400430 // Construct a paint that clears the outside of the rectangle and
431 // draw.
432 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
433 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
434 canvas.drawPath(path, paint);
Patrick Scotte09761e2009-03-24 20:43:37 -0700435
Patrick Scotte09761e2009-03-24 20:43:37 -0700436 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
Patrick Scott3918d442009-08-04 13:22:29 -0400437 } else {
438 Bitmap favicon = getFavicon(position);
439 if (favicon == null) {
440 i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
441 Intent.ShortcutIconResource.fromContext(
442 BrowserBookmarksPage.this,
443 R.drawable.ic_launcher_shortcut_browser_bookmark));
444 } else {
445 Bitmap icon = BitmapFactory.decodeResource(getResources(),
446 R.drawable.ic_launcher_shortcut_browser_bookmark);
447
448 // Make a copy of the regular icon so we can modify the pixels.
449 Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true);
450 Canvas canvas = new Canvas(copy);
451
452 // Make a Paint for the white background rectangle and for
453 // filtering the favicon.
454 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG
455 | Paint.FILTER_BITMAP_FLAG);
456 p.setStyle(Paint.Style.FILL_AND_STROKE);
457 p.setColor(Color.WHITE);
458
Patrick Scott4aff3aa2009-10-05 09:32:46 -0400459 float density = getResources().getDisplayMetrics().density;
Patrick Scott3918d442009-08-04 13:22:29 -0400460 // Create a rectangle that is slightly wider than the favicon
Patrick Scott4aff3aa2009-10-05 09:32:46 -0400461 final float iconSize = 16 * density; // 16x16 favicon
462 final float padding = 2; // white padding around icon
Patrick Scott3918d442009-08-04 13:22:29 -0400463 final float rectSize = iconSize + 2 * padding;
464 final float y = icon.getHeight() - rectSize;
465 RectF r = new RectF(0, y, rectSize, y + rectSize);
466
467 // Draw a white rounded rectangle behind the favicon
468 canvas.drawRoundRect(r, 2, 2, p);
469
470 // Draw the favicon in the same rectangle as the rounded
471 // rectangle but inset by the padding
472 // (results in a 16x16 favicon).
473 r.inset(padding, padding);
474 canvas.drawBitmap(favicon, null, r, p);
475 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
476 }
Patrick Scotte09761e2009-03-24 20:43:37 -0700477 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 // Do not allow duplicate items
479 i.putExtra("duplicate", false);
480 return i;
481 }
482
483 private void saveCurrentPage() {
484 Intent i = new Intent(BrowserBookmarksPage.this,
485 AddBookmarkPage.class);
486 i.putExtras(getIntent());
487 startActivityForResult(i, BOOKMARKS_SAVE);
488 }
489
490 private void loadUrl(int position) {
491 Intent intent = (new Intent()).setAction(getUrl(position));
492 setResultToParent(RESULT_OK, intent);
493 finish();
494 }
495
496 @Override
497 public boolean onCreateOptionsMenu(Menu menu) {
498 boolean result = super.onCreateOptionsMenu(menu);
Leon Scrogginsb3968bb2009-10-16 09:04:16 -0400499 if (!mCreateShortcut && !mMostVisited) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 MenuInflater inflater = getMenuInflater();
501 inflater.inflate(R.menu.bookmarks, menu);
502 return true;
503 }
504 return result;
505 }
506
507 @Override
Leon Scroggins0c786502009-08-04 16:04:55 -0400508 public boolean onPrepareOptionsMenu(Menu menu) {
Leon Scroggins8382d992009-08-19 11:25:14 -0400509 boolean result = super.onPrepareOptionsMenu(menu);
Leon Scrogginsea002572009-11-24 15:21:18 -0500510 if (mCreateShortcut || mMostVisited || mBookmarksAdapter == null
Leon Scrogginsb3968bb2009-10-16 09:04:16 -0400511 || mBookmarksAdapter.getCount() == 0) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400512 // No need to show the menu if there are no items.
Leon Scroggins8382d992009-08-19 11:25:14 -0400513 return result;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400514 }
Leon Scrogginsfdd10d72009-09-25 13:01:45 -0400515 MenuItem switchItem = menu.findItem(R.id.switch_mode_menu_id);
516 int titleResId;
517 int iconResId;
518 if (mViewMode == BookmarkViewMode.GRID) {
519 titleResId = R.string.switch_to_list;
520 iconResId = R.drawable.ic_menu_list;
521 } else {
522 titleResId = R.string.switch_to_thumbnails;
523 iconResId = R.drawable.ic_menu_thumbnail;
524 }
525 switchItem.setTitle(titleResId);
526 switchItem.setIcon(iconResId);
Leon Scroggins0c786502009-08-04 16:04:55 -0400527 return true;
528 }
529
530 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800531 public boolean onOptionsItemSelected(MenuItem item) {
532 switch (item.getItemId()) {
Leon Scroggins892df312009-07-14 14:48:02 -0400533 case R.id.new_context_menu_id:
534 saveCurrentPage();
535 break;
536
537 case R.id.switch_mode_menu_id:
Ben Murdoch328ea872009-09-16 13:33:29 +0100538 if (mViewMode == BookmarkViewMode.GRID) {
539 switchViewMode(BookmarkViewMode.LIST);
540 } else {
541 switchViewMode(BookmarkViewMode.GRID);
542 }
Leon Scroggins892df312009-07-14 14:48:02 -0400543 break;
544
545 default:
546 return super.onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800547 }
548 return true;
549 }
550
551 private void openInNewWindow(int position) {
552 Bundle b = new Bundle();
553 b.putBoolean("new_window", true);
554 setResultToParent(RESULT_OK,
555 (new Intent()).setAction(getUrl(position)).putExtras(b));
556
557 finish();
558 }
559
560
561 private void editBookmark(int position) {
562 Intent intent = new Intent(BrowserBookmarksPage.this,
563 AddBookmarkPage.class);
564 intent.putExtra("bookmark", getRow(position));
565 startActivityForResult(intent, BOOKMARKS_SAVE);
566 }
567
568 @Override
569 protected void onActivityResult(int requestCode, int resultCode,
570 Intent data) {
571 switch(requestCode) {
572 case BOOKMARKS_SAVE:
573 if (resultCode == RESULT_OK) {
574 Bundle extras;
575 if (data != null && (extras = data.getExtras()) != null) {
576 // If there are extras, then we need to save
577 // the edited bookmark. This is done in updateRow()
578 String title = extras.getString("title");
579 String url = extras.getString("url");
580 if (title != null && url != null) {
581 mBookmarksAdapter.updateRow(extras);
582 }
583 } else {
584 // extras == null then a new bookmark was added to
585 // the database.
586 refreshList();
587 }
588 }
589 break;
590 default:
591 break;
592 }
593 }
594
595 private void displayRemoveBookmarkDialog(int position) {
596 // Put up a dialog asking if the user really wants to
597 // delete the bookmark
598 final int deletePos = position;
599 new AlertDialog.Builder(this)
600 .setTitle(R.string.delete_bookmark)
601 .setIcon(android.R.drawable.ic_dialog_alert)
602 .setMessage(getText(R.string.delete_bookmark_warning).toString().replace(
603 "%s", getBookmarkTitle(deletePos)))
604 .setPositiveButton(R.string.ok,
605 new DialogInterface.OnClickListener() {
606 public void onClick(DialogInterface dialog, int whichButton) {
607 deleteBookmark(deletePos);
608 }
609 })
610 .setNegativeButton(R.string.cancel, null)
611 .show();
612 }
613
614 /**
615 * Refresh the shown list after the database has changed.
616 */
Leon Scroggins892df312009-07-14 14:48:02 -0400617 private void refreshList() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 mBookmarksAdapter.refreshList();
619 }
620
621 /**
622 * Return a hashmap representing the currently highlighted row.
623 */
624 public Bundle getRow(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500625 return mBookmarksAdapter == null ? null
626 : mBookmarksAdapter.getRow(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800627 }
628
629 /**
630 * Return the url of the currently highlighted row.
631 */
632 public String getUrl(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500633 return mBookmarksAdapter == null ? null
634 : mBookmarksAdapter.getUrl(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800635 }
636
Patrick Scotte09761e2009-03-24 20:43:37 -0700637 /**
638 * Return the favicon of the currently highlighted row.
639 */
640 public Bitmap getFavicon(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500641 return mBookmarksAdapter == null ? null
642 : mBookmarksAdapter.getFavicon(position);
Patrick Scotte09761e2009-03-24 20:43:37 -0700643 }
644
Patrick Scott3918d442009-08-04 13:22:29 -0400645 private Bitmap getTouchIcon(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500646 return mBookmarksAdapter == null ? null
647 : mBookmarksAdapter.getTouchIcon(position);
Patrick Scott3918d442009-08-04 13:22:29 -0400648 }
649
The Android Open Source Project0c908882009-03-03 19:32:16 -0800650 private void copy(CharSequence text) {
651 try {
652 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
653 if (clip != null) {
654 clip.setClipboardText(text);
655 }
656 } catch (android.os.RemoteException e) {
657 Log.e(LOGTAG, "Copy failed", e);
658 }
659 }
660
661 public String getBookmarkTitle(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500662 return mBookmarksAdapter == null ? null
663 : mBookmarksAdapter.getTitle(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664 }
665
666 /**
667 * Delete the currently highlighted row.
668 */
669 public void deleteBookmark(int position) {
Leon Scrogginsea002572009-11-24 15:21:18 -0500670 if (mBookmarksAdapter == null) return;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800671 mBookmarksAdapter.deleteRow(position);
672 }
Grace Kloba5942df02009-09-18 11:48:29 -0700673
674 @Override
675 public void onBackPressed() {
676 setResultToParent(RESULT_CANCELED, null);
677 mCanceled = true;
678 super.onBackPressed();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800679 }
680
681 // This Activity is generally a sub-Activity of CombinedHistoryActivity. In
682 // that situation, we need to pass our result code up to our parent.
683 // However, if someone calls this Activity directly, then this has no
684 // parent, and it needs to set it on itself.
685 private void setResultToParent(int resultCode, Intent data) {
686 Activity a = getParent() == null ? this : getParent();
687 a.setResult(resultCode, data);
688 }
689}