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 |
| 29 * item, for example a USB or Bluetooth device. |
| 30 */ |
| 31 public class SingleObjectPreferences |
| 32 extends PreferenceFragment implements Preference.OnPreferenceClickListen
er { |
| 33 public static final String EXTRA_USB_INFOS = "org.chromium.chrome.preference
s.usb_infos"; |
| 34 public static final String EXTRA_SITES = "org.chromium.chrome.preferences.si
te_set"; |
| 35 public static final String EXTRA_CATEGORY = |
| 36 "org.chromium.chrome.preferences.content_settings_type"; |
| 37 |
| 38 public static final String PREF_OBJECT_NAME = "object_name"; |
| 39 |
| 40 // The site settings category we are showing. |
| 41 private SiteSettingsCategory mCategory; |
| 42 // Canonical example of the USB device being examined. |
| 43 private UsbInfo mUsbInfo; |
| 44 // All of the USB device permission entries matching the canonical device. |
| 45 private List<UsbInfo> mUsbInfos; |
| 46 // The set of sites to display. |
| 47 private List<Website> mSites; |
| 48 // The view for searching the list of items. |
| 49 private SearchView mSearchView; |
| 50 // If not blank, represents a substring to use to search for site names. |
| 51 private String mSearch = ""; |
| 52 |
| 53 @Override |
| 54 @SuppressWarnings("unchecked") |
| 55 public void onActivityCreated(Bundle savedInstanceState) { |
| 56 addPreferencesFromResource(R.xml.single_object_preferences); |
| 57 ListView listView = (ListView) getView().findViewById(android.R.id.list)
; |
| 58 listView.setDivider(null); |
| 59 |
| 60 int contentSettingsType = getArguments().getInt(EXTRA_CATEGORY); |
| 61 mCategory = SiteSettingsCategory.fromContentSettingsType(contentSettings
Type); |
| 62 mUsbInfos = (List<UsbInfo>) getArguments().getSerializable(EXTRA_USB_INF
OS); |
| 63 mUsbInfo = mUsbInfos.get(0); |
| 64 mSites = (List<Website>) getArguments().getSerializable(EXTRA_SITES); |
| 65 String title = getArguments().getString(SingleCategoryPreferences.EXTRA_
TITLE); |
| 66 if (title != null) 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 if (mSites == null) { |
| 122 getInfo(); |
| 123 } else { |
| 124 resetList(); |
| 125 } |
| 126 } |
| 127 |
| 128 @Override |
| 129 public boolean onPreferenceClick(Preference preference) { |
| 130 if (PREF_OBJECT_NAME.equals(preference.getKey())) { |
| 131 for (UsbInfo info : mUsbInfos) info.revoke(); |
| 132 getActivity().finish(); |
| 133 return true; |
| 134 } |
| 135 |
| 136 if (preference instanceof WebsitePreference) { |
| 137 WebsitePreference websitePreference = (WebsitePreference) preference
; |
| 138 websitePreference.getExtras().putSerializable( |
| 139 SingleWebsitePreferences.EXTRA_SITE, websitePreference.site(
)); |
| 140 websitePreference.setFragment( |
| 141 "org.chromium.chrome.browser.preferences.website.SingleWebsi
tePreferences"); |
| 142 } |
| 143 return false; |
| 144 } |
| 145 |
| 146 private class ResultsPopulator implements WebsitePermissionsFetcher.WebsiteP
ermissionsCallback { |
| 147 @Override |
| 148 public void onWebsitePermissionsAvailable(Collection<Website> sites) { |
| 149 // This method may be called after the activity has been destroyed. |
| 150 // In that case, bail out. |
| 151 if (getActivity() == null) return; |
| 152 |
| 153 mUsbInfos.clear(); |
| 154 mSites = new LinkedList<Website>(); |
| 155 for (Website site : sites) { |
| 156 if (mSearch.isEmpty() || site.getTitle().toLowerCase().contains(
mSearch)) { |
| 157 for (UsbInfo info : site.getUsbInfo()) { |
| 158 if (info.equals(mUsbInfo)) { |
| 159 mUsbInfos.add(info); |
| 160 mSites.add(site); |
| 161 } |
| 162 } |
| 163 } |
| 164 } |
| 165 |
| 166 if (mSites.isEmpty() && mSearch.isEmpty()) { |
| 167 getActivity().finish(); |
| 168 } else { |
| 169 resetList(); |
| 170 } |
| 171 } |
| 172 } |
| 173 |
| 174 /** |
| 175 * Refreshes the list of sites with access to the object being examined. |
| 176 * |
| 177 * resetList() is called to refresh the view when the data is ready. |
| 178 */ |
| 179 private void getInfo() { |
| 180 WebsitePermissionsFetcher fetcher = new WebsitePermissionsFetcher(new Re
sultsPopulator()); |
| 181 fetcher.fetchPreferencesForCategory(mCategory); |
| 182 } |
| 183 |
| 184 private void resetList() { |
| 185 getPreferenceScreen().removeAll(); |
| 186 addPreferencesFromResource(R.xml.single_object_preferences); |
| 187 |
| 188 PreferenceScreen preferenceScreen = getPreferenceScreen(); |
| 189 Preference header = preferenceScreen.findPreference(PREF_OBJECT_NAME); |
| 190 header.setTitle(mUsbInfo.getName()); |
| 191 header.setOnPreferenceClickListener(this); |
| 192 |
| 193 for (Website site : mSites) { |
| 194 Preference preference = new WebsitePreference(getActivity(), site, m
Category); |
| 195 preference.setOnPreferenceClickListener(this); |
| 196 preferenceScreen.addPreference(preference); |
| 197 } |
| 198 |
| 199 // Force this list to be reloaded if the activity is resumed. |
| 200 mSites = null; |
| 201 } |
| 202 } |
OLD | NEW |