michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: package org.mozilla.gecko.sync.setup.activities;
michael@0:
michael@0: import org.mozilla.gecko.BrowserLocaleManager;
michael@0: import org.mozilla.gecko.LocaleManager;
michael@0:
michael@0: import android.annotation.TargetApi;
michael@0: import android.app.Activity;
michael@0: import android.content.Context;
michael@0: import android.os.Build;
michael@0: import android.os.Bundle;
michael@0: import android.os.StrictMode;
michael@0: import android.support.v4.app.FragmentActivity;
michael@0:
michael@0: /**
michael@0: * This is a helper class to do typical locale switching operations
michael@0: * without hitting StrictMode errors or adding boilerplate to common
michael@0: * activity subclasses.
michael@0: *
michael@0: * Either call {@link LocaleAware#initializeLocale(Context)} in your
michael@0: * onCreate
method, or inherit from LocaleAwareFragmentActivity
michael@0: * or LocaleAwareActivity
.
michael@0: */
michael@0: public class LocaleAware {
michael@0: @TargetApi(Build.VERSION_CODES.GINGERBREAD)
michael@0: public static void initializeLocale(Context context) {
michael@0: final LocaleManager localeManager = BrowserLocaleManager.getInstance();
michael@0: if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
michael@0: localeManager.getAndApplyPersistedLocale(context);
michael@0: } else {
michael@0: final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
michael@0: StrictMode.allowThreadDiskWrites();
michael@0: try {
michael@0: localeManager.getAndApplyPersistedLocale(context);
michael@0: } finally {
michael@0: StrictMode.setThreadPolicy(savedPolicy);
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: public static class LocaleAwareFragmentActivity extends FragmentActivity {
michael@0: @Override
michael@0: protected void onCreate(Bundle savedInstanceState) {
michael@0: LocaleAware.initializeLocale(getApplicationContext());
michael@0: super.onCreate(savedInstanceState);
michael@0: }
michael@0: }
michael@0:
michael@0: public static class LocaleAwareActivity extends Activity {
michael@0: @Override
michael@0: protected void onCreate(Bundle savedInstanceState) {
michael@0: LocaleAware.initializeLocale(getApplicationContext());
michael@0: super.onCreate(savedInstanceState);
michael@0: }
michael@0: }
michael@0: }