com_android_server_power_PowerManagerService.cpp revision 037c33eae74bee2774897d969d48947f9abe254f
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 userActivityFromNative;
46} gPowerManagerServiceClassInfo;
47
48// ----------------------------------------------------------------------------
49
50static jobject gPowerManagerServiceObj;
51static struct power_module* gPowerModule;
52
53static nsecs_t gLastEventTime[USER_ACTIVITY_EVENT_LAST + 1];
54
55// Throttling interval for user activity calls.
56static const nsecs_t MIN_TIME_BETWEEN_USERACTIVITIES = 500 * 1000000L; // 500ms
57
58// ----------------------------------------------------------------------------
59
60static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
61    if (env->ExceptionCheck()) {
62        ALOGE("An exception was thrown by callback '%s'.", methodName);
63        LOGE_EX(env);
64        env->ExceptionClear();
65        return true;
66    }
67    return false;
68}
69
70void android_server_PowerManagerService_userActivity(nsecs_t eventTime, int32_t eventType) {
71    // Tell the power HAL when user activity occurs.
72    if (gPowerModule && gPowerModule->powerHint) {
73        gPowerModule->powerHint(gPowerModule, POWER_HINT_INTERACTION, NULL);
74    }
75
76    if (gPowerManagerServiceObj) {
77        // Throttle calls into user activity by event type.
78        // We're a little conservative about argument checking here in case the caller
79        // passes in bad data which could corrupt system state.
80        if (eventType >= 0 && eventType <= USER_ACTIVITY_EVENT_LAST) {
81            nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
82            if (eventTime > now) {
83                eventTime = now;
84            }
85
86            if (gLastEventTime[eventType] + MIN_TIME_BETWEEN_USERACTIVITIES > eventTime) {
87                return;
88            }
89            gLastEventTime[eventType] = eventTime;
90        }
91
92        JNIEnv* env = AndroidRuntime::getJNIEnv();
93
94        env->CallVoidMethod(gPowerManagerServiceObj,
95                gPowerManagerServiceClassInfo.userActivityFromNative,
96                nanoseconds_to_milliseconds(eventTime), eventType, 0);
97        checkAndClearExceptionFromCallback(env, "userActivityFromNative");
98    }
99}
100
101// ----------------------------------------------------------------------------
102
103static void nativeInit(JNIEnv* env, jobject obj) {
104    gPowerManagerServiceObj = env->NewGlobalRef(obj);
105
106    status_t err = hw_get_module(POWER_HARDWARE_MODULE_ID,
107            (hw_module_t const**)&gPowerModule);
108    if (!err) {
109        gPowerModule->init(gPowerModule);
110    } else {
111        ALOGE("Couldn't load %s module (%s)", POWER_HARDWARE_MODULE_ID, strerror(-err));
112    }
113}
114
115static void nativeAcquireSuspendBlocker(JNIEnv *env, jclass clazz, jstring nameStr) {
116    ScopedUtfChars name(env, nameStr);
117    acquire_wake_lock(PARTIAL_WAKE_LOCK, name.c_str());
118}
119
120static void nativeReleaseSuspendBlocker(JNIEnv *env, jclass clazz, jstring nameStr) {
121    ScopedUtfChars name(env, nameStr);
122    release_wake_lock(name.c_str());
123}
124
125static void nativeSetInteractive(JNIEnv *env, jclass clazz, jboolean enable) {
126    if (gPowerModule) {
127        if (enable) {
128            ALOGD_IF_SLOW(20, "Excessive delay in setInteractive(true) while turning screen on");
129            gPowerModule->setInteractive(gPowerModule, true);
130        } else {
131            ALOGD_IF_SLOW(20, "Excessive delay in setInteractive(false) while turning screen off");
132            gPowerModule->setInteractive(gPowerModule, false);
133        }
134    }
135}
136
137static void nativeSetAutoSuspend(JNIEnv *env, jclass clazz, jboolean enable) {
138    if (enable) {
139        ALOGD_IF_SLOW(100, "Excessive delay in autosuspend_enable() while turning screen off");
140        autosuspend_enable();
141    } else {
142        ALOGD_IF_SLOW(100, "Excessive delay in autosuspend_disable() while turning screen on");
143        autosuspend_disable();
144    }
145}
146
147// ----------------------------------------------------------------------------
148
149static JNINativeMethod gPowerManagerServiceMethods[] = {
150    /* name, signature, funcPtr */
151    { "nativeInit", "()V",
152            (void*) nativeInit },
153    { "nativeAcquireSuspendBlocker", "(Ljava/lang/String;)V",
154            (void*) nativeAcquireSuspendBlocker },
155    { "nativeReleaseSuspendBlocker", "(Ljava/lang/String;)V",
156            (void*) nativeReleaseSuspendBlocker },
157    { "nativeSetInteractive", "(Z)V",
158            (void*) nativeSetInteractive },
159    { "nativeSetAutoSuspend", "(Z)V",
160            (void*) nativeSetAutoSuspend },
161};
162
163#define FIND_CLASS(var, className) \
164        var = env->FindClass(className); \
165        LOG_FATAL_IF(! var, "Unable to find class " className);
166
167#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
168        var = env->GetMethodID(clazz, methodName, methodDescriptor); \
169        LOG_FATAL_IF(! var, "Unable to find method " methodName);
170
171#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
172        var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
173        LOG_FATAL_IF(! var, "Unable to find field " fieldName);
174
175int register_android_server_PowerManagerService(JNIEnv* env) {
176    int res = jniRegisterNativeMethods(env, "com/android/server/power/PowerManagerService",
177            gPowerManagerServiceMethods, NELEM(gPowerManagerServiceMethods));
178    LOG_FATAL_IF(res < 0, "Unable to register native methods.");
179
180    // Callbacks
181
182    jclass clazz;
183    FIND_CLASS(clazz, "com/android/server/power/PowerManagerService");
184
185    GET_METHOD_ID(gPowerManagerServiceClassInfo.userActivityFromNative, clazz,
186            "userActivityFromNative", "(JII)V");
187
188    // Initialize
189    for (int i = 0; i <= USER_ACTIVITY_EVENT_LAST; i++) {
190        gLastEventTime[i] = LLONG_MIN;
191    }
192    gPowerManagerServiceObj = NULL;
193    gPowerModule = NULL;
194    return 0;
195}
196
197} /* namespace android */
198