Merge "Guard __AActivityManager_* APIs with __builtin_available"
diff --git a/media/libmediatranscoding/TranscodingClientManager.cpp b/media/libmediatranscoding/TranscodingClientManager.cpp
index c0cc862..09afb1f 100644
--- a/media/libmediatranscoding/TranscodingClientManager.cpp
+++ b/media/libmediatranscoding/TranscodingClientManager.cpp
@@ -302,11 +302,13 @@
     }
 
     int32_t result;
-    if (APermissionManager_checkPermission("android.permission.WRITE_MEDIA_STORAGE", pid, uid,
-                                           &result) == PERMISSION_MANAGER_STATUS_OK &&
-        result == PERMISSION_MANAGER_PERMISSION_GRANTED) {
-        mTrustedUids.insert(uid);
-        return true;
+    if (__builtin_available(android 31, *)) {
+        if (APermissionManager_checkPermission("android.permission.WRITE_MEDIA_STORAGE", pid, uid,
+                                               &result) == PERMISSION_MANAGER_STATUS_OK &&
+            result == PERMISSION_MANAGER_PERMISSION_GRANTED) {
+            mTrustedUids.insert(uid);
+            return true;
+        }
     }
 
     return false;
diff --git a/media/libmediatranscoding/TranscodingUidPolicy.cpp b/media/libmediatranscoding/TranscodingUidPolicy.cpp
index a725387..b0fa545 100644
--- a/media/libmediatranscoding/TranscodingUidPolicy.cpp
+++ b/media/libmediatranscoding/TranscodingUidPolicy.cpp
@@ -48,8 +48,9 @@
 }
 
 void TranscodingUidPolicy::registerSelf() {
-    mUidObserver = AActivityManager_addUidImportanceListener(
-            &OnUidImportance, -1, (void*)this);
+    if (__builtin_available(android 31, *)) {
+        mUidObserver = AActivityManager_addUidImportanceListener(&OnUidImportance, -1, (void*)this);
+    }
 
     if (mUidObserver == nullptr) {
         ALOGE("Failed to register uid observer");
@@ -62,12 +63,16 @@
 }
 
 void TranscodingUidPolicy::unregisterSelf() {
-    AActivityManager_removeUidImportanceListener(mUidObserver);
-    mUidObserver = nullptr;
+    if (__builtin_available(android 31, *)) {
+        AActivityManager_removeUidImportanceListener(mUidObserver);
+        mUidObserver = nullptr;
 
-    Mutex::Autolock _l(mUidLock);
-    mRegistered = false;
-    ALOGI("Unregistered uid observer");
+        Mutex::Autolock _l(mUidLock);
+        mRegistered = false;
+        ALOGI("Unregistered uid observer");
+    } else {
+        ALOGE("Failed to unregister uid observer");
+    }
 }
 
 void TranscodingUidPolicy::setCallback(const std::shared_ptr<UidPolicyCallbackInterface>& cb) {
@@ -86,8 +91,10 @@
     }
 
     int32_t state = IMPORTANCE_UNKNOWN;
-    if (mRegistered && AActivityManager_isUidActive(uid)) {
-        state = AActivityManager_getUidImportance(uid);
+    if (__builtin_available(android 31, *)) {
+        if (mRegistered && AActivityManager_isUidActive(uid)) {
+            state = AActivityManager_getUidImportance(uid);
+        }
     }
 
     ALOGV("%s: inserting new uid: %u, procState %d", __FUNCTION__, uid, state);
diff --git a/services/mediatranscoding/MediaTranscodingService.cpp b/services/mediatranscoding/MediaTranscodingService.cpp
index 64def5e..74477a8 100644
--- a/services/mediatranscoding/MediaTranscodingService.cpp
+++ b/services/mediatranscoding/MediaTranscodingService.cpp
@@ -62,16 +62,18 @@
 
     uid_t callingUid = AIBinder_getCallingUid();
     pid_t callingPid = AIBinder_getCallingPid();
-    int32_t permissionResult;
-    if (APermissionManager_checkPermission("android.permission.DUMP", callingPid, callingUid,
-                                           &permissionResult) != PERMISSION_MANAGER_STATUS_OK ||
-        permissionResult != PERMISSION_MANAGER_PERMISSION_GRANTED) {
-        result.format(
-                "Permission Denial: "
-                "can't dump MediaTranscodingService from pid=%d, uid=%d\n",
-                AIBinder_getCallingPid(), AIBinder_getCallingUid());
-        write(fd, result.string(), result.size());
-        return PERMISSION_DENIED;
+    if (__builtin_available(android 31, *)) {
+        int32_t permissionResult;
+        if (APermissionManager_checkPermission("android.permission.DUMP", callingPid, callingUid,
+                                               &permissionResult) != PERMISSION_MANAGER_STATUS_OK ||
+            permissionResult != PERMISSION_MANAGER_PERMISSION_GRANTED) {
+            result.format(
+                    "Permission Denial: "
+                    "can't dump MediaTranscodingService from pid=%d, uid=%d\n",
+                    AIBinder_getCallingPid(), AIBinder_getCallingUid());
+            write(fd, result.string(), result.size());
+            return PERMISSION_DENIED;
+        }
     }
 
     const size_t SIZE = 256;