|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.preferences; |
|
7 |
|
8 import java.lang.reflect.Field; |
|
9 |
|
10 import org.mozilla.gecko.GeckoSharedPrefs; |
|
11 import org.mozilla.gecko.PrefsHelper; |
|
12 import org.mozilla.gecko.R; |
|
13 |
|
14 import android.app.Activity; |
|
15 import android.os.Bundle; |
|
16 import android.preference.PreferenceActivity; |
|
17 import android.preference.PreferenceFragment; |
|
18 import android.preference.PreferenceScreen; |
|
19 import android.util.Log; |
|
20 import android.view.Menu; |
|
21 import android.view.MenuInflater; |
|
22 import android.view.ViewConfiguration; |
|
23 |
|
24 /* A simple implementation of PreferenceFragment for large screen devices |
|
25 * This will strip category headers (so that they aren't shown to the user twice) |
|
26 * as well as initializing Gecko prefs when a fragment is shown. |
|
27 */ |
|
28 public class GeckoPreferenceFragment extends PreferenceFragment { |
|
29 |
|
30 private static final String LOGTAG = "GeckoPreferenceFragment"; |
|
31 private int mPrefsRequestId = 0; |
|
32 |
|
33 @Override |
|
34 public void onCreate(Bundle savedInstanceState) { |
|
35 super.onCreate(savedInstanceState); |
|
36 |
|
37 // Write prefs to our custom GeckoSharedPrefs file. |
|
38 getPreferenceManager().setSharedPreferencesName(GeckoSharedPrefs.APP_PREFS_NAME); |
|
39 |
|
40 int res = getResource(); |
|
41 |
|
42 // Display a menu for Search preferences. |
|
43 if (res == R.xml.preferences_search) { |
|
44 setHasOptionsMenu(true); |
|
45 } |
|
46 |
|
47 addPreferencesFromResource(res); |
|
48 |
|
49 PreferenceScreen screen = getPreferenceScreen(); |
|
50 setPreferenceScreen(screen); |
|
51 mPrefsRequestId = ((GeckoPreferences)getActivity()).setupPreferences(screen); |
|
52 } |
|
53 |
|
54 /* |
|
55 * Get the resource from Fragment arguments and return it. |
|
56 * |
|
57 * If no resource can be found, return the resource id of the default preference screen. |
|
58 */ |
|
59 private int getResource() { |
|
60 int resid = 0; |
|
61 |
|
62 String resourceName = getArguments().getString("resource"); |
|
63 if (resourceName != null) { |
|
64 // Fetch resource id by resource name. |
|
65 resid = getActivity().getResources().getIdentifier(resourceName, |
|
66 "xml", |
|
67 getActivity().getPackageName()); |
|
68 } |
|
69 |
|
70 if (resid == 0) { |
|
71 // The resource was invalid. Use the default resource. |
|
72 Log.e(LOGTAG, "Failed to find resource: " + resourceName + ". Displaying default settings."); |
|
73 |
|
74 boolean isMultiPane = ((PreferenceActivity) getActivity()).onIsMultiPane(); |
|
75 resid = isMultiPane ? R.xml.preferences_customize_tablet : R.xml.preferences; |
|
76 } |
|
77 |
|
78 return resid; |
|
79 } |
|
80 |
|
81 @Override |
|
82 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
|
83 super.onCreateOptionsMenu(menu, inflater); |
|
84 inflater.inflate(R.menu.preferences_search_menu, menu); |
|
85 } |
|
86 |
|
87 @Override |
|
88 public void onDestroy() { |
|
89 super.onDestroy(); |
|
90 if (mPrefsRequestId > 0) { |
|
91 PrefsHelper.removeObserver(mPrefsRequestId); |
|
92 } |
|
93 } |
|
94 |
|
95 @Override |
|
96 public void onAttach(Activity activity) { |
|
97 super.onAttach(activity); |
|
98 showOverflowMenu(activity); |
|
99 } |
|
100 |
|
101 /* |
|
102 * Force the overflow 3-dot menu to be displayed if it isn't already displayed. |
|
103 * |
|
104 * This is an ugly hack for 4.0+ Android devices that don't have a dedicated menu button |
|
105 * because Android does not provide a public API to display the ActionBar overflow menu. |
|
106 */ |
|
107 private void showOverflowMenu(Activity activity) { |
|
108 try { |
|
109 ViewConfiguration config = ViewConfiguration.get(activity); |
|
110 Field menuOverflow = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); |
|
111 if (menuOverflow != null) { |
|
112 menuOverflow.setAccessible(true); |
|
113 menuOverflow.setBoolean(config, false); |
|
114 } |
|
115 } catch (Exception e) { |
|
116 Log.d(LOGTAG, "Failed to force overflow menu, ignoring."); |
|
117 } |
|
118 } |
|
119 } |