diff --git a/app/src/main/java/com/example/android/sunshine/app/widget/TodayWidgetIntentService.java b/app/src/main/java/com/example/android/sunshine/app/widget/TodayWidgetIntentService.java
index c07739a..0ab8854 100644
--- a/app/src/main/java/com/example/android/sunshine/app/widget/TodayWidgetIntentService.java
+++ b/app/src/main/java/com/example/android/sunshine/app/widget/TodayWidgetIntentService.java
@@ -24,6 +24,9 @@ import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
+import android.os.Bundle;
+import android.util.DisplayMetrics;
+import android.util.TypedValue;
import android.widget.RemoteViews;
import com.example.android.sunshine.app.MainActivity;
@@ -38,12 +41,14 @@ public class TodayWidgetIntentService extends IntentService {
private static final String[] FORECAST_COLUMNS = {
WeatherContract.WeatherEntry.COLUMN_WEATHER_ID,
WeatherContract.WeatherEntry.COLUMN_SHORT_DESC,
- WeatherContract.WeatherEntry.COLUMN_MAX_TEMP
+ WeatherContract.WeatherEntry.COLUMN_MAX_TEMP,
+ WeatherContract.WeatherEntry.COLUMN_MIN_TEMP
};
// these indices must match the projection
private static final int INDEX_WEATHER_ID = 0;
private static final int INDEX_SHORT_DESC = 1;
private static final int INDEX_MAX_TEMP = 2;
+ private static final int INDEX_MIN_TEMP = 3;
public TodayWidgetIntentService() {
super("TodayWidgetIntentService");
@@ -75,12 +80,25 @@ public class TodayWidgetIntentService extends IntentService {
int weatherArtResourceId = Utility.getArtResourceForWeatherCondition(weatherId);
String description = data.getString(INDEX_SHORT_DESC);
double maxTemp = data.getDouble(INDEX_MAX_TEMP);
+ double minTemp = data.getDouble(INDEX_MIN_TEMP);
String formattedMaxTemperature = Utility.formatTemperature(this, maxTemp);
+ String formattedMinTemperature = Utility.formatTemperature(this, minTemp);
data.close();
// Perform this loop procedure for each Today widget
for (int appWidgetId : appWidgetIds) {
- int layoutId = R.layout.widget_today_small;
+ // Find the correct layout based on the widget's width
+ int widgetWidth = getWidgetWidth(appWidgetManager, appWidgetId);
+ int defaultWidth = getResources().getDimensionPixelSize(R.dimen.widget_today_default_width);
+ int largeWidth = getResources().getDimensionPixelSize(R.dimen.widget_today_large_width);
+ int layoutId;
+ if (widgetWidth >= largeWidth) {
+ layoutId = R.layout.widget_today_large;
+ } else if (widgetWidth >= defaultWidth) {
+ layoutId = R.layout.widget_today;
+ } else {
+ layoutId = R.layout.widget_today_small;
+ }
RemoteViews views = new RemoteViews(getPackageName(), layoutId);
// Add the data to the RemoteViews
@@ -89,7 +107,9 @@ public class TodayWidgetIntentService extends IntentService {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
setRemoteContentDescription(views, description);
}
+ views.setTextViewText(R.id.widget_description, description);
views.setTextViewText(R.id.widget_high_temperature, formattedMaxTemperature);
+ views.setTextViewText(R.id.widget_low_temperature, formattedMinTemperature);
// Create an Intent to launch MainActivity
Intent launchIntent = new Intent(this, MainActivity.class);
@@ -101,6 +121,29 @@ public class TodayWidgetIntentService extends IntentService {
}
}
+ private int getWidgetWidth(AppWidgetManager appWidgetManager, int appWidgetId) {
+ // Prior to Jelly Bean, widgets were always their default size
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
+ return getResources().getDimensionPixelSize(R.dimen.widget_today_default_width);
+ }
+ // For Jelly Bean and higher devices, widgets can be resized - the current size can be
+ // retrieved from the newly added App Widget Options
+ return getWidgetWidthFromOptions(appWidgetManager, appWidgetId);
+ }
+
+ @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
+ private int getWidgetWidthFromOptions(AppWidgetManager appWidgetManager, int appWidgetId) {
+ Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
+ if (options.containsKey(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)) {
+ int minWidthDp = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
+ // The width returned is in dp, but we'll convert it to pixels to match the other widths
+ DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
+ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, minWidthDp,
+ displayMetrics);
+ }
+ return getResources().getDimensionPixelSize(R.dimen.widget_today_default_width);
+ }
+
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
private void setRemoteContentDescription(RemoteViews views, String description) {
views.setContentDescription(R.id.widget_icon, description);
diff --git a/app/src/main/java/com/example/android/sunshine/app/widget/TodayWidgetProvider.java b/app/src/main/java/com/example/android/sunshine/app/widget/TodayWidgetProvider.java
index ee74909..81c1dfe 100644
--- a/app/src/main/java/com/example/android/sunshine/app/widget/TodayWidgetProvider.java
+++ b/app/src/main/java/com/example/android/sunshine/app/widget/TodayWidgetProvider.java
@@ -25,7 +25,7 @@ import android.support.annotation.NonNull;
import com.example.android.sunshine.app.sync.SunshineSyncAdapter;
/**
- * Provider for a widget showing today's weather.
+ * Provider for a horizontally expandable widget showing today's weather.
*
* Delegates widget updating to {@link TodayWidgetIntentService} to ensure that
* data retrieval is done on a background thread
diff --git a/app/src/main/res/drawable-nodpi/widget_preview_today.png b/app/src/main/res/drawable-nodpi/widget_preview_today.png
new file mode 100644
index 0000000..7be01f0
Binary files /dev/null and b/app/src/main/res/drawable-nodpi/widget_preview_today.png differ
diff --git a/app/src/main/res/layout/widget_today.xml b/app/src/main/res/layout/widget_today.xml
new file mode 100644
index 0000000..caf01ed
--- /dev/null
+++ b/app/src/main/res/layout/widget_today.xml
@@ -0,0 +1,66 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/layout/widget_today_large.xml b/app/src/main/res/layout/widget_today_large.xml
new file mode 100644
index 0000000..3838aaf
--- /dev/null
+++ b/app/src/main/res/layout/widget_today_large.xml
@@ -0,0 +1,82 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml
index 12b07d1..1c7cbb2 100644
--- a/app/src/main/res/values/dimens.xml
+++ b/app/src/main/res/values/dimens.xml
@@ -54,6 +54,9 @@
360dp
- 40dp
+ 110dp
40dp
+ 40dp
+ @dimen/widget_today_default_height
+ 220dp
diff --git a/app/src/main/res/xml/widget_info_today.xml b/app/src/main/res/xml/widget_info_today.xml
index 9812f0b..c36d564 100644
--- a/app/src/main/res/xml/widget_info_today.xml
+++ b/app/src/main/res/xml/widget_info_today.xml
@@ -14,7 +14,13 @@
limitations under the License.
-->
\ No newline at end of file
+ android:previewImage="@drawable/widget_preview_today"
+ android:resizeMode="horizontal"
+ android:updatePeriodMillis="0"
+ tools:ignore="UnusedAttribute" />