blob: 5cfb9b1f6fcb6788b32317d395ef721a853a9d9a [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
28static 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
35static void
36acquireWakeLock(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
50static void
51releaseWakeLock(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
66static int
67setLastUserActivityTimeout(JNIEnv *env, jobject clazz, jlong timeMS)
68{
69 return set_last_user_activity_timeout(timeMS/1000);
70}
71
72static int
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073setScreenState(JNIEnv *env, jobject clazz, jboolean on)
74{
75 return set_screen_state(on);
76}
77
78static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
79{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080#ifdef HAVE_ANDROID_OS
Ken Sumrall0da47da2011-03-07 23:42:39 -080081 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
82#else
83 sync();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084#endif
85}
86
87static void android_os_Power_reboot(JNIEnv *env, jobject clazz, jstring reason)
88{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089#ifdef HAVE_ANDROID_OS
90 if (reason == NULL) {
Ken Sumrall0da47da2011-03-07 23:42:39 -080091 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 } else {
93 const char *chars = env->GetStringUTFChars(reason, NULL);
Ken Sumrall0da47da2011-03-07 23:42:39 -080094 android_reboot(ANDROID_RB_RESTART2, 0, (char *) chars);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 env->ReleaseStringUTFChars(reason, chars); // In case it fails.
96 }
97 jniThrowIOException(env, errno);
Ken Sumrall0da47da2011-03-07 23:42:39 -080098#else
99 sync();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100#endif
101}
102
103static JNINativeMethod method_table[] = {
104 { "acquireWakeLock", "(ILjava/lang/String;)V", (void*)acquireWakeLock },
105 { "releaseWakeLock", "(Ljava/lang/String;)V", (void*)releaseWakeLock },
106 { "setLastUserActivityTimeout", "(J)I", (void*)setLastUserActivityTimeout },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 { "setScreenState", "(Z)I", (void*)setScreenState },
108 { "shutdown", "()V", (void*)android_os_Power_shutdown },
San Mehat7ebf0172010-01-12 07:57:42 -0800109 { "rebootNative", "(Ljava/lang/String;)V", (void*)android_os_Power_reboot },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110};
111
112int register_android_os_Power(JNIEnv *env)
113{
114 return AndroidRuntime::registerNativeMethods(
115 env, "android/os/Power",
116 method_table, NELEM(method_table));
117}
118
119};