| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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.os.Bundle; | 7 import android.os.Bundle; |
| 8 import android.preference.PreferenceFragment; | 8 import android.preference.PreferenceFragment; |
| 9 import android.view.LayoutInflater; | 9 import android.view.LayoutInflater; |
| 10 import android.view.View; | 10 import android.view.View; |
| 11 import android.view.View.OnLayoutChangeListener; | |
| 12 import android.view.ViewGroup; | 11 import android.view.ViewGroup; |
| 13 import android.widget.ListView; | 12 import android.widget.ListView; |
| 14 | 13 |
| 15 import org.chromium.base.VisibleForTesting; | 14 import org.chromium.base.VisibleForTesting; |
| 16 import org.chromium.base.metrics.RecordUserAction; | |
| 17 import org.chromium.chrome.R; | 15 import org.chromium.chrome.R; |
| 18 import org.chromium.chrome.browser.locale.LocaleManager; | |
| 19 import org.chromium.chrome.browser.search_engines.TemplateUrlService; | |
| 20 | 16 |
| 21 /** | 17 /** |
| 22 * A preference fragment for selecting a default search engine. | 18 * A preference fragment for selecting a default search engine. |
| 23 */ | 19 */ |
| 24 public class SearchEnginePreference extends PreferenceFragment | 20 public class SearchEnginePreference extends PreferenceFragment { |
| 25 implements View.OnClickListener, SearchEngineAdapter.SelectSearchEngineC
allback, | |
| 26 OnLayoutChangeListener { | |
| 27 private ListView mListView; | 21 private ListView mListView; |
| 28 private View mCancelButton; | |
| 29 private View mSaveButton; | |
| 30 private View mDivider; | |
| 31 | 22 |
| 32 private SearchEngineAdapter mSearchEngineAdapter; | 23 private SearchEngineAdapter mSearchEngineAdapter; |
| 33 private int mSelectedIndex; | |
| 34 | 24 |
| 35 @VisibleForTesting | 25 @VisibleForTesting |
| 36 String getValueForTesting() { | 26 String getValueForTesting() { |
| 37 return mSearchEngineAdapter.getValueForTesting(); | 27 return mSearchEngineAdapter.getValueForTesting(); |
| 38 } | 28 } |
| 39 | 29 |
| 40 @VisibleForTesting | 30 @VisibleForTesting |
| 41 void setValueForTesting(String value) { | 31 String setValueForTesting(String value) { |
| 42 mSearchEngineAdapter.setValueForTesting(value); | 32 return mSearchEngineAdapter.setValueForTesting(value); |
| 43 TemplateUrlService.getInstance().setSearchEngine(mSelectedIndex); | 33 } |
| 34 |
| 35 @VisibleForTesting |
| 36 String getKeywordFromIndexTesting(int index) { |
| 37 return mSearchEngineAdapter.getKeywordForTesting(index); |
| 44 } | 38 } |
| 45 | 39 |
| 46 @Override | 40 @Override |
| 47 public void onCreate(Bundle savedInstanceState) { | 41 public void onCreate(Bundle savedInstanceState) { |
| 48 super.onCreate(savedInstanceState); | 42 super.onCreate(savedInstanceState); |
| 49 getActivity().setTitle(R.string.prefs_search_engine); | 43 getActivity().setTitle(R.string.prefs_search_engine); |
| 50 mSearchEngineAdapter = new SearchEngineAdapter(getActivity(), this); | 44 mSearchEngineAdapter = new SearchEngineAdapter(getActivity()); |
| 51 } | 45 } |
| 52 | 46 |
| 53 @Override | 47 @Override |
| 54 public View onCreateView( | 48 public View onCreateView( |
| 55 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceSt
ate) { | 49 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceSt
ate) { |
| 56 View view = inflater.inflate(R.layout.search_engine_layout, container, f
alse); | 50 View view = inflater.inflate(R.layout.search_engine_layout, container, f
alse); |
| 57 mListView = (ListView) view.findViewById(android.R.id.list); | 51 mListView = (ListView) view.findViewById(android.R.id.list); |
| 58 mListView.setAdapter(mSearchEngineAdapter); | 52 mListView.setAdapter(mSearchEngineAdapter); |
| 59 mListView.setDivider(null); | 53 mListView.setDivider(null); |
| 60 mListView.addOnLayoutChangeListener(this); | |
| 61 mCancelButton = view.findViewById(R.id.cancel_button); | |
| 62 mCancelButton.setOnClickListener(this); | |
| 63 mSaveButton = view.findViewById(R.id.save_button); | |
| 64 mSaveButton.setOnClickListener(this); | |
| 65 mDivider = view.findViewById(R.id.bottom_shadow); | |
| 66 return view; | 54 return view; |
| 67 } | 55 } |
| 68 | 56 |
| 69 @Override | 57 @Override |
| 70 public void onActivityCreated(Bundle savedInstanceState) { | |
| 71 super.onActivityCreated(savedInstanceState); | |
| 72 } | |
| 73 | |
| 74 @Override | |
| 75 public void onClick(View v) { | |
| 76 if (v == mCancelButton) { | |
| 77 getActivity().finish(); | |
| 78 } else if (v == mSaveButton) { | |
| 79 TemplateUrlService.getInstance().setSearchEngine(mSelectedIndex); | |
| 80 // If the user has manually set the default search engine, disable a
uto switching. | |
| 81 boolean manualSwitch = mSelectedIndex == mSearchEngineAdapter | |
| 82 .getInitialSearchEnginePosition(); | |
| 83 if (manualSwitch) { | |
| 84 RecordUserAction.record("SearchEngine_ManualChange"); | |
| 85 LocaleManager.getInstance().setSearchEngineAutoSwitch(false); | |
| 86 } | |
| 87 getActivity().finish(); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 @Override | |
| 92 public void currentSearchEngineDetermined(int selectedIndex) { | |
| 93 mSelectedIndex = selectedIndex; | |
| 94 } | |
| 95 | |
| 96 /** | |
| 97 * Displays the divider if the Listview is longer than its viewport. | |
| 98 */ | |
| 99 public void updateBottombarDivider() { | |
| 100 if (mListView.getLastVisiblePosition() == mSearchEngineAdapter.getCount(
) - 1) { | |
| 101 mDivider.setVisibility(View.INVISIBLE); | |
| 102 } else { | |
| 103 mDivider.setVisibility(View.VISIBLE); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 @Override | |
| 108 public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, | |
| 109 int oldTop, int oldRight, int oldBottom) { | |
| 110 if (v == mListView) { | |
| 111 updateBottombarDivider(); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 @Override | |
| 116 public void onResume() { | 58 public void onResume() { |
| 117 super.onResume(); | 59 super.onResume(); |
| 118 /** | 60 /** |
| 119 * Handle UI update when location setting for a search engine is changed
. | 61 * Handle UI update when location setting for a search engine is changed
. |
| 120 */ | 62 */ |
| 121 mSearchEngineAdapter.notifyDataSetChanged(); | 63 mSearchEngineAdapter.notifyDataSetChanged(); |
| 122 } | 64 } |
| 123 } | 65 } |
| OLD | NEW |