• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.app.job.JobScheduler;
20 import android.car.hardware.power.CarPowerManager;
21 import android.car.hardware.power.CarPowerManager.CarPowerStateListener;
22 import android.car.hardware.power.CarPowerManager.CarPowerStateListenerWithCompletion;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.Handler;
26 import android.os.Looper;
27 import android.os.UserHandle;
28 
29 import com.android.car.CarLocalServices;
30 import com.android.car.CarLog;
31 import com.android.car.systeminterface.SystemInterface;
32 import com.android.internal.annotations.VisibleForTesting;
33 import com.android.server.utils.Slogf;
34 
35 import java.io.PrintWriter;
36 import java.util.concurrent.CompletableFuture;
37 
38 /**
39  * Main controller for GarageMode. It controls all the flows of GarageMode and defines the logic.
40  */
41 public class Controller implements CarPowerStateListenerWithCompletion {
42 
43     private static final String TAG = CarLog.tagFor(GarageMode.class) + "_"
44             + Controller.class.getSimpleName();
45 
46     @VisibleForTesting final WakeupPolicy mWakeupPolicy;
47     private final GarageMode mGarageMode;
48     private final Handler mHandler;
49     private final Context mContext;
50     private CarPowerManager mCarPowerManager;
51 
Controller(Context context, Looper looper)52     public Controller(Context context, Looper looper) {
53         this(context, looper, null, null, null);
54     }
55 
Controller( Context context, Looper looper, WakeupPolicy wakeupPolicy, Handler handler, GarageMode garageMode)56     public Controller(
57             Context context,
58             Looper looper,
59             WakeupPolicy wakeupPolicy,
60             Handler handler,
61             GarageMode garageMode) {
62         mContext = context;
63         mHandler = (handler == null ? new Handler(looper) : handler);
64         mWakeupPolicy =
65                 (wakeupPolicy == null ? WakeupPolicy.initFromResources(context) : wakeupPolicy);
66         mGarageMode = (garageMode == null ? new GarageMode(this) : garageMode);
67     }
68 
69     /** init */
init()70     public void init() {
71         mCarPowerManager = CarLocalServices.createCarPowerManager(mContext);
72         mCarPowerManager.setListenerWithCompletion(Controller.this);
73         mGarageMode.init();
74     }
75 
76     /** release */
release()77     public void release() {
78         mCarPowerManager.clearListener();
79         mGarageMode.release();
80     }
81 
82     @Override
onStateChanged(int state, CompletableFuture<Void> future)83     public void onStateChanged(int state, CompletableFuture<Void> future) {
84         switch (state) {
85             case CarPowerStateListener.SHUTDOWN_CANCELLED:
86                 Slogf.d(TAG, "CPM state changed to SHUTDOWN_CANCELLED");
87                 handleShutdownCancelled();
88                 break;
89             case CarPowerStateListener.SHUTDOWN_ENTER:
90                 Slogf.d(TAG, "CPM state changed to SHUTDOWN_ENTER");
91                 handleShutdownEnter();
92                 break;
93             case CarPowerStateListener.SHUTDOWN_PREPARE:
94                 Slogf.d(TAG, "CPM state changed to SHUTDOWN_PREPARE");
95                 handleShutdownPrepare(future);
96                 break;
97             case CarPowerStateListener.SUSPEND_ENTER:
98                 Slogf.d(TAG, "CPM state changed to SUSPEND_ENTER");
99                 handleSuspendEnter();
100                 break;
101             case CarPowerStateListener.SUSPEND_EXIT:
102                 Slogf.d(TAG, "CPM state changed to SUSPEND_EXIT");
103                 handleSuspendExit();
104                 break;
105             default:
106         }
107     }
108 
109     /**
110      * @return boolean whether any jobs are currently in running that GarageMode cares about
111      */
isGarageModeActive()112     boolean isGarageModeActive() {
113         return mGarageMode.isGarageModeActive();
114     }
115 
116     /**
117      * Prints Garage Mode's status, including what jobs it is waiting for
118      */
dump(PrintWriter writer)119     void dump(PrintWriter writer) {
120         mGarageMode.dump(writer);
121     }
122 
123     /**
124      * Wrapper method to send a broadcast
125      *
126      * @param i intent that contains broadcast data
127      */
sendBroadcast(Intent i)128     void sendBroadcast(Intent i) {
129         SystemInterface systemInterface = CarLocalServices.getService(SystemInterface.class);
130         Slogf.d(TAG, "Sending broadcast with action: %s", i.getAction());
131         systemInterface.sendBroadcastAsUser(i, UserHandle.ALL);
132     }
133 
134     /**
135      * @return JobSchedulerService instance
136      */
getJobSchedulerService()137     JobScheduler getJobSchedulerService() {
138         return (JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);
139     }
140 
141     /**
142      * @return Handler instance used by controller
143      */
getHandler()144     Handler getHandler() {
145         return mHandler;
146     }
147 
148     /**
149      * Initiates GarageMode flow which will set the system idleness to true and will start
150      * monitoring jobs which has idleness constraint enabled.
151      */
initiateGarageMode(CompletableFuture<Void> future)152     void initiateGarageMode(CompletableFuture<Void> future) {
153         mWakeupPolicy.incrementCounter();
154         mGarageMode.enterGarageMode(future);
155     }
156 
157     /**
158      * Resets GarageMode.
159      */
resetGarageMode()160     void resetGarageMode() {
161         mGarageMode.cancel();
162         mWakeupPolicy.resetCounter();
163     }
164 
165     @VisibleForTesting
finishGarageMode()166     void finishGarageMode() {
167         mGarageMode.finish();
168     }
169 
170     @VisibleForTesting
setCarPowerManager(CarPowerManager cpm)171     void setCarPowerManager(CarPowerManager cpm) {
172         mCarPowerManager = cpm;
173     }
174 
scheduleNextWakeup()175     void scheduleNextWakeup() {
176         if (mWakeupPolicy.getNextWakeUpInterval() <= 0) {
177             // Either there is no policy or nothing left to schedule
178             return;
179         }
180         int seconds = mWakeupPolicy.getNextWakeUpInterval();
181         mCarPowerManager.scheduleNextWakeupTime(seconds);
182     }
183 
handleSuspendExit()184     private void handleSuspendExit() {
185         resetGarageMode();
186     }
187 
handleSuspendEnter()188     private void handleSuspendEnter() {
189         resetGarageMode();
190     }
191 
handleShutdownEnter()192     private void handleShutdownEnter() {
193         resetGarageMode();
194     }
195 
handleShutdownPrepare(CompletableFuture<Void> future)196     private void handleShutdownPrepare(CompletableFuture<Void> future) {
197         initiateGarageMode(future);
198     }
199 
handleShutdownCancelled()200     private void handleShutdownCancelled() {
201         resetGarageMode();
202     }
203 }
204