MmsWidgetProvider.java revision c7aa632be8e7d3ebe71f236f534ea2f0af71e04a
1/*
2 * Copyright (C) 2012 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.mms.widget;
18
19import android.app.PendingIntent;
20import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProvider;
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.Intent;
25import android.net.Uri;
26import android.util.Log;
27import android.widget.RemoteViews;
28
29import com.android.mms.LogTag;
30import com.android.mms.R;
31import com.android.mms.ui.ComposeMessageActivity;
32import com.android.mms.ui.ConversationList;
33
34public class MmsWidgetProvider extends AppWidgetProvider {
35    public static final String ACTION_NOTIFY_DATASET_CHANGED =
36            "com.android.mms.intent.action.ACTION_NOTIFY_DATASET_CHANGED";
37
38    private static final String TAG = "MmsWidgetProvider";
39
40    /**
41     * Update all widgets in the list
42     */
43    @Override
44    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
45        super.onUpdate(context, appWidgetManager, appWidgetIds);
46
47        for (int i = 0; i < appWidgetIds.length; ++i) {
48            updateWidget(context, appWidgetIds[i]);
49        }
50    }
51
52    @Override
53    public void onReceive(Context context, Intent intent) {
54        if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
55            Log.v(TAG, "onReceive intent: " + intent);
56        }
57        String action = intent.getAction();
58
59        // The base class AppWidgetProvider's onReceive handles the normal widget intents. Here
60        // we're looking for an intent sent by the messaging app when it knows a message has
61        // been sent or received (or a conversation has been read) and is telling the widget it
62        // needs to update.
63        if (ACTION_NOTIFY_DATASET_CHANGED.equals(action)) {
64            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
65            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context,
66                    MmsWidgetProvider.class));
67            if (appWidgetIds.length > 0) {
68                appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[0],
69                        R.id.conversation_list);
70            }
71        } else {
72            super.onReceive(context, intent);
73        }
74    }
75
76    /**
77     * Update the widget appWidgetId
78     */
79    private static void updateWidget(Context context, int appWidgetId) {
80        if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
81            Log.v(TAG, "updateWidget appWidgetId: " + appWidgetId);
82        }
83        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
84        PendingIntent clickIntent;
85
86        // Launch an intent to avoid ANRs
87        final Intent intent = new Intent(context, MmsWidgetService.class);
88        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
89        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
90        remoteViews.setRemoteAdapter(appWidgetId, R.id.conversation_list, intent);
91
92        remoteViews.setTextViewText(R.id.widget_label, context.getString(R.string.app_label));
93
94        // Open Mms's app conversation list when click on header
95        final Intent convIntent = new Intent(context, ConversationList.class);
96        clickIntent = PendingIntent.getActivity(
97                context, 0, convIntent, PendingIntent.FLAG_UPDATE_CURRENT);
98        remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);
99
100        // On click intent for Compose
101        final Intent composeIntent = new Intent(context, ComposeMessageActivity.class);
102        composeIntent.setAction(Intent.ACTION_SENDTO);
103        clickIntent = PendingIntent.getActivity(
104                context, 0, composeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
105        remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);
106
107        // On click intent for Conversation
108        final Intent conversationIntent = new Intent();
109        conversationIntent.setAction(Intent.ACTION_VIEW);
110        clickIntent = PendingIntent.getActivity(
111                context, 0, conversationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
112        remoteViews.setPendingIntentTemplate(R.id.conversation_list, clickIntent);
113
114        AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
115    }
116
117    /*
118     * notifyDatasetChanged call when the conversation list changes so the mms widget will
119     * update and reflect the changes
120     */
121    public static void notifyDatasetChanged(Context context) {
122        if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
123            Log.v(TAG, "notifyDatasetChanged");
124        }
125        final Intent intent = new Intent(ACTION_NOTIFY_DATASET_CHANGED);
126        context.sendBroadcast(intent);
127    }
128
129}