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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/preferences/website/UsbPreferences.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: Make new preference code USB-specific for now. 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 UsbPreferences
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 <>();
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);
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 (mSitesByObject.isEmpty()) {
171 if (mEmptyView != null) {
172 mEmptyView.setText(R.string.no_saved_website_settings);
Theresa 2016/08/09 00:24:39 "You have no saved website settings." doesn't seem
173 }
174 return;
175 }
176
177 for (Map.Entry<UsbInfo, LinkedList<UsbInfo>> entry : mPermissionsByObjec t.entrySet()) {
178 Preference preference = new Preference(getActivity());
179 preference.getExtras().putSerializable(
180 UsbDevicePreferences.EXTRA_USB_INFOS, entry.getValue());
181 preference.setIcon(R.drawable.settings_usb);
182 preference.setOnPreferenceClickListener(this);
183 preference.setTitle(entry.getKey().getName());
184 getPreferenceScreen().addPreference(preference);
185 }
186 }
187
188 @Override
189 public boolean onPreferenceClick(Preference preference) {
190 Bundle extras = preference.getExtras();
191 @SuppressWarnings("unchecked")
192 List<UsbInfo> info =
193 (List<UsbInfo>) extras.getSerializable(UsbDevicePreferences.EXTR A_USB_INFOS);
194 extras.putString(
195 SingleCategoryPreferences.EXTRA_TITLE, getActivity().getTitle(). toString());
196 extras.putInt(UsbDevicePreferences.EXTRA_CATEGORY, mCategory.toContentSe ttingsType());
197 extras.putSerializable(
198 UsbDevicePreferences.EXTRA_SITES, mSitesByObject.get(info.get(0) ));
199 preference.setFragment(
200 "org.chromium.chrome.browser.preferences.website.UsbDevicePrefer ences");
201 return false;
202 }
203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698