The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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> |
Ken Sumrall | 0da47da | 2011-03-07 23:42:39 -0800 | [diff] [blame] | 23 | #include <cutils/android_reboot.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android |
| 26 | { |
| 27 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | static void |
| 29 | acquireWakeLock(JNIEnv *env, jobject clazz, jint lock, jstring idObj) |
| 30 | { |
| 31 | if (idObj == NULL) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 32 | jniThrowNullPointerException(env, "id is null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
| 44 | releaseWakeLock(JNIEnv *env, jobject clazz, jstring idObj) |
| 45 | { |
| 46 | if (idObj == NULL) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 47 | jniThrowNullPointerException(env, "id is null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
| 60 | setLastUserActivityTimeout(JNIEnv *env, jobject clazz, jlong timeMS) |
| 61 | { |
| 62 | return set_last_user_activity_timeout(timeMS/1000); |
| 63 | } |
| 64 | |
| 65 | static int |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | setScreenState(JNIEnv *env, jobject clazz, jboolean on) |
| 67 | { |
| 68 | return set_screen_state(on); |
| 69 | } |
| 70 | |
| 71 | static void android_os_Power_shutdown(JNIEnv *env, jobject clazz) |
| 72 | { |
Ken Sumrall | 0da47da | 2011-03-07 23:42:39 -0800 | [diff] [blame] | 73 | android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void android_os_Power_reboot(JNIEnv *env, jobject clazz, jstring reason) |
| 77 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | if (reason == NULL) { |
Ken Sumrall | 0da47da | 2011-03-07 23:42:39 -0800 | [diff] [blame] | 79 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } else { |
| 81 | const char *chars = env->GetStringUTFChars(reason, NULL); |
Ken Sumrall | 0da47da | 2011-03-07 23:42:39 -0800 | [diff] [blame] | 82 | android_reboot(ANDROID_RB_RESTART2, 0, (char *) chars); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | env->ReleaseStringUTFChars(reason, chars); // In case it fails. |
| 84 | } |
| 85 | jniThrowIOException(env, errno); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | { "setScreenState", "(Z)I", (void*)setScreenState }, |
| 93 | { "shutdown", "()V", (void*)android_os_Power_shutdown }, |
San Mehat | 7ebf017 | 2010-01-12 07:57:42 -0800 | [diff] [blame] | 94 | { "rebootNative", "(Ljava/lang/String;)V", (void*)android_os_Power_reboot }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
| 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 | }; |