mobile/android/base/preferences/GeckoPreferenceFragment.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/preferences/GeckoPreferenceFragment.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,119 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.preferences;
    1.10 +
    1.11 +import java.lang.reflect.Field;
    1.12 +
    1.13 +import org.mozilla.gecko.GeckoSharedPrefs;
    1.14 +import org.mozilla.gecko.PrefsHelper;
    1.15 +import org.mozilla.gecko.R;
    1.16 +
    1.17 +import android.app.Activity;
    1.18 +import android.os.Bundle;
    1.19 +import android.preference.PreferenceActivity;
    1.20 +import android.preference.PreferenceFragment;
    1.21 +import android.preference.PreferenceScreen;
    1.22 +import android.util.Log;
    1.23 +import android.view.Menu;
    1.24 +import android.view.MenuInflater;
    1.25 +import android.view.ViewConfiguration;
    1.26 +
    1.27 +/* A simple implementation of PreferenceFragment for large screen devices
    1.28 + * This will strip category headers (so that they aren't shown to the user twice)
    1.29 + * as well as initializing Gecko prefs when a fragment is shown.
    1.30 +*/
    1.31 +public class GeckoPreferenceFragment extends PreferenceFragment {
    1.32 +
    1.33 +    private static final String LOGTAG = "GeckoPreferenceFragment";
    1.34 +    private int mPrefsRequestId = 0;
    1.35 +
    1.36 +    @Override
    1.37 +    public void onCreate(Bundle savedInstanceState) {
    1.38 +        super.onCreate(savedInstanceState);
    1.39 +
    1.40 +        // Write prefs to our custom GeckoSharedPrefs file.
    1.41 +        getPreferenceManager().setSharedPreferencesName(GeckoSharedPrefs.APP_PREFS_NAME);
    1.42 +
    1.43 +        int res = getResource();
    1.44 +
    1.45 +        // Display a menu for Search preferences.
    1.46 +        if (res == R.xml.preferences_search) {
    1.47 +            setHasOptionsMenu(true);
    1.48 +        }
    1.49 +
    1.50 +        addPreferencesFromResource(res);
    1.51 +
    1.52 +        PreferenceScreen screen = getPreferenceScreen();
    1.53 +        setPreferenceScreen(screen);
    1.54 +        mPrefsRequestId = ((GeckoPreferences)getActivity()).setupPreferences(screen);
    1.55 +    }
    1.56 +
    1.57 +    /*
    1.58 +     * Get the resource from Fragment arguments and return it.
    1.59 +     *
    1.60 +     * If no resource can be found, return the resource id of the default preference screen.
    1.61 +     */
    1.62 +    private int getResource() {
    1.63 +        int resid = 0;
    1.64 +
    1.65 +        String resourceName = getArguments().getString("resource");
    1.66 +        if (resourceName != null) {
    1.67 +            // Fetch resource id by resource name.
    1.68 +            resid = getActivity().getResources().getIdentifier(resourceName,
    1.69 +                                                             "xml",
    1.70 +                                                             getActivity().getPackageName());
    1.71 +        }
    1.72 +
    1.73 +        if (resid == 0) {
    1.74 +            // The resource was invalid. Use the default resource.
    1.75 +            Log.e(LOGTAG, "Failed to find resource: " + resourceName + ". Displaying default settings.");
    1.76 +
    1.77 +            boolean isMultiPane = ((PreferenceActivity) getActivity()).onIsMultiPane();
    1.78 +            resid = isMultiPane ? R.xml.preferences_customize_tablet : R.xml.preferences;
    1.79 +        }
    1.80 +
    1.81 +        return resid;
    1.82 +    }
    1.83 +
    1.84 +    @Override
    1.85 +    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    1.86 +        super.onCreateOptionsMenu(menu, inflater);
    1.87 +        inflater.inflate(R.menu.preferences_search_menu, menu);
    1.88 +    }
    1.89 +
    1.90 +    @Override
    1.91 +    public void onDestroy() {
    1.92 +        super.onDestroy();
    1.93 +        if (mPrefsRequestId > 0) {
    1.94 +            PrefsHelper.removeObserver(mPrefsRequestId);
    1.95 +        }
    1.96 +    }
    1.97 +
    1.98 +    @Override
    1.99 +    public void onAttach(Activity activity) {
   1.100 +        super.onAttach(activity);
   1.101 +        showOverflowMenu(activity);
   1.102 +    }
   1.103 +
   1.104 +    /*
   1.105 +     * Force the overflow 3-dot menu to be displayed if it isn't already displayed.
   1.106 +     *
   1.107 +     * This is an ugly hack for 4.0+ Android devices that don't have a dedicated menu button
   1.108 +     * because Android does not provide a public API to display the ActionBar overflow menu.
   1.109 +     */
   1.110 +    private void showOverflowMenu(Activity activity) {
   1.111 +        try {
   1.112 +            ViewConfiguration config = ViewConfiguration.get(activity);
   1.113 +            Field menuOverflow = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
   1.114 +            if (menuOverflow != null) {
   1.115 +                menuOverflow.setAccessible(true);
   1.116 +                menuOverflow.setBoolean(config, false);
   1.117 +            }
   1.118 +        } catch (Exception e) {
   1.119 +            Log.d(LOGTAG, "Failed to force overflow menu, ignoring.");
   1.120 +        }
   1.121 +    }
   1.122 +}

mercurial