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.preference.PreferenceScreen; | |
11 import android.support.v4.view.MenuItemCompat; | |
12 import android.support.v7.widget.SearchView; | |
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 | |
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.LinkedList; | |
25 import java.util.List; | |
26 | |
27 /** | |
28 * Shows the list of sites that the user has granted access to a particular USB device. | |
29 */ | |
30 public class UsbDevicePreferences | |
31 extends PreferenceFragment implements Preference.OnPreferenceClickListen er { | |
32 public static final String EXTRA_USB_INFOS = "org.chromium.chrome.preference s.usb_infos"; | |
33 public static final String EXTRA_SITES = "org.chromium.chrome.preferences.si te_set"; | |
34 public static final String EXTRA_CATEGORY = | |
35 "org.chromium.chrome.preferences.content_settings_type"; | |
36 | |
37 public static final String PREF_OBJECT_NAME = "object_name"; | |
38 | |
39 // The site settings category we are showing. | |
40 private SiteSettingsCategory mCategory; | |
41 // Canonical example of the USB device being examined. | |
42 private UsbInfo mUsbInfo; | |
43 // All of the USB device permission entries matching the canonical device. | |
44 private List<UsbInfo> mUsbInfos; | |
45 // The set of sites to display. | |
46 private List<Website> mSites; | |
47 // The view for searching the list of items. | |
48 private SearchView mSearchView; | |
49 // If not blank, represents a substring to use to search for site names. | |
50 private String mSearch = ""; | |
51 | |
52 @Override | |
53 @SuppressWarnings("unchecked") | |
54 public void onActivityCreated(Bundle savedInstanceState) { | |
55 addPreferencesFromResource(R.xml.single_object_preferences); | |
56 ListView listView = (ListView) getView().findViewById(android.R.id.list) ; | |
57 listView.setDivider(null); | |
58 | |
59 int contentSettingsType = getArguments().getInt(EXTRA_CATEGORY); | |
60 mCategory = SiteSettingsCategory.fromContentSettingsType(contentSettings Type); | |
61 mUsbInfos = (List<UsbInfo>) getArguments().getSerializable(EXTRA_USB_INF OS); | |
62 mUsbInfo = mUsbInfos.get(0); | |
63 mSites = (List<Website>) getArguments().getSerializable(EXTRA_SITES); | |
64 String title = getArguments().getString(SingleCategoryPreferences.EXTRA_ TITLE); | |
65 if (title != null) getActivity().setTitle(title); | |
66 | |
67 setHasOptionsMenu(true); | |
68 | |
69 super.onActivityCreated(savedInstanceState); | |
70 } | |
71 | |
72 @Override | |
73 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | |
74 menu.clear(); | |
75 inflater.inflate(R.menu.website_preferences_menu, menu); | |
76 | |
77 MenuItem searchItem = menu.findItem(R.id.search); | |
78 mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); | |
79 mSearchView.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN); | |
80 SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQuer yTextListener() { | |
81 @Override | |
82 public boolean onQueryTextSubmit(String query) { | |
83 return true; | |
84 } | |
85 | |
86 @Override | |
87 public boolean onQueryTextChange(String query) { | |
88 // Make search case-insensitive. | |
89 query = query.toLowerCase(); | |
90 | |
91 if (query.equals(mSearch)) return true; | |
92 | |
93 mSearch = query; | |
94 getInfo(); | |
95 return true; | |
96 } | |
97 }; | |
98 mSearchView.setOnQueryTextListener(queryTextListener); | |
99 | |
100 MenuItem help = | |
101 menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.str ing.menu_help); | |
102 help.setIcon(R.drawable.ic_help_and_feedback); | |
103 } | |
104 | |
105 @Override | |
106 public boolean onOptionsItemSelected(MenuItem item) { | |
107 if (item.getItemId() == R.id.menu_id_targeted_help) { | |
108 HelpAndFeedback.getInstance(getActivity()) | |
109 .show(getActivity(), getString(R.string.help_context_setting s), | |
110 Profile.getLastUsedProfile(), null); | |
111 return true; | |
112 } | |
113 return false; | |
114 } | |
115 | |
116 @Override | |
117 public void onResume() { | |
118 super.onResume(); | |
119 | |
120 if (mSites == null) { | |
121 getInfo(); | |
122 } else { | |
123 resetList(); | |
124 } | |
125 } | |
126 | |
127 @Override | |
128 public boolean onPreferenceClick(Preference preference) { | |
129 if (PREF_OBJECT_NAME.equals(preference.getKey())) { | |
130 for (UsbInfo info : mUsbInfos) info.revoke(); | |
Ted C
2016/08/15 20:31:59
Since this is a linkedlist, I don't believe this w
Reilly Grant (use Gerrit)
2016/08/16 17:41:59
I don't understand this comment. Using LinkedList.
Ted C
2016/08/17 14:49:13
You are definitely correct. LinkedList isn't the
Reilly Grant (use Gerrit)
2016/08/17 20:04:22
Done.
| |
131 getActivity().finish(); | |
132 return true; | |
133 } | |
134 | |
135 if (preference instanceof WebsitePreference) { | |
136 WebsitePreference websitePreference = (WebsitePreference) preference ; | |
137 websitePreference.getExtras().putSerializable( | |
138 SingleWebsitePreferences.EXTRA_SITE, websitePreference.site( )); | |
139 websitePreference.setFragment( | |
140 "org.chromium.chrome.browser.preferences.website.SingleWebsi tePreferences"); | |
Ted C
2016/08/15 20:31:59
same comment about using class names directly inst
Reilly Grant (use Gerrit)
2016/08/16 17:41:59
Done.
| |
141 } | |
142 return false; | |
143 } | |
144 | |
145 private class ResultsPopulator implements WebsitePermissionsFetcher.WebsiteP ermissionsCallback { | |
146 @Override | |
147 public void onWebsitePermissionsAvailable(Collection<Website> sites) { | |
148 // This method may be called after the activity has been destroyed. | |
149 // In that case, bail out. | |
150 if (getActivity() == null) return; | |
151 | |
152 mUsbInfos.clear(); | |
153 mSites = new LinkedList<Website>(); | |
154 for (Website site : sites) { | |
155 for (UsbInfo info : site.getUsbInfo()) { | |
156 if (info.equals(mUsbInfo)) { | |
157 mUsbInfos.add(info); | |
158 if (mSearch.isEmpty() || site.getTitle().toLowerCase().c ontains(mSearch)) { | |
159 mSites.add(site); | |
160 } | |
161 } | |
162 } | |
163 } | |
164 | |
165 // After revoking a site's permission to access a device the user ma y end up back at | |
166 // this activity. It is awkward to display this empty list because t here's no action | |
167 // that can be taken from it. In this case we dismiss this activity as well, taking | |
168 // them back to UsbChooserPreferences which will now no longer offer the option to | |
169 // examine the permissions for this device. | |
170 if (mUsbInfos.isEmpty()) { | |
171 getActivity().finish(); | |
172 } else { | |
173 resetList(); | |
174 } | |
175 } | |
176 } | |
177 | |
178 /** | |
179 * Refreshes the list of sites with access to the object being examined. | |
180 * | |
181 * resetList() is called to refresh the view when the data is ready. | |
182 */ | |
183 private void getInfo() { | |
184 WebsitePermissionsFetcher fetcher = new WebsitePermissionsFetcher(new Re sultsPopulator()); | |
185 fetcher.fetchPreferencesForCategory(mCategory); | |
186 } | |
187 | |
188 private void resetList() { | |
189 getPreferenceScreen().removeAll(); | |
190 addPreferencesFromResource(R.xml.single_object_preferences); | |
191 | |
192 PreferenceScreen preferenceScreen = getPreferenceScreen(); | |
193 Preference header = preferenceScreen.findPreference(PREF_OBJECT_NAME); | |
194 header.setTitle(mUsbInfo.getName()); | |
195 header.setOnPreferenceClickListener(this); | |
196 | |
197 for (Website site : mSites) { | |
198 Preference preference = new WebsitePreference(getActivity(), site, m Category); | |
199 preference.setOnPreferenceClickListener(this); | |
200 preferenceScreen.addPreference(preference); | |
201 } | |
202 | |
203 // Force this list to be reloaded if the activity is resumed. | |
204 mSites = null; | |
205 } | |
206 } | |
OLD | NEW |