Skip to content

Commit 412de09

Browse files
Winson ChungAndroid (Google) Code Review
authored andcommitted
Merge "DO NOT MERGE Deferring new collections widget apis. Revert "Updating WeatherListWidget to the new collections widget api."" into jb-mr2-dev
2 parents 55cadb0 + d86ea2c commit 412de09

File tree

4 files changed

+140
-49
lines changed

4 files changed

+140
-49
lines changed

samples/WeatherListWidget/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</receiver>
3737

3838
<!-- The service serving the RemoteViews to the collection widget -->
39-
<service android:name="android.support.v13.app.RemoteViewsServiceCompat"
39+
<service android:name="WeatherWidgetService"
4040
android:permission="android.permission.BIND_REMOTEVIEWS"
4141
android:exported="false" />
4242

samples/WeatherListWidget/res/xml/widgetinfo.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
android:initialLayout="@layout/widget_layout"
2222
android:resizeMode="vertical"
2323
android:minResizeWidth="280dp"
24-
android:minResizeHeight="40dp"
24+
android:minResizeHeight="70dp"
2525
android:previewImage="@drawable/preview">
2626
</appwidget-provider>

samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetProvider.java

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,21 @@
1919
import android.app.PendingIntent;
2020
import android.appwidget.AppWidgetManager;
2121
import android.appwidget.AppWidgetProvider;
22+
import android.content.Context;
23+
import android.content.Intent;
2224
import android.content.ComponentName;
25+
import android.content.ContentValues;
2326
import android.content.ContentResolver;
2427
import android.content.ContentUris;
25-
import android.content.ContentValues;
26-
import android.content.Context;
27-
import android.content.Intent;
28-
import android.database.ContentObserver;
2928
import android.database.Cursor;
29+
import android.database.ContentObserver;
3030
import android.net.Uri;
3131
import android.os.Bundle;
3232
import android.os.Handler;
3333
import android.os.HandlerThread;
34-
import android.util.Log;
3534
import android.widget.RemoteViews;
3635
import android.widget.Toast;
3736

38-
import java.util.ArrayList;
3937
import java.util.Random;
4038

4139
/**
@@ -75,6 +73,7 @@ public class WeatherWidgetProvider extends AppWidgetProvider {
7573
private static final int sMaxDegrees = 96;
7674

7775
private boolean mIsLargeLayout = true;
76+
private int mHeaderWeatherState = 0;
7877

7978
public WeatherWidgetProvider() {
8079
// Start the worker thread
@@ -117,7 +116,6 @@ public void run() {
117116
final Cursor c = r.query(WeatherDataProvider.CONTENT_URI, null, null, null,
118117
null);
119118
final int count = c.getCount();
120-
c.close();
121119

122120
// We disable the data changed observer temporarily since each of the updates
123121
// will trigger an onChange() in our data observer.
@@ -133,13 +131,12 @@ public void run() {
133131

134132
final AppWidgetManager mgr = AppWidgetManager.getInstance(context);
135133
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);
141135
}
142136
});
137+
138+
final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
139+
AppWidgetManager.INVALID_APPWIDGET_ID);
143140
} else if (action.equals(CLICK_ACTION)) {
144141
// Show a toast
145142
final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
@@ -152,46 +149,16 @@ public void run() {
152149
super.onReceive(ctx, intent);
153150
}
154151

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-
183152
private RemoteViews buildLayout(Context context, int appWidgetId, boolean largeLayout) {
184-
final String packageName = context.getPackageName();
185-
186153
RemoteViews rv;
187154
if (largeLayout) {
188155
// Specify the service to provide data for the collection widget. Note that we need to
189156
// 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);
195162

196163
// Set the empty view to be displayed if the collection is empty. It must be a sibling
197164
// view of the collection view.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.weatherlistwidget;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import android.appwidget.AppWidgetManager;
23+
import android.content.Context;
24+
import android.content.Intent;
25+
import android.content.ContentUris;
26+
import android.database.Cursor;
27+
import android.net.Uri;
28+
import android.os.Bundle;
29+
import android.widget.RemoteViews;
30+
import android.widget.RemoteViewsService;
31+
32+
/**
33+
* This is the service that provides the factory to be bound to the collection service.
34+
*/
35+
public class WeatherWidgetService extends RemoteViewsService {
36+
@Override
37+
public RemoteViewsFactory onGetViewFactory(Intent intent) {
38+
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
39+
}
40+
}
41+
42+
/**
43+
* This is the factory that will provide data to the collection widget.
44+
*/
45+
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
46+
private Context mContext;
47+
private Cursor mCursor;
48+
private int mAppWidgetId;
49+
50+
public StackRemoteViewsFactory(Context context, Intent intent) {
51+
mContext = context;
52+
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
53+
AppWidgetManager.INVALID_APPWIDGET_ID);
54+
}
55+
56+
public void onCreate() {
57+
// Since we reload the cursor in onDataSetChanged() which gets called immediately after
58+
// onCreate(), we do nothing here.
59+
}
60+
61+
public void onDestroy() {
62+
if (mCursor != null) {
63+
mCursor.close();
64+
}
65+
}
66+
67+
public int getCount() {
68+
return mCursor.getCount();
69+
}
70+
71+
public RemoteViews getViewAt(int position) {
72+
// Get the data for this position from the content provider
73+
String day = "Unknown Day";
74+
int temp = 0;
75+
if (mCursor.moveToPosition(position)) {
76+
final int dayColIndex = mCursor.getColumnIndex(WeatherDataProvider.Columns.DAY);
77+
final int tempColIndex = mCursor.getColumnIndex(
78+
WeatherDataProvider.Columns.TEMPERATURE);
79+
day = mCursor.getString(dayColIndex);
80+
temp = mCursor.getInt(tempColIndex);
81+
}
82+
83+
// Return a proper item with the proper day and temperature
84+
final String formatStr = mContext.getResources().getString(R.string.item_format_string);
85+
final int itemId = R.layout.widget_item;
86+
RemoteViews rv = new RemoteViews(mContext.getPackageName(), itemId);
87+
rv.setTextViewText(R.id.widget_item, String.format(formatStr, temp, day));
88+
89+
// Set the click intent so that we can handle it and show a toast message
90+
final Intent fillInIntent = new Intent();
91+
final Bundle extras = new Bundle();
92+
extras.putString(WeatherWidgetProvider.EXTRA_DAY_ID, day);
93+
fillInIntent.putExtras(extras);
94+
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
95+
96+
return rv;
97+
}
98+
public RemoteViews getLoadingView() {
99+
// We aren't going to return a default loading view in this sample
100+
return null;
101+
}
102+
103+
public int getViewTypeCount() {
104+
// Technically, we have two types of views (the dark and light background views)
105+
return 2;
106+
}
107+
108+
public long getItemId(int position) {
109+
return position;
110+
}
111+
112+
public boolean hasStableIds() {
113+
return true;
114+
}
115+
116+
public void onDataSetChanged() {
117+
// Refresh the cursor
118+
if (mCursor != null) {
119+
mCursor.close();
120+
}
121+
mCursor = mContext.getContentResolver().query(WeatherDataProvider.CONTENT_URI, null, null,
122+
null, null);
123+
}
124+
}

0 commit comments

Comments
 (0)