1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBTEXTCLASSIFIER_UTILS_CALENDAR_CALENDAR_JAVAICU_H_
18 #define LIBTEXTCLASSIFIER_UTILS_CALENDAR_CALENDAR_JAVAICU_H_
19 
20 #include <jni.h>
21 #include <memory>
22 #include <string>
23 
24 #include "annotator/types.h"
25 #include "utils/base/integral_types.h"
26 #include "utils/calendar/calendar-common.h"
27 #include "utils/java/jni-cache.h"
28 #include "utils/java/scoped_local_ref.h"
29 
30 namespace libtextclassifier3 {
31 
32 class Calendar {
33  public:
34   explicit Calendar(JniCache* jni_cache);
35   bool Initialize(const std::string& time_zone, const std::string& locale,
36                   int64 time_ms_utc);
37   bool AddSecond(int value) const;
38   bool AddMinute(int value) const;
39   bool AddHourOfDay(int value) const;
40   bool AddDayOfMonth(int value) const;
41   bool AddYear(int value) const;
42   bool AddMonth(int value) const;
43   bool GetDayOfWeek(int* value) const;
44   bool GetFirstDayOfWeek(int* value) const;
45   bool GetTimeInMillis(int64* value) const;
46   bool SetZoneOffset(int value) const;
47   bool SetDstOffset(int value) const;
48   bool SetYear(int value) const;
49   bool SetMonth(int value) const;
50   bool SetDayOfYear(int value) const;
51   bool SetDayOfMonth(int value) const;
52   bool SetDayOfWeek(int value) const;
53   bool SetHourOfDay(int value) const;
54   bool SetMinute(int value) const;
55   bool SetSecond(int value) const;
56   bool SetMillisecond(int value) const;
57 
58  private:
59   JniCache* jni_cache_;
60   JNIEnv* jenv_;
61   ScopedLocalRef<jobject> calendar_;
62 };
63 
64 class CalendarLib {
65  public:
66   CalendarLib();
67   explicit CalendarLib(const std::shared_ptr<JniCache>& jni_cache);
68 
69   // Returns false (dummy version).
InterpretParseData(const DateParseData & parse_data,int64 reference_time_ms_utc,const std::string & reference_timezone,const std::string & reference_locale,int64 * interpreted_time_ms_utc,DatetimeGranularity * granularity)70   bool InterpretParseData(const DateParseData& parse_data,
71                           int64 reference_time_ms_utc,
72                           const std::string& reference_timezone,
73                           const std::string& reference_locale,
74                           int64* interpreted_time_ms_utc,
75                           DatetimeGranularity* granularity) const {
76     Calendar calendar(jni_cache_.get());
77     if (!impl_.InterpretParseData(parse_data, reference_time_ms_utc,
78                                   reference_timezone, reference_locale,
79                                   &calendar, granularity)) {
80       return false;
81     }
82     return calendar.GetTimeInMillis(interpreted_time_ms_utc);
83   }
84 
GetGranularity(const DateParseData & data)85   DatetimeGranularity GetGranularity(const DateParseData& data) const {
86     return impl_.GetGranularity(data);
87   }
88 
89  private:
90   std::shared_ptr<JniCache> jni_cache_;
91   calendar::CalendarLibTempl<Calendar> impl_;
92 };
93 
94 }  // namespace libtextclassifier3
95 
96 #endif  // LIBTEXTCLASSIFIER_UTILS_CALENDAR_CALENDAR_JAVAICU_H_
97