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..5f959d62 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 @@ -96,11 +96,11 @@ private static float getDimension(Context context, int dimenId) { } } - public static void setSingleLine(RemoteViews rv, int viewId, boolean singleLine) { - rv.setBoolean(viewId, METHOD_SET_SINGLE_LINE, singleLine); + public static void setMultiline(RemoteViews rv, int viewId, boolean multiLine) { + rv.setBoolean(viewId, METHOD_SET_SINGLE_LINE, !multiLine); } - public static void setImageFromAttr(Context context, RemoteViews rv, int viewId, int attrResId) { + public static void setImageFromAttr(Context context, RemoteViews rv, int viewId, int attrResId) { TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(attrResId, outValue, true); setImage(rv, viewId, outValue.resourceId); 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 5bcd6724..5102cb54 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 @@ -4,7 +4,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; -import android.text.TextUtils; import android.view.View; import android.widget.RemoteViews; @@ -14,6 +13,7 @@ import com.plusonelabs.calendar.R; import com.plusonelabs.calendar.prefs.CalendarPreferences; import com.plusonelabs.calendar.widget.CalendarEntry; +import com.plusonelabs.calendar.widget.EventEntryLayout; import com.plusonelabs.calendar.widget.WidgetEntry; import org.joda.time.DateTime; @@ -25,7 +25,7 @@ import static com.plusonelabs.calendar.RemoteViewsUtil.setAlpha; import static com.plusonelabs.calendar.RemoteViewsUtil.setBackgroundColor; import static com.plusonelabs.calendar.RemoteViewsUtil.setImageFromAttr; -import static com.plusonelabs.calendar.RemoteViewsUtil.setSingleLine; +import static com.plusonelabs.calendar.RemoteViewsUtil.setMultiline; import static com.plusonelabs.calendar.RemoteViewsUtil.setTextColorFromAttr; import static com.plusonelabs.calendar.RemoteViewsUtil.setTextSize; import static com.plusonelabs.calendar.Theme.getCurrentThemeId; @@ -49,34 +49,23 @@ public CalendarEventVisualizer(Context context) { public RemoteViews getRemoteView(WidgetEntry eventEntry) { CalendarEntry event = (CalendarEntry) eventEntry; - RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.event_entry); + EventEntryLayout eventEntryLayout = CalendarPreferences.getEventEntryLayout(context); + RemoteViews rv = new RemoteViews(context.getPackageName(), eventEntryLayout.layoutId); rv.setOnClickFillInIntent(R.id.event_entry, createOnItemClickIntent(event.getEvent())); setTitle(event, rv); - setEventDetails(event, rv); + eventEntryLayout.visualizeEvent(context, event, rv); setAlarmActive(event, rv); setRecurring(event, rv); setColor(event, rv); return rv; } - private void setTitle(CalendarEntry event, RemoteViews rv) { + private void setTitle(CalendarEntry event, RemoteViews rv) { rv.setTextViewText(R.id.event_entry_title, event.getTitle(context)); setTextSize(context, rv, R.id.event_entry_title, R.dimen.event_entry_title); 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)); - } - - private void setEventDetails(CalendarEntry entry, RemoteViews rv) { - String eventDetails = entry.getEventDetails(context); - if (TextUtils.isEmpty(eventDetails)) { - rv.setViewVisibility(R.id.event_entry_details, View.GONE); - } else { - 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); - setTextColorFromAttr(context, rv, R.id.event_entry_details, R.attr.eventEntryDetails); - } + setMultiline(rv, R.id.event_entry_title, + prefs.getBoolean(PREF_MULTILINE_TITLE, PREF_MULTILINE_TITLE_DEFAULT)); } private void setAlarmActive(CalendarEntry entry, RemoteViews rv) { 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 9cedb6bb..bd51b6e3 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,5 +1,6 @@ package com.plusonelabs.calendar.prefs; +import android.content.SharedPreferences; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.Preference; @@ -13,7 +14,8 @@ import java.util.TimeZone; -public class AppearancePreferencesFragment extends PreferenceFragment { +public class AppearancePreferencesFragment extends PreferenceFragment + implements SharedPreferences.OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { @@ -25,8 +27,17 @@ public void onCreate(Bundle savedInstanceState) { public void onResume() { super.onResume(); showLockTimeZone(true); + showEventEntryLayout(); + getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); } + private void showEventEntryLayout() { + Preference preference = findPreference(CalendarPreferences.PREF_EVENT_ENTRY_LAYOUT); + if (preference != null) { + preference.setSummary(CalendarPreferences.getEventEntryLayout(getActivity()).summaryResId); + } + } + private void showLockTimeZone(boolean setAlso) { CheckBoxPreference preference = (CheckBoxPreference) findPreference(CalendarPreferences.PREF_LOCK_TIME_ZONE); if (preference != null) { @@ -78,7 +89,19 @@ public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preferen @Override public void onPause() { super.onPause(); + getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); EventAppWidgetProvider.updateEventList(getActivity()); EventAppWidgetProvider.updateAllWidgets(getActivity()); } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + switch (key) { + case CalendarPreferences.PREF_EVENT_ENTRY_LAYOUT: + showEventEntryLayout(); + break; + default: + break; + } + } } \ No newline at end of file diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/CalendarPreferences.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/CalendarPreferences.java index 9bddc229..9b7e8798 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/CalendarPreferences.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/prefs/CalendarPreferences.java @@ -9,6 +9,7 @@ import com.plusonelabs.calendar.DateUtil; import com.plusonelabs.calendar.EndedSomeTimeAgo; import com.plusonelabs.calendar.Theme; +import com.plusonelabs.calendar.widget.EventEntryLayout; import org.joda.time.DateTimeZone; import org.json.JSONException; @@ -57,6 +58,7 @@ public class CalendarPreferences { private static final boolean PREF_ABBREVIATE_DATES_DEFAULT = false; static final String PREF_LOCK_TIME_ZONE = "lockTimeZone"; private static final String PREF_LOCKED_TIME_ZONE_ID = "lockedTimeZoneId"; + static final String PREF_EVENT_ENTRY_LAYOUT = "eventEntryLayout"; private static volatile String lockedTimeZoneId = null; @@ -227,4 +229,13 @@ public static void setLockedTimeZoneId(Context context, String value) { public static boolean isTimeZoneLocked(Context context) { return !TextUtils.isEmpty(getLockedTimeZoneId(context)); } + + public static void setEventEntryLayout(Context context, EventEntryLayout value) { + setStringPreference(context, PREF_EVENT_ENTRY_LAYOUT, value.value); + } + + public static EventEntryLayout getEventEntryLayout(Context context) { + return EventEntryLayout.fromValue(PreferenceManager.getDefaultSharedPreferences(context).getString( + PREF_EVENT_ENTRY_LAYOUT, "")); + } } diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/CalendarEntry.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/CalendarEntry.java index 050722dd..856e3512 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/CalendarEntry.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/CalendarEntry.java @@ -18,7 +18,7 @@ public class CalendarEntry extends WidgetEntry { private static final String SPACE_ARROW = " →"; private static final String ARROW_SPACE = "→ "; private static final String EMPTY_STRING = ""; - private static final String SPACE_DASH_SPACE = " - "; + static final String SPACE_DASH_SPACE = " - "; private static final String SPACE_PIPE_SPACE = " | "; private DateTime endDate; @@ -90,20 +90,24 @@ public CalendarEvent getEvent() { return event; } - public String getEventDetails(Context context) { - if (spansOneFullDay() && !(isStartOfMultiDayEvent() - || isEndOfMultiDayEvent()) - || isAllDay() && CalendarPreferences.getFillAllDayEvents(context)) { - return ""; - } else { - String eventDetails = createTimeSpanString(context); - if (CalendarPreferences.getShowLocation(context) && getLocation() != null && !getLocation().isEmpty()) { - eventDetails += SPACE_PIPE_SPACE + getLocation(); - } - return eventDetails; - } + public String getEventDetails(Context context) { + if (hideEventDetails(context)) return ""; + String eventDetails = createTimeSpanString(context); + if (CalendarPreferences.getShowLocation(context) && getLocation() != null && !getLocation().isEmpty()) { + eventDetails += SPACE_PIPE_SPACE + getLocation(); + } + return eventDetails; } + String getEventTimeString(Context context) { + return hideEventDetails(context) ? "" : createTimeSpanString(context); + } + + private boolean hideEventDetails(Context context) { + return spansOneFullDay() && !(isStartOfMultiDayEvent() || isEndOfMultiDayEvent()) || + isAllDay() && CalendarPreferences.getFillAllDayEvents(context); + } + private String createTimeSpanString(Context context) { if (isAllDay() && !CalendarPreferences.getFillAllDayEvents(context)) { DateTime dateTime = getEvent().getEndDate().minusDays(1); diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/EventEntryLayout.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/EventEntryLayout.java new file mode 100644 index 00000000..9502bb8c --- /dev/null +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/EventEntryLayout.java @@ -0,0 +1,84 @@ +package com.plusonelabs.calendar.widget; + +import android.content.Context; +import android.support.annotation.LayoutRes; +import android.support.annotation.StringRes; +import android.text.TextUtils; +import android.view.View; +import android.widget.RemoteViews; + +import com.plusonelabs.calendar.R; +import com.plusonelabs.calendar.RemoteViewsUtil; +import com.plusonelabs.calendar.prefs.CalendarPreferences; + +import static com.plusonelabs.calendar.RemoteViewsUtil.setTextColorFromAttr; +import static com.plusonelabs.calendar.RemoteViewsUtil.setTextSize; + +/** + * @author yvolk@yurivolkov.com + */ +public enum EventEntryLayout { + DEFAULT(R.layout.event_entry, "DEFAULT", R.string.default_multiline_layout), + ONE_LINE(R.layout.event_entry_one_line, "ONE_LINE", R.string.single_line_layout) { + @Override + public void visualizeEvent(Context context, CalendarEntry entry, RemoteViews rv) { + setEventDate(context, entry, rv); + setEventTime(context, entry, rv); + } + }; + + @LayoutRes + public final int layoutId; + public final String value; + @StringRes + public final int summaryResId; + + EventEntryLayout(@LayoutRes int layoutId, String value, int summaryResId) { + this.layoutId = layoutId; + this.value = value; + this.summaryResId = summaryResId; + } + + public static EventEntryLayout fromValue(String value) { + EventEntryLayout layout = DEFAULT; + for (EventEntryLayout item : EventEntryLayout.values()) { + if (item.value.equals(value)) { + layout = item; + break; + } + } + return layout; + } + + public void visualizeEvent(Context context, CalendarEntry entry, RemoteViews rv) { + setEventDetails(context, entry, rv); + } + + protected void setEventDate(Context context, CalendarEntry entry, RemoteViews rv) { + if (CalendarPreferences.getShowDayHeaders(context)) { + rv.setViewVisibility(R.id.event_entry_date, View.GONE); + } else { + rv.setViewVisibility(R.id.event_entry_date, View.VISIBLE); + rv.setTextViewText(R.id.event_entry_date, entry.getDateString(context)); + } + } + + protected void setEventTime(Context context, CalendarEntry entry, RemoteViews rv) { + RemoteViewsUtil.setMultiline(rv, R.id.event_entry_time, CalendarPreferences.getShowEndTime(context)); + rv.setTextViewText(R.id.event_entry_time, entry.getEventTimeString(context).replace(CalendarEntry + .SPACE_DASH_SPACE, " ")); + } + + private void setEventDetails(Context context, CalendarEntry entry, RemoteViews rv) { + String eventDetails = entry.getEventDetails(context); + if (TextUtils.isEmpty(eventDetails)) { + rv.setViewVisibility(R.id.event_entry_details, View.GONE); + } else { + 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); + setTextColorFromAttr(context, rv, R.id.event_entry_details, R.attr.eventEntryDetails); + } + } + +} diff --git a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/WidgetEntry.java b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/WidgetEntry.java index 91eca781..06bfb1cd 100644 --- a/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/WidgetEntry.java +++ b/app/calendar-widget/src/main/java/com/plusonelabs/calendar/widget/WidgetEntry.java @@ -1,6 +1,12 @@ package com.plusonelabs.calendar.widget; +import android.content.Context; + +import com.plusonelabs.calendar.DateUtil; +import com.plusonelabs.calendar.R; + import org.joda.time.DateTime; +import org.joda.time.Days; public class WidgetEntry implements Comparable { @@ -23,6 +29,20 @@ public String toString() { return this.getClass().getSimpleName() + " [startDate=" + startDate + "]"; } + public CharSequence getDateString(Context context) { + Days days = Days.daysBetween(DateUtil.now().withTimeAtStartOfDay(), startDate.withTimeAtStartOfDay()); + switch (days.getDays()) { + case -1: + return context.getText(R.string.yesterday); + case 0: + return context.getText(R.string.today); + case 1: + return context.getText(R.string.tomorrow); + default: + return Integer.toString(days.getDays()); + } + } + @Override public int compareTo(WidgetEntry otherEvent) { if (getStartDate().isAfter(otherEvent.getStartDate())) { diff --git a/app/calendar-widget/src/main/res/layout/event_entry.xml b/app/calendar-widget/src/main/res/layout/event_entry.xml index 5a2fb3a2..d096620a 100644 --- a/app/calendar-widget/src/main/res/layout/event_entry.xml +++ b/app/calendar-widget/src/main/res/layout/event_entry.xml @@ -30,7 +30,8 @@ android:layout_alignWithParentIfMissing="true" android:layout_toLeftOf="@+id/event_entry_indicator_layout_ref" android:gravity="center_vertical" - tools:ignore="SelectableText" /> + tools:text="Event Entry Title. It may be long and not fit in one line" + tools:ignore="SelectableText"/> + tools:text="Event Entry Details, which could be quite long and span more than one line" + tools:ignore="SelectableText"/> + android:layout_alignTop="@id/event_entry_title"/> \ No newline at end of file diff --git a/app/calendar-widget/src/main/res/layout/event_entry_one_line.xml b/app/calendar-widget/src/main/res/layout/event_entry_one_line.xml new file mode 100644 index 00000000..fccdc7aa --- /dev/null +++ b/app/calendar-widget/src/main/res/layout/event_entry_one_line.xml @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/calendar-widget/src/main/res/values/arrays.xml b/app/calendar-widget/src/main/res/values/arrays.xml index 6218e6e2..92cfcd8e 100644 --- a/app/calendar-widget/src/main/res/values/arrays.xml +++ b/app/calendar-widget/src/main/res/values/arrays.xml @@ -85,4 +85,12 @@ TODAY YESTERDAY + + @string/default_multiline_layout + @string/single_line_layout + + + DEFAULT + ONE_LINE + diff --git a/app/calendar-widget/src/main/res/values/strings.xml b/app/calendar-widget/src/main/res/values/strings.xml index ee6a21db..3b848efb 100644 --- a/app/calendar-widget/src/main/res/values/strings.xml +++ b/app/calendar-widget/src/main/res/values/strings.xml @@ -2,6 +2,7 @@ Calendar Widget + Yesterday Today Tomorrow No upcoming events @@ -31,6 +32,9 @@ Medium Large Very large + Event entry layout + Default multiline layout + Single line layout Multiline title Spread event title over multiple lines Show time in am/pm or 24 hours format diff --git a/app/calendar-widget/src/main/res/values/styles.xml b/app/calendar-widget/src/main/res/values/styles.xml index 30ff754c..4ab3d39b 100644 --- a/app/calendar-widget/src/main/res/values/styles.xml +++ b/app/calendar-widget/src/main/res/values/styles.xml @@ -31,7 +31,7 @@ diff --git a/app/calendar-widget/src/main/res/xml/preferences_appearance.xml b/app/calendar-widget/src/main/res/xml/preferences_appearance.xml index 9dbf5696..7186412f 100644 --- a/app/calendar-widget/src/main/res/xml/preferences_appearance.xml +++ b/app/calendar-widget/src/main/res/xml/preferences_appearance.xml @@ -8,6 +8,14 @@ android:summary="@string/appearance_text_size_desc" android:title="@string/appearance_text_size_title" /> + +