summaryrefslogtreecommitdiff
path: root/samples/StackWidget/src/com/example/android/stackwidget/StackWidgetProvider.java
diff options
context:
space:
mode:
authorAdam Cohen <adamcohen@google.com>2011-02-10 18:19:50 -0800
committerAdam Cohen <adamcohen@google.com>2011-02-11 15:11:43 -0800
commit1ff28c7b12986856d8a975d4713d35093b831016 (patch)
tree0ea2b8245f51bbf26a42a852eaab16db3d5d19f8 /samples/StackWidget/src/com/example/android/stackwidget/StackWidgetProvider.java
parentf8da117394bf87387fb71f3d8465edb558949c21 (diff)
Making StackWidget sample code stand alone as opposed to bundled with HoneycombGallery
Change-Id: I9da8433ee8a9710f5a5362a6f82fc96d4f456c88
Diffstat (limited to 'samples/StackWidget/src/com/example/android/stackwidget/StackWidgetProvider.java')
-rw-r--r--samples/StackWidget/src/com/example/android/stackwidget/StackWidgetProvider.java94
1 files changed, 94 insertions, 0 deletions
diff --git a/samples/StackWidget/src/com/example/android/stackwidget/StackWidgetProvider.java b/samples/StackWidget/src/com/example/android/stackwidget/StackWidgetProvider.java
new file mode 100644
index 000000000..e053c2103
--- /dev/null
+++ b/samples/StackWidget/src/com/example/android/stackwidget/StackWidgetProvider.java
@@ -0,0 +1,94 @@
+/*
+ * 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.stackwidget;
+
+import android.app.PendingIntent;
+import android.appwidget.AppWidgetManager;
+import android.appwidget.AppWidgetProvider;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.widget.RemoteViews;
+import android.widget.Toast;
+
+public class StackWidgetProvider extends AppWidgetProvider {
+ public static final String TOAST_ACTION = "com.example.android.stackwidget.TOAST_ACTION";
+ public static final String EXTRA_ITEM = "com.example.android.stackwidget.EXTRA_ITEM";
+
+ @Override
+ public void onDeleted(Context context, int[] appWidgetIds) {
+ super.onDeleted(context, appWidgetIds);
+ }
+
+ @Override
+ public void onDisabled(Context context) {
+ super.onDisabled(context);
+ }
+
+ @Override
+ public void onEnabled(Context context) {
+ super.onEnabled(context);
+ }
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ AppWidgetManager mgr = AppWidgetManager.getInstance(context);
+ if (intent.getAction().equals(TOAST_ACTION)) {
+ int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
+ AppWidgetManager.INVALID_APPWIDGET_ID);
+ int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
+ Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
+ }
+ super.onReceive(context, 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) {
+
+ // Here we setup the intent which points to the StackViewService which will
+ // provide the views for this collection.
+ Intent intent = new Intent(context, StackWidgetService.class);
+ intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
+ // When intents are compared, the extras are ignored, so we need to embed the extras
+ // into the data so that the extras will not be ignored.
+ intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
+ RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
+ rv.setRemoteAdapter(appWidgetIds[i], R.id.stack_view, intent);
+
+ // The empty view is displayed when the collection has no items. It should be a sibling
+ // of the collection view.
+ rv.setEmptyView(R.id.stack_view, R.id.empty_view);
+
+ // Here we setup the a pending intent template. Individuals items of a collection
+ // cannot setup their own pending intents, instead, the collection as a whole can
+ // setup a pending intent template, and the individual items can set a fillInIntent
+ // to create unique before on an item to item basis.
+ Intent toastIntent = new Intent(context, StackWidgetProvider.class);
+ toastIntent.setAction(StackWidgetProvider.TOAST_ACTION);
+ toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
+ intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
+ PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
+ PendingIntent.FLAG_UPDATE_CURRENT);
+ rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);
+
+ appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
+ }
+ super.onUpdate(context, appWidgetManager, appWidgetIds);
+ }
+} \ No newline at end of file