Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Android provide an API for determining weekend days that is locale-aware?

I want to provide some styling to an app that is aware of work days and weekends, but I need it to be locale-aware. I'm aware of Calendar#getFirstDayOfWeek, but this only returns the first day of the week, not the first day of the work week.

For example, with English(Canada) Calendar#getFirstDayOfWeek returns Calendar.SUNDAY, which is the first day of our week. Our work week starts on Calendar.MONDAY, and our weekend days are Calendar.SATURDAY and Calendar.SUNDAY.

In English(United Kingdom), Calendar#getFirstDayOfWeek returns Calendar.MONDAY, and the first day of their work-week is Monday. Their weekend days are Calendar.SATURDAY and Calendar.SUNDAY.

Things get tricky for locales like Hebrew. The first day of their week is Calendar.SUNDAY, but instead of the first day of their work week being Calendar.MONDAY, it's Calendar.SUNDAY. Their weekend days are Calendar.FRIDAY and Calendar.SATURDAY. Egypt is the same.

Iran only has one weekend day, Calendar.FRIDAY.

Is there any method in Android to determine weekend days? We have some logic in place that covers most Western cases, but it's fragile, and a locale-aware platform API would be ideal, but I'm not aware of one.

Please note, I am not asking for a recommendation on some "best" library or anything of the sort. I'm asking if any such locale data exists on the Android platform at all, and if so, what API calls exist to access this data.

like image 774
JymmyZ Avatar asked Nov 09 '22 05:11

JymmyZ


1 Answers

There are newly released snapshot and fence APIs in Awareness API for your use-case for a locale-aware API for time.

Please take a look at Awareness.SnapshotApi.getTimeIntervals which returns a TimeIntervalsResult. You can call getTimeIntervals() method to return an object that has information about the properties of the current time at current locale (specifically the weekend and holidays that you have asked for).

For example, the following code snippet checks if it is a weekend at the device's locale at the time of the snapshot api call.

Awareness.SnapshotApi.getTimeIntervalsmGoogleApiClient)
    .setResultCallback(new ResultCallback<TimeIntervalsResult>() {
        @Override
        public void onResult(@NonNull TimeIntervalsResult result) {
            if (!result.getStatus().isSuccess()) {
                Log.e(TAG, "Could not get the time intervals");
                return;
            }
            if (result.getTimeIntervals().hasTimeInterval(
                TimeFence.TIME_INTERVAL_WEEKEND)) {
                // do your custom action, this means it is weekend at the 
                // device locale at this time
            }
        }

If you'd like your app to get a callback on the next weekend or holiday at the device locale, you can use the new TimeFence Api inTimeInterval(). For example, borrowing the code snippet from the developer docs,

// Create TimeFence
AwarenessFence weekendHolidayFence = Awareness.or(
    TimeFence.inTimeInterval(TimeFence.TIME_INTERVAL_WEEKEND),
    TimeFence.inTimeInterval(TimeFence.TIME_INTERVAL_HOLIDAY))

Awareness.FenceApi.updateFences(
    mGoogleApiClient,
    new FenceUpdateRequest.Builder()
        .addFence("fenceKey", weekendHolidayFence, myPendingIntent)
        .build())
    .setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            if (status.isSuccess()) {
                Log.i(TAG, "Fence was successfully registered.");
            } else {
                Log.e(TAG, "Fence could not be registered: " + status);
            }
        }
    });
like image 122
MRC Avatar answered Nov 14 '22 23:11

MRC