1 /*
2  * Copyright (C) 2023 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.health.connect;
18 
19 import android.annotation.SystemApi;
20 import android.app.SystemServiceRegistry;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.health.connect.aidl.IHealthConnectService;
24 
25 /**
26  * Class for performing registration for Health services.
27  *
28  * @hide
29  */
30 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
31 public class HealthServicesInitializer {
HealthServicesInitializer()32     private HealthServicesInitializer() {}
33 
34     /**
35      * Called by {@link SystemServiceRegistry}'s static initializer and registers all Health
36      * services to {@link Context}, so that {@link Context#getSystemService} can return them.
37      *
38      * @throws IllegalStateException if this is called from anywhere besides {@link
39      *     SystemServiceRegistry}
40      */
registerServiceWrappers()41     public static void registerServiceWrappers() {
42         SystemServiceRegistry.registerContextAwareService(
43                 Context.HEALTHCONNECT_SERVICE,
44                 HealthConnectManager.class,
45                 (context, serviceBinder) -> {
46                     if (!isHardwareSupported(context)) {
47                         return null;
48                     }
49                     IHealthConnectService service =
50                             IHealthConnectService.Stub.asInterface(serviceBinder);
51                     return new HealthConnectManager(context, service);
52                 });
53     }
54 
isHardwareSupported(Context context)55     private static boolean isHardwareSupported(Context context) {
56         PackageManager pm = context.getPackageManager();
57         return (!pm.hasSystemFeature(PackageManager.FEATURE_EMBEDDED)
58                 && !pm.hasSystemFeature(PackageManager.FEATURE_WATCH)
59                 && !pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK)
60                 && !pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE));
61     }
62 }
63