Home | History | Annotate | Download | only in systeminterface
      1 /*
      2  * Copyright (C) 2016 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.systeminterface;
     18 
     19 import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
     20 
     21 import android.os.SystemClock;
     22 import java.util.concurrent.ScheduledExecutorService;
     23 import java.util.concurrent.TimeUnit;
     24 
     25 /**
     26  * Interface that abstracts time operations
     27  */
     28 public interface TimeInterface {
     29     public static final boolean INCLUDE_DEEP_SLEEP_TIME = true;
     30     public static final boolean EXCLUDE_DEEP_SLEEP_TIME = false;
     31 
     32     default long getUptime() {
     33         return getUptime(EXCLUDE_DEEP_SLEEP_TIME);
     34     }
     35     default long getUptime(boolean includeDeepSleepTime) {
     36         return includeDeepSleepTime ?
     37             SystemClock.elapsedRealtime() :
     38             SystemClock.uptimeMillis();
     39     }
     40 
     41     void scheduleAction(Runnable r, long delayMs);
     42     void cancelAllActions();
     43 
     44     class DefaultImpl implements TimeInterface {
     45         private final ScheduledExecutorService mExecutor = newSingleThreadScheduledExecutor();
     46 
     47         @Override
     48         public void scheduleAction(Runnable r, long delayMs) {
     49             mExecutor.scheduleAtFixedRate(r, delayMs, delayMs, TimeUnit.MILLISECONDS);
     50         }
     51 
     52         @Override
     53         public void cancelAllActions() {
     54             mExecutor.shutdownNow();
     55         }
     56     }
     57 }
     58