blob: 4a089fb0bebc5408b426952255ea56330469e109 [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;
57import android.widget.ArrayAdapter;
Jeff Hamilton84029622010-08-05 14:29:28 -050058import android.widget.Button;
59import android.widget.GridView;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050060import android.widget.Spinner;
Jeff Hamilton84029622010-08-05 14:29:28 -050061import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080062
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050063import java.util.ArrayList;
Jeff Hamilton84029622010-08-05 14:29:28 -050064import java.util.Stack;
65
The Android Open Source Project0c908882009-03-03 19:32:16 -080066/**
67 * View showing the user's bookmarks in the browser.
68 */
Jeff Hamilton64144e42010-08-18 16:31:59 -050069public class BrowserBookmarksPage extends Fragment implements View.OnCreateContextMenuListener,
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050070 LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener, IconListener, OnClickListener,
71 OnItemSelectedListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080072
Jeff Hamilton84029622010-08-05 14:29:28 -050073 static final int BOOKMARKS_SAVE = 1;
74 static final String LOGTAG = "browser";
Nicolas Catania095292f2010-03-15 09:00:14 -070075
Jeff Hamilton84029622010-08-05 14:29:28 -050076 static final int LOADER_BOOKMARKS = 1;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050077 static final int LOADER_ACCOUNTS = 2;
78 static final int LOADER_ACCOUNTS_THEN_BOOKMARKS = 3;
Jeff Hamilton84029622010-08-05 14:29:28 -050079
Michael Kolb801ecb72010-08-25 12:57:38 -070080 static final String EXTRA_SHORTCUT = "create_shortcut";
81 static final String EXTRA_DISABLE_WINDOW = "disable_new_window";
82
Jeff Hamilton70c77692010-09-21 19:11:24 -050083 static final String ACCOUNT_NAME_UNSYNCED = "Unsynced";
84
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050085 public static final String PREF_ACCOUNT_TYPE = "acct_type";
86 public static final String PREF_ACCOUNT_NAME = "acct_name";
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050087
88 static final String DEFAULT_ACCOUNT = "local";
89
Jeff Hamilton64144e42010-08-18 16:31:59 -050090 BookmarksHistoryCallbacks mCallbacks;
Jeff Hamilton84029622010-08-05 14:29:28 -050091 GridView mGrid;
Jeff Hamiltondb90aa82010-09-16 03:38:04 -050092 Spinner mAccountSelector;
Jeff Hamilton84029622010-08-05 14:29:28 -050093 BrowserBookmarksAdapter mAdapter;
94 boolean mDisableNewWindow;
95 BookmarkItem mContextHeader;
96 boolean mCanceled = false;
97 boolean mCreateShortcut;
98 View mEmptyView;
99 View mContentView;
100 Stack<Pair<String, Uri>> mFolderStack = new Stack<Pair<String, Uri>>();
101 Button mUpButton;
102
103 @Override
104 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
105 switch (id) {
106 case LOADER_BOOKMARKS: {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700107 String accountType = null;
108 String accountName = null;
Jeff Hamilton84029622010-08-05 14:29:28 -0500109 if (args != null) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700110 accountType = args.getString(BookmarksLoader.ARG_ACCOUNT_TYPE);
111 accountName = args.getString(BookmarksLoader.ARG_ACCOUNT_NAME);
Jeff Hamilton84029622010-08-05 14:29:28 -0500112 }
Jeff Hamilton1a805652010-09-07 12:36:30 -0700113 return new BookmarksLoader(getActivity(), accountType, accountName);
Jeff Hamilton84029622010-08-05 14:29:28 -0500114 }
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500115
116 case LOADER_ACCOUNTS:
117 case LOADER_ACCOUNTS_THEN_BOOKMARKS: {
118 return new CursorLoader(getActivity(), Accounts.CONTENT_URI,
119 new String[] { Accounts.ACCOUNT_TYPE, Accounts.ACCOUNT_NAME }, null, null,
120 null);
121 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500122 }
123 throw new UnsupportedOperationException("Unknown loader id " + id);
124 }
125
126 @Override
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500127 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
128 switch (loader.getId()) {
129 case LOADER_BOOKMARKS: {
130 // Set the visibility of the empty vs. content views
131 if (cursor == null || cursor.getCount() == 0) {
132 mEmptyView.setVisibility(View.VISIBLE);
133 mGrid.setVisibility(View.GONE);
134 } else {
135 mEmptyView.setVisibility(View.GONE);
136 mGrid.setVisibility(View.VISIBLE);
137 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500138
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500139 // Fill in the "up" button if needed
140 BookmarksLoader bl = (BookmarksLoader) loader;
141 String path = bl.getUri().getPath();
142 boolean rootFolder =
Jeff Hamilton70c77692010-09-21 19:11:24 -0500143 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER.getPath().equals(path);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500144 if (rootFolder) {
145 mUpButton.setText(R.string.defaultBookmarksUpButton);
146 mUpButton.setEnabled(false);
147 } else {
148 mUpButton.setText(mFolderStack.peek().first);
149 mUpButton.setEnabled(true);
150 }
151 mUpButton.setVisibility(View.VISIBLE);
Jeff Hamilton84029622010-08-05 14:29:28 -0500152
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500153 // Give the new data to the adapter
154 mAdapter.changeCursor(cursor);
155
156 break;
157 }
158
159 case LOADER_ACCOUNTS:
160 case LOADER_ACCOUNTS_THEN_BOOKMARKS: {
161 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
162 getActivity());
163 String storedAccountType = prefs.getString(PREF_ACCOUNT_TYPE, null);
164 String storedAccountName = prefs.getString(PREF_ACCOUNT_NAME, null);
165 String accountType =
166 TextUtils.isEmpty(storedAccountType) ? DEFAULT_ACCOUNT : storedAccountType;
167 String accountName =
168 TextUtils.isEmpty(storedAccountName) ? DEFAULT_ACCOUNT : storedAccountName;
169
170 Bundle args = null;
171 if (cursor == null || !cursor.moveToFirst()) {
172 // No accounts, set the prefs to the default
173 accountType = DEFAULT_ACCOUNT;
174 accountName = DEFAULT_ACCOUNT;
175 mAccountSelector.setVisibility(View.GONE);
176 } else {
177 int accountPosition = -1;
178
179 if (!DEFAULT_ACCOUNT.equals(accountType) &&
180 !DEFAULT_ACCOUNT.equals(accountName)) {
181 // Check to see if the account in prefs still exists
182 cursor.moveToFirst();
183 do {
184 if (accountType.equals(cursor.getString(0))
185 && accountName.equals(cursor.getString(1))) {
186 accountPosition = cursor.getPosition();
187 break;
188 }
189 } while (cursor.moveToNext());
190 }
191
192 if (accountPosition == -1) {
Jeff Hamilton70c77692010-09-21 19:11:24 -0500193 if ((DEFAULT_ACCOUNT.equals(accountType)
194 && DEFAULT_ACCOUNT.equals(accountName))) {
195 // The "unsynced" account is selected
196 accountPosition = cursor.getCount();
197 } else {
198 // No account is set in prefs and there is at least one,
199 // so pick the first one as the default
200 cursor.moveToFirst();
201 accountType = cursor.getString(0);
202 accountName = cursor.getString(1);
203 accountPosition = 0;
204 }
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500205 }
206
207 args = new Bundle();
208 args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
209 args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
210
Jeff Hamilton70c77692010-09-21 19:11:24 -0500211 // Add in the sync accounts
212 ArrayList<String> accounts = new ArrayList<String>();
213 cursor.moveToFirst();
214 do {
215 accounts.add(cursor.getString(1));
216 } while (cursor.moveToNext());
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500217
Jeff Hamilton70c77692010-09-21 19:11:24 -0500218 // STOPSHIP: Add in the "unsynced" account temporarily until we
219 // have support for migrated unsynced bookmarks into sync accounts.
220 accounts.add(ACCOUNT_NAME_UNSYNCED);
221
222 mAccountSelector.setAdapter(new ArrayAdapter<String>(getActivity(),
223 android.R.layout.simple_list_item_1, android.R.id.text1, accounts));
224 mAccountSelector.setVisibility(View.VISIBLE);
225 mAccountSelector.setSelection(accountPosition);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500226 }
227 if (!accountType.equals(storedAccountType)
228 || !accountName.equals(storedAccountName)) {
229 prefs.edit()
230 .putString(PREF_ACCOUNT_TYPE, accountType)
231 .putString(PREF_ACCOUNT_NAME, accountName)
232 .apply();
233 }
234 if (loader.getId() == LOADER_ACCOUNTS_THEN_BOOKMARKS) {
235 getLoaderManager().initLoader(LOADER_BOOKMARKS, args, this);
236 }
237
238 break;
239 }
240 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500241 }
242
243 @Override
244 public void onClick(View view) {
245 if (view == mUpButton) {
246 Pair<String, Uri> pair = mFolderStack.pop();
Michael Kolb801ecb72010-08-25 12:57:38 -0700247 BookmarksLoader loader =
Jeff Hamilton84029622010-08-05 14:29:28 -0500248 (BookmarksLoader) ((Loader) getLoaderManager().getLoader(LOADER_BOOKMARKS));
249 loader.setUri(pair.second);
250 loader.forceLoad();
251 }
252 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253
254 @Override
255 public boolean onContextItemSelected(MenuItem item) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500256 final Activity activity = getActivity();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 // It is possible that the view has been canceled when we get to
Nicolas Catania095292f2010-03-15 09:00:14 -0700258 // this point as back has a higher priority
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259 if (mCanceled) {
260 return true;
261 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700262 AdapterView.AdapterContextMenuInfo i =
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263 (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
264 // If we have no menu info, we can't tell which item was selected.
265 if (i == null) {
266 return true;
267 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700268
The Android Open Source Project0c908882009-03-03 19:32:16 -0800269 switch (item.getItemId()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 case R.id.open_context_menu_id:
271 loadUrl(i.position);
272 break;
273 case R.id.edit_context_menu_id:
274 editBookmark(i.position);
275 break;
276 case R.id.shortcut_context_menu_id:
Jeff Hamilton64144e42010-08-18 16:31:59 -0500277 activity.sendBroadcast(createShortcutIntent(i.position));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800278 break;
279 case R.id.delete_context_menu_id:
Jeff Hamilton84029622010-08-05 14:29:28 -0500280 displayRemoveBookmarkDialog(i.position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800281 break;
282 case R.id.new_window_context_menu_id:
283 openInNewWindow(i.position);
284 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500285 case R.id.share_link_context_menu_id: {
286 Cursor cursor = (Cursor) mAdapter.getItem(i.position);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500287 BrowserActivity.sharePage(activity,
Jeff Hamilton84029622010-08-05 14:29:28 -0500288 cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE),
289 cursor.getString(BookmarksLoader.COLUMN_INDEX_URL),
290 getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON),
291 getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_THUMBNAIL));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800292 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500293 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800294 case R.id.copy_url_context_menu_id:
295 copy(getUrl(i.position));
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400296 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500297 case R.id.homepage_context_menu_id: {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500298 BrowserSettings.getInstance().setHomePage(activity, getUrl(i.position));
299 Toast.makeText(activity, R.string.homepage_set, Toast.LENGTH_LONG).show();
Leon Scrogginsfeb941d2009-05-28 17:27:38 -0400300 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500301 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400302 // Only for the Most visited page
Jeff Hamilton84029622010-08-05 14:29:28 -0500303 case R.id.save_to_bookmarks_menu_id: {
304 Cursor cursor = (Cursor) mAdapter.getItem(i.position);
305 String name = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
306 String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400307 // If the site is bookmarked, the item becomes remove from
308 // bookmarks.
Jeff Hamilton64144e42010-08-18 16:31:59 -0500309 Bookmarks.removeFromBookmarks(activity, activity.getContentResolver(), url, name);
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400310 break;
Jeff Hamilton84029622010-08-05 14:29:28 -0500311 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800312 default:
313 return super.onContextItemSelected(item);
314 }
315 return true;
316 }
317
Jeff Hamilton84029622010-08-05 14:29:28 -0500318 Bitmap getBitmap(Cursor cursor, int columnIndex) {
319 byte[] data = cursor.getBlob(columnIndex);
320 if (data == null) {
321 return null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800322 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500323 return BitmapFactory.decodeByteArray(data, 0, data.length);
324 }
325
326 @Override
327 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
328 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Leon Scroggins5e8a40f2010-09-27 17:13:35 -0400329 Cursor cursor = (Cursor) mAdapter.getItem(info.position);
330 boolean isFolder
331 = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
332 if (isFolder) return;
Michael Kolb801ecb72010-08-25 12:57:38 -0700333
Jeff Hamilton64144e42010-08-18 16:31:59 -0500334 final Activity activity = getActivity();
335 MenuInflater inflater = activity.getMenuInflater();
Jeff Hamilton84029622010-08-05 14:29:28 -0500336 inflater.inflate(R.menu.bookmarkscontext, menu);
337
338 if (mDisableNewWindow) {
339 menu.findItem(R.id.new_window_context_menu_id).setVisible(false);
340 }
341
342 if (mContextHeader == null) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500343 mContextHeader = new BookmarkItem(activity);
Jeff Hamilton84029622010-08-05 14:29:28 -0500344 } else if (mContextHeader.getParent() != null) {
345 ((ViewGroup) mContextHeader.getParent()).removeView(mContextHeader);
346 }
347
Leon Scroggins5e8a40f2010-09-27 17:13:35 -0400348 populateBookmarkItem(cursor, mContextHeader);
Jeff Hamilton84029622010-08-05 14:29:28 -0500349
350 menu.setHeaderView(mContextHeader);
351 }
352
Leon Scroggins5e8a40f2010-09-27 17:13:35 -0400353 private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500354 String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
355 item.setUrl(url);
356 item.setName(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
357 Bitmap bitmap = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON);
358 if (bitmap == null) {
359 bitmap = CombinedBookmarkHistoryActivity.getIconListenerSet().getFavicon(url);
360 }
361 item.setFavicon(bitmap);
362 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800363
364 /**
365 * Create a new BrowserBookmarksPage.
Nicolas Catania095292f2010-03-15 09:00:14 -0700366 */
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 @Override
Jeff Hamilton64144e42010-08-18 16:31:59 -0500368 public void onCreate(Bundle icicle) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800369 super.onCreate(icicle);
370
Jeff Hamilton64144e42010-08-18 16:31:59 -0500371 Bundle args = getArguments();
372 mCreateShortcut = args == null ? false : args.getBoolean("create_shortcut", false);
373 mDisableNewWindow = args == null ? false : args.getBoolean("disable_new_window", false);
374 }
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400375
Jeff Hamilton64144e42010-08-18 16:31:59 -0500376 @Override
377 public void onAttach(Activity activity) {
378 super.onAttach(activity);
379 mCallbacks = (BookmarksHistoryCallbacks) activity;
380 }
Michael Kolb801ecb72010-08-25 12:57:38 -0700381
Jeff Hamilton64144e42010-08-18 16:31:59 -0500382 @Override
383 public View onCreateView(LayoutInflater inflater, ViewGroup container,
384 Bundle savedInstanceState) {
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500385 Context context = getActivity();
386
Jeff Hamilton64144e42010-08-18 16:31:59 -0500387 View root = inflater.inflate(R.layout.bookmarks, container, false);
388 mEmptyView = root.findViewById(android.R.id.empty);
389 mContentView = root.findViewById(android.R.id.content);
Leon Scrogginsd87f85e2009-08-18 14:13:31 -0400390
Jeff Hamilton64144e42010-08-18 16:31:59 -0500391 mGrid = (GridView) root.findViewById(R.id.grid);
Jeff Hamilton84029622010-08-05 14:29:28 -0500392 mGrid.setOnItemClickListener(this);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500393 mGrid.setColumnWidth(BrowserActivity.getDesiredThumbnailWidth(getActivity()));
Jeff Hamilton84029622010-08-05 14:29:28 -0500394 if (!mCreateShortcut) {
395 mGrid.setOnCreateContextMenuListener(this);
Ben Murdoch328ea872009-09-16 13:33:29 +0100396 }
Patrick Scottf49ecd62010-04-09 12:43:26 -0400397
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500398 mAccountSelector = (Spinner) root.findViewById(R.id.accounts);
399 mAccountSelector.setOnItemSelectedListener(this);
400 mAccountSelector.setVisibility(View.INVISIBLE);
401
Jeff Hamilton64144e42010-08-18 16:31:59 -0500402 mUpButton = (Button) root.findViewById(R.id.up);
Jeff Hamilton84029622010-08-05 14:29:28 -0500403 mUpButton.setEnabled(false);
404 mUpButton.setOnClickListener(this);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500405 mUpButton.setVisibility(View.GONE);
Jeff Hamilton84029622010-08-05 14:29:28 -0500406
Jeff Hamilton64144e42010-08-18 16:31:59 -0500407 mAdapter = new BrowserBookmarksAdapter(getActivity());
Jeff Hamilton84029622010-08-05 14:29:28 -0500408 mGrid.setAdapter(mAdapter);
409
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500410 // Start the loaders
411 LoaderManager lm = getLoaderManager();
412 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
413 String accountType = prefs.getString(PREF_ACCOUNT_TYPE, null);
414 String accountName = prefs.getString(PREF_ACCOUNT_NAME, null);
415 if (!TextUtils.isEmpty(accountType) && !TextUtils.isEmpty(accountName)) {
416 // There is an account set, load up that one
417 Bundle args = null;
418 if (!DEFAULT_ACCOUNT.equals(accountType) && !DEFAULT_ACCOUNT.equals(accountName)) {
419 args = new Bundle();
420 args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
421 args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
422 }
Jeff Hamilton70c77692010-09-21 19:11:24 -0500423 lm.restartLoader(LOADER_BOOKMARKS, args, this);
424 lm.restartLoader(LOADER_ACCOUNTS, null, this);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500425 } else {
426 // No account set, load them first
Jeff Hamilton70c77692010-09-21 19:11:24 -0500427 lm.restartLoader(LOADER_ACCOUNTS_THEN_BOOKMARKS, null, this);
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500428 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500429
430 // Add our own listener in case there are favicons that have yet to be loaded.
431 CombinedBookmarkHistoryActivity.getIconListenerSet().addListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800432
Jeff Hamilton64144e42010-08-18 16:31:59 -0500433 return root;
434 }
Michael Kolb801ecb72010-08-25 12:57:38 -0700435
Nicolas Catania095292f2010-03-15 09:00:14 -0700436 @Override
Jeff Hamilton84029622010-08-05 14:29:28 -0500437 public void onReceivedIcon(String url, Bitmap icon) {
438 // A new favicon has been loaded, so let anything attached to the adapter know about it
439 // so new icons will be loaded.
440 mAdapter.notifyDataSetChanged();
Nicolas Catania095292f2010-03-15 09:00:14 -0700441 }
442
Jeff Hamilton84029622010-08-05 14:29:28 -0500443 @Override
444 public void onItemClick(AdapterView parent, View v, int position, long id) {
445 // It is possible that the view has been canceled when we get to
446 // this point as back has a higher priority
447 if (mCanceled) {
448 android.util.Log.e(LOGTAG, "item clicked when dismissing");
449 return;
450 }
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500451
Jeff Hamilton84029622010-08-05 14:29:28 -0500452 if (mCreateShortcut) {
Michael Kolb801ecb72010-08-25 12:57:38 -0700453 Intent intent = createShortcutIntent(position);
454 // the activity handles the intent in startActivityFromFragment
455 startActivity(intent);
Ben Murdoch328ea872009-09-16 13:33:29 +0100456 return;
457 }
458
Jeff Hamilton84029622010-08-05 14:29:28 -0500459 Cursor cursor = (Cursor) mAdapter.getItem(position);
460 boolean isFolder = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
461 if (!isFolder) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500462 mCallbacks.onUrlSelected(getUrl(position), false);
Leon Scroggins892df312009-07-14 14:48:02 -0400463 } else {
Jeff Hamilton84029622010-08-05 14:29:28 -0500464 String title;
465 if (mFolderStack.size() != 0) {
466 title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
467 } else {
468 // TODO localize
Michael Kolb801ecb72010-08-25 12:57:38 -0700469 title = "Bookmarks";
Leon Scroggins892df312009-07-14 14:48:02 -0400470 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500471 LoaderManager manager = getLoaderManager();
Michael Kolb801ecb72010-08-25 12:57:38 -0700472 BookmarksLoader loader =
Jeff Hamilton84029622010-08-05 14:29:28 -0500473 (BookmarksLoader) ((Loader) manager.getLoader(LOADER_BOOKMARKS));
474 mFolderStack.push(new Pair(title, loader.getUri()));
475 Uri uri = ContentUris.withAppendedId(
476 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, id);
477 loader.setUri(uri);
478 loader.forceLoad();
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400479 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800480 }
481
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500482 @Override
483 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
484 Adapter adapter = parent.getAdapter();
485 String accountType = "com.google";
486 String accountName = adapter.getItem(position).toString();
487
Jeff Hamilton70c77692010-09-21 19:11:24 -0500488 Bundle args = null;
489 if (ACCOUNT_NAME_UNSYNCED.equals(accountName)) {
490 accountType = DEFAULT_ACCOUNT;
491 accountName = DEFAULT_ACCOUNT;
492 } else {
493 args = new Bundle();
494 args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType);
495 args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName);
496 }
497
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500498 // Remember the selection for later
499 PreferenceManager.getDefaultSharedPreferences(getActivity()).edit()
500 .putString(PREF_ACCOUNT_TYPE, accountType)
501 .putString(PREF_ACCOUNT_NAME, accountName)
502 .apply();
503
Jeff Hamiltondb90aa82010-09-16 03:38:04 -0500504 getLoaderManager().restartLoader(LOADER_BOOKMARKS, args, this);
505 }
506
507 @Override
508 public void onNothingSelected(AdapterView<?> parent) {
509 // Do nothing
510 }
511
Patrick Scott3918d442009-08-04 13:22:29 -0400512 private Intent createShortcutIntent(int position) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500513 Cursor cursor = (Cursor) mAdapter.getItem(position);
514 String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
515 String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
516 Bitmap touchIcon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_TOUCH_ICON);
517 Bitmap favicon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500518 return BookmarkUtils.createAddToHomeIntent(getActivity(), url, title, touchIcon, favicon);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800519 }
520
The Android Open Source Project0c908882009-03-03 19:32:16 -0800521 private void loadUrl(int position) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500522 mCallbacks.onUrlSelected(getUrl(position), false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800523 }
524
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 private void openInNewWindow(int position) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500526 mCallbacks.onUrlSelected(getUrl(position), true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800527 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700528
The Android Open Source Project0c908882009-03-03 19:32:16 -0800529 private void editBookmark(int position) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500530 Intent intent = new Intent(getActivity(), AddBookmarkPage.class);
Jeff Hamilton84029622010-08-05 14:29:28 -0500531 Cursor cursor = (Cursor) mAdapter.getItem(position);
532 Bundle item = new Bundle();
Michael Kolbbef7a752010-08-25 17:11:35 -0700533 item.putString(BrowserContract.Bookmarks.TITLE,
Jeff Hamilton84029622010-08-05 14:29:28 -0500534 cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
Michael Kolbbef7a752010-08-25 17:11:35 -0700535 item.putString(BrowserContract.Bookmarks.URL,
Jeff Hamilton84029622010-08-05 14:29:28 -0500536 cursor.getString(BookmarksLoader.COLUMN_INDEX_URL));
537 byte[] data = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON);
538 if (data != null) {
Michael Kolbbef7a752010-08-25 17:11:35 -0700539 item.putParcelable(BrowserContract.Bookmarks.FAVICON,
Jeff Hamilton84029622010-08-05 14:29:28 -0500540 BitmapFactory.decodeByteArray(data, 0, data.length));
541 }
542 item.putInt("id", cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400543 item.putLong(BrowserContract.Bookmarks.PARENT,
544 cursor.getLong(BookmarksLoader.COLUMN_INDEX_PARENT));
Jeff Hamilton84029622010-08-05 14:29:28 -0500545 intent.putExtra("bookmark", item);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800546 startActivityForResult(intent, BOOKMARKS_SAVE);
547 }
548
549 @Override
Jeff Hamilton64144e42010-08-18 16:31:59 -0500550 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800551 switch(requestCode) {
552 case BOOKMARKS_SAVE:
Jeff Hamilton64144e42010-08-18 16:31:59 -0500553 if (resultCode == Activity.RESULT_OK) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800554 Bundle extras;
555 if (data != null && (extras = data.getExtras()) != null) {
556 // If there are extras, then we need to save
557 // the edited bookmark. This is done in updateRow()
558 String title = extras.getString("title");
559 String url = extras.getString("url");
560 if (title != null && url != null) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500561 updateRow(extras);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800562 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 }
564 }
565 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800566 }
567 }
Nicolas Catania095292f2010-03-15 09:00:14 -0700568
Jeff Hamilton84029622010-08-05 14:29:28 -0500569 /**
Michael Kolb801ecb72010-08-25 12:57:38 -0700570 * Update a row in the database with new information.
Jeff Hamilton84029622010-08-05 14:29:28 -0500571 * @param map Bundle storing id, title and url of new information
572 */
573 public void updateRow(Bundle map) {
574
575 // Find the record
Michael Kolbbef7a752010-08-25 17:11:35 -0700576 int id = map.getInt("id");
Jeff Hamilton84029622010-08-05 14:29:28 -0500577 int position = -1;
578 Cursor cursor = mAdapter.getCursor();
579 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
580 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID) == id) {
581 position = cursor.getPosition();
582 break;
583 }
584 }
585 if (position < 0) {
586 return;
587 }
588
589 cursor.moveToPosition(position);
590 ContentValues values = new ContentValues();
Michael Kolbbef7a752010-08-25 17:11:35 -0700591 String title = map.getString(BrowserContract.Bookmarks.TITLE);
592 if (!title.equals(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE))) {
593 values.put(BrowserContract.Bookmarks.TITLE, title);
Jeff Hamilton84029622010-08-05 14:29:28 -0500594 }
Michael Kolbbef7a752010-08-25 17:11:35 -0700595 String url = map.getString(BrowserContract.Bookmarks.URL);
596 if (!url.equals(cursor.getString(BookmarksLoader.COLUMN_INDEX_URL))) {
597 values.put(BrowserContract.Bookmarks.URL, url);
Jeff Hamilton84029622010-08-05 14:29:28 -0500598 }
599
600 if (map.getBoolean("invalidateThumbnail") == true) {
Michael Kolbbef7a752010-08-25 17:11:35 -0700601 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
Jeff Hamilton84029622010-08-05 14:29:28 -0500602 }
603
604 if (values.size() > 0) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500605 getActivity().getContentResolver().update(
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500606 ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id),
607 values, null, null);
Jeff Hamilton84029622010-08-05 14:29:28 -0500608 }
609 }
610
611 private void displayRemoveBookmarkDialog(final int position) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 // Put up a dialog asking if the user really wants to
613 // delete the bookmark
Jeff Hamilton84029622010-08-05 14:29:28 -0500614 Cursor cursor = (Cursor) mAdapter.getItem(position);
Jeff Hamilton64144e42010-08-18 16:31:59 -0500615 Context context = getActivity();
Jeff Hamilton1a805652010-09-07 12:36:30 -0700616 final ContentResolver resolver = context.getContentResolver();
617 final Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI,
618 cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID));
619
Jeff Hamilton64144e42010-08-18 16:31:59 -0500620 new AlertDialog.Builder(context)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800621 .setTitle(R.string.delete_bookmark)
622 .setIcon(android.R.drawable.ic_dialog_alert)
Jeff Hamilton1a805652010-09-07 12:36:30 -0700623 .setMessage(context.getString(R.string.delete_bookmark_warning,
624 cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)))
Nicolas Catania095292f2010-03-15 09:00:14 -0700625 .setPositiveButton(R.string.ok,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800626 new DialogInterface.OnClickListener() {
627 public void onClick(DialogInterface dialog, int whichButton) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700628 resolver.delete(uri, null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 }
630 })
631 .setNegativeButton(R.string.cancel, null)
632 .show();
633 }
634
Jeff Hamilton84029622010-08-05 14:29:28 -0500635 private String getUrl(int position) {
636 Cursor cursor = (Cursor) mAdapter.getItem(position);
637 return cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800638 }
Michael Kolb801ecb72010-08-25 12:57:38 -0700639
The Android Open Source Project0c908882009-03-03 19:32:16 -0800640 private void copy(CharSequence text) {
Jeff Hamilton64144e42010-08-18 16:31:59 -0500641 ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(
642 Context.CLIPBOARD_SERVICE);
Dianne Hackborneb07b172010-08-26 22:18:14 -0700643 cm.setPrimaryClip(ClipData.newRawUri(null, null, Uri.parse(text.toString())));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800644 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800645}