michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- 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.preferences; michael@0: michael@0: import java.lang.reflect.Field; michael@0: michael@0: import org.mozilla.gecko.GeckoSharedPrefs; michael@0: import org.mozilla.gecko.PrefsHelper; michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.app.Activity; michael@0: import android.os.Bundle; michael@0: import android.preference.PreferenceActivity; michael@0: import android.preference.PreferenceFragment; michael@0: import android.preference.PreferenceScreen; michael@0: import android.util.Log; michael@0: import android.view.Menu; michael@0: import android.view.MenuInflater; michael@0: import android.view.ViewConfiguration; michael@0: michael@0: /* A simple implementation of PreferenceFragment for large screen devices michael@0: * This will strip category headers (so that they aren't shown to the user twice) michael@0: * as well as initializing Gecko prefs when a fragment is shown. michael@0: */ michael@0: public class GeckoPreferenceFragment extends PreferenceFragment { michael@0: michael@0: private static final String LOGTAG = "GeckoPreferenceFragment"; michael@0: private int mPrefsRequestId = 0; michael@0: michael@0: @Override michael@0: public void onCreate(Bundle savedInstanceState) { michael@0: super.onCreate(savedInstanceState); michael@0: michael@0: // Write prefs to our custom GeckoSharedPrefs file. michael@0: getPreferenceManager().setSharedPreferencesName(GeckoSharedPrefs.APP_PREFS_NAME); michael@0: michael@0: int res = getResource(); michael@0: michael@0: // Display a menu for Search preferences. michael@0: if (res == R.xml.preferences_search) { michael@0: setHasOptionsMenu(true); michael@0: } michael@0: michael@0: addPreferencesFromResource(res); michael@0: michael@0: PreferenceScreen screen = getPreferenceScreen(); michael@0: setPreferenceScreen(screen); michael@0: mPrefsRequestId = ((GeckoPreferences)getActivity()).setupPreferences(screen); michael@0: } michael@0: michael@0: /* michael@0: * Get the resource from Fragment arguments and return it. michael@0: * michael@0: * If no resource can be found, return the resource id of the default preference screen. michael@0: */ michael@0: private int getResource() { michael@0: int resid = 0; michael@0: michael@0: String resourceName = getArguments().getString("resource"); michael@0: if (resourceName != null) { michael@0: // Fetch resource id by resource name. michael@0: resid = getActivity().getResources().getIdentifier(resourceName, michael@0: "xml", michael@0: getActivity().getPackageName()); michael@0: } michael@0: michael@0: if (resid == 0) { michael@0: // The resource was invalid. Use the default resource. michael@0: Log.e(LOGTAG, "Failed to find resource: " + resourceName + ". Displaying default settings."); michael@0: michael@0: boolean isMultiPane = ((PreferenceActivity) getActivity()).onIsMultiPane(); michael@0: resid = isMultiPane ? R.xml.preferences_customize_tablet : R.xml.preferences; michael@0: } michael@0: michael@0: return resid; michael@0: } michael@0: michael@0: @Override michael@0: public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { michael@0: super.onCreateOptionsMenu(menu, inflater); michael@0: inflater.inflate(R.menu.preferences_search_menu, menu); michael@0: } michael@0: michael@0: @Override michael@0: public void onDestroy() { michael@0: super.onDestroy(); michael@0: if (mPrefsRequestId > 0) { michael@0: PrefsHelper.removeObserver(mPrefsRequestId); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void onAttach(Activity activity) { michael@0: super.onAttach(activity); michael@0: showOverflowMenu(activity); michael@0: } michael@0: michael@0: /* michael@0: * Force the overflow 3-dot menu to be displayed if it isn't already displayed. michael@0: * michael@0: * This is an ugly hack for 4.0+ Android devices that don't have a dedicated menu button michael@0: * because Android does not provide a public API to display the ActionBar overflow menu. michael@0: */ michael@0: private void showOverflowMenu(Activity activity) { michael@0: try { michael@0: ViewConfiguration config = ViewConfiguration.get(activity); michael@0: Field menuOverflow = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); michael@0: if (menuOverflow != null) { michael@0: menuOverflow.setAccessible(true); michael@0: menuOverflow.setBoolean(config, false); michael@0: } michael@0: } catch (Exception e) { michael@0: Log.d(LOGTAG, "Failed to force overflow menu, ignoring."); michael@0: } michael@0: } michael@0: }