1 /*
2  * Copyright (C) 2022 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 package android.adservices.ondevicepersonalization;
18 
19 import android.adservices.ondevicepersonalization.aidl.IOnDevicePersonalizationDebugService;
20 import android.content.Context;
21 import android.os.RemoteException;
22 
23 import com.android.federatedcompute.internal.util.AbstractServiceBinder;
24 
25 import java.util.List;
26 import java.util.Objects;
27 import java.util.concurrent.Executors;
28 
29 /**
30  * Provides APIs to support testing.
31  * @hide
32  */
33 public class OnDevicePersonalizationDebugManager {
34 
35     private static final String INTENT_FILTER_ACTION =
36             "android.OnDevicePersonalizationDebugService";
37     private static final String ODP_MANAGING_SERVICE_PACKAGE_SUFFIX =
38             "com.android.ondevicepersonalization.services";
39 
40     private static final String ALT_ODP_MANAGING_SERVICE_PACKAGE_SUFFIX =
41             "com.google.android.ondevicepersonalization.services";
42 
43     private final AbstractServiceBinder<IOnDevicePersonalizationDebugService> mServiceBinder;
44 
OnDevicePersonalizationDebugManager(Context context)45     public OnDevicePersonalizationDebugManager(Context context) {
46         mServiceBinder =
47                 AbstractServiceBinder.getServiceBinderByIntent(
48                         context,
49                         INTENT_FILTER_ACTION,
50                         List.of(
51                                 ODP_MANAGING_SERVICE_PACKAGE_SUFFIX,
52                                 ALT_ODP_MANAGING_SERVICE_PACKAGE_SUFFIX),
53                         IOnDevicePersonalizationDebugService.Stub::asInterface);
54     }
55 
56     /** Returns whether the service is enabled. */
isEnabled()57     public Boolean isEnabled() {
58         try {
59             IOnDevicePersonalizationDebugService service = Objects.requireNonNull(
60                     mServiceBinder.getService(Executors.newSingleThreadExecutor()));
61             boolean result = service.isEnabled();
62             mServiceBinder.unbindFromService();
63             return result;
64         } catch (RemoteException e) {
65             throw e.rethrowAsRuntimeException();
66         }
67     }
68 }
69