Index: chrome/android/java/src/org/chromium/chrome/browser/preferences/website/UsbChooserPreferences.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/UsbChooserPreferences.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/UsbChooserPreferences.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c1f078d3f1c6b84293091fc2b5eb32b1c545b597 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/UsbChooserPreferences.java |
@@ -0,0 +1,200 @@ |
+// 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 USB devices that the user has granted websites permission to access. |
+ * |
+ * When the user selects an item UsbDevicePreferences is launched to show which sites have access |
+ * to the device. |
+ */ |
+public class UsbChooserPreferences |
+ extends PreferenceFragment implements Preference.OnPreferenceClickListener { |
+ // The site settings category we are showing. |
+ private SiteSettingsCategory mCategory; |
+ // Multiple sites may have access to the same device. A canonical UsbInfo for each device is |
+ // therefore arbitrarily chosen to represent it. |mPermissionsByObject| lists all of the |
+ // individual permission grants for that device. |mSitesByObject| lists all of the sites with |
+ // access to that device. |
+ // |
+ // UsbInfo's hashCode() and equals() methods compare only the device it represents, not the |
+ // origin to which permission was granted. |
+ private Map<UsbInfo, LinkedList<UsbInfo>> mPermissionsByObject = new HashMap<>(); |
Ted C
2016/08/15 20:31:59
unless you need some property of LinkedList, it'd
Reilly Grant (use Gerrit)
2016/08/16 17:41:59
It needs to be LinkedList so that it's Serializabl
Ted C
2016/08/17 14:49:13
I guess the fundamental question I have is why Lin
Reilly Grant (use Gerrit)
2016/08/17 20:04:22
Done.
|
+ 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); |
Ted C
2016/08/15 20:31:59
naming nit, but it's odd to have these be called p
Reilly Grant (use Gerrit)
2016/08/16 17:41:59
Agreed, it was a HashSet in an earlier version of
|
+ 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 native. |
+ * |
+ * 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 (mPermissionsByObject.isEmpty() && mSearch.isEmpty() && mEmptyView != null) { |
+ mEmptyView.setText(R.string.website_settings_usb_no_devices); |
+ } |
+ |
+ for (Map.Entry<UsbInfo, LinkedList<UsbInfo>> entry : mPermissionsByObject.entrySet()) { |
+ Preference preference = new Preference(getActivity()); |
+ preference.getExtras().putSerializable( |
+ UsbDevicePreferences.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(UsbDevicePreferences.EXTRA_USB_INFOS); |
+ extras.putString( |
Ted C
2016/08/15 20:31:59
any reason we don't set these extras when we are b
Reilly Grant (use Gerrit)
2016/08/16 17:41:59
To avoid serializing a bunch of stuff that won't b
Ted C
2016/08/17 14:49:13
Does it actually get serialized though? Just addi
Reilly Grant (use Gerrit)
2016/08/17 20:04:22
Done.
|
+ SingleCategoryPreferences.EXTRA_TITLE, getActivity().getTitle().toString()); |
+ extras.putInt(UsbDevicePreferences.EXTRA_CATEGORY, mCategory.toContentSettingsType()); |
+ extras.putSerializable( |
+ UsbDevicePreferences.EXTRA_SITES, mSitesByObject.get(info.get(0))); |
+ preference.setFragment( |
+ "org.chromium.chrome.browser.preferences.website.UsbDevicePreferences"); |
Ted C
2016/08/15 20:31:59
can we use UsbDevicePreferences.class.getFullName(
Reilly Grant (use Gerrit)
2016/08/16 17:41:59
Done.
|
+ return false; |
+ } |
+} |