|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync.setup.activities; |
|
6 |
|
7 import org.mozilla.gecko.BrowserLocaleManager; |
|
8 import org.mozilla.gecko.LocaleManager; |
|
9 |
|
10 import android.annotation.TargetApi; |
|
11 import android.app.Activity; |
|
12 import android.content.Context; |
|
13 import android.os.Build; |
|
14 import android.os.Bundle; |
|
15 import android.os.StrictMode; |
|
16 import android.support.v4.app.FragmentActivity; |
|
17 |
|
18 /** |
|
19 * This is a helper class to do typical locale switching operations |
|
20 * without hitting StrictMode errors or adding boilerplate to common |
|
21 * activity subclasses. |
|
22 * |
|
23 * Either call {@link LocaleAware#initializeLocale(Context)} in your |
|
24 * <code>onCreate</code> method, or inherit from <code>LocaleAwareFragmentActivity</code> |
|
25 * or <code>LocaleAwareActivity</code>. |
|
26 */ |
|
27 public class LocaleAware { |
|
28 @TargetApi(Build.VERSION_CODES.GINGERBREAD) |
|
29 public static void initializeLocale(Context context) { |
|
30 final LocaleManager localeManager = BrowserLocaleManager.getInstance(); |
|
31 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) { |
|
32 localeManager.getAndApplyPersistedLocale(context); |
|
33 } else { |
|
34 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); |
|
35 StrictMode.allowThreadDiskWrites(); |
|
36 try { |
|
37 localeManager.getAndApplyPersistedLocale(context); |
|
38 } finally { |
|
39 StrictMode.setThreadPolicy(savedPolicy); |
|
40 } |
|
41 } |
|
42 } |
|
43 |
|
44 public static class LocaleAwareFragmentActivity extends FragmentActivity { |
|
45 @Override |
|
46 protected void onCreate(Bundle savedInstanceState) { |
|
47 LocaleAware.initializeLocale(getApplicationContext()); |
|
48 super.onCreate(savedInstanceState); |
|
49 } |
|
50 } |
|
51 |
|
52 public static class LocaleAwareActivity extends Activity { |
|
53 @Override |
|
54 protected void onCreate(Bundle savedInstanceState) { |
|
55 LocaleAware.initializeLocale(getApplicationContext()); |
|
56 super.onCreate(savedInstanceState); |
|
57 } |
|
58 } |
|
59 } |