blob: 4630d4e854b2e86903d79d9f8f29286af8ebbc3f [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;
Jeff Hamilton84029622010-08-05 14:29:28 -050034import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.graphics.Bitmap;
Jeff Hamilton84029622010-08-05 14:29:28 -050036import android.graphics.BitmapFactory;
37import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.os.Bundle;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050039import android.preference.PreferenceManager;
Jeff Hamilton69bd7072010-08-17 12:38:22 -050040import android.provider.BrowserContract;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050041import android.provider.BrowserContract.Accounts;
42import android.text.TextUtils;
Jeff Hamilton84029622010-08-05 14:29:28 -050043import android.util.Pair;
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 Hamilton1a805652010-09-07 12:36:30 -070050import android.view.View.OnClickListener;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050051import android.view.ViewGroup;
Patrick Scottc1cf63a2010-03-09 16:02:08 -050052import android.webkit.WebIconDatabase.IconListener;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050053import android.widget.Adapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054import android.widget.AdapterView;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050055import android.widget.AdapterView.OnItemClickListener;
56import android.widget.AdapterView.OnItemSelectedListener;
Jeff Hamilton84029622010-08-05 14:29:28 -050057import android.widget.Button;
58import android.widget.GridView;
59import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080060
Jeff Hamilton84029622010-08-05 14:29:28 -050061import java.util.Stack;
62
The Android Open Source Project0c908882009-03-03 19:32:16 -080063/**
64 * View showing the user's bookmarks in the browser.
65 */
Jeff Hamilton64144e42010-08-18 16:31:59 -050066public class BrowserBookmarksPage extends Fragment implements View.OnCreateContextMenuListener,
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050067 LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener, IconListener, OnClickListener,
68 OnItemSelectedListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080069
Jeff Hamilton84029622010-08-05 14:29:28 -050070 static final int BOOKMARKS_SAVE = 1;
71 static final String LOGTAG = "browser";
Nicolas Catania095292f2010-03-15 09:00:14 -070072
Jeff Hamilton84029622010-08-05 14:29:28 -050073 static final int LOADER_BOOKMARKS = 1;
Jeff Hamiltona9bad832010-09-24 11:05:30 -050074 static final int LOADER_ACCOUNTS_THEN_BOOKMARKS = 2;
Jeff Hamilton84029622010-08-05 14:29:28 -050075
Michael Kolb801ecb72010-08-25 12:57:38 -070076 static final String EXTRA_SHORTCUT = "create_shortcut";
77 static final String EXTRA_DISABLE_WINDOW = "disable_new_window";
78
Jeff Hamilton70c77692010-09-21 19:11:24 -050079 static final String ACCOUNT_NAME_UNSYNCED = "Unsynced";
80
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050081 public static final String PREF_ACCOUNT_TYPE = "acct_type";
82 public static final String PREF_ACCOUNT_NAME = "acct_name";
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050083
84 static final String DEFAULT_ACCOUNT = "local";
85
Jeff Hamilton64144e42010-08-18 16:31:59 -050086 BookmarksHistoryCallbacks mCallbacks;
Jeff Hamilton84029622010-08-05 14:29:28 -050087 GridView mGrid;
88 BrowserBookmarksAdapter mAdapter;
89 boolean mDisableNewWindow;
90 BookmarkItem mContextHeader;
91 boolean mCanceled = false;
92 boolean mCreateShortcut;
93 View mEmptyView;
94 View mContentView;
95 Stack<Pair<String, Uri>> mFolderStack = new Stack<Pair<String, Uri>>();
96 Button mUpButton;
97
98 @Override
99 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
100 switch (id) {
101 case LOADER_BOOKMARKS: {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700102 String accountType = null;
103 String accountName = null;
Jeff Hamilton84029622010-08-05 14:29:28 -0500104 if (args != null) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700105 accountType = args.getString(BookmarksLoader.ARG_ACCOUNT_TYPE);
106 accountName = args.getString(BookmarksLoader.ARG_ACCOUNT_NAME);
Jeff Hamilton84029622010-08-05 14:29:28 -0500107 }
Jeff Hamilton1a805652010-09-07 12:36:30 -0700108 return new BookmarksLoader(getActivity(), accountType, accountName);
Jeff Hamilton84029622010-08-05 14:29:28 -0500109 }
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500110
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500111 case LOADER_ACCOUNTS_THEN_BOOKMARKS: {
112 return new CursorLoader(getActivity(), Accounts.CONTENT_URI,
113 new String[] { Accounts.ACCOUNT_TYPE, Accounts.ACCOUNT_NAME }, null, null,
114 null);
115 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500116 }
117 throw new UnsupportedOperationException("Unknown loader id " + id);
118 }
119
120 @Override
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500121 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
122 switch (loader.getId()) {
123 case LOADER_BOOKMARKS: {
124 // Set the visibility of the empty vs. content views
125 if (cursor == null || cursor.getCount() == 0) {
126 mEmptyView.setVisibility(View.VISIBLE);
127 mGrid.setVisibility(View.GONE);
128 } else {
129 mEmptyView.setVisibility(View.GONE);
130 mGrid.setVisibility(View.VISIBLE);
131 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500132
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500133 // Fill in the "up" button if needed
134 BookmarksLoader bl = (BookmarksLoader) loader;
135 String path = bl.getUri().getPath();
136 boolean rootFolder =
Jeff Hamilton70c77692010-09-21 19:11:24 -0500137 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER.getPath().equals(path);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500138 if (rootFolder) {
139 mUpButton.setText(R.string.defaultBookmarksUpButton);
140 mUpButton.setEnabled(false);
141 } else {
142 mUpButton.setText(mFolderStack.peek().first);
143 mUpButton.setEnabled(true);
144 }
145 mUpButton.setVisibility(View.VISIBLE);
Jeff Hamilton84029622010-08-05 14:29:28 -0500146
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500147 // Give the new data to the adapter
148 mAdapter.changeCursor(cursor);
149
150 break;
151 }
152
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500153 case LOADER_ACCOUNTS_THEN_BOOKMARKS: {
154 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
155 getActivity());
156 String storedAccountType = prefs.getString(PREF_ACCOUNT_TYPE, null);
157 String storedAccountName = prefs.getString(PREF_ACCOUNT_NAME, null);
158 String accountType =
159 TextUtils.isEmpty(storedAccountType) ? DEFAULT_ACCOUNT : storedAccountType;
160 String accountName =
161 TextUtils.isEmpty(storedAccountName) ? DEFAULT_ACCOUNT : storedAccountName;
162
163 Bundle args = null;
164 if (cursor == null || !cursor.moveToFirst()) {
165 // No accounts, set the prefs to the default
166 accountType = DEFAULT_ACCOUNT;
167 accountName = DEFAULT_ACCOUNT;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500168 } else {
169 int accountPosition = -1;
170
171 if (!DEFAULT_ACCOUNT.equals(accountType) &&
172 !DEFAULT_ACCOUNT.equals(accountName)) {
173 // Check to see if the account in prefs still exists
174 cursor.moveToFirst();
175 do {
176 if (accountType.equals(cursor.getString(0))
177 && accountName.equals(cursor.getString(1))) {
178 accountPosition = cursor.getPosition();
179 break;
180 }
181 } while (cursor.moveToNext());
182 }
183
184 if (accountPosition == -1) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500185 if (!(DEFAULT_ACCOUNT.equals(accountType)
Jeff Hamilton70c77692010-09-21 19:11:24 -0500186 && DEFAULT_ACCOUNT.equals(accountName))) {
Jeff Hamilton70c77692010-09-21 19:11:24 -0500187 // No account is set in prefs and there is at least one,
188 // so pick the first one as the default
189 cursor.moveToFirst();
190 accountType = cursor.getString(0);
191 accountName = cursor.getString(1);
Jeff Hamilton70c77692010-09-21 19:11:24 -0500192 }
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500193 }
194
195 args = new Bundle();
196 args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
197 args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500198 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500199
200 // The stored account name wasn't found, update the stored account with a valid one
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500201 if (!accountType.equals(storedAccountType)
202 || !accountName.equals(storedAccountName)) {
203 prefs.edit()
204 .putString(PREF_ACCOUNT_TYPE, accountType)
205 .putString(PREF_ACCOUNT_NAME, accountName)
206 .apply();
207 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500208 getLoaderManager().initLoader(LOADER_BOOKMARKS, args, this);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500209
210 break;
211 }
212 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500213 }
214
215 @Override
216 public void onClick(View view) {
217 if (view == mUpButton) {
218 Pair<String, Uri> pair = mFolderStack.pop();
Michael Kolb801ecb72010-08-25 12:57:38 -0700219 BookmarksLoader loader =
Jeff Hamilton84029622010-08-05 14:29:28 -0500220 (BookmarksLoader) ((Loader) getLoaderManager().getLoader(LOADER_BOOKMARKS));
221 loader.setUri(pair.second);
222 loader.forceLoad();
223 }
224 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800225
226 @Override
227 public boolean onContextItemSelected(MenuItem item) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500228 final Activity activity = getActivity();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 // It is possible that the view has been canceled when we get to
Nicolas Catania095292f2010-03-15 09:00:14 -0700230 // this point as back has a higher priority
The Android Open Source Project0c908882009-03-03 19:32:16 -0800231 if (mCanceled) {
232 return true;
233 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700234 AdapterView.AdapterContextMenuInfo i =
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235 (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
236 // If we have no menu info, we can't tell which item was selected.
237 if (i == null) {
238 return true;
239 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700240
The Android Open Source Project0c908882009-03-03 19:32:16 -0800241 switch (item.getItemId()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242 case R.id.open_context_menu_id:
243 loadUrl(i.position);
244 break;
245 case R.id.edit_context_menu_id:
246 editBookmark(i.position);
247 break;
248 case R.id.shortcut_context_menu_id:
Jeff Hamilton64144e42010-08-18 16:31:59 -0500249 activity.sendBroadcast(createShortcutIntent(i.position));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800250 break;
251 case R.id.delete_context_menu_id:
Jeff Hamilton84029622010-08-05 14:29:28 -0500252 displayRemoveBookmarkDialog(i.position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 break;
254 case R.id.new_window_context_menu_id:
255 openInNewWindow(i.position);
256 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500257 case R.id.share_link_context_menu_id: {
258 Cursor cursor = (Cursor) mAdapter.getItem(i.position);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500259 BrowserActivity.sharePage(activity,
Jeff Hamilton84029622010-08-05 14:29:28 -0500260 cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE),
261 cursor.getString(BookmarksLoader.COLUMN_INDEX_URL),
262 getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON),
263 getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_THUMBNAIL));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800264 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500265 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800266 case R.id.copy_url_context_menu_id:
267 copy(getUrl(i.position));
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400268 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500269 case R.id.homepage_context_menu_id: {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500270 BrowserSettings.getInstance().setHomePage(activity, getUrl(i.position));
271 Toast.makeText(activity, R.string.homepage_set, Toast.LENGTH_LONG).show();
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400272 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500273 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400274 // Only for the Most visited page
Jeff Hamilton84029622010-08-05 14:29:28 -0500275 case R.id.save_to_bookmarks_menu_id: {
276 Cursor cursor = (Cursor) mAdapter.getItem(i.position);
277 String name = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
278 String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400279 // If the site is bookmarked, the item becomes remove from
280 // bookmarks.
Jeff Hamilton64144e42010-08-18 16:31:59 -0500281 Bookmarks.removeFromBookmarks(activity, activity.getContentResolver(), url, name);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400282 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500283 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800284 default:
285 return super.onContextItemSelected(item);
286 }
287 return true;
288 }
289
Jeff Hamilton84029622010-08-05 14:29:28 -0500290 Bitmap getBitmap(Cursor cursor, int columnIndex) {
291 byte[] data = cursor.getBlob(columnIndex);
292 if (data == null) {
293 return null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800294 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500295 return BitmapFactory.decodeByteArray(data, 0, data.length);
296 }
297
298 @Override
299 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
300 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Leon Scroggins5e8a40f2010-09-27 17:13:35 -0400301 Cursor cursor = (Cursor) mAdapter.getItem(info.position);
302 boolean isFolder
303 = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
304 if (isFolder) return;
Michael Kolb801ecb72010-08-25 12:57:38 -0700305
Jeff Hamilton64144e42010-08-18 16:31:59 -0500306 final Activity activity = getActivity();
307 MenuInflater inflater = activity.getMenuInflater();
Jeff Hamilton84029622010-08-05 14:29:28 -0500308 inflater.inflate(R.menu.bookmarkscontext, menu);
309
310 if (mDisableNewWindow) {
311 menu.findItem(R.id.new_window_context_menu_id).setVisible(false);
312 }
313
314 if (mContextHeader == null) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500315 mContextHeader = new BookmarkItem(activity);
Jeff Hamilton84029622010-08-05 14:29:28 -0500316 } else if (mContextHeader.getParent() != null) {
317 ((ViewGroup) mContextHeader.getParent()).removeView(mContextHeader);
318 }
319
Leon Scroggins5e8a40f2010-09-27 17:13:35 -0400320 populateBookmarkItem(cursor, mContextHeader);
Jeff Hamilton84029622010-08-05 14:29:28 -0500321
322 menu.setHeaderView(mContextHeader);
323 }
324
Leon Scroggins5e8a40f2010-09-27 17:13:35 -0400325 private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500326 String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
327 item.setUrl(url);
328 item.setName(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
329 Bitmap bitmap = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON);
330 if (bitmap == null) {
331 bitmap = CombinedBookmarkHistoryActivity.getIconListenerSet().getFavicon(url);
332 }
333 item.setFavicon(bitmap);
334 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800335
336 /**
337 * Create a new BrowserBookmarksPage.
Nicolas Catania095292f2010-03-15 09:00:14 -0700338 */
The Android Open Source Project0c908882009-03-03 19:32:16 -0800339 @Override
Jeff Hamilton64144e42010-08-18 16:31:59 -0500340 public void onCreate(Bundle icicle) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800341 super.onCreate(icicle);
342
Jeff Hamilton64144e42010-08-18 16:31:59 -0500343 Bundle args = getArguments();
344 mCreateShortcut = args == null ? false : args.getBoolean("create_shortcut", false);
345 mDisableNewWindow = args == null ? false : args.getBoolean("disable_new_window", false);
346 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400347
Jeff Hamilton64144e42010-08-18 16:31:59 -0500348 @Override
349 public void onAttach(Activity activity) {
350 super.onAttach(activity);
351 mCallbacks = (BookmarksHistoryCallbacks) activity;
352 }
Michael Kolb801ecb72010-08-25 12:57:38 -0700353
Jeff Hamilton64144e42010-08-18 16:31:59 -0500354 @Override
355 public View onCreateView(LayoutInflater inflater, ViewGroup container,
356 Bundle savedInstanceState) {
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500357 Context context = getActivity();
358
Jeff Hamilton64144e42010-08-18 16:31:59 -0500359 View root = inflater.inflate(R.layout.bookmarks, container, false);
360 mEmptyView = root.findViewById(android.R.id.empty);
361 mContentView = root.findViewById(android.R.id.content);
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400362
Jeff Hamilton64144e42010-08-18 16:31:59 -0500363 mGrid = (GridView) root.findViewById(R.id.grid);
Jeff Hamilton84029622010-08-05 14:29:28 -0500364 mGrid.setOnItemClickListener(this);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500365 mGrid.setColumnWidth(BrowserActivity.getDesiredThumbnailWidth(getActivity()));
Jeff Hamilton84029622010-08-05 14:29:28 -0500366 if (!mCreateShortcut) {
367 mGrid.setOnCreateContextMenuListener(this);
Ben Murdoch328ea872009-09-16 13:33:29 +0100368 }
Patrick Scottf49ecd62010-04-09 12:43:26 -0400369
Jeff Hamilton64144e42010-08-18 16:31:59 -0500370 mUpButton = (Button) root.findViewById(R.id.up);
Jeff Hamilton84029622010-08-05 14:29:28 -0500371 mUpButton.setEnabled(false);
372 mUpButton.setOnClickListener(this);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500373 mUpButton.setVisibility(View.GONE);
Jeff Hamilton84029622010-08-05 14:29:28 -0500374
Jeff Hamilton64144e42010-08-18 16:31:59 -0500375 mAdapter = new BrowserBookmarksAdapter(getActivity());
Jeff Hamilton84029622010-08-05 14:29:28 -0500376 mGrid.setAdapter(mAdapter);
377
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500378 // Start the loaders
379 LoaderManager lm = getLoaderManager();
380 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500381 String accountType = prefs.getString(PREF_ACCOUNT_TYPE, DEFAULT_ACCOUNT);
382 String accountName = prefs.getString(PREF_ACCOUNT_NAME, DEFAULT_ACCOUNT);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500383 if (!TextUtils.isEmpty(accountType) && !TextUtils.isEmpty(accountName)) {
384 // There is an account set, load up that one
385 Bundle args = null;
386 if (!DEFAULT_ACCOUNT.equals(accountType) && !DEFAULT_ACCOUNT.equals(accountName)) {
387 args = new Bundle();
388 args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
389 args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
390 }
Jeff Hamilton70c77692010-09-21 19:11:24 -0500391 lm.restartLoader(LOADER_BOOKMARKS, args, this);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500392 } else {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500393 // No account set, load the account list first
Jeff Hamilton70c77692010-09-21 19:11:24 -0500394 lm.restartLoader(LOADER_ACCOUNTS_THEN_BOOKMARKS, null, this);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500395 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500396
397 // Add our own listener in case there are favicons that have yet to be loaded.
398 CombinedBookmarkHistoryActivity.getIconListenerSet().addListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800399
Jeff Hamilton64144e42010-08-18 16:31:59 -0500400 return root;
401 }
Michael Kolb801ecb72010-08-25 12:57:38 -0700402
Nicolas Catania095292f2010-03-15 09:00:14 -0700403 @Override
Jeff Hamilton84029622010-08-05 14:29:28 -0500404 public void onReceivedIcon(String url, Bitmap icon) {
405 // A new favicon has been loaded, so let anything attached to the adapter know about it
406 // so new icons will be loaded.
407 mAdapter.notifyDataSetChanged();
Nicolas Catania095292f2010-03-15 09:00:14 -0700408 }
409
Jeff Hamilton84029622010-08-05 14:29:28 -0500410 @Override
411 public void onItemClick(AdapterView parent, View v, int position, long id) {
412 // It is possible that the view has been canceled when we get to
413 // this point as back has a higher priority
414 if (mCanceled) {
415 android.util.Log.e(LOGTAG, "item clicked when dismissing");
416 return;
417 }
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500418
Jeff Hamilton84029622010-08-05 14:29:28 -0500419 if (mCreateShortcut) {
Michael Kolb801ecb72010-08-25 12:57:38 -0700420 Intent intent = createShortcutIntent(position);
421 // the activity handles the intent in startActivityFromFragment
422 startActivity(intent);
Ben Murdoch328ea872009-09-16 13:33:29 +0100423 return;
424 }
425
Jeff Hamilton84029622010-08-05 14:29:28 -0500426 Cursor cursor = (Cursor) mAdapter.getItem(position);
427 boolean isFolder = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
428 if (!isFolder) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500429 mCallbacks.onUrlSelected(getUrl(position), false);
Leon Scroggins892df312009-07-14 14:48:02 -0400430 } else {
Jeff Hamilton84029622010-08-05 14:29:28 -0500431 String title;
432 if (mFolderStack.size() != 0) {
433 title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
434 } else {
435 // TODO localize
Michael Kolb801ecb72010-08-25 12:57:38 -0700436 title = "Bookmarks";
Leon Scroggins892df312009-07-14 14:48:02 -0400437 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500438 LoaderManager manager = getLoaderManager();
Michael Kolb801ecb72010-08-25 12:57:38 -0700439 BookmarksLoader loader =
Jeff Hamilton84029622010-08-05 14:29:28 -0500440 (BookmarksLoader) ((Loader) manager.getLoader(LOADER_BOOKMARKS));
441 mFolderStack.push(new Pair(title, loader.getUri()));
442 Uri uri = ContentUris.withAppendedId(
443 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, id);
444 loader.setUri(uri);
445 loader.forceLoad();
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400446 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800447 }
448
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500449 @Override
450 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
451 Adapter adapter = parent.getAdapter();
452 String accountType = "com.google";
453 String accountName = adapter.getItem(position).toString();
454
Jeff Hamilton70c77692010-09-21 19:11:24 -0500455 Bundle args = null;
456 if (ACCOUNT_NAME_UNSYNCED.equals(accountName)) {
457 accountType = DEFAULT_ACCOUNT;
458 accountName = DEFAULT_ACCOUNT;
459 } else {
460 args = new Bundle();
461 args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
462 args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
463 }
464
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500465 // Remember the selection for later
466 PreferenceManager.getDefaultSharedPreferences(getActivity()).edit()
467 .putString(PREF_ACCOUNT_TYPE, accountType)
468 .putString(PREF_ACCOUNT_NAME, accountName)
469 .apply();
470
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500471 getLoaderManager().restartLoader(LOADER_BOOKMARKS, args, this);
472 }
473
474 @Override
475 public void onNothingSelected(AdapterView<?> parent) {
476 // Do nothing
477 }
478
Patrick Scott3918d442009-08-04 13:22:29 -0400479 private Intent createShortcutIntent(int position) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500480 Cursor cursor = (Cursor) mAdapter.getItem(position);
481 String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
482 String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
483 Bitmap touchIcon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_TOUCH_ICON);
484 Bitmap favicon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500485 return BookmarkUtils.createAddToHomeIntent(getActivity(), url, title, touchIcon, favicon);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800486 }
487
The Android Open Source Project0c908882009-03-03 19:32:16 -0800488 private void loadUrl(int position) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500489 mCallbacks.onUrlSelected(getUrl(position), false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800490 }
491
The Android Open Source Project0c908882009-03-03 19:32:16 -0800492 private void openInNewWindow(int position) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500493 mCallbacks.onUrlSelected(getUrl(position), true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800494 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700495
The Android Open Source Project0c908882009-03-03 19:32:16 -0800496 private void editBookmark(int position) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500497 Intent intent = new Intent(getActivity(), AddBookmarkPage.class);
Jeff Hamilton84029622010-08-05 14:29:28 -0500498 Cursor cursor = (Cursor) mAdapter.getItem(position);
499 Bundle item = new Bundle();
Michael Kolbbef7a752010-08-25 17:11:35 -0700500 item.putString(BrowserContract.Bookmarks.TITLE,
Jeff Hamilton84029622010-08-05 14:29:28 -0500501 cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
Michael Kolbbef7a752010-08-25 17:11:35 -0700502 item.putString(BrowserContract.Bookmarks.URL,
Jeff Hamilton84029622010-08-05 14:29:28 -0500503 cursor.getString(BookmarksLoader.COLUMN_INDEX_URL));
504 byte[] data = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON);
505 if (data != null) {
Michael Kolbbef7a752010-08-25 17:11:35 -0700506 item.putParcelable(BrowserContract.Bookmarks.FAVICON,
Jeff Hamilton84029622010-08-05 14:29:28 -0500507 BitmapFactory.decodeByteArray(data, 0, data.length));
508 }
509 item.putInt("id", cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400510 item.putLong(BrowserContract.Bookmarks.PARENT,
511 cursor.getLong(BookmarksLoader.COLUMN_INDEX_PARENT));
Jeff Hamilton84029622010-08-05 14:29:28 -0500512 intent.putExtra("bookmark", item);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800513 startActivityForResult(intent, BOOKMARKS_SAVE);
514 }
515
516 @Override
Jeff Hamilton64144e42010-08-18 16:31:59 -0500517 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800518 switch(requestCode) {
519 case BOOKMARKS_SAVE:
Jeff Hamilton64144e42010-08-18 16:31:59 -0500520 if (resultCode == Activity.RESULT_OK) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800521 Bundle extras;
522 if (data != null && (extras = data.getExtras()) != null) {
523 // If there are extras, then we need to save
524 // the edited bookmark. This is done in updateRow()
525 String title = extras.getString("title");
526 String url = extras.getString("url");
527 if (title != null && url != null) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500528 updateRow(extras);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800529 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 }
531 }
532 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800533 }
534 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700535
Jeff Hamilton84029622010-08-05 14:29:28 -0500536 /**
Michael Kolb801ecb72010-08-25 12:57:38 -0700537 * Update a row in the database with new information.
Jeff Hamilton84029622010-08-05 14:29:28 -0500538 * @param map Bundle storing id, title and url of new information
539 */
540 public void updateRow(Bundle map) {
541
542 // Find the record
Michael Kolbbef7a752010-08-25 17:11:35 -0700543 int id = map.getInt("id");
Jeff Hamilton84029622010-08-05 14:29:28 -0500544 int position = -1;
545 Cursor cursor = mAdapter.getCursor();
546 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
547 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID) == id) {
548 position = cursor.getPosition();
549 break;
550 }
551 }
552 if (position < 0) {
553 return;
554 }
555
556 cursor.moveToPosition(position);
557 ContentValues values = new ContentValues();
Michael Kolbbef7a752010-08-25 17:11:35 -0700558 String title = map.getString(BrowserContract.Bookmarks.TITLE);
559 if (!title.equals(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE))) {
560 values.put(BrowserContract.Bookmarks.TITLE, title);
Jeff Hamilton84029622010-08-05 14:29:28 -0500561 }
Michael Kolbbef7a752010-08-25 17:11:35 -0700562 String url = map.getString(BrowserContract.Bookmarks.URL);
563 if (!url.equals(cursor.getString(BookmarksLoader.COLUMN_INDEX_URL))) {
564 values.put(BrowserContract.Bookmarks.URL, url);
Jeff Hamilton84029622010-08-05 14:29:28 -0500565 }
566
567 if (map.getBoolean("invalidateThumbnail") == true) {
Michael Kolbbef7a752010-08-25 17:11:35 -0700568 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
Jeff Hamilton84029622010-08-05 14:29:28 -0500569 }
570
571 if (values.size() > 0) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500572 getActivity().getContentResolver().update(
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500573 ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id),
574 values, null, null);
Jeff Hamilton84029622010-08-05 14:29:28 -0500575 }
576 }
577
578 private void displayRemoveBookmarkDialog(final int position) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 // Put up a dialog asking if the user really wants to
580 // delete the bookmark
Jeff Hamilton84029622010-08-05 14:29:28 -0500581 Cursor cursor = (Cursor) mAdapter.getItem(position);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500582 Context context = getActivity();
Jeff Hamilton1a805652010-09-07 12:36:30 -0700583 final ContentResolver resolver = context.getContentResolver();
584 final Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI,
585 cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID));
586
Jeff Hamilton64144e42010-08-18 16:31:59 -0500587 new AlertDialog.Builder(context)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800588 .setTitle(R.string.delete_bookmark)
589 .setIcon(android.R.drawable.ic_dialog_alert)
Jeff Hamilton1a805652010-09-07 12:36:30 -0700590 .setMessage(context.getString(R.string.delete_bookmark_warning,
591 cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)))
Nicolas Catania095292f2010-03-15 09:00:14 -0700592 .setPositiveButton(R.string.ok,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800593 new DialogInterface.OnClickListener() {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500594 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800595 public void onClick(DialogInterface dialog, int whichButton) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700596 resolver.delete(uri, null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800597 }
598 })
599 .setNegativeButton(R.string.cancel, null)
600 .show();
601 }
602
Jeff Hamilton84029622010-08-05 14:29:28 -0500603 private String getUrl(int position) {
604 Cursor cursor = (Cursor) mAdapter.getItem(position);
605 return cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800606 }
Michael Kolb801ecb72010-08-25 12:57:38 -0700607
The Android Open Source Project0c908882009-03-03 19:32:16 -0800608 private void copy(CharSequence text) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500609 ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(
610 Context.CLIPBOARD_SERVICE);
Dianne Hackborneb07b172010-08-26 22:18:14 -0700611 cm.setPrimaryClip(ClipData.newRawUri(null, null, Uri.parse(text.toString())));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800613}