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