private void searchEngineSelected(int position) { // First clean up any automatically added permissions (if any) for the previously selected // search engine. SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferences(); if (sharedPreferences.getBoolean(PrefServiceBridge.LOCATION_AUTO_ALLOWED, false)) { if (locationEnabled(mSelectedSearchEnginePosition, false)) { String url = TemplateUrlService.getInstance() .getSearchEngineUrlFromTemplateUrl(toIndex(mSelectedSearchEnginePosition)); WebsitePreferenceBridge.nativeSetGeolocationSettingForOrigin( url, url, ContentSetting.DEFAULT.toInt(), false); } sharedPreferences.edit().remove(PrefServiceBridge.LOCATION_AUTO_ALLOWED).apply(); } // Record the change in search engine. mSelectedSearchEnginePosition = position; TemplateUrlService.getInstance().setSearchEngine(toIndex(mSelectedSearchEnginePosition)); // Report the change back. TemplateUrl templateUrl = mSearchEngines.get(mSelectedSearchEnginePosition); mCallback.currentSearchEngineDetermined(templateUrl.getShortName()); notifyDataSetChanged(); }
@Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (convertView == null) { view = mLayoutInflater.inflate(R.layout.search_engine, null); } view.setOnClickListener(this); view.setTag(position); // TODO(finnur): There's a tinting bug in the AppCompat lib (see http://crbug.com/474695), // which causes the first radiobox to always appear selected, even if it is not. It is being // addressed, but in the meantime we should use the native RadioButton instead. RadioButton radioButton = (RadioButton) view.findViewById(R.id.radiobutton); // On Lollipop this removes the redundant animation ring on selection but on older versions // it would cause the radio button to disappear. // TODO(finnur): Remove the encompassing if statement once we go back to using the AppCompat // control. final boolean selected = position == mSelectedSearchEnginePosition; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { radioButton.setBackgroundResource(0); } radioButton.setChecked(selected); TextView description = (TextView) view.findViewById(R.id.description); TemplateUrl templateUrl = mSearchEngines.get(position); Resources resources = mContext.getResources(); description.setText(templateUrl.getShortName()); // To improve the explore-by-touch experience, the radio button is hidden from accessibility // and instead, "checked" or "not checked" is read along with the search engine's name, e.g. // "google.com checked" or "google.com not checked". radioButton.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); description.setAccessibilityDelegate( new AccessibilityDelegate() { @Override public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { super.onInitializeAccessibilityEvent(host, event); event.setChecked(selected); } @Override public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(host, info); info.setCheckable(true); info.setChecked(selected); } }); TextView link = (TextView) view.findViewById(R.id.link); link.setVisibility(selected ? View.VISIBLE : View.GONE); if (selected) { ForegroundColorSpan linkSpan = new ForegroundColorSpan( ApiCompatibilityUtils.getColor(resources, R.color.pref_accent_color)); if (LocationSettings.getInstance().isSystemLocationSettingEnabled()) { String message = mContext.getString( locationEnabled(position, true) ? R.string.search_engine_location_allowed : R.string.search_engine_location_blocked); SpannableString messageWithLink = new SpannableString(message); messageWithLink.setSpan(linkSpan, 0, messageWithLink.length(), 0); link.setText(messageWithLink); } else { link.setText( SpanApplier.applySpans( mContext.getString(R.string.android_location_off), new SpanInfo("<link>", "</link>", linkSpan))); } link.setOnClickListener(this); } return view; }
@Override public Object getItem(int pos) { TemplateUrl templateUrl = mSearchEngines.get(pos); return templateUrl.getShortName(); }