1 /* 2 * Copyright (C) 2018 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.car.garagemode; 18 19 import static com.android.car.CarServiceUtils.getHandlerThread; 20 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.DUMP_INFO; 21 22 import android.content.Context; 23 import android.os.HandlerThread; 24 import android.util.proto.ProtoOutputStream; 25 26 import com.android.car.CarServiceBase; 27 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport; 28 import com.android.car.internal.util.IndentingPrintWriter; 29 import com.android.internal.annotations.VisibleForTesting; 30 31 /** 32 * Main service container for car Garage Mode. 33 * Garage Mode enables idle time in cars. 34 */ 35 public class GarageModeService implements CarServiceBase { 36 37 private static final String HANDLER_THREAD_NAME = "GarageModeService"; 38 39 private final GarageModeController mController; 40 private final HandlerThread mHandlerThread = getHandlerThread(HANDLER_THREAD_NAME); 41 GarageModeService(Context context)42 public GarageModeService(Context context) { 43 this(context, /* controller= */ null); 44 } 45 46 @VisibleForTesting GarageModeService(Context context, GarageModeController controller)47 protected GarageModeService(Context context, GarageModeController controller) { 48 mController = (controller != null ? controller 49 : new GarageModeController(context, mHandlerThread.getLooper())); 50 } 51 52 /** 53 * Initializes GarageMode 54 */ 55 @Override init()56 public void init() { 57 mController.init(); 58 } 59 60 /** 61 * Cleans up GarageMode processes 62 */ 63 @Override release()64 public void release() { 65 mController.release(); 66 mHandlerThread.quitSafely(); 67 } 68 69 /** 70 * Dumps useful information about GarageMode 71 * @param writer Where to dump the information 72 */ 73 @Override 74 @ExcludeFromCodeCoverageGeneratedReport(reason = DUMP_INFO) dump(IndentingPrintWriter writer)75 public void dump(IndentingPrintWriter writer) { 76 boolean isActive = mController.isGarageModeActive(); 77 writer.println("GarageModeInProgress " + isActive); 78 mController.dump(writer); 79 } 80 81 @Override 82 @ExcludeFromCodeCoverageGeneratedReport(reason = DUMP_INFO) dumpProto(ProtoOutputStream proto)83 public void dumpProto(ProtoOutputStream proto) {} 84 85 /** 86 * @return whether GarageMode is in progress. Used by {@link com.android.car.ICarImpl}. 87 */ isGarageModeActive()88 public boolean isGarageModeActive() { 89 return mController.isGarageModeActive(); 90 } 91 92 /** 93 * Forces GarageMode to start. Used by {@link com.android.car.ICarImpl}. 94 */ forceStartGarageMode()95 public void forceStartGarageMode() { 96 mController.initiateGarageMode(null); 97 } 98 99 /** 100 * Stops and resets the GarageMode. Used by {@link com.android.car.ICarImpl}. 101 */ stopAndResetGarageMode()102 public void stopAndResetGarageMode() { 103 mController.resetGarageMode(null); 104 } 105 } 106