Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleObjectPreferences.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleObjectPreferences.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleObjectPreferences.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..49643147cee4f818d293d538e5d386ffa12589a2 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleObjectPreferences.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.preference.PreferenceScreen; |
| +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 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.LinkedList; |
| +import java.util.List; |
| + |
| +/** |
| + * Shows the list of sites that the user has granted access to a particular |
| + * item, for example a USB or Bluetooth device. |
| + */ |
| +public class SingleObjectPreferences |
| + extends PreferenceFragment implements Preference.OnPreferenceClickListener { |
| + public static final String EXTRA_USB_INFOS = "org.chromium.chrome.preferences.usb_infos"; |
| + public static final String EXTRA_SITES = "org.chromium.chrome.preferences.site_set"; |
| + public static final String EXTRA_CATEGORY = |
| + "org.chromium.chrome.preferences.content_settings_type"; |
| + |
| + public static final String PREF_OBJECT_NAME = "object_name"; |
| + |
| + // The site settings category we are showing. |
| + private SiteSettingsCategory mCategory; |
| + // Canonical example of the USB device being examined. |
| + private UsbInfo mUsbInfo; |
| + // All of the USB device permission entries matching the canonical device. |
| + private List<UsbInfo> mUsbInfos; |
| + // The set of sites to display. |
| + private List<Website> mSites; |
| + // 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 |
| + @SuppressWarnings("unchecked") |
| + public void onActivityCreated(Bundle savedInstanceState) { |
| + addPreferencesFromResource(R.xml.single_object_preferences); |
| + int contentSettingsType = getArguments().getInt(EXTRA_CATEGORY); |
| + mCategory = SiteSettingsCategory.fromContentSettingsType(contentSettingsType); |
| + mUsbInfos = (List<UsbInfo>) getArguments().getSerializable(EXTRA_USB_INFOS); |
| + mUsbInfo = mUsbInfos.get(0); |
| + mSites = (List<Website>) getArguments().getSerializable(EXTRA_SITES); |
| + String title = getArguments().getString(SingleCategoryPreferences.EXTRA_TITLE); |
| + if (title != null) getActivity().setTitle(title); |
| + |
| + setHasOptionsMenu(true); |
| + |
| + super.onActivityCreated(savedInstanceState); |
| + } |
| + |
| + @Override |
| + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
|
Theresa
2016/08/04 16:39:55
Would it make sense to pull this out into a super
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
Can I do that in a followup patch? I don't want to
Theresa
2016/08/04 20:22:28
sg
|
| + 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(); |
| + |
| + // First time this activity is launched the intent may have already included the set of |
| + // sites with permission for this device. |
|
Theresa
2016/08/04 16:39:55
Does this comment go with resetList() (if the set
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
I've removed this comment since the one down in re
|
| + if (mSites == null) { |
| + getInfo(); |
| + } else { |
| + resetList(); |
| + } |
| + } |
| + |
| + @Override |
| + public boolean onPreferenceClick(Preference preference) { |
| + if (PREF_OBJECT_NAME.equals(preference.getKey())) { |
| + for (UsbInfo info : mUsbInfos) info.revoke(); |
| + getActivity().finish(); |
| + return true; |
| + } |
| + |
| + if (preference instanceof WebsitePreference) { |
| + WebsitePreference websitePreference = (WebsitePreference) preference; |
| + websitePreference.getExtras().putSerializable( |
| + SingleWebsitePreferences.EXTRA_SITE, websitePreference.site()); |
| + websitePreference.setFragment( |
| + "org.chromium.chrome.browser.preferences.website.SingleWebsitePreferences"); |
| + } |
| + return false; |
| + } |
| + |
| + 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; |
| + |
| + mUsbInfos.clear(); |
| + mSites = new LinkedList<Website>(); |
| + for (Website site : sites) { |
| + if (mSearch.isEmpty() || site.getTitle().toLowerCase().contains(mSearch)) { |
| + for (UsbInfo info : site.getUsbInfo()) { |
| + if (info.equals(mUsbInfo)) { |
| + mUsbInfos.add(info); |
| + mSites.add(site); |
| + } |
| + } |
| + } |
| + } |
| + |
| + if (mSites.isEmpty() && mSearch.isEmpty()) { |
| + getActivity().finish(); |
|
Theresa
2016/08/04 16:39:55
Should we display something if there are no sites
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
This will happen if the user returns to this activ
Theresa
2016/08/04 20:22:28
Just to check my understanding, they're here -> si
Reilly Grant (use Gerrit)
2016/08/04 21:30:25
That's correct. In theory the user could see the o
|
| + } else { |
| + resetList(); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * Refreshes the list of sites with access to the object being examined. |
| + * |
| + * 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_object_preferences); |
| + ListView listView = (ListView) getView().findViewById(android.R.id.list); |
| + listView.setDivider(null); |
|
Theresa
2016/08/04 16:39:55
Does this need to be called every time resetList()
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
It probably can be.
|
| + |
| + PreferenceScreen preferenceScreen = getPreferenceScreen(); |
| + Preference header = preferenceScreen.findPreference(PREF_OBJECT_NAME); |
| + header.setTitle(mUsbInfo.getName()); |
| + header.setOnPreferenceClickListener(this); |
| + |
| + for (Website site : mSites) { |
| + Preference preference = new WebsitePreference(getActivity(), site, mCategory); |
| + preference.setOnPreferenceClickListener(this); |
| + preferenceScreen.addPreference(preference); |
| + } |
| + |
| + // Force this list to be reloaded if the activity is resumed. |
| + mSites = null; |
| + } |
| +} |