WidgetProvider.java revision 7f4cf3c46b0dd1bc001c547807e83b7c280f074b
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 com.android.email.Email;
20import com.android.email.widget.EmailWidget;
21import com.android.email.widget.WidgetManager;
22import com.android.emailcommon.Logging;
23
24import android.app.Service;
25import android.appwidget.AppWidgetManager;
26import android.appwidget.AppWidgetProvider;
27import android.content.Context;
28import android.content.Intent;
29import android.util.Log;
30import android.widget.RemoteViewsService;
31
32import java.io.FileDescriptor;
33import java.io.PrintWriter;
34
35public class WidgetProvider extends AppWidgetProvider {
36    @Override
37    public void onEnabled(final Context context) {
38        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
39            Log.d(EmailWidget.TAG, "onEnabled");
40        }
41        super.onEnabled(context);
42    }
43
44    @Override
45    public void onDisabled(Context context) {
46        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
47            Log.d(EmailWidget.TAG, "onDisabled");
48        }
49        context.stopService(new Intent(context, WidgetService.class));
50        super.onDisabled(context);
51    }
52
53    @Override
54    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
55        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
56            Log.d(EmailWidget.TAG, "onUpdate");
57        }
58        super.onUpdate(context, appWidgetManager, appWidgetIds);
59        WidgetManager.getInstance().createWidgets(context, appWidgetIds);
60    }
61
62    @Override
63    public void onDeleted(Context context, int[] appWidgetIds) {
64        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
65            Log.d(EmailWidget.TAG, "onDeleted");
66        }
67        WidgetManager.getInstance().deleteWidgets(context, appWidgetIds);
68        super.onDeleted(context, appWidgetIds);
69    }
70
71    @Override
72    public void onReceive(final Context context, Intent intent) {
73        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
74            Log.d(EmailWidget.TAG, "onReceive");
75        }
76        super.onReceive(context, intent);
77    }
78
79    /**
80     * We use the WidgetService for two purposes:
81     *  1) To provide a widget factory for RemoteViews, and
82     *  2) Catch our command Uri's (i.e. take actions on user clicks) and let EmailWidget
83     *     handle them.
84     */
85    public static class WidgetService extends RemoteViewsService {
86        @Override
87        public RemoteViewsFactory onGetViewFactory(Intent intent) {
88            // Which widget do we want (nice alliteration, huh?)
89            int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
90            if (widgetId == -1) return null;
91            // Find the existing widget or create it
92            return WidgetManager.getInstance().getOrCreateWidget(this, widgetId);
93        }
94
95        @Override
96        public int onStartCommand(Intent intent, int flags, int startId) {
97            if (intent.getData() != null) {
98                // EmailWidget creates intents, so it knows how to handle them.
99                EmailWidget.processIntent(this, intent);
100            }
101            return Service.START_NOT_STICKY;
102        }
103
104        @Override
105        protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
106            WidgetManager.getInstance().dump(fd, writer, args);
107        }
108    }
109 }
110