blob: dc1699089e3849816362c99421dac24626cc2158 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/libs/android_runtime/android_os_Power.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include "JNIHelp.h"
19#include "jni.h"
20#include "android_runtime/AndroidRuntime.h"
21#include <utils/misc.h>
22#include <hardware_legacy/power.h>
Ken Sumrall0da47da2011-03-07 23:42:39 -080023#include <cutils/android_reboot.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25namespace android
26{
27
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028static void
29acquireWakeLock(JNIEnv *env, jobject clazz, jint lock, jstring idObj)
30{
31 if (idObj == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -070032 jniThrowNullPointerException(env, "id is null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 return ;
34 }
35
36 const char *id = env->GetStringUTFChars(idObj, NULL);
37
38 acquire_wake_lock(lock, id);
39
40 env->ReleaseStringUTFChars(idObj, id);
41}
42
43static void
44releaseWakeLock(JNIEnv *env, jobject clazz, jstring idObj)
45{
46 if (idObj == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -070047 jniThrowNullPointerException(env, "id is null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 return ;
49 }
50
51 const char *id = env->GetStringUTFChars(idObj, NULL);
52
53 release_wake_lock(id);
54
55 env->ReleaseStringUTFChars(idObj, id);
56
57}
58
59static int
60setLastUserActivityTimeout(JNIEnv *env, jobject clazz, jlong timeMS)
61{
62 return set_last_user_activity_timeout(timeMS/1000);
63}
64
65static int
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066setScreenState(JNIEnv *env, jobject clazz, jboolean on)
67{
68 return set_screen_state(on);
69}
70
71static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
72{
Ken Sumrall0da47da2011-03-07 23:42:39 -080073 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074}
75
76static void android_os_Power_reboot(JNIEnv *env, jobject clazz, jstring reason)
77{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 if (reason == NULL) {
Ken Sumrall0da47da2011-03-07 23:42:39 -080079 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 } else {
81 const char *chars = env->GetStringUTFChars(reason, NULL);
Ken Sumrall0da47da2011-03-07 23:42:39 -080082 android_reboot(ANDROID_RB_RESTART2, 0, (char *) chars);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 env->ReleaseStringUTFChars(reason, chars); // In case it fails.
84 }
85 jniThrowIOException(env, errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086}
87
88static JNINativeMethod method_table[] = {
89 { "acquireWakeLock", "(ILjava/lang/String;)V", (void*)acquireWakeLock },
90 { "releaseWakeLock", "(Ljava/lang/String;)V", (void*)releaseWakeLock },
91 { "setLastUserActivityTimeout", "(J)I", (void*)setLastUserActivityTimeout },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 { "setScreenState", "(Z)I", (void*)setScreenState },
93 { "shutdown", "()V", (void*)android_os_Power_shutdown },
San Mehat7ebf0172010-01-12 07:57:42 -080094 { "rebootNative", "(Ljava/lang/String;)V", (void*)android_os_Power_reboot },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095};
96
97int register_android_os_Power(JNIEnv *env)
98{
99 return AndroidRuntime::registerNativeMethods(
100 env, "android/os/Power",
101 method_table, NELEM(method_table));
102}
103
104};