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.internal.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.server.SystemService;
     31 import com.android.internal.car.ICarServiceHelper;
     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 final Context mContext;
     42     private IBinder mCarService;
     43     private final ServiceConnection mCarServiceConnection = new ServiceConnection() {
     44 
     45         @Override
     46         public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
     47             Slog.i(TAG, "**CarService connected**");
     48             mCarService = iBinder;
     49             // Cannot depend on ICar which is defined in CarService, so handle binder call directly
     50             // instead.
     51             // void setCarServiceHelper(in IBinder helper)
     52             Parcel data = Parcel.obtain();
     53             data.writeInterfaceToken(CAR_SERVICE_INTERFACE);
     54             data.writeStrongBinder(mHelper.asBinder());
     55             try {
     56                 mCarService.transact(IBinder.FIRST_CALL_TRANSACTION, // setCarServiceHelper
     57                         data, null, Binder.FLAG_ONEWAY);
     58             } catch (RemoteException e) {
     59                 Slog.w(TAG, "RemoteException from car service", e);
     60                 handleCarServiceCrash();
     61             }
     62         }
     63 
     64         @Override
     65         public void onServiceDisconnected(ComponentName componentName) {
     66             handleCarServiceCrash();
     67         }
     68     };
     69 
     70     public CarServiceHelperService(Context context) {
     71         super(context);
     72         mContext = context;
     73     }
     74 
     75     @Override
     76     public void onStart() {
     77         Intent intent = new Intent();
     78         intent.setPackage("com.android.car");
     79         intent.setAction(CAR_SERVICE_INTERFACE);
     80         if (!getContext().bindServiceAsUser(intent, mCarServiceConnection, Context.BIND_AUTO_CREATE,
     81                 UserHandle.SYSTEM)) {
     82             Slog.wtf(TAG, "cannot start car service");
     83         }
     84         System.loadLibrary("car-framework-service-jni");
     85     }
     86 
     87     private void handleCarServiceCrash() {
     88         //TODO define recovery bahavior
     89     }
     90 
     91     private static native int nativeForceSuspend(int timeoutMs);
     92 
     93     private class ICarServiceHelperImpl extends ICarServiceHelper.Stub {
     94        /**
     95          * Force device to suspend
     96          *
     97          * @param timeoutMs
     98          */
     99         @Override // Binder call
    100         public int forceSuspend(int timeoutMs) {
    101             int retVal;
    102             mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
    103             final long ident = Binder.clearCallingIdentity();
    104             try {
    105                 retVal = nativeForceSuspend(timeoutMs);
    106             } finally {
    107                 Binder.restoreCallingIdentity(ident);
    108             }
    109             return retVal;
    110         }
    111     }
    112 }
    113