keunyoung | ca51507 | 2015-07-10 12:21:47 -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; |
| 17 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 18 | import static android.os.SystemClock.elapsedRealtime; |
| 19 | |
| 20 | import android.annotation.Nullable; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 21 | import android.app.Service; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 24 | import android.hardware.vehicle.V2_0.IVehicle; |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 25 | import android.os.Binder; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 26 | import android.os.IBinder; |
Vitalii Tomkiv | 973d2a2 | 2016-04-13 17:05:38 -0700 | [diff] [blame] | 27 | import android.os.SystemProperties; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 30 | import java.io.FileDescriptor; |
| 31 | import java.io.PrintWriter; |
| 32 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 33 | public class CarService extends Service { |
| 34 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 35 | /** Default vehicle HAL service name. */ |
| 36 | private static final String VEHICLE_SERVICE_NAME = "Vehicle"; |
| 37 | |
| 38 | private static final long WAIT_FOR_VEHICLE_HAL_TIMEOUT_MS = 10_000; |
| 39 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 40 | private ICarImpl mICarImpl; |
| 41 | |
| 42 | @Override |
| 43 | public void onCreate() { |
| 44 | Log.i(CarLog.TAG_SERVICE, "Service onCreate"); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 45 | IVehicle vehicle = getVehicle(WAIT_FOR_VEHICLE_HAL_TIMEOUT_MS); |
| 46 | if (vehicle == null) { |
| 47 | throw new IllegalStateException("Vehicle HAL service is not available."); |
| 48 | } |
| 49 | |
| 50 | mICarImpl = new ICarImpl(this, vehicle, SystemInterface.getDefault(this)); |
| 51 | mICarImpl.init(); |
Vitalii Tomkiv | 973d2a2 | 2016-04-13 17:05:38 -0700 | [diff] [blame] | 52 | SystemProperties.set("boot.car_service_created", "1"); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 53 | super.onCreate(); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void onDestroy() { |
| 58 | Log.i(CarLog.TAG_SERVICE, "Service onDestroy"); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 59 | mICarImpl.release(); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 60 | super.onDestroy(); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 65 | // keep it alive. |
| 66 | return START_STICKY; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public IBinder onBind(Intent intent) { |
| 71 | return mICarImpl; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 76 | if (checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 77 | != PackageManager.PERMISSION_GRANTED) { |
| 78 | writer.println("Permission Denial: can't dump CarService from from pid=" |
| 79 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 80 | + " without permission " + android.Manifest.permission.DUMP); |
| 81 | return; |
| 82 | } |
| 83 | if (args == null || args.length == 0) { |
| 84 | writer.println("*dump car service*"); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 85 | mICarImpl.dump(writer); |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 86 | } else { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 87 | mICarImpl.execShellCmd(args, writer); |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 88 | } |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 89 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 90 | |
| 91 | @Nullable |
| 92 | private IVehicle getVehicle(long waitMilliseconds) { |
| 93 | IVehicle vehicle = getVehicle(); |
| 94 | long start = elapsedRealtime(); |
| 95 | while (vehicle == null && (start + waitMilliseconds) > elapsedRealtime()) { |
| 96 | try { |
| 97 | Thread.sleep(100); |
| 98 | } catch (InterruptedException e) { |
| 99 | throw new RuntimeException("Sleep was interrupted", e); |
| 100 | } |
| 101 | |
| 102 | vehicle = getVehicle(); |
| 103 | } |
| 104 | return vehicle; |
| 105 | } |
| 106 | |
| 107 | @Nullable |
| 108 | private IVehicle getVehicle() { |
| 109 | return IVehicle.getService(VEHICLE_SERVICE_NAME); |
| 110 | } |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 111 | } |