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 <cutils/android_reboot.h>
24
25 namespace android
26 {
27
28 static void
acquireWakeLock(JNIEnv * env,jobject clazz,jint lock,jstring idObj)29 acquireWakeLock(JNIEnv *env, jobject clazz, jint lock, jstring idObj)
30 {
31 if (idObj == NULL) {
32 jniThrowNullPointerException(env, "id is null");
33 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
43 static void
releaseWakeLock(JNIEnv * env,jobject clazz,jstring idObj)44 releaseWakeLock(JNIEnv *env, jobject clazz, jstring idObj)
45 {
46 if (idObj == NULL) {
47 jniThrowNullPointerException(env, "id is null");
48 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
59 static int
setLastUserActivityTimeout(JNIEnv * env,jobject clazz,jlong timeMS)60 setLastUserActivityTimeout(JNIEnv *env, jobject clazz, jlong timeMS)
61 {
62 return set_last_user_activity_timeout(timeMS/1000);
63 }
64
65 static int
setScreenState(JNIEnv * env,jobject clazz,jboolean on)66 setScreenState(JNIEnv *env, jobject clazz, jboolean on)
67 {
68 return set_screen_state(on);
69 }
70
android_os_Power_shutdown(JNIEnv * env,jobject clazz)71 static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
72 {
73 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
74 }
75
android_os_Power_reboot(JNIEnv * env,jobject clazz,jstring reason)76 static void android_os_Power_reboot(JNIEnv *env, jobject clazz, jstring reason)
77 {
78 if (reason == NULL) {
79 android_reboot(ANDROID_RB_RESTART, 0, 0);
80 } else {
81 const char *chars = env->GetStringUTFChars(reason, NULL);
82 android_reboot(ANDROID_RB_RESTART2, 0, (char *) chars);
83 env->ReleaseStringUTFChars(reason, chars); // In case it fails.
84 }
85 jniThrowIOException(env, errno);
86 }
87
88 static 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 },
92 { "setScreenState", "(Z)I", (void*)setScreenState },
93 { "shutdown", "()V", (void*)android_os_Power_shutdown },
94 { "rebootNative", "(Ljava/lang/String;)V", (void*)android_os_Power_reboot },
95 };
96
register_android_os_Power(JNIEnv * env)97 int 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 };
105