WidgetProvider.java revision bc47398187c6ffd132435e51d8d61e6ec79a79db
1/*
2 * Copyright (C) 2010 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
17package com.android.email.provider;
18
19import android.app.Service;
20import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProvider;
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.Intent;
25import android.util.Log;
26import android.widget.RemoteViewsService;
27
28import com.android.email.Email;
29import com.android.email.R;
30import com.android.email.widget.EmailWidget;
31import com.android.email.widget.WidgetManager;
32import com.android.emailcommon.Logging;
33
34import java.io.FileDescriptor;
35import java.io.PrintWriter;
36
37public class WidgetProvider extends AppWidgetProvider {
38    @Override
39    public void onEnabled(final Context context) {
40        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
41            Log.d(EmailWidget.TAG, "onEnabled");
42        }
43        super.onEnabled(context);
44    }
45
46    @Override
47    public void onDisabled(Context context) {
48        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
49            Log.d(EmailWidget.TAG, "onDisabled");
50        }
51        context.stopService(new Intent(context, WidgetService.class));
52        super.onDisabled(context);
53    }
54
55    @Override
56    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
57        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
58            Log.d(EmailWidget.TAG, "onUpdate");
59        }
60        super.onUpdate(context, appWidgetManager, appWidgetIds);
61        WidgetManager.getInstance().updateWidgets(context, appWidgetIds);
62    }
63
64    @Override
65    public void onDeleted(Context context, int[] appWidgetIds) {
66        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
67            Log.d(EmailWidget.TAG, "onDeleted");
68        }
69        WidgetManager.getInstance().deleteWidgets(context, appWidgetIds);
70        super.onDeleted(context, appWidgetIds);
71    }
72
73    @Override
74    public void onReceive(final Context context, Intent intent) {
75        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
76            Log.d(EmailWidget.TAG, "onReceive");
77        }
78        super.onReceive(context, intent);
79
80        if (EmailProvider.ACTION_NOTIFY_MESSAGE_LIST_DATASET_CHANGED.equals(intent.getAction())) {
81            // Retrieve the list of current widgets.
82            final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
83            final ComponentName component = new ComponentName(context, WidgetProvider.class);
84            final int[] widgetIds = appWidgetManager.getAppWidgetIds(component);
85
86            // Ideally, this would only call notify AppWidgetViewDataChanged for the widgets, where
87            // the account had the change, but the current intent doesn't include this information.
88
89            // Calling notifyAppWidgetViewDataChanged will cause onDataSetChanged() to be called
90            // on the RemoteViewsService.RemoteViewsFactory, starting the service if necessary.
91            appWidgetManager.notifyAppWidgetViewDataChanged(widgetIds, R.id.message_list);
92        }
93    }
94
95    /**
96     * We use the WidgetService for two purposes:
97     *  1) To provide a widget factory for RemoteViews, and
98     *  2) Catch our command Uri's (i.e. take actions on user clicks) and let EmailWidget
99     *     handle them.
100     */
101    public static class WidgetService extends RemoteViewsService {
102        @Override
103        public RemoteViewsFactory onGetViewFactory(Intent intent) {
104            // Which widget do we want (nice alliteration, huh?)
105            int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
106            if (widgetId == -1) return null;
107            // Find the existing widget or create it
108            return WidgetManager.getInstance().getOrCreateWidget(this, widgetId);
109        }
110
111        @Override
112        public int onStartCommand(Intent intent, int flags, int startId) {
113            if (intent.getData() != null) {
114                // EmailWidget creates intents, so it knows how to handle them.
115                EmailWidget.processIntent(this, intent);
116            }
117            return Service.START_NOT_STICKY;
118        }
119
120        @Override
121        protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
122            WidgetManager.getInstance().dump(fd, writer, args);
123        }
124    }
125 }
126