{{announcement.body}}
{{announcement.title}}

Time Awareness: A Journey Through HMS Awareness (Part 4)

DZone 's Guide to

Time Awareness: A Journey Through HMS Awareness (Part 4)

Time awareness is used to obtain holiday information in most regions and sunrise and sunset time of all cities around the world.

· Integration Zone ·
Free Resource

Introduction

Time awareness is used to obtain holiday information in most regions and sunrise and sunset time of all cities around the world.

Barrier API

This API is used to set a timer or a barrier based on the target time. For example, one hour before and after the sunrise can be set as a sunrise barrier.

Capture API

We can use the  Capture  API  to obtains the current local time or time of a specified location, such as working day, weekend, holiday, morning, afternoon, evening, or late at night.

For calling Time Awareness capability we have to assign the given permissions in the manifest file.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

To understand more about Barrier API and Capture API here we are discussing the following two classes:

  • TimeBarrier

  • TimeCategories

Let's go to the details.

TimeBarrier

This class features barriers to be triggered at a time or periodically based on-time status.

Constants

The below table gives the details of different constants that are commonly used in the TimeBarrier class.

Name

Constant Value

Description

SUNDAY_CODE

1

Sunday.

MONDAY_CODE

2

Monday

TUESDAY_CODE

3

Tuesday

WEDNESDAY_CODE

4

Wednesday.

THURSDAY_CODE

5

Thursday

FRIDAY_CODE

6

Friday.

SATURDAY_CODE

7

Saturday

SUNRISE_CODE

0

Constant indicating sunrise

SUNSET_CODE

1

Constant indicating sunset.

TIME_CATEGORY_MORNING

1

Morning time period, that is, 6:00–12:00.

TIME_CATEGORY_AFTERNOON

2

Afternoon time period, that is, 12:00–17:00.

TIME_CATEGORY_EVENING

3

Evening time period, that is, 17:00–21:00. 

TIME_CATEGORY_NIGHT

4

Nighttime period, that is, 21:00–06:00 (next day)

TIME_CATEGORY_HOLIDAY

5

Constant indicating holidays.

TIME_CATEGORY_NOT_HOLIDAY

8

Constant indicating non-holiday dates.

TIME_CATEGORY_WEEKDAY

6

Constant indicating workdays.

TIME_CATEGORY_WEEKEND

7

Constant indicating weekends.

Public Methods

There are five methods in the TimeBarrier class.

  1. duringPeriodOfDay

  2. duringPeriodOfWeek

  3. duringTimePeriod

  4. inSunriseOrSunsetPeriod

  5. inTimeCategory

Let's go to the details

1)  duringPeriodOfDay

It is the barrier of a specified period. After this barrier is added, when the time is in the specified period of a specified time zone, the barrier status is TRUE. Otherwise, the barrier status is FALSE.

Syntax:

public static AwarenessBarrier duringPeriodOfDay(TimeZone timeZone, long startTimeOfDay, long stopTimeOfDay)

Parameters:

timeZone

Time zone specified by you. If the time zone does not exist, the time zone of the area where the user is located is used.

startTimeOfDay

Start time of the barrier, in milliseconds. The value 0 indicates 00:00. The maximum value is the number of milliseconds of 24 hours.

stopTimeOfDay

Stop time of the barrier, in milliseconds. The value of stopTimeOfDay must be greater than or equal to the value of startTimeOfDay.


Java
 




xxxxxxxxxx
1
97


1
private void addbarrier(Context context) {
2
 
          
3
 
4
 
          
5
    //define the barrier
6
 
          
7
    long oneHourMilliSecond = 60 * 60 * 1000L;
8
 
          
9
 
10
 
          
11
    AwarenessBarrier duringPeriodOfDay = TimeBarrier.duringPeriodOfDay(TimeZone.getDefault(),
12
 
          
13
            11 * oneHourMilliSecond12 * oneHourMilliSecond);
14
 
          
15
 
16
 
          
17
    //define the label for the barrier and add the barrier
18
 
          
19
    String timeBarrierLabel = "time barrier";
20
 
          
21
 
22
 
          
23
    //add the barrier
24
 
          
25
    BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
26
 
          
27
    BarrierUpdateRequest request = builder.addBarrier(timeBarrierLabel, duringPeriodOfDay ,pendingIntent).build();
28
 
          
29
    Awareness.getBarrierClient(context).updateBarriers(request)
30
 
          
31
            .addOnSuccessListener(new OnSuccessListener<Void>() {
32
 
          
33
                @Override
34
 
          
35
                public void onSuccess(Void aVoid) {
36
 
          
37
                    Toast.makeText(getApplicationContext(), "add barrier success", Toast.LENGTH_SHORT).show();
38
 
          
39
                }
40
 
          
41
            })
42
 
          
43
            .addOnFailureListener(new OnFailureListener() {
44
 
          
45
                @Override
46
 
          
47
                public void onFailure(Exception e) {
48
 
          
49
                    Toast.makeText(getApplicationContext(), "add barrier failed", Toast.LENGTH_SHORT).show();
50
 
          
51
                    Log.e(TAG"add barrier failed", e);
52
 
          
53
                }
54
 
          
55
            });
56
 
          
57
}
58
 
          
59
 
60
 
          
61
// define the broadcast receiver to listen for the barrier event.
62
 
          
63
class TimeBarrierReceiver extends BroadcastReceiver {
64
 
          
65
    @Override
66
 
          
67
    public void onReceive(Context context, Intent intent) {
68
 
          
69
        BarrierStatus barrierStatus = BarrierStatus.extract(intent);
70
 
          
71
        String label = barrierStatus.getBarrierLabel();
72
 
          
73
        switch(barrierStatus.getPresentStatus()) {
74
 
          
75
            case BarrierStatus.TRUE:
76
 
          
77
                time_info_barrier.setText("The time is in the specified time period of a specified time zone");
78
 
          
79
                break;
80
 
          
81
            case BarrierStatus.FALSE:
82
 
          
83
                time_info_barrier.setText("It's not that time");
84
 
          
85
                break;
86
 
          
87
            case BarrierStatus.UNKNOWN:
88
 
          
89
                Log.i(TAG, label + " status:unknown");
90
 
          
91
                break;
92
 
          
93
        }
94
 
          
95
    }
96
 
          
97
}



2) duringPeriodOfWeek

It is the barrier for a specific day of a week. After this barrier is added, when the time is in the specified period on a day of a week in a specified time zone, the barrier status is TRUE. Otherwise, the barrier status is FALSE.

Syntax

public static AwarenessBarrier duringPeriodOfWeek(int dayOfWeek, TimeZone timeZone, long startTimeOfSpecifiedDay, long stopTimeOfSpecifiedDay)

Parameters

dayOfWeek

Day of a week. The options are as follows: 

SUNDAY_CODE, MONDAY_CODE, TUESDAY_CODE, WEDNESDAY_CODE, THURSDAY_CODE, FRIDAY_CODE, and SATURDAY_CODE.

timeZone

Time zone specified by you. If the time zone does not exist, the time zone of the area where the user is located is used.

startTimeOfSpecifiedDay

Start time of the barrier, in milliseconds. The value 0 indicates 00:00. The maximum value is the number of milliseconds of 24 hours.

stopTimeOfSpecifiedDay

Stop time of the barrier, in milliseconds. The value of stopTimeOfSpecifiedDay must be greater than or equal to the value of startTimeOfSpecifiedDay.

Java
 




xxxxxxxxxx
1
89


1
private void addbarrier(Context context) {
2
 
          
3
    //define the barrier
4
    long oneHourMilliSecond = 60 * 60 * 1000L;
5
    AwarenessBarrier duringPeriodOfWeek = TimeBarrier.duringPeriodOfWeek(TimeBarrier.FRIDAY_CODE,TimeZone.getDefault(),
6
 
          
7
            11 * oneHourMilliSecond12 * oneHourMilliSecond);
8
 
          
9
    //define the label for the barrier and add the barrier
10
 
          
11
    String timeBarrierLabel = "time barrier";
12
 
          
13
 
14
 
          
15
    //add the barrier
16
 
          
17
    BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
18
 
          
19
    BarrierUpdateRequest request = builder.addBarrier(timeBarrierLabel, duringPeriodOfWeek ,pendingIntent).build();
20
 
          
21
    Awareness.getBarrierClient(context).updateBarriers(request)
22
 
          
23
            .addOnSuccessListener(new OnSuccessListener<Void>() {
24
 
          
25
                @Override
26
 
          
27
                public void onSuccess(Void aVoid) {
28
 
          
29
                    Toast.makeText(getApplicationContext(), "add barrier success", Toast.LENGTH_SHORT).show();
30
 
          
31
                }
32
 
          
33
            })
34
 
          
35
            .addOnFailureListener(new OnFailureListener() {
36
 
          
37
                @Override
38
 
          
39
                public void onFailure(Exception e) {
40
 
          
41
                    Toast.makeText(getApplicationContext(), "add barrier failed", Toast.LENGTH_SHORT).show();
42
 
          
43
                    Log.e(TAG"add barrier failed", e);
44
 
          
45
                }
46
 
          
47
            });
48
 
          
49
}
50
 
          
51
 
52
 
          
53
// define the broadcast receiver to listen for the barrier event.
54
 
          
55
class TimeBarrierReceiver extends BroadcastReceiver {
56
 
          
57
    @Override
58
 
          
59
    public void onReceive(Context context, Intent intent) {
60
 
          
61
        BarrierStatus barrierStatus = BarrierStatus.extract(intent);
62
 
          
63
        String label = barrierStatus.getBarrierLabel();
64
 
          
65
        switch(barrierStatus.getPresentStatus()) {
66
 
          
67
            case BarrierStatus.TRUE:
68
 
          
69
                time_info_barrier.setText("It is specified time period on Friday");
70
 
          
71
                break;
72
 
          
73
            case BarrierStatus.FALSE:
74
 
          
75
                time_info_barrier.setText("It's not that time");
76
 
          
77
                break;
78
 
          
79
            case BarrierStatus.UNKNOWN:
80
 
          
81
                Log.i(TAG, label + " status:unknown");
82
 
          
83
                break;
84
 
          
85
        }
86
 
          
87
    }
88
 
          
89
}



3) duringTimePeriod

It is the period barrier. After this barrier is added, when the time is in the specified period, the barrier status is TRUE. Otherwise, the barrier status is FALSE.

Syntax

public static AwarenessBarrier duringTimePeriod(long startTimeStamp, long stopTimeStamp)

Parameters

startTimeStamp

Start timestamp. The value must be greater than or equal to 0.

stopTimeStamp

Stop timestamp. The value of stopTimeStamp must be greater than or equal to that of startTimeStamp.


Java
 




xxxxxxxxxx
1
95


 
1
private void addbarrier(Context context) {
2
 
          
3
 
4
 
          
5
    //define the barrier
6
 
          
7
 
8
 
          
9
    AwarenessBarrier duringTimePeriod = TimeBarrier.duringTimePeriod(
10
 
          
11
            System.currentTimeMillis(),System.currentTimeMillis()+10*1000);
12
 
          
13
 
14
 
          
15
    //define the label for the barrier and add the barrier
16
 
          
17
    String timeBarrierLabel = "time barrier";
18
 
          
19
 
20
 
          
21
    //add the barrier
22
 
          
23
    BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
24
 
          
25
    BarrierUpdateRequest request = builder.addBarrier(timeBarrierLabel, duringTimePeriod ,pendingIntent).build();
26
 
          
27
    Awareness.getBarrierClient(context).updateBarriers(request)
28
 
          
29
            .addOnSuccessListener(new OnSuccessListener<Void>() {
30
 
          
31
                @Override
32
 
          
33
                public void onSuccess(Void aVoid) {
34
 
          
35
                    Toast.makeText(getApplicationContext(), "add barrier success", Toast.LENGTH_SHORT).show();
36
 
          
37
                }
38
 
          
39
            })
40
 
          
41
            .addOnFailureListener(new OnFailureListener() {
42
 
          
43
                @Override
44
 
          
45
                public void onFailure(Exception e) {
46
 
          
47
                    Toast.makeText(getApplicationContext(), "add barrier failed", Toast.LENGTH_SHORT).show();
48
 
          
49
                    Log.e(TAG, "add barrier failed", e);
50
 
          
51
                }
52
 
          
53
            });
54
 
          
55
}
56
 
          
57
 
58
 
          
59
// define the broadcast receiver to listen for the barrier event.
60
 
          
61
class TimeBarrierReceiver extends BroadcastReceiver {
62
 
          
63
    @Override
64
 
          
65
    public void onReceive(Context context, Intent intent) {
66
 
          
67
        BarrierStatus barrierStatus = BarrierStatus.extract(intent);
68
 
          
69
        String label = barrierStatus.getBarrierLabel();
70
 
          
71
        switch(barrierStatus.getPresentStatus()) {
72
 
          
73
            case BarrierStatus.TRUE:
74
 
          
75
                time_info_barrier.setText("The time is in the specified time period");
76
 
          
77
                break;
78
 
          
79
            case BarrierStatus.FALSE:
80
 
          
81
                time_info_barrier.setText("It's not that time");
82
 
          
83
                break;
84
 
          
85
            case BarrierStatus.UNKNOWN:
86
 
          
87
                Log.i(TAG, label + " status:unknown");
88
 
          
89
                break;
90
 
          
91
        }
92
 
          
93
    }
94
 
          
95
}



4) inSunriseOrSunsetPeriod

It is the Sunrise and sunset barrier. After this barrier is added, when the time is in the specified period using the sunrise or sunset time as the benchmark, the barrier status is TRUE. Otherwise, the barrier status is FALSE.

Syntax

public static AwarenessBarrier inSunriseOrSunsetPeriod(int timeInstant, long startTimeOffset, long stopTimeOffset)

Parameters

timeInstant

Constant indicating sunrise or sunset.


SUNRISE_CODE

SUNSET_CODE

startTimeOffset Start time offset of the time barrier. The unit is millisecond. The value range is milliseconds from –24 hours to +24 hours. The value must be less than the value of stopTimeOffset.
stopTimeOffset

Stop time offset of the time barrier. The unit is millisecond. The value range is milliseconds from –24 hours to +24 hours. The value must be greater than the value of startTimeOffset.


Java
 




xxxxxxxxxx
1
99


1
private void addbarrier(Context context) {
2
 
          
3
 
4
 
          
5
    //define the barrier
6
 
          
7
 
8
 
          
9
    long oneHourMilliSecond = 60 * 60 * 1000L;
10
 
          
11
 
12
 
          
13
    AwarenessBarrier inSunrisePeriod = TimeBarrier.inSunriseOrSunsetPeriod(TimeBarrier.SUNRISE_CODE,
14
 
          
15
            5* oneHourMilliSecond, 7 * oneHourMilliSecond);
16
 
          
17
 
18
 
          
19
    //define the label for the barrier and add the barrier
20
 
          
21
    String timeBarrierLabel = "time barrier";
22
 
          
23
 
24
 
          
25
    //add the barrier
26
 
          
27
    BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
28
 
          
29
    BarrierUpdateRequest request = builder.addBarrier(timeBarrierLabel, inSunrisePeriod ,pendingIntent).build();
30
 
          
31
    Awareness.getBarrierClient(context).updateBarriers(request)
32
 
          
33
            .addOnSuccessListener(new OnSuccessListener<Void>() {
34
 
          
35
                @Override
36
 
          
37
                public void onSuccess(Void aVoid) {
38
 
          
39
                    Toast.makeText(getApplicationContext(), "add barrier success", Toast.LENGTH_SHORT).show();
40
 
          
41
                }
42
 
          
43
            })
44
 
          
45
            .addOnFailureListener(new OnFailureListener() {
46
 
          
47
                @Override
48
 
          
49
                public void onFailure(Exception e) {
50
 
          
51
                    Toast.makeText(getApplicationContext(), "add barrier failed", Toast.LENGTH_SHORT).show();
52
 
          
53
                    Log.e(TAG, "add barrier failed", e);
54
 
          
55
                }
56
 
          
57
            });
58
 
          
59
}
60
 
          
61
 
62
 
          
63
// define the broadcast receiver to listen for the barrier event.
64
 
          
65
class TimeBarrierReceiver extends BroadcastReceiver {
66
 
          
67
    @Override
68
 
          
69
    public void onReceive(Context context, Intent intent) {
70
 
          
71
        BarrierStatus barrierStatus = BarrierStatus.extract(intent);
72
 
          
73
        String label = barrierStatus.getBarrierLabel();
74
 
          
75
        switch(barrierStatus.getPresentStatus()) {
76
 
          
77
            case BarrierStatus.TRUE:
78
 
          
79
                time_info_barrier.setText("The specified time is Sunrise ");
80
 
          
81
                break;
82
 
          
83
            case BarrierStatus.FALSE:
84
 
          
85
                time_info_barrier.setText("IThe specified time is not Sunrise");
86
 
          
87
                break;
88
 
          
89
            case BarrierStatus.UNKNOWN:
90
 
          
91
                Log.i(TAG, label + " status:unknown");
92
 
          
93
                break;
94
 
          
95
        }
96
 
          
97
    }
98
 
          
99
}



5) inTimeCategory

It is the time status barrier. After this barrier is added, when the time is in the specified time state, the barrier status is TRUE. Otherwise, the barrier status is FALSE.

Syntax

public static AwarenessBarrier inTimeCategory(int timeCategory)

Parameter

timeCategory

Time status constant. The options are as follows:

TIME_CATEGORY_MORNING, TIME_CATEGORY_AFTERNOON, TIME_CATEGORY_EVENING, TIME_CATEGORY_NIGHT, TIME_CATEGORY_WEEKDAY, TIME_CATEGORY_WEEKEND,  TIME_CATEGORY_HOLIDAY,andTIME_CATEGORY_NOT_HOLIDAY (non-holiday date).


Java
 




xxxxxxxxxx
1
95


1
private void addbarrier(Context context) {
2
 
          
3
 
4
 
          
5
    //define the barrier
6
 
          
7
 
8
 
          
9
    AwarenessBarrier inTimeCategory= TimeBarrier.inTimeCategory(TimeBarrier.TIME_CATEGORY_MORNING);
10
 
          
11
 
12
 
          
13
 
14
 
          
15
    //define the label for the barrier and add the barrier
16
 
          
17
    String timeBarrierLabel = "time barrier";
18
 
          
19
 
20
 
          
21
    //add the barrier
22
 
          
23
    BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
24
 
          
25
    BarrierUpdateRequest request = builder.addBarrier(timeBarrierLabel, inTimeCategory,pendingIntent).build();
26
 
          
27
    Awareness.getBarrierClient(context).updateBarriers(request)
28
 
          
29
            .addOnSuccessListener(new OnSuccessListener<Void>() {
30
 
          
31
                @Override
32
 
          
33
                public void onSuccess(Void aVoid) {
34
 
          
35
                    Toast.makeText(getApplicationContext(), "add barrier success", Toast.LENGTH_SHORT).show();
36
 
          
37
                }
38
 
          
39
            })
40
 
          
41
            .addOnFailureListener(new OnFailureListener() {
42
 
          
43
                @Override
44
 
          
45
                public void onFailure(Exception e) {
46
 
          
47
                    Toast.makeText(getApplicationContext(), "add barrier failed", Toast.LENGTH_SHORT).show();
48
 
          
49
                    Log.e(TAG, "add barrier failed", e);
50
 
          
51
                }
52
 
          
53
            });
54
 
          
55
}
56
 
          
57
 
58
 
          
59
// define the broadcast receiver to listen for the barrier event.
60
 
          
61
class TimeBarrierReceiver extends BroadcastReceiver {
62
 
          
63
    @Override
64
 
          
65
    public void onReceive(Context context, Intent intent) {
66
 
          
67
        BarrierStatus barrierStatus = BarrierStatus.extract(intent);
68
 
          
69
        String label = barrierStatus.getBarrierLabel();
70
 
          
71
        switch(barrierStatus.getPresentStatus()) {
72
 
          
73
            case BarrierStatus.TRUE:
74
 
          
75
                time_info_barrier.setText("It is morning");
76
 
          
77
                break;
78
 
          
79
            case BarrierStatus.FALSE:
80
 
          
81
                time_info_barrier.setText("It is not morning");
82
 
          
83
                break;
84
 
          
85
            case BarrierStatus.UNKNOWN:
86
 
          
87
                Log.i(TAG, label + " status:unknown");
88
 
          
89
                break;
90
 
          
91
        }
92
 
          
93
    }
94
 
          
95
}



TimeCategories

This API class represents the semantic time of the current location. 

Public Methods:

There are two methods in TimeCategories class.

  1. getTimeCategories

  2. isTimeCategory

Let's go to the details.

1) getTimeCategories

It obtains the time of the current user location. 

Syntax

public abstract int[] getTimeCategories()

The function returns an array and each value in the array is defined in the TimeBarrier class. The three integer element in the returning array should be in the given order

[ weekday(6) or weekend(7),

holiday(5) or not holiday(8),

period of day(morning(1) or afternoon(2) or evening(3) or night(4)) ]

For example, if the returned array is [6,8,2], the time is [weekday, not a holiday, afternoon].

Java
 




xxxxxxxxxx
1
99


1
private void getTimeInfo() {
2
 
          
3
    Awareness.getCaptureClient(this).getTimeCategories()
4
 
          
5
            .addOnSuccessListener(new OnSuccessListener<TimeCategoriesResponse>() {
6
 
          
7
                @Override
8
 
          
9
                public void onSuccess(TimeCategoriesResponse timeCategoriesResponse) {
10
 
          
11
                    TimeCategories categories = timeCategoriesResponse.getTimeCategories();
12
 
          
13
                    int[] timeInfo = categories.getTimeCategories();
14
 
          
15
                    String timeInfoStr=timeInfo[0]+","+timeInfo[1]+","+timeInfo[2]+"\n";
16
 
          
17
 
18
 
          
19
                    switch(timeInfo[0]){
20
 
          
21
                        case 6:timeInfoStr+="Weekday,";
22
 
          
23
                            break;
24
 
          
25
                        case 7:timeInfoStr+="Weekend,";
26
 
          
27
                            break;
28
 
          
29
                        default:timeInfoStr+="Unknown,";
30
 
          
31
                            break;
32
 
          
33
                    }
34
 
          
35
                    switch(timeInfo[1]){
36
 
          
37
                        case 5:timeInfoStr+="Holiday,";
38
 
          
39
                            break;
40
 
          
41
                        case 8:timeInfoStr+="Not Holiday,";
42
 
          
43
                            break;
44
 
          
45
                        default:timeInfoStr+="Unknown,";
46
 
          
47
                            break;
48
 
          
49
                    }
50
 
          
51
                    switch(timeInfo[2]){
52
 
          
53
                        case 1:timeInfoStr+="Morning";
54
 
          
55
                            break;
56
 
          
57
                        case 2:timeInfoStr+="Afternoon";
58
 
          
59
                            break;
60
 
          
61
                        case 3:timeInfoStr+="Evening,";
62
 
          
63
                            break;
64
 
          
65
                        case 4:timeInfoStr+="Night";
66
 
          
67
                            break;
68
 
          
69
                        default:timeInfoStr+=" Unknown,";
70
 
          
71
                            break;
72
 
          
73
                    }
74
 
          
75
                    time_info_capture.setTextColor(getColor(R.color.green));
76
 
          
77
                    time_info_capture.setText(""+timeInfoStr);
78
 
          
79
                }
80
 
          
81
            })
82
 
          
83
            .addOnFailureListener(new OnFailureListener() {
84
 
          
85
                @Override
86
 
          
87
                public void onFailure(Exception e) {
88
 
          
89
                    Log.e(TAG, "get Time Categories failed", e);
90
 
          
91
                    time_info_capture.setTextColor(getColor(R.color.red));
92
 
          
93
                    time_info_capture.setText("get Time Categories failed: "+e);
94
 
          
95
                }
96
 
          
97
            });
98
 
          
99
}



2) isTimeCategory

It checks whether the current time information contains a time label.

Syntax

public abstract boolean isTimeCategory(int desiredTimeCategory)

Parameter

desiredTimeCategory

Time label


Java
 




xxxxxxxxxx
1
47


1
private void getIsTimeCategory() {
2
 
          
3
    Awareness.getCaptureClient(this).getTimeCategories()
4
 
          
5
            .addOnSuccessListener(new OnSuccessListener<TimeCategoriesResponse>() {
6
 
          
7
                @Override
8
 
          
9
                public void onSuccess(TimeCategoriesResponse timeCategoriesResponse) {
10
 
          
11
                    TimeCategories categories = timeCategoriesResponse.getTimeCategories();
12
 
          
13
                    time_info_capture.setTextColor(getColor(R.color.green));
14
 
          
15
                    if(categories.isTimeCategory(TimeBarrier.TIME_CATEGORY_NOT_HOLIDAY)){
16
 
          
17
                        time_info_capture.setText("Today is not Holiday");
18
 
          
19
                    }else{
20
 
          
21
                        time_info_capture.setText("Today is Holiday");
22
 
          
23
                    }
24
 
          
25
 
26
 
          
27
                }
28
 
          
29
            })
30
 
          
31
            .addOnFailureListener(new OnFailureListener() {
32
 
          
33
                @Override
34
 
          
35
                public void onFailure(Exception e) {
36
 
          
37
                    Log.e(TAG, "get Time Categories failed", e);
38
 
          
39
                    time_info_capture.setTextColor(getColor(R.color.red));
40
 
          
41
                    time_info_capture.setText("get Time Categories failed: "+e);
42
 
          
43
                }
44
 
          
45
            });
46
 
          
47
}



References:

https://developer.huawei.com/consumer/en/doc/development/HMS-References/time-categories-4

https://developer.huawei.com/consumer/en/doc/development/HMS-References/awareness-barrier-timebarrier

Topics:
hms, integration, java, kotlin, mobile, opensource

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}