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