keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package com.android.car.vehiclenetwork; |
| 17 | |
| 18 | import android.annotation.Nullable; |
| 19 | import android.os.Handler; |
| 20 | import android.os.Looper; |
| 21 | import android.os.Message; |
| 22 | import android.os.RemoteException; |
| 23 | import android.os.ServiceManager; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs; |
| 27 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue; |
| 28 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues; |
| 29 | |
| 30 | import java.lang.ref.WeakReference; |
| 31 | |
| 32 | /** |
| 33 | * System API to access Vehicle network. This is only for system services and applications should |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame^] | 34 | * not use this. All APIs will fail with security error if normal app tries this. |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 35 | */ |
| 36 | public class VehicleNetwork { |
| 37 | public interface VehicleNetworkListener { |
| 38 | void onVehicleNetworkEvents(VehiclePropValues values); |
| 39 | } |
| 40 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 41 | private static final String TAG = VehicleNetwork.class.getSimpleName(); |
| 42 | |
| 43 | private final IVehicleNetwork mService; |
| 44 | private final VehicleNetworkListener mListener; |
| 45 | private final IVehicleNetworkListenerImpl mVehicleNetworkListener; |
| 46 | private final EventHandler mEventHandler; |
| 47 | |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame^] | 48 | public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener, |
| 49 | Looper looper) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 50 | IVehicleNetwork service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService( |
| 51 | IVehicleNetwork.class.getCanonicalName())); |
| 52 | if (service == null) { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame^] | 53 | throw new RuntimeException("Vehicle network service not available:" + |
| 54 | IVehicleNetwork.class.getCanonicalName()); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 55 | } |
| 56 | return new VehicleNetwork(service, listener, looper); |
| 57 | } |
| 58 | |
| 59 | private VehicleNetwork(IVehicleNetwork service, VehicleNetworkListener listener, |
| 60 | Looper looper) { |
| 61 | mService = service; |
| 62 | mListener = listener; |
| 63 | mEventHandler = new EventHandler(looper); |
| 64 | mVehicleNetworkListener = new IVehicleNetworkListenerImpl(this); |
| 65 | } |
| 66 | |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame^] | 67 | public VehiclePropConfigs listProperties() { |
| 68 | return listProperties(0 /* all */); |
| 69 | } |
| 70 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 71 | public VehiclePropConfigs listProperties(int property) { |
| 72 | try { |
| 73 | VehiclePropConfigsParcelable parcelable = mService.listProperties(property); |
| 74 | if (parcelable != null) { |
| 75 | return parcelable.configs; |
| 76 | } |
| 77 | } catch (RemoteException e) { |
| 78 | handleRemoteException(e); |
| 79 | } |
| 80 | return null; |
| 81 | } |
| 82 | |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame^] | 83 | public void setProperty(VehiclePropValue value) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 84 | VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value); |
| 85 | try { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame^] | 86 | mService.setProperty(parcelable); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 87 | } catch (RemoteException e) { |
| 88 | handleRemoteException(e); |
| 89 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | public VehiclePropValue getProperty(int property) { |
| 93 | try { |
| 94 | VehiclePropValueParcelable parcelable = mService.getProperty(property); |
| 95 | if (parcelable != null) { |
| 96 | return parcelable.value; |
| 97 | } |
| 98 | } catch (RemoteException e) { |
| 99 | handleRemoteException(e); |
| 100 | } |
| 101 | return null; |
| 102 | } |
| 103 | |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame^] | 104 | public void subscribe(int property, float sampleRate) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 105 | try { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame^] | 106 | mService.subscribe(mVehicleNetworkListener, property, sampleRate); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 107 | } catch (RemoteException e) { |
| 108 | handleRemoteException(e); |
| 109 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | public void unsubscribe(int property) { |
| 113 | try { |
| 114 | mService.unsubscribe(mVehicleNetworkListener, property); |
| 115 | } catch (RemoteException e) { |
| 116 | handleRemoteException(e); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private void handleRemoteException(RemoteException e) { |
| 121 | throw new RuntimeException("Vehicle network service not working ", e); |
| 122 | } |
| 123 | |
| 124 | private void handleVehicleNetworkEvents(VehiclePropValues values) { |
| 125 | mEventHandler.notifyEvents(values); |
| 126 | } |
| 127 | |
| 128 | private void doHandleVehicleNetworkEvents(VehiclePropValues values) { |
| 129 | mListener.onVehicleNetworkEvents(values); |
| 130 | } |
| 131 | |
| 132 | private class EventHandler extends Handler { |
| 133 | private static final int MSG_EVENTS = 0; |
| 134 | |
| 135 | private EventHandler(Looper looper) { |
| 136 | super(looper); |
| 137 | } |
| 138 | |
| 139 | private void notifyEvents(VehiclePropValues values) { |
| 140 | Message msg = obtainMessage(MSG_EVENTS, values); |
| 141 | sendMessage(msg); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void handleMessage(Message msg) { |
| 146 | switch (msg.what) { |
| 147 | case MSG_EVENTS: |
| 148 | doHandleVehicleNetworkEvents((VehiclePropValues)msg.obj); |
| 149 | break; |
| 150 | default: |
| 151 | Log.w(TAG, "unown message:" + msg.what, new RuntimeException()); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub { |
| 158 | private final WeakReference<VehicleNetwork> mVehicleNetwork; |
| 159 | |
| 160 | private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) { |
| 161 | mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) { |
| 166 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 167 | if (vehicleNetwork != null) { |
| 168 | vehicleNetwork.handleVehicleNetworkEvents(values.values); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |