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