Home | History | Annotate | Download | only in car
      1 /*
      2  * Copyright (C) 2017 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.car;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.ServiceConnection;
     23 import android.os.Binder;
     24 import android.os.IBinder;
     25 import android.os.Parcel;
     26 import android.os.RemoteException;
     27 import android.os.UserHandle;
     28 import android.util.Slog;
     29 
     30 import com.android.internal.car.ICarServiceHelper;
     31 import com.android.server.SystemService;
     32 
     33 /**
     34  * System service side companion service for CarService.
     35  * Starts car service and provide necessary API for CarService. Only for car product.
     36  */
     37 public class CarServiceHelperService extends SystemService {
     38     private static final String TAG = "CarServiceHelper";
     39     private static final String CAR_SERVICE_INTERFACE = "android.car.ICar";
     40     private final ICarServiceHelperImpl mHelper = new ICarServiceHelperImpl();
     41     private IBinder mCarService;
     42     private final ServiceConnection mCarServiceConnection = new ServiceConnection() {
     43 
     44         @Override
     45         public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
     46             Slog.i(TAG, "**CarService connected**");
     47             mCarService = iBinder;
     48             // Cannot depend on ICar which is defined in CarService, so handle binder call directly
     49             // instead.
     50             // void setCarServiceHelper(in IBinder helper)
     51             Parcel data = Parcel.obtain();
     52             data.writeInterfaceToken(CAR_SERVICE_INTERFACE);
     53             data.writeStrongBinder(mHelper.asBinder());
     54             try {
     55                 mCarService.transact(IBinder.FIRST_CALL_TRANSACTION, // setCarServiceHelper
     56                         data, null, Binder.FLAG_ONEWAY);
     57             } catch (RemoteException e) {
     58                 Slog.w(TAG, "RemoteException from car service", e);
     59                 handleCarServiceCrash();
     60             }
     61         }
     62 
     63         @Override
     64         public void onServiceDisconnected(ComponentName componentName) {
     65             handleCarServiceCrash();
     66         }
     67     };
     68 
     69     public CarServiceHelperService(Context context) {
     70         super(context);
     71     }
     72 
     73     @Override
     74     public void onStart() {
     75         Intent intent = new Intent();
     76         intent.setPackage("com.android.car");
     77         intent.setAction(CAR_SERVICE_INTERFACE);
     78         if (!getContext().bindServiceAsUser(intent, mCarServiceConnection, Context.BIND_AUTO_CREATE,
     79                 UserHandle.SYSTEM)) {
     80             Slog.wtf(TAG, "cannot start car service");
     81         }
     82     }
     83 
     84     private void handleCarServiceCrash() {
     85         //TODO define recovery bahavior
     86     }
     87 
     88     private class ICarServiceHelperImpl extends ICarServiceHelper.Stub {
     89         //TODO
     90     }
     91 }
     92