• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.email.provider;
18 
19 import android.app.Service;
20 import android.appwidget.AppWidgetManager;
21 import android.appwidget.AppWidgetProvider;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.util.Log;
26 import android.widget.RemoteViewsService;
27 
28 import com.android.email.Email;
29 import com.android.email.R;
30 import com.android.email.widget.EmailWidget;
31 import com.android.email.widget.WidgetManager;
32 import com.android.emailcommon.Logging;
33 
34 import java.io.FileDescriptor;
35 import java.io.PrintWriter;
36 
37 public class WidgetProvider extends AppWidgetProvider {
38     @Override
onEnabled(final Context context)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
onDisabled(Context context)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
onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)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
onDeleted(Context context, int[] appWidgetIds)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
onReceive(final Context context, Intent intent)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
onGetViewFactory(Intent intent)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
onStartCommand(Intent intent, int flags, int startId)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
dump(FileDescriptor fd, PrintWriter writer, String[] args)121         protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
122             WidgetManager.getInstance().dump(fd, writer, args);
123         }
124     }
125  }
126