1 /* 2 * Copyright (C) 2021 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 com.android.server.location.gnss; 18 19 import static com.android.server.location.gnss.GnssManagerService.TAG; 20 21 import android.annotation.Nullable; 22 import android.location.GnssAntennaInfo; 23 import android.location.IGnssAntennaInfoListener; 24 import android.location.util.identity.CallerIdentity; 25 import android.os.Binder; 26 import android.os.IBinder; 27 28 import com.android.server.location.gnss.hal.GnssNative; 29 import com.android.server.location.listeners.BinderListenerRegistration; 30 import com.android.server.location.listeners.ListenerMultiplexer; 31 import com.android.server.location.listeners.ListenerRegistration; 32 33 import java.util.Collection; 34 import java.util.List; 35 36 /** 37 * Antenna info HAL module and listener multiplexer. 38 */ 39 public class GnssAntennaInfoProvider extends 40 ListenerMultiplexer<IBinder, IGnssAntennaInfoListener, 41 ListenerRegistration<IGnssAntennaInfoListener>, Void> implements 42 GnssNative.BaseCallbacks, GnssNative.AntennaInfoCallbacks { 43 44 /** 45 * Registration object for GNSS listeners. 46 */ 47 protected class AntennaInfoListenerRegistration extends 48 BinderListenerRegistration<Void, IGnssAntennaInfoListener> { 49 AntennaInfoListenerRegistration(CallerIdentity callerIdentity, IGnssAntennaInfoListener listener)50 protected AntennaInfoListenerRegistration(CallerIdentity callerIdentity, 51 IGnssAntennaInfoListener listener) { 52 super(null, callerIdentity, listener); 53 } 54 55 @Override getOwner()56 protected GnssAntennaInfoProvider getOwner() { 57 return GnssAntennaInfoProvider.this; 58 } 59 } 60 61 private final GnssNative mGnssNative; 62 63 private volatile @Nullable List<GnssAntennaInfo> mAntennaInfos; 64 GnssAntennaInfoProvider(GnssNative gnssNative)65 GnssAntennaInfoProvider(GnssNative gnssNative) { 66 mGnssNative = gnssNative; 67 mGnssNative.addBaseCallbacks(this); 68 mGnssNative.addAntennaInfoCallbacks(this); 69 } 70 getAntennaInfos()71 @Nullable List<GnssAntennaInfo> getAntennaInfos() { 72 return mAntennaInfos; 73 } 74 75 @Override getTag()76 public String getTag() { 77 return TAG; 78 } 79 isSupported()80 public boolean isSupported() { 81 return mGnssNative.isAntennaInfoSupported(); 82 } 83 addListener(CallerIdentity callerIdentity, IGnssAntennaInfoListener listener)84 public void addListener(CallerIdentity callerIdentity, IGnssAntennaInfoListener listener) { 85 long identity = Binder.clearCallingIdentity(); 86 try { 87 putRegistration(listener.asBinder(), 88 new AntennaInfoListenerRegistration(callerIdentity, listener)); 89 } finally { 90 Binder.restoreCallingIdentity(identity); 91 } 92 } 93 removeListener(IGnssAntennaInfoListener listener)94 public void removeListener(IGnssAntennaInfoListener listener) { 95 long identity = Binder.clearCallingIdentity(); 96 try { 97 removeRegistration(listener.asBinder()); 98 } finally { 99 Binder.restoreCallingIdentity(identity); 100 } 101 } 102 103 @Override registerWithService(Void merged, Collection<ListenerRegistration<IGnssAntennaInfoListener>> listenerRegistrations)104 protected boolean registerWithService(Void merged, 105 Collection<ListenerRegistration<IGnssAntennaInfoListener>> listenerRegistrations) { 106 return true; 107 } 108 109 @Override unregisterWithService()110 protected void unregisterWithService() {} 111 112 @Override isActive(ListenerRegistration<IGnssAntennaInfoListener> registration)113 protected boolean isActive(ListenerRegistration<IGnssAntennaInfoListener> registration) { 114 return true; 115 } 116 117 @Override mergeRegistrations( Collection<ListenerRegistration<IGnssAntennaInfoListener>> listenerRegistrations)118 protected Void mergeRegistrations( 119 Collection<ListenerRegistration<IGnssAntennaInfoListener>> listenerRegistrations) { 120 return null; 121 } 122 123 @Override onHalStarted()124 public void onHalStarted() { 125 mGnssNative.startAntennaInfoListening(); 126 } 127 128 @Override onHalRestarted()129 public void onHalRestarted() { 130 mGnssNative.startAntennaInfoListening(); 131 } 132 133 @Override onReportAntennaInfo(List<GnssAntennaInfo> antennaInfos)134 public void onReportAntennaInfo(List<GnssAntennaInfo> antennaInfos) { 135 if (antennaInfos.equals(mAntennaInfos)) { 136 return; 137 } 138 139 mAntennaInfos = antennaInfos; 140 deliverToListeners(listener -> { 141 listener.onGnssAntennaInfoChanged(antennaInfos); 142 }); 143 } 144 } 145