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 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 
SystemInterface(DisplayInterface displayInterface, IOInterface ioInterface, StorageMonitoringInterface storageMonitoringInterface, SystemStateInterface systemStateInterface, TimeInterface timeInterface, WakeLockInterface wakeLockInterface)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 
getDisplayInterface()61     public DisplayInterface getDisplayInterface() { return mDisplayInterface; }
getIOInterface()62     public IOInterface getIOInterface() { return mIOInterface; }
getSystemStateInterface()63     public SystemStateInterface getSystemStateInterface() { return mSystemStateInterface; }
getTimeInterface()64     public TimeInterface getTimeInterface() { return mTimeInterface; }
getWakeLockInterface()65     public WakeLockInterface getWakeLockInterface() { return mWakeLockInterface; }
setCarServiceHelper(ICarServiceHelper helper)66     public void setCarServiceHelper(ICarServiceHelper helper) {
67         mSystemStateInterface.setCarServiceHelper(helper);
68     }
69 
70     @Override
getSystemCarDir()71     public File getSystemCarDir() {
72         return mIOInterface.getSystemCarDir();
73     }
74 
75     @Override
releaseAllWakeLocks()76     public void releaseAllWakeLocks() {
77         mWakeLockInterface.releaseAllWakeLocks();
78     }
79 
80     @Override
switchToPartialWakeLock()81     public void switchToPartialWakeLock() {
82         mWakeLockInterface.switchToPartialWakeLock();
83     }
84 
85     @Override
switchToFullWakeLock()86     public void switchToFullWakeLock() {
87         mWakeLockInterface.switchToFullWakeLock();
88     }
89 
90     @Override
getUptime()91     public long getUptime() {
92         return mTimeInterface.getUptime();
93     }
94 
95     @Override
getUptime(boolean includeDeepSleepTime)96     public long getUptime(boolean includeDeepSleepTime) {
97         return mTimeInterface.getUptime(includeDeepSleepTime);
98     }
99 
100     @Override
scheduleAction(Runnable r, long delayMs)101     public void scheduleAction(Runnable r, long delayMs) {
102         mTimeInterface.scheduleAction(r, delayMs);
103     }
104 
105     @Override
getRunningProcesses()106     public List<ProcessInfo> getRunningProcesses() {
107         return mSystemStateInterface.getRunningProcesses();
108     }
109 
110     @Override
cancelAllActions()111     public void cancelAllActions() {
112         mTimeInterface.cancelAllActions();
113     }
114 
115     @Override
setDisplayBrightness(int brightness)116     public void setDisplayBrightness(int brightness) {
117         mDisplayInterface.setDisplayBrightness(brightness);
118     }
119 
120     @Override
setDisplayState(boolean on)121     public void setDisplayState(boolean on) {
122         mDisplayInterface.setDisplayState(on);
123     }
124 
125     @Override
reconfigureSecondaryDisplays()126     public void reconfigureSecondaryDisplays() {
127         mDisplayInterface.reconfigureSecondaryDisplays();
128     }
129 
130     @Override
startDisplayStateMonitoring(CarPowerManagementService service)131     public void startDisplayStateMonitoring(CarPowerManagementService service) {
132         mDisplayInterface.startDisplayStateMonitoring(service);
133     }
134 
135     @Override
stopDisplayStateMonitoring()136     public void stopDisplayStateMonitoring() {
137         mDisplayInterface.stopDisplayStateMonitoring();
138     }
139 
140     @Override
getFlashWearInformationProviders()141     public WearInformationProvider[] getFlashWearInformationProviders() {
142         return mStorageMonitoringInterface.getFlashWearInformationProviders();
143     }
144 
145     @Override
getUidIoStatsProvider()146     public UidIoStatsProvider getUidIoStatsProvider() {
147         return mStorageMonitoringInterface.getUidIoStatsProvider();
148     }
149 
150     @Override
getLifetimeWriteInfoProvider()151     public LifetimeWriteInfoProvider getLifetimeWriteInfoProvider() {
152         return mStorageMonitoringInterface.getLifetimeWriteInfoProvider();
153     }
154 
155     @Override
shutdown()156     public void shutdown() {
157         mSystemStateInterface.shutdown();
158     }
159 
160     @Override
enterDeepSleep()161     public boolean enterDeepSleep() {
162         return mSystemStateInterface.enterDeepSleep();
163     }
164 
165     @Override
scheduleActionForBootCompleted(Runnable action, Duration delay)166     public void scheduleActionForBootCompleted(Runnable action, Duration delay) {
167         mSystemStateInterface.scheduleActionForBootCompleted(action, delay);
168     }
169 
170     @Override
isWakeupCausedByTimer()171     public boolean isWakeupCausedByTimer() {
172         return mSystemStateInterface.isWakeupCausedByTimer();
173     }
174 
175     @Override
isSystemSupportingDeepSleep()176     public boolean isSystemSupportingDeepSleep() {
177         return mSystemStateInterface.isSystemSupportingDeepSleep();
178     }
179 
180     @Override
refreshDisplayBrightness()181     public void refreshDisplayBrightness() {
182         mDisplayInterface.refreshDisplayBrightness();
183     }
184 
185     public final static class Builder {
186         private DisplayInterface mDisplayInterface;
187         private IOInterface mIOInterface;
188         private StorageMonitoringInterface mStorageMonitoringInterface;
189         private SystemStateInterface mSystemStateInterface;
190         private TimeInterface mTimeInterface;
191         private WakeLockInterface mWakeLockInterface;
192 
Builder()193         private Builder() {}
194 
newSystemInterface()195         public static Builder newSystemInterface() {
196             return new Builder();
197         }
198 
defaultSystemInterface(Context context)199         public static Builder defaultSystemInterface(Context context) {
200             Objects.requireNonNull(context);
201             Builder builder = newSystemInterface();
202             builder.withWakeLockInterface(new WakeLockInterface.DefaultImpl(context));
203             builder.withDisplayInterface(new DisplayInterface.DefaultImpl(context,
204                     builder.mWakeLockInterface));
205             builder.withIOInterface(new IOInterface.DefaultImpl(context));
206             builder.withStorageMonitoringInterface(new StorageMonitoringInterface.DefaultImpl());
207             builder.withSystemStateInterface(new SystemStateInterface.DefaultImpl(context));
208             return builder.withTimeInterface(new TimeInterface.DefaultImpl());
209         }
210 
fromBuilder(Builder otherBuilder)211         public static Builder fromBuilder(Builder otherBuilder) {
212             return newSystemInterface()
213                     .withDisplayInterface(otherBuilder.mDisplayInterface)
214                     .withIOInterface(otherBuilder.mIOInterface)
215                     .withStorageMonitoringInterface(otherBuilder.mStorageMonitoringInterface)
216                     .withSystemStateInterface(otherBuilder.mSystemStateInterface)
217                     .withTimeInterface(otherBuilder.mTimeInterface)
218                     .withWakeLockInterface(otherBuilder.mWakeLockInterface);
219         }
220 
withDisplayInterface(DisplayInterface displayInterface)221         public Builder withDisplayInterface(DisplayInterface displayInterface) {
222             mDisplayInterface = displayInterface;
223             return this;
224         }
225 
withIOInterface(IOInterface ioInterface)226         public Builder withIOInterface(IOInterface ioInterface) {
227             mIOInterface = ioInterface;
228             return this;
229         }
230 
withStorageMonitoringInterface(StorageMonitoringInterface storageMonitoringInterface)231         public Builder withStorageMonitoringInterface(StorageMonitoringInterface
232                 storageMonitoringInterface) {
233             mStorageMonitoringInterface = storageMonitoringInterface;
234             return this;
235         }
236 
withSystemStateInterface(SystemStateInterface systemStateInterface)237         public Builder withSystemStateInterface(SystemStateInterface systemStateInterface) {
238             mSystemStateInterface = systemStateInterface;
239             return this;
240         }
241 
withTimeInterface(TimeInterface timeInterface)242         public Builder withTimeInterface(TimeInterface timeInterface) {
243             mTimeInterface = timeInterface;
244             return this;
245         }
246 
withWakeLockInterface(WakeLockInterface wakeLockInterface)247         public Builder withWakeLockInterface(WakeLockInterface wakeLockInterface) {
248             mWakeLockInterface = wakeLockInterface;
249             return this;
250         }
251 
build()252         public SystemInterface build() {
253             return new SystemInterface(Objects.requireNonNull(mDisplayInterface),
254                 Objects.requireNonNull(mIOInterface),
255                 Objects.requireNonNull(mStorageMonitoringInterface),
256                 Objects.requireNonNull(mSystemStateInterface),
257                 Objects.requireNonNull(mTimeInterface),
258                 Objects.requireNonNull(mWakeLockInterface));
259         }
260     }
261 }
262