1 /* //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>
23 #include <sys/reboot.h>
24
25 namespace android
26 {
27
throw_NullPointerException(JNIEnv * env,const char * msg)28 static void throw_NullPointerException(JNIEnv *env, const char* msg)
29 {
30 jclass clazz;
31 clazz = env->FindClass("java/lang/NullPointerException");
32 env->ThrowNew(clazz, msg);
33 }
34
35 static void
acquireWakeLock(JNIEnv * env,jobject clazz,jint lock,jstring idObj)36 acquireWakeLock(JNIEnv *env, jobject clazz, jint lock, jstring idObj)
37 {
38 if (idObj == NULL) {
39 throw_NullPointerException(env, "id is null");
40 return ;
41 }
42
43 const char *id = env->GetStringUTFChars(idObj, NULL);
44
45 acquire_wake_lock(lock, id);
46
47 env->ReleaseStringUTFChars(idObj, id);
48 }
49
50 static void
releaseWakeLock(JNIEnv * env,jobject clazz,jstring idObj)51 releaseWakeLock(JNIEnv *env, jobject clazz, jstring idObj)
52 {
53 if (idObj == NULL) {
54 throw_NullPointerException(env, "id is null");
55 return ;
56 }
57
58 const char *id = env->GetStringUTFChars(idObj, NULL);
59
60 release_wake_lock(id);
61
62 env->ReleaseStringUTFChars(idObj, id);
63
64 }
65
66 static int
setLastUserActivityTimeout(JNIEnv * env,jobject clazz,jlong timeMS)67 setLastUserActivityTimeout(JNIEnv *env, jobject clazz, jlong timeMS)
68 {
69 return set_last_user_activity_timeout(timeMS/1000);
70 }
71
72 static int
setScreenState(JNIEnv * env,jobject clazz,jboolean on)73 setScreenState(JNIEnv *env, jobject clazz, jboolean on)
74 {
75 return set_screen_state(on);
76 }
77
android_os_Power_shutdown(JNIEnv * env,jobject clazz)78 static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
79 {
80 sync();
81 #ifdef HAVE_ANDROID_OS
82 reboot(RB_POWER_OFF);
83 #endif
84 }
85
android_os_Power_reboot(JNIEnv * env,jobject clazz,jstring reason)86 static void android_os_Power_reboot(JNIEnv *env, jobject clazz, jstring reason)
87 {
88 sync();
89 #ifdef HAVE_ANDROID_OS
90 if (reason == NULL) {
91 reboot(RB_AUTOBOOT);
92 } else {
93 const char *chars = env->GetStringUTFChars(reason, NULL);
94 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
95 LINUX_REBOOT_CMD_RESTART2, (char*) chars);
96 env->ReleaseStringUTFChars(reason, chars); // In case it fails.
97 }
98 jniThrowIOException(env, errno);
99 #endif
100 }
101
102 static JNINativeMethod method_table[] = {
103 { "acquireWakeLock", "(ILjava/lang/String;)V", (void*)acquireWakeLock },
104 { "releaseWakeLock", "(Ljava/lang/String;)V", (void*)releaseWakeLock },
105 { "setLastUserActivityTimeout", "(J)I", (void*)setLastUserActivityTimeout },
106 { "setScreenState", "(Z)I", (void*)setScreenState },
107 { "shutdown", "()V", (void*)android_os_Power_shutdown },
108 { "reboot", "(Ljava/lang/String;)V", (void*)android_os_Power_reboot },
109 };
110
register_android_os_Power(JNIEnv * env)111 int register_android_os_Power(JNIEnv *env)
112 {
113 return AndroidRuntime::registerNativeMethods(
114 env, "android/os/Power",
115 method_table, NELEM(method_table));
116 }
117
118 };
119