Chromium Code Reviews| 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 int contentSettingsType = getArguments().getInt(EXTRA_CATEGORY); | |
| 58 mCategory = SiteSettingsCategory.fromContentSettingsType(contentSettings Type); | |
| 59 mUsbInfos = (List<UsbInfo>) getArguments().getSerializable(EXTRA_USB_INF OS); | |
| 60 mUsbInfo = mUsbInfos.get(0); | |
| 61 mSites = (List<Website>) getArguments().getSerializable(EXTRA_SITES); | |
| 62 String title = getArguments().getString(SingleCategoryPreferences.EXTRA_ TITLE); | |
| 63 if (title != null) getActivity().setTitle(title); | |
| 64 | |
| 65 setHasOptionsMenu(true); | |
| 66 | |
| 67 super.onActivityCreated(savedInstanceState); | |
| 68 } | |
| 69 | |
| 70 @Override | |
| 71 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | |
|
Theresa
2016/08/04 16:39:55
Would it make sense to pull this out into a super
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
Can I do that in a followup patch? I don't want to
Theresa
2016/08/04 20:22:28
sg
| |
| 72 menu.clear(); | |
| 73 inflater.inflate(R.menu.website_preferences_menu, menu); | |
| 74 | |
| 75 MenuItem searchItem = menu.findItem(R.id.search); | |
| 76 mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); | |
| 77 mSearchView.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN); | |
| 78 SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQuer yTextListener() { | |
| 79 @Override | |
| 80 public boolean onQueryTextSubmit(String query) { | |
| 81 return true; | |
| 82 } | |
| 83 | |
| 84 @Override | |
| 85 public boolean onQueryTextChange(String query) { | |
| 86 // Make search case-insensitive. | |
| 87 query = query.toLowerCase(); | |
| 88 | |
| 89 if (query.equals(mSearch)) return true; | |
| 90 | |
| 91 mSearch = query; | |
| 92 getInfo(); | |
| 93 return true; | |
| 94 } | |
| 95 }; | |
| 96 mSearchView.setOnQueryTextListener(queryTextListener); | |
| 97 | |
| 98 MenuItem help = | |
| 99 menu.add(Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.str ing.menu_help); | |
| 100 help.setIcon(R.drawable.ic_help_and_feedback); | |
| 101 } | |
| 102 | |
| 103 @Override | |
| 104 public boolean onOptionsItemSelected(MenuItem item) { | |
| 105 if (item.getItemId() == R.id.menu_id_targeted_help) { | |
| 106 HelpAndFeedback.getInstance(getActivity()) | |
| 107 .show(getActivity(), getString(R.string.help_context_setting s), | |
| 108 Profile.getLastUsedProfile(), null); | |
| 109 return true; | |
| 110 } | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 @Override | |
| 115 public void onResume() { | |
| 116 super.onResume(); | |
| 117 | |
| 118 // First time this activity is launched the intent may have already incl uded the set of | |
| 119 // sites with permission for this device. | |
|
Theresa
2016/08/04 16:39:55
Does this comment go with resetList() (if the set
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
I've removed this comment since the one down in re
| |
| 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(); | |
| 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"); | |
| 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 if (mSearch.isEmpty() || site.getTitle().toLowerCase().contains( mSearch)) { | |
| 156 for (UsbInfo info : site.getUsbInfo()) { | |
| 157 if (info.equals(mUsbInfo)) { | |
| 158 mUsbInfos.add(info); | |
| 159 mSites.add(site); | |
| 160 } | |
| 161 } | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 if (mSites.isEmpty() && mSearch.isEmpty()) { | |
| 166 getActivity().finish(); | |
|
Theresa
2016/08/04 16:39:55
Should we display something if there are no sites
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
This will happen if the user returns to this activ
Theresa
2016/08/04 20:22:28
Just to check my understanding, they're here -> si
Reilly Grant (use Gerrit)
2016/08/04 21:30:25
That's correct. In theory the user could see the o
| |
| 167 } else { | |
| 168 resetList(); | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 /** | |
| 174 * Refreshes the list of sites with access to the object being examined. | |
| 175 * | |
| 176 * resetList() is called to refresh the view when the data is ready. | |
| 177 */ | |
| 178 private void getInfo() { | |
| 179 WebsitePermissionsFetcher fetcher = new WebsitePermissionsFetcher(new Re sultsPopulator()); | |
| 180 fetcher.fetchPreferencesForCategory(mCategory); | |
| 181 } | |
| 182 | |
| 183 private void resetList() { | |
| 184 getPreferenceScreen().removeAll(); | |
| 185 addPreferencesFromResource(R.xml.single_object_preferences); | |
| 186 ListView listView = (ListView) getView().findViewById(android.R.id.list) ; | |
| 187 listView.setDivider(null); | |
|
Theresa
2016/08/04 16:39:55
Does this need to be called every time resetList()
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
It probably can be.
| |
| 188 | |
| 189 PreferenceScreen preferenceScreen = getPreferenceScreen(); | |
| 190 Preference header = preferenceScreen.findPreference(PREF_OBJECT_NAME); | |
| 191 header.setTitle(mUsbInfo.getName()); | |
| 192 header.setOnPreferenceClickListener(this); | |
| 193 | |
| 194 for (Website site : mSites) { | |
| 195 Preference preference = new WebsitePreference(getActivity(), site, m Category); | |
| 196 preference.setOnPreferenceClickListener(this); | |
| 197 preferenceScreen.addPreference(preference); | |
| 198 } | |
| 199 | |
| 200 // Force this list to be reloaded if the activity is resumed. | |
| 201 mSites = null; | |
| 202 } | |
| 203 } | |
| OLD | NEW |