19
19
import android .app .PendingIntent ;
20
20
import android .appwidget .AppWidgetManager ;
21
21
import android .appwidget .AppWidgetProvider ;
22
+ import android .content .Context ;
23
+ import android .content .Intent ;
22
24
import android .content .ComponentName ;
25
+ import android .content .ContentValues ;
23
26
import android .content .ContentResolver ;
24
27
import android .content .ContentUris ;
25
- import android .content .ContentValues ;
26
- import android .content .Context ;
27
- import android .content .Intent ;
28
- import android .database .ContentObserver ;
29
28
import android .database .Cursor ;
29
+ import android .database .ContentObserver ;
30
30
import android .net .Uri ;
31
31
import android .os .Bundle ;
32
32
import android .os .Handler ;
33
33
import android .os .HandlerThread ;
34
- import android .util .Log ;
35
34
import android .widget .RemoteViews ;
36
35
import android .widget .Toast ;
37
36
38
- import java .util .ArrayList ;
39
37
import java .util .Random ;
40
38
41
39
/**
@@ -75,6 +73,7 @@ public class WeatherWidgetProvider extends AppWidgetProvider {
75
73
private static final int sMaxDegrees = 96 ;
76
74
77
75
private boolean mIsLargeLayout = true ;
76
+ private int mHeaderWeatherState = 0 ;
78
77
79
78
public WeatherWidgetProvider () {
80
79
// Start the worker thread
@@ -117,7 +116,6 @@ public void run() {
117
116
final Cursor c = r .query (WeatherDataProvider .CONTENT_URI , null , null , null ,
118
117
null );
119
118
final int count = c .getCount ();
120
- c .close ();
121
119
122
120
// We disable the data changed observer temporarily since each of the updates
123
121
// will trigger an onChange() in our data observer.
@@ -133,13 +131,12 @@ public void run() {
133
131
134
132
final AppWidgetManager mgr = AppWidgetManager .getInstance (context );
135
133
final ComponentName cn = new ComponentName (context , WeatherWidgetProvider .class );
136
- int [] appWidgetIds = mgr .getAppWidgetIds (cn );
137
- for (int i = 0 ; i < appWidgetIds .length ; ++i ) {
138
- RemoteViews layout = buildLayout (context , appWidgetIds [i ], mIsLargeLayout );
139
- mgr .updateAppWidget (appWidgetIds [i ], layout );
140
- }
134
+ mgr .notifyAppWidgetViewDataChanged (mgr .getAppWidgetIds (cn ), R .id .weather_list );
141
135
}
142
136
});
137
+
138
+ final int appWidgetId = intent .getIntExtra (AppWidgetManager .EXTRA_APPWIDGET_ID ,
139
+ AppWidgetManager .INVALID_APPWIDGET_ID );
143
140
} else if (action .equals (CLICK_ACTION )) {
144
141
// Show a toast
145
142
final int appWidgetId = intent .getIntExtra (AppWidgetManager .EXTRA_APPWIDGET_ID ,
@@ -152,46 +149,16 @@ public void run() {
152
149
super .onReceive (ctx , intent );
153
150
}
154
151
155
- private ArrayList <RemoteViews > getListOfCities (Context context ) {
156
- final String packageName = context .getPackageName ();
157
- ArrayList <RemoteViews > citiesList = new ArrayList <RemoteViews >();
158
- Cursor c = context .getContentResolver ().query (WeatherDataProvider .CONTENT_URI , null ,
159
- null , null , null );
160
- final String itemFormatStr = context .getResources ().getString (R .string .item_format_string );
161
- while (c .moveToNext ()) {
162
- int tempColIndex = c .getColumnIndex (WeatherDataProvider .Columns .TEMPERATURE );
163
- int temp = c .getInt (tempColIndex );
164
- int dayColIndex = c .getColumnIndex (WeatherDataProvider .Columns .DAY );
165
- String day = c .getString (dayColIndex );
166
-
167
- RemoteViews rvRow = new RemoteViews (packageName , R .layout .widget_item );
168
- rvRow .setTextViewText (R .id .widget_item , String .format (itemFormatStr , temp , day ));
169
-
170
- // Set the click intent so that we can handle it and show a toast message
171
- final Intent fillInIntent = new Intent ();
172
- final Bundle extras = new Bundle ();
173
- extras .putString (WeatherWidgetProvider .EXTRA_DAY_ID , day );
174
- fillInIntent .putExtras (extras );
175
- rvRow .setOnClickFillInIntent (R .id .widget_item , fillInIntent );
176
-
177
- citiesList .add (rvRow );
178
- }
179
- c .close ();
180
- return citiesList ;
181
- }
182
-
183
152
private RemoteViews buildLayout (Context context , int appWidgetId , boolean largeLayout ) {
184
- final String packageName = context .getPackageName ();
185
-
186
153
RemoteViews rv ;
187
154
if (largeLayout ) {
188
155
// Specify the service to provide data for the collection widget. Note that we need to
189
156
// embed the appWidgetId via the data otherwise it will be ignored.
190
- rv = new RemoteViews ( packageName , R . layout . widget_layout );
191
-
192
- // Set the list of RemoteViews
193
- ArrayList < RemoteViews > citiesList = getListOfCities (context );
194
- rv .setRemoteAdapter (R .id .weather_list , citiesList , 1 );
157
+ final Intent intent = new Intent ( context , WeatherWidgetService . class );
158
+ intent . putExtra ( AppWidgetManager . EXTRA_APPWIDGET_ID , appWidgetId );
159
+ intent . setData ( Uri . parse ( intent . toUri ( Intent . URI_INTENT_SCHEME )));
160
+ rv = new RemoteViews (context . getPackageName (), R . layout . widget_layout );
161
+ rv .setRemoteAdapter (appWidgetId , R .id .weather_list , intent );
195
162
196
163
// Set the empty view to be displayed if the collection is empty. It must be a sibling
197
164
// view of the collection view.
0 commit comments