Updating WeatherListWidget to use new resize callbacks. (Bug 6441164)
Change-Id: Ifc2730c564532147e5f60022955a7e97d3e1d855
diff --git a/samples/WeatherListWidget/res/drawable-hdpi/body.png b/samples/WeatherListWidget/res/drawable-hdpi/body.png
deleted file mode 100644
index 17d303f..0000000
--- a/samples/WeatherListWidget/res/drawable-hdpi/body.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-hdpi/footer.png b/samples/WeatherListWidget/res/drawable-hdpi/footer.png
deleted file mode 100644
index 43962f7..0000000
--- a/samples/WeatherListWidget/res/drawable-hdpi/footer.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-hdpi/header.9.png b/samples/WeatherListWidget/res/drawable-hdpi/header.9.png
deleted file mode 100644
index 5f34768..0000000
--- a/samples/WeatherListWidget/res/drawable-hdpi/header.9.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-hdpi/item_bg_dark.png b/samples/WeatherListWidget/res/drawable-hdpi/item_bg_dark.png
deleted file mode 100644
index f5886bd..0000000
--- a/samples/WeatherListWidget/res/drawable-hdpi/item_bg_dark.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-hdpi/item_bg_light.png b/samples/WeatherListWidget/res/drawable-hdpi/item_bg_light.png
deleted file mode 100644
index e8b5aaf..0000000
--- a/samples/WeatherListWidget/res/drawable-hdpi/item_bg_light.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-hdpi/refresh.png b/samples/WeatherListWidget/res/drawable-hdpi/refresh.png
deleted file mode 100644
index eaec9cb..0000000
--- a/samples/WeatherListWidget/res/drawable-hdpi/refresh.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-hdpi/refresh_pressed.png b/samples/WeatherListWidget/res/drawable-hdpi/refresh_pressed.png
deleted file mode 100644
index 34438b7..0000000
--- a/samples/WeatherListWidget/res/drawable-hdpi/refresh_pressed.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-hdpi/sunny.png b/samples/WeatherListWidget/res/drawable-hdpi/sunny.png
new file mode 100644
index 0000000..42785b9
--- /dev/null
+++ b/samples/WeatherListWidget/res/drawable-hdpi/sunny.png
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-mdpi/body.png b/samples/WeatherListWidget/res/drawable-mdpi/body.png
deleted file mode 100644
index a08d03b..0000000
--- a/samples/WeatherListWidget/res/drawable-mdpi/body.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-mdpi/footer.png b/samples/WeatherListWidget/res/drawable-mdpi/footer.png
deleted file mode 100644
index d3960a7..0000000
--- a/samples/WeatherListWidget/res/drawable-mdpi/footer.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-mdpi/header.9.png b/samples/WeatherListWidget/res/drawable-mdpi/header.9.png
deleted file mode 100644
index 2372225..0000000
--- a/samples/WeatherListWidget/res/drawable-mdpi/header.9.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-mdpi/item_bg_dark.png b/samples/WeatherListWidget/res/drawable-mdpi/item_bg_dark.png
deleted file mode 100644
index a3ac9d7..0000000
--- a/samples/WeatherListWidget/res/drawable-mdpi/item_bg_dark.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-mdpi/item_bg_light.png b/samples/WeatherListWidget/res/drawable-mdpi/item_bg_light.png
deleted file mode 100644
index ec6f5aa..0000000
--- a/samples/WeatherListWidget/res/drawable-mdpi/item_bg_light.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-mdpi/refresh.png b/samples/WeatherListWidget/res/drawable-mdpi/refresh.png
deleted file mode 100644
index 006bcc5..0000000
--- a/samples/WeatherListWidget/res/drawable-mdpi/refresh.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-mdpi/refresh_pressed.png b/samples/WeatherListWidget/res/drawable-mdpi/refresh_pressed.png
deleted file mode 100644
index d8ca9b5..0000000
--- a/samples/WeatherListWidget/res/drawable-mdpi/refresh_pressed.png
+++ /dev/null
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-mdpi/sunny.png b/samples/WeatherListWidget/res/drawable-mdpi/sunny.png
new file mode 100644
index 0000000..9453447
--- /dev/null
+++ b/samples/WeatherListWidget/res/drawable-mdpi/sunny.png
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable-nodpi/preview.png b/samples/WeatherListWidget/res/drawable-nodpi/preview.png
index f0cbdaf..b9c8780 100644
--- a/samples/WeatherListWidget/res/drawable-nodpi/preview.png
+++ b/samples/WeatherListWidget/res/drawable-nodpi/preview.png
Binary files differ
diff --git a/samples/WeatherListWidget/res/drawable/refresh_button.xml b/samples/WeatherListWidget/res/drawable/refresh_button.xml
deleted file mode 100644
index 1c0017e..0000000
--- a/samples/WeatherListWidget/res/drawable/refresh_button.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true"
- android:drawable="@drawable/refresh_pressed" /> <!-- pressed -->
- <item android:drawable="@drawable/refresh" /> <!-- default -->
-</selector>
\ No newline at end of file
diff --git a/samples/WeatherListWidget/res/layout/dark_widget_item.xml b/samples/WeatherListWidget/res/layout/dark_widget_item.xml
deleted file mode 100644
index 1f920a2..0000000
--- a/samples/WeatherListWidget/res/layout/dark_widget_item.xml
+++ /dev/null
@@ -1,24 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2011 The Android Open Source Project
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
--->
-<TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/widget_item"
- android:layout_width="match_parent"
- android:layout_height="46dp"
- android:paddingLeft="25dp"
- android:gravity="center_vertical"
- android:background="@drawable/item_bg_dark"
- android:textColor="#e5e5e1"
- android:textSize="24sp" />
diff --git a/samples/WeatherListWidget/res/layout/light_widget_item.xml b/samples/WeatherListWidget/res/layout/widget_item.xml
similarity index 89%
rename from samples/WeatherListWidget/res/layout/light_widget_item.xml
rename to samples/WeatherListWidget/res/layout/widget_item.xml
index bb2946f..c0002e5 100644
--- a/samples/WeatherListWidget/res/layout/light_widget_item.xml
+++ b/samples/WeatherListWidget/res/layout/widget_item.xml
@@ -19,6 +19,6 @@
android:layout_height="46dp"
android:paddingLeft="25dp"
android:gravity="center_vertical"
- android:background="@drawable/item_bg_light"
- android:textColor="#e5e5e1"
- android:textSize="24sp" />
+ android:background="#F0F0F0"
+ android:textColor="#232323"
+ android:textSize="20sp" />
diff --git a/samples/WeatherListWidget/res/layout/widget_layout.xml b/samples/WeatherListWidget/res/layout/widget_layout.xml
index 4c58fa7..f3eb6de 100644
--- a/samples/WeatherListWidget/res/layout/widget_layout.xml
+++ b/samples/WeatherListWidget/res/layout/widget_layout.xml
@@ -23,30 +23,37 @@
android:layout_marginRight="@dimen/widget_margin_right">
<!-- We define separate margins to allow for flexibility in twiddling the margins
depending on device form factor and target SDK version. -->
- <FrameLayout
+
+ <LinearLayout
android:layout_width="match_parent"
- android:layout_height="wrap_content">
+ android:layout_height="80dp"
+ android:background="#F8F8F8"
+ android:orientation="horizontal">
<ImageView
- android:id="@+id/header"
- android:layout_width="match_parent"
+ android:id="@+id/city_weather"
+ android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:scaleType="fitXY"
- android:src="@drawable/header" />
- <ImageButton
- android:id="@+id/refresh"
- android:layout_width="56dp"
- android:layout_height="39dp"
- android:layout_gravity="right|top"
- android:layout_marginRight="15dp"
- android:layout_marginTop="20dp"
- android:background="@drawable/refresh_button" />
- </FrameLayout>
+ android:padding="12dp"
+ android:scaleType="fitStart"
+ android:adjustViewBounds="true"
+ android:src="@drawable/sunny" />
+ <TextView
+ android:id="@+id/city_name"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_vertical"
+ android:text="@string/city_name"
+ android:textAllCaps="true"
+ android:textColor="#232323"
+ android:textSize="24sp" />
+ </LinearLayout>
+
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
- android:background="@drawable/body">
+ android:background="#F8F8F8">
<ListView
android:id="@+id/weather_list"
android:layout_width="match_parent"
@@ -61,10 +68,16 @@
android:text="@string/empty_view_text"
android:textSize="20sp" />
</FrameLayout>
- <ImageView
- android:id="@+id/footer"
+
+ <Button
+ android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- android:scaleType="fitXY"
- android:src="@drawable/footer" />
+ android:padding="12dp"
+ android:gravity="center"
+ android:background="#F8F8F8"
+ android:text="@string/refresh"
+ android:textAllCaps="true"
+ android:textColor="#232323"
+ android:textSize="14sp" />
</LinearLayout>
diff --git a/samples/WeatherListWidget/res/layout/widget_layout_small.xml b/samples/WeatherListWidget/res/layout/widget_layout_small.xml
new file mode 100644
index 0000000..20227b7
--- /dev/null
+++ b/samples/WeatherListWidget/res/layout/widget_layout_small.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical"
+ android:layout_marginTop="@dimen/widget_margin_top"
+ android:layout_marginBottom="@dimen/widget_margin_bottom"
+ android:layout_marginLeft="@dimen/widget_margin_left"
+ android:layout_marginRight="@dimen/widget_margin_right">
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="80dp"
+ android:background="#F8F8F8"
+ android:orientation="horizontal">
+ <ImageView
+ android:id="@+id/city_weather"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:padding="12dp"
+ android:scaleType="fitStart"
+ android:adjustViewBounds="true"
+ android:src="@drawable/sunny" />
+ <TextView
+ android:id="@+id/city_name"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_vertical"
+ android:text="@string/city_name"
+ android:textAllCaps="true"
+ android:textColor="#232323"
+ android:textSize="24sp" />
+ </LinearLayout>
+</LinearLayout>
diff --git a/samples/WeatherListWidget/res/values/strings.xml b/samples/WeatherListWidget/res/values/strings.xml
index 6542545..a3b7e06 100644
--- a/samples/WeatherListWidget/res/values/strings.xml
+++ b/samples/WeatherListWidget/res/values/strings.xml
@@ -15,6 +15,10 @@
-->
<resources>
<string name="empty_view_text">No cities found...</string>
- <string name="toast_format_string">%1$s says Hi!</string>
- <string name="item_format_string">%1$d\u00B0 in %2$s</string>
+ <string name="toast_format_string">%1$s!</string>
+ <string name="item_format_string">%1$d\u00B0 on %2$s</string>
+ <string name="header_format_string">%1$d\u00B0 in %2$s</string>
+
+ <string name="refresh">Refresh</string>
+ <string name="city_name">San Francisco</string>
</resources>
diff --git a/samples/WeatherListWidget/res/xml/widgetinfo.xml b/samples/WeatherListWidget/res/xml/widgetinfo.xml
index e6e9cf3..2e41943 100644
--- a/samples/WeatherListWidget/res/xml/widgetinfo.xml
+++ b/samples/WeatherListWidget/res/xml/widgetinfo.xml
@@ -15,12 +15,12 @@
-->
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="250dp"
+ android:minWidth="280dp"
android:minHeight="180dp"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/widget_layout"
android:resizeMode="vertical"
- android:minResizeWidth="250dp"
- android:minResizeHeight="110dp"
+ android:minResizeWidth="280dp"
+ android:minResizeHeight="70dp"
android:previewImage="@drawable/preview">
</appwidget-provider>
diff --git a/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherDataProvider.java b/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherDataProvider.java
index 92a1cb3..ede0039 100644
--- a/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherDataProvider.java
+++ b/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherDataProvider.java
@@ -36,12 +36,12 @@
* data will only be stored in memory.
*/
class WeatherDataPoint {
- String city;
+ String day;
int degrees;
- WeatherDataPoint(String c, int d) {
- city = c;
- degrees = d;
+ WeatherDataPoint(String d, int deg) {
+ day = d;
+ degrees = deg;
}
}
@@ -53,7 +53,7 @@
Uri.parse("content://com.example.android.weatherlistwidget.provider");
public static class Columns {
public static final String ID = "_id";
- public static final String CITY = "city";
+ public static final String DAY = "day";
public static final String TEMPERATURE = "temperature";
}
@@ -67,17 +67,20 @@
@Override
public boolean onCreate() {
// We are going to initialize the data provider with some default values
- sData.add(new WeatherDataPoint("San Francisco", 13));
- sData.add(new WeatherDataPoint("New York", 1));
- sData.add(new WeatherDataPoint("Seattle", 7));
- sData.add(new WeatherDataPoint("Boston", 4));
- sData.add(new WeatherDataPoint("Miami", 22));
- sData.add(new WeatherDataPoint("Toronto", -10));
- sData.add(new WeatherDataPoint("Calgary", -13));
- sData.add(new WeatherDataPoint("Tokyo", 8));
- sData.add(new WeatherDataPoint("Kyoto", 11));
- sData.add(new WeatherDataPoint("London", -1));
- sData.add(new WeatherDataPoint("Nomanisan", 27));
+ sData.add(new WeatherDataPoint("Monday", 13));
+ sData.add(new WeatherDataPoint("Tuesday", 1));
+ sData.add(new WeatherDataPoint("Wednesday", 7));
+ sData.add(new WeatherDataPoint("Thursday", 4));
+ sData.add(new WeatherDataPoint("Friday", 22));
+ sData.add(new WeatherDataPoint("Saturday", -10));
+ sData.add(new WeatherDataPoint("Sunday", -13));
+ sData.add(new WeatherDataPoint("Monday", 8));
+ sData.add(new WeatherDataPoint("Tuesday", 11));
+ sData.add(new WeatherDataPoint("Wednesday", -1));
+ sData.add(new WeatherDataPoint("Thursday", 27));
+ sData.add(new WeatherDataPoint("Friday", 27));
+ sData.add(new WeatherDataPoint("Saturday", 27));
+ sData.add(new WeatherDataPoint("Sunday", 27));
return true;
}
@@ -89,17 +92,17 @@
// In this sample, we only query without any parameters, so we can just return a cursor to
// all the weather data.
final MatrixCursor c = new MatrixCursor(
- new String[]{ Columns.ID, Columns.CITY, Columns.TEMPERATURE });
+ new String[]{ Columns.ID, Columns.DAY, Columns.TEMPERATURE });
for (int i = 0; i < sData.size(); ++i) {
final WeatherDataPoint data = sData.get(i);
- c.addRow(new Object[]{ new Integer(i), data.city, new Integer(data.degrees) });
+ c.addRow(new Object[]{ new Integer(i), data.day, new Integer(data.degrees) });
}
return c;
}
@Override
public String getType(Uri uri) {
- return "vnd.android.cursor.dir/vnd.weatherlistwidget.citytemperature";
+ return "vnd.android.cursor.dir/vnd.weatherlistwidget.temperature";
}
@Override
@@ -123,7 +126,7 @@
// temperature values.
final int index = Integer.parseInt(uri.getPathSegments().get(0));
final MatrixCursor c = new MatrixCursor(
- new String[]{ Columns.ID, Columns.CITY, Columns.TEMPERATURE });
+ new String[]{ Columns.ID, Columns.DAY, Columns.TEMPERATURE });
assert(0 <= index && index < sData.size());
final WeatherDataPoint data = sData.get(index);
data.degrees = values.getAsInteger(Columns.TEMPERATURE);
@@ -134,4 +137,4 @@
return 1;
}
-}
\ No newline at end of file
+}
diff --git a/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetProvider.java b/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetProvider.java
index 2f2b347..ea3f944 100644
--- a/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetProvider.java
+++ b/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetProvider.java
@@ -28,6 +28,7 @@
import android.database.Cursor;
import android.database.ContentObserver;
import android.net.Uri;
+import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.widget.RemoteViews;
@@ -64,11 +65,15 @@
public class WeatherWidgetProvider extends AppWidgetProvider {
public static String CLICK_ACTION = "com.example.android.weatherlistwidget.CLICK";
public static String REFRESH_ACTION = "com.example.android.weatherlistwidget.REFRESH";
- public static String EXTRA_CITY_ID = "com.example.android.weatherlistwidget.city";
+ public static String EXTRA_DAY_ID = "com.example.android.weatherlistwidget.day";
private static HandlerThread sWorkerThread;
private static Handler sWorkerQueue;
private static WeatherDataProviderObserver sDataObserver;
+ private static final int sMaxDegrees = 96;
+
+ private boolean mIsLargeLayout = true;
+ private int mHeaderWeatherState = 0;
public WeatherWidgetProvider() {
// Start the worker thread
@@ -77,6 +82,8 @@
sWorkerQueue = new Handler(sWorkerThread.getLooper());
}
+ // XXX: clear the worker queue if we are destroyed?
+
@Override
public void onEnabled(Context context) {
// Register for external updates to the data to trigger an update of the widget. When using
@@ -109,7 +116,6 @@
final Cursor c = r.query(WeatherDataProvider.CONTENT_URI, null, null, null,
null);
final int count = c.getCount();
- final int maxDegrees = 96;
// We disable the data changed observer temporarily since each of the updates
// will trigger an onChange() in our data observer.
@@ -118,7 +124,7 @@
final Uri uri = ContentUris.withAppendedId(WeatherDataProvider.CONTENT_URI, i);
final ContentValues values = new ContentValues();
values.put(WeatherDataProvider.Columns.TEMPERATURE,
- new Random().nextInt(maxDegrees));
+ new Random().nextInt(sMaxDegrees));
r.update(uri, values, null, null);
}
r.registerContentObserver(WeatherDataProvider.CONTENT_URI, true, sDataObserver);
@@ -128,29 +134,31 @@
mgr.notifyAppWidgetViewDataChanged(mgr.getAppWidgetIds(cn), R.id.weather_list);
}
});
+
+ final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
+ AppWidgetManager.INVALID_APPWIDGET_ID);
} else if (action.equals(CLICK_ACTION)) {
// Show a toast
final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
- final String city = intent.getStringExtra(EXTRA_CITY_ID);
+ final String day = intent.getStringExtra(EXTRA_DAY_ID);
final String formatStr = ctx.getResources().getString(R.string.toast_format_string);
- Toast.makeText(ctx, String.format(formatStr, city), Toast.LENGTH_SHORT).show();
+ Toast.makeText(ctx, String.format(formatStr, day), Toast.LENGTH_SHORT).show();
}
super.onReceive(ctx, intent);
}
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
- // Update each of the widgets with the remote adapter
- for (int i = 0; i < appWidgetIds.length; ++i) {
+ private RemoteViews buildLayout(Context context, int appWidgetId, boolean largeLayout) {
+ RemoteViews rv;
+ if (largeLayout) {
// Specify the service to provide data for the collection widget. Note that we need to
// embed the appWidgetId via the data otherwise it will be ignored.
final Intent intent = new Intent(context, WeatherWidgetService.class);
- intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
+ intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
- final RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
- rv.setRemoteAdapter(appWidgetIds[i], R.id.weather_list, intent);
+ rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
+ rv.setRemoteAdapter(appWidgetId, R.id.weather_list, intent);
// Set the empty view to be displayed if the collection is empty. It must be a sibling
// view of the collection view.
@@ -161,7 +169,7 @@
// ignored otherwise.
final Intent onClickIntent = new Intent(context, WeatherWidgetProvider.class);
onClickIntent.setAction(WeatherWidgetProvider.CLICK_ACTION);
- onClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
+ onClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
onClickIntent.setData(Uri.parse(onClickIntent.toUri(Intent.URI_INTENT_SCHEME)));
final PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0,
onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
@@ -174,8 +182,53 @@
refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.refresh, refreshPendingIntent);
- appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
+ // Restore the minimal header
+ rv.setTextViewText(R.id.city_name, context.getString(R.string.city_name));
+ } else {
+ rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout_small);
+
+ // Update the header to reflect the weather for "today"
+ Cursor c = context.getContentResolver().query(WeatherDataProvider.CONTENT_URI, null,
+ null, null, null);
+ if (c.moveToPosition(0)) {
+ int tempColIndex = c.getColumnIndex(WeatherDataProvider.Columns.TEMPERATURE);
+ int temp = c.getInt(tempColIndex);
+ String formatStr = context.getResources().getString(R.string.header_format_string);
+ String header = String.format(formatStr, temp,
+ context.getString(R.string.city_name));
+ rv.setTextViewText(R.id.city_name, header);
+ }
+ c.close();
+ }
+ return rv;
+ }
+
+ @Override
+ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
+ // Update each of the widgets with the remote adapter
+ for (int i = 0; i < appWidgetIds.length; ++i) {
+ RemoteViews layout = buildLayout(context, appWidgetIds[i], mIsLargeLayout);
+ appWidgetManager.updateAppWidget(appWidgetIds[i], layout);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
+
+ @Override
+ public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
+ int appWidgetId, Bundle newOptions) {
+
+ int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
+ int maxWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
+ int minHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
+ int maxHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
+
+ RemoteViews layout;
+ if (minHeight < 100) {
+ mIsLargeLayout = false;
+ } else {
+ mIsLargeLayout = true;
+ }
+ layout = buildLayout(context, appWidgetId, mIsLargeLayout);
+ appWidgetManager.updateAppWidget(appWidgetId, layout);
+ }
}
\ No newline at end of file
diff --git a/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetService.java b/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetService.java
index 1d3c349..4780e80 100644
--- a/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetService.java
+++ b/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetService.java
@@ -70,28 +70,26 @@
public RemoteViews getViewAt(int position) {
// Get the data for this position from the content provider
- String city = "Unknown City";
+ String day = "Unknown Day";
int temp = 0;
if (mCursor.moveToPosition(position)) {
- final int cityColIndex = mCursor.getColumnIndex(WeatherDataProvider.Columns.CITY);
+ final int dayColIndex = mCursor.getColumnIndex(WeatherDataProvider.Columns.DAY);
final int tempColIndex = mCursor.getColumnIndex(
WeatherDataProvider.Columns.TEMPERATURE);
- city = mCursor.getString(cityColIndex);
+ day = mCursor.getString(dayColIndex);
temp = mCursor.getInt(tempColIndex);
}
- // Return a proper item with the proper city and temperature. Just for fun, we alternate
- // the items to make the list easier to read.
+ // Return a proper item with the proper day and temperature
final String formatStr = mContext.getResources().getString(R.string.item_format_string);
- final int itemId = (position % 2 == 0 ? R.layout.light_widget_item
- : R.layout.dark_widget_item);
+ final int itemId = R.layout.widget_item;
RemoteViews rv = new RemoteViews(mContext.getPackageName(), itemId);
- rv.setTextViewText(R.id.widget_item, String.format(formatStr, temp, city));
+ rv.setTextViewText(R.id.widget_item, String.format(formatStr, temp, day));
// Set the click intent so that we can handle it and show a toast message
final Intent fillInIntent = new Intent();
final Bundle extras = new Bundle();
- extras.putString(WeatherWidgetProvider.EXTRA_CITY_ID, city);
+ extras.putString(WeatherWidgetProvider.EXTRA_DAY_ID, day);
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);