Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.preferences; | 5 package org.chromium.chrome.browser.preferences; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.Intent; | 8 import android.content.Intent; |
| 9 import android.content.SharedPreferences; | 9 import android.content.SharedPreferences; |
| 10 import android.content.res.Resources; | 10 import android.content.res.Resources; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 import org.chromium.base.ContextUtils; | 27 import org.chromium.base.ContextUtils; |
| 28 import org.chromium.chrome.R; | 28 import org.chromium.chrome.R; |
| 29 import org.chromium.chrome.browser.omnibox.geo.GeolocationHeader; | 29 import org.chromium.chrome.browser.omnibox.geo.GeolocationHeader; |
| 30 import org.chromium.chrome.browser.preferences.website.ContentSetting; | 30 import org.chromium.chrome.browser.preferences.website.ContentSetting; |
| 31 import org.chromium.chrome.browser.preferences.website.GeolocationInfo; | 31 import org.chromium.chrome.browser.preferences.website.GeolocationInfo; |
| 32 import org.chromium.chrome.browser.preferences.website.SingleWebsitePreferences; | 32 import org.chromium.chrome.browser.preferences.website.SingleWebsitePreferences; |
| 33 import org.chromium.chrome.browser.preferences.website.WebsitePreferenceBridge; | 33 import org.chromium.chrome.browser.preferences.website.WebsitePreferenceBridge; |
| 34 import org.chromium.chrome.browser.search_engines.TemplateUrlService; | 34 import org.chromium.chrome.browser.search_engines.TemplateUrlService; |
| 35 import org.chromium.chrome.browser.search_engines.TemplateUrlService.LoadListene r; | 35 import org.chromium.chrome.browser.search_engines.TemplateUrlService.LoadListene r; |
| 36 import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl ; | 36 import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl ; |
| 37 import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl Type; | |
| 37 import org.chromium.components.location.LocationUtils; | 38 import org.chromium.components.location.LocationUtils; |
| 38 import org.chromium.ui.text.SpanApplier; | 39 import org.chromium.ui.text.SpanApplier; |
| 39 import org.chromium.ui.text.SpanApplier.SpanInfo; | 40 import org.chromium.ui.text.SpanApplier.SpanInfo; |
| 40 | 41 |
| 42 import java.util.Collections; | |
| 41 import java.util.List; | 43 import java.util.List; |
| 42 | 44 |
| 43 /** | 45 /** |
| 44 * A custom adapter for listing search engines. | 46 * A custom adapter for listing search engines. |
| 45 */ | 47 */ |
| 46 public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On ClickListener { | 48 public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On ClickListener { |
| 47 /** | 49 /** |
| 48 * A callback for reporting progress to the owner. | 50 * A callback for reporting progress to the owner. |
| 49 */ | 51 */ |
| 50 public interface SelectSearchEngineCallback { | 52 public interface SelectSearchEngineCallback { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 private void initEntries() { | 103 private void initEntries() { |
| 102 TemplateUrlService templateUrlService = TemplateUrlService.getInstance() ; | 104 TemplateUrlService templateUrlService = TemplateUrlService.getInstance() ; |
| 103 if (!templateUrlService.isLoaded()) { | 105 if (!templateUrlService.isLoaded()) { |
| 104 templateUrlService.registerLoadListener(this); | 106 templateUrlService.registerLoadListener(this); |
| 105 templateUrlService.load(); | 107 templateUrlService.load(); |
| 106 return; // Flow continues in onTemplateUrlServiceLoaded below. | 108 return; // Flow continues in onTemplateUrlServiceLoaded below. |
| 107 } | 109 } |
| 108 | 110 |
| 109 // Fetch all the search engine info and the currently active one. | 111 // Fetch all the search engine info and the currently active one. |
| 110 mSearchEngines = templateUrlService.getLocalizedSearchEngines(); | 112 mSearchEngines = templateUrlService.getLocalizedSearchEngines(); |
| 111 int searchEngineIndex = templateUrlService.getDefaultSearchEngineIndex() ; | 113 int searchEngineIndex = templateUrlService.getDefaultSearchEngineIndex() ; |
|
Ian Wen
2016/09/27 04:42:00
Rename this to be defaultEngineIndex.
ltian
2016/09/28 19:19:46
Done.
| |
| 114 | |
| 115 for (int i = 0; i < mSearchEngines.size(); ++i) { | |
| 116 setSearchEngineType(mSearchEngines.get(i), searchEngineIndex); | |
| 117 } | |
| 118 TemplateUrl divider = new TemplateUrl(-1, "", "", false); | |
|
Ian Wen
2016/09/27 04:42:00
Oops this is wrong. Don't do it. What will happen
ltian
2016/09/28 19:19:46
Done.
| |
| 119 divider.setTemplateUrlType(TemplateUrlType.DIVIDER); | |
| 120 mSearchEngines.add(divider); | |
| 121 Collections.sort(mSearchEngines); | |
| 122 | |
| 112 // Convert the TemplateUrl index into an index into mSearchEngines. | 123 // Convert the TemplateUrl index into an index into mSearchEngines. |
| 113 mSelectedSearchEnginePosition = -1; | 124 mSelectedSearchEnginePosition = -1; |
| 114 for (int i = 0; i < mSearchEngines.size(); ++i) { | 125 for (int i = 0; i < mSearchEngines.size(); ++i) { |
| 115 if (mSearchEngines.get(i).getIndex() == searchEngineIndex) { | 126 if (mSearchEngines.get(i).getIndex() == searchEngineIndex) { |
| 116 mSelectedSearchEnginePosition = i; | 127 mSelectedSearchEnginePosition = i; |
| 117 } | 128 } |
| 118 } | 129 } |
| 119 | 130 |
| 120 // Report back what is selected. | 131 // Report back what is selected. |
| 121 mCallback.currentSearchEngineDetermined(toIndex(mSelectedSearchEnginePos ition)); | 132 mCallback.currentSearchEngineDetermined(toIndex(mSelectedSearchEnginePos ition)); |
| 122 } | 133 } |
| 123 | 134 |
| 135 private void setSearchEngineType(TemplateUrl templateUrl, int searchEngineIn dex) { | |
|
Ian Wen
2016/09/27 04:42:00
The adapter shouldn't be responsible for setting t
ltian
2016/09/28 19:19:46
Done.
| |
| 136 if (templateUrl.getIsPrepopulated()) { | |
| 137 templateUrl.setTemplateUrlType(TemplateUrlType.PREPOPULATED); | |
| 138 } else if (templateUrl.getIndex() == searchEngineIndex) { | |
| 139 templateUrl.setTemplateUrlType(TemplateUrlType.DEFAULT); | |
| 140 } else { | |
| 141 templateUrl.setTemplateUrlType(TemplateUrlType.CUSTOM); | |
| 142 } | |
| 143 } | |
| 144 | |
| 124 private int toIndex(int position) { | 145 private int toIndex(int position) { |
| 125 return mSearchEngines.get(position).getIndex(); | 146 return mSearchEngines.get(position).getIndex(); |
| 126 } | 147 } |
| 127 | 148 |
| 128 // BaseAdapter: | 149 // BaseAdapter: |
| 129 | 150 |
| 130 @Override | 151 @Override |
| 131 public int getCount() { | 152 public int getCount() { |
| 132 return mSearchEngines.size(); | 153 return mSearchEngines.size(); |
| 133 } | 154 } |
| 134 | 155 |
| 135 @Override | 156 @Override |
| 136 public Object getItem(int pos) { | 157 public Object getItem(int pos) { |
| 137 TemplateUrl templateUrl = mSearchEngines.get(pos); | 158 TemplateUrl templateUrl = mSearchEngines.get(pos); |
| 138 return templateUrl.getShortName(); | 159 return templateUrl.getShortName(); |
| 139 } | 160 } |
| 140 | 161 |
| 141 @Override | 162 @Override |
| 142 public long getItemId(int position) { | 163 public long getItemId(int position) { |
| 143 return position; | 164 return position; |
| 144 } | 165 } |
| 145 | 166 |
| 146 @Override | 167 @Override |
| 147 public View getView(int position, View convertView, ViewGroup parent) { | 168 public View getView(int position, View convertView, ViewGroup parent) { |
| 148 View view = convertView; | 169 View view = convertView; |
| 170 TemplateUrl templateUrl = mSearchEngines.get(position); | |
| 149 if (convertView == null) { | 171 if (convertView == null) { |
| 150 view = mLayoutInflater.inflate(R.layout.search_engine, null); | 172 if (templateUrl.getTemplateUrlType() == TemplateUrlType.DIVIDER) { |
| 173 view = mLayoutInflater.inflate(R.layout.search_engine_divider, n ull); | |
| 174 } else { | |
| 175 view = mLayoutInflater.inflate(R.layout.search_engine, null); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 if (templateUrl.getTemplateUrlType() == TemplateUrlType.DIVIDER) { | |
| 180 view.setOnClickListener(null); | |
| 181 view.setEnabled(false); | |
| 182 return view; | |
| 151 } | 183 } |
| 152 | 184 |
| 153 view.setOnClickListener(this); | 185 view.setOnClickListener(this); |
| 154 view.setTag(position); | 186 view.setTag(position); |
| 155 | 187 |
| 156 // TODO(finnur): There's a tinting bug in the AppCompat lib (see http:// crbug.com/474695), | 188 // TODO(finnur): There's a tinting bug in the AppCompat lib (see http:// crbug.com/474695), |
| 157 // which causes the first radiobox to always appear selected, even if it is not. It is being | 189 // which causes the first radiobox to always appear selected, even if it is not. It is being |
| 158 // addressed, but in the meantime we should use the native RadioButton i nstead. | 190 // addressed, but in the meantime we should use the native RadioButton i nstead. |
| 159 RadioButton radioButton = (RadioButton) view.findViewById(R.id.radiobutt on); | 191 RadioButton radioButton = (RadioButton) view.findViewById(R.id.radiobutt on); |
| 160 // On Lollipop this removes the redundant animation ring on selection bu t on older versions | 192 // On Lollipop this removes the redundant animation ring on selection bu t on older versions |
| 161 // it would cause the radio button to disappear. | 193 // it would cause the radio button to disappear. |
| 162 // TODO(finnur): Remove the encompassing if statement once we go back to using the AppCompat | 194 // TODO(finnur): Remove the encompassing if statement once we go back to using the AppCompat |
| 163 // control. | 195 // control. |
| 164 final boolean selected = position == mSelectedSearchEnginePosition; | 196 final boolean selected = position == mSelectedSearchEnginePosition; |
| 165 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 197 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 166 radioButton.setBackgroundResource(0); | 198 radioButton.setBackgroundResource(0); |
| 167 } | 199 } |
| 168 radioButton.setChecked(selected); | 200 radioButton.setChecked(selected); |
| 169 | 201 |
| 170 TextView description = (TextView) view.findViewById(R.id.description); | 202 TextView description = (TextView) view.findViewById(R.id.description); |
| 171 TemplateUrl templateUrl = mSearchEngines.get(position); | |
| 172 Resources resources = mContext.getResources(); | 203 Resources resources = mContext.getResources(); |
| 173 description.setText(templateUrl.getShortName()); | 204 description.setText(templateUrl.getShortName()); |
| 174 | 205 |
| 206 TextView url = (TextView) view.findViewById(R.id.url); | |
| 207 url.setText(templateUrl.getUrl()); | |
| 208 if (templateUrl.getTemplateUrlType() == TemplateUrlType.PREPOPULATED | |
| 209 || templateUrl.getUrl().length() == 0) { | |
| 210 url.setVisibility(View.GONE); | |
| 211 } | |
| 212 | |
| 175 // To improve the explore-by-touch experience, the radio button is hidde n from accessibility | 213 // To improve the explore-by-touch experience, the radio button is hidde n from accessibility |
| 176 // and instead, "checked" or "not checked" is read along with the search engine's name, e.g. | 214 // and instead, "checked" or "not checked" is read along with the search engine's name, e.g. |
| 177 // "google.com checked" or "google.com not checked". | 215 // "google.com checked" or "google.com not checked". |
| 178 radioButton.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILIT Y_NO); | 216 radioButton.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILIT Y_NO); |
| 179 description.setAccessibilityDelegate(new AccessibilityDelegate() { | 217 description.setAccessibilityDelegate(new AccessibilityDelegate() { |
| 180 @Override | 218 @Override |
| 181 public void onInitializeAccessibilityEvent(View host, AccessibilityE vent event) { | 219 public void onInitializeAccessibilityEvent(View host, AccessibilityE vent event) { |
| 182 super.onInitializeAccessibilityEvent(host, event); | 220 super.onInitializeAccessibilityEvent(host, event); |
| 183 event.setChecked(selected); | 221 event.setChecked(selected); |
| 184 } | 222 } |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 toIndex(position)); | 320 toIndex(position)); |
| 283 GeolocationInfo locationSettings = new GeolocationInfo(url, null, false) ; | 321 GeolocationInfo locationSettings = new GeolocationInfo(url, null, false) ; |
| 284 ContentSetting locationPermission = locationSettings.getContentSetting() ; | 322 ContentSetting locationPermission = locationSettings.getContentSetting() ; |
| 285 // Handle the case where the geoHeader being sent when no permission has been specified. | 323 // Handle the case where the geoHeader being sent when no permission has been specified. |
| 286 if (locationPermission == ContentSetting.ASK && checkGeoHeader) { | 324 if (locationPermission == ContentSetting.ASK && checkGeoHeader) { |
| 287 return GeolocationHeader.isGeoHeaderEnabledForUrl(mContext, url, fal se); | 325 return GeolocationHeader.isGeoHeaderEnabledForUrl(mContext, url, fal se); |
| 288 } | 326 } |
| 289 return locationPermission == ContentSetting.ALLOW; | 327 return locationPermission == ContentSetting.ALLOW; |
| 290 } | 328 } |
| 291 } | 329 } |
| OLD | NEW |