| 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.widget.LinearLayout.LayoutParams; |
| 10 import android.view.View; | |
| 11 import android.view.View.OnLayoutChangeListener; | |
| 12 import android.view.ViewGroup; | |
| 13 import android.widget.ListView; | 10 import android.widget.ListView; |
| 14 | 11 |
| 15 import org.chromium.base.VisibleForTesting; | 12 import org.chromium.base.VisibleForTesting; |
| 16 import org.chromium.base.metrics.RecordUserAction; | |
| 17 import org.chromium.chrome.R; | 13 import org.chromium.chrome.R; |
| 18 import org.chromium.chrome.browser.locale.LocaleManager; | |
| 19 import org.chromium.chrome.browser.search_engines.TemplateUrlService; | |
| 20 | 14 |
| 21 /** | 15 /** |
| 22 * A preference fragment for selecting a default search engine. | 16 * A preference fragment for selecting a default search engine. |
| 23 */ | 17 */ |
| 24 public class SearchEnginePreference extends PreferenceFragment | 18 public class SearchEnginePreference extends PreferenceFragment { |
| 25 implements View.OnClickListener, SearchEngineAdapter.SelectSearchEngineC
allback, | |
| 26 OnLayoutChangeListener { | |
| 27 private ListView mListView; | 19 private ListView mListView; |
| 28 private View mCancelButton; | |
| 29 private View mSaveButton; | |
| 30 private View mDivider; | |
| 31 | 20 |
| 32 private SearchEngineAdapter mSearchEngineAdapter; | 21 private SearchEngineAdapter mSearchEngineAdapter; |
| 33 private int mSelectedIndex; | |
| 34 | 22 |
| 35 @VisibleForTesting | 23 @VisibleForTesting |
| 36 String getValueForTesting() { | 24 String getValueForTesting() { |
| 37 return mSearchEngineAdapter.getValueForTesting(); | 25 return mSearchEngineAdapter.getValueForTesting(); |
| 38 } | 26 } |
| 39 | 27 |
| 40 @VisibleForTesting | 28 @VisibleForTesting |
| 41 void setValueForTesting(String value) { | 29 String setValueForTesting(String value) { |
| 42 mSearchEngineAdapter.setValueForTesting(value); | 30 return mSearchEngineAdapter.setValueForTesting(value); |
| 43 TemplateUrlService.getInstance().setSearchEngine(mSelectedIndex); | 31 } |
| 32 |
| 33 @VisibleForTesting |
| 34 String getKeywordFromIndexForTesting(int index) { |
| 35 return mSearchEngineAdapter.getKeywordForTesting(index); |
| 44 } | 36 } |
| 45 | 37 |
| 46 @Override | 38 @Override |
| 47 public void onCreate(Bundle savedInstanceState) { | 39 public void onCreate(Bundle savedInstanceState) { |
| 48 super.onCreate(savedInstanceState); | 40 super.onCreate(savedInstanceState); |
| 49 getActivity().setTitle(R.string.prefs_search_engine); | 41 getActivity().setTitle(R.string.prefs_search_engine); |
| 50 mSearchEngineAdapter = new SearchEngineAdapter(getActivity(), this); | 42 mSearchEngineAdapter = new SearchEngineAdapter(getActivity()); |
| 51 } | |
| 52 | |
| 53 @Override | |
| 54 public View onCreateView( | |
| 55 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceSt
ate) { | |
| 56 View view = inflater.inflate(R.layout.search_engine_layout, container, f
alse); | |
| 57 mListView = (ListView) view.findViewById(android.R.id.list); | |
| 58 mListView.setAdapter(mSearchEngineAdapter); | |
| 59 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; | |
| 67 } | 43 } |
| 68 | 44 |
| 69 @Override | 45 @Override |
| 70 public void onActivityCreated(Bundle savedInstanceState) { | 46 public void onActivityCreated(Bundle savedInstanceState) { |
| 71 super.onActivityCreated(savedInstanceState); | 47 super.onActivityCreated(savedInstanceState); |
| 48 mListView = (ListView) getView().findViewById(android.R.id.list); |
| 49 int marginTop = getActivity().getResources().getDimensionPixelSize( |
| 50 R.dimen.search_engine_list_margin_top); |
| 51 LayoutParams layoutParams = |
| 52 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_P
ARENT); |
| 53 layoutParams.setMargins(0, marginTop, 0, 0); |
| 54 mListView.setLayoutParams(layoutParams); |
| 55 mListView.setAdapter(mSearchEngineAdapter); |
| 56 mListView.setDivider(null); |
| 72 } | 57 } |
| 73 | 58 |
| 74 @Override | 59 @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() { | 60 public void onResume() { |
| 117 super.onResume(); | 61 super.onResume(); |
| 118 /** | 62 /** |
| 119 * Handle UI update when location setting for a search engine is changed
. | 63 * Handle UI update when location setting for a search engine is changed
. |
| 120 */ | 64 */ |
| 121 mSearchEngineAdapter.notifyDataSetChanged(); | 65 mSearchEngineAdapter.notifyDataSetChanged(); |
| 122 } | 66 } |
| 123 } | 67 } |
| OLD | NEW |