diff options
Diffstat (limited to 'src/com/android')
17 files changed, 344 insertions, 913 deletions
diff --git a/src/com/android/browser/AccountsChangedReceiver.java b/src/com/android/browser/AccountsChangedReceiver.java deleted file mode 100644 index a34180a..0000000 --- a/src/com/android/browser/AccountsChangedReceiver.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.browser; - -import com.android.browser.widget.BookmarkThumbnailWidgetProvider; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.content.BroadcastReceiver; -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.provider.BrowserContract; - -public class AccountsChangedReceiver extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - // Validate that the account we are syncing to still exists - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); - String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); - if (accountType == null || accountName == null) { - // Not syncing, nothing to do - return; - } - Account[] accounts = AccountManager.get(context).getAccountsByType(accountType); - for (Account a : accounts) { - if (accountName.equals(a.name)) { - // Still have a valid account, sweet - return; - } - } - // Account deleted - disable sync - prefs.edit() - .remove(BrowserBookmarksPage.PREF_ACCOUNT_TYPE) - .remove(BrowserBookmarksPage.PREF_ACCOUNT_NAME) - .commit(); - BrowserContract.Settings.setSyncEnabled(context, false); - for (Account a : accounts) { - ContentResolver.setSyncAutomatically(a, BrowserContract.AUTHORITY, false); - ContentResolver.setIsSyncable(a, BrowserContract.AUTHORITY, 0); - } - BookmarkThumbnailWidgetProvider.refreshWidgets(context, true); - } - -} diff --git a/src/com/android/browser/AddBookmarkPage.java b/src/com/android/browser/AddBookmarkPage.java index a9e5f52..f330cd5 100644 --- a/src/com/android/browser/AddBookmarkPage.java +++ b/src/com/android/browser/AddBookmarkPage.java @@ -996,7 +996,7 @@ public class AddBookmarkPage extends Activity } } - static class AccountsLoader extends CursorLoader { + public static class AccountsLoader extends CursorLoader { static final String[] PROJECTION = new String[] { Accounts.ACCOUNT_NAME, @@ -1015,11 +1015,11 @@ public class AddBookmarkPage extends Activity } - static class BookmarkAccount { + public static class BookmarkAccount { private String mLabel; String accountName, accountType; - long rootFolderId; + public long rootFolderId; public BookmarkAccount(Context context, Cursor cursor) { accountName = cursor.getString( diff --git a/src/com/android/browser/BookmarkDragHandler.java b/src/com/android/browser/BookmarkDragHandler.java index 0707058..fc0752f 100644 --- a/src/com/android/browser/BookmarkDragHandler.java +++ b/src/com/android/browser/BookmarkDragHandler.java @@ -16,23 +16,30 @@ package com.android.browser; +import android.app.Activity; import android.content.ClipData; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; -import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.BrowserContract; -import android.provider.BrowserContract.Bookmarks; +import android.view.ActionMode; +import android.view.ActionMode.Callback; import android.view.DragEvent; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; import android.view.View.OnDragListener; +import android.view.ViewGroup; -public class BookmarkDragHandler { +public class BookmarkDragHandler implements Callback { public static interface BookmarkDragController { boolean startDrag(Cursor item); + + ViewGroup getActionModeView(ActionMode mode, BookmarkDragState state); + void actionItemClicked(View v, BookmarkDragState state); } public static interface BookmarkDragAdapter { @@ -40,26 +47,29 @@ public class BookmarkDragHandler { Cursor getItemForView(View v); } - static class BookmarkDragState { - long id; - long parent; + public static class BookmarkDragState { + public long id; + public long parent; + public Object extraState; } static final String BOOKMARK_DRAG_LABEL = "com.android.browser.BOOKMARK_LABEL"; - private Context mContext; + private Activity mActivity; private BookmarkDragController mDragController; private BookmarkDragAdapter mDragAdapter; + private ActionMode mActionMode; + private BookmarkDragState mDragState; - public BookmarkDragHandler(Context context, BookmarkDragController controller, + public BookmarkDragHandler(Activity activity, BookmarkDragController controller, BookmarkDragAdapter adapter) { - mContext = context; + mActivity = activity; mDragController = controller; mDragAdapter = adapter; mDragAdapter.setBookmarkDragHandler(this); } - public boolean startDrag(View view, Cursor item, long id) { + public boolean startDrag(View view, Cursor item, long id, Object extraState) { if (!mDragController.startDrag(item)) { return false; } @@ -69,7 +79,10 @@ public class BookmarkDragHandler { BookmarkDragState state = new BookmarkDragState(); state.id = id; state.parent = item.getLong(BookmarksLoader.COLUMN_INDEX_PARENT); + state.extraState = extraState; + mDragState = state; view.startDrag(data, new View.DragShadowBuilder(view), state, 0); + mActionMode = view.startActionMode(this); return true; } @@ -98,9 +111,9 @@ public class BookmarkDragHandler { parent = c.getLong(BookmarksLoader.COLUMN_INDEX_ID); } if (parent != state.parent) { - ContentResolver cr = mContext.getContentResolver(); + ContentResolver cr = mActivity.getContentResolver(); ContentValues values = new ContentValues(); - values.put(Bookmarks.PARENT, parent); + values.put(BrowserContract.Bookmarks.PARENT, parent); Uri uri = event.getClipData().getItemAt(0).getUri(); cr.update(uri, values, null, null); } @@ -110,7 +123,55 @@ public class BookmarkDragHandler { } }; + private OnDragListener mActionModeDragListener = new OnDragListener() { + + @Override + public boolean onDrag(View v, DragEvent event) { + BookmarkDragState state = (BookmarkDragState) event.getLocalState(); + switch (event.getAction()) { + case DragEvent.ACTION_DRAG_STARTED: + return true; + case DragEvent.ACTION_DROP: + mDragController.actionItemClicked(v, state); + // fall through + case DragEvent.ACTION_DRAG_ENDED: + if (mActionMode != null) { + mActionMode.finish(); + mActionMode = null; + } + return true; + } + return false; + } + }; + static boolean isFolder(Cursor c) { return c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; } + + @Override + public boolean onCreateActionMode(ActionMode mode, Menu menu) { + ViewGroup view = mDragController.getActionModeView(mode, mDragState); + int count = view.getChildCount(); + for (int i = 0; i < count; i++) { + view.getChildAt(i).setOnDragListener(mActionModeDragListener); + } + mode.setCustomView(view); + return true; + } + + @Override + public boolean onPrepareActionMode(ActionMode mode, Menu menu) { + return true; + } + + @Override + public boolean onActionItemClicked(ActionMode mode, MenuItem item) { + return false; + } + + @Override + public void onDestroyActionMode(ActionMode mode) { + } + } diff --git a/src/com/android/browser/BookmarkUtils.java b/src/com/android/browser/BookmarkUtils.java index 383dc7e..491c16c 100644 --- a/src/com/android/browser/BookmarkUtils.java +++ b/src/com/android/browser/BookmarkUtils.java @@ -219,18 +219,6 @@ public class BookmarkUtils { return BrowserContract.Bookmarks.CONTENT_URI; } - @Deprecated - public static Uri.Builder addAccountInfo(Context context, Uri.Builder ub) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); - String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); - ub.appendQueryParameter( - BrowserContract.Bookmarks.PARAM_ACCOUNT_NAME,accountName); - ub.appendQueryParameter( - BrowserContract.Bookmarks.PARAM_ACCOUNT_TYPE, accountType); - return ub; - } - /** * Show a confirmation dialog to remove a bookmark. * @param id Id of the bookmark to remove diff --git a/src/com/android/browser/Bookmarks.java b/src/com/android/browser/Bookmarks.java index e7dc729..bd3b872 100644 --- a/src/com/android/browser/Bookmarks.java +++ b/src/com/android/browser/Bookmarks.java @@ -72,10 +72,6 @@ public class Bookmarks { ContentValues values = new ContentValues(); try { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); - String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); - values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountType); - values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountName); values.put(BrowserContract.Bookmarks.TITLE, name); values.put(BrowserContract.Bookmarks.URL, url); values.put(BrowserContract.Bookmarks.IS_FOLDER, 0); diff --git a/src/com/android/browser/BrowserBookmarksPage.java b/src/com/android/browser/BrowserBookmarksPage.java index 448f881..d4d1c0d 100644 --- a/src/com/android/browser/BrowserBookmarksPage.java +++ b/src/com/android/browser/BrowserBookmarksPage.java @@ -16,10 +16,6 @@ package com.android.browser; -import com.android.browser.BookmarkDragHandler.BookmarkDragController; -import com.android.browser.view.BookmarkExpandableGridView; -import com.android.browser.view.BookmarkExpandableGridView.BookmarkContextMenuInfo; - import android.app.Activity; import android.app.Fragment; import android.app.LoaderManager; @@ -44,6 +40,7 @@ import android.preference.PreferenceManager; import android.provider.BrowserContract; import android.provider.BrowserContract.Accounts; import android.provider.BrowserContract.ChromeSyncColumns; +import android.view.ActionMode; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; @@ -55,10 +52,17 @@ import android.view.ViewGroup; import android.webkit.WebIconDatabase.IconListener; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; +import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupMenu.OnMenuItemClickListener; import android.widget.Toast; +import com.android.browser.BookmarkDragHandler.BookmarkDragController; +import com.android.browser.BookmarkDragHandler.BookmarkDragState; +import com.android.browser.view.BookmarkExpandableGridView; +import com.android.browser.view.BookmarkExpandableGridView.BookmarkContextMenuInfo; + import java.util.HashMap; interface BookmarksPageCallbacks { @@ -75,6 +79,11 @@ public class BrowserBookmarksPage extends Fragment implements View.OnCreateConte LoaderManager.LoaderCallbacks<Cursor>, IconListener, BreadCrumbView.Controller, OnMenuItemClickListener, OnChildClickListener { + public static class ExtraDragState { + public int childPosition; + public int groupPosition; + } + static final String LOGTAG = "browser"; static final int LOADER_ACCOUNTS = 1; @@ -82,9 +91,6 @@ public class BrowserBookmarksPage extends Fragment implements View.OnCreateConte static final String EXTRA_DISABLE_WINDOW = "disable_new_window"; - public static final String PREF_ACCOUNT_TYPE = "acct_type"; - public static final String PREF_ACCOUNT_NAME = "acct_name"; - static final String ACCOUNT_TYPE = "account_type"; static final String ACCOUNT_NAME = "account_name"; @@ -182,33 +188,42 @@ public class BrowserBookmarksPage extends Fragment implements View.OnCreateConte @Override public boolean onContextItemSelected(MenuItem item) { - final Activity activity = getActivity(); BookmarkContextMenuInfo i = (BookmarkContextMenuInfo)item.getMenuInfo(); // If we have no menu info, we can't tell which item was selected. if (i == null) { return false; } - BrowserBookmarksAdapter adapter = getChildAdapter(i.groupPosition); - switch (item.getItemId()) { + if (handleContextItem(item.getItemId(), i.groupPosition, i.childPosition)) { + return true; + } + return super.onContextItemSelected(item); + } + + public boolean handleContextItem(int itemId, int groupPosition, + int childPosition) { + final Activity activity = getActivity(); + BrowserBookmarksAdapter adapter = getChildAdapter(groupPosition); + + switch (itemId) { case R.id.open_context_menu_id: - loadUrl(adapter, i.childPosition); + loadUrl(adapter, childPosition); break; case R.id.edit_context_menu_id: - editBookmark(adapter, i.childPosition); + editBookmark(adapter, childPosition); break; case R.id.shortcut_context_menu_id: - Cursor c = adapter.getItem(i.childPosition); + Cursor c = adapter.getItem(childPosition); activity.sendBroadcast(createShortcutIntent(getActivity(), c)); break; case R.id.delete_context_menu_id: - displayRemoveBookmarkDialog(adapter, i.childPosition); + displayRemoveBookmarkDialog(adapter, childPosition); break; case R.id.new_window_context_menu_id: - openInNewWindow(adapter, i.childPosition); + openInNewWindow(adapter, childPosition); break; case R.id.share_link_context_menu_id: { - Cursor cursor = adapter.getItem(i.childPosition); + Cursor cursor = adapter.getItem(childPosition); Controller.sharePage(activity, cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE), cursor.getString(BookmarksLoader.COLUMN_INDEX_URL), @@ -217,16 +232,16 @@ public class BrowserBookmarksPage extends Fragment implements View.OnCreateConte break; } case R.id.copy_url_context_menu_id: - copy(getUrl(adapter, i.childPosition)); + copy(getUrl(adapter, childPosition)); break; case R.id.homepage_context_menu_id: { - BrowserSettings.getInstance().setHomePage(getUrl(adapter, i.childPosition)); + BrowserSettings.getInstance().setHomePage(getUrl(adapter, childPosition)); Toast.makeText(activity, R.string.homepage_set, Toast.LENGTH_LONG).show(); break; } // Only for the Most visited page case R.id.save_to_bookmarks_menu_id: { - Cursor cursor = adapter.getItem(i.childPosition); + Cursor cursor = adapter.getItem(childPosition); String name = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); // If the site is bookmarked, the item becomes remove from @@ -235,7 +250,7 @@ public class BrowserBookmarksPage extends Fragment implements View.OnCreateConte break; } default: - return super.onContextItemSelected(item); + return false; } return true; } @@ -687,6 +702,41 @@ public class BrowserBookmarksPage extends Fragment implements View.OnCreateConte public boolean startDrag(Cursor item) { return canEdit(item); } + + @Override + public ViewGroup getActionModeView(ActionMode mode, + BookmarkDragState state) { + LayoutInflater inflater = LayoutInflater.from(getActivity()); + LinearLayout view = (LinearLayout) inflater.inflate( + R.layout.bookmarks_drag_actionmode, null); + view.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE); + ExtraDragState extraState = (ExtraDragState) state.extraState; + BrowserBookmarksAdapter adapter = getChildAdapter(extraState.groupPosition); + Cursor c = adapter.getItem(extraState.childPosition); + boolean isFolder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; + if (isFolder) { + view.findViewById(R.id.open_context_menu_id).setVisibility(View.GONE); + ImageView iv = (ImageView) view.findViewById( + R.id.new_window_context_menu_id); + iv.setImageResource(R.drawable.ic_windows_holo_dark); + } + return view; + } + + @Override + public void actionItemClicked(View v, BookmarkDragState state) { + if (v.getId() == R.id.info) { + if (mCurrentView == VIEW_THUMBNAILS) { + mGrid.showContextMenuForState(state); + } else { + // TODO: Support expandable list + } + } else { + ExtraDragState extraState = (ExtraDragState) state.extraState; + handleContextItem(v.getId(), extraState.groupPosition, + extraState.childPosition); + } + } }; private static class LookupBookmarkCount extends AsyncTask<Long, Void, Integer> { diff --git a/src/com/android/browser/BrowserHistoryPage.java b/src/com/android/browser/BrowserHistoryPage.java index e728254..bcc33e2 100644 --- a/src/com/android/browser/BrowserHistoryPage.java +++ b/src/com/android/browser/BrowserHistoryPage.java @@ -136,11 +136,7 @@ public class BrowserHistoryPage extends Fragment public Loader<Cursor> onCreateLoader(int id, Bundle args) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( getActivity()); - String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); - String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); Uri.Builder combinedBuilder = Combined.CONTENT_URI.buildUpon(); - combinedBuilder.appendQueryParameter(BrowserContract.Bookmarks.PARAM_ACCOUNT_TYPE, accountType); - combinedBuilder.appendQueryParameter(BrowserContract.Bookmarks.PARAM_ACCOUNT_NAME, accountName); switch (id) { case LOADER_HISTORY: { diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java index ee40a93..e089f5d 100644 --- a/src/com/android/browser/Controller.java +++ b/src/com/android/browser/Controller.java @@ -480,15 +480,8 @@ public class Controller break; case R.id.open_newtab_context_menu_id: final Tab parent = mTabControl.getCurrentTab(); - final Tab newTab = - openTab(url, - (parent != null) - && parent.isPrivateBrowsingEnabled(), - !mSettings.openInBackground(), - true); - if (newTab != null && newTab != parent) { - parent.addChildTab(newTab); - } + openTab(url, parent, + !mSettings.openInBackground(), true); break; case R.id.copy_link_context_menu_id: copy(url); @@ -1392,15 +1385,9 @@ public class Controller @Override public boolean onMenuItemClick(MenuItem item) { final Tab parent = mTabControl.getCurrentTab(); - final Tab newTab = - openTab(extra, - (parent != null) - && parent.isPrivateBrowsingEnabled(), - !mSettings.openInBackground(), - true); - if (newTab != parent) { - parent.addChildTab(newTab); - } + openTab(extra, parent, + !mSettings.openInBackground(), + true); return true; } }); @@ -2225,8 +2212,23 @@ public class Controller @Override public Tab openTab(String url, boolean incognito, boolean setActive, boolean useCurrent) { + return openTab(url, incognito, setActive, useCurrent, null); + } + + @Override + public Tab openTab(String url, Tab parent, boolean setActive, + boolean useCurrent) { + return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(), + setActive, useCurrent, parent); + } + + public Tab openTab(String url, boolean incognito, boolean setActive, + boolean useCurrent, Tab parent) { Tab tab = createNewTab(incognito, setActive, useCurrent); if (tab != null) { + if (parent != null && parent != tab) { + parent.addChildTab(tab); + } WebView w = tab.getWebView(); if (url != null) { loadUrl(w, url); diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java index 8962475..4097334 100644 --- a/src/com/android/browser/Tab.java +++ b/src/com/android/browser/Tab.java @@ -855,11 +855,7 @@ class Tab { transport.setWebView(mSubView); } else { final Tab newTab = mWebViewController.openTab(null, - Tab.this.isPrivateBrowsingEnabled(), - true, true); - if (newTab != Tab.this) { - Tab.this.addChildTab(newTab); - } + Tab.this, true, true); transport.setWebView(newTab.getWebView()); } msg.sendToTarget(); diff --git a/src/com/android/browser/WebViewController.java b/src/com/android/browser/WebViewController.java index bf3bdba..87703bd 100644 --- a/src/com/android/browser/WebViewController.java +++ b/src/com/android/browser/WebViewController.java @@ -104,6 +104,9 @@ public interface WebViewController { Tab openTab(String url, boolean incognito, boolean setActive, boolean useCurrent); + Tab openTab(String url, Tab parent, boolean setActive, + boolean useCurrent); + boolean switchToTab(Tab tab); void closeTab(Tab tab); diff --git a/src/com/android/browser/preferences/GeneralPreferencesFragment.java b/src/com/android/browser/preferences/GeneralPreferencesFragment.java index 879b95d..af0fc50 100644 --- a/src/com/android/browser/preferences/GeneralPreferencesFragment.java +++ b/src/com/android/browser/preferences/GeneralPreferencesFragment.java @@ -16,45 +16,21 @@ package com.android.browser.preferences; -import com.android.browser.BrowserBookmarksPage; -import com.android.browser.BrowserHomepagePreference; -import com.android.browser.BrowserPreferencesPage; -import com.android.browser.PreferenceKeys; -import com.android.browser.R; -import com.android.browser.widget.BookmarkThumbnailWidgetProvider; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.accounts.AccountManagerCallback; -import android.accounts.AccountManagerFuture; -import android.app.AlertDialog; -import android.app.Dialog; -import android.app.DialogFragment; -import android.content.Context; -import android.content.DialogInterface; -import android.content.SharedPreferences; -import android.content.SharedPreferences.OnSharedPreferenceChangeListener; -import android.os.AsyncTask; import android.os.Bundle; import android.preference.Preference; -import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceFragment; -import android.preference.PreferenceManager; import android.preference.PreferenceScreen; -import android.provider.BrowserContract; import android.util.Log; +import com.android.browser.BrowserHomepagePreference; +import com.android.browser.BrowserPreferencesPage; +import com.android.browser.PreferenceKeys; +import com.android.browser.R; + public class GeneralPreferencesFragment extends PreferenceFragment - implements OnPreferenceClickListener, Preference.OnPreferenceChangeListener { + implements Preference.OnPreferenceChangeListener { static final String TAG = "PersonalPreferencesFragment"; - static final String PREF_CHROME_SYNC = "sync_with_chrome"; - - Preference mChromeSync; - boolean mEnabled; - SharedPreferences mSharedPrefs; - Account[] mAccounts; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -68,8 +44,6 @@ public class GeneralPreferencesFragment extends PreferenceFragment .getString(PreferenceKeys.PREF_HOMEPAGE, null)); ((BrowserHomepagePreference) e).setCurrentPage( getActivity().getIntent().getStringExtra(BrowserPreferencesPage.CURRENT_PAGE)); - mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); - mSharedPrefs.registerOnSharedPreferenceChangeListener(mListener); } @Override @@ -93,171 +67,12 @@ public class GeneralPreferencesFragment extends PreferenceFragment public void onResume() { super.onResume(); - // Setup the proper state for the sync with chrome item - mChromeSync = findPreference(PREF_CHROME_SYNC); refreshUi(); } - @Override - public void onDestroy() { - super.onDestroy(); - - mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mListener); - } - - OnSharedPreferenceChangeListener mListener - = new OnSharedPreferenceChangeListener() { - @Override - public void onSharedPreferenceChanged( - SharedPreferences sharedPreferences, String key) { - if (BrowserBookmarksPage.PREF_ACCOUNT_NAME.equals(key) - || BrowserBookmarksPage.PREF_ACCOUNT_TYPE.equals(key)) { - refreshUi(); - BookmarkThumbnailWidgetProvider.refreshWidgets(getActivity(), true); - } - } - - }; - - private AccountManagerCallback<Bundle> mCallback = - new AccountManagerCallback<Bundle>() { - - @Override - public void run(AccountManagerFuture<Bundle> future) { - try { - Bundle bundle = future.getResult(); - String name = bundle.getString(AccountManager.KEY_ACCOUNT_NAME); - String type = bundle.getString(AccountManager.KEY_ACCOUNT_TYPE); - Account account = new Account(name, type); - mAccounts = new Account[] { account }; - ImportWizard wizard = ImportWizard.newInstance(mAccounts); - wizard.show(getFragmentManager(), null); - } catch (Exception ex) { - // Canceled or failed to login, doesn't matter to us - } - } - }; - - OnPreferenceClickListener mAddAccount = new OnPreferenceClickListener() { - - @Override - public boolean onPreferenceClick(Preference preference) { - AccountManager am = AccountManager.get(getActivity()); - am.addAccount("com.google", null, null, null, getActivity(), - mCallback, null); - return true; - } - }; - - private class GetAccountsTask extends AsyncTask<Void, Void, String> { - private Context mContext; - - GetAccountsTask(Context ctx) { - mContext = ctx; - } - - @Override - protected String doInBackground(Void... unused) { - AccountManager am = (AccountManager) mContext.getSystemService(Context.ACCOUNT_SERVICE); - Account[] accounts = am.getAccountsByType("com.google"); - if (accounts == null || accounts.length == 0) { - // No Google accounts setup, don't offer Chrome sync - if (mChromeSync != null) { - mChromeSync.setOnPreferenceClickListener(mAddAccount); - } - } else { - // Google accounts are present. - mAccounts = accounts; - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); - Bundle args = mChromeSync.getExtras(); - args.putParcelableArray("accounts", accounts); - mEnabled = BrowserContract.Settings.isSyncEnabled(mContext); - mChromeSync.setOnPreferenceClickListener(GeneralPreferencesFragment.this); - - if (!mEnabled) { - // Setup a link to the enable wizard - return mContext.getResources().getString( - R.string.pref_personal_sync_with_chrome_summary); - } else { - // Chrome sync is enabled, setup a link to account switcher - String accountName = prefs.getString( - BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); - args.putString("curAccount", accountName); - return accountName; - } - } - - return null; - } - - @Override - protected void onPostExecute(String summary) { - if (summary != null) { - mChromeSync.setSummary(summary); - } - } - } - void refreshUi() { - new GetAccountsTask(getActivity()).execute(); - PreferenceScreen autoFillSettings = (PreferenceScreen)findPreference(PreferenceKeys.PREF_AUTOFILL_PROFILE); autoFillSettings.setDependency(PreferenceKeys.PREF_AUTOFILL_ENABLED); } - - @Override - public boolean onPreferenceClick(Preference preference) { - if (mAccounts == null) { - Log.w(TAG, "NULL accounts!"); - return true; - } - DialogFragment frag; - if (mEnabled) { - frag = new AccountChooserDialog(); - frag.setArguments(preference.getExtras()); - } else { - frag = ImportWizard.newInstance(mAccounts); - } - frag.show(getFragmentManager(), null); - return true; - } - - public static class AccountChooserDialog extends DialogFragment - implements DialogInterface.OnClickListener { - - AlertDialog mDialog; - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - Bundle args = getArguments(); - Account[] accounts = (Account[]) args.getParcelableArray("accounts"); - String curAccount = args.getString("curAccount"); - int length = accounts.length; - int curAccountOffset = 0; - CharSequence[] accountNames = new CharSequence[length]; - for (int i = 0; i < length; i++) { - String name = accounts[i].name; - if (name.equals(curAccount)) { - curAccountOffset = i; - } - accountNames[i] = name; - } - - mDialog = new AlertDialog.Builder(getActivity()) - .setIcon(android.R.drawable.ic_dialog_alert) - .setTitle(R.string.account_chooser_dialog_title) - .setSingleChoiceItems(accountNames, curAccountOffset, this) - .create(); - return mDialog; - } - - @Override - public void onClick(DialogInterface dialog, int which) { - String accountName = mDialog.getListView().getAdapter().getItem(which).toString(); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); - prefs.edit().putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountName).apply(); - dismiss(); - } - } } diff --git a/src/com/android/browser/preferences/ImportWizard.java b/src/com/android/browser/preferences/ImportWizard.java deleted file mode 100644 index 7105f4d..0000000 --- a/src/com/android/browser/preferences/ImportWizard.java +++ /dev/null @@ -1,491 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - */ - -package com.android.browser.preferences; - -import com.android.browser.BrowserBookmarksPage; -import com.android.browser.R; -import com.android.browser.view.EventRedirectingFrameLayout; - -import android.accounts.Account; -import android.animation.LayoutTransition; -import android.animation.ObjectAnimator; -import android.app.AlertDialog; -import android.app.Dialog; -import android.app.DialogFragment; -import android.content.ContentProviderOperation; -import android.content.ContentResolver; -import android.content.ContentValues; -import android.content.DialogInterface; -import android.content.DialogInterface.OnKeyListener; -import android.content.DialogInterface.OnShowListener; -import android.content.OperationApplicationException; -import android.content.SharedPreferences; -import android.content.res.Resources; -import android.database.Cursor; -import android.os.Bundle; -import android.os.RemoteException; -import android.preference.PreferenceManager; -import android.provider.BrowserContract; -import android.provider.BrowserContract.Bookmarks; -import android.provider.BrowserContract.ChromeSyncColumns; -import android.util.Log; -import android.view.KeyEvent; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.ArrayAdapter; -import android.widget.Button; -import android.widget.ListView; -import android.widget.TextView; - -import java.util.ArrayList; - -public class ImportWizard extends DialogFragment implements OnClickListener, - OnItemClickListener { - - static final String TAG = "BookmarkImportWizard"; - - static final int PAGE_IMPORT_OR_DELETE = 0; - static final int PAGE_SELECT_ACCOUNT = 1; - static final int PAGE_CONFIRMATION = 2; - - static final String STATE_CURRENT_PAGE = "wizard.current_page"; - static final String STATE_IMPORT_OR_DELETE = "wizard.import_or_delete"; - static final String STATE_SELECTED_ACCOUNT = "wizard.selected_account"; - - static final String ARG_ACCOUNTS = "accounts"; - - AlertDialog mDialog; - EventRedirectingFrameLayout mPages; - int mCurrentPage; - Button mPositiveButton, mNegativeButton; - ListView mImportOrDelete, mSelectAccount; - Account[] mAccounts; - TextView mSelectAccountDescription, mConfirmation; - - static ImportWizard newInstance(Account[] accounts) { - ImportWizard wizard = new ImportWizard(); - Bundle args = new Bundle(); - args.putParcelableArray(ARG_ACCOUNTS, accounts); - wizard.setArguments(args); - return wizard; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mAccounts = (Account[]) getArguments().getParcelableArray(ARG_ACCOUNTS); - } - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - mDialog = new AlertDialog.Builder(getActivity()) - .setTitle(R.string.import_bookmarks_dialog_title) - .setView(createView(savedInstanceState)) - .setPositiveButton("?", null) // This is just a placeholder - .setNegativeButton("?", null) // Ditto - .setOnKeyListener(new OnKeyListener() { - @Override - public boolean onKey(DialogInterface arg0, int arg1, KeyEvent key) { - if (key.getKeyCode() == KeyEvent.KEYCODE_BACK) { - if (key.getAction() == KeyEvent.ACTION_UP - && !key.isCanceled()) { - mNegativeButton.performClick(); - } - return true; - } - return false; - } - }) - .create(); - mDialog.setOnShowListener(new OnShowListener() { - @Override - public void onShow(DialogInterface dialog) { - mPositiveButton = mDialog.getButton(AlertDialog.BUTTON_POSITIVE); - mNegativeButton = mDialog.getButton(AlertDialog.BUTTON_NEGATIVE); - mPositiveButton.setOnClickListener(ImportWizard.this); - mNegativeButton.setOnClickListener(ImportWizard.this); - setupAnimations(); - updateNavigation(); - } - }); - return mDialog; - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - outState.putInt(STATE_CURRENT_PAGE, mCurrentPage); - outState.putInt(STATE_IMPORT_OR_DELETE, mImportOrDelete.getCheckedItemPosition()); - outState.putInt(STATE_SELECTED_ACCOUNT, mSelectAccount.getCheckedItemPosition()); - } - - public View createView(Bundle savedInstanceState) { - LayoutInflater inflater = LayoutInflater.from(getActivity()); - View root = inflater.inflate(R.layout.bookmark_sync_wizard, null); - mPages = (EventRedirectingFrameLayout) root.findViewById(R.id.pages); - if (mPages.getChildCount() < 1) { - throw new IllegalStateException("no pages in wizard!"); - } - if (savedInstanceState != null) { - mCurrentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE); - } else { - mCurrentPage = 0; - } - setupPage1(savedInstanceState); - setupPage2(savedInstanceState); - setupPage3(savedInstanceState); - for (int i = 0; i < mPages.getChildCount(); i++) { - View v = mPages.getChildAt(i); - if (i <= mCurrentPage) { - preparePage(); - v.setVisibility(View.VISIBLE); - } else { - v.setVisibility(View.GONE); - } - } - mPages.setTargetChild(mCurrentPage); - return root; - } - - void setupPage1(Bundle savedInstanceState) { - mImportOrDelete = (ListView) mPages.findViewById(R.id.add_remove_bookmarks); - // Add an empty header so we get a divider above the list - mImportOrDelete.addHeaderView(new View(getActivity())); - Resources res = getActivity().getResources(); - String[] choices = new String[] { - res.getString(R.string.import_bookmarks_dialog_add), - res.getString(R.string.import_bookmarks_dialog_remove) - }; - ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), - R.layout.bookmark_sync_wizard_item, choices); - mImportOrDelete.setAdapter(adapter); - if (savedInstanceState != null) { - int position = savedInstanceState.getInt(STATE_IMPORT_OR_DELETE); - if (position == ListView.INVALID_POSITION) { - mImportOrDelete.clearChoices(); - } else { - mImportOrDelete.setItemChecked(position, true); - } - } - mImportOrDelete.setOnItemClickListener(this); - } - - void setupPage2(Bundle savedInstanceState) { - mSelectAccount = (ListView) mPages.findViewById(R.id.select_account); - mSelectAccountDescription = - (TextView) mPages.findViewById(R.id.select_account_description); - // Add an empty header so we get a divider above the list - mSelectAccount.addHeaderView(new View(getActivity())); - Resources res = getActivity().getResources(); - String[] accountNames = new String[mAccounts.length]; - for (int i = 0; i < mAccounts.length; i++) { - accountNames[i] = mAccounts[i].name; - } - ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), - R.layout.bookmark_sync_wizard_item, accountNames); - mSelectAccount.setAdapter(adapter); - mSelectAccount.setItemChecked(mSelectAccount.getHeaderViewsCount(), true); - if (savedInstanceState != null) { - int position = savedInstanceState.getInt(STATE_SELECTED_ACCOUNT); - if (position != ListView.INVALID_POSITION) { - mSelectAccount.setItemChecked(position, true); - } - } - mSelectAccount.setOnItemClickListener(this); - } - - void setupPage3(Bundle savedInstanceState) { - mConfirmation = (TextView) mPages.findViewById(R.id.confirm); - } - - void preparePage() { - switch (mCurrentPage) { - case PAGE_SELECT_ACCOUNT: - if (shouldDeleteBookmarks()) { - mSelectAccountDescription.setText( - R.string.import_bookmarks_dialog_delete_select_account); - } else { - mSelectAccountDescription.setText( - R.string.import_bookmarks_dialog_select_add_account); - } - break; - case PAGE_CONFIRMATION: - String account = getSelectedAccount().name; - String confirmationMessage; - if (shouldDeleteBookmarks()) { - confirmationMessage = getActivity().getString( - R.string.import_bookmarks_dialog_confirm_delete, account); - } else { - confirmationMessage = getActivity().getString( - R.string.import_bookmarks_dialog_confirm_add, account); - } - mConfirmation.setText(confirmationMessage); - break; - } - } - - int getAdjustedCheckedItemPosition(ListView list) { - int position = list.getCheckedItemPosition(); - if (position != ListView.INVALID_POSITION) { - position -= list.getHeaderViewsCount(); - } - return position; - } - - Account getSelectedAccount() { - return mAccounts[getAdjustedCheckedItemPosition(mSelectAccount)]; - } - - boolean shouldDeleteBookmarks() { - return getAdjustedCheckedItemPosition(mImportOrDelete) == 1; - } - - @Override - public void onItemClick( - AdapterView<?> parent, View view, int position, long id) { - validate(); - } - - void updateNavigation() { - if (mCurrentPage == 0) { - mNegativeButton.setText(R.string.import_bookmarks_wizard_cancel); - } else { - mNegativeButton.setText(R.string.import_bookmarks_wizard_previous); - } - if ((mCurrentPage + 1) == mPages.getChildCount()) { - mPositiveButton.setText(R.string.import_bookmarks_wizard_done); - } else { - mPositiveButton.setText(R.string.import_bookmarks_wizard_next); - } - validate(); - } - - void validate() { - switch (mCurrentPage) { - case PAGE_IMPORT_OR_DELETE: - mPositiveButton.setEnabled( - mImportOrDelete.getCheckedItemPosition() != ListView.INVALID_POSITION); - break; - case PAGE_SELECT_ACCOUNT: - mPositiveButton.setEnabled( - mSelectAccount.getCheckedItemPosition() != ListView.INVALID_POSITION); - break; - } - } - - void setupAnimations() { - float animX = mPages.getMeasuredWidth(); - final LayoutTransition transitioner = new LayoutTransition(); - ObjectAnimator appearing = ObjectAnimator.ofFloat(this, "translationX", - animX, 0); - ObjectAnimator disappearing = ObjectAnimator.ofFloat(this, "translationX", - 0, animX); - transitioner.setAnimator(LayoutTransition.APPEARING, appearing); - transitioner.setAnimator(LayoutTransition.DISAPPEARING, disappearing); - mPages.setLayoutTransition(transitioner); - } - - boolean next() { - if (mCurrentPage + 1 < mPages.getChildCount()) { - mCurrentPage++; - preparePage(); - mPages.getChildAt(mCurrentPage).setVisibility(View.VISIBLE); - mPages.setTargetChild(mCurrentPage); - return true; - } - return false; - } - - boolean prev() { - if (mCurrentPage > 0) { - mPages.getChildAt(mCurrentPage).setVisibility(View.GONE); - mCurrentPage--; - mPages.setTargetChild(mCurrentPage); - return true; - } - return false; - } - - void done() { - ContentResolver resolver = getActivity().getContentResolver(); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); - String accountName = getSelectedAccount().name; - if (shouldDeleteBookmarks()) { - // The user chose to remove their old bookmarks, delete them now - resolver.delete(Bookmarks.CONTENT_URI, - Bookmarks.PARENT + "=1 AND " + Bookmarks.ACCOUNT_NAME + " IS NULL", null); - } else { - // The user chose to migrate their old bookmarks to the account they're syncing - migrateBookmarks(resolver, accountName); - } - - // Record the fact that we turned on sync - BrowserContract.Settings.setSyncEnabled(getActivity(), true); - prefs.edit() - .putString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, "com.google") - .putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountName) - .apply(); - - // Enable bookmark sync on all accounts - Account[] accounts = (Account[]) getArguments().getParcelableArray("accounts"); - for (Account account : accounts) { - if (ContentResolver.getIsSyncable(account, BrowserContract.AUTHORITY) == 0) { - // Account wasn't syncable, enable it - ContentResolver.setIsSyncable(account, BrowserContract.AUTHORITY, 1); - ContentResolver.setSyncAutomatically(account, BrowserContract.AUTHORITY, true); - } - } - - dismiss(); - } - - @Override - public void onClick(View v) { - if (v == mNegativeButton) { - if (prev()) { - updateNavigation(); - } else { - dismiss(); - } - } else if (v == mPositiveButton) { - if (next()) { - updateNavigation(); - } else { - done(); - } - } - } - - /** - * Migrates bookmarks to the given account - */ - void migrateBookmarks(ContentResolver resolver, String accountName) { - Cursor cursor = null; - try { - // Re-parent the bookmarks in the default root folder - cursor = resolver.query(Bookmarks.CONTENT_URI, new String[] { Bookmarks._ID }, - Bookmarks.ACCOUNT_NAME + " =? AND " + - ChromeSyncColumns.SERVER_UNIQUE + " =?", - new String[] { accountName, - ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR }, - null); - ContentValues values = new ContentValues(); - if (cursor == null || !cursor.moveToFirst()) { - // The root folders don't exist for the account, create them now - ArrayList<ContentProviderOperation> ops = - new ArrayList<ContentProviderOperation>(); - - // Chrome sync root folder - values.clear(); - values.put(ChromeSyncColumns.SERVER_UNIQUE, ChromeSyncColumns.FOLDER_NAME_ROOT); - values.put(Bookmarks.TITLE, "Google Chrome"); - values.put(Bookmarks.POSITION, 0); - values.put(Bookmarks.IS_FOLDER, true); - values.put(Bookmarks.DIRTY, true); - ops.add(ContentProviderOperation.newInsert( - Bookmarks.CONTENT_URI.buildUpon().appendQueryParameter( - BrowserContract.CALLER_IS_SYNCADAPTER, "true").build()) - .withValues(values) - .build()); - - // Bookmarks folder - values.clear(); - values.put(ChromeSyncColumns.SERVER_UNIQUE, - ChromeSyncColumns.FOLDER_NAME_BOOKMARKS); - values.put(Bookmarks.TITLE, "Bookmarks"); - values.put(Bookmarks.POSITION, 0); - values.put(Bookmarks.IS_FOLDER, true); - values.put(Bookmarks.DIRTY, true); - ops.add(ContentProviderOperation.newInsert(Bookmarks.CONTENT_URI) - .withValues(values) - .withValueBackReference(Bookmarks.PARENT, 0) - .build()); - - // Bookmarks Bar folder - values.clear(); - values.put(ChromeSyncColumns.SERVER_UNIQUE, - ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR); - values.put(Bookmarks.TITLE, "Bookmarks Bar"); - values.put(Bookmarks.POSITION, 0); - values.put(Bookmarks.IS_FOLDER, true); - values.put(Bookmarks.DIRTY, true); - ops.add(ContentProviderOperation.newInsert(Bookmarks.CONTENT_URI) - .withValues(values) - .withValueBackReference(Bookmarks.PARENT, 1) - .build()); - - // Other Bookmarks folder - values.clear(); - values.put(ChromeSyncColumns.SERVER_UNIQUE, - ChromeSyncColumns.FOLDER_NAME_OTHER_BOOKMARKS); - values.put(Bookmarks.TITLE, "Other Bookmarks"); - values.put(Bookmarks.POSITION, 1000); - values.put(Bookmarks.IS_FOLDER, true); - values.put(Bookmarks.DIRTY, true); - ops.add(ContentProviderOperation.newInsert(Bookmarks.CONTENT_URI) - .withValues(values) - .withValueBackReference(Bookmarks.PARENT, 1) - .build()); - - // Re-parent the existing bookmarks to the newly create bookmarks bar folder - ops.add(ContentProviderOperation.newUpdate(Bookmarks.CONTENT_URI) - .withValueBackReference(Bookmarks.PARENT, 2) - .withSelection(Bookmarks.ACCOUNT_NAME + " IS NULL AND " + - Bookmarks.PARENT + "=?", - new String[] { Integer.toString(1) }) - .build()); - - // Mark all non-root folder items as belonging to the new account - values.clear(); - values.put(Bookmarks.ACCOUNT_TYPE, "com.google"); - values.put(Bookmarks.ACCOUNT_NAME, accountName); - ops.add(ContentProviderOperation.newUpdate(Bookmarks.CONTENT_URI) - .withValues(values) - .withSelection(Bookmarks.ACCOUNT_NAME + " IS NULL AND " + - Bookmarks._ID + "<>1", null) - .build()); - - try { - resolver.applyBatch(BrowserContract.AUTHORITY, ops); - } catch (RemoteException e) { - Log.e(TAG, "failed to create root folder for account " + accountName, e); - return; - } catch (OperationApplicationException e) { - Log.e(TAG, "failed to create root folder for account " + accountName, e); - return; - } - } else { - values.put(Bookmarks.PARENT, cursor.getLong(0)); - resolver.update(Bookmarks.CONTENT_URI, values, Bookmarks.PARENT + "=?", - new String[] { Integer.toString(1) }); - - // Mark all bookmarks at all levels as part of the new account - values.clear(); - values.put(Bookmarks.ACCOUNT_TYPE, "com.google"); - values.put(Bookmarks.ACCOUNT_NAME, accountName); - resolver.update(Bookmarks.CONTENT_URI, values, - Bookmarks.ACCOUNT_NAME + " IS NULL AND " + Bookmarks._ID + "<>1", - null); - } - } finally { - if (cursor != null) cursor.close(); - } - } -} diff --git a/src/com/android/browser/provider/BrowserProvider2.java b/src/com/android/browser/provider/BrowserProvider2.java index a8739ca..d9760e5 100644 --- a/src/com/android/browser/provider/BrowserProvider2.java +++ b/src/com/android/browser/provider/BrowserProvider2.java @@ -333,7 +333,7 @@ public class BrowserProvider2 extends SQLiteContentProvider { final class DatabaseHelper extends SQLiteOpenHelper { static final String DATABASE_NAME = "browser2.db"; - static final int DATABASE_VERSION = 27; + static final int DATABASE_VERSION = 28; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @@ -402,6 +402,15 @@ public class BrowserProvider2 extends SQLiteContentProvider { if (!importFromBrowserProvider(db)) { createDefaultBookmarks(db); } + + enableSync(db); + } + + void enableSync(SQLiteDatabase db) { + ContentValues values = new ContentValues(); + values.put(Settings.KEY, Settings.KEY_SYNC_ENABLED); + values.put(Settings.VALUE, 1); + insertSettingsInTransaction(db, values); } boolean importFromBrowserProvider(SQLiteDatabase db) { @@ -491,7 +500,9 @@ public class BrowserProvider2 extends SQLiteContentProvider { @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - // TODO write upgrade logic + if (oldVersion < 28) { + enableSync(db); + } if (oldVersion < 27) { createAccountsView(db); } @@ -944,10 +955,6 @@ public class BrowserProvider2 extends SQLiteContentProvider { && projection == null) { projection = Browser.HISTORY_PROJECTION; } - if (match == LEGACY) { - uri = BookmarkUtils.addAccountInfo(getContext(), - uri.buildUpon()).build(); - } String[] args = createCombinedQuery(uri, projection, qb); if (selectionArgs == null) { selectionArgs = args; @@ -996,19 +1003,6 @@ public class BrowserProvider2 extends SQLiteContentProvider { selection = DatabaseUtils.concatenateWhere(selection, Bookmarks.IS_DELETED + "=0 AND " + Bookmarks.IS_FOLDER + "=0"); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); - String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); - String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); - if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { - selection = DatabaseUtils.concatenateWhere(selection, - Bookmarks.ACCOUNT_TYPE + "=? AND " + Bookmarks.ACCOUNT_NAME + "=? "); - selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs, - new String[] { accountType, accountName }); - } else { - selection = DatabaseUtils.concatenateWhere(selection, - Bookmarks.ACCOUNT_TYPE + " IS NULL AND " + - Bookmarks.ACCOUNT_NAME + " IS NULL "); - } Cursor c = mOpenHelper.getReadableDatabase().query(TABLE_BOOKMARKS, SUGGEST_PROJECTION, selection, selectionArgs, null, null, DEFAULT_BOOKMARKS_SORT_ORDER, null); @@ -1148,8 +1142,6 @@ public class BrowserProvider2 extends SQLiteContentProvider { String[] projection = new String[] { Combined._ID, Combined.IS_BOOKMARK, Combined.URL }; SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); - uri = BookmarkUtils.addAccountInfo(getContext(), uri.buildUpon()) - .build(); String[] args = createCombinedQuery(uri, projection, qb); if (selectionArgs == null) { selectionArgs = args; @@ -1219,14 +1211,6 @@ public class BrowserProvider2 extends SQLiteContentProvider { values.remove(BookmarkColumns.DATE); values.remove(BookmarkColumns.VISITS); values.remove(BookmarkColumns.USER_ENTERED); - SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(getContext()); - String accountType = prefs.getString( - BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); - String accountName = prefs.getString( - BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); - values.put(Bookmarks.ACCOUNT_TYPE, accountType); - values.put(Bookmarks.ACCOUNT_NAME, accountName); values.put(Bookmarks.IS_FOLDER, 0); } } diff --git a/src/com/android/browser/view/BookmarkExpandableGridView.java b/src/com/android/browser/view/BookmarkExpandableGridView.java index 2cf4a65..f6b9f19 100644 --- a/src/com/android/browser/view/BookmarkExpandableGridView.java +++ b/src/com/android/browser/view/BookmarkExpandableGridView.java @@ -16,13 +16,6 @@ package com.android.browser.view; -import com.android.browser.BookmarkDragHandler; -import com.android.browser.BreadCrumbView; -import com.android.browser.BrowserBookmarksAdapter; -import com.android.browser.R; -import com.android.browser.BookmarkDragHandler.BookmarkDragAdapter; -import com.android.internal.view.menu.MenuBuilder; - import android.content.Context; import android.database.Cursor; import android.database.DataSetObserver; @@ -40,6 +33,15 @@ import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.TextView; +import com.android.browser.BookmarkDragHandler; +import com.android.browser.BookmarkDragHandler.BookmarkDragAdapter; +import com.android.browser.BookmarkDragHandler.BookmarkDragState; +import com.android.browser.BreadCrumbView; +import com.android.browser.BrowserBookmarksAdapter; +import com.android.browser.BrowserBookmarksPage.ExtraDragState; +import com.android.browser.R; +import com.android.internal.view.menu.MenuBuilder; + import java.util.ArrayList; import java.util.HashMap; @@ -206,6 +208,16 @@ public class BookmarkExpandableGridView extends ExpandableListView return mDragAdapter; } + public void showContextMenuForState(BookmarkDragState state) { + ExtraDragState extraState = (ExtraDragState) state.extraState; + mContextMenuInfo = new BookmarkContextMenuInfo( + extraState.childPosition, + extraState.groupPosition); + if (getParent() != null) { + getParent().showContextMenuForChild(BookmarkExpandableGridView.this); + } + } + private BookmarkDragAdapter mDragAdapter = new BookmarkDragAdapter() { @Override @@ -219,6 +231,7 @@ public class BookmarkExpandableGridView extends ExpandableListView int childPosition = (Integer) v.getTag(R.id.child_position); return getChildAdapter(groupPosition).getItem(childPosition); } + }; private OnClickListener mChildClickListener = new OnClickListener() { @@ -252,11 +265,13 @@ public class BookmarkExpandableGridView extends ExpandableListView @Override public boolean onLongClick(View v) { - int groupPosition = (Integer) v.getTag(R.id.group_position); - int childPosition = (Integer) v.getTag(R.id.child_position); + ExtraDragState state = new ExtraDragState(); + state.groupPosition = (Integer) v.getTag(R.id.group_position); + state.childPosition = (Integer) v.getTag(R.id.child_position); long id = (Long) v.getTag(R.id.child_id); - Cursor c = getChildAdapter(groupPosition).getItem(childPosition); - return mDragHandler.startDrag(v, c, id); + Cursor c = getChildAdapter(state.groupPosition) + .getItem(state.childPosition); + return mDragHandler.startDrag(v, c, id, state); } }; diff --git a/src/com/android/browser/widget/BookmarkThumbnailWidgetProvider.java b/src/com/android/browser/widget/BookmarkThumbnailWidgetProvider.java index 48d7123..f3d2675 100644 --- a/src/com/android/browser/widget/BookmarkThumbnailWidgetProvider.java +++ b/src/com/android/browser/widget/BookmarkThumbnailWidgetProvider.java @@ -16,9 +16,6 @@ package com.android.browser.widget; -import com.android.browser.BrowserActivity; -import com.android.browser.R; - import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; @@ -28,6 +25,9 @@ import android.content.Intent; import android.net.Uri; import android.widget.RemoteViews; +import com.android.browser.BrowserActivity; +import com.android.browser.R; + /** * Widget that shows a preview of the user's bookmarks. */ @@ -91,7 +91,7 @@ public class BookmarkThumbnailWidgetProvider extends AppWidgetProvider { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.bookmarkthumbnailwidget); views.setOnClickPendingIntent(R.id.app_shortcut, launchBrowser); - views.setRemoteAdapter(appWidgetId, R.id.bookmarks_list, updateIntent); + views.setRemoteAdapter(R.id.bookmarks_list, updateIntent); appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.bookmarks_list); Intent ic = new Intent(context, BookmarkWidgetProxy.class); views.setPendingIntentTemplate(R.id.bookmarks_list, @@ -110,27 +110,9 @@ public class BookmarkThumbnailWidgetProvider extends AppWidgetProvider { } public static void refreshWidgets(Context context) { - refreshWidgets(context, false); + context.sendBroadcast(new Intent( + BookmarkThumbnailWidgetProvider.ACTION_BOOKMARK_APPWIDGET_UPDATE, + null, context, BookmarkThumbnailWidgetProvider.class)); } - public static void refreshWidgets(Context context, boolean zeroState) { - if (zeroState) { - final Context appContext = context.getApplicationContext(); - new Thread() { - @Override - public void run() { - AppWidgetManager wm = AppWidgetManager.getInstance(appContext); - int[] ids = wm.getAppWidgetIds(getComponentName(appContext)); - for (int id : ids) { - BookmarkThumbnailWidgetService.clearWidgetState(appContext, id); - } - refreshWidgets(appContext, false); - } - }.start(); - } else { - context.sendBroadcast(new Intent( - BookmarkThumbnailWidgetProvider.ACTION_BOOKMARK_APPWIDGET_UPDATE, - null, context, BookmarkThumbnailWidgetProvider.class)); - } - } } diff --git a/src/com/android/browser/widget/BookmarkThumbnailWidgetService.java b/src/com/android/browser/widget/BookmarkThumbnailWidgetService.java index 675cdd9..7d174ee 100644 --- a/src/com/android/browser/widget/BookmarkThumbnailWidgetService.java +++ b/src/com/android/browser/widget/BookmarkThumbnailWidgetService.java @@ -16,10 +16,6 @@ package com.android.browser.widget; -import com.android.browser.BookmarkUtils; -import com.android.browser.BrowserActivity; -import com.android.browser.R; - import android.appwidget.AppWidgetManager; import android.content.ContentUris; import android.content.Context; @@ -40,9 +36,12 @@ import android.util.Log; import android.widget.RemoteViews; import android.widget.RemoteViewsService; +import com.android.browser.BrowserActivity; +import com.android.browser.R; +import com.android.browser.provider.BrowserProvider2; + import java.io.File; import java.io.FilenameFilter; -import java.util.Arrays; import java.util.HashSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -110,11 +109,11 @@ public class BookmarkThumbnailWidgetService extends RemoteViewsService { } } - static void clearWidgetState(Context context, int widgetId) { + static void setupWidgetState(Context context, int widgetId, long rootFolder) { SharedPreferences pref = getWidgetState(context, widgetId); pref.edit() - .remove(STATE_CURRENT_FOLDER) - .remove(STATE_ROOT_FOLDER) + .putLong(STATE_CURRENT_FOLDER, rootFolder) + .putLong(STATE_ROOT_FOLDER, rootFolder) .commit(); } @@ -311,8 +310,8 @@ public class BookmarkThumbnailWidgetService extends RemoteViewsService { long token = Binder.clearCallingIdentity(); syncState(); if (mRootFolder < 0 || mCurrentFolder < 0) { - // Our state has been zero'd, reset (account change most likely) - mRootFolder = getRootFolder(); + // This shouldn't happen, but JIC default to the local account + mRootFolder = BrowserProvider2.FIXED_ID_ROOT; mCurrentFolder = mRootFolder; saveState(); } @@ -327,27 +326,12 @@ public class BookmarkThumbnailWidgetService extends RemoteViewsService { } } - long getRootFolder() { - Uri uri = Uri.withAppendedPath( - BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, "id"); - uri = BookmarkUtils.addAccountInfo(mContext, uri.buildUpon()).build(); - Cursor c = mContext.getContentResolver().query( - uri, null, null, null, null); - try { - c.moveToFirst(); - return c.getLong(0); - } finally { - c.close(); - } - } - void loadBookmarks() { resetBookmarks(); Uri uri = ContentUris.withAppendedId( BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, mCurrentFolder); - uri = BookmarkUtils.addAccountInfo(mContext, uri.buildUpon()).build(); mBookmarks = mContext.getContentResolver().query(uri, PROJECTION, null, null, null); if (mCurrentFolder != mRootFolder) { diff --git a/src/com/android/browser/widget/BookmarkWidgetConfigure.java b/src/com/android/browser/widget/BookmarkWidgetConfigure.java new file mode 100644 index 0000000..4231f37 --- /dev/null +++ b/src/com/android/browser/widget/BookmarkWidgetConfigure.java @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.browser.widget; + +import android.app.ListActivity; +import android.app.LoaderManager.LoaderCallbacks; +import android.appwidget.AppWidgetManager; +import android.content.Intent; +import android.content.Loader; +import android.database.Cursor; +import android.os.Bundle; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.ArrayAdapter; +import android.widget.ListView; + +import com.android.browser.AddBookmarkPage.AccountsLoader; +import com.android.browser.AddBookmarkPage.BookmarkAccount; +import com.android.browser.R; +import com.android.browser.provider.BrowserProvider2; + +public class BookmarkWidgetConfigure extends ListActivity + implements OnClickListener, LoaderCallbacks<Cursor> { + + static final int LOADER_ACCOUNTS = 1; + + private ArrayAdapter<BookmarkAccount> mAccountAdapter; + private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setResult(RESULT_CANCELED); + setVisible(false); + setContentView(R.layout.widget_account_selection); + findViewById(R.id.cancel).setOnClickListener(this); + mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this, + android.R.layout.simple_list_item_1); + setListAdapter(mAccountAdapter); + Intent intent = getIntent(); + Bundle extras = intent.getExtras(); + if (extras != null) { + mAppWidgetId = extras.getInt( + AppWidgetManager.EXTRA_APPWIDGET_ID, + AppWidgetManager.INVALID_APPWIDGET_ID); + } + if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { + finish(); + } else { + getLoaderManager().initLoader(LOADER_ACCOUNTS, null, this); + } + } + + @Override + public void onClick(View v) { + finish(); + } + + @Override + protected void onListItemClick(ListView l, View v, int position, long id) { + BookmarkAccount account = mAccountAdapter.getItem(position); + pickAccount(account.rootFolderId); + } + + @Override + public Loader<Cursor> onCreateLoader(int id, Bundle args) { + return new AccountsLoader(this); + } + + void pickAccount(long rootId) { + BookmarkThumbnailWidgetService.setupWidgetState(this, mAppWidgetId, rootId); + Intent result = new Intent(); + result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); + setResult(RESULT_OK, result); + finish(); + } + + @Override + public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { + if (cursor == null || cursor.getCount() <= 1) { + // We always have the local account, so if count == 1 it must + // be the local account + pickAccount(BrowserProvider2.FIXED_ID_ROOT); + } else { + mAccountAdapter.clear(); + while (cursor.moveToNext()) { + mAccountAdapter.add(new BookmarkAccount(this, cursor)); + } + setVisible(true); + } + getLoaderManager().destroyLoader(LOADER_ACCOUNTS); + } + + @Override + public void onLoaderReset(Loader<Cursor> loader) { + // Don't care + } + +} |