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 <limits.h> 25 26 #include <android_runtime/AndroidRuntime.h> 27 #include <utils/Timers.h> 28 #include <surfaceflinger/ISurfaceComposer.h> 29 #include <surfaceflinger/SurfaceComposerClient.h> 30 31 #include "com_android_server_PowerManagerService.h" 32 33 namespace android { 34 35 // ---------------------------------------------------------------------------- 36 37 static struct { 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 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 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) { 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 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 125 static void android_server_PowerManagerService_nativeStartSurfaceFlingerAnimation(JNIEnv* env, 126 jobject obj, jint mode) { 127 sp<ISurfaceComposer> s(ComposerService::getComposerService()); 128 s->turnElectronBeamOff(mode); 129 } 130 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 }, 139 { "nativeStartSurfaceFlingerAnimation", "(I)V", 140 (void*) android_server_PowerManagerService_nativeStartSurfaceFlingerAnimation }, 141 }; 142 143 #define FIND_CLASS(var, className) \ 144 var = env->FindClass(className); \ 145 LOG_FATAL_IF(! var, "Unable to find class " className); 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 162 jclass clazz; 163 FIND_CLASS(clazz, "com/android/server/PowerManagerService"); 164 165 GET_METHOD_ID(gPowerManagerServiceClassInfo.goToSleep, clazz, 166 "goToSleep", "(J)V"); 167 168 GET_METHOD_ID(gPowerManagerServiceClassInfo.userActivity, clazz, 169 "userActivity", "(JZIZ)V"); 170 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; 177 return 0; 178 } 179 180 } /* namespace android */ 181