Search in sources :

Example 1 with WeatherInfo

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);
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) Gson(com.google.gson.Gson) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Prefs(com.a5corp.weather.preferences.Prefs)

Example 2 with WeatherInfo

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;
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) URL(java.net.URL) IOException(java.io.IOException)

Example 3 with WeatherInfo

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;
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) URL(java.net.URL) IOException(java.io.IOException)

Example 4 with WeatherInfo

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;
    }
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with WeatherInfo

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);
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) RemoteViews(android.widget.RemoteViews) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) WeatherActivity(com.a5corp.weather.activity.WeatherActivity)

Aggregations

WeatherInfo (com.a5corp.weather.model.WeatherInfo)8 IOException (java.io.IOException)5 PendingIntent (android.app.PendingIntent)4 Gson (com.google.gson.Gson)4 Intent (android.content.Intent)3 Prefs (com.a5corp.weather.preferences.Prefs)3 GsonBuilder (com.google.gson.GsonBuilder)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 SharedPreferences (android.content.SharedPreferences)2 RemoteViews (android.widget.RemoteViews)2 AlarmManager (android.app.AlarmManager)1 WeatherActivity (com.a5corp.weather.activity.WeatherActivity)1 CheckConnection (com.a5corp.weather.internet.CheckConnection)1 Request (com.a5corp.weather.internet.Request)1