Car.java revision b878e1fef3b83bc8f2c3910584c9a5c58b0b0eb4
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
17package android.car;
18
19import android.annotation.IntDef;
20import android.annotation.Nullable;
21import android.annotation.SystemApi;
22import android.car.content.pm.CarPackageManager;
23import android.car.hardware.CarSensorManager;
24import android.car.hardware.camera.CarCameraManager;
25import android.car.hardware.hvac.CarHvacManager;
26import android.car.hardware.radio.CarRadioManager;
27import android.car.media.CarAudioManager;
28import android.car.navigation.CarNavigationManager;
29import android.car.test.CarTestManagerBinderWrapper;
30import android.content.ComponentName;
31import android.content.Context;
32import android.content.Intent;
33import android.content.ServiceConnection;
34import android.content.pm.PackageManager;
35import android.os.Handler;
36import android.os.IBinder;
37import android.os.Looper;
38import android.os.RemoteException;
39import android.util.Log;
40
41import com.android.internal.annotations.GuardedBy;
42
43import java.lang.annotation.Retention;
44import java.lang.annotation.RetentionPolicy;
45import java.util.HashMap;
46
47/**
48 *   Top level car API for embedded Android Auto deployments.
49 *   This API works only for devices with {@link PackageManager#FEATURE_AUTOMOTIVE}
50 *   Calling this API on a device with no such feature will lead to an exception.
51 */
52public class Car {
53
54    /** Service name for {@link CarSensorManager}, to be used in {@link #getCarManager(String)}. */
55    public static final String SENSOR_SERVICE = "sensor";
56
57    /** Service name for {@link CarInfoManager}, to be used in {@link #getCarManager(String)}. */
58    public static final String INFO_SERVICE = "info";
59
60    /** Service name for {@link CarAppContextManager}. */
61    public static final String APP_CONTEXT_SERVICE = "app_context";
62
63    /** Service name for {@link CarPackageManager} */
64    public static final String PACKAGE_SERVICE = "package";
65
66    /** Service name for {@link CarAudioManager} */
67    public static final String AUDIO_SERVICE = "audio";
68    /**
69     * Service name for {@link CarNavigationManager}
70     * @hide
71     */
72    public static final String CAR_NAVIGATION_SERVICE = "car_navigation_service";
73
74    /**
75     * @hide
76     */
77    @SystemApi
78    public static final String CAMERA_SERVICE = "camera";
79
80    /**
81     * @hide
82     */
83    @SystemApi
84    public static final String RADIO_SERVICE = "radio";
85
86    /**
87     * @hide
88     */
89    @SystemApi
90    public static final String HVAC_SERVICE = "hvac";
91
92    /**
93     * @hide
94     */
95    @SystemApi
96    public static final String PROJECTION_SERVICE = "projection";
97
98    /**
99     * Service for testing. This is system app only feature.
100     * Service name for {@link CarTestManager}, to be used in {@link #getCarManager(String)}.
101     * @hide
102     */
103    @SystemApi
104    public static final String TEST_SERVICE = "car-service-test";
105
106    /** Permission necessary to access car's mileage information. */
107    public static final String PERMISSION_MILEAGE = "android.car.permission.CAR_MILEAGE";
108
109    /** Permission necessary to access car's fuel level. */
110    public static final String PERMISSION_FUEL = "android.car.permission.CAR_FUEL";
111
112    /** Permission necessary to access car's speed. */
113    public static final String PERMISSION_SPEED = "android.car.permission.CAR_SPEED";
114
115    /**
116     * Permission necessary to use {@link CarNavigationManager}.
117     * @hide
118     */
119    public static final String PERMISSION_CAR_NAVIGATION_MANAGER =
120            "android.car.permission.PERMISSION_CAR_NAVIGATION_MANAGER";
121
122    /**
123     * Permission necessary to access car specific communication channel.
124     * @hide
125     */
126    @SystemApi
127    public static final String PERMISSION_VENDOR_EXTENSION =
128            "android.car.permission.CAR_VENDOR_EXTENSION";
129
130    /**
131     * @hide
132     */
133    @SystemApi
134    public static final String PERMISSION_CONTROL_APP_BLOCKING =
135            "android.car.permission.CONTROL_APP_BLOCKING";
136
137    /**
138     * Permission necessary to access Car Camera APIs.
139     * @hide
140     */
141    @SystemApi
142    public static final String PERMISSION_CAR_CAMERA = "android.car.permission.CAR_CAMERA";
143
144    /**
145     * Permission necessary to access Car HVAC APIs.
146     * @hide
147     */
148    @SystemApi
149    public static final String PERMISSION_CAR_HVAC = "android.car.permission.CAR_HVAC";
150
151    /**
152     * Permission necessary to access Car RADIO system APIs.
153     * @hide
154     */
155    @SystemApi
156    public static final String PERMISSION_CAR_RADIO = "android.car.permission.CAR_RADIO";
157
158    /**
159     * Permission necesary to access Car PRJECTION system APIs.
160     * @hide
161     */
162    @SystemApi
163    public static final String PERMISSION_CAR_PROJECTION = "android.car.permission.CAR_PROJECTION";
164
165    /**
166     * Permission necessary to mock vehicle hal for testing.
167     * @hide
168     */
169    @SystemApi
170    public static final String PERMISSION_MOCK_VEHICLE_HAL =
171            "android.car.permission.CAR_MOCK_VEHICLE_HAL";
172
173    /** Type of car connection: platform runs directly in car. */
174    public static final int CONNECTION_TYPE_EMBEDDED = 5;
175    /**
176     * Type of car connection: platform runs directly in car but with mocked vehicle hal.
177     * This will only happen in testing environment.
178     * @hide
179     */
180    public static final int CONNECTION_TYPE_EMBEDDED_MOCKING = 6;
181
182
183    /** @hide */
184    @IntDef({CONNECTION_TYPE_EMBEDDED, CONNECTION_TYPE_EMBEDDED_MOCKING})
185    @Retention(RetentionPolicy.SOURCE)
186    public @interface ConnectionType {}
187
188    /**
189     * CarXyzService throws IllegalStateException with this message is re-thrown as
190     * {@link CarNotConnectedException}.
191     *
192     * @hide
193     */
194    public static final String CAR_NOT_CONNECTED_EXCEPTION_MSG = "CarNotConnected";
195
196    /** @hide */
197    public static final String CAR_SERVICE_INTERFACE_NAME = "android.car.ICar";
198
199    private static final String CAR_SERVICE_PACKAGE = "com.android.car";
200
201    private static final String CAR_TEST_MANAGER_CLASS = "android.car.CarTestManager";
202
203    private final Context mContext;
204    private final Looper mLooper;
205    @GuardedBy("this")
206    private ICar mService;
207    private static final int STATE_DISCONNECTED = 0;
208    private static final int STATE_CONNECTING = 1;
209    private static final int STATE_CONNECTED = 2;
210    @GuardedBy("this")
211    private int mConnectionState;
212
213    private final ServiceConnection mServiceConnectionListener =
214            new ServiceConnection () {
215        public void onServiceConnected(ComponentName name, IBinder service) {
216            synchronized (Car.this) {
217                mService = ICar.Stub.asInterface(service);
218                mConnectionState = STATE_CONNECTED;
219            }
220            mServiceConnectionListenerClient.onServiceConnected(name, service);
221        }
222
223        public void onServiceDisconnected(ComponentName name) {
224            synchronized (Car.this) {
225                mService = null;
226                if (mConnectionState  == STATE_DISCONNECTED) {
227                    return;
228                }
229                mConnectionState = STATE_DISCONNECTED;
230            }
231            // unbind explicitly here.
232            disconnect();
233            mServiceConnectionListenerClient.onServiceDisconnected(name);
234        }
235    };
236
237    private final ServiceConnection mServiceConnectionListenerClient;
238    private final Object mCarManagerLock = new Object();
239    @GuardedBy("mCarManagerLock")
240    private final HashMap<String, CarManagerBase> mServiceMap = new HashMap<>();
241
242    /** Handler for generic event dispatching. */
243    private final Handler mEventHandler;
244
245    /**
246     * A factory method that creates Car instance for all Car API access.
247     * @param context
248     * @param serviceConnectionListener listener for monitoring service connection.
249     * @param looper Looper to dispatch all listeners. If null, it will use main thread. Note that
250     *        service connection listener will be always in main thread regardless of this Looper.
251     * @return Car instance if system is in car environment and returns {@code null} otherwise.
252     */
253    public static Car createCar(Context context, ServiceConnection serviceConnectionListener,
254            @Nullable Looper looper) {
255        if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
256            Log.e(CarLibLog.TAG_CAR, "FEATURE_AUTOMOTIVE not declared while android.car is used");
257            return null;
258        }
259        try {
260          return new Car(context, serviceConnectionListener, looper);
261        } catch (IllegalArgumentException e) {
262          // Expected when car service loader is not available.
263        }
264        return null;
265    }
266
267    /**
268     * A factory method that creates Car instance for all Car API access using main thread {@code
269     * Looper}.
270     *
271     * @see #createCar(Context, ServiceConnection, Looper)
272     */
273    public static Car createCar(Context context, ServiceConnection serviceConnectionListener) {
274      return createCar(context, serviceConnectionListener, null);
275    }
276
277    private Car(Context context, ServiceConnection serviceConnectionListener,
278            @Nullable Looper looper) {
279        mContext = context;
280        mServiceConnectionListenerClient = serviceConnectionListener;
281        if (looper == null) {
282            mLooper = Looper.getMainLooper();
283        } else {
284            mLooper = looper;
285        }
286        mEventHandler = new Handler(mLooper);
287    }
288
289    /**
290     * Car constructor when ICar binder is already available.
291     * @param context
292     * @param service
293     * @param looper
294     *
295     * @hide
296     */
297    public Car(Context context, ICar service, @Nullable Looper looper) {
298        mContext = context;
299        if (looper == null) {
300            mLooper = Looper.getMainLooper();
301        } else {
302            mLooper = looper;
303        }
304        mEventHandler = new Handler(mLooper);
305        mService = service;
306        mConnectionState = STATE_CONNECTED;
307        mServiceConnectionListenerClient = null;
308    }
309
310    /**
311     * Connect to car service. This can be called while it is disconnected.
312     * @throws IllegalStateException If connection is still on-going from previous
313     *         connect call or it is already connected
314     */
315    public void connect() throws IllegalStateException {
316        synchronized (this) {
317            if (mConnectionState != STATE_DISCONNECTED) {
318                throw new IllegalStateException("already connected or connecting");
319            }
320            mConnectionState = STATE_CONNECTING;
321            startCarService();
322        }
323    }
324
325    /**
326     * Disconnect from car service. This can be called while disconnected. Once disconnect is
327     * called, all Car*Managers from this instance becomes invalid, and
328     * {@link Car#getCarManager(String)} will return different instance if it is connected again.
329     */
330    public void disconnect() {
331        synchronized (this) {
332            if (mConnectionState == STATE_DISCONNECTED) {
333                return;
334            }
335            tearDownCarManagers();
336            mService = null;
337            mConnectionState = STATE_DISCONNECTED;
338            mContext.unbindService(mServiceConnectionListener);
339        }
340    }
341
342    /**
343     * Tells if it is connected to the service or not. This will return false if it is still
344     * connecting.
345     * @return
346     */
347    public boolean isConnected() {
348        synchronized (this) {
349            return mService != null;
350        }
351    }
352
353    /**
354     * Tells if this instance is already connecting to car service or not.
355     * @return
356     */
357    public boolean isConnecting() {
358        synchronized (this) {
359            return mConnectionState == STATE_CONNECTING;
360        }
361    }
362
363    /**
364     * Get car specific service as in {@link Context#getSystemService(String)}. Returned
365     * {@link Object} should be type-casted to the desired service.
366     * For example, to get sensor service,
367     * SensorManagerService sensorManagerService = car.getCarManager(Car.SENSOR_SERVICE);
368     * @param serviceName Name of service that should be created like {@link #SENSOR_SERVICE}.
369     * @return Matching service manager or null if there is no such service.
370     */
371    public Object getCarManager(String serviceName) {
372        CarManagerBase manager = null;
373        ICar service = getICarOrThrow();
374        synchronized (mCarManagerLock) {
375            manager = mServiceMap.get(serviceName);
376            if (manager == null) {
377                try {
378                    IBinder binder = service.getCarService(serviceName);
379                    if (binder == null) {
380                        Log.w(CarLibLog.TAG_CAR, "getCarManager could not get binder for service:" +
381                                serviceName);
382                        return null;
383                    }
384                    manager = createCarManager(serviceName, binder);
385                    if (manager == null) {
386                        Log.w(CarLibLog.TAG_CAR,
387                                "getCarManager could not create manager for service:" +
388                                serviceName);
389                        return null;
390                    }
391                    mServiceMap.put(serviceName, manager);
392                } catch (RemoteException e) {
393                    handleRemoteException(e);
394                }
395            }
396        }
397        return manager;
398    }
399
400    /**
401     * Return the type of currently connected car.
402     * @return
403     */
404    @ConnectionType
405    public int getCarConnectionType() {
406        return CONNECTION_TYPE_EMBEDDED;
407    }
408
409    /**
410     * IllegalStateException from XyzCarService with special message is re-thrown as a different
411     * exception. If the IllegalStateException is not understood then this message will throw the
412     * original exception.
413     *
414     * @param e exception from XyzCarService.
415     * @throws CarNotConnectedException
416     * @hide
417     */
418    public static void checkCarNotConnectedExceptionFromCarService(
419            IllegalStateException e) throws CarNotConnectedException, IllegalStateException {
420        String message = e.getMessage();
421        if (message.equals(CAR_NOT_CONNECTED_EXCEPTION_MSG)) {
422            throw new CarNotConnectedException();
423        } else {
424            throw e;
425        }
426    }
427
428    private CarManagerBase createCarManager(String serviceName, IBinder binder) {
429        CarManagerBase manager = null;
430        switch (serviceName) {
431            case AUDIO_SERVICE:
432                manager = new CarAudioManager(binder, mContext);
433                break;
434            case SENSOR_SERVICE:
435                manager = new CarSensorManager(binder, mContext, mLooper);
436                break;
437            case INFO_SERVICE:
438                manager = new CarInfoManager(binder);
439                break;
440            case APP_CONTEXT_SERVICE:
441                manager = new CarAppContextManager(binder, mLooper);
442                break;
443            case PACKAGE_SERVICE:
444                manager = new CarPackageManager(binder, mContext);
445                break;
446            case CAR_NAVIGATION_SERVICE:
447                manager = new CarNavigationManager(binder, mLooper);
448                break;
449            case CAMERA_SERVICE:
450                manager = new CarCameraManager(binder, mContext);
451                break;
452            case HVAC_SERVICE:
453                manager = new CarHvacManager(binder, mContext, mLooper);
454                break;
455            case PROJECTION_SERVICE:
456                manager = new CarProjectionManager(binder, mLooper);
457                break;
458            case RADIO_SERVICE:
459                manager = new CarRadioManager(binder, mLooper);
460                break;
461            case TEST_SERVICE:
462                /* CarTestManager exist in static library. So instead of constructing it here,
463                 * only pass binder wrapper so that CarTestManager can be constructed outside. */
464                manager = new CarTestManagerBinderWrapper(binder);
465                break;
466        }
467        return manager;
468    }
469
470    private void startCarService() {
471        Intent intent = new Intent();
472        intent.setPackage(CAR_SERVICE_PACKAGE);
473        intent.setAction(Car.CAR_SERVICE_INTERFACE_NAME);
474        mContext.startService(intent);
475        mContext.bindService(intent, mServiceConnectionListener, 0);
476    }
477
478    private synchronized ICar getICarOrThrow() throws IllegalStateException {
479        if (mService == null) {
480            throw new IllegalStateException("not connected");
481        }
482        return mService;
483    }
484
485    private void handleRemoteException(RemoteException e) {
486        Log.w(CarLibLog.TAG_CAR, "RemoteException", e);
487        disconnect();
488    }
489
490    private void tearDownCarManagers() {
491        synchronized (mCarManagerLock) {
492            for (CarManagerBase manager: mServiceMap.values()) {
493                manager.onCarDisconnected();
494            }
495            mServiceMap.clear();
496        }
497    }
498}
499