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> |
Jeff Brown | 9029157 | 2010-08-16 14:19:45 -0700 | [diff] [blame] | 27 | #include <utils/Timers.h> |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 28 | #include <surfaceflinger/ISurfaceComposer.h> |
| 29 | #include <surfaceflinger/SurfaceComposerClient.h> |
| 30 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 31 | #include "com_android_server_PowerManagerService.h" |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | static struct { |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 38 | jmethodID goToSleep; |
| 39 | jmethodID userActivity; |
| 40 | } gPowerManagerServiceClassInfo; |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | |
| 44 | static jobject gPowerManagerServiceObj; |
| 45 | |
| 46 | static Mutex gPowerManagerLock; |
| 47 | static bool gScreenOn; |
| 48 | static bool gScreenBright; |
| 49 | |
Jeff Brown | 9029157 | 2010-08-16 14:19:45 -0700 | [diff] [blame] | 50 | static nsecs_t gLastEventTime[POWER_MANAGER_LAST_EVENT + 1]; |
| 51 | |
| 52 | // Throttling interval for user activity calls. |
| 53 | static const nsecs_t MIN_TIME_BETWEEN_USERACTIVITIES = 500 * 1000000L; // 500ms |
| 54 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 55 | // ---------------------------------------------------------------------------- |
| 56 | |
| 57 | static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 58 | if (env->ExceptionCheck()) { |
| 59 | LOGE("An exception was thrown by callback '%s'.", methodName); |
| 60 | LOGE_EX(env); |
| 61 | env->ExceptionClear(); |
| 62 | return true; |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | bool android_server_PowerManagerService_isScreenOn() { |
| 68 | AutoMutex _l(gPowerManagerLock); |
| 69 | return gScreenOn; |
| 70 | } |
| 71 | |
| 72 | bool android_server_PowerManagerService_isScreenBright() { |
| 73 | AutoMutex _l(gPowerManagerLock); |
| 74 | return gScreenBright; |
| 75 | } |
| 76 | |
| 77 | void android_server_PowerManagerService_userActivity(nsecs_t eventTime, int32_t eventType) { |
| 78 | if (gPowerManagerServiceObj) { |
Jeff Brown | 9029157 | 2010-08-16 14:19:45 -0700 | [diff] [blame] | 79 | // Throttle calls into user activity by event type. |
| 80 | // We're a little conservative about argument checking here in case the caller |
| 81 | // passes in bad data which could corrupt system state. |
| 82 | if (eventType >= 0 && eventType <= POWER_MANAGER_LAST_EVENT) { |
| 83 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 84 | if (eventTime > now) { |
| 85 | eventTime = now; |
| 86 | } |
| 87 | |
| 88 | if (gLastEventTime[eventType] + MIN_TIME_BETWEEN_USERACTIVITIES > eventTime) { |
| 89 | return; |
| 90 | } |
| 91 | gLastEventTime[eventType] = eventTime; |
| 92 | } |
| 93 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 94 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 95 | |
| 96 | env->CallVoidMethod(gPowerManagerServiceObj, gPowerManagerServiceClassInfo.userActivity, |
| 97 | nanoseconds_to_milliseconds(eventTime), false, eventType, false); |
| 98 | checkAndClearExceptionFromCallback(env, "userActivity"); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void android_server_PowerManagerService_goToSleep(nsecs_t eventTime) { |
| 103 | if (gPowerManagerServiceObj) { |
| 104 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 105 | |
| 106 | env->CallVoidMethod(gPowerManagerServiceObj, gPowerManagerServiceClassInfo.goToSleep, |
| 107 | nanoseconds_to_milliseconds(eventTime)); |
| 108 | checkAndClearExceptionFromCallback(env, "goToSleep"); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // ---------------------------------------------------------------------------- |
| 113 | |
| 114 | static void android_server_PowerManagerService_nativeInit(JNIEnv* env, jobject obj) { |
| 115 | gPowerManagerServiceObj = env->NewGlobalRef(obj); |
| 116 | } |
| 117 | |
| 118 | static void android_server_PowerManagerService_nativeSetPowerState(JNIEnv* env, |
| 119 | jobject serviceObj, jboolean screenOn, jboolean screenBright) { |
| 120 | AutoMutex _l(gPowerManagerLock); |
| 121 | gScreenOn = screenOn; |
| 122 | gScreenBright = screenBright; |
| 123 | } |
| 124 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 125 | static void android_server_PowerManagerService_nativeStartSurfaceFlingerAnimation(JNIEnv* env, |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 126 | jobject obj, jint mode) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 127 | sp<ISurfaceComposer> s(ComposerService::getComposerService()); |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 128 | s->turnElectronBeamOff(mode); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 131 | // ---------------------------------------------------------------------------- |
| 132 | |
| 133 | static JNINativeMethod gPowerManagerServiceMethods[] = { |
| 134 | /* name, signature, funcPtr */ |
| 135 | { "nativeInit", "()V", |
| 136 | (void*) android_server_PowerManagerService_nativeInit }, |
| 137 | { "nativeSetPowerState", "(ZZ)V", |
| 138 | (void*) android_server_PowerManagerService_nativeSetPowerState }, |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 139 | { "nativeStartSurfaceFlingerAnimation", "(I)V", |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 140 | (void*) android_server_PowerManagerService_nativeStartSurfaceFlingerAnimation }, |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | #define FIND_CLASS(var, className) \ |
| 144 | var = env->FindClass(className); \ |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 145 | LOG_FATAL_IF(! var, "Unable to find class " className); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 146 | |
| 147 | #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ |
| 148 | var = env->GetMethodID(clazz, methodName, methodDescriptor); \ |
| 149 | LOG_FATAL_IF(! var, "Unable to find method " methodName); |
| 150 | |
| 151 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 152 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 153 | LOG_FATAL_IF(! var, "Unable to find field " fieldName); |
| 154 | |
| 155 | int register_android_server_PowerManagerService(JNIEnv* env) { |
| 156 | int res = jniRegisterNativeMethods(env, "com/android/server/PowerManagerService", |
| 157 | gPowerManagerServiceMethods, NELEM(gPowerManagerServiceMethods)); |
| 158 | LOG_FATAL_IF(res < 0, "Unable to register native methods."); |
| 159 | |
| 160 | // Callbacks |
| 161 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 162 | jclass clazz; |
| 163 | FIND_CLASS(clazz, "com/android/server/PowerManagerService"); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 164 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 165 | GET_METHOD_ID(gPowerManagerServiceClassInfo.goToSleep, clazz, |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 166 | "goToSleep", "(J)V"); |
| 167 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 168 | GET_METHOD_ID(gPowerManagerServiceClassInfo.userActivity, clazz, |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 169 | "userActivity", "(JZIZ)V"); |
| 170 | |
Jeff Brown | 9029157 | 2010-08-16 14:19:45 -0700 | [diff] [blame] | 171 | // Initialize |
| 172 | for (int i = 0; i < POWER_MANAGER_LAST_EVENT; i++) { |
| 173 | gLastEventTime[i] = LLONG_MIN; |
| 174 | } |
| 175 | gScreenOn = true; |
| 176 | gScreenBright = true; |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | } /* namespace android */ |