Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 76922f88 authored by Matías Hernández's avatar Matías Hernández
Browse files

Add helper method to generate alternative version of schedule-time rule trigger descriptions

Bug: 370358575
Test: atest SystemZenRulesTest
Flag: EXEMPT adding minor util method
Change-Id: I5996cb0af215dcdd326a5283f9809459e52eabe5
parent f76196eb
Loading
Loading
Loading
Loading
+32 −8
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.service.notification;
import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.StringRes;
import android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.Context;
@@ -122,7 +123,7 @@ public final class SystemZenRules {
    public static String getTriggerDescriptionForScheduleTime(Context context,
            @NonNull ScheduleInfo schedule) {
        final StringBuilder sb = new StringBuilder();
        String daysSummary = getShortDaysSummary(context, schedule);
        String daysSummary = getDaysOfWeekShort(context, schedule);
        if (daysSummary == null) {
            // no use outputting times without dates
            return null;
@@ -134,12 +135,36 @@ public final class SystemZenRules {
        return sb.toString();
    }

    /**
     * Returns a short, ordered summarized list of the days on which this schedule applies, using
     * abbreviated week days, with adjacent days grouped together ("Sun-Wed" instead of
     * "Sun,Mon,Tue,Wed").
     */
    @Nullable
    public static String getDaysOfWeekShort(Context context, @NonNull ScheduleInfo schedule) {
        return getDaysSummary(context, R.string.zen_mode_trigger_summary_range_symbol_combination,
                new SimpleDateFormat("EEE", getLocale(context)), schedule);
    }

    /**
     * Returns a string representing the days on which this schedule applies, using full week days,
     * with adjacent days grouped together (e.g. "Sunday to Wednesday" instead of
     * "Sunday,Monday,Tuesday,Wednesday").
     */
    @Nullable
    public static String getDaysOfWeekFull(Context context, @NonNull ScheduleInfo schedule) {
        return getDaysSummary(context, R.string.zen_mode_trigger_summary_range_words,
                new SimpleDateFormat("EEEE", getLocale(context)), schedule);
    }

    /**
     * Returns an ordered summarized list of the days on which this schedule applies, with
     * adjacent days grouped together ("Sun-Wed" instead of "Sun,Mon,Tue,Wed").
     * adjacent days grouped together. The formatting of each individual day of week is done with
     * the provided {@link SimpleDateFormat}.
     */
    @Nullable
    public static String getShortDaysSummary(Context context, @NonNull ScheduleInfo schedule) {
    private static String getDaysSummary(Context context, @StringRes int rangeFormatResId,
            SimpleDateFormat dayOfWeekFormat, @NonNull ScheduleInfo schedule) {
        // Compute a list of days with contiguous days grouped together, for example: "Sun-Thu" or
        // "Sun-Mon,Wed,Fri"
        final int[] days = schedule.days;
@@ -197,19 +222,18 @@ public final class SystemZenRules {
                                context.getString(R.string.zen_mode_trigger_summary_divider_text));
                    }

                    SimpleDateFormat dayFormat = new SimpleDateFormat("EEE", getLocale(context));
                    if (startDay == lastSeenDay) {
                        // last group was only one day
                        cStart.set(Calendar.DAY_OF_WEEK, daysOfWeek[startDay]);
                        sb.append(dayFormat.format(cStart.getTime()));
                        sb.append(dayOfWeekFormat.format(cStart.getTime()));
                    } else {
                        // last group was a contiguous group of days, so group them together
                        cStart.set(Calendar.DAY_OF_WEEK, daysOfWeek[startDay]);
                        cEnd.set(Calendar.DAY_OF_WEEK, daysOfWeek[lastSeenDay]);
                        sb.append(context.getString(
                                R.string.zen_mode_trigger_summary_range_symbol_combination,
                                dayFormat.format(cStart.getTime()),
                                dayFormat.format(cEnd.getTime())));
                                rangeFormatResId,
                                dayOfWeekFormat.format(cStart.getTime()),
                                dayOfWeekFormat.format(cEnd.getTime())));
                    }
                }
            }
+2 −0
Original line number Diff line number Diff line
@@ -5348,6 +5348,8 @@
    <string name="zen_mode_trigger_summary_divider_text">,\u0020</string>
    <!-- [CHAR LIMIT=40] General template for a symbolic start - end range in a text summary, used for the trigger description of a Zen mode -->
    <string name="zen_mode_trigger_summary_range_symbol_combination"><xliff:g id="start" example="Sun">%1$s</xliff:g> - <xliff:g id="end" example="Thu">%2$s</xliff:g></string>
    <!-- [CHAR LIMIT=40] General template for a start - end range in a text summary, used for the trigger description of a Zen mode -->
    <string name="zen_mode_trigger_summary_range_words"><xliff:g id="start" example="Sunday">%1$s</xliff:g> to <xliff:g id="end" example="Thursday">%2$s</xliff:g></string>
    <!-- [CHAR LIMIT=40] Event-based rule calendar option value for any calendar, used for the trigger description of a Zen mode -->
    <string name="zen_mode_trigger_event_calendar_any">Any calendar</string>

+1 −0
Original line number Diff line number Diff line
@@ -2657,6 +2657,7 @@
  <java-symbol type="string" name="zen_mode_implicit_deactivated" />
  <java-symbol type="string" name="zen_mode_trigger_summary_divider_text" />
  <java-symbol type="string" name="zen_mode_trigger_summary_range_symbol_combination" />
  <java-symbol type="string" name="zen_mode_trigger_summary_range_words" />
  <java-symbol type="string" name="zen_mode_trigger_event_calendar_any" />

  <java-symbol type="string" name="display_rotation_camera_compat_toast_after_rotation" />
+14 −2
Original line number Diff line number Diff line
@@ -209,17 +209,29 @@ public class SystemZenRulesTest extends UiServiceTestCase {
    }

    @Test
    public void getShortDaysSummary_onlyDays() {
    public void getDaysOfWeekShort_summarizesDays() {
        ScheduleInfo scheduleInfo = new ScheduleInfo();
        scheduleInfo.startHour = 10;
        scheduleInfo.endHour = 16;
        scheduleInfo.days = new int[] {Calendar.MONDAY, Calendar.TUESDAY,
                Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY};

        assertThat(SystemZenRules.getShortDaysSummary(mContext, scheduleInfo))
        assertThat(SystemZenRules.getDaysOfWeekShort(mContext, scheduleInfo))
                .isEqualTo("Mon-Fri");
    }

    @Test
    public void getDaysOfWeekFull_summarizesDays() {
        ScheduleInfo scheduleInfo = new ScheduleInfo();
        scheduleInfo.startHour = 10;
        scheduleInfo.endHour = 16;
        scheduleInfo.days = new int[] {Calendar.MONDAY, Calendar.TUESDAY,
                Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY};

        assertThat(SystemZenRules.getDaysOfWeekFull(mContext, scheduleInfo))
                .isEqualTo("Monday to Friday");
    }

    @Test
    public void getTimeSummary_onlyTime() {
        ScheduleInfo scheduleInfo = new ScheduleInfo();