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, | 19 private static final int PADDING_TOP = 6; |
gone
2016/11/01 21:30:29
nit: Remove.
ltian
2016/11/01 22:59:36
Done.
| |
26 OnLayoutChangeListener { | |
27 private ListView mListView; | 20 private ListView mListView; |
28 private View mCancelButton; | |
29 private View mSaveButton; | |
30 private View mDivider; | |
31 | 21 |
32 private SearchEngineAdapter mSearchEngineAdapter; | 22 private SearchEngineAdapter mSearchEngineAdapter; |
33 private int mSelectedIndex; | |
34 | 23 |
35 @VisibleForTesting | 24 @VisibleForTesting |
36 String getValueForTesting() { | 25 String getValueForTesting() { |
37 return mSearchEngineAdapter.getValueForTesting(); | 26 return mSearchEngineAdapter.getValueForTesting(); |
38 } | 27 } |
39 | 28 |
40 @VisibleForTesting | 29 @VisibleForTesting |
41 void setValueForTesting(String value) { | 30 String setValueForTesting(String value) { |
42 mSearchEngineAdapter.setValueForTesting(value); | 31 return mSearchEngineAdapter.setValueForTesting(value); |
43 TemplateUrlService.getInstance().setSearchEngine(mSelectedIndex); | 32 } |
33 | |
34 @VisibleForTesting | |
35 String getKeywordFromIndexTesting(int index) { | |
gone
2016/11/01 21:30:29
ForTesting
ltian
2016/11/01 22:59:36
Done.
| |
36 return mSearchEngineAdapter.getKeywordForTesting(index); | |
44 } | 37 } |
45 | 38 |
46 @Override | 39 @Override |
47 public void onCreate(Bundle savedInstanceState) { | 40 public void onCreate(Bundle savedInstanceState) { |
48 super.onCreate(savedInstanceState); | 41 super.onCreate(savedInstanceState); |
49 getActivity().setTitle(R.string.prefs_search_engine); | 42 getActivity().setTitle(R.string.prefs_search_engine); |
50 mSearchEngineAdapter = new SearchEngineAdapter(getActivity(), this); | 43 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 } | 44 } |
68 | 45 |
69 @Override | 46 @Override |
70 public void onActivityCreated(Bundle savedInstanceState) { | 47 public void onActivityCreated(Bundle savedInstanceState) { |
71 super.onActivityCreated(savedInstanceState); | 48 super.onActivityCreated(savedInstanceState); |
49 mListView = (ListView) getView().findViewById(android.R.id.list); | |
50 float pxToDp = 1.0f / getActivity().getResources().getDisplayMetrics().d ensity; | |
gone
2016/11/01 21:30:29
Instead of manually calculating this you should be
ltian
2016/11/01 22:59:36
Done.
| |
51 int marginTop = (int) (pxToDp | |
52 * getActivity().getResources().getDimension(R.dimen.search_engin e_list_margin_top)); | |
53 LayoutParams layoutParams = | |
54 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_P ARENT); | |
55 layoutParams.setMargins(0, marginTop, 0, 0); | |
56 mListView.setLayoutParams(layoutParams); | |
57 mListView.setAdapter(mSearchEngineAdapter); | |
58 mListView.setDivider(null); | |
72 } | 59 } |
73 | 60 |
74 @Override | 61 @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() { | 62 public void onResume() { |
117 super.onResume(); | 63 super.onResume(); |
118 /** | 64 /** |
119 * Handle UI update when location setting for a search engine is changed . | 65 * Handle UI update when location setting for a search engine is changed . |
120 */ | 66 */ |
121 mSearchEngineAdapter.notifyDataSetChanged(); | 67 mSearchEngineAdapter.notifyDataSetChanged(); |
122 } | 68 } |
123 } | 69 } |
OLD | NEW |