OLD | NEW |
---|---|
(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 items that the user has granted websites permission to access . | |
31 * | |
32 * For example, this could show all USB devices the user has granted sites permi ssion to connect to | |
33 * or all Bluetooth devices that have been paired. When the user selects an item | |
34 * SingleObjectPreferences is launched to show which sites have access to the it em. | |
35 */ | |
36 public class SingleChooserPreferences | |
37 extends PreferenceFragment implements Preference.OnPreferenceClickListen er { | |
38 // The site settings category we are showing. | |
39 private SiteSettingsCategory mCategory; | |
40 // Multiple sites may have access to the same object. A canonical UsbInfo fo r each object is | |
41 // therefore arbitrarily chosen to represent the object. |mPermissionsByObje ct| lists all of | |
42 // the individual permission grants for that object. |mSitesByObject| lists all of the sites | |
43 // with access to that object. | |
44 // | |
45 // TODO: UsbInfo will need to be replaced with something more generic when o ther types of | |
Theresa
2016/08/05 21:29:51
I was thinking through this more and wondering - i
| |
46 // objects can be displayed by this UI. The native equivalent is ChooserCont extBase::Object. | |
47 // | |
48 // UsbInfo's hashCode() and equals() methods compare only the object they re present, not the | |
49 // origin to which permission was granted. | |
50 private Map<UsbInfo, LinkedList<UsbInfo>> mPermissionsByObject = new HashMap <>(); | |
51 private Map<UsbInfo, LinkedList<Website>> mSitesByObject = new HashMap<>(); | |
52 // The view to show when the list is empty. | |
53 private TextView mEmptyView; | |
54 // The view for searching the list of items. | |
55 private SearchView mSearchView; | |
56 // If not blank, represents a substring to use to search for site names. | |
57 private String mSearch = ""; | |
58 | |
59 @Override | |
60 public void onActivityCreated(Bundle savedInstanceState) { | |
61 addPreferencesFromResource(R.xml.single_chooser_preferences); | |
62 ListView listView = (ListView) getView().findViewById(android.R.id.list) ; | |
63 mEmptyView = (TextView) getView().findViewById(android.R.id.empty); | |
64 listView.setEmptyView(mEmptyView); | |
65 listView.setDivider(null); | |
66 | |
67 String category = getArguments().getString(SingleCategoryPreferences.EXT RA_CATEGORY); | |
68 mCategory = SiteSettingsCategory.fromString(category); | |
69 String title = getArguments().getString(SingleCategoryPreferences.EXTRA_ TITLE); | |
70 getActivity().setTitle(title); | |
71 | |
72 setHasOptionsMenu(true); | |
73 | |
74 super.onActivityCreated(savedInstanceState); | |
75 } | |
76 | |
77 @Override | |
78 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | |
79 menu.clear(); | |
80 inflater.inflate(R.menu.website_preferences_menu, menu); | |
81 | |
82 MenuItem searchItem = menu.findItem(R.id.search); | |
83 mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); | |
84 mSearchView.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN); | |
85 SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQuer yTextListener() { | |
86 @Override | |
87 public boolean onQueryTextSubmit(String query) { | |
88 return true; | |
89 } | |
90 | |
91 @Override | |
92 public boolean onQueryTextChange(String query) { | |
93 // Make search case-insensitive. | |
94 query = query.toLowerCase(); | |
95 | |
96 if (query.equals(mSearch)) return true; | |
97 | |
98 mSearch = query; | |
99 getInfo(); | |
100 return true; | |
101 } | |
102 }; | |
103 mSearchView.setOnQueryTextListener(queryTextListener); | |
104 | |
105 MenuItem help = | |
106 menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.str ing.menu_help); | |
107 help.setIcon(R.drawable.ic_help_and_feedback); | |
108 } | |
109 | |
110 @Override | |
111 public boolean onOptionsItemSelected(MenuItem item) { | |
112 if (item.getItemId() == R.id.menu_id_targeted_help) { | |
113 HelpAndFeedback.getInstance(getActivity()) | |
114 .show(getActivity(), getString(R.string.help_context_setting s), | |
115 Profile.getLastUsedProfile(), null); | |
116 return true; | |
117 } | |
118 return false; | |
119 } | |
120 | |
121 @Override | |
122 public void onResume() { | |
123 super.onResume(); | |
124 | |
125 getInfo(); | |
126 } | |
127 | |
128 private class ResultsPopulator implements WebsitePermissionsFetcher.WebsiteP ermissionsCallback { | |
129 @Override | |
130 public void onWebsitePermissionsAvailable(Collection<Website> sites) { | |
131 // This method may be called after the activity has been destroyed. | |
132 // In that case, bail out. | |
133 if (getActivity() == null) return; | |
134 | |
135 mPermissionsByObject.clear(); | |
136 mSitesByObject.clear(); | |
137 for (Website site : sites) { | |
138 for (UsbInfo info : site.getUsbInfo()) { | |
139 if (mSearch.isEmpty() || info.getName().toLowerCase().contai ns(mSearch)) { | |
140 LinkedList<UsbInfo> permissionSet = mPermissionsByObject .get(info); | |
141 if (permissionSet == null) { | |
142 permissionSet = new LinkedList<>(); | |
143 mPermissionsByObject.put(info, permissionSet); | |
144 } | |
145 permissionSet.add(info); | |
146 LinkedList<Website> sitesSet = mSitesByObject.get(info); | |
147 if (sitesSet == null) { | |
148 sitesSet = new LinkedList<>(); | |
149 mSitesByObject.put(info, sitesSet); | |
150 } | |
151 sitesSet.add(site); | |
152 } | |
153 } | |
154 } | |
155 | |
156 resetList(); | |
157 } | |
158 } | |
159 | |
160 /** | |
161 * Refreshes |mPermissionsByObject| and |mSitesByObject| with new data from native. | |
162 * | |
163 * resetList() is called to refresh the view when the data is ready. | |
164 */ | |
165 private void getInfo() { | |
166 WebsitePermissionsFetcher fetcher = new WebsitePermissionsFetcher(new Re sultsPopulator()); | |
167 fetcher.fetchPreferencesForCategory(mCategory); | |
168 } | |
169 | |
170 private void resetList() { | |
171 getPreferenceScreen().removeAll(); | |
172 addPreferencesFromResource(R.xml.single_chooser_preferences); | |
173 | |
174 if (mSitesByObject.isEmpty()) { | |
175 if (mEmptyView != null) { | |
176 mEmptyView.setText(R.string.no_saved_website_settings); | |
177 } | |
178 return; | |
179 } | |
180 | |
181 for (Map.Entry<UsbInfo, LinkedList<UsbInfo>> entry : mPermissionsByObjec t.entrySet()) { | |
182 Preference preference = new Preference(getActivity()); | |
183 preference.getExtras().putSerializable( | |
184 SingleObjectPreferences.EXTRA_USB_INFOS, entry.getValue()); | |
185 preference.setIcon(R.drawable.settings_usb); | |
186 preference.setOnPreferenceClickListener(this); | |
187 preference.setTitle(entry.getKey().getName()); | |
188 getPreferenceScreen().addPreference(preference); | |
189 } | |
190 } | |
191 | |
192 @Override | |
193 public boolean onPreferenceClick(Preference preference) { | |
194 Bundle extras = preference.getExtras(); | |
195 @SuppressWarnings("unchecked") | |
196 List<UsbInfo> info = | |
197 (List<UsbInfo>) extras.getSerializable(SingleObjectPreferences.E XTRA_USB_INFOS); | |
198 extras.putString( | |
199 SingleCategoryPreferences.EXTRA_TITLE, getActivity().getTitle(). toString()); | |
200 extras.putInt(SingleObjectPreferences.EXTRA_CATEGORY, mCategory.toConten tSettingsType()); | |
201 extras.putSerializable( | |
202 SingleObjectPreferences.EXTRA_SITES, mSitesByObject.get(info.get (0))); | |
203 preference.setFragment( | |
204 "org.chromium.chrome.browser.preferences.website.SingleObjectPre ferences"); | |
205 return false; | |
206 } | |
207 } | |
OLD | NEW |