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 android.content.Context;
     20 
     21 import com.android.car.CarPowerManagementService;
     22 import com.android.car.procfsinspector.ProcessInfo;
     23 import com.android.car.storagemonitoring.LifetimeWriteInfoProvider;
     24 import com.android.car.storagemonitoring.UidIoStatsProvider;
     25 import com.android.car.storagemonitoring.WearInformationProvider;
     26 import com.android.internal.car.ICarServiceHelper;
     27 
     28 import java.io.File;
     29 import java.time.Duration;
     30 import java.util.List;
     31 import java.util.Objects;
     32 
     33 /**
     34  * This class contains references to all the different wrapper interfaces between
     35  * CarService and the Android OS APIs.
     36  */
     37 public final class SystemInterface implements DisplayInterface, IOInterface,
     38         StorageMonitoringInterface, SystemStateInterface, TimeInterface,
     39         WakeLockInterface {
     40     private final DisplayInterface mDisplayInterface;
     41     private final IOInterface mIOInterface;
     42     private final StorageMonitoringInterface mStorageMonitoringInterface;
     43     private final SystemStateInterface mSystemStateInterface;
     44     private final TimeInterface mTimeInterface;
     45     private final WakeLockInterface mWakeLockInterface;
     46 
     47     SystemInterface(DisplayInterface displayInterface,
     48             IOInterface ioInterface,
     49             StorageMonitoringInterface storageMonitoringInterface,
     50             SystemStateInterface systemStateInterface,
     51             TimeInterface timeInterface,
     52             WakeLockInterface wakeLockInterface) {
     53         mDisplayInterface = displayInterface;
     54         mIOInterface = ioInterface;
     55         mStorageMonitoringInterface = storageMonitoringInterface;
     56         mSystemStateInterface = systemStateInterface;
     57         mTimeInterface = timeInterface;
     58         mWakeLockInterface = wakeLockInterface;
     59     }
     60 
     61     public DisplayInterface getDisplayInterface() { return mDisplayInterface; }
     62     public IOInterface getIOInterface() { return mIOInterface; }
     63     public SystemStateInterface getSystemStateInterface() { return mSystemStateInterface; }
     64     public TimeInterface getTimeInterface() { return mTimeInterface; }
     65     public WakeLockInterface getWakeLockInterface() { return mWakeLockInterface; }
     66     public void setCarServiceHelper(ICarServiceHelper helper) {
     67         mSystemStateInterface.setCarServiceHelper(helper);
     68     }
     69 
     70     @Override
     71     public File getFilesDir() {
     72         return mIOInterface.getFilesDir();
     73     }
     74 
     75     @Override
     76     public void releaseAllWakeLocks() {
     77         mWakeLockInterface.releaseAllWakeLocks();
     78     }
     79 
     80     @Override
     81     public void switchToPartialWakeLock() {
     82         mWakeLockInterface.switchToPartialWakeLock();
     83     }
     84 
     85     @Override
     86     public void switchToFullWakeLock() {
     87         mWakeLockInterface.switchToFullWakeLock();
     88     }
     89 
     90     @Override
     91     public long getUptime() {
     92         return mTimeInterface.getUptime();
     93     }
     94 
     95     @Override
     96     public long getUptime(boolean includeDeepSleepTime) {
     97         return mTimeInterface.getUptime(includeDeepSleepTime);
     98     }
     99 
    100     @Override
    101     public void scheduleAction(Runnable r, long delayMs) {
    102         mTimeInterface.scheduleAction(r, delayMs);
    103     }
    104 
    105     @Override
    106     public List<ProcessInfo> getRunningProcesses() {
    107         return mSystemStateInterface.getRunningProcesses();
    108     }
    109 
    110     @Override
    111     public void cancelAllActions() {
    112         mTimeInterface.cancelAllActions();
    113     }
    114 
    115     @Override
    116     public void setDisplayBrightness(int brightness) {
    117         mDisplayInterface.setDisplayBrightness(brightness);
    118     }
    119 
    120     @Override
    121     public void setDisplayState(boolean on) {
    122         mDisplayInterface.setDisplayState(on);
    123     }
    124 
    125     @Override
    126     public void startDisplayStateMonitoring(CarPowerManagementService service) {
    127         mDisplayInterface.startDisplayStateMonitoring(service);
    128     }
    129 
    130     @Override
    131     public void stopDisplayStateMonitoring() {
    132         mDisplayInterface.stopDisplayStateMonitoring();
    133     }
    134 
    135     @Override
    136     public WearInformationProvider[] getFlashWearInformationProviders() {
    137         return mStorageMonitoringInterface.getFlashWearInformationProviders();
    138     }
    139 
    140     @Override
    141     public UidIoStatsProvider getUidIoStatsProvider() {
    142         return mStorageMonitoringInterface.getUidIoStatsProvider();
    143     }
    144 
    145     @Override
    146     public LifetimeWriteInfoProvider getLifetimeWriteInfoProvider() {
    147         return mStorageMonitoringInterface.getLifetimeWriteInfoProvider();
    148     }
    149 
    150     @Override
    151     public void shutdown() {
    152         mSystemStateInterface.shutdown();
    153     }
    154 
    155     @Override
    156     public boolean enterDeepSleep(int wakeupTimeSec) {
    157         return mSystemStateInterface.enterDeepSleep(wakeupTimeSec);
    158     }
    159 
    160     @Override
    161     public void scheduleActionForBootCompleted(Runnable action, Duration delay) {
    162         mSystemStateInterface.scheduleActionForBootCompleted(action, delay);
    163     }
    164 
    165     @Override
    166     public boolean isWakeupCausedByTimer() {
    167         return mSystemStateInterface.isWakeupCausedByTimer();
    168     }
    169 
    170     @Override
    171     public boolean isSystemSupportingDeepSleep() {
    172         return mSystemStateInterface.isSystemSupportingDeepSleep();
    173     }
    174 
    175     public final static class Builder {
    176         private DisplayInterface mDisplayInterface;
    177         private IOInterface mIOInterface;
    178         private StorageMonitoringInterface mStorageMonitoringInterface;
    179         private SystemStateInterface mSystemStateInterface;
    180         private TimeInterface mTimeInterface;
    181         private WakeLockInterface mWakeLockInterface;
    182 
    183         private Builder() {}
    184 
    185         public static Builder newSystemInterface() {
    186             return new Builder();
    187         }
    188 
    189         public static Builder defaultSystemInterface(Context context) {
    190             Objects.requireNonNull(context);
    191             Builder builder = newSystemInterface();
    192             builder.withWakeLockInterface(new WakeLockInterface.DefaultImpl(context));
    193             builder.withDisplayInterface(new DisplayInterface.DefaultImpl(context,
    194                     builder.mWakeLockInterface));
    195             builder.withIOInterface(new IOInterface.DefaultImpl(context));
    196             builder.withStorageMonitoringInterface(new StorageMonitoringInterface.DefaultImpl());
    197             builder.withSystemStateInterface(new SystemStateInterface.DefaultImpl(context));
    198             return builder.withTimeInterface(new TimeInterface.DefaultImpl());
    199         }
    200 
    201         public static Builder fromBuilder(Builder otherBuilder) {
    202             return newSystemInterface()
    203                     .withDisplayInterface(otherBuilder.mDisplayInterface)
    204                     .withIOInterface(otherBuilder.mIOInterface)
    205                     .withStorageMonitoringInterface(otherBuilder.mStorageMonitoringInterface)
    206                     .withSystemStateInterface(otherBuilder.mSystemStateInterface)
    207                     .withTimeInterface(otherBuilder.mTimeInterface)
    208                     .withWakeLockInterface(otherBuilder.mWakeLockInterface);
    209         }
    210 
    211         public Builder withDisplayInterface(DisplayInterface displayInterface) {
    212             mDisplayInterface = displayInterface;
    213             return this;
    214         }
    215 
    216         public Builder withIOInterface(IOInterface ioInterface) {
    217             mIOInterface = ioInterface;
    218             return this;
    219         }
    220 
    221         public Builder withStorageMonitoringInterface(StorageMonitoringInterface
    222                 storageMonitoringInterface) {
    223             mStorageMonitoringInterface = storageMonitoringInterface;
    224             return this;
    225         }
    226 
    227         public Builder withSystemStateInterface(SystemStateInterface systemStateInterface) {
    228             mSystemStateInterface = systemStateInterface;
    229             return this;
    230         }
    231 
    232         public Builder withTimeInterface(TimeInterface timeInterface) {
    233             mTimeInterface = timeInterface;
    234             return this;
    235         }
    236 
    237         public Builder withWakeLockInterface(WakeLockInterface wakeLockInterface) {
    238             mWakeLockInterface = wakeLockInterface;
    239             return this;
    240         }
    241 
    242         public SystemInterface build() {
    243             return new SystemInterface(Objects.requireNonNull(mDisplayInterface),
    244                 Objects.requireNonNull(mIOInterface),
    245                 Objects.requireNonNull(mStorageMonitoringInterface),
    246                 Objects.requireNonNull(mSystemStateInterface),
    247                 Objects.requireNonNull(mTimeInterface),
    248                 Objects.requireNonNull(mWakeLockInterface));
    249         }
    250     }
    251 }
    252