1 /*
2  * Copyright (C) 2021 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 #define LOG_TAG "GnssGeofenceCbJni"
18 
19 #include "GnssGeofenceCallback.h"
20 
21 namespace android::gnss {
22 
23 namespace {
24 
25 jmethodID method_reportGeofenceTransition;
26 jmethodID method_reportGeofenceStatus;
27 jmethodID method_reportGeofenceAddStatus;
28 jmethodID method_reportGeofenceRemoveStatus;
29 jmethodID method_reportGeofencePauseStatus;
30 jmethodID method_reportGeofenceResumeStatus;
31 
32 } // anonymous namespace
33 
34 using binder::Status;
35 using hardware::Return;
36 using hardware::Void;
37 using GeofenceAvailability =
38         android::hardware::gnss::V1_0::IGnssGeofenceCallback::GeofenceAvailability;
39 using GeofenceStatus = android::hardware::gnss::V1_0::IGnssGeofenceCallback::GeofenceStatus;
40 using GeofenceTransition = android::hardware::gnss::V1_0::IGnssGeofenceCallback::GeofenceTransition;
41 
42 using GnssLocationAidl = android::hardware::gnss::GnssLocation;
43 using GnssLocation_V1_0 = android::hardware::gnss::V1_0::GnssLocation;
44 
GnssGeofence_class_init_once(JNIEnv * env,jclass clazz)45 void GnssGeofence_class_init_once(JNIEnv* env, jclass clazz) {
46     method_reportGeofenceTransition = env->GetMethodID(clazz, "reportGeofenceTransition",
47                                                        "(ILandroid/location/Location;IJ)V");
48     method_reportGeofenceStatus =
49             env->GetMethodID(clazz, "reportGeofenceStatus", "(ILandroid/location/Location;)V");
50     method_reportGeofenceAddStatus = env->GetMethodID(clazz, "reportGeofenceAddStatus", "(II)V");
51     method_reportGeofenceRemoveStatus =
52             env->GetMethodID(clazz, "reportGeofenceRemoveStatus", "(II)V");
53     method_reportGeofenceResumeStatus =
54             env->GetMethodID(clazz, "reportGeofenceResumeStatus", "(II)V");
55     method_reportGeofencePauseStatus =
56             env->GetMethodID(clazz, "reportGeofencePauseStatus", "(II)V");
57 }
58 
gnssGeofenceTransitionCb(int geofenceId,const GnssLocationAidl & location,int transition,int64_t timestampMillis)59 Status GnssGeofenceCallbackAidl::gnssGeofenceTransitionCb(int geofenceId,
60                                                           const GnssLocationAidl& location,
61                                                           int transition, int64_t timestampMillis) {
62     GnssGeofenceCallbackUtil::gnssGeofenceTransitionCb(geofenceId, location, transition,
63                                                        timestampMillis);
64     return Status::ok();
65 }
66 
gnssGeofenceStatusCb(int availability,const GnssLocationAidl & lastLocation)67 Status GnssGeofenceCallbackAidl::gnssGeofenceStatusCb(int availability,
68                                                       const GnssLocationAidl& lastLocation) {
69     GnssGeofenceCallbackUtil::gnssGeofenceStatusCb(availability, lastLocation);
70     return Status::ok();
71 }
72 
gnssGeofenceAddCb(int geofenceId,int status)73 Status GnssGeofenceCallbackAidl::gnssGeofenceAddCb(int geofenceId, int status) {
74     GnssGeofenceCallbackUtil::gnssGeofenceAddCb(geofenceId, status);
75     return Status::ok();
76 }
77 
gnssGeofenceRemoveCb(int geofenceId,int status)78 Status GnssGeofenceCallbackAidl::gnssGeofenceRemoveCb(int geofenceId, int status) {
79     GnssGeofenceCallbackUtil::gnssGeofenceRemoveCb(geofenceId, status);
80     return Status::ok();
81 }
82 
gnssGeofencePauseCb(int geofenceId,int status)83 Status GnssGeofenceCallbackAidl::gnssGeofencePauseCb(int geofenceId, int status) {
84     GnssGeofenceCallbackUtil::gnssGeofencePauseCb(geofenceId, status);
85     return Status::ok();
86 }
87 
gnssGeofenceResumeCb(int geofenceId,int status)88 Status GnssGeofenceCallbackAidl::gnssGeofenceResumeCb(int geofenceId, int status) {
89     GnssGeofenceCallbackUtil::gnssGeofenceResumeCb(geofenceId, status);
90     return Status::ok();
91 }
92 
gnssGeofenceTransitionCb(int32_t geofenceId,const GnssLocation_V1_0 & location,GeofenceTransition transition,hardware::gnss::V1_0::GnssUtcTime timestamp)93 Return<void> GnssGeofenceCallbackHidl::gnssGeofenceTransitionCb(
94         int32_t geofenceId, const GnssLocation_V1_0& location, GeofenceTransition transition,
95         hardware::gnss::V1_0::GnssUtcTime timestamp) {
96     GnssGeofenceCallbackUtil::gnssGeofenceTransitionCb(geofenceId, location, (int)transition,
97                                                        (int64_t)timestamp);
98     return Void();
99 }
100 
gnssGeofenceStatusCb(GeofenceAvailability availability,const GnssLocation_V1_0 & location)101 Return<void> GnssGeofenceCallbackHidl::gnssGeofenceStatusCb(GeofenceAvailability availability,
102                                                             const GnssLocation_V1_0& location) {
103     GnssGeofenceCallbackUtil::gnssGeofenceStatusCb((int)availability, location);
104     return Void();
105 }
106 
gnssGeofenceAddCb(int32_t geofenceId,GeofenceStatus status)107 Return<void> GnssGeofenceCallbackHidl::gnssGeofenceAddCb(int32_t geofenceId,
108                                                          GeofenceStatus status) {
109     GnssGeofenceCallbackUtil::gnssGeofenceAddCb(geofenceId, (int)status);
110     return Void();
111 }
112 
gnssGeofenceRemoveCb(int32_t geofenceId,GeofenceStatus status)113 Return<void> GnssGeofenceCallbackHidl::gnssGeofenceRemoveCb(int32_t geofenceId,
114                                                             GeofenceStatus status) {
115     GnssGeofenceCallbackUtil::gnssGeofenceRemoveCb(geofenceId, (int)status);
116     return Void();
117 }
118 
gnssGeofencePauseCb(int32_t geofenceId,GeofenceStatus status)119 Return<void> GnssGeofenceCallbackHidl::gnssGeofencePauseCb(int32_t geofenceId,
120                                                            GeofenceStatus status) {
121     GnssGeofenceCallbackUtil::gnssGeofencePauseCb(geofenceId, (int)status);
122     return Void();
123 }
124 
gnssGeofenceResumeCb(int32_t geofenceId,GeofenceStatus status)125 Return<void> GnssGeofenceCallbackHidl::gnssGeofenceResumeCb(int32_t geofenceId,
126                                                             GeofenceStatus status) {
127     GnssGeofenceCallbackUtil::gnssGeofenceResumeCb(geofenceId, (int)status);
128     return Void();
129 }
130 
gnssGeofenceAddCb(int geofenceId,int status)131 void GnssGeofenceCallbackUtil::gnssGeofenceAddCb(int geofenceId, int status) {
132     JNIEnv* env = getJniEnv();
133     if (status != hardware::gnss::IGnssGeofenceCallback::OPERATION_SUCCESS) {
134         ALOGE("%s: Error in adding a Geofence: %d\n", __func__, status);
135     }
136 
137     env->CallVoidMethod(mCallbacksObj, method_reportGeofenceAddStatus, geofenceId, status);
138     checkAndClearExceptionFromCallback(env, __FUNCTION__);
139 }
140 
gnssGeofenceRemoveCb(int geofenceId,int status)141 void GnssGeofenceCallbackUtil::gnssGeofenceRemoveCb(int geofenceId, int status) {
142     JNIEnv* env = getJniEnv();
143     if (status != hardware::gnss::IGnssGeofenceCallback::OPERATION_SUCCESS) {
144         ALOGE("%s: Error in removing a Geofence: %d\n", __func__, status);
145     }
146 
147     env->CallVoidMethod(mCallbacksObj, method_reportGeofenceRemoveStatus, geofenceId, status);
148     checkAndClearExceptionFromCallback(env, __FUNCTION__);
149 }
150 
gnssGeofencePauseCb(int geofenceId,int status)151 void GnssGeofenceCallbackUtil::gnssGeofencePauseCb(int geofenceId, int status) {
152     JNIEnv* env = getJniEnv();
153     if (status != hardware::gnss::IGnssGeofenceCallback::OPERATION_SUCCESS) {
154         ALOGE("%s: Error in pausing Geofence: %d\n", __func__, status);
155     }
156 
157     env->CallVoidMethod(mCallbacksObj, method_reportGeofencePauseStatus, geofenceId, status);
158     checkAndClearExceptionFromCallback(env, __FUNCTION__);
159 }
160 
gnssGeofenceResumeCb(int geofenceId,int status)161 void GnssGeofenceCallbackUtil::gnssGeofenceResumeCb(int geofenceId, int status) {
162     JNIEnv* env = getJniEnv();
163     if (status != hardware::gnss::IGnssGeofenceCallback::OPERATION_SUCCESS) {
164         ALOGE("%s: Error in resuming Geofence: %d\n", __func__, status);
165     }
166 
167     env->CallVoidMethod(mCallbacksObj, method_reportGeofenceResumeStatus, geofenceId, status);
168     checkAndClearExceptionFromCallback(env, __FUNCTION__);
169 }
170 
171 } // namespace android::gnss
172