• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.stackwidget;
18 
19 import android.app.PendingIntent;
20 import android.appwidget.AppWidgetManager;
21 import android.appwidget.AppWidgetProvider;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.net.Uri;
25 import android.widget.RemoteViews;
26 import android.widget.Toast;
27 
28 public class StackWidgetProvider extends AppWidgetProvider {
29     public static final String TOAST_ACTION = "com.example.android.stackwidget.TOAST_ACTION";
30     public static final String EXTRA_ITEM = "com.example.android.stackwidget.EXTRA_ITEM";
31 
32     @Override
onDeleted(Context context, int[] appWidgetIds)33     public void onDeleted(Context context, int[] appWidgetIds) {
34         super.onDeleted(context, appWidgetIds);
35     }
36 
37     @Override
onDisabled(Context context)38     public void onDisabled(Context context) {
39         super.onDisabled(context);
40     }
41 
42     @Override
onEnabled(Context context)43     public void onEnabled(Context context) {
44         super.onEnabled(context);
45     }
46 
47     @Override
onReceive(Context context, Intent intent)48     public void onReceive(Context context, Intent intent) {
49         AppWidgetManager mgr = AppWidgetManager.getInstance(context);
50         if (intent.getAction().equals(TOAST_ACTION)) {
51             int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
52                     AppWidgetManager.INVALID_APPWIDGET_ID);
53             int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
54             Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
55         }
56         super.onReceive(context, intent);
57     }
58 
59     @Override
onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)60     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
61         // update each of the widgets with the remote adapter
62         for (int i = 0; i < appWidgetIds.length; ++i) {
63 
64             // Here we setup the intent which points to the StackViewService which will
65             // provide the views for this collection.
66             Intent intent = new Intent(context, StackWidgetService.class);
67             intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
68             // When intents are compared, the extras are ignored, so we need to embed the extras
69             // into the data so that the extras will not be ignored.
70             intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
71             RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
72             rv.setRemoteAdapter(appWidgetIds[i], R.id.stack_view, intent);
73 
74             // The empty view is displayed when the collection has no items. It should be a sibling
75             // of the collection view.
76             rv.setEmptyView(R.id.stack_view, R.id.empty_view);
77 
78             // Here we setup the a pending intent template. Individuals items of a collection
79             // cannot setup their own pending intents, instead, the collection as a whole can
80             // setup a pending intent template, and the individual items can set a fillInIntent
81             // to create unique before on an item to item basis.
82             Intent toastIntent = new Intent(context, StackWidgetProvider.class);
83             toastIntent.setAction(StackWidgetProvider.TOAST_ACTION);
84             toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
85             intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
86             PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
87                     PendingIntent.FLAG_UPDATE_CURRENT);
88             rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);
89 
90             appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
91         }
92         super.onUpdate(context, appWidgetManager, appWidgetIds);
93     }
94 }