Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 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" |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 23 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 24 | #include <limits.h> |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 25 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 26 | #include <android_runtime/AndroidRuntime.h> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 27 | #include <gui/ISurfaceComposer.h> |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 28 | #include <utils/Timers.h> |
| 29 | #include <utils/misc.h> |
| 30 | #include <utils/String8.h> |
| 31 | #include <hardware/power.h> |
| 32 | #include <hardware_legacy/power.h> |
| 33 | #include <cutils/android_reboot.h> |
| 34 | #include <suspend/autosuspend.h> |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 35 | |
Mathias Agopian | 3f15700 | 2011-11-17 17:48:35 -0800 | [diff] [blame] | 36 | #include <private/gui/ComposerService.h> |
| 37 | |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 38 | #include "com_android_server_power_PowerManagerService.h" |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | |
| 44 | static struct { |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 45 | jmethodID goToSleep; |
| 46 | jmethodID userActivity; |
| 47 | } gPowerManagerServiceClassInfo; |
| 48 | |
| 49 | // ---------------------------------------------------------------------------- |
| 50 | |
| 51 | static jobject gPowerManagerServiceObj; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 52 | static struct power_module* gPowerModule; |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 53 | |
| 54 | static Mutex gPowerManagerLock; |
| 55 | static bool gScreenOn; |
| 56 | static bool gScreenBright; |
| 57 | |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame^] | 58 | static nsecs_t gLastEventTime[USER_ACTIVITY_EVENT_LAST + 1]; |
Jeff Brown | 9029157 | 2010-08-16 14:19:45 -0700 | [diff] [blame] | 59 | |
| 60 | // Throttling interval for user activity calls. |
| 61 | static const nsecs_t MIN_TIME_BETWEEN_USERACTIVITIES = 500 * 1000000L; // 500ms |
| 62 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 63 | // ---------------------------------------------------------------------------- |
| 64 | |
| 65 | static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 66 | if (env->ExceptionCheck()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 67 | ALOGE("An exception was thrown by callback '%s'.", methodName); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 68 | LOGE_EX(env); |
| 69 | env->ExceptionClear(); |
| 70 | return true; |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | bool android_server_PowerManagerService_isScreenOn() { |
| 76 | AutoMutex _l(gPowerManagerLock); |
| 77 | return gScreenOn; |
| 78 | } |
| 79 | |
| 80 | bool android_server_PowerManagerService_isScreenBright() { |
| 81 | AutoMutex _l(gPowerManagerLock); |
| 82 | return gScreenBright; |
| 83 | } |
| 84 | |
| 85 | void android_server_PowerManagerService_userActivity(nsecs_t eventTime, int32_t eventType) { |
Jeff Brown | d232afa | 2012-05-16 13:06:15 -0700 | [diff] [blame] | 86 | // Tell the power HAL when user activity occurs. |
| 87 | if (gPowerModule && gPowerModule->powerHint) { |
| 88 | gPowerModule->powerHint(gPowerModule, POWER_HINT_INTERACTION, NULL); |
Jeff Brown | 30e5eb4 | 2012-05-11 19:18:56 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 91 | if (gPowerManagerServiceObj) { |
Jeff Brown | 9029157 | 2010-08-16 14:19:45 -0700 | [diff] [blame] | 92 | // Throttle calls into user activity by event type. |
| 93 | // We're a little conservative about argument checking here in case the caller |
| 94 | // passes in bad data which could corrupt system state. |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame^] | 95 | if (eventType >= 0 && eventType <= USER_ACTIVITY_EVENT_LAST) { |
Jeff Brown | 9029157 | 2010-08-16 14:19:45 -0700 | [diff] [blame] | 96 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 97 | if (eventTime > now) { |
| 98 | eventTime = now; |
| 99 | } |
| 100 | |
| 101 | if (gLastEventTime[eventType] + MIN_TIME_BETWEEN_USERACTIVITIES > eventTime) { |
| 102 | return; |
| 103 | } |
| 104 | gLastEventTime[eventType] = eventTime; |
| 105 | } |
| 106 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 107 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 108 | |
| 109 | env->CallVoidMethod(gPowerManagerServiceObj, gPowerManagerServiceClassInfo.userActivity, |
| 110 | nanoseconds_to_milliseconds(eventTime), false, eventType, false); |
| 111 | checkAndClearExceptionFromCallback(env, "userActivity"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void android_server_PowerManagerService_goToSleep(nsecs_t eventTime) { |
| 116 | if (gPowerManagerServiceObj) { |
| 117 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 118 | |
| 119 | env->CallVoidMethod(gPowerManagerServiceObj, gPowerManagerServiceClassInfo.goToSleep, |
| 120 | nanoseconds_to_milliseconds(eventTime)); |
| 121 | checkAndClearExceptionFromCallback(env, "goToSleep"); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // ---------------------------------------------------------------------------- |
| 126 | |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 127 | static void nativeInit(JNIEnv* env, jobject obj) { |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 128 | gPowerManagerServiceObj = env->NewGlobalRef(obj); |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 129 | |
| 130 | status_t err = hw_get_module(POWER_HARDWARE_MODULE_ID, |
| 131 | (hw_module_t const**)&gPowerModule); |
Jeff Brown | d232afa | 2012-05-16 13:06:15 -0700 | [diff] [blame] | 132 | if (!err) { |
| 133 | gPowerModule->init(gPowerModule); |
| 134 | } else { |
| 135 | ALOGE("Couldn't load %s module (%s)", POWER_HARDWARE_MODULE_ID, strerror(-err)); |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 136 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 139 | static void nativeSetPowerState(JNIEnv* env, |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 140 | jobject serviceObj, jboolean screenOn, jboolean screenBright) { |
| 141 | AutoMutex _l(gPowerManagerLock); |
| 142 | gScreenOn = screenOn; |
| 143 | gScreenBright = screenBright; |
| 144 | } |
| 145 | |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 146 | static void nativeStartSurfaceFlingerAnimation(JNIEnv* env, |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 147 | jobject obj, jint mode) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 148 | sp<ISurfaceComposer> s(ComposerService::getComposerService()); |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 149 | s->turnElectronBeamOff(mode); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 152 | static void nativeAcquireWakeLock(JNIEnv *env, jobject clazz, jint lock, jstring idObj) { |
| 153 | if (idObj == NULL) { |
| 154 | jniThrowNullPointerException(env, "id is null"); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | const char *id = env->GetStringUTFChars(idObj, NULL); |
| 159 | |
| 160 | acquire_wake_lock(lock, id); |
| 161 | |
| 162 | env->ReleaseStringUTFChars(idObj, id); |
| 163 | } |
| 164 | |
| 165 | static void nativeReleaseWakeLock(JNIEnv *env, jobject clazz, jstring idObj) { |
| 166 | if (idObj == NULL) { |
| 167 | jniThrowNullPointerException(env, "id is null"); |
| 168 | return ; |
| 169 | } |
| 170 | |
| 171 | const char *id = env->GetStringUTFChars(idObj, NULL); |
| 172 | |
| 173 | release_wake_lock(id); |
| 174 | |
| 175 | env->ReleaseStringUTFChars(idObj, id); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | static int nativeSetScreenState(JNIEnv *env, jobject clazz, jboolean on) { |
Colin Cross | dccb2c1 | 2012-06-07 00:16:02 -0700 | [diff] [blame] | 180 | sp<ISurfaceComposer> s(ComposerService::getComposerService()); |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 181 | if (on) { |
| 182 | autosuspend_disable(); |
| 183 | if (gPowerModule) { |
| 184 | gPowerModule->setInteractive(gPowerModule, true); |
| 185 | } |
Colin Cross | dccb2c1 | 2012-06-07 00:16:02 -0700 | [diff] [blame] | 186 | s->unblank(); |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 187 | } else { |
Colin Cross | dccb2c1 | 2012-06-07 00:16:02 -0700 | [diff] [blame] | 188 | s->blank(); |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 189 | if (gPowerModule) { |
| 190 | gPowerModule->setInteractive(gPowerModule, false); |
| 191 | } |
| 192 | autosuspend_enable(); |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static void nativeShutdown(JNIEnv *env, jobject clazz) { |
| 199 | android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
| 200 | } |
| 201 | |
| 202 | static void nativeReboot(JNIEnv *env, jobject clazz, jstring reason) { |
| 203 | if (reason == NULL) { |
| 204 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 205 | } else { |
| 206 | const char *chars = env->GetStringUTFChars(reason, NULL); |
| 207 | android_reboot(ANDROID_RB_RESTART2, 0, (char *) chars); |
| 208 | env->ReleaseStringUTFChars(reason, chars); // In case it fails. |
| 209 | } |
| 210 | jniThrowIOException(env, errno); |
| 211 | } |
| 212 | |
| 213 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 214 | // ---------------------------------------------------------------------------- |
| 215 | |
| 216 | static JNINativeMethod gPowerManagerServiceMethods[] = { |
| 217 | /* name, signature, funcPtr */ |
| 218 | { "nativeInit", "()V", |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 219 | (void*) nativeInit }, |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 220 | { "nativeSetPowerState", "(ZZ)V", |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 221 | (void*) nativeSetPowerState }, |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 222 | { "nativeStartSurfaceFlingerAnimation", "(I)V", |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 223 | (void*) nativeStartSurfaceFlingerAnimation }, |
| 224 | { "nativeAcquireWakeLock", "(ILjava/lang/String;)V", |
| 225 | (void*) nativeAcquireWakeLock }, |
| 226 | { "nativeReleaseWakeLock", "(Ljava/lang/String;)V", |
| 227 | (void*) nativeReleaseWakeLock }, |
| 228 | { "nativeSetScreenState", "(Z)I", |
| 229 | (void*) nativeSetScreenState }, |
| 230 | { "nativeShutdown", "()V", |
| 231 | (void*) nativeShutdown }, |
| 232 | { "nativeReboot", "(Ljava/lang/String;)V", |
| 233 | (void*) nativeReboot }, |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | #define FIND_CLASS(var, className) \ |
| 237 | var = env->FindClass(className); \ |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 238 | LOG_FATAL_IF(! var, "Unable to find class " className); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 239 | |
| 240 | #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ |
| 241 | var = env->GetMethodID(clazz, methodName, methodDescriptor); \ |
| 242 | LOG_FATAL_IF(! var, "Unable to find method " methodName); |
| 243 | |
| 244 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 245 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 246 | LOG_FATAL_IF(! var, "Unable to find field " fieldName); |
| 247 | |
| 248 | int register_android_server_PowerManagerService(JNIEnv* env) { |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 249 | int res = jniRegisterNativeMethods(env, "com/android/server/power/PowerManagerService", |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 250 | gPowerManagerServiceMethods, NELEM(gPowerManagerServiceMethods)); |
| 251 | LOG_FATAL_IF(res < 0, "Unable to register native methods."); |
| 252 | |
| 253 | // Callbacks |
| 254 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 255 | jclass clazz; |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 256 | FIND_CLASS(clazz, "com/android/server/power/PowerManagerService"); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 257 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 258 | GET_METHOD_ID(gPowerManagerServiceClassInfo.goToSleep, clazz, |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 259 | "goToSleep", "(J)V"); |
| 260 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 261 | GET_METHOD_ID(gPowerManagerServiceClassInfo.userActivity, clazz, |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 262 | "userActivity", "(JZIZ)V"); |
| 263 | |
Jeff Brown | 9029157 | 2010-08-16 14:19:45 -0700 | [diff] [blame] | 264 | // Initialize |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame^] | 265 | for (int i = 0; i <= USER_ACTIVITY_EVENT_LAST; i++) { |
Jeff Brown | 9029157 | 2010-08-16 14:19:45 -0700 | [diff] [blame] | 266 | gLastEventTime[i] = LLONG_MIN; |
| 267 | } |
| 268 | gScreenOn = true; |
| 269 | gScreenBright = true; |
Jeff Brown | 30e5eb4 | 2012-05-11 19:18:56 -0700 | [diff] [blame] | 270 | gPowerManagerServiceObj = NULL; |
| 271 | gPowerModule = NULL; |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | } /* namespace android */ |