Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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/. */
6 package org.mozilla.gecko.preferences;
8 import java.lang.reflect.Field;
10 import org.mozilla.gecko.GeckoSharedPrefs;
11 import org.mozilla.gecko.PrefsHelper;
12 import org.mozilla.gecko.R;
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;
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 {
30 private static final String LOGTAG = "GeckoPreferenceFragment";
31 private int mPrefsRequestId = 0;
33 @Override
34 public void onCreate(Bundle savedInstanceState) {
35 super.onCreate(savedInstanceState);
37 // Write prefs to our custom GeckoSharedPrefs file.
38 getPreferenceManager().setSharedPreferencesName(GeckoSharedPrefs.APP_PREFS_NAME);
40 int res = getResource();
42 // Display a menu for Search preferences.
43 if (res == R.xml.preferences_search) {
44 setHasOptionsMenu(true);
45 }
47 addPreferencesFromResource(res);
49 PreferenceScreen screen = getPreferenceScreen();
50 setPreferenceScreen(screen);
51 mPrefsRequestId = ((GeckoPreferences)getActivity()).setupPreferences(screen);
52 }
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;
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 }
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.");
74 boolean isMultiPane = ((PreferenceActivity) getActivity()).onIsMultiPane();
75 resid = isMultiPane ? R.xml.preferences_customize_tablet : R.xml.preferences;
76 }
78 return resid;
79 }
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 }
87 @Override
88 public void onDestroy() {
89 super.onDestroy();
90 if (mPrefsRequestId > 0) {
91 PrefsHelper.removeObserver(mPrefsRequestId);
92 }
93 }
95 @Override
96 public void onAttach(Activity activity) {
97 super.onAttach(activity);
98 showOverflowMenu(activity);
99 }
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 }