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