Java tutorial
/* * Copyright (C) 2016 Ilnur Saifutdinov ( ) * e-mail: ilnursoft@gmail.com * MIT License * https://opensource.org/licenses/mit-license.php */ package ru.kaefik.isaifutdinov.an_weather_widget; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.util.Log; import android.widget.RemoteViews; import org.json.JSONException; import ru.kaefik.isaifutdinov.an_weather_widget.Services.GetWeatherCityService; import ru.kaefik.isaifutdinov.an_weather_widget.city.CityModel; import ru.kaefik.isaifutdinov.an_weather_widget.utils.Utils; import static android.content.Context.*; public class AnWeatherWidget extends AppWidgetProvider { // ? intent public final static String PARAM_CITY = "city"; // public final static String PARAM_APPID = "appID"; public final static String PARAM_TEMP = "temp"; public final static String PARAM_WIND = "wind"; public final static String PARAM_TIMEREFRESH = "timeRefresh"; public final static String PARAM_WEATHERIMAGE = "weatherImage"; public final static String PARAM_DESCWEATHER = "descriptionWeather"; public final static String PARAM_WIDGETID = "widgetId"; // ? ? public final static String FORCE_WIDGET_UPDATE = "ru.kaefik.isaifutdinov.an_weather_widget.FORCE_WIDGET_UPDATE"; // ? ( ) public final static String CLICK_WIDGET_BUTTON = "ru.kaefik.isaifutdinov.an_weather_widget.CLICK_WIDGET_BUTTON"; public static String TAG_SERVICE = "AnWeatherWidget"; public static final String WIDGET_PREF = "anweatherwidgetconfig"; static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) throws JSONException { Log.i(TAG_SERVICE, "start updateAppWidget" + " appWidgetId = " + String.valueOf(appWidgetId)); String nameCity = ConfigActivity.loadStringParametersFromFile(context, String.valueOf(appWidgetId)); CityModel mCityModel = new CityModel(nameCity); // ? mCityModel = GetWeatherCityService.restoreCityInfoFromFile(context, mCityModel); Log.i(TAG_SERVICE, "updateAppWidget -> ?? " + mCityModel.getName() + " -> " + mCityModel.getTemp()); if (!nameCity.trim().equals("")) { Log.i(TAG_SERVICE, " updateAppWidget - >: " + nameCity + " appWidgetId = " + String.valueOf(appWidgetId)); // ? CLICK_WIDGET_BUTTON onReceive RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.an_weather_widget); // Intent ? Broadcast Intent active = new Intent(context, AnWeatherWidget.class); active.setAction(CLICK_WIDGET_BUTTON); //? ? PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); //? ? remoteViews.setOnClickPendingIntent(R.id.refreshButton, actionPendingIntent); //? remoteViews.setTextViewText(R.id.cityNameText, mCityModel.getName()); remoteViews.setTextViewText(R.id.tempCityText, mCityModel.getTemp() + "C"); remoteViews.setTextViewText(R.id.windText, Utils.windGradus2Rumb(mCityModel.getWinddirection()) + " (" + Float.toString(mCityModel.getWindspeed()) + " /?)"); remoteViews.setTextViewText(R.id.timeRefreshText, mCityModel.getTimeRefresh()); remoteViews.setTextViewText(R.id.descriptionWeatherText, mCityModel.getWeather("description")); remoteViews.setImageViewUri(R.id.weatherImageView, Uri.parse("android.resource://ru.kaefik.isaifutdinov.an_weather_widget/mipmap/" + "weather" + mCityModel.getWeather("icon"))); appWidgetManager.updateAppWidget(appWidgetId, remoteViews); // END - ? CLICK_WIDGET_BUTTON onReceive startGetWeatherCityService(context, appWidgetId, mCityModel); } else { Log.i(TAG_SERVICE, " updateAppWidget - > ? " + " appWidgetId = " + String.valueOf(appWidgetId)); } } // ? ?? GetWeatherCityService ? public static void startGetWeatherCityService(Context context, int appWidgetId, CityModel cityModel) { Intent intent; Utils.createTranslateWeatherDescription(); intent = new Intent(context, GetWeatherCityService.class); intent.putExtra(PARAM_CITY, cityModel.getName()); intent.putExtra(PARAM_WIDGETID, appWidgetId); context.startService(intent); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // There may be multiple widgets active, so update all of them for (int appWidgetId : appWidgetIds) { try { updateAppWidget(context, appWidgetManager, appWidgetId); } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onReceive(Context context, Intent intent) { Log.i(TAG_SERVICE, " onReceive " + intent.getAction()); super.onReceive(context, intent); if (FORCE_WIDGET_UPDATE.equals(intent.getAction())) { String nameCity = intent.getStringExtra(PARAM_CITY); String tempCity = intent.getStringExtra(PARAM_TEMP); String windCity = intent.getStringExtra(PARAM_WIND); String timeRefreshCity = intent.getStringExtra(PARAM_TIMEREFRESH); String weatherImageCity = intent.getStringExtra(PARAM_WEATHERIMAGE); String descriptionWeather = intent.getStringExtra(PARAM_DESCWEATHER); int WidgetId = intent.getIntExtra(PARAM_WIDGETID, 0); //TODO: ? , ?. ? ? GetWeatherCityService Log.i(TAG_SERVICE, "onReceive " + nameCity + " -> " + tempCity); // AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName thisWidget = new ComponentName(context, AnWeatherWidget.class); int[] appWidgetId = appWidgetManager.getAppWidgetIds(thisWidget); for (int i = 0; i < appWidgetId.length; i++) { Log.i(TAG_SERVICE, "onReceive -> appWidgetId = " + String.valueOf(appWidgetId[i])); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.an_weather_widget); if (WidgetId == appWidgetId[i]) { views.setTextViewText(R.id.cityNameText, nameCity); views.setTextViewText(R.id.tempCityText, tempCity); views.setTextViewText(R.id.windText, windCity); views.setTextViewText(R.id.timeRefreshText, timeRefreshCity); views.setTextViewText(R.id.descriptionWeatherText, descriptionWeather); views.setImageViewUri(R.id.weatherImageView, Uri.parse("android.resource://ru.kaefik.isaifutdinov.an_weather_widget/mipmap/" + "weather" + weatherImageCity)); // ? CLICK_WIDGET_BUTTON onReceive // Intent ? Broadcast Intent active = new Intent(context, AnWeatherWidget.class); active.setAction(CLICK_WIDGET_BUTTON); //? ? PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); //? ? views.setOnClickPendingIntent(R.id.refreshButton, actionPendingIntent); //? // END - ? CLICK_WIDGET_BUTTON onReceive appWidgetManager.updateAppWidget(appWidgetId[i], views); break; } appWidgetManager.updateAppWidget(appWidgetId[i], views); } } if (CLICK_WIDGET_BUTTON.equals(intent.getAction())) { Log.i(TAG_SERVICE, " "); // AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName thisWidget = new ComponentName(context, AnWeatherWidget.class); int[] appWidgetId = appWidgetManager.getAppWidgetIds(thisWidget); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.an_weather_widget); // ? for (int i = 0; i < appWidgetId.length; i++) { Log.i(TAG_SERVICE, "id -> " + String.valueOf(appWidgetId[i])); String nameCity = ConfigActivity.loadStringParametersFromFile(context, String.valueOf(appWidgetId[i])); startGetWeatherCityService(context, appWidgetId[i], new CityModel(nameCity)); } } } @Override public void onEnabled(Context context) { //TODO: ? ? activity ? // mCityModel = new CityModel("Kazan"); // mCityModel.setMYAPPID("76d6de6e46c704733f12c8738307dbb5"); Log.i(TAG_SERVICE, "onEnabled Widget"); } @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); // ? Preferences SharedPreferences mSPref = context.getSharedPreferences(WIDGET_PREF, MODE_PRIVATE); SharedPreferences.Editor editor = mSPref.edit(); for (int widgetID : appWidgetIds) { editor.remove(String.valueOf(widgetID)); } editor.commit(); } @Override public void onDisabled(Context context) { // Enter relevant functionality for when the last widget is disabled } }