Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleChooserPreferences.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleChooserPreferences.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleChooserPreferences.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4d48e915cc035b3c03d4b976e9320d9c3a41bf68 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleChooserPreferences.java |
| @@ -0,0 +1,203 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.preferences.website; |
| + |
| +import android.os.Bundle; |
| +import android.preference.Preference; |
| +import android.preference.PreferenceFragment; |
| +import android.support.v4.view.MenuItemCompat; |
| +import android.support.v7.widget.SearchView; |
| +import android.view.Menu; |
| +import android.view.MenuInflater; |
| +import android.view.MenuItem; |
| +import android.view.inputmethod.EditorInfo; |
| +import android.widget.ListView; |
| +import android.widget.TextView; |
| + |
| +import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.help.HelpAndFeedback; |
| +import org.chromium.chrome.browser.profiles.Profile; |
| + |
| +import java.util.Collection; |
| +import java.util.HashMap; |
| +import java.util.LinkedList; |
| +import java.util.List; |
| +import java.util.Map; |
| + |
| +/** |
| + * Shows a list of items that the user has granted to websites through a chooser. For example, |
|
Theresa
2016/08/04 16:39:54
nit: "Shows a list of items that the user has gran
Reilly Grant (use Gerrit)
2016/08/04 19:57:10
Done.
|
| + * this could show all USB devices the user has granted sites permission to connect to or all |
| + * Bluetooth devices that have been paired. When the user selects an item, |
| + * SingleObjectPreferences is launched to show which sites have access to the item. |
| + */ |
| +public class SingleChooserPreferences |
| + extends PreferenceFragment implements Preference.OnPreferenceClickListener { |
| + // The site settings category we are showing. |
| + private SiteSettingsCategory mCategory; |
| + // Multiple sites may have access to the same object. A canonical UsbInfo for each object is |
|
Theresa
2016/08/04 01:11:47
Does using a UsbInfo object work if this is displa
Reilly Grant (use Gerrit)
2016/08/04 01:15:52
When Bluetooth starts storing permissions we will
Theresa
2016/08/04 16:39:55
That sounds like a good plan. Will you please add
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
Done.
|
| + // therefore arbitrarily chosen to represent the object. |mPermissionsByObject| lists all of |
| + // the individual permission grants for that object. |mSitesByObject| lists all of the sites |
| + // with access to that object. |
| + // |
| + // UsbInfo's hashCode() and equals() methods compare only the object they represent, not the |
| + // origin to which permission was granted. |
| + private Map<UsbInfo, LinkedList<UsbInfo>> mPermissionsByObject = new HashMap<>(); |
| + private Map<UsbInfo, LinkedList<Website>> mSitesByObject = new HashMap<>(); |
| + // The view to show when the list is empty. |
| + private TextView mEmptyView; |
| + // The view for searching the list of items. |
| + private SearchView mSearchView; |
| + // If not blank, represents a substring to use to search for site names. |
| + private String mSearch = ""; |
| + |
| + @Override |
| + public void onActivityCreated(Bundle savedInstanceState) { |
| + addPreferencesFromResource(R.xml.single_chooser_preferences); |
| + ListView listView = (ListView) getView().findViewById(android.R.id.list); |
| + mEmptyView = (TextView) getView().findViewById(android.R.id.empty); |
| + listView.setEmptyView(mEmptyView); |
| + listView.setDivider(null); |
| + |
| + String category = getArguments().getString(SingleCategoryPreferences.EXTRA_CATEGORY); |
| + mCategory = SiteSettingsCategory.fromString(category); |
| + String title = getArguments().getString(SingleCategoryPreferences.EXTRA_TITLE); |
| + getActivity().setTitle(title); |
| + |
| + setHasOptionsMenu(true); |
| + |
| + super.onActivityCreated(savedInstanceState); |
| + } |
| + |
| + @Override |
| + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| + menu.clear(); |
| + inflater.inflate(R.menu.website_preferences_menu, menu); |
| + |
| + MenuItem searchItem = menu.findItem(R.id.search); |
| + mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); |
| + mSearchView.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN); |
| + SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() { |
| + @Override |
| + public boolean onQueryTextSubmit(String query) { |
| + return true; |
| + } |
| + |
| + @Override |
| + public boolean onQueryTextChange(String query) { |
| + // Make search case-insensitive. |
| + query = query.toLowerCase(); |
| + |
| + if (query.equals(mSearch)) return true; |
| + |
| + mSearch = query; |
| + getInfo(); |
| + return true; |
| + } |
| + }; |
| + mSearchView.setOnQueryTextListener(queryTextListener); |
| + |
| + MenuItem help = |
| + menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.string.menu_help); |
| + help.setIcon(R.drawable.ic_help_and_feedback); |
| + } |
| + |
| + @Override |
| + public boolean onOptionsItemSelected(MenuItem item) { |
| + if (item.getItemId() == R.id.menu_id_targeted_help) { |
| + HelpAndFeedback.getInstance(getActivity()) |
| + .show(getActivity(), getString(R.string.help_context_settings), |
| + Profile.getLastUsedProfile(), null); |
| + return true; |
| + } |
| + return false; |
| + } |
| + |
| + @Override |
| + public void onResume() { |
| + super.onResume(); |
| + |
| + getInfo(); |
| + } |
| + |
| + private class ResultsPopulator implements WebsitePermissionsFetcher.WebsitePermissionsCallback { |
| + @Override |
| + public void onWebsitePermissionsAvailable(Collection<Website> sites) { |
| + // This method may be called after the activity has been destroyed. |
| + // In that case, bail out. |
| + if (getActivity() == null) return; |
| + |
| + mPermissionsByObject.clear(); |
| + mSitesByObject.clear(); |
| + for (Website site : sites) { |
| + for (UsbInfo info : site.getUsbInfo()) { |
| + if (mSearch.isEmpty() || info.getName().toLowerCase().contains(mSearch)) { |
| + LinkedList<UsbInfo> permissionSet = mPermissionsByObject.get(info); |
| + if (permissionSet == null) { |
| + permissionSet = new LinkedList<>(); |
| + mPermissionsByObject.put(info, permissionSet); |
| + } |
| + permissionSet.add(info); |
| + LinkedList<Website> sitesSet = mSitesByObject.get(info); |
| + if (sitesSet == null) { |
| + sitesSet = new LinkedList<>(); |
| + mSitesByObject.put(info, sitesSet); |
| + } |
| + sitesSet.add(site); |
| + } |
| + } |
| + } |
| + |
| + resetList(); |
| + } |
| + } |
| + |
| + /** |
| + * Refreshes |mPermissionsByObject| and |mSitesByObject| with new data from C++. |
|
Theresa
2016/08/04 16:39:55
nit: s/C++/native
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
Done.
|
| + * |
| + * resetList() is called to refresh the view when the data is ready. |
| + */ |
| + private void getInfo() { |
| + WebsitePermissionsFetcher fetcher = new WebsitePermissionsFetcher(new ResultsPopulator()); |
| + fetcher.fetchPreferencesForCategory(mCategory); |
| + } |
| + |
| + private void resetList() { |
| + getPreferenceScreen().removeAll(); |
| + addPreferencesFromResource(R.xml.single_chooser_preferences); |
| + |
| + if (mSitesByObject.isEmpty()) { |
|
Theresa
2016/08/04 16:39:55
Does it matter if this is mSitesByObject vs mPermi
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
No, the key sets should be equal.
|
| + if (mEmptyView != null) { |
| + mEmptyView.setText(R.string.no_saved_website_settings); |
| + } |
| + return; |
| + } |
| + |
| + for (Map.Entry<UsbInfo, LinkedList<UsbInfo>> entry : mPermissionsByObject.entrySet()) { |
| + Preference preference = new Preference(getActivity()); |
| + preference.getExtras().putSerializable( |
| + SingleObjectPreferences.EXTRA_USB_INFOS, entry.getValue()); |
| + preference.setIcon(R.drawable.settings_usb); |
| + preference.setOnPreferenceClickListener(this); |
| + preference.setTitle(entry.getKey().getName()); |
| + getPreferenceScreen().addPreference(preference); |
| + } |
| + } |
| + |
| + @Override |
| + public boolean onPreferenceClick(Preference preference) { |
| + Bundle extras = preference.getExtras(); |
| + @SuppressWarnings("unchecked") |
| + List<UsbInfo> info = |
| + (List<UsbInfo>) extras.getSerializable(SingleObjectPreferences.EXTRA_USB_INFOS); |
| + extras.putString( |
| + SingleCategoryPreferences.EXTRA_TITLE, getActivity().getTitle().toString()); |
| + extras.putInt(SingleObjectPreferences.EXTRA_CATEGORY, mCategory.toContentSettingsType()); |
| + extras.putSerializable( |
| + SingleObjectPreferences.EXTRA_SITES, mSitesByObject.get(info.get(0))); |
| + preference.setFragment( |
| + "org.chromium.chrome.browser.preferences.website.SingleObjectPreferences"); |
| + return false; |
| + } |
| +} |