mobile/android/base/preferences/GeckoPreferenceFragment.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial