JObjectHolder.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright 2018, 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. #ifndef JOBJECT_HOLDER_H_
  17. #define JOBJECT_HOLDER_H_
  18. #include "jni.h"
  19. #include <mediaplayer2/JavaVMHelper.h>
  20. #include <utils/RefBase.h>
  21. namespace android {
  22. // Helper class for managing global reference of jobject.
  23. struct JObjectHolder : public RefBase {
  24. JObjectHolder(jobject obj) {
  25. JNIEnv *env = JavaVMHelper::getJNIEnv();
  26. mJObject = reinterpret_cast<jobject>(env->NewGlobalRef(obj));
  27. }
  28. virtual ~JObjectHolder() {
  29. JNIEnv *env = JavaVMHelper::getJNIEnv();
  30. env->DeleteGlobalRef(mJObject);
  31. }
  32. jobject getJObject() { return mJObject; }
  33. private:
  34. jobject mJObject;
  35. };
  36. } //" android
  37. #endif // JOBJECT_HOLDER_H_