Skip to content

Instantly share code, notes, and snippets.

@AchrafAmil
Created June 23, 2020 11:50
Show Gist options
  • Save AchrafAmil/3674b147381142143574e70a8f38e845 to your computer and use it in GitHub Desktop.
Save AchrafAmil/3674b147381142143574e70a8f38e845 to your computer and use it in GitHub Desktop.
Android's AppOpsManager.OnOpNotedCallback implementation to log sensitive data access ops
import android.app.AppOpsManager
import android.app.AsyncNotedAppOp
import android.app.SyncNotedAppOp
import android.content.Context
import android.util.Log
import androidx.annotation.RequiresApi
@RequiresApi(30)
class AppOpsCallback(private val context: Context) : AppOpsManager.OnOpNotedCallback() {
override fun onNoted(syncNotedAppOp: SyncNotedAppOp) {
Log.i(
LOG_TAG,
"Sync data access by a foreign (or unknown) UID. Operation: ${syncNotedAppOp.op} " +
"- attribution tag: ${syncNotedAppOp.attributionTag} "
)
Log.v(LOG_TAG, "stacktrace: ", Throwable())
}
override fun onSelfNoted(syncNotedAppOp: SyncNotedAppOp) {
Log.i(
LOG_TAG,
"Sync data access by self UID. Operation: ${syncNotedAppOp.op} " +
"- attribution tag: ${syncNotedAppOp.attributionTag} "
)
Log.v(LOG_TAG, "stacktrace: ", Throwable())
}
override fun onAsyncNoted(asyncNotedAppOp: AsyncNotedAppOp) {
val packageNames = context.packageManager.getPackagesForUid(asyncNotedAppOp.notingUid)
Log.i(
LOG_TAG,
"Async data access " +
"- Operation: ${asyncNotedAppOp.op} " +
"- From UID: ${asyncNotedAppOp.notingUid} (${packageNames?.contentDeepToString()}) " +
"- attribution tag: ${asyncNotedAppOp.attributionTag} " +
"- Message: ${asyncNotedAppOp.message}"
)
}
companion object {
private const val LOG_TAG = "AppOpsCallback"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment