/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.mms.widget;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
import com.android.mms.LogTag;
import com.android.mms.R;
import com.android.mms.ui.ComposeMessageActivity;
import com.android.mms.ui.ConversationList;
public class MmsWidgetProvider extends AppWidgetProvider {
public static final String ACTION_NOTIFY_DATASET_CHANGED =
"com.android.mms.intent.action.ACTION_NOTIFY_DATASET_CHANGED";
private static final String TAG = "MmsWidgetProvider";
/**
* Update all widgets in the list
*/
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i = 0; i < appWidgetIds.length; ++i) {
updateWidget(context, appWidgetIds[i]);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
Log.v(TAG, "onReceive intent: " + intent);
}
String action = intent.getAction();
// The base class AppWidgetProvider's onReceive handles the normal widget intents. Here
// we're looking for an intent sent by the messaging app when it knows a message has
// been sent or received (or a conversation has been read) and is telling the widget it
// needs to update.
if (ACTION_NOTIFY_DATASET_CHANGED.equals(action)) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context,
MmsWidgetProvider.class));
// We need to update all Mms appwidgets on the home screen.
for (int appWidgetId : appWidgetIds) {
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId,
R.id.conversation_list);
}
} else {
super.onReceive(context, intent);
}
}
/**
* Update the widget appWidgetId
*/
private static void updateWidget(Context context, int appWidgetId) {
if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
Log.v(TAG, "updateWidget appWidgetId: " + appWidgetId);
}
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
PendingIntent clickIntent;
// Launch an intent to avoid ANRs
final Intent intent = new Intent(context, MmsWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.conversation_list, intent);
remoteViews.setTextViewText(R.id.widget_label, context.getString(R.string.app_label));
// Open Mms's app conversation list when click on header
final Intent convIntent = new Intent(context, ConversationList.class);
clickIntent = PendingIntent.getActivity(
context, 0, convIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);
// On click intent for Compose
final Intent composeIntent = new Intent(context, ComposeMessageActivity.class);
composeIntent.setAction(Intent.ACTION_SENDTO);
clickIntent = PendingIntent.getActivity(
context, 0, composeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);
// On click intent for Conversation
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addParentStack(ComposeMessageActivity.class);
Intent msgIntent = new Intent(Intent.ACTION_VIEW);
msgIntent.setType("vnd.android-dir/mms-sms");
taskStackBuilder.addNextIntent(msgIntent);
remoteViews.setPendingIntentTemplate(R.id.conversation_list,
taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
}
/*
* notifyDatasetChanged call when the conversation list changes so the mms widget will
* update and reflect the changes
*/
public static void notifyDatasetChanged(Context context) {
if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
Log.v(TAG, "notifyDatasetChanged");
}
final Intent intent = new Intent(ACTION_NOTIFY_DATASET_CHANGED);
context.sendBroadcast(intent);
}
}