JWakeLock.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "JWakeLock"
  18. #include <utils/Log.h>
  19. #include "JWakeLock.h"
  20. #include <media/stagefright/foundation/ADebug.h>
  21. namespace android {
  22. JWakeLock::JWakeLock(const sp<JObjectHolder> &context) :
  23. mWakeLockCount(0),
  24. mWakeLock(NULL),
  25. mContext(context) {}
  26. JWakeLock::~JWakeLock() {
  27. clearJavaWakeLock();
  28. }
  29. bool JWakeLock::acquire() {
  30. if (mWakeLockCount == 0) {
  31. if (mWakeLock == NULL) {
  32. JNIEnv *env = JavaVMHelper::getJNIEnv();
  33. jclass jContextCls = env->FindClass("android/content/Context");
  34. jclass jPowerManagerCls = env->FindClass("android/os/PowerManager");
  35. jmethodID jGetSystemService = env->GetMethodID(jContextCls,
  36. "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
  37. jobject javaPowerManagerObj = env->CallObjectMethod(mContext->getJObject(),
  38. jGetSystemService, env->NewStringUTF("power"));
  39. jfieldID jPARTIAL_WAKE_LOCK = env->GetStaticFieldID(jPowerManagerCls,
  40. "PARTIAL_WAKE_LOCK", "I");
  41. jint PARTIAL_WAKE_LOCK = env->GetStaticIntField(jPowerManagerCls, jPARTIAL_WAKE_LOCK);
  42. jmethodID jNewWakeLock = env->GetMethodID(jPowerManagerCls,
  43. "newWakeLock", "(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;");
  44. jobject javaWakeLock = env->CallObjectMethod(javaPowerManagerObj,
  45. jNewWakeLock, PARTIAL_WAKE_LOCK, env->NewStringUTF("JWakeLock"));
  46. mWakeLock = new JObjectHolder(javaWakeLock);
  47. env->DeleteLocalRef(javaPowerManagerObj);
  48. env->DeleteLocalRef(javaWakeLock);
  49. }
  50. if (mWakeLock != NULL) {
  51. JNIEnv *env = JavaVMHelper::getJNIEnv();
  52. jclass wakeLockCls = env->FindClass("android/os/PowerManager$WakeLock");
  53. jmethodID jAcquire = env->GetMethodID(wakeLockCls, "acquire", "()V");
  54. env->CallVoidMethod(mWakeLock->getJObject(), jAcquire);
  55. mWakeLockCount++;
  56. return true;
  57. }
  58. } else {
  59. mWakeLockCount++;
  60. return true;
  61. }
  62. return false;
  63. }
  64. void JWakeLock::release(bool force) {
  65. if (mWakeLockCount == 0) {
  66. return;
  67. }
  68. if (force) {
  69. // Force wakelock release below by setting reference count to 1.
  70. mWakeLockCount = 1;
  71. }
  72. if (--mWakeLockCount == 0) {
  73. if (mWakeLock != NULL) {
  74. JNIEnv *env = JavaVMHelper::getJNIEnv();
  75. jclass wakeLockCls = env->FindClass("android/os/PowerManager$WakeLock");
  76. jmethodID jRelease = env->GetMethodID(wakeLockCls, "release", "()V");
  77. env->CallVoidMethod(mWakeLock->getJObject(), jRelease);
  78. }
  79. }
  80. }
  81. void JWakeLock::clearJavaWakeLock() {
  82. release(true);
  83. }
  84. } // namespace android