1 /*
2 * Copyright (C) 2021 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 #define LOG_TAG "AppInfoFetcher"
18
19 #include "AppInfoFetcher.h"
20
21 #include <Utils.h>
22 #include <android-base/file.h>
23 #include <android-base/properties.h>
24 #include <binder/IServiceManager.h>
25
26 #include <algorithm>
27 #include <limits>
28 #include <map>
29 #include <memory>
30 #include <string>
31 #include <string_view>
32 #include <vector>
33
34 #ifdef __ANDROID__
35 #include <PackageInfo.h>
36 #include <procpartition/procpartition.h>
37 #endif // __ANDROID__
38
39 namespace android {
40 namespace nn {
41
42 #ifdef __ANDROID__
43 namespace {
44
45 // Query PackageManagerNative service about Android app properties.
46 // On success, it will populate appInfo->app* fields.
fetchAppPackageLocationInfo(uid_t uid,AppInfoFetcher::AppInfo * appInfo)47 bool fetchAppPackageLocationInfo(uid_t uid, AppInfoFetcher::AppInfo* appInfo) {
48 ANeuralNetworks_PackageInfo packageInfo;
49 if (!ANeuralNetworks_fetch_PackageInfo(uid, &packageInfo)) {
50 return false;
51 }
52 appInfo->appPackageName = packageInfo.appPackageName;
53 appInfo->appIsSystemApp = packageInfo.appIsSystemApp;
54 appInfo->appIsOnVendorImage = packageInfo.appIsOnVendorImage;
55 appInfo->appIsOnProductImage = packageInfo.appIsOnProductImage;
56
57 ANeuralNetworks_free_PackageInfo(&packageInfo);
58 return true;
59 }
60
61 } // namespace
62
AppInfoFetcher()63 AppInfoFetcher::AppInfoFetcher()
64 : appInfo({.binaryPath = ::android::procpartition::getExe(getpid()),
65 .appPackageName = "",
66 .appIsSystemApp = false,
67 .appIsOnVendorImage = false,
68 .appIsOnProductImage = false}) {
69 if (appInfo.binaryPath == "/system/bin/app_process64" ||
70 appInfo.binaryPath == "/system/bin/app_process32") {
71 if (!fetchAppPackageLocationInfo(getuid(), &appInfo)) {
72 LOG(ERROR) << "Failed to get app information from package_manager_native";
73 }
74 }
75 }
76
77 #else // __ANDROID__
78
79 AppInfoFetcher::AppInfoFetcher()
80 : appInfo({.binaryPath = "/system/bin/app_process64",
81 .appPackageName = "",
82 .appIsSystemApp = false,
83 .appIsOnVendorImage = false,
84 .appIsOnProductImage = false}) {}
85
86 #endif // __ANDROID__
87
88 } // namespace nn
89 } // namespace android
90