1 /*
2 * Copyright (C) 2024 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 #include "PackageManagerProxy.h"
18
19 #include <android-base/properties.h>
20 #include <android/binder_manager.h>
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23 #include <log/log.h>
24 #include <utils/Looper.h>
25
26 #include <android_car_feature.h>
27
28 #include <sstream>
29
30 namespace google {
31 namespace sdv {
32 namespace packagemanagerproxy {
33
34 namespace {
35
36 using ::aidl::google::sdv::packagemanagerproxy::IPackageManagerProxy;
37 using ::android::IBinder;
38 using ::android::interface_cast;
39 using ::android::IServiceManager;
40 using ::android::sp;
41 using ::android::String16;
42 using ::android::base::Error;
43 using ::android::base::GetProperty;
44 using ::android::base::Result;
45 using ::android::content::pm::IPackageManagerNative;
46 using ::ndk::ScopedAStatus;
47
48 } // namespace
49
init()50 Result<void> PackageManagerProxy::init() {
51 // If the feature flag is not enabled, do not initialize the service
52 if (!android::car::feature::package_manager_extensions_for_sdv()) {
53 ALOGI("Flag package_manager_extensions_for_sdv disabled, disabling service");
54 return {};
55 }
56
57 sp<IServiceManager> serviceManager = android::defaultServiceManager();
58 if (serviceManager.get() == nullptr) {
59 return Error() << __func__ << ": unable to access native ServiceManager";
60 }
61
62 sp<IBinder> binder = serviceManager->waitForService(String16("package_native"));
63 mPackageManagerNativeService = interface_cast<IPackageManagerNative>(binder);
64 if (mPackageManagerNativeService == nullptr) {
65 return Error() << __func__ << ": unable to access native PackageManager";
66 }
67
68 const auto instanceName = std::string(IPackageManagerProxy::descriptor) + "/default";
69 const binder_exception_t err =
70 AServiceManager_addService(this->asBinder().get(), instanceName.data());
71 if (err != EX_NONE) {
72 return Error(err) << "Failed to add IPackageManagerProxy to ServiceManager";
73 }
74
75 return {};
76 }
77
getNamesForUids(const std::vector<int32_t> & uids,std::vector<std::string> * _aidl_return)78 ndk::ScopedAStatus PackageManagerProxy::getNamesForUids(const std::vector<int32_t>& uids,
79 std::vector<std::string>* _aidl_return) {
80 const auto result = mPackageManagerNativeService->getNamesForUids(uids, _aidl_return);
81
82 if (!result.isOk()) {
83 return ScopedAStatus::fromServiceSpecificErrorWithMessage(result.exceptionCode(),
84 result.exceptionMessage()
85 .c_str());
86 }
87
88 return ScopedAStatus::ok();
89 }
90
getPackageUid(const std::string & packageName,int64_t flags,int32_t userId,int32_t * _aidl_return)91 ndk::ScopedAStatus PackageManagerProxy::getPackageUid(const std::string& packageName, int64_t flags,
92 int32_t userId, int32_t* _aidl_return) {
93 const auto result =
94 mPackageManagerNativeService->getPackageUid(packageName, flags, userId, _aidl_return);
95
96 if (!result.isOk()) {
97 return ScopedAStatus::fromServiceSpecificErrorWithMessage(result.exceptionCode(),
98 result.exceptionMessage()
99 .c_str());
100 }
101
102 return ScopedAStatus::ok();
103 }
104
getVersionCodeForPackage(const std::string & packageName,int64_t * _aidl_return)105 ndk::ScopedAStatus PackageManagerProxy::getVersionCodeForPackage(const std::string& packageName,
106 int64_t* _aidl_return) {
107 const String16 packageNameString16(packageName.c_str(), packageName.length());
108 const auto result = mPackageManagerNativeService->getVersionCodeForPackage(packageNameString16,
109 _aidl_return);
110
111 if (!result.isOk()) {
112 return ScopedAStatus::fromServiceSpecificErrorWithMessage(result.exceptionCode(),
113 result.exceptionMessage()
114 .c_str());
115 }
116
117 return ScopedAStatus::ok();
118 }
119
120 } // namespace packagemanagerproxy
121 } // namespace sdv
122 } // namespace google
123