/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.location; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; import android.util.Log; import android.view.Gravity; import android.widget.Switch; import com.android.settings.R; import com.android.settings.SettingsPreferenceFragment; /** * Location access settings. */ public class LocationSettings extends SettingsPreferenceFragment implements Preference.OnPreferenceChangeListener { private static final String TAG = LocationSettings.class.getSimpleName(); private static final String KEY_LOCATION_MODE = "location_mode"; private PreferenceScreen mLocationMode; private LocationEnabler mLocationEnabler; @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); createPreferenceHierarchy(); } @Override public void onResume() { super.onResume(); // Make sure we reload the preference hierarchy since some of these settings // depend on others... createPreferenceHierarchy(); } private PreferenceScreen createPreferenceHierarchy() { PreferenceScreen root = getPreferenceScreen(); if (root != null) { root.removeAll(); } addPreferencesFromResource(R.xml.location_settings); root = getPreferenceScreen(); mLocationMode = (PreferenceScreen) root.findPreference((KEY_LOCATION_MODE)); mLocationMode.setOnPreferenceClickListener( new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { PreferenceActivity preferenceActivity = (PreferenceActivity) getActivity(); preferenceActivity.startPreferencePanel( LocationMode.class.getName(), null, R.string.location_mode_screen_title, null, LocationSettings.this, 0); return true; } }); Activity activity = getActivity(); Switch actionBarSwitch = new Switch(activity); if (activity instanceof PreferenceActivity) { PreferenceActivity preferenceActivity = (PreferenceActivity) activity; // Only show the master switch when we're not in multi-pane mode, and not being used as // Setup Wizard. if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) { final int padding = activity.getResources().getDimensionPixelSize( R.dimen.action_bar_switch_padding); actionBarSwitch.setPaddingRelative(0, 0, padding, 0); activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM); activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams( ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.END)); } } else { Log.wtf(TAG, "Current activity is not an instance of PreferenceActivity!"); } mLocationEnabler = new LocationEnabler(activity, actionBarSwitch); setHasOptionsMenu(true); return root; } @Override public int getHelpResource() { return R.string.help_url_location_access; } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { return true; } }