blob: a389c114752ffa5adda5094c62a8524e10187639 [file] [log] [blame]
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001/*
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 Onoratob08a1af2010-10-11 19:28:58 -070023
Jeff Brown00fa7bd2010-07-02 15:37:36 -070024#include <limits.h>
Joe Onoratob08a1af2010-10-11 19:28:58 -070025
Jeff Brown00fa7bd2010-07-02 15:37:36 -070026#include <android_runtime/AndroidRuntime.h>
Jeff Brown90291572010-08-16 14:19:45 -070027#include <utils/Timers.h>
Joe Onoratob08a1af2010-10-11 19:28:58 -070028#include <surfaceflinger/ISurfaceComposer.h>
29#include <surfaceflinger/SurfaceComposerClient.h>
30
Jeff Brown00fa7bd2010-07-02 15:37:36 -070031#include "com_android_server_PowerManagerService.h"
32
33namespace android {
34
35// ----------------------------------------------------------------------------
36
37static struct {
Jeff Brown00fa7bd2010-07-02 15:37:36 -070038 jmethodID goToSleep;
39 jmethodID userActivity;
40} gPowerManagerServiceClassInfo;
41
42// ----------------------------------------------------------------------------
43
44static jobject gPowerManagerServiceObj;
45
46static Mutex gPowerManagerLock;
47static bool gScreenOn;
48static bool gScreenBright;
49
Jeff Brown90291572010-08-16 14:19:45 -070050static nsecs_t gLastEventTime[POWER_MANAGER_LAST_EVENT + 1];
51
52// Throttling interval for user activity calls.
53static const nsecs_t MIN_TIME_BETWEEN_USERACTIVITIES = 500 * 1000000L; // 500ms
54
Jeff Brown00fa7bd2010-07-02 15:37:36 -070055// ----------------------------------------------------------------------------
56
57static 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
67bool android_server_PowerManagerService_isScreenOn() {
68 AutoMutex _l(gPowerManagerLock);
69 return gScreenOn;
70}
71
72bool android_server_PowerManagerService_isScreenBright() {
73 AutoMutex _l(gPowerManagerLock);
74 return gScreenBright;
75}
76
77void android_server_PowerManagerService_userActivity(nsecs_t eventTime, int32_t eventType) {
78 if (gPowerManagerServiceObj) {
Jeff Brown90291572010-08-16 14:19:45 -070079 // 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 Brown00fa7bd2010-07-02 15:37:36 -070094 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
102void 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
114static void android_server_PowerManagerService_nativeInit(JNIEnv* env, jobject obj) {
115 gPowerManagerServiceObj = env->NewGlobalRef(obj);
116}
117
118static 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 Onoratob08a1af2010-10-11 19:28:58 -0700125static void android_server_PowerManagerService_nativeStartSurfaceFlingerAnimation(JNIEnv* env,
Joe Onorato609695d2010-10-14 14:57:49 -0700126 jobject obj, jint mode) {
Joe Onoratob08a1af2010-10-11 19:28:58 -0700127 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Joe Onorato609695d2010-10-14 14:57:49 -0700128 s->turnElectronBeamOff(mode);
Joe Onoratob08a1af2010-10-11 19:28:58 -0700129}
130
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700131// ----------------------------------------------------------------------------
132
133static 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 Onorato609695d2010-10-14 14:57:49 -0700139 { "nativeStartSurfaceFlingerAnimation", "(I)V",
Joe Onoratob08a1af2010-10-11 19:28:58 -0700140 (void*) android_server_PowerManagerService_nativeStartSurfaceFlingerAnimation },
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700141};
142
143#define FIND_CLASS(var, className) \
144 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800145 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700146
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
155int 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 Shapiro17cc33a2011-03-05 20:53:16 -0800162 jclass clazz;
163 FIND_CLASS(clazz, "com/android/server/PowerManagerService");
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700164
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800165 GET_METHOD_ID(gPowerManagerServiceClassInfo.goToSleep, clazz,
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700166 "goToSleep", "(J)V");
167
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800168 GET_METHOD_ID(gPowerManagerServiceClassInfo.userActivity, clazz,
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700169 "userActivity", "(JZIZ)V");
170
Jeff Brown90291572010-08-16 14:19:45 -0700171 // 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 Brown00fa7bd2010-07-02 15:37:36 -0700177 return 0;
178}
179
180} /* namespace android */