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 wakeUpFromNative; 46 jmethodID goToSleepFromNative; 47 jmethodID userActivityFromNative; 48 } gPowerManagerServiceClassInfo; 49 50 // ---------------------------------------------------------------------------- 51 52 static jobject gPowerManagerServiceObj; 53 static struct power_module* gPowerModule; 54 55 static Mutex gPowerManagerLock; 56 static bool gScreenOn; 57 static bool gScreenBright; 58 59 static nsecs_t gLastEventTime[USER_ACTIVITY_EVENT_LAST + 1]; 60 61 // Throttling interval for user activity calls. 62 static const nsecs_t MIN_TIME_BETWEEN_USERACTIVITIES = 500 * 1000000L; // 500ms 63 64 // ---------------------------------------------------------------------------- 65 66 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { 67 if (env->ExceptionCheck()) { 68 ALOGE("An exception was thrown by callback '%s'.", methodName); 69 LOGE_EX(env); 70 env->ExceptionClear(); 71 return true; 72 } 73 return false; 74 } 75 76 bool android_server_PowerManagerService_isScreenOn() { 77 AutoMutex _l(gPowerManagerLock); 78 return gScreenOn; 79 } 80 81 bool android_server_PowerManagerService_isScreenBright() { 82 AutoMutex _l(gPowerManagerLock); 83 return gScreenBright; 84 } 85 86 void android_server_PowerManagerService_userActivity(nsecs_t eventTime, int32_t eventType) { 87 // Tell the power HAL when user activity occurs. 88 if (gPowerModule && gPowerModule->powerHint) { 89 gPowerModule->powerHint(gPowerModule, POWER_HINT_INTERACTION, NULL); 90 } 91 92 if (gPowerManagerServiceObj) { 93 // Throttle calls into user activity by event type. 94 // We're a little conservative about argument checking here in case the caller 95 // passes in bad data which could corrupt system state. 96 if (eventType >= 0 && eventType <= USER_ACTIVITY_EVENT_LAST) { 97 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); 98 if (eventTime > now) { 99 eventTime = now; 100 } 101 102 if (gLastEventTime[eventType] + MIN_TIME_BETWEEN_USERACTIVITIES > eventTime) { 103 return; 104 } 105 gLastEventTime[eventType] = eventTime; 106 } 107 108 JNIEnv* env = AndroidRuntime::getJNIEnv(); 109 110 env->CallVoidMethod(gPowerManagerServiceObj, 111 gPowerManagerServiceClassInfo.userActivityFromNative, 112 nanoseconds_to_milliseconds(eventTime), eventType, 0); 113 checkAndClearExceptionFromCallback(env, "userActivityFromNative"); 114 } 115 } 116 117 void android_server_PowerManagerService_wakeUp(nsecs_t eventTime) { 118 if (gPowerManagerServiceObj) { 119 JNIEnv* env = AndroidRuntime::getJNIEnv(); 120 121 env->CallVoidMethod(gPowerManagerServiceObj, 122 gPowerManagerServiceClassInfo.wakeUpFromNative, 123 nanoseconds_to_milliseconds(eventTime)); 124 checkAndClearExceptionFromCallback(env, "wakeUpFromNative"); 125 } 126 } 127 128 void android_server_PowerManagerService_goToSleep(nsecs_t eventTime) { 129 if (gPowerManagerServiceObj) { 130 JNIEnv* env = AndroidRuntime::getJNIEnv(); 131 132 env->CallVoidMethod(gPowerManagerServiceObj, 133 gPowerManagerServiceClassInfo.goToSleepFromNative, 134 nanoseconds_to_milliseconds(eventTime), 0); 135 checkAndClearExceptionFromCallback(env, "goToSleepFromNative"); 136 } 137 } 138 139 // ---------------------------------------------------------------------------- 140 141 static void nativeInit(JNIEnv* env, jobject obj) { 142 gPowerManagerServiceObj = env->NewGlobalRef(obj); 143 144 status_t err = hw_get_module(POWER_HARDWARE_MODULE_ID, 145 (hw_module_t const**)&gPowerModule); 146 if (!err) { 147 gPowerModule->init(gPowerModule); 148 } else { 149 ALOGE("Couldn't load %s module (%s)", POWER_HARDWARE_MODULE_ID, strerror(-err)); 150 } 151 } 152 153 static void nativeSetPowerState(JNIEnv* env, 154 jclass clazz, jboolean screenOn, jboolean screenBright) { 155 AutoMutex _l(gPowerManagerLock); 156 gScreenOn = screenOn; 157 gScreenBright = screenBright; 158 } 159 160 static void nativeAcquireSuspendBlocker(JNIEnv *env, jclass clazz, jstring nameStr) { 161 ScopedUtfChars name(env, nameStr); 162 acquire_wake_lock(PARTIAL_WAKE_LOCK, name.c_str()); 163 } 164 165 static void nativeReleaseSuspendBlocker(JNIEnv *env, jclass clazz, jstring nameStr) { 166 ScopedUtfChars name(env, nameStr); 167 release_wake_lock(name.c_str()); 168 } 169 170 static void nativeSetInteractive(JNIEnv *env, jclass clazz, jboolean enable) { 171 if (gPowerModule) { 172 if (enable) { 173 ALOGD_IF_SLOW(20, "Excessive delay in setInteractive(true) while turning screen on"); 174 gPowerModule->setInteractive(gPowerModule, true); 175 } else { 176 ALOGD_IF_SLOW(20, "Excessive delay in setInteractive(false) while turning screen off"); 177 gPowerModule->setInteractive(gPowerModule, false); 178 } 179 } 180 } 181 182 static void nativeSetAutoSuspend(JNIEnv *env, jclass clazz, jboolean enable) { 183 if (enable) { 184 ALOGD_IF_SLOW(100, "Excessive delay in autosuspend_enable() while turning screen off"); 185 autosuspend_enable(); 186 } else { 187 ALOGD_IF_SLOW(100, "Excessive delay in autosuspend_disable() while turning screen on"); 188 autosuspend_disable(); 189 } 190 } 191 192 // ---------------------------------------------------------------------------- 193 194 static JNINativeMethod gPowerManagerServiceMethods[] = { 195 /* name, signature, funcPtr */ 196 { "nativeInit", "()V", 197 (void*) nativeInit }, 198 { "nativeSetPowerState", "(ZZ)V", 199 (void*) nativeSetPowerState }, 200 { "nativeAcquireSuspendBlocker", "(Ljava/lang/String;)V", 201 (void*) nativeAcquireSuspendBlocker }, 202 { "nativeReleaseSuspendBlocker", "(Ljava/lang/String;)V", 203 (void*) nativeReleaseSuspendBlocker }, 204 { "nativeSetInteractive", "(Z)V", 205 (void*) nativeSetInteractive }, 206 { "nativeSetAutoSuspend", "(Z)V", 207 (void*) nativeSetAutoSuspend }, 208 }; 209 210 #define FIND_CLASS(var, className) \ 211 var = env->FindClass(className); \ 212 LOG_FATAL_IF(! var, "Unable to find class " className); 213 214 #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ 215 var = env->GetMethodID(clazz, methodName, methodDescriptor); \ 216 LOG_FATAL_IF(! var, "Unable to find method " methodName); 217 218 #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ 219 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ 220 LOG_FATAL_IF(! var, "Unable to find field " fieldName); 221 222 int register_android_server_PowerManagerService(JNIEnv* env) { 223 int res = jniRegisterNativeMethods(env, "com/android/server/power/PowerManagerService", 224 gPowerManagerServiceMethods, NELEM(gPowerManagerServiceMethods)); 225 LOG_FATAL_IF(res < 0, "Unable to register native methods."); 226 227 // Callbacks 228 229 jclass clazz; 230 FIND_CLASS(clazz, "com/android/server/power/PowerManagerService"); 231 232 GET_METHOD_ID(gPowerManagerServiceClassInfo.wakeUpFromNative, clazz, 233 "wakeUpFromNative", "(J)V"); 234 235 GET_METHOD_ID(gPowerManagerServiceClassInfo.goToSleepFromNative, clazz, 236 "goToSleepFromNative", "(JI)V"); 237 238 GET_METHOD_ID(gPowerManagerServiceClassInfo.userActivityFromNative, clazz, 239 "userActivityFromNative", "(JII)V"); 240 241 // Initialize 242 for (int i = 0; i <= USER_ACTIVITY_EVENT_LAST; i++) { 243 gLastEventTime[i] = LLONG_MIN; 244 } 245 gScreenOn = true; 246 gScreenBright = true; 247 gPowerManagerServiceObj = NULL; 248 gPowerModule = NULL; 249 return 0; 250 } 251 252 } /* namespace android */ 253