com_android_server_power_PowerManagerService.cpp revision f20a5eb279035d462e1f5d9895f4eb66cc152215
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 40namespace android { 41 42// ---------------------------------------------------------------------------- 43 44static struct { 45 jmethodID wakeUpFromNative; 46 jmethodID goToSleepFromNative; 47 jmethodID userActivityFromNative; 48} gPowerManagerServiceClassInfo; 49 50// ---------------------------------------------------------------------------- 51 52static jobject gPowerManagerServiceObj; 53static struct power_module* gPowerModule; 54 55static Mutex gPowerManagerLock; 56static bool gScreenOn; 57static bool gScreenBright; 58 59static nsecs_t gLastEventTime[USER_ACTIVITY_EVENT_LAST + 1]; 60 61// Throttling interval for user activity calls. 62static const nsecs_t MIN_TIME_BETWEEN_USERACTIVITIES = 500 * 1000000L; // 500ms 63 64// ---------------------------------------------------------------------------- 65 66static 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 76bool android_server_PowerManagerService_isScreenOn() { 77 AutoMutex _l(gPowerManagerLock); 78 return gScreenOn; 79} 80 81bool android_server_PowerManagerService_isScreenBright() { 82 AutoMutex _l(gPowerManagerLock); 83 return gScreenBright; 84} 85 86void 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 117void 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 128void 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 141static 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 153static void nativeSetPowerState(JNIEnv* env, 154 jclass clazz, jboolean screenOn, jboolean screenBright) { 155 AutoMutex _l(gPowerManagerLock); 156 gScreenOn = screenOn; 157 gScreenBright = screenBright; 158} 159 160static 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 165static void nativeReleaseSuspendBlocker(JNIEnv *env, jclass clazz, jstring nameStr) { 166 ScopedUtfChars name(env, nameStr); 167 release_wake_lock(name.c_str()); 168} 169 170static 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 182static 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 192static void nativeSendPowerHint(JNIEnv *env, jclass clazz, jint hintId, jint data) { 193 if (gPowerModule && gPowerModule->powerHint) { 194 gPowerModule->powerHint(gPowerModule, (power_hint_t)hintId, (void *)data); 195 } 196} 197 198// ---------------------------------------------------------------------------- 199 200static JNINativeMethod gPowerManagerServiceMethods[] = { 201 /* name, signature, funcPtr */ 202 { "nativeInit", "()V", 203 (void*) nativeInit }, 204 { "nativeSetPowerState", "(ZZ)V", 205 (void*) nativeSetPowerState }, 206 { "nativeAcquireSuspendBlocker", "(Ljava/lang/String;)V", 207 (void*) nativeAcquireSuspendBlocker }, 208 { "nativeReleaseSuspendBlocker", "(Ljava/lang/String;)V", 209 (void*) nativeReleaseSuspendBlocker }, 210 { "nativeSetInteractive", "(Z)V", 211 (void*) nativeSetInteractive }, 212 { "nativeSetAutoSuspend", "(Z)V", 213 (void*) nativeSetAutoSuspend }, 214 { "nativeSendPowerHint", "(II)V", 215 (void*) nativeSendPowerHint }, 216}; 217 218#define FIND_CLASS(var, className) \ 219 var = env->FindClass(className); \ 220 LOG_FATAL_IF(! var, "Unable to find class " className); 221 222#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ 223 var = env->GetMethodID(clazz, methodName, methodDescriptor); \ 224 LOG_FATAL_IF(! var, "Unable to find method " methodName); 225 226#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ 227 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ 228 LOG_FATAL_IF(! var, "Unable to find field " fieldName); 229 230int register_android_server_PowerManagerService(JNIEnv* env) { 231 int res = jniRegisterNativeMethods(env, "com/android/server/power/PowerManagerService", 232 gPowerManagerServiceMethods, NELEM(gPowerManagerServiceMethods)); 233 LOG_FATAL_IF(res < 0, "Unable to register native methods."); 234 235 // Callbacks 236 237 jclass clazz; 238 FIND_CLASS(clazz, "com/android/server/power/PowerManagerService"); 239 240 GET_METHOD_ID(gPowerManagerServiceClassInfo.wakeUpFromNative, clazz, 241 "wakeUpFromNative", "(J)V"); 242 243 GET_METHOD_ID(gPowerManagerServiceClassInfo.goToSleepFromNative, clazz, 244 "goToSleepFromNative", "(JI)V"); 245 246 GET_METHOD_ID(gPowerManagerServiceClassInfo.userActivityFromNative, clazz, 247 "userActivityFromNative", "(JII)V"); 248 249 // Initialize 250 for (int i = 0; i <= USER_ACTIVITY_EVENT_LAST; i++) { 251 gLastEventTime[i] = LLONG_MIN; 252 } 253 gScreenOn = true; 254 gScreenBright = true; 255 gPowerManagerServiceObj = NULL; 256 gPowerModule = NULL; 257 return 0; 258} 259 260} /* namespace android */ 261