Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/preferences/website/UsbChooserPreferences.java

Issue 2159533002: Add a section to Site Settings listing USB devices. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@android_usb_settings
Patch Set: Add comment explaining the automatic dismiss behavior. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.preferences.website;
6
7 import android.os.Bundle;
8 import android.preference.Preference;
9 import android.preference.PreferenceFragment;
10 import android.support.v4.view.MenuItemCompat;
11 import android.support.v7.widget.SearchView;
12 import android.view.Menu;
13 import android.view.MenuInflater;
14 import android.view.MenuItem;
15 import android.view.inputmethod.EditorInfo;
16 import android.widget.ListView;
17 import android.widget.TextView;
18
19 import org.chromium.chrome.R;
20 import org.chromium.chrome.browser.help.HelpAndFeedback;
21 import org.chromium.chrome.browser.profiles.Profile;
22
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Map;
28
29 /**
30 * Shows a list of USB devices that the user has granted websites permission to access.
31 *
32 * When the user selects an item UsbDevicePreferences is launched to show which sites have access
33 * to the device.
34 */
35 public class UsbChooserPreferences
36 extends PreferenceFragment implements Preference.OnPreferenceClickListen er {
37 // The site settings category we are showing.
38 private SiteSettingsCategory mCategory;
39 // Multiple sites may have access to the same device. A canonical UsbInfo fo r each device is
40 // therefore arbitrarily chosen to represent it. |mPermissionsByObject| list s all of the
41 // individual permission grants for that device. |mSitesByObject| lists all of the sites with
42 // access to that device.
43 //
44 // UsbInfo's hashCode() and equals() methods compare only the device it repr esents, not the
45 // origin to which permission was granted.
46 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.
47 private Map<UsbInfo, LinkedList<Website>> mSitesByObject = new HashMap<>();
48 // The view to show when the list is empty.
49 private TextView mEmptyView;
50 // The view for searching the list of items.
51 private SearchView mSearchView;
52 // If not blank, represents a substring to use to search for site names.
53 private String mSearch = "";
54
55 @Override
56 public void onActivityCreated(Bundle savedInstanceState) {
57 addPreferencesFromResource(R.xml.single_chooser_preferences);
58 ListView listView = (ListView) getView().findViewById(android.R.id.list) ;
59 mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
60 listView.setEmptyView(mEmptyView);
61 listView.setDivider(null);
62
63 String category = getArguments().getString(SingleCategoryPreferences.EXT RA_CATEGORY);
64 mCategory = SiteSettingsCategory.fromString(category);
65 String title = getArguments().getString(SingleCategoryPreferences.EXTRA_ TITLE);
66 getActivity().setTitle(title);
67
68 setHasOptionsMenu(true);
69
70 super.onActivityCreated(savedInstanceState);
71 }
72
73 @Override
74 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
75 menu.clear();
76 inflater.inflate(R.menu.website_preferences_menu, menu);
77
78 MenuItem searchItem = menu.findItem(R.id.search);
79 mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
80 mSearchView.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN);
81 SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQuer yTextListener() {
82 @Override
83 public boolean onQueryTextSubmit(String query) {
84 return true;
85 }
86
87 @Override
88 public boolean onQueryTextChange(String query) {
89 // Make search case-insensitive.
90 query = query.toLowerCase();
91
92 if (query.equals(mSearch)) return true;
93
94 mSearch = query;
95 getInfo();
96 return true;
97 }
98 };
99 mSearchView.setOnQueryTextListener(queryTextListener);
100
101 MenuItem help =
102 menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.str ing.menu_help);
103 help.setIcon(R.drawable.ic_help_and_feedback);
104 }
105
106 @Override
107 public boolean onOptionsItemSelected(MenuItem item) {
108 if (item.getItemId() == R.id.menu_id_targeted_help) {
109 HelpAndFeedback.getInstance(getActivity())
110 .show(getActivity(), getString(R.string.help_context_setting s),
111 Profile.getLastUsedProfile(), null);
112 return true;
113 }
114 return false;
115 }
116
117 @Override
118 public void onResume() {
119 super.onResume();
120
121 getInfo();
122 }
123
124 private class ResultsPopulator implements WebsitePermissionsFetcher.WebsiteP ermissionsCallback {
125 @Override
126 public void onWebsitePermissionsAvailable(Collection<Website> sites) {
127 // This method may be called after the activity has been destroyed.
128 // In that case, bail out.
129 if (getActivity() == null) return;
130
131 mPermissionsByObject.clear();
132 mSitesByObject.clear();
133 for (Website site : sites) {
134 for (UsbInfo info : site.getUsbInfo()) {
135 if (mSearch.isEmpty() || info.getName().toLowerCase().contai ns(mSearch)) {
136 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
137 if (permissionSet == null) {
138 permissionSet = new LinkedList<>();
139 mPermissionsByObject.put(info, permissionSet);
140 }
141 permissionSet.add(info);
142 LinkedList<Website> sitesSet = mSitesByObject.get(info);
143 if (sitesSet == null) {
144 sitesSet = new LinkedList<>();
145 mSitesByObject.put(info, sitesSet);
146 }
147 sitesSet.add(site);
148 }
149 }
150 }
151
152 resetList();
153 }
154 }
155
156 /**
157 * Refreshes |mPermissionsByObject| and |mSitesByObject| with new data from native.
158 *
159 * resetList() is called to refresh the view when the data is ready.
160 */
161 private void getInfo() {
162 WebsitePermissionsFetcher fetcher = new WebsitePermissionsFetcher(new Re sultsPopulator());
163 fetcher.fetchPreferencesForCategory(mCategory);
164 }
165
166 private void resetList() {
167 getPreferenceScreen().removeAll();
168 addPreferencesFromResource(R.xml.single_chooser_preferences);
169
170 if (mPermissionsByObject.isEmpty() && mSearch.isEmpty() && mEmptyView != null) {
171 mEmptyView.setText(R.string.website_settings_usb_no_devices);
172 }
173
174 for (Map.Entry<UsbInfo, LinkedList<UsbInfo>> entry : mPermissionsByObjec t.entrySet()) {
175 Preference preference = new Preference(getActivity());
176 preference.getExtras().putSerializable(
177 UsbDevicePreferences.EXTRA_USB_INFOS, entry.getValue());
178 preference.setIcon(R.drawable.settings_usb);
179 preference.setOnPreferenceClickListener(this);
180 preference.setTitle(entry.getKey().getName());
181 getPreferenceScreen().addPreference(preference);
182 }
183 }
184
185 @Override
186 public boolean onPreferenceClick(Preference preference) {
187 Bundle extras = preference.getExtras();
188 @SuppressWarnings("unchecked")
189 List<UsbInfo> info =
190 (List<UsbInfo>) extras.getSerializable(UsbDevicePreferences.EXTR A_USB_INFOS);
191 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.
192 SingleCategoryPreferences.EXTRA_TITLE, getActivity().getTitle(). toString());
193 extras.putInt(UsbDevicePreferences.EXTRA_CATEGORY, mCategory.toContentSe ttingsType());
194 extras.putSerializable(
195 UsbDevicePreferences.EXTRA_SITES, mSitesByObject.get(info.get(0) ));
196 preference.setFragment(
197 "org.chromium.chrome.browser.preferences.website.UsbDevicePrefer ences");
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.
198 return false;
199 }
200 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698