/*
  * Creates toggles for each available location provider
  */
 private void updateLocationToggles() {
   ContentResolver res = getContentResolver();
   boolean gpsEnabled =
       Settings.Secure.isLocationProviderEnabled(res, LocationManager.GPS_PROVIDER);
   mNetwork.setChecked(
       Settings.Secure.isLocationProviderEnabled(res, LocationManager.NETWORK_PROVIDER));
   mGps.setChecked(gpsEnabled);
   if (mAssistedGps != null) {
     mAssistedGps.setChecked(
         Settings.Secure.getInt(res, Settings.Secure.ASSISTED_GPS_ENABLED, 2) == 1);
     mAssistedGps.setEnabled(gpsEnabled);
   }
 }
 /*
  * Creates toggles for each available location provider
  */
 private void updateLocationToggles() {
   ContentResolver res = getContentResolver();
   boolean gpsEnabled =
       Settings.Secure.isLocationProviderEnabled(res, LocationManager.GPS_PROVIDER);
   boolean networkEnabled =
       Settings.Secure.isLocationProviderEnabled(res, LocationManager.NETWORK_PROVIDER);
   mGps.setChecked(gpsEnabled);
   mNetwork.setChecked(networkEnabled);
   mLocationAccess.setChecked(gpsEnabled || networkEnabled);
   if (mAssistedGps != null) {
     mAssistedGps.setChecked(
         Settings.Global.getInt(res, Settings.Global.ASSISTED_GPS_ENABLED, 2) == 1);
     mAssistedGps.setEnabled(gpsEnabled);
   }
   if (mGpsDownloadDataWifiOnly != null) {
     mGpsDownloadDataWifiOnly.setEnabled(gpsEnabled);
   }
 }
  private Boolean displayGpsStatus() {
    ContentResolver contentResolver = getBaseContext().getContentResolver();
    boolean gpsStatus =
        Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
    if (gpsStatus) {
      return true;

    } else {
      return false;
    }
  }
 private void setOffMessage() {
   if (mEmptyView != null) {
     mEmptyView.setText(R.string.wifi_empty_list_wifi_off);
     if (Settings.Global.getInt(
             getActivity().getContentResolver(), Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0)
         == 1) {
       mEmptyView.append("\n\n");
       int resId;
       if (Settings.Secure.isLocationProviderEnabled(
           getActivity().getContentResolver(), LocationManager.NETWORK_PROVIDER)) {
         resId = R.string.wifi_scan_notify_text_location_on;
       } else {
         resId = R.string.wifi_scan_notify_text_location_off;
       }
       CharSequence charSeq = getText(resId);
       mEmptyView.append(charSeq);
     }
   }
   getPreferenceScreen().removeAll();
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs_weather);

    prefs = getActivity().getSharedPreferences("weather", Context.MODE_PRIVATE);

    mWeatherSyncInterval = (ListPreference) findPreference("refresh_interval");
    mWeatherSyncInterval.setOnPreferenceChangeListener(this);
    mWeatherSyncInterval.setSummary(
        Integer.toString(WeatherPrefs.getRefreshInterval(mContext)) + " minutes");

    mCustomWeatherLoc = (EditTextPreference) findPreference("custom_location");
    mCustomWeatherLoc.setOnPreferenceChangeListener(this);
    mCustomWeatherLoc.setSummary(WeatherPrefs.getCustomLocation(mContext));

    mEnableWeather = (CheckBoxPreference) findPreference("enable_weather");
    mEnableWeather.setChecked(
        Settings.System.getInt(getContentResolver(), Settings.System.USE_WEATHER, 0) == 1);

    mUseCustomLoc = (CheckBoxPreference) findPreference(WeatherPrefs.KEY_USE_CUSTOM_LOCATION);
    mUseCustomLoc.setChecked(WeatherPrefs.getUseCustomLocation(mContext));

    mShowLoc = (CheckBoxPreference) findPreference("show_location");
    mShowLoc.setChecked(
        Settings.System.getInt(getContentResolver(), Settings.System.WEATHER_SHOW_LOCATION, 0)
            == 1);

    mUseCelcius = (CheckBoxPreference) findPreference(WeatherPrefs.KEY_USE_CELCIUS);
    mUseCelcius.setChecked(WeatherPrefs.getUseCelcius(mContext));

    setHasOptionsMenu(true);

    if (!Settings.Secure.isLocationProviderEnabled(
            getContentResolver(), LocationManager.NETWORK_PROVIDER)
        && !mUseCustomLoc.isChecked()) {
      showDialog(LOC_WARNING);
    }
  }
  @Override
  protected void onHandleIntent(Intent intent) {
    WeatherInfo w = null;
    String extra = null;
    String action = intent.getAction();
    String woeid = null;
    Context context = getApplicationContext();

    if (Settings.System.getInt(getContentResolver(), Settings.System.USE_WEATHER, 0) == 0) {
      stopSelf();
      return;
    }

    if (!Settings.Secure.isLocationProviderEnabled(
            getContentResolver(), LocationManager.NETWORK_PROVIDER)
        && !WeatherPrefs.getUseCustomLocation(getApplicationContext())) {
      stopSelf();
      return;
    }

    if (action != null && action.equals(INTENT_WEATHER_REQUEST)) {
      // custom location
      boolean useCustomLoc = WeatherPrefs.getUseCustomLocation(getApplicationContext());
      String customLoc = WeatherPrefs.getCustomLocation(getApplicationContext());
      boolean manual = false;
      Bundle extras = intent.getExtras();
      if (extras != null) {
        manual = extras.getBoolean(INTENT_EXTRA_ISMANUAL, false);
      }
      if (customLoc != null && useCustomLoc) {
        if (manual) {
          makeToast(context.getString(R.string.weather_refreshing));
        }
        woeid = YahooPlaceFinder.GeoCode(getApplicationContext(), customLoc);
        // network location
      } else {
        // do not attempt to get a location without data
        boolean networkAvailable = Helpers.isNetworkAvailable(getApplicationContext());
        if (networkAvailable) {
          if (manual) {
            makeToast(context.getString(R.string.weather_refreshing));
          }
          final LocationManager locationManager =
              (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
          if (!intent.hasExtra(INTENT_EXTRA_NEWLOCATION)) {
            intent.putExtra(INTENT_EXTRA_NEWLOCATION, true);
            PendingIntent pi =
                PendingIntent.getService(
                    getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, pi);
            return;
          }

          Criteria crit = new Criteria();
          crit.setAccuracy(Criteria.ACCURACY_COARSE);
          String bestProvider = locationManager.getBestProvider(crit, true);
          Location loc = null;
          if (bestProvider != null) {
            loc = locationManager.getLastKnownLocation(bestProvider);
          } else {
            loc = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
          }
          try {
            woeid =
                YahooPlaceFinder.reverseGeoCode(
                    getApplicationContext(), loc.getLatitude(), loc.getLongitude());
          } catch (Exception e) {
            e.printStackTrace();
          }
        } else {
          if (manual) {
            makeToast(context.getString(R.string.location_unavailable));
          }
          stopSelf();
          return;
        }
      }
      try {
        w = parseXml(getDocument(woeid));
        if (w != null) {
          sendBroadcast(w);
          updateLatest(w);
        }
      } catch (Exception e) {
        Log.e(TAG, "ohnoes: " + e.getMessage());
      }
    }
    stopSelf();
  }