use of com.a5corp.weather.model.WeatherInfo in project Weather by Sparker0i.
the class LargeWidgetProvider method loadFromPreference.
private void loadFromPreference(Prefs preferences, RemoteViews remoteViews, AppWidgetManager appWidgetManager, int[] appWidgetIds, int widgetId) {
WeatherInfo json;
if (preferences.getLargeWidget() != null)
json = new Gson().fromJson(preferences.getLargeWidget(), WeatherInfo.class);
else
return;
double temp = json.getMain().getTemp();
Intent intent = new Intent(context, LargeWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widget_button_refresh, pendingIntent);
remoteViews.setTextViewText(R.id.widget_city, json.getName() + ", " + json.getSys().getCountry());
String ut = new Prefs(context).getUnits().equals("metric") ? "C" : "F";
remoteViews.setTextViewText(R.id.widget_temperature, Integer.toString((int) temp) + "°" + ut);
setWeatherIcon(json.getWeather().get(0).getId(), context, remoteViews);
String rs = json.getWeather().get(0).getDescription();
String[] strArray = rs.split(" ");
StringBuilder builder = new StringBuilder();
for (String s : strArray) {
String cap = s.substring(0, 1).toUpperCase() + s.substring(1);
builder.append(cap.concat(" "));
}
remoteViews.setTextViewText(R.id.widget_description, builder.toString());
remoteViews.setTextViewText(R.id.widget_wind, "Wind : " + json.getWind().getSpeed() + " m/" + (preferences.getUnits().equals("metric") ? "s" : "h"));
remoteViews.setTextViewText(R.id.widget_humidity, "Humidity : " + json.getMain().getHumidity() + " %");
remoteViews.setTextViewText(R.id.widget_pressure, "Pressure : " + json.getMain().getPressure() + " hPa");
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
use of com.a5corp.weather.model.WeatherInfo in project Weather by Sparker0i.
the class FetchWeather method gsonWeather.
private WeatherInfo gsonWeather() throws IOException {
URL day = new URL(builtDay.toString());
HttpURLConnection connection1 = (HttpURLConnection) day.openConnection();
connection1.addRequestProperty("x-api-key", preferences.getWeatherKey());
InputStream content = connection1.getInputStream();
try {
// Read the server response and attempt to parse it as JSON
Reader reader = new InputStreamReader(content);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("M/d/yy hh:mm a");
Gson gson = gsonBuilder.create();
WeatherInfo posts = gson.fromJson(reader, WeatherInfo.class);
System.out.println(gson.toJson(posts));
content.close();
return posts;
} catch (Exception ex) {
Log.e("FetchWeather", "Failed to parse JSON due to: " + ex);
}
return null;
}
use of com.a5corp.weather.model.WeatherInfo in project Weather by Sparker0i.
the class FetchWeatherOther method gsonWeather.
private WeatherInfo gsonWeather() throws IOException {
URL day = new URL(builtDay.toString());
HttpURLConnection connection1 = (HttpURLConnection) day.openConnection();
connection1.addRequestProperty("x-api-key", preferences.getWeatherKey());
InputStream content = connection1.getInputStream();
try {
//Read the server response and attempt to parse it as JSON
Reader reader = new InputStreamReader(content);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("M/d/yy hh:mm a");
Gson gson = gsonBuilder.create();
WeatherInfo posts = gson.fromJson(reader, WeatherInfo.class);
System.out.println(gson.toJson(posts));
content.close();
return posts;
} catch (Exception ex) {
Log.e(LOG_TAG, "Failed to parse JSON due to: " + ex);
}
return null;
}
use of com.a5corp.weather.model.WeatherInfo in project Weather by Sparker0i.
the class FetchWeatherOther method doInBackground.
@Override
public WeatherInfo doInBackground(String... params) {
city(params);
try {
Log.d(LOG_TAG, "Execution");
URL day = new URL(builtDay.toString());
Log.i("day", day.toString());
Log.d(LOG_TAG, "URI Ready");
WeatherInfo weather = gsonWeather();
if (weather.getCod() != 200) {
Log.e(LOG_TAG, "cod is not 200");
return null;
}
return weather;
} catch (IOException e) {
Log.e(LOG_TAG, "Execution Failed IO");
e.printStackTrace();
return null;
}
}
use of com.a5corp.weather.model.WeatherInfo in project Weather by Sparker0i.
the class SmallWidgetProvider method onUpdate.
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int widgetId : appWidgetIds) {
Log.i("In", "New Widget Provider");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_small);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widget_button_refresh, pendingIntent);
intent = new Intent(context, WeatherActivity.class);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_root, pendingIntent);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
WeatherInfo weather;
if (!sp.getString("lastToday", "").equals("")) {
weather = parseWidgetJson(sp.getString("lastToday", ""));
} else {
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
return;
}
String temperatureScale = PreferenceManager.getDefaultSharedPreferences(context).getString(Constants.PREF_TEMPERATURE_UNITS, Constants.METRIC).equals(Constants.METRIC) ? context.getString(R.string.c) : context.getString(R.string.f);
String temperature = String.format(Locale.getDefault(), "%.0f", weather.getMain().getTemp());
int iconId = weather.getWeather().get(0).getId();
String weatherIcon = Utils.setWeatherIcon(context, iconId);
remoteViews.setTextViewText(R.id.widget_city, weather.getName() + "," + weather.getSys().getCountry());
remoteViews.setTextViewText(R.id.widget_temperature, temperature + temperatureScale);
remoteViews.setImageViewBitmap(R.id.widget_icon, Utils.createWeatherIcon(context, weatherIcon));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
scheduleNextUpdate(context);
}
Aggregations