Home | History | Annotate | Download | only in widget
      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
     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
     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             appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,
     71                     R.id.conversation_list);
     72         } else {
     73             super.onReceive(context, intent);
     74         }
     75     }
     76 
     77     /**
     78      * Update the widget appWidgetId
     79      */
     80     private static void updateWidget(Context context, int appWidgetId) {
     81         if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
     82             Log.v(TAG, "updateWidget appWidgetId: " + appWidgetId);
     83         }
     84         RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
     85         PendingIntent clickIntent;
     86 
     87         // Launch an intent to avoid ANRs
     88         final Intent intent = new Intent(context, MmsWidgetService.class);
     89         intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
     90         intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
     91         remoteViews.setRemoteAdapter(appWidgetId, R.id.conversation_list, intent);
     92 
     93         remoteViews.setTextViewText(R.id.widget_label, context.getString(R.string.app_label));
     94 
     95         // Open Mms's app conversation list when click on header
     96         final Intent convIntent = new Intent(context, ConversationList.class);
     97         clickIntent = PendingIntent.getActivity(
     98                 context, 0, convIntent, PendingIntent.FLAG_UPDATE_CURRENT);
     99         remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);
    100 
    101         // On click intent for Compose
    102         final Intent composeIntent = new Intent(context, ComposeMessageActivity.class);
    103         composeIntent.setAction(Intent.ACTION_SENDTO);
    104         clickIntent = PendingIntent.getActivity(
    105                 context, 0, composeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    106         remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);
    107 
    108         // On click intent for Conversation
    109         TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    110         taskStackBuilder.addParentStack(ComposeMessageActivity.class);
    111         Intent msgIntent = new Intent(Intent.ACTION_VIEW);
    112         msgIntent.setType("vnd.android-dir/mms-sms");
    113         taskStackBuilder.addNextIntent(msgIntent);
    114         remoteViews.setPendingIntentTemplate(R.id.conversation_list,
    115                 taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
    116 
    117         AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
    118     }
    119 
    120     /*
    121      * notifyDatasetChanged call when the conversation list changes so the mms widget will
    122      * update and reflect the changes
    123      */
    124     public static void notifyDatasetChanged(Context context) {
    125         if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
    126             Log.v(TAG, "notifyDatasetChanged");
    127         }
    128         final Intent intent = new Intent(ACTION_NOTIFY_DATASET_CHANGED);
    129         context.sendBroadcast(intent);
    130     }
    131 
    132 }