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.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 to websites through a chooser . For example, | |
|
Theresa
2016/08/04 16:39:54
nit: "Shows a list of items that the user has gran
Reilly Grant (use Gerrit)
2016/08/04 19:57:10
Done.
| |
| 31 * this could show all USB devices the user has granted sites permission to conn ect to or all | |
| 32 * Bluetooth devices that have been paired. When the user selects an item, | |
| 33 * SingleObjectPreferences is launched to show which sites have access to the it em. | |
| 34 */ | |
| 35 public class SingleChooserPreferences | |
| 36 extends PreferenceFragment implements Preference.OnPreferenceClickListen er { | |
| 37 // The site settings category we are showing. | |
| 38 private SiteSettingsCategory mCategory; | |
| 39 // Multiple sites may have access to the same object. A canonical UsbInfo fo r each object is | |
|
Theresa
2016/08/04 01:11:47
Does using a UsbInfo object work if this is displa
Reilly Grant (use Gerrit)
2016/08/04 01:15:52
When Bluetooth starts storing permissions we will
Theresa
2016/08/04 16:39:55
That sounds like a good plan. Will you please add
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
Done.
| |
| 40 // therefore arbitrarily chosen to represent the object. |mPermissionsByObje ct| lists all of | |
| 41 // the individual permission grants for that object. |mSitesByObject| lists all of the sites | |
| 42 // with access to that object. | |
| 43 // | |
| 44 // UsbInfo's hashCode() and equals() methods compare only the object they re present, not the | |
| 45 // origin to which permission was granted. | |
| 46 private Map<UsbInfo, LinkedList<UsbInfo>> mPermissionsByObject = new HashMap <>(); | |
| 47 private Map<UsbInfo, LinkedList<Website>> mSitesByObject = new HashMap<>(); | |
| 48 // The view to show when the list is empty. | |
| 49 private TextView mEmptyView; | |
| 50 // The view for searching the list of items. | |
| 51 private SearchView mSearchView; | |
| 52 // If not blank, represents a substring to use to search for site names. | |
| 53 private String mSearch = ""; | |
| 54 | |
| 55 @Override | |
| 56 public void onActivityCreated(Bundle savedInstanceState) { | |
| 57 addPreferencesFromResource(R.xml.single_chooser_preferences); | |
| 58 ListView listView = (ListView) getView().findViewById(android.R.id.list) ; | |
| 59 mEmptyView = (TextView) getView().findViewById(android.R.id.empty); | |
| 60 listView.setEmptyView(mEmptyView); | |
| 61 listView.setDivider(null); | |
| 62 | |
| 63 String category = getArguments().getString(SingleCategoryPreferences.EXT RA_CATEGORY); | |
| 64 mCategory = SiteSettingsCategory.fromString(category); | |
| 65 String title = getArguments().getString(SingleCategoryPreferences.EXTRA_ TITLE); | |
| 66 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 getInfo(); | |
| 122 } | |
| 123 | |
| 124 private class ResultsPopulator implements WebsitePermissionsFetcher.WebsiteP ermissionsCallback { | |
| 125 @Override | |
| 126 public void onWebsitePermissionsAvailable(Collection<Website> sites) { | |
| 127 // This method may be called after the activity has been destroyed. | |
| 128 // In that case, bail out. | |
| 129 if (getActivity() == null) return; | |
| 130 | |
| 131 mPermissionsByObject.clear(); | |
| 132 mSitesByObject.clear(); | |
| 133 for (Website site : sites) { | |
| 134 for (UsbInfo info : site.getUsbInfo()) { | |
| 135 if (mSearch.isEmpty() || info.getName().toLowerCase().contai ns(mSearch)) { | |
| 136 LinkedList<UsbInfo> permissionSet = mPermissionsByObject .get(info); | |
| 137 if (permissionSet == null) { | |
| 138 permissionSet = new LinkedList<>(); | |
| 139 mPermissionsByObject.put(info, permissionSet); | |
| 140 } | |
| 141 permissionSet.add(info); | |
| 142 LinkedList<Website> sitesSet = mSitesByObject.get(info); | |
| 143 if (sitesSet == null) { | |
| 144 sitesSet = new LinkedList<>(); | |
| 145 mSitesByObject.put(info, sitesSet); | |
| 146 } | |
| 147 sitesSet.add(site); | |
| 148 } | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 resetList(); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 /** | |
| 157 * Refreshes |mPermissionsByObject| and |mSitesByObject| with new data from C++. | |
|
Theresa
2016/08/04 16:39:55
nit: s/C++/native
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
Done.
| |
| 158 * | |
| 159 * resetList() is called to refresh the view when the data is ready. | |
| 160 */ | |
| 161 private void getInfo() { | |
| 162 WebsitePermissionsFetcher fetcher = new WebsitePermissionsFetcher(new Re sultsPopulator()); | |
| 163 fetcher.fetchPreferencesForCategory(mCategory); | |
| 164 } | |
| 165 | |
| 166 private void resetList() { | |
| 167 getPreferenceScreen().removeAll(); | |
| 168 addPreferencesFromResource(R.xml.single_chooser_preferences); | |
| 169 | |
| 170 if (mSitesByObject.isEmpty()) { | |
|
Theresa
2016/08/04 16:39:55
Does it matter if this is mSitesByObject vs mPermi
Reilly Grant (use Gerrit)
2016/08/04 19:57:11
No, the key sets should be equal.
| |
| 171 if (mEmptyView != null) { | |
| 172 mEmptyView.setText(R.string.no_saved_website_settings); | |
| 173 } | |
| 174 return; | |
| 175 } | |
| 176 | |
| 177 for (Map.Entry<UsbInfo, LinkedList<UsbInfo>> entry : mPermissionsByObjec t.entrySet()) { | |
| 178 Preference preference = new Preference(getActivity()); | |
| 179 preference.getExtras().putSerializable( | |
| 180 SingleObjectPreferences.EXTRA_USB_INFOS, entry.getValue()); | |
| 181 preference.setIcon(R.drawable.settings_usb); | |
| 182 preference.setOnPreferenceClickListener(this); | |
| 183 preference.setTitle(entry.getKey().getName()); | |
| 184 getPreferenceScreen().addPreference(preference); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 @Override | |
| 189 public boolean onPreferenceClick(Preference preference) { | |
| 190 Bundle extras = preference.getExtras(); | |
| 191 @SuppressWarnings("unchecked") | |
| 192 List<UsbInfo> info = | |
| 193 (List<UsbInfo>) extras.getSerializable(SingleObjectPreferences.E XTRA_USB_INFOS); | |
| 194 extras.putString( | |
| 195 SingleCategoryPreferences.EXTRA_TITLE, getActivity().getTitle(). toString()); | |
| 196 extras.putInt(SingleObjectPreferences.EXTRA_CATEGORY, mCategory.toConten tSettingsType()); | |
| 197 extras.putSerializable( | |
| 198 SingleObjectPreferences.EXTRA_SITES, mSitesByObject.get(info.get (0))); | |
| 199 preference.setFragment( | |
| 200 "org.chromium.chrome.browser.preferences.website.SingleObjectPre ferences"); | |
| 201 return false; | |
| 202 } | |
| 203 } | |
| OLD | NEW |