• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 
17 #include <jni.h>
18 #include <binder/AppOpsManager.h>
19 #include <utils/String16.h>
20 
21 using namespace android;
22 
23 #include "android/log.h"
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27 #define LOG_TAG "AppOpsLoggingTest"
28 
29 // Note op from native code
30 extern "C" JNIEXPORT void JNICALL
Java_android_app_AppOpsLoggingTestKt_nativeNoteOp(JNIEnv * env,jobject obj,jint op,jint uid,jstring jCallingPackageName,jstring jAttributionTag,jstring jMessage)31 Java_android_app_AppOpsLoggingTestKt_nativeNoteOp(JNIEnv* env, jobject obj,
32         jint op, jint uid, jstring jCallingPackageName, jstring jAttributionTag, jstring jMessage) {
33     AppOpsManager appOpsManager;
34 
35     const char *nativeCallingPackageName = env->GetStringUTFChars(jCallingPackageName, 0);
36     String16 callingPackageName(nativeCallingPackageName);
37 
38     const char *nativeAttributionTag;
39     std::optional<String16> attributionTag;
40     if (jAttributionTag != nullptr) {
41         nativeAttributionTag = env->GetStringUTFChars(jAttributionTag, 0);
42         attributionTag = String16(nativeAttributionTag);
43     }
44 
45     const char *nativeMessage;
46     String16 message;
47     if (jMessage != nullptr) {
48         nativeMessage = env->GetStringUTFChars(jMessage, 0);
49         message = String16(nativeMessage);
50     }
51 
52     appOpsManager.noteOp(op, uid, callingPackageName, attributionTag, message);
53 
54     env->ReleaseStringUTFChars(jCallingPackageName, nativeCallingPackageName);
55 
56     if (jAttributionTag != nullptr) {
57         env->ReleaseStringUTFChars(jAttributionTag, nativeAttributionTag);
58     }
59 
60     if (jMessage != nullptr) {
61         env->ReleaseStringUTFChars(jMessage, nativeMessage);
62     }
63 }
64