From dbafc83c7f11470efb9f4c03283188dc00731063 Mon Sep 17 00:00:00 2001 From: Jeremy Symon Date: Fri, 25 Jul 2014 11:55:14 +1200 Subject: [PATCH] Add support for per-widget settings (#37) --- .../calendar/EventAppWidgetProvider.java | 32 ++++++++++------ .../calendar/EventRemoteViewsFactory.java | 16 ++++---- .../calendar/EventWidgetService.java | 12 +++++- .../plusonelabs/calendar/RemoteViewsUtil.java | 20 +++++----- .../java/com/plusonelabs/calendar/Theme.java | 3 +- .../calendar/WidgetConfigurationActivity.java | 34 ++++++++++++++--- .../calendar/CalendarEventProvider.java | 7 ++-- .../calendar/CalendarEventVisualizer.java | 14 +++---- .../prefs/AppearancePreferencesFragment.java | 11 ++++-- .../prefs/BackgroundTransparencyDialog.java | 21 +++++++---- .../prefs/CalendarPreferencesFragment.java | 6 +-- .../EventDetailsPreferencesFragment.java | 3 +- .../prefs/FeedbackPreferencesFragment.java | 3 +- .../prefs/UniquePreferencesFragment.java | 37 +++++++++++++++++++ 14 files changed, 149 insertions(+), 70 deletions(-) create mode 100644 app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/UniquePreferencesFragment.java diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventAppWidgetProvider.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventAppWidgetProvider.java index 94d0ad8a..0e561038 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventAppWidgetProvider.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventAppWidgetProvider.java @@ -4,6 +4,7 @@ import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; +import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -13,10 +14,13 @@ import android.net.Uri; import android.preference.PreferenceManager; import android.text.format.DateUtils; +import android.util.Log; import android.view.ContextThemeWrapper; import android.view.View; import android.widget.RemoteViews; +import com.plusonelabs.calendar.prefs.UniquePreferencesFragment; + import org.joda.time.DateTime; import java.util.List; @@ -44,20 +48,23 @@ public class EventAppWidgetProvider extends AppWidgetProvider { @Override public void onUpdate(Context baseContext, AppWidgetManager appWidgetManager, int[] appWidgetIds) { - int themeId = getCurrentThemeId(baseContext, PREF_HEADER_THEME, PREF_HEADER_THEME_DEFAULT); - Context context = new ContextThemeWrapper(baseContext, themeId); - AlarmReceiver.scheduleAlarm(context); for (int widgetId : appWidgetIds) { + int themeId = getCurrentThemeId( + baseContext, PREF_HEADER_THEME, PREF_HEADER_THEME_DEFAULT, + UniquePreferencesFragment.getPreferences(baseContext, widgetId) + ); + Context context = new ContextThemeWrapper(baseContext, themeId); + AlarmReceiver.scheduleAlarm(context); + SharedPreferences prefs = UniquePreferencesFragment.getPreferences(context, widgetId); RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget); - configureBackground(context, rv); - configureActionBar(context, rv); + configureBackground(context, rv, prefs); + configureActionBar(context, rv, widgetId, prefs); configureList(context, widgetId, rv); appWidgetManager.updateAppWidget(widgetId, rv); } } - private void configureBackground(Context context, RemoteViews rv) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + private void configureBackground(Context context, RemoteViews rv, SharedPreferences prefs) { if (prefs.getBoolean(PREF_SHOW_HEADER, true)) { rv.setViewVisibility(R.id.action_bar, View.VISIBLE); } else { @@ -69,14 +76,17 @@ private void configureBackground(Context context, RemoteViews rv) { setAlpha(rv, R.id.background_image, alpha(color)); } - private void configureActionBar(Context context, RemoteViews rv) { + private void configureActionBar(Context context, RemoteViews rv, int widgetId, SharedPreferences prefs) { rv.setOnClickPendingIntent(R.id.calendar_current_date, createOpenCalendarPendingIntent(context)); String formattedDate = DateUtils.formatDateTime(context, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY); rv.setTextViewText(R.id.calendar_current_date, formattedDate.toUpperCase(Locale.getDefault())); setTextColorFromAttr(context, rv, R.id.calendar_current_date, R.attr.header); - setActionIcons(context, rv); + setActionIcons(context, rv, prefs); Intent startConfigIntent = new Intent(context, WidgetConfigurationActivity.class); + startConfigIntent.setData(ContentUris.withAppendedId(Uri.EMPTY, widgetId)); + startConfigIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId); + startConfigIntent.putExtra(WidgetConfigurationActivity.WIDGET_INIT, false); PendingIntent menuPendingIntent = PendingIntent.getActivity(context, 0, startConfigIntent, PendingIntent.FLAG_UPDATE_CURRENT); rv.setOnClickPendingIntent(R.id.overflow_menu, menuPendingIntent); @@ -90,10 +100,10 @@ private void configureActionBar(Context context, RemoteViews rv) { } } - private void setActionIcons(Context context, RemoteViews rv) { + private void setActionIcons(Context context, RemoteViews rv, SharedPreferences prefs) { setImageFromAttr(context, rv, R.id.add_event, R.attr.header_action_add_event); setImageFromAttr(context, rv, R.id.overflow_menu, R.attr.header_action_overflow); - int themeId = getCurrentThemeId(context, PREF_HEADER_THEME, PREF_HEADER_THEME_DEFAULT); + int themeId = getCurrentThemeId(context, PREF_HEADER_THEME, PREF_HEADER_THEME_DEFAULT, prefs); int alpha = 255; if (themeId == R.style.Theme_Calendar_Dark || themeId == R.style.Theme_Calendar_Light) { alpha = 154; diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventRemoteViewsFactory.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventRemoteViewsFactory.java index 62d51cd7..b3c57adb 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventRemoteViewsFactory.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventRemoteViewsFactory.java @@ -4,18 +4,17 @@ import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; -import android.text.format.DateUtils; import android.widget.RemoteViews; import android.widget.RemoteViewsService.RemoteViewsFactory; import com.plusonelabs.calendar.calendar.CalendarEventVisualizer; import com.plusonelabs.calendar.model.DayHeader; import com.plusonelabs.calendar.model.Event; +import com.plusonelabs.calendar.prefs.UniquePreferencesFragment; import org.joda.time.DateTime; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.Locale; @@ -37,11 +36,13 @@ public class EventRemoteViewsFactory implements RemoteViewsFactory { private final Context context; private final List eventEntries; private final List> eventProviders; + private final SharedPreferences prefs; - public EventRemoteViewsFactory(Context context) { + public EventRemoteViewsFactory(Context context, SharedPreferences prefs) { this.context = context; + this.prefs = prefs; eventProviders = new ArrayList<>(); - eventProviders.add(new CalendarEventVisualizer(context)); + eventProviders.add(new CalendarEventVisualizer(context, prefs)); eventEntries = new ArrayList<>(); } @@ -74,24 +75,23 @@ public RemoteViews getViewAt(int position) { } public RemoteViews updateDayHeader(DayHeader dayHeader) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String alignment = prefs.getString(PREF_DAY_HEADER_ALIGNMENT, PREF_DAY_HEADER_ALIGNMENT_DEFAULT); RemoteViews rv = new RemoteViews(context.getPackageName(), valueOf(alignment).getLayoutId()); String dateString = DateUtil.createDateString(context, dayHeader.getStartDate()) .toUpperCase(Locale.getDefault()); rv.setTextViewText(R.id.day_header_title, dateString); - setTextSize(context, rv, R.id.day_header_title, R.dimen.day_header_title); + setTextSize(context, rv, R.id.day_header_title, R.dimen.day_header_title, prefs); setTextColorFromAttr(context, rv, R.id.day_header_title, R.attr.dayHeaderTitle); setBackgroundColorFromAttr(context, rv, R.id.day_header_separator, R.attr.dayHeaderSeparator); setPadding(context, rv, R.id.day_header_title, 0, R.dimen.day_header_padding_top, - R.dimen.day_header_padding_right, R.dimen.day_header_padding_bottom); + R.dimen.day_header_padding_right, R.dimen.day_header_padding_bottom, prefs); Intent intent = createOpenCalendarAtDayIntent(dayHeader.getStartDate()); rv.setOnClickFillInIntent(R.id.day_header, intent); return rv; } public void onDataSetChanged() { - context.setTheme(getCurrentThemeId(context, PREF_ENTRY_THEME, PREF_ENTRY_THEME_DEFAULT)); + context.setTheme(getCurrentThemeId(context, PREF_ENTRY_THEME, PREF_ENTRY_THEME_DEFAULT, prefs)); eventEntries.clear(); List events = new ArrayList<>(); for (IEventVisualizer eventProvider : eventProviders) { diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventWidgetService.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventWidgetService.java index 2303da00..ba744ab2 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventWidgetService.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/EventWidgetService.java @@ -1,10 +1,15 @@ package com.plusonelabs.calendar; +import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; import android.view.ContextThemeWrapper; import android.widget.RemoteViewsService; +import com.plusonelabs.calendar.prefs.UniquePreferencesFragment; + import static com.plusonelabs.calendar.Theme.getCurrentThemeId; import static com.plusonelabs.calendar.prefs.CalendarPreferences.PREF_ENTRY_THEME; import static com.plusonelabs.calendar.prefs.CalendarPreferences.PREF_ENTRY_THEME_DEFAULT; @@ -15,8 +20,11 @@ public class EventWidgetService extends RemoteViewsService { @Override public RemoteViewsFactory onGetViewFactory(Intent intent) { Context appContext = getApplicationContext(); - int currentThemeId = getCurrentThemeId(appContext, PREF_ENTRY_THEME, PREF_ENTRY_THEME_DEFAULT); + Bundle extras = intent.getExtras(); + int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); + SharedPreferences prefs = UniquePreferencesFragment.getPreferences(appContext, widgetId); + int currentThemeId = getCurrentThemeId(appContext, PREF_ENTRY_THEME, PREF_ENTRY_THEME_DEFAULT, prefs); ContextThemeWrapper context = new ContextThemeWrapper(appContext, currentThemeId); - return new EventRemoteViewsFactory(context); + return new EventRemoteViewsFactory(context, prefs); } } \ No newline at end of file diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/RemoteViewsUtil.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/RemoteViewsUtil.java index bfd7ac81..d429ffe6 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/RemoteViewsUtil.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/RemoteViewsUtil.java @@ -27,12 +27,12 @@ private RemoteViewsUtil() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static void setPadding(Context context, RemoteViews rv, int viewId, int leftDimenId, - int topDimenId, int rightDimenId, int bottomDimenId) { + int topDimenId, int rightDimenId, int bottomDimenId, SharedPreferences prefs) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - int leftPadding = Math.round(getScaledValueInPixel(context, leftDimenId)); - int topPadding = Math.round(getScaledValueInPixel(context, topDimenId)); - int rightPadding = Math.round(getScaledValueInPixel(context, rightDimenId)); - int bottomPadding = Math.round(getScaledValueInPixel(context, bottomDimenId)); + int leftPadding = Math.round(getScaledValueInPixel(context, leftDimenId, prefs)); + int topPadding = Math.round(getScaledValueInPixel(context, topDimenId, prefs)); + int rightPadding = Math.round(getScaledValueInPixel(context, rightDimenId, prefs)); + int bottomPadding = Math.round(getScaledValueInPixel(context, bottomDimenId, prefs)); rv.setViewPadding(viewId, leftPadding, topPadding, rightPadding, bottomPadding); } } @@ -45,8 +45,8 @@ public static void setColorFilter(RemoteViews rv, int viewId, int color) { rv.setInt(viewId, METHOD_SET_COLOR_FILTER, color); } - public static void setTextSize(Context context, RemoteViews rv, int viewId, int dimenId) { - rv.setFloat(viewId, METHOD_SET_TEXT_SIZE, getScaledValue(context, dimenId)); + public static void setTextSize(Context context, RemoteViews rv, int viewId, int dimenId, SharedPreferences prefs) { + rv.setFloat(viewId, METHOD_SET_TEXT_SIZE, getScaledValue(context, dimenId, prefs)); } public static void setTextColorFromAttr(Context context, RemoteViews rv, int viewId, int colorAttrId) { @@ -63,17 +63,15 @@ public static void setBackgroundColor(RemoteViews rv, int viewId, int color) { } - private static float getScaledValueInPixel(Context context, int dimenId) { + private static float getScaledValueInPixel(Context context, int dimenId, SharedPreferences prefs) { float resValue = getDimension(context, dimenId); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); float prefTextScale = parseFloat(prefs.getString(PREF_TEXT_SIZE_SCALE, PREF_TEXT_SIZE_SCALE_DEFAULT)); return resValue * prefTextScale; } - private static float getScaledValue(Context context, int dimenId) { + private static float getScaledValue(Context context, int dimenId, SharedPreferences prefs) { float resValue = getDimension(context, dimenId); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); float density = context.getResources().getDisplayMetrics().density; float prefTextScale = parseFloat(prefs.getString(PREF_TEXT_SIZE_SCALE, PREF_TEXT_SIZE_SCALE_DEFAULT)); diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/Theme.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/Theme.java index 9d57b721..f021cd9a 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/Theme.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/Theme.java @@ -17,8 +17,7 @@ private Theme(int themeResId) { this.themeResId = themeResId; } - public static int getCurrentThemeId(Context context, String prefKey, String prefDefault) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + public static int getCurrentThemeId(Context context, String prefKey, String prefDefault, SharedPreferences prefs) { return Theme.valueOf(prefs.getString(prefKey, prefDefault)).themeResId; } diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/WidgetConfigurationActivity.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/WidgetConfigurationActivity.java index 73b658a1..f92fbbce 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/WidgetConfigurationActivity.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/WidgetConfigurationActivity.java @@ -6,6 +6,7 @@ import android.os.Build; import android.os.Bundle; import android.preference.PreferenceActivity; +import android.util.Log; import android.util.TypedValue; import android.view.View; import android.view.View.OnClickListener; @@ -19,22 +20,43 @@ public class WidgetConfigurationActivity extends PreferenceActivity { private static final String PREFERENCES_PACKAGE_NAME = "com.plusonelabs.calendar.prefs"; + public static final String WIDGET_INIT = "widgetInit"; - private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; + public int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; + private boolean init = true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - Bundle extras = getIntent().getExtras(); - if (extras != null) { - appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, - AppWidgetManager.INVALID_APPWIDGET_ID); + if (savedInstanceState != null) { + appWidgetId = savedInstanceState.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); + init = savedInstanceState.getBoolean(WidgetConfigurationActivity.WIDGET_INIT); + } else { + Bundle extras = getIntent().getExtras(); + if (extras != null) { + appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, + AppWidgetManager.INVALID_APPWIDGET_ID); + init = extras.getBoolean(WidgetConfigurationActivity.WIDGET_INIT, true); + } } - if (hasHeaders() && appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) { + if (init && appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID && hasHeaders()) { createAddButton(); } } + @Override + public Intent onBuildStartFragmentIntent (String fragmentName, Bundle args, int titleRes, int shortTitleRes) { + Intent intent = super.onBuildStartFragmentIntent(fragmentName, args, titleRes, shortTitleRes); + intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, this.appWidgetId); + return intent; + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + outState.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); + outState.putBoolean(WidgetConfigurationActivity.WIDGET_INIT, init); + } + private void createAddButton() { TypedValue value = new TypedValue(); getTheme().resolveAttribute(android.R.attr.dividerHorizontal, value, true); diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/calendar/CalendarEventProvider.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/calendar/CalendarEventProvider.java index c21642c3..d52b943e 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/calendar/CalendarEventProvider.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/calendar/CalendarEventProvider.java @@ -49,9 +49,11 @@ public class CalendarEventProvider { private static final String AND_BRACKET = " AND ("; private final Context context; + private final SharedPreferences prefs; - public CalendarEventProvider(Context context) { + public CalendarEventProvider(Context context, SharedPreferences prefs) { this.context = context; + this.prefs = prefs; } public List getEvents() { @@ -66,7 +68,6 @@ public List getEvents() { } private List createEventList(Cursor calendarCursor) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean fillAllDayEvents = prefs.getBoolean(PREF_FILL_ALL_DAY, PREF_FILL_ALL_DAY_DEFAULT); List eventList = new ArrayList<>(); for (int i = 0; i < calendarCursor.getCount(); i++) { @@ -164,7 +165,6 @@ private int getAsOpaque(int color) { } private Cursor createLoadedCursor() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int dateRange = Integer .valueOf(prefs.getString(PREF_EVENT_RANGE, PREF_EVENT_RANGE_DEFAULT)); long start = System.currentTimeMillis(); @@ -185,7 +185,6 @@ private String[] getProjection() { } private String createSelectionClause() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); Set activeCalenders = prefs.getStringSet(CalendarPreferences.PREF_ACTIVE_CALENDARS, new HashSet()); if (activeCalenders.isEmpty()) { diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/calendar/CalendarEventVisualizer.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/calendar/CalendarEventVisualizer.java index 521b46b3..acfc2f81 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/calendar/CalendarEventVisualizer.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/calendar/CalendarEventVisualizer.java @@ -13,6 +13,7 @@ import com.plusonelabs.calendar.IEventVisualizer; import com.plusonelabs.calendar.R; import com.plusonelabs.calendar.model.Event; +import com.plusonelabs.calendar.prefs.UniquePreferencesFragment; import org.joda.time.DateTime; @@ -55,10 +56,10 @@ public class CalendarEventVisualizer implements IEventVisualizer private final CalendarEventProvider calendarContentProvider; private final SharedPreferences prefs; - public CalendarEventVisualizer(Context context) { + public CalendarEventVisualizer(Context context, SharedPreferences prefs) { this.context = context; - calendarContentProvider = new CalendarEventProvider(context); - prefs = PreferenceManager.getDefaultSharedPreferences(context); + this.prefs = prefs; + calendarContentProvider = new CalendarEventProvider(context, prefs); } public RemoteViews getRemoteView(Event eventEntry) { @@ -79,7 +80,7 @@ private void setTitle(CalendarEvent event, RemoteViews rv) { title = context.getResources().getString(R.string.no_title); } rv.setTextViewText(R.id.event_entry_title, title); - setTextSize(context, rv, R.id.event_entry_title, R.dimen.event_entry_title); + setTextSize(context, rv, R.id.event_entry_title, R.dimen.event_entry_title, prefs); setTextColorFromAttr(context, rv, R.id.event_entry_title, R.attr.eventEntryTitle); setSingleLine(rv, R.id.event_entry_title, !prefs.getBoolean(PREF_MULTILINE_TITLE, PREF_MULTILINE_TITLE_DEFAULT)); @@ -99,7 +100,7 @@ private void setEventDetails(CalendarEvent event, RemoteViews rv) { } rv.setViewVisibility(R.id.event_entry_details, View.VISIBLE); rv.setTextViewText(R.id.event_entry_details, eventDetails); - setTextSize(context, rv, R.id.event_entry_details, R.dimen.event_entry_details); + setTextSize(context, rv, R.id.event_entry_details, R.dimen.event_entry_details, prefs); setTextColorFromAttr(context, rv, R.id.event_entry_details, R.attr.eventEntryDetails); } } @@ -118,7 +119,7 @@ private void setIndicator(RemoteViews rv, boolean showIndication, int viewId, in if (showIndication) { rv.setViewVisibility(viewId, View.VISIBLE); setImageFromAttr(context, rv, viewId, imageAttrId); - int themeId = getCurrentThemeId(context, PREF_ENTRY_THEME, PREF_ENTRY_THEME_DEFAULT); + int themeId = getCurrentThemeId(context, PREF_ENTRY_THEME, PREF_ENTRY_THEME_DEFAULT, prefs); int alpha = 255; if (themeId == R.style.Theme_Calendar_Dark || themeId == R.style.Theme_Calendar_Light) { alpha = 128; @@ -178,7 +179,6 @@ private String createTimeStringForEventEntry(CalendarEvent event) { } private String createTimeString(DateTime time) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String dateFormat = prefs.getString(PREF_DATE_FORMAT, PREF_DATE_FORMAT_DEFAULT); if (DateUtil.hasAmPmClock(Locale.getDefault()) && dateFormat.equals(AUTO) || dateFormat.equals(TWELVE)) { diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/AppearancePreferencesFragment.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/AppearancePreferencesFragment.java index 457fae43..49c395da 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/AppearancePreferencesFragment.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/AppearancePreferencesFragment.java @@ -1,16 +1,16 @@ package com.plusonelabs.calendar.prefs; +import android.appwidget.AppWidgetManager; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.Preference; -import android.preference.PreferenceFragment; import android.preference.PreferenceScreen; import android.widget.Toast; import com.plusonelabs.calendar.EventAppWidgetProvider; import com.plusonelabs.calendar.R; -public class AppearancePreferencesFragment extends PreferenceFragment { +public class AppearancePreferencesFragment extends UniquePreferencesFragment { private static final String BACKGROUND_COLOR_DIALOG = "backgroundColorDialog"; @@ -32,8 +32,11 @@ public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preferen } } if (preference.getKey().equals(CalendarPreferences.PREF_BACKGROUND_COLOR)) { - new BackgroundTransparencyDialog().show(getFragmentManager(), - BACKGROUND_COLOR_DIALOG); + BackgroundTransparencyDialog btd = new BackgroundTransparencyDialog(); + Bundle args = new Bundle(); + args.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, this.appWidgetId); + btd.setArguments(args); + btd.show(getFragmentManager(), BACKGROUND_COLOR_DIALOG); } return super.onPreferenceTreeClick(preferenceScreen, preference); } diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/BackgroundTransparencyDialog.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/BackgroundTransparencyDialog.java index c1f2e52c..58b390fb 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/BackgroundTransparencyDialog.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/BackgroundTransparencyDialog.java @@ -3,12 +3,14 @@ import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; +import android.appwidget.AppWidgetManager; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.preference.PreferenceManager; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -23,6 +25,14 @@ public class BackgroundTransparencyDialog extends DialogFragment { private ColorPicker picker; + private int widgetId = AppWidgetManager.INVALID_APPWIDGET_ID; + + @Override + public void setArguments(Bundle args) { + if (args != null) { + widgetId = args.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); + } + } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { @@ -31,27 +41,24 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { picker = (ColorPicker) layout.findViewById(R.id.background_color_picker); picker.addSVBar((SVBar) layout.findViewById(R.id.background_color_svbar)); picker.addOpacityBar((OpacityBar) layout.findViewById(R.id.background_color_opacitybar)); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); + SharedPreferences prefs = UniquePreferencesFragment.getPreferences(getActivity().getApplicationContext(), widgetId); int color = prefs.getInt(PREF_BACKGROUND_COLOR, PREF_BACKGROUND_COLOR_DEFAULT); picker.setColor(color); picker.setOldCenterColor(color); - return createDialog(layout); + return createDialog(layout, prefs); } - - private Dialog createDialog(View layout) { + private Dialog createDialog(View layout, final SharedPreferences prefs) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.appearance_background_color_title); builder.setView(layout); builder.setPositiveButton(android.R.string.ok, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { - SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(getActivity()); Editor editor = prefs.edit(); editor.putInt(PREF_BACKGROUND_COLOR, picker.getColor()); editor.commit(); - } + } }); return builder.create(); } diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/CalendarPreferencesFragment.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/CalendarPreferencesFragment.java index 2c906c30..44b60a76 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/CalendarPreferencesFragment.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/CalendarPreferencesFragment.java @@ -10,7 +10,6 @@ import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.Preference; -import android.preference.PreferenceFragment; import android.preference.PreferenceScreen; import android.provider.CalendarContract.Calendars; @@ -22,7 +21,7 @@ import static com.plusonelabs.calendar.prefs.CalendarPreferences.PREF_ACTIVE_CALENDARS; -public class CalendarPreferencesFragment extends PreferenceFragment { +public class CalendarPreferencesFragment extends UniquePreferencesFragment { private static final String CALENDAR_ID = "calendarId"; private static final String[] PROJECTION = new String[] { Calendars._ID, @@ -35,8 +34,7 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences_calendars); SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); - initialActiveCalendars = prefs.getStringSet(PREF_ACTIVE_CALENDARS, - null); + initialActiveCalendars = prefs.getStringSet(PREF_ACTIVE_CALENDARS, null); populatePreferenceScreen(initialActiveCalendars); } diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/EventDetailsPreferencesFragment.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/EventDetailsPreferencesFragment.java index 5fbbbb98..b25ddb33 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/EventDetailsPreferencesFragment.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/EventDetailsPreferencesFragment.java @@ -1,12 +1,11 @@ package com.plusonelabs.calendar.prefs; import android.os.Bundle; -import android.preference.PreferenceFragment; import com.plusonelabs.calendar.EventAppWidgetProvider; import com.plusonelabs.calendar.R; -public class EventDetailsPreferencesFragment extends PreferenceFragment { +public class EventDetailsPreferencesFragment extends UniquePreferencesFragment { @Override public void onCreate(Bundle savedInstanceState) { diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/FeedbackPreferencesFragment.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/FeedbackPreferencesFragment.java index 58912fb1..1b9023af 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/FeedbackPreferencesFragment.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/FeedbackPreferencesFragment.java @@ -1,11 +1,10 @@ package com.plusonelabs.calendar.prefs; import android.os.Bundle; -import android.preference.PreferenceFragment; import com.plusonelabs.calendar.R; -public class FeedbackPreferencesFragment extends PreferenceFragment { +public class FeedbackPreferencesFragment extends UniquePreferencesFragment { @Override public void onCreate(Bundle savedInstanceState) { diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/UniquePreferencesFragment.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/UniquePreferencesFragment.java new file mode 100644 index 00000000..d1964a6c --- /dev/null +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/UniquePreferencesFragment.java @@ -0,0 +1,37 @@ +package com.plusonelabs.calendar.prefs; + +import android.appwidget.AppWidgetManager; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; +import android.util.Log; + +public abstract class UniquePreferencesFragment extends PreferenceFragment { + + protected int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; + private static final String PACKAGE_NAME = "com.plusonelabs.calendar.prefs"; + + @Override + public void onCreate(Bundle savedInstanceState) { + if (savedInstanceState != null) { + appWidgetId = savedInstanceState.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); + } else { + Bundle extras = getActivity().getIntent().getExtras(); + if (extras != null) { + appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, + AppWidgetManager.INVALID_APPWIDGET_ID); + } + } + + super.onCreate(savedInstanceState); + + PreferenceManager manager = this.getPreferenceManager(); + manager.setSharedPreferencesName(PACKAGE_NAME + appWidgetId); + } + + public static SharedPreferences getPreferences(Context context, int widgetId) { + return context.getSharedPreferences(PACKAGE_NAME + widgetId, Context.MODE_PRIVATE); + } +}