blob: 54ff7d3ddd7571e69e05fcdd206e82a7f6b105c1 [file] [log] [blame]
keunyounge18e25d2015-08-28 15:57:19 -07001/*
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 */
16package com.android.car.vehiclenetwork;
17
18import android.annotation.Nullable;
19import android.os.Handler;
20import android.os.Looper;
21import android.os.Message;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.util.Log;
25
26import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs;
27import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
28import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues;
29
30import java.lang.ref.WeakReference;
31
32/**
33 * System API to access Vehicle network. This is only for system services and applications should
keunyoung15882e52015-09-16 16:57:58 -070034 * not use this. All APIs will fail with security error if normal app tries this.
keunyounge18e25d2015-08-28 15:57:19 -070035 */
36public class VehicleNetwork {
37 public interface VehicleNetworkListener {
38 void onVehicleNetworkEvents(VehiclePropValues values);
39 }
40
keunyounge18e25d2015-08-28 15:57:19 -070041 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
keunyoung15882e52015-09-16 16:57:58 -070048 public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener,
49 Looper looper) {
keunyounge18e25d2015-08-28 15:57:19 -070050 IVehicleNetwork service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService(
51 IVehicleNetwork.class.getCanonicalName()));
52 if (service == null) {
keunyoung15882e52015-09-16 16:57:58 -070053 throw new RuntimeException("Vehicle network service not available:" +
54 IVehicleNetwork.class.getCanonicalName());
keunyounge18e25d2015-08-28 15:57:19 -070055 }
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
keunyoung15882e52015-09-16 16:57:58 -070067 public VehiclePropConfigs listProperties() {
68 return listProperties(0 /* all */);
69 }
70
keunyounge18e25d2015-08-28 15:57:19 -070071 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
keunyoung15882e52015-09-16 16:57:58 -070083 public void setProperty(VehiclePropValue value) {
keunyounge18e25d2015-08-28 15:57:19 -070084 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
85 try {
keunyoung15882e52015-09-16 16:57:58 -070086 mService.setProperty(parcelable);
keunyounge18e25d2015-08-28 15:57:19 -070087 } catch (RemoteException e) {
88 handleRemoteException(e);
89 }
keunyounge18e25d2015-08-28 15:57:19 -070090 }
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
keunyoung15882e52015-09-16 16:57:58 -0700104 public void subscribe(int property, float sampleRate) {
keunyounge18e25d2015-08-28 15:57:19 -0700105 try {
keunyoung15882e52015-09-16 16:57:58 -0700106 mService.subscribe(mVehicleNetworkListener, property, sampleRate);
keunyounge18e25d2015-08-28 15:57:19 -0700107 } catch (RemoteException e) {
108 handleRemoteException(e);
109 }
keunyounge18e25d2015-08-28 15:57:19 -0700110 }
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}