• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.HealthPermissions;
24 import android.health.connect.aidl.IHealthConnectService;
25 
26 import com.android.healthfitness.flags.Flags;
27 
28 /**
29  * Class for performing registration for Health services.
30  *
31  * @hide
32  */
33 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
34 public class HealthServicesInitializer {
HealthServicesInitializer()35     private HealthServicesInitializer() {}
36 
37     /**
38      * Called by {@link SystemServiceRegistry}'s static initializer and registers all Health
39      * services to {@link Context}, so that {@link Context#getSystemService} can return them.
40      *
41      * @throws IllegalStateException if this is called from anywhere besides {@link
42      *     SystemServiceRegistry}
43      */
registerServiceWrappers()44     public static void registerServiceWrappers() {
45         SystemServiceRegistry.registerContextAwareService(
46                 Context.HEALTHCONNECT_SERVICE,
47                 HealthConnectManager.class,
48                 (context, serviceBinder) -> {
49                     if (!shouldReturnHealthConnectManager(context)) {
50                         return null;
51                     }
52 
53                     IHealthConnectService service =
54                             IHealthConnectService.Stub.asInterface(serviceBinder);
55                     return new HealthConnectManager(context, service);
56                 });
57     }
58 
shouldReturnHealthConnectManager(Context context)59     private static boolean shouldReturnHealthConnectManager(Context context) {
60         PackageManager pm = context.getPackageManager();
61         // Not available on embedded/tv/auto.
62         if (pm.hasSystemFeature(PackageManager.FEATURE_EMBEDDED)
63                 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK)
64                 || pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
65             return false;
66         }
67         // Only available on Wear for permission management.
68         if (pm.hasSystemFeature(PackageManager.FEATURE_WATCH)) {
69             return Flags.replaceBodySensorPermissionEnabled()
70                     && context.checkSelfPermission(HealthPermissions.MANAGE_HEALTH_PERMISSIONS)
71                             == PackageManager.PERMISSION_GRANTED;
72         }
73         // Supported everywhere else.
74         return true;
75     }
76 }