Merge "DO NOT MERGE Deferring new collections widget apis. Revert "Updating WeatherListWidget to the new collections widget api."" into jb-mr2-dev
This commit is contained in:
@@ -36,7 +36,7 @@
|
|||||||
</receiver>
|
</receiver>
|
||||||
|
|
||||||
<!-- The service serving the RemoteViews to the collection widget -->
|
<!-- The service serving the RemoteViews to the collection widget -->
|
||||||
<service android:name="android.support.v13.app.RemoteViewsServiceCompat"
|
<service android:name="WeatherWidgetService"
|
||||||
android:permission="android.permission.BIND_REMOTEVIEWS"
|
android:permission="android.permission.BIND_REMOTEVIEWS"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,6 @@
|
|||||||
android:initialLayout="@layout/widget_layout"
|
android:initialLayout="@layout/widget_layout"
|
||||||
android:resizeMode="vertical"
|
android:resizeMode="vertical"
|
||||||
android:minResizeWidth="280dp"
|
android:minResizeWidth="280dp"
|
||||||
android:minResizeHeight="40dp"
|
android:minResizeHeight="70dp"
|
||||||
android:previewImage="@drawable/preview">
|
android:previewImage="@drawable/preview">
|
||||||
</appwidget-provider>
|
</appwidget-provider>
|
||||||
|
|||||||
@@ -19,23 +19,21 @@ package com.example.android.weatherlistwidget;
|
|||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.appwidget.AppWidgetManager;
|
import android.appwidget.AppWidgetManager;
|
||||||
import android.appwidget.AppWidgetProvider;
|
import android.appwidget.AppWidgetProvider;
|
||||||
import android.content.ComponentName;
|
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.ContentUris;
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.database.ContentObserver;
|
import android.content.ComponentName;
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.content.ContentResolver;
|
||||||
|
import android.content.ContentUris;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.database.ContentObserver;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.HandlerThread;
|
import android.os.HandlerThread;
|
||||||
import android.util.Log;
|
|
||||||
import android.widget.RemoteViews;
|
import android.widget.RemoteViews;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,6 +73,7 @@ public class WeatherWidgetProvider extends AppWidgetProvider {
|
|||||||
private static final int sMaxDegrees = 96;
|
private static final int sMaxDegrees = 96;
|
||||||
|
|
||||||
private boolean mIsLargeLayout = true;
|
private boolean mIsLargeLayout = true;
|
||||||
|
private int mHeaderWeatherState = 0;
|
||||||
|
|
||||||
public WeatherWidgetProvider() {
|
public WeatherWidgetProvider() {
|
||||||
// Start the worker thread
|
// Start the worker thread
|
||||||
@@ -117,7 +116,6 @@ public class WeatherWidgetProvider extends AppWidgetProvider {
|
|||||||
final Cursor c = r.query(WeatherDataProvider.CONTENT_URI, null, null, null,
|
final Cursor c = r.query(WeatherDataProvider.CONTENT_URI, null, null, null,
|
||||||
null);
|
null);
|
||||||
final int count = c.getCount();
|
final int count = c.getCount();
|
||||||
c.close();
|
|
||||||
|
|
||||||
// We disable the data changed observer temporarily since each of the updates
|
// We disable the data changed observer temporarily since each of the updates
|
||||||
// will trigger an onChange() in our data observer.
|
// will trigger an onChange() in our data observer.
|
||||||
@@ -133,13 +131,12 @@ public class WeatherWidgetProvider extends AppWidgetProvider {
|
|||||||
|
|
||||||
final AppWidgetManager mgr = AppWidgetManager.getInstance(context);
|
final AppWidgetManager mgr = AppWidgetManager.getInstance(context);
|
||||||
final ComponentName cn = new ComponentName(context, WeatherWidgetProvider.class);
|
final ComponentName cn = new ComponentName(context, WeatherWidgetProvider.class);
|
||||||
int[] appWidgetIds = mgr.getAppWidgetIds(cn);
|
mgr.notifyAppWidgetViewDataChanged(mgr.getAppWidgetIds(cn), R.id.weather_list);
|
||||||
for (int i = 0; i < appWidgetIds.length; ++i) {
|
|
||||||
RemoteViews layout = buildLayout(context, appWidgetIds[i], mIsLargeLayout);
|
|
||||||
mgr.updateAppWidget(appWidgetIds[i], layout);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
|
||||||
|
AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||||
} else if (action.equals(CLICK_ACTION)) {
|
} else if (action.equals(CLICK_ACTION)) {
|
||||||
// Show a toast
|
// Show a toast
|
||||||
final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
|
final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
|
||||||
@@ -152,46 +149,16 @@ public class WeatherWidgetProvider extends AppWidgetProvider {
|
|||||||
super.onReceive(ctx, intent);
|
super.onReceive(ctx, intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<RemoteViews> getListOfCities(Context context) {
|
|
||||||
final String packageName = context.getPackageName();
|
|
||||||
ArrayList<RemoteViews> citiesList = new ArrayList<RemoteViews>();
|
|
||||||
Cursor c = context.getContentResolver().query(WeatherDataProvider.CONTENT_URI, null,
|
|
||||||
null, null, null);
|
|
||||||
final String itemFormatStr = context.getResources().getString(R.string.item_format_string);
|
|
||||||
while (c.moveToNext()) {
|
|
||||||
int tempColIndex = c.getColumnIndex(WeatherDataProvider.Columns.TEMPERATURE);
|
|
||||||
int temp = c.getInt(tempColIndex);
|
|
||||||
int dayColIndex = c.getColumnIndex(WeatherDataProvider.Columns.DAY);
|
|
||||||
String day = c.getString(dayColIndex);
|
|
||||||
|
|
||||||
RemoteViews rvRow = new RemoteViews(packageName, R.layout.widget_item);
|
|
||||||
rvRow.setTextViewText(R.id.widget_item, String.format(itemFormatStr, 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_DAY_ID, day);
|
|
||||||
fillInIntent.putExtras(extras);
|
|
||||||
rvRow.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
|
|
||||||
|
|
||||||
citiesList.add(rvRow);
|
|
||||||
}
|
|
||||||
c.close();
|
|
||||||
return citiesList;
|
|
||||||
}
|
|
||||||
|
|
||||||
private RemoteViews buildLayout(Context context, int appWidgetId, boolean largeLayout) {
|
private RemoteViews buildLayout(Context context, int appWidgetId, boolean largeLayout) {
|
||||||
final String packageName = context.getPackageName();
|
|
||||||
|
|
||||||
RemoteViews rv;
|
RemoteViews rv;
|
||||||
if (largeLayout) {
|
if (largeLayout) {
|
||||||
// Specify the service to provide data for the collection widget. Note that we need to
|
// 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.
|
// embed the appWidgetId via the data otherwise it will be ignored.
|
||||||
rv = new RemoteViews(packageName, R.layout.widget_layout);
|
final Intent intent = new Intent(context, WeatherWidgetService.class);
|
||||||
|
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
|
||||||
// Set the list of RemoteViews
|
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
|
||||||
ArrayList<RemoteViews> citiesList = getListOfCities(context);
|
rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
|
||||||
rv.setRemoteAdapter(R.id.weather_list, citiesList, 1);
|
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
|
// Set the empty view to be displayed if the collection is empty. It must be a sibling
|
||||||
// view of the collection view.
|
// view of the collection view.
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.example.android.weatherlistwidget;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.ContentUris;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.widget.RemoteViews;
|
||||||
|
import android.widget.RemoteViewsService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the service that provides the factory to be bound to the collection service.
|
||||||
|
*/
|
||||||
|
public class WeatherWidgetService extends RemoteViewsService {
|
||||||
|
@Override
|
||||||
|
public RemoteViewsFactory onGetViewFactory(Intent intent) {
|
||||||
|
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the factory that will provide data to the collection widget.
|
||||||
|
*/
|
||||||
|
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
|
||||||
|
private Context mContext;
|
||||||
|
private Cursor mCursor;
|
||||||
|
private int mAppWidgetId;
|
||||||
|
|
||||||
|
public StackRemoteViewsFactory(Context context, Intent intent) {
|
||||||
|
mContext = context;
|
||||||
|
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
|
||||||
|
AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onCreate() {
|
||||||
|
// Since we reload the cursor in onDataSetChanged() which gets called immediately after
|
||||||
|
// onCreate(), we do nothing here.
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onDestroy() {
|
||||||
|
if (mCursor != null) {
|
||||||
|
mCursor.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCount() {
|
||||||
|
return mCursor.getCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
public RemoteViews getViewAt(int position) {
|
||||||
|
// Get the data for this position from the content provider
|
||||||
|
String day = "Unknown Day";
|
||||||
|
int temp = 0;
|
||||||
|
if (mCursor.moveToPosition(position)) {
|
||||||
|
final int dayColIndex = mCursor.getColumnIndex(WeatherDataProvider.Columns.DAY);
|
||||||
|
final int tempColIndex = mCursor.getColumnIndex(
|
||||||
|
WeatherDataProvider.Columns.TEMPERATURE);
|
||||||
|
day = mCursor.getString(dayColIndex);
|
||||||
|
temp = mCursor.getInt(tempColIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a proper item with the proper day and temperature
|
||||||
|
final String formatStr = mContext.getResources().getString(R.string.item_format_string);
|
||||||
|
final int itemId = R.layout.widget_item;
|
||||||
|
RemoteViews rv = new RemoteViews(mContext.getPackageName(), itemId);
|
||||||
|
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_DAY_ID, day);
|
||||||
|
fillInIntent.putExtras(extras);
|
||||||
|
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
public RemoteViews getLoadingView() {
|
||||||
|
// We aren't going to return a default loading view in this sample
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getViewTypeCount() {
|
||||||
|
// Technically, we have two types of views (the dark and light background views)
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getItemId(int position) {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasStableIds() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onDataSetChanged() {
|
||||||
|
// Refresh the cursor
|
||||||
|
if (mCursor != null) {
|
||||||
|
mCursor.close();
|
||||||
|
}
|
||||||
|
mCursor = mContext.getContentResolver().query(WeatherDataProvider.CONTENT_URI, null, null,
|
||||||
|
null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user