1 /*
2 * Copyright (C) 2019 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 "PackageInfo"
18
19 #include "PackageInfo.h"
20 #include <android-base/logging.h>
21 #include <android/content/pm/IPackageManagerNative.h>
22 #include <binder/IServiceManager.h>
23 #include <string>
24 #include <vector>
25
ANeuralNetworks_fetch_PackageInfo(uid_t uid,ANeuralNetworks_PackageInfo * appPackageInfo)26 bool ANeuralNetworks_fetch_PackageInfo(uid_t uid, ANeuralNetworks_PackageInfo* appPackageInfo) {
27 if (appPackageInfo == nullptr) {
28 LOG(ERROR) << "appPackageInfo can't be a nullptr";
29 return false;
30 }
31
32 ::android::sp<::android::IServiceManager> sm(::android::defaultServiceManager());
33 ::android::sp<::android::IBinder> binder(sm->getService(::android::String16("package_native")));
34 if (binder == nullptr) {
35 LOG(ERROR) << "getService package_native failed";
36 return false;
37 }
38
39 ::android::sp<::android::content::pm::IPackageManagerNative> packageMgr =
40 ::android::interface_cast<::android::content::pm::IPackageManagerNative>(binder);
41 std::vector<int> uids{static_cast<int>(uid)};
42 std::vector<std::string> names;
43 ::android::binder::Status status = packageMgr->getNamesForUids(uids, &names);
44 if (!status.isOk()) {
45 LOG(ERROR) << "package_native::getNamesForUids failed: "
46 << status.exceptionMessage().c_str();
47 return false;
48 }
49 const std::string& packageName = names[0];
50
51 int flags = 0;
52 status = packageMgr->getLocationFlags(packageName, &flags);
53 if (!status.isOk()) {
54 LOG(ERROR) << "package_native::getLocationFlags failed: "
55 << status.exceptionMessage().c_str();
56 return false;
57 }
58
59 appPackageInfo->appPackageName = new char[packageName.size() + 1];
60 memcpy(appPackageInfo->appPackageName, packageName.c_str(), packageName.size() + 1);
61
62 // isSystemApp()
63 appPackageInfo->appIsSystemApp =
64 ((flags & ::android::content::pm::IPackageManagerNative::LOCATION_SYSTEM) != 0);
65 // isVendor()
66 appPackageInfo->appIsOnVendorImage =
67 ((flags & ::android::content::pm::IPackageManagerNative::LOCATION_VENDOR) != 0);
68 // isProduct()
69 appPackageInfo->appIsOnProductImage =
70 ((flags & ::android::content::pm::IPackageManagerNative::LOCATION_PRODUCT) != 0);
71 return true;
72 }
73
ANeuralNetworks_free_PackageInfo(ANeuralNetworks_PackageInfo * appPackageInfo)74 void ANeuralNetworks_free_PackageInfo(ANeuralNetworks_PackageInfo* appPackageInfo) {
75 if (appPackageInfo != nullptr) {
76 if (appPackageInfo->appPackageName != nullptr) {
77 delete[] appPackageInfo->appPackageName;
78 }
79 }
80 }
81