Backports implementation for AltitudeService to T.

Test: VtsHalAltitudeServiceTargetTest
Bug: 265013616
Change-Id: I793d22fcfdd43c6d8ff2e4be52e3fb4a247fdf84
diff --git a/services/Android.bp b/services/Android.bp
index 0284019..1750b67 100644
--- a/services/Android.bp
+++ b/services/Android.bp
@@ -178,6 +178,10 @@
         "service-sdksandbox.stubs.system_server",
     ],
 
+    vintf_fragments: [
+        "manifest_services.xml",
+    ],
+
     // Uncomment to enable output of certain warnings (deprecated, unchecked)
     //javacflags: ["-Xlint"],
 }
diff --git a/services/core/Android.bp b/services/core/Android.bp
index 821927d..6a14dea 100644
--- a/services/core/Android.bp
+++ b/services/core/Android.bp
@@ -142,6 +142,7 @@
     ],
 
     static_libs: [
+        "android.frameworks.location.altitude-V1-java", // AIDL
         "android.hardware.authsecret-V1.0-java",
         "android.hardware.boot-V1.0-java", // HIDL
         "android.hardware.boot-V1.1-java", // HIDL
diff --git a/services/core/java/com/android/server/location/altitude/AltitudeService.java b/services/core/java/com/android/server/location/altitude/AltitudeService.java
new file mode 100644
index 0000000..b321e4d
--- /dev/null
+++ b/services/core/java/com/android/server/location/altitude/AltitudeService.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.location.altitude;
+
+import android.content.Context;
+import android.frameworks.location.altitude.AddMslAltitudeToLocationRequest;
+import android.frameworks.location.altitude.AddMslAltitudeToLocationResponse;
+import android.frameworks.location.altitude.IAltitudeService;
+import android.location.Location;
+import android.location.altitude.AltitudeConverter;
+import android.os.RemoteException;
+
+import com.android.server.SystemService;
+
+import java.io.IOException;
+
+/**
+ * Manages altitude information exchange through the HAL, e.g., geoid height requests such that
+ * vendors can perform altitude conversions.
+ *
+ * @hide
+ */
+public class AltitudeService extends IAltitudeService.Stub {
+
+    private final AltitudeConverter mAltitudeConverter = new AltitudeConverter();
+    private final Context mContext;
+
+    /** @hide */
+    public AltitudeService(Context context) {
+        mContext = context;
+    }
+
+    @Override
+    public AddMslAltitudeToLocationResponse addMslAltitudeToLocation(
+            AddMslAltitudeToLocationRequest request) throws RemoteException {
+        Location location = new Location("");
+        location.setLatitude(request.latitudeDegrees);
+        location.setLongitude(request.longitudeDegrees);
+        location.setAltitude(request.altitudeMeters);
+        location.setVerticalAccuracyMeters(request.verticalAccuracyMeters);
+        try {
+            mAltitudeConverter.addMslAltitudeToLocation(mContext, location);
+        } catch (IOException e) {
+            throw new RemoteException(e);
+        }
+
+        AddMslAltitudeToLocationResponse response = new AddMslAltitudeToLocationResponse();
+        response.mslAltitudeMeters = location.getMslAltitudeMeters();
+        response.mslAltitudeAccuracyMeters = location.getMslAltitudeAccuracyMeters();
+        return response;
+    }
+
+    @Override
+    public String getInterfaceHash() {
+        return IAltitudeService.HASH;
+    }
+
+    @Override
+    public int getInterfaceVersion() {
+        return IAltitudeService.VERSION;
+    }
+
+    /** @hide */
+    public static class Lifecycle extends SystemService {
+
+        private static final String SERVICE_NAME = IAltitudeService.DESCRIPTOR + "/default";
+
+        private AltitudeService mService;
+
+        public Lifecycle(Context context) {
+            super(context);
+        }
+
+        @Override
+        public void onStart() {
+            mService = new AltitudeService(getContext());
+            publishBinderService(SERVICE_NAME, mService);
+        }
+    }
+}
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 2f5c533..87663a5 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -139,6 +139,7 @@
 import com.android.server.lights.LightsService;
 import com.android.server.locales.LocaleManagerService;
 import com.android.server.location.LocationManagerService;
+import com.android.server.location.altitude.AltitudeService;
 import com.android.server.logcat.LogcatManagerService;
 import com.android.server.media.MediaRouterService;
 import com.android.server.media.metrics.MediaMetricsManagerService;
@@ -2097,6 +2098,14 @@
             }
             t.traceEnd();
 
+            t.traceBegin("StartAltitudeService");
+            try {
+                mSystemServiceManager.startService(AltitudeService.Lifecycle.class);
+            } catch (Throwable e) {
+                reportWtf("starting AltitudeService service", e);
+            }
+            t.traceEnd();
+
             t.traceBegin("StartLocationTimeZoneManagerService");
             try {
                 mSystemServiceManager.startService(LOCATION_TIME_ZONE_MANAGER_SERVICE_CLASS);
diff --git a/services/manifest_services.xml b/services/manifest_services.xml
new file mode 100644
index 0000000..7638915
--- /dev/null
+++ b/services/manifest_services.xml
@@ -0,0 +1,7 @@
+<manifest version="1.0" type="framework">
+    <hal format="aidl">
+        <name>android.frameworks.location.altitude</name>
+        <version>1</version>
+        <fqname>IAltitudeService/default</fqname>
+    </hal>
+</manifest>