Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.38 KB | None | 0 0
  1. private boolean isRuntimePostGingerbread() {
  2. return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
  3. }
  4.  
  5. public void loadWeathercard() {
  6. //Set the date
  7. SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  8. citylb = (TextView) findViewById(R.id.citybox);
  9. //Check if the custom weather place preference is enabled
  10. if (!sharedPrefs.getBoolean("weather_customlocationcheck", false)) {
  11. citylb.setText(getString(R.string.rilevamento));
  12.  
  13. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  14. /**
  15. * Check the provider exists before registering for updates from it
  16. * */
  17. if (locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
  18. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, this);
  19.  
  20. if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))
  21. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, this);
  22. locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  23. } else {
  24. //Get the custom city, refresh the city label and get the weather
  25. citylb.setText(sharedPrefs.getString("weather_customlocation", getString(R.string.location_null)));
  26. getWeather((String) citylb.getText());
  27. }
  28. }
  29.  
  30. /**
  31. * Function to check if the device is online
  32. */
  33. /**
  34. * Default method
  35. */
  36.  
  37. public Boolean isOnline() {
  38. ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  39. NetworkInfo ni = cm.getActiveNetworkInfo();
  40. if (ni != null && ni.isConnected()){
  41. return true;
  42. // This is method suggested.. Have i use this?
  43. }
  44. return false;
  45. }
  46. private void getWeather(String location) {
  47. if (isOnline()) {
  48. Log.d("YWeatherGetter4a", "onCreate");
  49. YahooWeatherUtils yahooWeatherUtils = YahooWeatherUtils.getInstance();
  50. yahooWeatherUtils.queryYahooWeather(getApplicationContext(), location, this); // Basta mettere la location presa dalle preferences
  51. } else
  52. Toast.makeText(getApplicationContext(), "Sorry, no connection available", Toast.LENGTH_SHORT).show();
  53.  
  54. }
  55. /**
  56. * Rilevo la posizione attuale
  57. * */
  58.  
  59. public void gotWeatherInfo(final WeatherInfo weatherInfo) {
  60. if (weatherInfo != null) {
  61. current = weatherInfo.getCurrentText().toString();
  62. // Converto in stringa la temperatura
  63. TempCurrent = String.valueOf(weatherInfo.getCurrentTempC());
  64. String TempHigh = String.valueOf(weatherInfo.getForecast1TempHighC());
  65. String TempLow = String.valueOf(weatherInfo.getForecast1TempLowC());
  66. // + weatherInfo.getAtmosphereHumidity() + "%" per l'umidit
  67. /* LoadWebImagesTask task = new LoadWebImagesTask();
  68. task.execute(
  69.  
  70. weatherInfo.getCurrentConditionIconURL()
  71. );*/
  72. Time oggi = new Time(Time.getCurrentTimezone());
  73. oggi.setToNow();
  74. String orariometeo = oggi.format("%k:%M");
  75.  
  76. ImageView imgView = (ImageView)findViewById(R.id.imageview_forecast_info);
  77. if(current.toLowerCase().contains("sunny")){
  78. current = getResources().getString(R.string.sunny);
  79. Drawable myDrawable = getResources().getDrawable(R.drawable.sunny);
  80. imgView.setImageDrawable(myDrawable);
  81.  
  82. } else if (current.toLowerCase().contains("partly cloudy")) {
  83. current = getResources().getString(R.string.partlycloudy);
  84. Drawable myDrawable = getResources().getDrawable(R.drawable.partlycloudy);
  85. imgView.setImageDrawable(myDrawable);
  86.  
  87. } else if (current.toLowerCase().contains("mostly cloudy")) {
  88. current = getResources().getString(R.string.cloudy);
  89. Drawable myDrawable = getResources().getDrawable(R.drawable.mostlycloudy);
  90. imgView.setImageDrawable(myDrawable);
  91.  
  92. } else if (current.toLowerCase().contains("fair")) {
  93. current = getResources().getString(R.string.fair);
  94. Drawable myDrawable = getResources().getDrawable(R.drawable.clear);
  95. imgView.setImageDrawable(myDrawable);
  96.  
  97. } else if (current.toLowerCase().contains("showers")) {
  98. current = getResources().getString(R.string.showers);
  99. Drawable myDrawable = getResources().getDrawable(R.drawable.rain);
  100. imgView.setImageDrawable(myDrawable);
  101.  
  102. } else if (current.toLowerCase().contains("foggy")) {
  103. current = getResources().getString(R.string.foggy);
  104. Drawable myDrawable = getResources().getDrawable(R.drawable.mist);
  105. imgView.setImageDrawable(myDrawable);
  106.  
  107. } else if (current.toLowerCase().contains("thunderstorms")) {
  108. current = getResources().getString(R.string.thunderstorms);
  109. Drawable myDrawable = getResources().getDrawable(R.drawable.thunderstorm);
  110. imgView.setImageDrawable(myDrawable);
  111.  
  112. } else if (current.toLowerCase().contains("thundershowers")) {
  113. current = getResources().getString(R.string.thundershowers);
  114. Drawable myDrawable = getResources().getDrawable(R.drawable.storm);
  115. imgView.setImageDrawable(myDrawable);
  116.  
  117. } else if (current.toLowerCase().contains("freezingdrizzle") || current.toLowerCase().contains("hail")) {
  118. current = getResources().getString(R.string.freezingdrizzle);
  119. Drawable myDrawable = getResources().getDrawable(R.drawable.freezingdrizzle);
  120. imgView.setImageDrawable(myDrawable);
  121.  
  122. } else if (current.toLowerCase().contains("lightsnowshowers")) {
  123. current = getResources().getString(R.string.lightsnowshowers);
  124. Drawable myDrawable = getResources().getDrawable(R.drawable.chanceofsnow);
  125. imgView.setImageDrawable(myDrawable);
  126.  
  127. } else if (current.toLowerCase().contains("clear") || current.toLowerCase().contains("hot")) {
  128. current = getResources().getString(R.string.clear);
  129. Drawable myDrawable = getResources().getDrawable(R.drawable.clear);
  130. imgView.setImageDrawable(myDrawable);
  131.  
  132. } else if (current.toLowerCase().contains("cloudy")) {
  133. current = getResources().getString(R.string.cloudy);
  134. Drawable myDrawable = getResources().getDrawable(R.drawable.cloudy);
  135. imgView.setImageDrawable(myDrawable);
  136.  
  137. } else if (current.toLowerCase().contains("snow")) {
  138. current = getResources().getString(R.string.snow);
  139. Drawable myDrawable = getResources().getDrawable(R.drawable.snow);
  140. imgView.setImageDrawable(myDrawable);
  141.  
  142. } else if (current.toLowerCase().contains("drizzle")) {
  143. current = getResources().getString(R.string.drizzle);
  144. Drawable myDrawable = getResources().getDrawable(R.drawable.lightrain);
  145. imgView.setImageDrawable(myDrawable);
  146.  
  147. } else if (current.toLowerCase().contains("rain") || current.toLowerCase().contains("showers")) {
  148. current = getResources().getString(R.string.showers);
  149. Drawable myDrawable = getResources().getDrawable(R.drawable.rain);
  150. imgView.setImageDrawable(myDrawable);
  151.  
  152. }
  153.  
  154. temperaturenow = (TextView) findViewById(R.id.temperaturenow);
  155. temperaturenow.setText(current + " ");
  156.  
  157. temperatura = (TextView)findViewById(R.id.temperatura);
  158. temperatura.setText(TempCurrent + "°");
  159.  
  160. maxmin = (TextView)findViewById(R.id.maxmin);
  161. maxmin.setText(Html.fromHtml(TempHigh + "/" +"<small>"+TempLow+"</small>" +"°C"));
  162.  
  163. imgView.setOnClickListener(new View.OnClickListener(){
  164. @Override
  165. public void onClick(View view){
  166. boolean installed = appInstalledOrNot("com.yahoo.mobile.client.android.weather");
  167. if(installed) {
  168. String meteoinfo = weatherInfo.getWeatherurl();
  169. //This intent will help you to launch if the package is already installed
  170. Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.yahoo.mobile.client.android.weather");
  171. LaunchIntent.putExtra(SearchManager.QUERY, meteoinfo);
  172. startActivity(LaunchIntent);
  173.  
  174. } else {
  175. Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(weatherInfo.getWeatherurl()));
  176. startActivity(i);
  177. }
  178. }
  179. });
  180. } else {
  181. Toast.makeText(getApplicationContext(), "Sorry, no result returned", Toast.LENGTH_SHORT).show();
  182. }
  183. }
  184.  
  185. class LoadWebImagesTask extends AsyncTask<String, Void, Bitmap[]> {
  186.  
  187. @Override
  188. protected Bitmap[] doInBackground(String... params) {
  189. Bitmap[] res = new Bitmap[6];
  190. res[0] = ImageUtils.getBitmapFromWeb(params[0]);
  191.  
  192. return res;
  193. }
  194.  
  195. @Override
  196. protected void onPostExecute(Bitmap[] results) {
  197. super.onPostExecute(results);
  198. citylb.setCompoundDrawablesWithIntrinsicBounds(null, null, null, new BitmapDrawable(getResources(), results[0]));
  199.  
  200. }
  201.  
  202. }
  203.  
  204.  
  205. public void loadWeathercard1() {
  206. //Set the date
  207. SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  208. citylbpref = (TextView) findViewById(R.id.cityboxpref);
  209. //Check if the custom weather place preference is enabled
  210.  
  211. //Get the custom city, refresh the city label and get the weather
  212. citylbpref.setText(sharedPrefs.getString("weather_customlocation", getString(R.string.location_null)));
  213. getWeather1((String) citylbpref.getText());
  214.  
  215. }
  216.  
  217. @Override
  218. public void onLocationChanged(Location arg0) {
  219. // TODO Auto-generated method stub
  220. List<Address> addresses = null;
  221. if (arg0 != null){
  222. double longitude = arg0.getLongitude();
  223. double latitude = arg0.getLatitude();
  224. Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
  225.  
  226. try {
  227. addresses = gcd.getFromLocation(latitude, longitude, 1);
  228. } catch (IOException e) {
  229. // TODO Auto-generated catch block
  230. Log.e("MyTag", "MyMessage", e); // use this instead e.printStackTrace();
  231. }
  232.  
  233. if(addresses != null && addresses.size() > 0){
  234. citylb.setText(addresses.get(0).getLocality());
  235.  
  236. getWeather((String) citylb.getText());
  237. }else{
  238. citylb.setText(getString(R.string.location_null));
  239. }
  240. //citylb = (TextView) findViewById(R.id.citybox);
  241.  
  242.  
  243. //if (addresses.size() > 0) return addresses.get(0).getLocality();
  244. }
  245.  
  246. }
  247.  
  248. yahooWeatherUtils.queryYahooWeather(getApplicationContext(), location, this);
  249.  
  250. yahooWeatherUtils.queryYahooWeather(getApplicationContext(), "New York", this);
  251.  
  252. Location location = locationClient.getLastLocation();
  253.  
  254. YahooWeatherUtils ywu = YahooWeatherUtils.getInstance();
  255. if PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("weatherUseCurrentLocation", true) && location != null) {
  256. ywu.queryYahooWeather(getActivity(), location , this);
  257. } else {
  258. String city = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("weatherCity", "Vancouver Canada");
  259. ywu.queryYahooWeather(getActivity(), city , this);
  260. }
  261.  
  262. public class KmlReader extends ActionBarActivity implements
  263.  
  264. GooglePlayServicesClient.ConnectionCallbacks,
  265. GooglePlayServicesClient.OnConnectionFailedListener, LocationListener
  266. {
  267.  
  268. ...
  269.  
  270. if (locationManager == null) {
  271. locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  272. locationManager.addGpsStatusListener(this);
  273. }
  274.  
  275.  
  276.  
  277.  
  278. * Called by Location Services when the request to connect the client
  279. * finishes successfully. At this point, you can request the current
  280. * location or start periodic updates
  281. */
  282. @Override
  283. public void onConnected(Bundle dataBundle) {
  284. if (action_track) {
  285. if (mLocationRequest == null) {
  286. // Create a new global location parameters object
  287. mLocationRequest = LocationRequest.create();
  288. // Set the update interval
  289. mLocationRequest.setInterval(UPDATE_INTERVAL);
  290. // Use high accuracy
  291. mLocationRequest
  292. .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  293. // Set the interval ceiling to one minute
  294. mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
  295. }
  296. mLocationClient.requestLocationUpdates(mLocationRequest, this);
  297. } else {
  298. stopLocationUpdates();
  299. }
  300. }
  301.  
  302. @Override
  303. public void onLocationChanged(Location location) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement