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; |
| 11 import android.os.Build; | 11 import android.os.Build; |
| 12 import android.os.Bundle; | 12 import android.os.Bundle; |
| 13 import android.support.annotation.IntDef; | |
| 13 import android.text.SpannableString; | 14 import android.text.SpannableString; |
| 14 import android.text.style.ForegroundColorSpan; | 15 import android.text.style.ForegroundColorSpan; |
| 15 import android.view.LayoutInflater; | 16 import android.view.LayoutInflater; |
| 16 import android.view.View; | 17 import android.view.View; |
| 17 import android.view.View.AccessibilityDelegate; | 18 import android.view.View.AccessibilityDelegate; |
| 18 import android.view.View.OnClickListener; | 19 import android.view.View.OnClickListener; |
| 19 import android.view.ViewGroup; | 20 import android.view.ViewGroup; |
| 20 import android.view.accessibility.AccessibilityEvent; | 21 import android.view.accessibility.AccessibilityEvent; |
| 21 import android.view.accessibility.AccessibilityNodeInfo; | 22 import android.view.accessibility.AccessibilityNodeInfo; |
| 22 import android.widget.BaseAdapter; | 23 import android.widget.BaseAdapter; |
| 23 import android.widget.RadioButton; | 24 import android.widget.RadioButton; |
| 24 import android.widget.TextView; | 25 import android.widget.TextView; |
| 25 | 26 |
| 26 import org.chromium.base.ApiCompatibilityUtils; | 27 import org.chromium.base.ApiCompatibilityUtils; |
| 27 import org.chromium.base.ContextUtils; | 28 import org.chromium.base.ContextUtils; |
| 28 import org.chromium.chrome.R; | 29 import org.chromium.chrome.R; |
| 29 import org.chromium.chrome.browser.omnibox.geo.GeolocationHeader; | 30 import org.chromium.chrome.browser.omnibox.geo.GeolocationHeader; |
| 30 import org.chromium.chrome.browser.preferences.website.ContentSetting; | 31 import org.chromium.chrome.browser.preferences.website.ContentSetting; |
| 31 import org.chromium.chrome.browser.preferences.website.GeolocationInfo; | 32 import org.chromium.chrome.browser.preferences.website.GeolocationInfo; |
| 32 import org.chromium.chrome.browser.preferences.website.SingleWebsitePreferences; | 33 import org.chromium.chrome.browser.preferences.website.SingleWebsitePreferences; |
| 33 import org.chromium.chrome.browser.preferences.website.WebsitePreferenceBridge; | 34 import org.chromium.chrome.browser.preferences.website.WebsitePreferenceBridge; |
| 34 import org.chromium.chrome.browser.search_engines.TemplateUrlService; | 35 import org.chromium.chrome.browser.search_engines.TemplateUrlService; |
| 35 import org.chromium.chrome.browser.search_engines.TemplateUrlService.LoadListene r; | 36 import org.chromium.chrome.browser.search_engines.TemplateUrlService.LoadListene r; |
| 36 import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl ; | 37 import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl ; |
| 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.lang.annotation.Retention; | |
| 43 import java.lang.annotation.RetentionPolicy; | |
| 41 import java.util.List; | 44 import java.util.List; |
| 42 | 45 |
| 43 /** | 46 /** |
| 44 * A custom adapter for listing search engines. | 47 * A custom adapter for listing search engines. |
| 45 */ | 48 */ |
| 46 public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On ClickListener { | 49 public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On ClickListener { |
| 47 /** | 50 /** |
| 48 * A callback for reporting progress to the owner. | 51 * A callback for reporting progress to the owner. |
| 49 */ | 52 */ |
| 50 public interface SelectSearchEngineCallback { | 53 public interface SelectSearchEngineCallback { |
| 51 /** | 54 /** |
| 52 * Called when the search engine data has loaded and we've determined th e currently active | 55 * Called when the search engine data has loaded and we've determined th e currently active |
| 53 * one. | 56 * one. |
| 54 * @param name Provides the name of it (with a simplified URL in parenth esis). | 57 * @param name Provides the name of it (with a simplified URL in parenth esis). |
| 55 */ | 58 */ |
| 56 void currentSearchEngineDetermined(int selectedIndex); | 59 void currentSearchEngineDetermined(int selectedIndex); |
| 57 } | 60 } |
| 58 | 61 |
| 62 private static final int TYPE_LOCATION_ALLOW = 0; | |
| 63 private static final int TYPE_LOCATION_BLOCK = 1; | |
| 64 private static final int TYPE_LOCATION_ASK = 2; | |
| 65 @IntDef({TYPE_LOCATION_ALLOW, TYPE_LOCATION_BLOCK, TYPE_LOCATION_ASK}) | |
| 66 @Retention(RetentionPolicy.SOURCE) | |
| 67 private @interface LocationType {} | |
|
Peter Kasting
2016/11/18 20:25:43
Can you just return the ContentSetting directly?
ltian
2016/11/18 23:10:51
Done.
| |
| 68 | |
| 59 // The current context. | 69 // The current context. |
| 60 private Context mContext; | 70 private Context mContext; |
| 61 | 71 |
| 62 // The layout inflater to use for the custom views. | 72 // The layout inflater to use for the custom views. |
| 63 private LayoutInflater mLayoutInflater; | 73 private LayoutInflater mLayoutInflater; |
| 64 | 74 |
| 65 // The callback to use for notifying caller of progress. | 75 // The callback to use for notifying caller of progress. |
| 66 private SelectSearchEngineCallback mCallback; | 76 private SelectSearchEngineCallback mCallback; |
| 67 | 77 |
| 68 // The list of available search engines. | 78 // The list of available search engines. |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 public void onInitializeAccessibilityNodeInfo(View host, Accessibili tyNodeInfo info) { | 208 public void onInitializeAccessibilityNodeInfo(View host, Accessibili tyNodeInfo info) { |
| 199 super.onInitializeAccessibilityNodeInfo(host, info); | 209 super.onInitializeAccessibilityNodeInfo(host, info); |
| 200 info.setCheckable(true); | 210 info.setCheckable(true); |
| 201 info.setChecked(selected); | 211 info.setChecked(selected); |
| 202 } | 212 } |
| 203 }); | 213 }); |
| 204 | 214 |
| 205 TextView link = (TextView) view.findViewById(R.id.link); | 215 TextView link = (TextView) view.findViewById(R.id.link); |
| 206 link.setVisibility(selected ? View.VISIBLE : View.GONE); | 216 link.setVisibility(selected ? View.VISIBLE : View.GONE); |
| 207 if (selected) { | 217 if (selected) { |
| 208 ForegroundColorSpan linkSpan = new ForegroundColorSpan( | 218 if (shouldShowLocationInfo(position)) { |
| 209 ApiCompatibilityUtils.getColor(resources, R.color.pref_accen t_color)); | 219 ForegroundColorSpan linkSpan = new ForegroundColorSpan( |
| 210 if (LocationUtils.getInstance().isSystemLocationSettingEnabled()) { | 220 ApiCompatibilityUtils.getColor(resources, R.color.pref_a ccent_color)); |
| 211 String message = mContext.getString( | 221 if (LocationUtils.getInstance().isSystemLocationSettingEnabled() ) { |
| 212 locationEnabled(position, true) | 222 String message = mContext.getString( |
| 213 ? R.string.search_engine_location_allowed | 223 locationEnabled(position, true) |
| 214 : R.string.search_engine_location_blocked); | 224 ? R.string.search_engine_location_allowed |
| 215 SpannableString messageWithLink = new SpannableString(message); | 225 : R.string.search_engine_location_blocked); |
| 216 messageWithLink.setSpan(linkSpan, 0, messageWithLink.length(), 0 ); | 226 SpannableString messageWithLink = new SpannableString(messag e); |
| 217 link.setText(messageWithLink); | 227 messageWithLink.setSpan(linkSpan, 0, messageWithLink.length( ), 0); |
| 228 link.setText(messageWithLink); | |
| 229 } else { | |
| 230 link.setText(SpanApplier.applySpans( | |
| 231 mContext.getString(R.string.android_location_off), | |
| 232 new SpanInfo("<link>", "</link>", linkSpan))); | |
| 233 } | |
| 234 | |
| 235 link.setOnClickListener(this); | |
| 218 } else { | 236 } else { |
| 219 link.setText(SpanApplier.applySpans( | 237 link.setVisibility(View.GONE); |
| 220 mContext.getString(R.string.android_location_off), | |
| 221 new SpanInfo("<link>", "</link>", linkSpan))); | |
| 222 } | 238 } |
| 223 | |
| 224 link.setOnClickListener(this); | |
| 225 } | 239 } |
| 226 | 240 |
| 227 return view; | 241 return view; |
| 228 } | 242 } |
| 229 | 243 |
| 230 // TemplateUrlService.LoadListener | 244 // TemplateUrlService.LoadListener |
| 231 | 245 |
| 232 @Override | 246 @Override |
| 233 public void onTemplateUrlServiceLoaded() { | 247 public void onTemplateUrlServiceLoaded() { |
| 234 TemplateUrlService.getInstance().unregisterLoadListener(this); | 248 TemplateUrlService.getInstance().unregisterLoadListener(this); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 String url = TemplateUrlService.getInstance().getSearchEngineUrlFrom TemplateUrl( | 294 String url = TemplateUrlService.getInstance().getSearchEngineUrlFrom TemplateUrl( |
| 281 toIndex(mSelectedSearchEnginePosition)); | 295 toIndex(mSelectedSearchEnginePosition)); |
| 282 Bundle fragmentArgs = SingleWebsitePreferences.createFragmentArgsFor Site(url); | 296 Bundle fragmentArgs = SingleWebsitePreferences.createFragmentArgsFor Site(url); |
| 283 fragmentArgs.putBoolean(SingleWebsitePreferences.EXTRA_LOCATION, | 297 fragmentArgs.putBoolean(SingleWebsitePreferences.EXTRA_LOCATION, |
| 284 locationEnabled(mSelectedSearchEnginePosition, true)); | 298 locationEnabled(mSelectedSearchEnginePosition, true)); |
| 285 settingsIntent.putExtra(Preferences.EXTRA_SHOW_FRAGMENT_ARGUMENTS, f ragmentArgs); | 299 settingsIntent.putExtra(Preferences.EXTRA_SHOW_FRAGMENT_ARGUMENTS, f ragmentArgs); |
| 286 mContext.startActivity(settingsIntent); | 300 mContext.startActivity(settingsIntent); |
| 287 } | 301 } |
| 288 } | 302 } |
| 289 | 303 |
| 290 private boolean locationEnabled(int position, boolean checkGeoHeader) { | 304 @LocationType private int getLocationPermissionType(int position, boolean ch eckGeoHeader) { |
| 291 if (position == -1) return false; | 305 if (position == -1) { |
| 306 return TYPE_LOCATION_BLOCK; | |
| 307 } | |
| 292 | 308 |
| 293 String url = TemplateUrlService.getInstance().getSearchEngineUrlFromTemp lateUrl( | 309 String url = TemplateUrlService.getInstance().getSearchEngineUrlFromTemp lateUrl( |
| 294 toIndex(position)); | 310 toIndex(position)); |
| 295 GeolocationInfo locationSettings = new GeolocationInfo(url, null, false) ; | 311 GeolocationInfo locationSettings = new GeolocationInfo(url, null, false) ; |
| 296 ContentSetting locationPermission = locationSettings.getContentSetting() ; | 312 ContentSetting locationPermission = locationSettings.getContentSetting() ; |
| 297 // Handle the case where the geoHeader being sent when no permission has been specified. | 313 if (locationPermission == ContentSetting.ASK) { |
| 298 if (locationPermission == ContentSetting.ASK && checkGeoHeader) { | 314 // Handle the case where the geoHeader being sent when no permission has been specified. |
| 299 return GeolocationHeader.isGeoHeaderEnabledForUrl(mContext, url, fal se); | 315 if (checkGeoHeader && GeolocationHeader.isGeoHeaderEnabledForUrl( |
|
Peter Kasting
2016/11/18 20:25:43
Nit: Simpler:
if (checkGeoHeader && ...)
lo
ltian
2016/11/18 23:10:51
Done.
| |
| 316 mContext, url, false)) { | |
| 317 return TYPE_LOCATION_ALLOW; | |
| 318 } else { | |
| 319 return TYPE_LOCATION_ASK; | |
| 320 } | |
| 321 } else if (locationPermission == ContentSetting.ALLOW) { | |
| 322 return TYPE_LOCATION_ALLOW; | |
| 323 } else { | |
| 324 return TYPE_LOCATION_BLOCK; | |
| 300 } | 325 } |
| 301 return locationPermission == ContentSetting.ALLOW; | 326 } |
| 327 | |
| 328 private boolean locationEnabled(int position, boolean checkGeoHeader) { | |
| 329 return getLocationPermissionType(position, checkGeoHeader) == TYPE_LOCAT ION_ALLOW; | |
| 330 } | |
| 331 | |
| 332 // For Google search engine, if location permission is granted for the whole app, | |
| 333 // even its own location permission is unset, location permission is still g ranted for it. | |
|
Peter Kasting
2016/11/18 20:25:43
Nit: This comment no longer seems relevant.
ltian
2016/11/18 23:10:51
Done.
| |
| 334 private boolean shouldShowLocationInfo(int position) { | |
| 335 return getLocationPermissionType(position, true) != TYPE_LOCATION_ASK; | |
|
Peter Kasting
2016/11/18 20:25:43
Nit: Could just inline this into the lone caller,
ltian
2016/11/18 23:10:51
Done.
| |
| 302 } | 336 } |
| 303 } | 337 } |
| OLD | NEW |