blob: 2b8c5350c376b25d23539c516beb4cc4c506cfb7 [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;
Jeff Hamilton64144e42010-08-18 16:31:59 -050021import android.app.Fragment;
Jeff Hamilton84029622010-08-05 14:29:28 -050022import android.app.LoaderManager;
Dianne Hackborneb07b172010-08-26 22:18:14 -070023import android.content.ClipData;
Michael Kolbbef7a752010-08-25 17:11:35 -070024import android.content.ClipboardManager;
Jeff Hamilton1a805652010-09-07 12:36:30 -070025import android.content.ContentResolver;
Jeff Hamilton84029622010-08-05 14:29:28 -050026import android.content.ContentUris;
27import android.content.ContentValues;
Dianne Hackborn80f32622010-08-05 14:17:53 -070028import android.content.Context;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050029import android.content.CursorLoader;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.content.DialogInterface;
31import android.content.Intent;
Jeff Hamilton84029622010-08-05 14:29:28 -050032import android.content.Loader;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050033import android.content.SharedPreferences;
John Reck8af90642010-11-23 10:27:29 -080034import android.content.SharedPreferences.Editor;
Jeff Hamilton84029622010-08-05 14:29:28 -050035import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.graphics.Bitmap;
Jeff Hamilton84029622010-08-05 14:29:28 -050037import android.graphics.BitmapFactory;
38import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.os.Bundle;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050040import android.preference.PreferenceManager;
Jeff Hamilton69bd7072010-08-17 12:38:22 -050041import android.provider.BrowserContract;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050042import android.provider.BrowserContract.Accounts;
43import android.text.TextUtils;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.view.ContextMenu;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050045import android.view.ContextMenu.ContextMenuInfo;
Jeff Hamilton64144e42010-08-18 16:31:59 -050046import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.view.MenuInflater;
48import android.view.MenuItem;
49import android.view.View;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050050import android.view.ViewGroup;
Patrick Scottc1cf63a2010-03-09 16:02:08 -050051import android.webkit.WebIconDatabase.IconListener;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050052import android.widget.Adapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053import android.widget.AdapterView;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050054import android.widget.AdapterView.OnItemClickListener;
55import android.widget.AdapterView.OnItemSelectedListener;
Jeff Hamilton84029622010-08-05 14:29:28 -050056import android.widget.GridView;
John Reck8af90642010-11-23 10:27:29 -080057import android.widget.ListView;
Jeff Hamilton84029622010-08-05 14:29:28 -050058import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059
60/**
61 * View showing the user's bookmarks in the browser.
62 */
Jeff Hamilton64144e42010-08-18 16:31:59 -050063public class BrowserBookmarksPage extends Fragment implements View.OnCreateContextMenuListener,
Michael Kolb370a4f32010-10-06 10:45:32 -070064 LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener, IconListener,
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050065 OnItemSelectedListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080066
Jeff Hamilton84029622010-08-05 14:29:28 -050067 static final int BOOKMARKS_SAVE = 1;
68 static final String LOGTAG = "browser";
Nicolas Catania095292f2010-03-15 09:00:14 -070069
Jeff Hamilton84029622010-08-05 14:29:28 -050070 static final int LOADER_BOOKMARKS = 1;
Jeff Hamiltona9bad832010-09-24 11:05:30 -050071 static final int LOADER_ACCOUNTS_THEN_BOOKMARKS = 2;
Jeff Hamilton84029622010-08-05 14:29:28 -050072
Michael Kolb801ecb72010-08-25 12:57:38 -070073 static final String EXTRA_SHORTCUT = "create_shortcut";
74 static final String EXTRA_DISABLE_WINDOW = "disable_new_window";
75
Jeff Hamilton70c77692010-09-21 19:11:24 -050076 static final String ACCOUNT_NAME_UNSYNCED = "Unsynced";
Michael Kolbe421c242010-10-04 19:29:01 -070077
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050078 public static final String PREF_ACCOUNT_TYPE = "acct_type";
79 public static final String PREF_ACCOUNT_NAME = "acct_name";
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050080
81 static final String DEFAULT_ACCOUNT = "local";
John Reck8af90642010-11-23 10:27:29 -080082 static final int VIEW_THUMBNAILS = 1;
83 static final int VIEW_LIST = 2;
84 static final String PREF_SELECTED_VIEW = "bookmarks_view";
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050085
Jeff Hamilton64144e42010-08-18 16:31:59 -050086 BookmarksHistoryCallbacks mCallbacks;
Jeff Hamilton84029622010-08-05 14:29:28 -050087 GridView mGrid;
John Reck8af90642010-11-23 10:27:29 -080088 ListView mList;
Jeff Hamilton84029622010-08-05 14:29:28 -050089 BrowserBookmarksAdapter mAdapter;
90 boolean mDisableNewWindow;
91 BookmarkItem mContextHeader;
92 boolean mCanceled = false;
93 boolean mCreateShortcut;
94 View mEmptyView;
John Reck8af90642010-11-23 10:27:29 -080095 int mCurrentView;
Jeff Hamilton84029622010-08-05 14:29:28 -050096
Michael Kolb370a4f32010-10-06 10:45:32 -070097 BreadCrumbView mCrumbs;
98
99 static BrowserBookmarksPage newInstance(BookmarksHistoryCallbacks cb,
100 BreadCrumbView crumbs, Bundle args) {
Michael Kolbe421c242010-10-04 19:29:01 -0700101 BrowserBookmarksPage bbp = new BrowserBookmarksPage();
102 bbp.mCallbacks = cb;
103 bbp.setArguments(args);
Michael Kolb370a4f32010-10-06 10:45:32 -0700104 bbp.mCrumbs = crumbs;
Michael Kolbe421c242010-10-04 19:29:01 -0700105 return bbp;
106 }
107
Jeff Hamilton84029622010-08-05 14:29:28 -0500108 @Override
109 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
110 switch (id) {
111 case LOADER_BOOKMARKS: {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700112 String accountType = null;
113 String accountName = null;
Jeff Hamilton84029622010-08-05 14:29:28 -0500114 if (args != null) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700115 accountType = args.getString(BookmarksLoader.ARG_ACCOUNT_TYPE);
116 accountName = args.getString(BookmarksLoader.ARG_ACCOUNT_NAME);
Jeff Hamilton84029622010-08-05 14:29:28 -0500117 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700118 BookmarksLoader bl = new BookmarksLoader(getActivity(), accountType, accountName);
119 if (mCrumbs != null) {
120 Uri uri = (Uri) mCrumbs.getTopData();
121 if (uri != null) {
122 bl.setUri(uri);
123 }
124 }
125 return bl;
Jeff Hamilton84029622010-08-05 14:29:28 -0500126 }
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500127 case LOADER_ACCOUNTS_THEN_BOOKMARKS: {
128 return new CursorLoader(getActivity(), Accounts.CONTENT_URI,
129 new String[] { Accounts.ACCOUNT_TYPE, Accounts.ACCOUNT_NAME }, null, null,
130 null);
131 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500132 }
133 throw new UnsupportedOperationException("Unknown loader id " + id);
134 }
135
136 @Override
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500137 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
138 switch (loader.getId()) {
139 case LOADER_BOOKMARKS: {
140 // Set the visibility of the empty vs. content views
141 if (cursor == null || cursor.getCount() == 0) {
142 mEmptyView.setVisibility(View.VISIBLE);
143 mGrid.setVisibility(View.GONE);
John Reck8af90642010-11-23 10:27:29 -0800144 mList.setVisibility(View.GONE);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500145 } else {
146 mEmptyView.setVisibility(View.GONE);
John Reck8af90642010-11-23 10:27:29 -0800147 setupBookmarkView();
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500148 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500149
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500150 // Give the new data to the adapter
151 mAdapter.changeCursor(cursor);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500152 break;
153 }
154
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500155 case LOADER_ACCOUNTS_THEN_BOOKMARKS: {
156 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
157 getActivity());
158 String storedAccountType = prefs.getString(PREF_ACCOUNT_TYPE, null);
159 String storedAccountName = prefs.getString(PREF_ACCOUNT_NAME, null);
160 String accountType =
161 TextUtils.isEmpty(storedAccountType) ? DEFAULT_ACCOUNT : storedAccountType;
162 String accountName =
163 TextUtils.isEmpty(storedAccountName) ? DEFAULT_ACCOUNT : storedAccountName;
164
165 Bundle args = null;
166 if (cursor == null || !cursor.moveToFirst()) {
167 // No accounts, set the prefs to the default
168 accountType = DEFAULT_ACCOUNT;
169 accountName = DEFAULT_ACCOUNT;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500170 } else {
171 int accountPosition = -1;
172
173 if (!DEFAULT_ACCOUNT.equals(accountType) &&
174 !DEFAULT_ACCOUNT.equals(accountName)) {
175 // Check to see if the account in prefs still exists
176 cursor.moveToFirst();
177 do {
178 if (accountType.equals(cursor.getString(0))
179 && accountName.equals(cursor.getString(1))) {
180 accountPosition = cursor.getPosition();
181 break;
182 }
183 } while (cursor.moveToNext());
184 }
185
186 if (accountPosition == -1) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500187 if (!(DEFAULT_ACCOUNT.equals(accountType)
Jeff Hamilton70c77692010-09-21 19:11:24 -0500188 && DEFAULT_ACCOUNT.equals(accountName))) {
Jeff Hamilton70c77692010-09-21 19:11:24 -0500189 // No account is set in prefs and there is at least one,
190 // so pick the first one as the default
191 cursor.moveToFirst();
192 accountType = cursor.getString(0);
193 accountName = cursor.getString(1);
Jeff Hamilton70c77692010-09-21 19:11:24 -0500194 }
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500195 }
196
197 args = new Bundle();
198 args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
199 args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500200 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500201
202 // The stored account name wasn't found, update the stored account with a valid one
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500203 if (!accountType.equals(storedAccountType)
204 || !accountName.equals(storedAccountName)) {
205 prefs.edit()
206 .putString(PREF_ACCOUNT_TYPE, accountType)
207 .putString(PREF_ACCOUNT_NAME, accountName)
208 .apply();
209 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500210 getLoaderManager().initLoader(LOADER_BOOKMARKS, args, this);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500211
212 break;
213 }
214 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500215 }
216
Michael Kolb370a4f32010-10-06 10:45:32 -0700217 long getFolderId() {
218 LoaderManager manager = getLoaderManager();
219 BookmarksLoader loader =
220 (BookmarksLoader) ((Loader)(manager.getLoader(LOADER_BOOKMARKS)));
221
222 Uri uri = loader.getUri();
223 if (uri != null) {
224 try {
225 return ContentUris.parseId(uri);
226 } catch (NumberFormatException nfx) {
227 return -1;
228 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500229 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700230 return -1;
231 }
232
233 public void onFolderChange(int level, Object data) {
234 Uri uri = (Uri) data;
235 if (uri == null) {
236 // top level
237 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
238 }
239 LoaderManager manager = getLoaderManager();
240 BookmarksLoader loader =
241 (BookmarksLoader) ((Loader) manager.getLoader(LOADER_BOOKMARKS));
242 loader.setUri(uri);
243 loader.forceLoad();
244
Jeff Hamilton84029622010-08-05 14:29:28 -0500245 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246
247 @Override
248 public boolean onContextItemSelected(MenuItem item) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500249 final Activity activity = getActivity();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800250 // It is possible that the view has been canceled when we get to
Nicolas Catania095292f2010-03-15 09:00:14 -0700251 // this point as back has a higher priority
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 if (mCanceled) {
Leon Scrogginsc8cae082010-11-03 18:51:46 -0400253 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800254 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700255 AdapterView.AdapterContextMenuInfo i =
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256 (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
257 // If we have no menu info, we can't tell which item was selected.
258 if (i == null) {
Leon Scrogginsc8cae082010-11-03 18:51:46 -0400259 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700261
The Android Open Source Project0c908882009-03-03 19:32:16 -0800262 switch (item.getItemId()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263 case R.id.open_context_menu_id:
264 loadUrl(i.position);
265 break;
266 case R.id.edit_context_menu_id:
267 editBookmark(i.position);
268 break;
269 case R.id.shortcut_context_menu_id:
Jeff Hamilton64144e42010-08-18 16:31:59 -0500270 activity.sendBroadcast(createShortcutIntent(i.position));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271 break;
272 case R.id.delete_context_menu_id:
Jeff Hamilton84029622010-08-05 14:29:28 -0500273 displayRemoveBookmarkDialog(i.position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800274 break;
275 case R.id.new_window_context_menu_id:
276 openInNewWindow(i.position);
277 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500278 case R.id.share_link_context_menu_id: {
279 Cursor cursor = (Cursor) mAdapter.getItem(i.position);
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 Controller.sharePage(activity,
Jeff Hamilton84029622010-08-05 14:29:28 -0500281 cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE),
282 cursor.getString(BookmarksLoader.COLUMN_INDEX_URL),
283 getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON),
284 getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_THUMBNAIL));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800285 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500286 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800287 case R.id.copy_url_context_menu_id:
288 copy(getUrl(i.position));
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400289 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500290 case R.id.homepage_context_menu_id: {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500291 BrowserSettings.getInstance().setHomePage(activity, getUrl(i.position));
292 Toast.makeText(activity, R.string.homepage_set, Toast.LENGTH_LONG).show();
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400293 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500294 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400295 // Only for the Most visited page
Jeff Hamilton84029622010-08-05 14:29:28 -0500296 case R.id.save_to_bookmarks_menu_id: {
297 Cursor cursor = (Cursor) mAdapter.getItem(i.position);
298 String name = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
299 String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400300 // If the site is bookmarked, the item becomes remove from
301 // bookmarks.
Jeff Hamilton64144e42010-08-18 16:31:59 -0500302 Bookmarks.removeFromBookmarks(activity, activity.getContentResolver(), url, name);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400303 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500304 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800305 default:
306 return super.onContextItemSelected(item);
307 }
308 return true;
309 }
310
Jeff Hamilton84029622010-08-05 14:29:28 -0500311 Bitmap getBitmap(Cursor cursor, int columnIndex) {
312 byte[] data = cursor.getBlob(columnIndex);
313 if (data == null) {
314 return null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800315 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500316 return BitmapFactory.decodeByteArray(data, 0, data.length);
317 }
318
319 @Override
320 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
321 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Leon Scroggins5e8a40f2010-09-27 17:13:35 -0400322 Cursor cursor = (Cursor) mAdapter.getItem(info.position);
323 boolean isFolder
324 = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
325 if (isFolder) return;
Michael Kolb801ecb72010-08-25 12:57:38 -0700326
Jeff Hamilton64144e42010-08-18 16:31:59 -0500327 final Activity activity = getActivity();
328 MenuInflater inflater = activity.getMenuInflater();
Jeff Hamilton84029622010-08-05 14:29:28 -0500329 inflater.inflate(R.menu.bookmarkscontext, menu);
330
331 if (mDisableNewWindow) {
332 menu.findItem(R.id.new_window_context_menu_id).setVisible(false);
333 }
334
335 if (mContextHeader == null) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500336 mContextHeader = new BookmarkItem(activity);
Jeff Hamilton84029622010-08-05 14:29:28 -0500337 } else if (mContextHeader.getParent() != null) {
338 ((ViewGroup) mContextHeader.getParent()).removeView(mContextHeader);
339 }
340
Leon Scroggins5e8a40f2010-09-27 17:13:35 -0400341 populateBookmarkItem(cursor, mContextHeader);
Jeff Hamilton84029622010-08-05 14:29:28 -0500342
343 menu.setHeaderView(mContextHeader);
344 }
345
Leon Scroggins5e8a40f2010-09-27 17:13:35 -0400346 private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500347 String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
348 item.setUrl(url);
349 item.setName(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
350 Bitmap bitmap = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON);
351 if (bitmap == null) {
Michael Kolbe421c242010-10-04 19:29:01 -0700352 bitmap = CombinedBookmarkHistoryView.getIconListenerSet().getFavicon(url);
Jeff Hamilton84029622010-08-05 14:29:28 -0500353 }
354 item.setFavicon(bitmap);
355 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800356
357 /**
358 * Create a new BrowserBookmarksPage.
Nicolas Catania095292f2010-03-15 09:00:14 -0700359 */
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 @Override
Jeff Hamilton64144e42010-08-18 16:31:59 -0500361 public void onCreate(Bundle icicle) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362 super.onCreate(icicle);
363
Jeff Hamilton64144e42010-08-18 16:31:59 -0500364 Bundle args = getArguments();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400365 mCreateShortcut = args == null ? false : args.getBoolean(EXTRA_SHORTCUT, false);
366 mDisableNewWindow = args == null ? false : args.getBoolean(EXTRA_DISABLE_WINDOW, false);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500367 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400368
Jeff Hamilton64144e42010-08-18 16:31:59 -0500369 @Override
Jeff Hamilton64144e42010-08-18 16:31:59 -0500370 public View onCreateView(LayoutInflater inflater, ViewGroup container,
371 Bundle savedInstanceState) {
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500372 Context context = getActivity();
373
Jeff Hamilton64144e42010-08-18 16:31:59 -0500374 View root = inflater.inflate(R.layout.bookmarks, container, false);
375 mEmptyView = root.findViewById(android.R.id.empty);
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400376
Jeff Hamilton64144e42010-08-18 16:31:59 -0500377 mGrid = (GridView) root.findViewById(R.id.grid);
Jeff Hamilton84029622010-08-05 14:29:28 -0500378 mGrid.setOnItemClickListener(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 mGrid.setColumnWidth(Controller.getDesiredThumbnailWidth(getActivity()));
Jeff Hamilton84029622010-08-05 14:29:28 -0500380 if (!mCreateShortcut) {
381 mGrid.setOnCreateContextMenuListener(this);
Ben Murdoch328ea872009-09-16 13:33:29 +0100382 }
John Reck8af90642010-11-23 10:27:29 -0800383 mList = (ListView) root.findViewById(R.id.list);
384 mList.setOnItemClickListener(this);
385 if (!mCreateShortcut) {
386 mList.setOnCreateContextMenuListener(this);
387 registerForContextMenu(mList);
388 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500389
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500390 // Start the loaders
391 LoaderManager lm = getLoaderManager();
392 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
John Reck8af90642010-11-23 10:27:29 -0800393 mCurrentView =
394 prefs.getInt(PREF_SELECTED_VIEW, BrowserBookmarksPage.VIEW_THUMBNAILS);
395 mAdapter = new BrowserBookmarksAdapter(getActivity(), mCurrentView);
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500396 String accountType = prefs.getString(PREF_ACCOUNT_TYPE, DEFAULT_ACCOUNT);
397 String accountName = prefs.getString(PREF_ACCOUNT_NAME, DEFAULT_ACCOUNT);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500398 if (!TextUtils.isEmpty(accountType) && !TextUtils.isEmpty(accountName)) {
399 // There is an account set, load up that one
400 Bundle args = null;
401 if (!DEFAULT_ACCOUNT.equals(accountType) && !DEFAULT_ACCOUNT.equals(accountName)) {
402 args = new Bundle();
403 args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
404 args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
405 }
Jeff Hamilton70c77692010-09-21 19:11:24 -0500406 lm.restartLoader(LOADER_BOOKMARKS, args, this);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500407 } else {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500408 // No account set, load the account list first
Jeff Hamilton70c77692010-09-21 19:11:24 -0500409 lm.restartLoader(LOADER_ACCOUNTS_THEN_BOOKMARKS, null, this);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500410 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500411
412 // Add our own listener in case there are favicons that have yet to be loaded.
Michael Kolbe421c242010-10-04 19:29:01 -0700413 CombinedBookmarkHistoryView.getIconListenerSet().addListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800414
Jeff Hamilton64144e42010-08-18 16:31:59 -0500415 return root;
416 }
Michael Kolb801ecb72010-08-25 12:57:38 -0700417
Nicolas Catania095292f2010-03-15 09:00:14 -0700418 @Override
Jeff Hamilton84029622010-08-05 14:29:28 -0500419 public void onReceivedIcon(String url, Bitmap icon) {
420 // A new favicon has been loaded, so let anything attached to the adapter know about it
421 // so new icons will be loaded.
422 mAdapter.notifyDataSetChanged();
Nicolas Catania095292f2010-03-15 09:00:14 -0700423 }
424
Jeff Hamilton84029622010-08-05 14:29:28 -0500425 @Override
426 public void onItemClick(AdapterView parent, View v, int position, long id) {
427 // It is possible that the view has been canceled when we get to
428 // this point as back has a higher priority
429 if (mCanceled) {
430 android.util.Log.e(LOGTAG, "item clicked when dismissing");
431 return;
432 }
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500433
Jeff Hamilton84029622010-08-05 14:29:28 -0500434 if (mCreateShortcut) {
Michael Kolb801ecb72010-08-25 12:57:38 -0700435 Intent intent = createShortcutIntent(position);
436 // the activity handles the intent in startActivityFromFragment
437 startActivity(intent);
Ben Murdoch328ea872009-09-16 13:33:29 +0100438 return;
439 }
440
Jeff Hamilton84029622010-08-05 14:29:28 -0500441 Cursor cursor = (Cursor) mAdapter.getItem(position);
442 boolean isFolder = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
443 if (!isFolder) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500444 mCallbacks.onUrlSelected(getUrl(position), false);
Leon Scroggins892df312009-07-14 14:48:02 -0400445 } else {
Michael Kolb370a4f32010-10-06 10:45:32 -0700446 String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
Jeff Hamilton84029622010-08-05 14:29:28 -0500447 LoaderManager manager = getLoaderManager();
Michael Kolb801ecb72010-08-25 12:57:38 -0700448 BookmarksLoader loader =
Jeff Hamilton84029622010-08-05 14:29:28 -0500449 (BookmarksLoader) ((Loader) manager.getLoader(LOADER_BOOKMARKS));
Jeff Hamilton84029622010-08-05 14:29:28 -0500450 Uri uri = ContentUris.withAppendedId(
451 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, id);
Michael Kolb370a4f32010-10-06 10:45:32 -0700452 if (mCrumbs != null) {
453 // update crumbs
454 mCrumbs.pushView(title, uri);
455 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500456 loader.setUri(uri);
457 loader.forceLoad();
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400458 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800459 }
460
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500461 @Override
462 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
463 Adapter adapter = parent.getAdapter();
464 String accountType = "com.google";
465 String accountName = adapter.getItem(position).toString();
466
Jeff Hamilton70c77692010-09-21 19:11:24 -0500467 Bundle args = null;
468 if (ACCOUNT_NAME_UNSYNCED.equals(accountName)) {
469 accountType = DEFAULT_ACCOUNT;
470 accountName = DEFAULT_ACCOUNT;
471 } else {
472 args = new Bundle();
473 args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
474 args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
475 }
476
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500477 // Remember the selection for later
478 PreferenceManager.getDefaultSharedPreferences(getActivity()).edit()
479 .putString(PREF_ACCOUNT_TYPE, accountType)
480 .putString(PREF_ACCOUNT_NAME, accountName)
481 .apply();
482
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500483 getLoaderManager().restartLoader(LOADER_BOOKMARKS, args, this);
484 }
485
486 @Override
487 public void onNothingSelected(AdapterView<?> parent) {
488 // Do nothing
489 }
490
Patrick Scott3918d442009-08-04 13:22:29 -0400491 private Intent createShortcutIntent(int position) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500492 Cursor cursor = (Cursor) mAdapter.getItem(position);
493 String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
Michael Kolbccaa8962010-10-20 17:03:10 -0700494 String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
Jeff Hamilton84029622010-08-05 14:29:28 -0500495 Bitmap touchIcon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_TOUCH_ICON);
496 Bitmap favicon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500497 return BookmarkUtils.createAddToHomeIntent(getActivity(), url, title, touchIcon, favicon);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 }
499
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 private void loadUrl(int position) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500501 mCallbacks.onUrlSelected(getUrl(position), false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 }
503
The Android Open Source Project0c908882009-03-03 19:32:16 -0800504 private void openInNewWindow(int position) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500505 mCallbacks.onUrlSelected(getUrl(position), true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800506 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700507
The Android Open Source Project0c908882009-03-03 19:32:16 -0800508 private void editBookmark(int position) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500509 Intent intent = new Intent(getActivity(), AddBookmarkPage.class);
Jeff Hamilton84029622010-08-05 14:29:28 -0500510 Cursor cursor = (Cursor) mAdapter.getItem(position);
511 Bundle item = new Bundle();
Michael Kolbbef7a752010-08-25 17:11:35 -0700512 item.putString(BrowserContract.Bookmarks.TITLE,
Jeff Hamilton84029622010-08-05 14:29:28 -0500513 cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
Michael Kolbbef7a752010-08-25 17:11:35 -0700514 item.putString(BrowserContract.Bookmarks.URL,
Jeff Hamilton84029622010-08-05 14:29:28 -0500515 cursor.getString(BookmarksLoader.COLUMN_INDEX_URL));
516 byte[] data = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON);
517 if (data != null) {
Michael Kolbbef7a752010-08-25 17:11:35 -0700518 item.putParcelable(BrowserContract.Bookmarks.FAVICON,
Jeff Hamilton84029622010-08-05 14:29:28 -0500519 BitmapFactory.decodeByteArray(data, 0, data.length));
520 }
521 item.putInt("id", cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400522 item.putLong(BrowserContract.Bookmarks.PARENT,
523 cursor.getLong(BookmarksLoader.COLUMN_INDEX_PARENT));
Jeff Hamilton84029622010-08-05 14:29:28 -0500524 intent.putExtra("bookmark", item);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 startActivityForResult(intent, BOOKMARKS_SAVE);
526 }
527
528 @Override
Jeff Hamilton64144e42010-08-18 16:31:59 -0500529 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 switch(requestCode) {
531 case BOOKMARKS_SAVE:
Jeff Hamilton64144e42010-08-18 16:31:59 -0500532 if (resultCode == Activity.RESULT_OK) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800533 Bundle extras;
534 if (data != null && (extras = data.getExtras()) != null) {
535 // If there are extras, then we need to save
536 // the edited bookmark. This is done in updateRow()
Leon Scrogginsbc922852010-10-22 12:15:27 -0400537 String title = extras.getString(BrowserContract.Bookmarks.TITLE);
538 String url = extras.getString(BrowserContract.Bookmarks.URL);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800539 if (title != null && url != null) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500540 updateRow(extras);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800541 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800542 }
543 }
544 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800545 }
546 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700547
Jeff Hamilton84029622010-08-05 14:29:28 -0500548 /**
Michael Kolb801ecb72010-08-25 12:57:38 -0700549 * Update a row in the database with new information.
Jeff Hamilton84029622010-08-05 14:29:28 -0500550 * @param map Bundle storing id, title and url of new information
551 */
552 public void updateRow(Bundle map) {
553
554 // Find the record
Michael Kolbbef7a752010-08-25 17:11:35 -0700555 int id = map.getInt("id");
Jeff Hamilton84029622010-08-05 14:29:28 -0500556 int position = -1;
557 Cursor cursor = mAdapter.getCursor();
558 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
559 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID) == id) {
560 position = cursor.getPosition();
561 break;
562 }
563 }
564 if (position < 0) {
565 return;
566 }
567
568 cursor.moveToPosition(position);
569 ContentValues values = new ContentValues();
Michael Kolbbef7a752010-08-25 17:11:35 -0700570 String title = map.getString(BrowserContract.Bookmarks.TITLE);
571 if (!title.equals(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE))) {
572 values.put(BrowserContract.Bookmarks.TITLE, title);
Jeff Hamilton84029622010-08-05 14:29:28 -0500573 }
Michael Kolbbef7a752010-08-25 17:11:35 -0700574 String url = map.getString(BrowserContract.Bookmarks.URL);
575 if (!url.equals(cursor.getString(BookmarksLoader.COLUMN_INDEX_URL))) {
576 values.put(BrowserContract.Bookmarks.URL, url);
Jeff Hamilton84029622010-08-05 14:29:28 -0500577 }
578
Leon Scrogginsbc922852010-10-22 12:15:27 -0400579 if (map.getBoolean(AddBookmarkPage.REMOVE_THUMBNAIL)) {
Michael Kolbbef7a752010-08-25 17:11:35 -0700580 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
Jeff Hamilton84029622010-08-05 14:29:28 -0500581 }
582
583 if (values.size() > 0) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500584 getActivity().getContentResolver().update(
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500585 ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id),
586 values, null, null);
Jeff Hamilton84029622010-08-05 14:29:28 -0500587 }
588 }
589
590 private void displayRemoveBookmarkDialog(final int position) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800591 // Put up a dialog asking if the user really wants to
592 // delete the bookmark
Jeff Hamilton84029622010-08-05 14:29:28 -0500593 Cursor cursor = (Cursor) mAdapter.getItem(position);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500594 Context context = getActivity();
Jeff Hamilton1a805652010-09-07 12:36:30 -0700595 final ContentResolver resolver = context.getContentResolver();
596 final Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI,
597 cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID));
598
Jeff Hamilton64144e42010-08-18 16:31:59 -0500599 new AlertDialog.Builder(context)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800600 .setTitle(R.string.delete_bookmark)
601 .setIcon(android.R.drawable.ic_dialog_alert)
Jeff Hamilton1a805652010-09-07 12:36:30 -0700602 .setMessage(context.getString(R.string.delete_bookmark_warning,
603 cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)))
Nicolas Catania095292f2010-03-15 09:00:14 -0700604 .setPositiveButton(R.string.ok,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800605 new DialogInterface.OnClickListener() {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500606 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800607 public void onClick(DialogInterface dialog, int whichButton) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700608 resolver.delete(uri, null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800609 }
610 })
611 .setNegativeButton(R.string.cancel, null)
612 .show();
613 }
614
Jeff Hamilton84029622010-08-05 14:29:28 -0500615 private String getUrl(int position) {
616 Cursor cursor = (Cursor) mAdapter.getItem(position);
617 return cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 }
Michael Kolb801ecb72010-08-25 12:57:38 -0700619
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620 private void copy(CharSequence text) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500621 ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(
622 Context.CLIPBOARD_SERVICE);
Dianne Hackborneb07b172010-08-26 22:18:14 -0700623 cm.setPrimaryClip(ClipData.newRawUri(null, null, Uri.parse(text.toString())));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800624 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700625
John Reck8af90642010-11-23 10:27:29 -0800626 void selectView(int view) {
627 if (view == mCurrentView) {
628 return;
629 }
630 mCurrentView = view;
631 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
632 Editor edit = prefs.edit();
633 edit.putInt(PREF_SELECTED_VIEW, mCurrentView);
634 edit.apply();
635 if (mEmptyView.getVisibility() == View.VISIBLE) {
636 return;
637 }
638 setupBookmarkView();
639 }
640
641 private void setupBookmarkView() {
642 mAdapter.selectView(mCurrentView);
643 switch (mCurrentView) {
644 case VIEW_THUMBNAILS:
645 mList.setAdapter(null);
646 mGrid.setAdapter(mAdapter);
647 mGrid.setVisibility(View.VISIBLE);
648 mList.setVisibility(View.GONE);
649 break;
650 case VIEW_LIST:
651 mGrid.setAdapter(null);
652 mList.setAdapter(mAdapter);
653 mGrid.setVisibility(View.GONE);
654 mList.setVisibility(View.VISIBLE);
655 break;
656 }
657 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800658}