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

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: Iterate ArrayLists by index. 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.util.Pair;
13 import android.view.Menu;
14 import android.view.MenuInflater;
15 import android.view.MenuItem;
16 import android.view.inputmethod.EditorInfo;
17 import android.widget.ListView;
18 import android.widget.TextView;
19
20 import org.chromium.chrome.R;
21 import org.chromium.chrome.browser.help.HelpAndFeedback;
22 import org.chromium.chrome.browser.profiles.Profile;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
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 extends PreferenceFragment {
36 // The site settings category we are showing.
37 private SiteSettingsCategory mCategory;
38 // Multiple sites may have access to the same device. A canonical UsbInfo fo r each device is
39 // therefore arbitrarily chosen to represent it.
40 private Map<String, Pair<ArrayList<UsbInfo>, ArrayList<Website>>> mPermissio nsByObject =
41 new HashMap<>();
42 // The view to show when the list is empty.
43 private TextView mEmptyView;
44 // The view for searching the list of items.
45 private SearchView mSearchView;
46 // If not blank, represents a substring to use to search for site names.
47 private String mSearch = "";
48
49 @Override
50 public void onActivityCreated(Bundle savedInstanceState) {
51 addPreferencesFromResource(R.xml.usb_chooser_preferences);
52 ListView listView = (ListView) getView().findViewById(android.R.id.list) ;
53 mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
54 listView.setEmptyView(mEmptyView);
55 listView.setDivider(null);
56
57 String category = getArguments().getString(SingleCategoryPreferences.EXT RA_CATEGORY);
58 mCategory = SiteSettingsCategory.fromString(category);
59 String title = getArguments().getString(SingleCategoryPreferences.EXTRA_ TITLE);
60 getActivity().setTitle(title);
61
62 setHasOptionsMenu(true);
63
64 super.onActivityCreated(savedInstanceState);
65 }
66
67 @Override
68 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
69 menu.clear();
70 inflater.inflate(R.menu.website_preferences_menu, menu);
71
72 MenuItem searchItem = menu.findItem(R.id.search);
73 mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
74 mSearchView.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN);
75 SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQuer yTextListener() {
76 @Override
77 public boolean onQueryTextSubmit(String query) {
78 return true;
79 }
80
81 @Override
82 public boolean onQueryTextChange(String query) {
83 // Make search case-insensitive.
84 query = query.toLowerCase();
85
86 if (query.equals(mSearch)) return true;
87
88 mSearch = query;
89 getInfo();
90 return true;
91 }
92 };
93 mSearchView.setOnQueryTextListener(queryTextListener);
94
95 MenuItem help =
96 menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.str ing.menu_help);
97 help.setIcon(R.drawable.ic_help_and_feedback);
98 }
99
100 @Override
101 public boolean onOptionsItemSelected(MenuItem item) {
102 if (item.getItemId() == R.id.menu_id_targeted_help) {
103 HelpAndFeedback.getInstance(getActivity())
104 .show(getActivity(), getString(R.string.help_context_setting s),
105 Profile.getLastUsedProfile(), null);
106 return true;
107 }
108 return false;
109 }
110
111 @Override
112 public void onResume() {
113 super.onResume();
114
115 getInfo();
116 }
117
118 private class ResultsPopulator implements WebsitePermissionsFetcher.WebsiteP ermissionsCallback {
119 @Override
120 public void onWebsitePermissionsAvailable(Collection<Website> sites) {
121 // This method may be called after the activity has been destroyed.
122 // In that case, bail out.
123 if (getActivity() == null) return;
124
125 mPermissionsByObject.clear();
126 for (Website site : sites) {
127 for (UsbInfo info : site.getUsbInfo()) {
128 if (mSearch.isEmpty() || info.getName().toLowerCase().contai ns(mSearch)) {
129 Pair<ArrayList<UsbInfo>, ArrayList<Website>> entry =
130 mPermissionsByObject.get(info.getObject());
131 if (entry == null) {
132 entry = Pair.create(new ArrayList<UsbInfo>(), new Ar rayList<Website>());
133 mPermissionsByObject.put(info.getObject(), entry);
134 }
135 entry.first.add(info);
136 entry.second.add(site);
137 }
138 }
139 }
140
141 resetList();
142 }
143 }
144
145 /**
146 * Refreshes |mPermissionsByObject| with new data from native.
147 *
148 * resetList() is called to refresh the view when the data is ready.
149 */
150 private void getInfo() {
151 WebsitePermissionsFetcher fetcher = new WebsitePermissionsFetcher(new Re sultsPopulator());
152 fetcher.fetchPreferencesForCategory(mCategory);
153 }
154
155 private void resetList() {
156 getPreferenceScreen().removeAll();
157 addPreferencesFromResource(R.xml.usb_chooser_preferences);
158
159 if (mPermissionsByObject.isEmpty() && mSearch.isEmpty() && mEmptyView != null) {
160 mEmptyView.setText(R.string.website_settings_usb_no_devices);
161 }
162
163 for (Pair<ArrayList<UsbInfo>, ArrayList<Website>> entry : mPermissionsBy Object.values()) {
164 Preference preference = new Preference(getActivity());
165 Bundle extras = preference.getExtras();
166 extras.putInt(UsbDevicePreferences.EXTRA_CATEGORY, mCategory.toConte ntSettingsType());
167 extras.putString(
168 SingleCategoryPreferences.EXTRA_TITLE, getActivity().getTitl e().toString());
169 extras.putSerializable(UsbDevicePreferences.EXTRA_USB_INFOS, entry.f irst);
170 extras.putSerializable(UsbDevicePreferences.EXTRA_SITES, entry.secon d);
171 preference.setIcon(R.drawable.settings_usb);
172 preference.setTitle(entry.first.get(0).getName());
173 preference.setFragment(UsbDevicePreferences.class.getCanonicalName() );
174 getPreferenceScreen().addPreference(preference);
175 }
176 }
177 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698