1 /*
2 * Copyright (C) 2010 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 "PowerManagerService-JNI"
18
19 //#define LOG_NDEBUG 0
20
21 #include "JNIHelp.h"
22 #include "jni.h"
23
24 #include <ScopedUtfChars.h>
25
26 #include <limits.h>
27
28 #include <android_runtime/AndroidRuntime.h>
29 #include <android_runtime/Log.h>
30 #include <utils/Timers.h>
31 #include <utils/misc.h>
32 #include <utils/String8.h>
33 #include <utils/Log.h>
34 #include <hardware/power.h>
35 #include <hardware_legacy/power.h>
36 #include <suspend/autosuspend.h>
37
38 #include "com_android_server_power_PowerManagerService.h"
39
40 namespace android {
41
42 // ----------------------------------------------------------------------------
43
44 static struct {
45 jmethodID userActivityFromNative;
46 } gPowerManagerServiceClassInfo;
47
48 // ----------------------------------------------------------------------------
49
50 static jobject gPowerManagerServiceObj;
51 static struct power_module* gPowerModule;
52
53 static nsecs_t gLastEventTime[USER_ACTIVITY_EVENT_LAST + 1];
54
55 // Throttling interval for user activity calls.
56 static const nsecs_t MIN_TIME_BETWEEN_USERACTIVITIES = 500 * 1000000L; // 500ms
57
58 // ----------------------------------------------------------------------------
59
checkAndClearExceptionFromCallback(JNIEnv * env,const char * methodName)60 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
61 if (env->ExceptionCheck()) {
62 ALOGE("An exception was thrown by callback '%s'.", methodName);
63 LOGE_EX(env);
64 env->ExceptionClear();
65 return true;
66 }
67 return false;
68 }
69
android_server_PowerManagerService_userActivity(nsecs_t eventTime,int32_t eventType)70 void android_server_PowerManagerService_userActivity(nsecs_t eventTime, int32_t eventType) {
71 // Tell the power HAL when user activity occurs.
72 if (gPowerModule && gPowerModule->powerHint) {
73 gPowerModule->powerHint(gPowerModule, POWER_HINT_INTERACTION, NULL);
74 }
75
76 if (gPowerManagerServiceObj) {
77 // Throttle calls into user activity by event type.
78 // We're a little conservative about argument checking here in case the caller
79 // passes in bad data which could corrupt system state.
80 if (eventType >= 0 && eventType <= USER_ACTIVITY_EVENT_LAST) {
81 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
82 if (eventTime > now) {
83 eventTime = now;
84 }
85
86 if (gLastEventTime[eventType] + MIN_TIME_BETWEEN_USERACTIVITIES > eventTime) {
87 return;
88 }
89 gLastEventTime[eventType] = eventTime;
90 }
91
92 JNIEnv* env = AndroidRuntime::getJNIEnv();
93
94 env->CallVoidMethod(gPowerManagerServiceObj,
95 gPowerManagerServiceClassInfo.userActivityFromNative,
96 nanoseconds_to_milliseconds(eventTime), eventType, 0);
97 checkAndClearExceptionFromCallback(env, "userActivityFromNative");
98 }
99 }
100
101 // ----------------------------------------------------------------------------
102
nativeInit(JNIEnv * env,jobject obj)103 static void nativeInit(JNIEnv* env, jobject obj) {
104 gPowerManagerServiceObj = env->NewGlobalRef(obj);
105
106 status_t err = hw_get_module(POWER_HARDWARE_MODULE_ID,
107 (hw_module_t const**)&gPowerModule);
108 if (!err) {
109 gPowerModule->init(gPowerModule);
110 } else {
111 ALOGE("Couldn't load %s module (%s)", POWER_HARDWARE_MODULE_ID, strerror(-err));
112 }
113 }
114
nativeAcquireSuspendBlocker(JNIEnv * env,jclass clazz,jstring nameStr)115 static void nativeAcquireSuspendBlocker(JNIEnv *env, jclass clazz, jstring nameStr) {
116 ScopedUtfChars name(env, nameStr);
117 acquire_wake_lock(PARTIAL_WAKE_LOCK, name.c_str());
118 }
119
nativeReleaseSuspendBlocker(JNIEnv * env,jclass clazz,jstring nameStr)120 static void nativeReleaseSuspendBlocker(JNIEnv *env, jclass clazz, jstring nameStr) {
121 ScopedUtfChars name(env, nameStr);
122 release_wake_lock(name.c_str());
123 }
124
nativeSetInteractive(JNIEnv * env,jclass clazz,jboolean enable)125 static void nativeSetInteractive(JNIEnv *env, jclass clazz, jboolean enable) {
126 if (gPowerModule) {
127 if (enable) {
128 ALOGD_IF_SLOW(20, "Excessive delay in setInteractive(true) while turning screen on");
129 gPowerModule->setInteractive(gPowerModule, true);
130 } else {
131 ALOGD_IF_SLOW(20, "Excessive delay in setInteractive(false) while turning screen off");
132 gPowerModule->setInteractive(gPowerModule, false);
133 }
134 }
135 }
136
nativeSetAutoSuspend(JNIEnv * env,jclass clazz,jboolean enable)137 static void nativeSetAutoSuspend(JNIEnv *env, jclass clazz, jboolean enable) {
138 if (enable) {
139 ALOGD_IF_SLOW(100, "Excessive delay in autosuspend_enable() while turning screen off");
140 autosuspend_enable();
141 } else {
142 ALOGD_IF_SLOW(100, "Excessive delay in autosuspend_disable() while turning screen on");
143 autosuspend_disable();
144 }
145 }
146
nativeSendPowerHint(JNIEnv * env,jclass clazz,jint hintId,jint data)147 static void nativeSendPowerHint(JNIEnv *env, jclass clazz, jint hintId, jint data) {
148 int data_param = data;
149
150 if (gPowerModule && gPowerModule->powerHint) {
151 if(data)
152 gPowerModule->powerHint(gPowerModule, (power_hint_t)hintId, &data_param);
153 else {
154 gPowerModule->powerHint(gPowerModule, (power_hint_t)hintId, NULL);
155 }
156 }
157 }
158
159 // ----------------------------------------------------------------------------
160
161 static JNINativeMethod gPowerManagerServiceMethods[] = {
162 /* name, signature, funcPtr */
163 { "nativeInit", "()V",
164 (void*) nativeInit },
165 { "nativeAcquireSuspendBlocker", "(Ljava/lang/String;)V",
166 (void*) nativeAcquireSuspendBlocker },
167 { "nativeReleaseSuspendBlocker", "(Ljava/lang/String;)V",
168 (void*) nativeReleaseSuspendBlocker },
169 { "nativeSetInteractive", "(Z)V",
170 (void*) nativeSetInteractive },
171 { "nativeSetAutoSuspend", "(Z)V",
172 (void*) nativeSetAutoSuspend },
173 { "nativeSendPowerHint", "(II)V",
174 (void*) nativeSendPowerHint },
175 };
176
177 #define FIND_CLASS(var, className) \
178 var = env->FindClass(className); \
179 LOG_FATAL_IF(! var, "Unable to find class " className);
180
181 #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
182 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
183 LOG_FATAL_IF(! var, "Unable to find method " methodName);
184
185 #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
186 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
187 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
188
register_android_server_PowerManagerService(JNIEnv * env)189 int register_android_server_PowerManagerService(JNIEnv* env) {
190 int res = jniRegisterNativeMethods(env, "com/android/server/power/PowerManagerService",
191 gPowerManagerServiceMethods, NELEM(gPowerManagerServiceMethods));
192 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
193
194 // Callbacks
195
196 jclass clazz;
197 FIND_CLASS(clazz, "com/android/server/power/PowerManagerService");
198
199 GET_METHOD_ID(gPowerManagerServiceClassInfo.userActivityFromNative, clazz,
200 "userActivityFromNative", "(JII)V");
201
202 // Initialize
203 for (int i = 0; i <= USER_ACTIVITY_EVENT_LAST; i++) {
204 gLastEventTime[i] = LLONG_MIN;
205 }
206 gPowerManagerServiceObj = NULL;
207 gPowerModule = NULL;
208 return 0;
209 }
210
211 } /* namespace android */
212