package com.nullware.android.fortunequote;
import java.util.Arrays;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.RemoteViews;
/**
* Service that displays a new random quote in all widgets.
*/
public class WidgetService extends Service {
private static final String TAG = FortuneQuote.TAG + ".WidgetService";
private static SharedPreferences preferences;
private static QuoteData quoteData;
@Override
public void onCreate() {
if (preferences == null) preferences = PreferenceManager.getDefaultSharedPreferences(this);
if (quoteData == null) quoteData = new QuoteData(this);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
handleIntent(intent);
}
public synchronized void handleIntent(Intent intent) {
Log.d(TAG, "handleIntent called");
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS) != null) {
updateWidget(this, extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS));
} else if (extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, 0) != 0) {
updateWidget(this, extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID));
}
}
stopSelf();
}
/**
* Update a group of widgets.
*
* @param context
* @param appWidgetIds List of widget IDs to update
*/
private void updateWidget(Context context, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
updateWidget(context, appWidgetId);
}
}
/**
* Update a single widget.
*
* @param context
* @param appWidgetId The widget ID of the widget to update
*/
private void updateWidget(Context context, int appWidgetId) {
assert (preferences != null);
Log.d(TAG, "updateWidget called with appWidgetId=" + appWidgetId);
if (quoteData == null) quoteData = new QuoteData(context);
String topicsDefault = context.getString(R.string.topics_default);
String topicsStr = preferences.getString(Widget.getAppWidgetIdPreferenceKey(appWidgetId, context.getString(R.string.widget_topics_key)), topicsDefault);
Log.d(TAG, "Loaded preference: " + Widget.getAppWidgetIdPreferenceKey(appWidgetId, context.getString(R.string.widget_topics_key)) + "=" + topicsStr);
String topicsStrArray[] = ListPreferenceMultiSelect.parseValue(topicsStr);
int[] topics = new int[topicsStrArray.length];
for (int j = 0; j < topicsStrArray.length; j++) {
topics[j] = Integer.parseInt(topicsStrArray[j]);
}
boolean randomTopicDefault = context.getString(R.string.randomTopic_default).equals("true");
boolean randomTopic = preferences.getBoolean(Widget.getAppWidgetIdPreferenceKey(appWidgetId, context.getString(R.string.widget_randomTopic_key)), randomTopicDefault);
Log.d(TAG, "Loaded preference: " + Widget.getAppWidgetIdPreferenceKey(appWidgetId, context.getString(R.string.widget_randomTopic_key)) + "=" + randomTopic);
Log.d(TAG, "getRandomQuote called with topics=" + Arrays.toString(topics) + ", randomTopic=" + randomTopic);
String quote = quoteData.getRandomQuote(topics, randomTopic);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.widget_text, quote);
Intent intent = new Intent(this, Widget.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setAction(Widget.REFRESH_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(appWidgetId, views);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}