| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public interface LocaleSelectedListener { |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
void onLocaleSelected(LocaleStore.LocaleInfo locale); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
interface LocaleCollectorBase { |
| 81 |
|
| 82 |
HashSet<String> getIgnoredLocaleList(boolean translatedOnly); |
| 83 |
|
| 84 |
|
| 85 |
Set<LocaleStore.LocaleInfo> getSupportedLocaleList(LocaleStore.LocaleInfo parent, |
| 86 |
boolean translatedOnly, boolean isForCountryMode); |
| 87 |
|
| 88 |
|
| 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, null, translatedOnly, |
| 120 |
localePickerController); |
| 121 |
return localePicker; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 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 |
|
| 168 |
|
| 169 |
|
| 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 |
|
| 190 |
|
| 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 |
|
| 224 |
if (mSearchView != null) { |
| 225 |
mPreviousSearchHadFocus = mSearchView.hasFocus(); |
| 226 |
mPreviousSearch = mSearchView.getQuery(); |
| 227 |
} else { |
| 228 |
mPreviousSearchHadFocus = false; |
| 229 |
mPreviousSearch = null; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 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 |
|
| 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 , |
| 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 |
|
| 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 ); |
| 291 |
} else { |
| 292 |
mSearchView.setQuery(null, false ); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 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 |
|
| 315 |
|
| 316 |
public void setOnActionExpandListener(OnActionExpandListener onActionExpandListener) { |
| 317 |
mOnActionExpandListener = onActionExpandListener; |
| 318 |
} |
| 319 |
} |