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.systemui.doze;
18  
19  import android.app.AlarmManager;
20  import android.os.Handler;
21  
22  import com.android.systemui.dagger.qualifiers.Main;
23  import com.android.systemui.doze.dagger.DozeScope;
24  import com.android.systemui.util.AlarmTimeout;
25  
26  import javax.inject.Inject;
27  
28  /**
29   * Moves the doze machine from the pausing to the paused state after a timeout.
30   */
31  @DozeScope
32  public class DozePauser implements DozeMachine.Part {
33      public static final String TAG = DozePauser.class.getSimpleName();
34      private final AlarmTimeout mPauseTimeout;
35      private DozeMachine mMachine;
36      private final AlwaysOnDisplayPolicy mPolicy;
37  
38      @Inject
DozePauser(@ain Handler handler, AlarmManager alarmManager, AlwaysOnDisplayPolicy policy)39      public DozePauser(@Main Handler handler, AlarmManager alarmManager,
40              AlwaysOnDisplayPolicy policy) {
41          mPauseTimeout = new AlarmTimeout(alarmManager, this::onTimeout, TAG, handler);
42          mPolicy = policy;
43      }
44  
45      @Override
setDozeMachine(DozeMachine dozeMachine)46      public void setDozeMachine(DozeMachine dozeMachine) {
47          mMachine = dozeMachine;
48      }
49  
50      @Override
transitionTo(DozeMachine.State oldState, DozeMachine.State newState)51      public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
52          switch (newState) {
53              case DOZE_AOD_PAUSING:
54                  mPauseTimeout.schedule(mPolicy.proxScreenOffDelayMs,
55                          AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
56                  break;
57              default:
58                  mPauseTimeout.cancel();
59                  break;
60          }
61      }
62  
onTimeout()63      private void onTimeout() {
64          mMachine.requestState(DozeMachine.State.DOZE_AOD_PAUSED);
65      }
66  }
67