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