Debian Codegraph @

core/java/com/android/internal/app/LocalePickerWithRegion.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.internal.app;
18
19 import android.app.FragmentManager;
20 import android.app.FragmentTransaction;
21 import android.app.ListFragment;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.text.TextUtils;
25 import android.view.Menu;
26 import android.view.MenuInflater;
27 import android.view.MenuItem;
28 import android.view.MenuItem.OnActionExpandListener;
29 import android.view.View;
30 import android.widget.ListView;
31 import android.widget.SearchView;
32
33 import com.android.internal.R;
34
35 import java.util.HashSet;
36 import java.util.Locale;
37 import java.util.Set;
38
39 /**
40 * A two-step locale picker. It shows a language, then a country.
41 *
42 * <p>It shows suggestions at the top, then the rest of the locales.
43 * Allows the user to search for locales using both their native name and their name in the
44 * default locale.</p>
45 */
46 public class LocalePickerWithRegion extends ListFragment implements SearchView.OnQueryTextListener {
47 private static final String TAG = LocalePickerWithRegion.class.getSimpleName();
48 private static final String PARENT_FRAGMENT_NAME = "localeListEditor";
49
50 private SuggestedLocaleAdapter mAdapter;
51 private LocaleSelectedListener mListener;
52 private LocaleCollectorBase mLocalePickerCollector;
53 private Set<LocaleStore.LocaleInfo> mLocaleList;
54 private LocaleStore.LocaleInfo mParentLocale;
55 private boolean mTranslatedOnly = false;
56 private SearchView mSearchView = null;
57 private CharSequence mPreviousSearch = null;
58 private boolean mPreviousSearchHadFocus = false;
59 private int mFirstVisiblePosition = 0;
60 private int mTopDistance = 0;
61 private CharSequence mTitle = null;
62 private OnActionExpandListener mOnActionExpandListener;
63
64 /**
65 * Other classes can register to be notified when a locale was selected.
66 *
67 * <p>This is the mechanism to "return" the result of the selection.</p>
68 */
69 public interface LocaleSelectedListener {
70 /**
71 * The classes that want to retrieve the locale picked should implement this method.
72 * @param locale the locale picked.
73 */
74 void onLocaleSelected(LocaleStore.LocaleInfo locale);
75 }
76
77 /**
78 * The interface which provides the locale list.
79 */
80 interface LocaleCollectorBase {
81 /** Gets the ignored locale list. */
82 HashSet<String> getIgnoredLocaleList(boolean translatedOnly);
83
84 /** Gets the supported locale list. */
85 Set<LocaleStore.LocaleInfo> getSupportedLocaleList(LocaleStore.LocaleInfo parent,
86 boolean translatedOnly, boolean isForCountryMode);
87
88 /** Indicates if the class work for specific package. */
89 boolean hasSpecificPackageName();
90 }
91
92 private static LocalePickerWithRegion createCountryPicker(
93 LocaleSelectedListener listener, LocaleStore.LocaleInfo parent,
94 boolean translatedOnly, OnActionExpandListener onActionExpandListener,
95 LocaleCollectorBase localePickerCollector) {
96 LocalePickerWithRegion localePicker = new LocalePickerWithRegion();
97 localePicker.setOnActionExpandListener(onActionExpandListener);
98 boolean shouldShowTheList = localePicker.setListener(listener, parent,
99 translatedOnly, localePickerCollector);
100 return shouldShowTheList ? localePicker : null;
101 }
102
103 public static LocalePickerWithRegion createLanguagePicker(Context context,
104 LocaleSelectedListener listener, boolean translatedOnly) {
105 return createLanguagePicker(context, listener, translatedOnly, null, null);
106 }
107
108 public static LocalePickerWithRegion createLanguagePicker(Context context,
109 LocaleSelectedListener listener, boolean translatedOnly, String appPackageName,
110 OnActionExpandListener onActionExpandListener) {
111 LocaleCollectorBase localePickerController;
112 if (TextUtils.isEmpty(appPackageName)) {
113 localePickerController = new SystemLocaleCollector(context);
114 } else {
115 localePickerController = new AppLocaleCollector(context, appPackageName);
116 }
117 LocalePickerWithRegion localePicker = new LocalePickerWithRegion();
118 localePicker.setOnActionExpandListener(onActionExpandListener);
119 localePicker.setListener(listener, /* parent */ null, translatedOnly,
120 localePickerController);
121 return localePicker;
122 }
123
124 /**
125 * Sets the listener and initializes the locale list.
126 *
127 * <p>Returns true if we need to show the list, false if not.</p>
128 *
129 * <p>Can return false because of an error, trying to show a list of countries,
130 * but no parent locale was provided.</p>
131 *
132 * <p>It can also return false if the caller tries to show the list in country mode and
133 * there is only one country available (i.e. Japanese => Japan).
134 * In this case we don't even show the list, we call the listener with that locale,
135 * "pretending" it was selected, and return false.</p>
136 */
137 private boolean setListener(LocaleSelectedListener listener, LocaleStore.LocaleInfo parent,
138 boolean translatedOnly, LocaleCollectorBase localePickerController) {
139 this.mParentLocale = parent;
140 this.mListener = listener;
141 this.mTranslatedOnly = translatedOnly;
142 this.mLocalePickerCollector = localePickerController;
143 setRetainInstance(true);
144
145 mLocaleList = localePickerController.getSupportedLocaleList(
146 parent, translatedOnly, parent != null);
147
148 if (parent != null && listener != null && mLocaleList.size() == 1) {
149 listener.onLocaleSelected(mLocaleList.iterator().next());
150 return false;
151 } else {
152 return true;
153 }
154 }
155
156 private void returnToParentFrame() {
157 getFragmentManager().popBackStack(PARENT_FRAGMENT_NAME,
158 FragmentManager.POP_BACK_STACK_INCLUSIVE);
159 }
160
161 @Override
162 public void onCreate(Bundle savedInstanceState) {
163 super.onCreate(savedInstanceState);
164 setHasOptionsMenu(true);
165
166 if (mLocaleList == null) {
167 // The fragment was killed and restored by the FragmentManager.
168 // At this point we have no data, no listener. Just return, to prevend a NPE.
169 // Fixes b/28748150. Created b/29400003 for a cleaner solution.
170 returnToParentFrame();
171 return;
172 }
173
174 mTitle = getActivity().getTitle();
175 final boolean countryMode = mParentLocale != null;
176 final Locale sortingLocale = countryMode ? mParentLocale.getLocale() : Locale.getDefault();
177 final boolean hasSpecificPackageName =
178 mLocalePickerCollector != null && mLocalePickerCollector.hasSpecificPackageName();
179 mAdapter = new SuggestedLocaleAdapter(mLocaleList, countryMode, hasSpecificPackageName);
180 final LocaleHelper.LocaleInfoComparator comp =
181 new LocaleHelper.LocaleInfoComparator(sortingLocale, countryMode);
182 mAdapter.sort(comp);
183 setListAdapter(mAdapter);
184 }
185
186 @Override
187 public void onViewCreated(View view, Bundle savedInstanceState) {
188 super.onViewCreated(view, savedInstanceState);
189 // In order to make the list view work with CollapsingToolbarLayout,
190 // we have to enable the nested scrolling feature of the list view.
191 getListView().setNestedScrollingEnabled(true);
192 getListView().setDivider(null);
193 }
194
195 @Override
196 public boolean onOptionsItemSelected(MenuItem menuItem) {
197 int id = menuItem.getItemId();
198 switch (id) {
199 case android.R.id.home:
200 getFragmentManager().popBackStack();
201 return true;
202 }
203 return super.onOptionsItemSelected(menuItem);
204 }
205
206 @Override
207 public void onResume() {
208 super.onResume();
209
210 if (mParentLocale != null) {
211 getActivity().setTitle(mParentLocale.getFullNameNative());
212 } else {
213 getActivity().setTitle(mTitle);
214 }
215
216 getListView().requestFocus();
217 }
218
219 @Override
220 public void onPause() {
221 super.onPause();
222
223 // Save search status
224 if (mSearchView != null) {
225 mPreviousSearchHadFocus = mSearchView.hasFocus();
226 mPreviousSearch = mSearchView.getQuery();
227 } else {
228 mPreviousSearchHadFocus = false;
229 mPreviousSearch = null;
230 }
231
232 // Save scroll position
233 final ListView list = getListView();
234 final View firstChild = list.getChildAt(0);
235 mFirstVisiblePosition = list.getFirstVisiblePosition();
236 mTopDistance = (firstChild == null) ? 0 : (firstChild.getTop() - list.getPaddingTop());
237 }
238
239 @Override
240 public void onListItemClick(ListView parent, View v, int position, long id) {
241 final LocaleStore.LocaleInfo locale =
242 (LocaleStore.LocaleInfo) parent.getAdapter().getItem(position);
243 // Special case for resetting the app locale to equal the system locale.
244 boolean isSystemLocale = locale.isSystemLocale();
245 boolean isRegionLocale = locale.getParent() != null;
246
247 if (isSystemLocale || isRegionLocale) {
248 if (mListener != null) {
249 mListener.onLocaleSelected(locale);
250 }
251 returnToParentFrame();
252 } else {
253 LocalePickerWithRegion selector = LocalePickerWithRegion.createCountryPicker(
254 mListener, locale, mTranslatedOnly /* translate only */,
255 mOnActionExpandListener, this.mLocalePickerCollector);
256 if (selector != null) {
257 getFragmentManager().beginTransaction()
258 .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
259 .replace(getId(), selector).addToBackStack(null)
260 .commit();
261 } else {
262 returnToParentFrame();
263 }
264 }
265 }
266
267 @Override
268 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
269 if (mParentLocale == null) {
270 inflater.inflate(R.menu.language_selection_list, menu);
271
272 final MenuItem searchMenuItem = menu.findItem(R.id.locale_search_menu);
273 if (mLocalePickerCollector.hasSpecificPackageName()
274 && mOnActionExpandListener != null) {
275 searchMenuItem.setOnActionExpandListener(mOnActionExpandListener);
276 }
277
278 mSearchView = (SearchView) searchMenuItem.getActionView();
279 mSearchView.setQueryHint(getText(R.string.search_language_hint));
280 mSearchView.setOnQueryTextListener(this);
281
282 // Restore previous search status
283 if (!TextUtils.isEmpty(mPreviousSearch)) {
284 searchMenuItem.expandActionView();
285 mSearchView.setIconified(false);
286 mSearchView.setActivated(true);
287 if (mPreviousSearchHadFocus) {
288 mSearchView.requestFocus();
289 }
290 mSearchView.setQuery(mPreviousSearch, true /* submit */);
291 } else {
292 mSearchView.setQuery(null, false /* submit */);
293 }
294
295 // Restore previous scroll position
296 getListView().setSelectionFromTop(mFirstVisiblePosition, mTopDistance);
297 }
298 }
299
300 @Override
301 public boolean onQueryTextSubmit(String query) {
302 return false;
303 }
304
305 @Override
306 public boolean onQueryTextChange(String newText) {
307 if (mAdapter != null) {
308 mAdapter.getFilter().filter(newText);
309 }
310 return false;
311 }
312
313 /**
314 * Sets OnActionExpandListener to LocalePickerWithRegion to dectect action of search bar.
315 */
316 public void setOnActionExpandListener(OnActionExpandListener onActionExpandListener) {
317 mOnActionExpandListener = onActionExpandListener;
318 }
319 }