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

Commit d86200e7 authored by Matías Hernández's avatar Matías Hernández Committed by Android (Google) Code Review
Browse files

Merge "Add helper method to generate alternative version of schedule-time rule...

Merge "Add helper method to generate alternative version of schedule-time rule trigger descriptions" into main
parents c146239d 76922f88
Loading
Loading
Loading
Loading
+32 −8
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@ package android.service.notification;
import android.annotation.FlaggedApi;
import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.StringRes;
import android.app.AutomaticZenRule;
import android.app.AutomaticZenRule;
import android.app.Flags;
import android.app.Flags;
import android.content.Context;
import android.content.Context;
@@ -122,7 +123,7 @@ public final class SystemZenRules {
    public static String getTriggerDescriptionForScheduleTime(Context context,
    public static String getTriggerDescriptionForScheduleTime(Context context,
            @NonNull ScheduleInfo schedule) {
            @NonNull ScheduleInfo schedule) {
        final StringBuilder sb = new StringBuilder();
        final StringBuilder sb = new StringBuilder();
        String daysSummary = getShortDaysSummary(context, schedule);
        String daysSummary = getDaysOfWeekShort(context, schedule);
        if (daysSummary == null) {
        if (daysSummary == null) {
            // no use outputting times without dates
            // no use outputting times without dates
            return null;
            return null;
@@ -134,12 +135,36 @@ public final class SystemZenRules {
        return sb.toString();
        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
     * 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
    @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
        // Compute a list of days with contiguous days grouped together, for example: "Sun-Thu" or
        // "Sun-Mon,Wed,Fri"
        // "Sun-Mon,Wed,Fri"
        final int[] days = schedule.days;
        final int[] days = schedule.days;
@@ -197,19 +222,18 @@ public final class SystemZenRules {
                                context.getString(R.string.zen_mode_trigger_summary_divider_text));
                                context.getString(R.string.zen_mode_trigger_summary_divider_text));
                    }
                    }


                    SimpleDateFormat dayFormat = new SimpleDateFormat("EEE", getLocale(context));
                    if (startDay == lastSeenDay) {
                    if (startDay == lastSeenDay) {
                        // last group was only one day
                        // last group was only one day
                        cStart.set(Calendar.DAY_OF_WEEK, daysOfWeek[startDay]);
                        cStart.set(Calendar.DAY_OF_WEEK, daysOfWeek[startDay]);
                        sb.append(dayFormat.format(cStart.getTime()));
                        sb.append(dayOfWeekFormat.format(cStart.getTime()));
                    } else {
                    } else {
                        // last group was a contiguous group of days, so group them together
                        // last group was a contiguous group of days, so group them together
                        cStart.set(Calendar.DAY_OF_WEEK, daysOfWeek[startDay]);
                        cStart.set(Calendar.DAY_OF_WEEK, daysOfWeek[startDay]);
                        cEnd.set(Calendar.DAY_OF_WEEK, daysOfWeek[lastSeenDay]);
                        cEnd.set(Calendar.DAY_OF_WEEK, daysOfWeek[lastSeenDay]);
                        sb.append(context.getString(
                        sb.append(context.getString(
                                R.string.zen_mode_trigger_summary_range_symbol_combination,
                                rangeFormatResId,
                                dayFormat.format(cStart.getTime()),
                                dayOfWeekFormat.format(cStart.getTime()),
                                dayFormat.format(cEnd.getTime())));
                                dayOfWeekFormat.format(cEnd.getTime())));
                    }
                    }
                }
                }
            }
            }
+2 −0
Original line number Original line Diff line number Diff line
@@ -5348,6 +5348,8 @@
    <string name="zen_mode_trigger_summary_divider_text">,\u0020</string>
    <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 -->
    <!-- [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>
    <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 -->
    <!-- [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>
    <string name="zen_mode_trigger_event_calendar_any">Any calendar</string>


+1 −0
Original line number Original line 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_implicit_deactivated" />
  <java-symbol type="string" name="zen_mode_trigger_summary_divider_text" />
  <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_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="zen_mode_trigger_event_calendar_any" />


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


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


        assertThat(SystemZenRules.getShortDaysSummary(mContext, scheduleInfo))
        assertThat(SystemZenRules.getDaysOfWeekShort(mContext, scheduleInfo))
                .isEqualTo("Mon-Fri");
                .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
    @Test
    public void getTimeSummary_onlyTime() {
    public void getTimeSummary_onlyTime() {
        ScheduleInfo scheduleInfo = new ScheduleInfo();
        ScheduleInfo scheduleInfo = new ScheduleInfo();