예제 #1
0
 private Document getDocument(String woeid) {
   try {
     boolean celcius = WeatherPrefs.getUseCelcius(getApplicationContext());
     String urlWithDegreeUnit;
     if (celcius) {
       urlWithDegreeUnit = URL_YAHOO_API_WEATHER + "c";
     } else {
       urlWithDegreeUnit = URL_YAHOO_API_WEATHER + "f";
     }
     return httpRetriever.getDocumentFromURL(String.format(urlWithDegreeUnit, woeid));
   } catch (IOException e) {
     Log.e(TAG, e.toString());
   }
   return null;
 }
예제 #2
0
  @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();
  }