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 import android.content.Intent;
21 import android.os.UserHandle;
22 
23 import com.android.car.power.CarPowerManagementService;
24 import com.android.car.procfsinspector.ProcessInfo;
25 import com.android.car.storagemonitoring.LifetimeWriteInfoProvider;
26 import com.android.car.storagemonitoring.UidIoStatsProvider;
27 import com.android.car.storagemonitoring.WearInformationProvider;
28 import com.android.car.user.CarUserService;
29 
30 import java.io.File;
31 import java.time.Duration;
32 import java.util.List;
33 import java.util.Objects;
34 
35 /**
36  * This class contains references to all the different wrapper interfaces between
37  * CarService and the Android OS APIs.
38  */
39 public class SystemInterface implements ActivityManagerInterface,
40         DisplayInterface, IOInterface, StorageMonitoringInterface,
41         SystemStateInterface, TimeInterface,
42         WakeLockInterface {
43     private final ActivityManagerInterface mActivityManagerInterface;
44     private final DisplayInterface mDisplayInterface;
45     private final IOInterface mIOInterface;
46     private final StorageMonitoringInterface mStorageMonitoringInterface;
47     private final SystemStateInterface mSystemStateInterface;
48     private final TimeInterface mTimeInterface;
49     private final WakeLockInterface mWakeLockInterface;
50 
SystemInterface(ActivityManagerInterface activityManagerInterface, DisplayInterface displayInterface, IOInterface ioInterface, StorageMonitoringInterface storageMonitoringInterface, SystemStateInterface systemStateInterface, TimeInterface timeInterface, WakeLockInterface wakeLockInterface)51     SystemInterface(ActivityManagerInterface activityManagerInterface,
52             DisplayInterface displayInterface,
53             IOInterface ioInterface,
54             StorageMonitoringInterface storageMonitoringInterface,
55             SystemStateInterface systemStateInterface,
56             TimeInterface timeInterface,
57             WakeLockInterface wakeLockInterface) {
58         mActivityManagerInterface = activityManagerInterface;
59         mDisplayInterface = displayInterface;
60         mIOInterface = ioInterface;
61         mStorageMonitoringInterface = storageMonitoringInterface;
62         mSystemStateInterface = systemStateInterface;
63         mTimeInterface = timeInterface;
64         mWakeLockInterface = wakeLockInterface;
65     }
66 
getActivityManagerInterface()67     public ActivityManagerInterface getActivityManagerInterface() {
68         return mActivityManagerInterface;
69     }
getDisplayInterface()70     public DisplayInterface getDisplayInterface() { return mDisplayInterface; }
getIOInterface()71     public IOInterface getIOInterface() { return mIOInterface; }
getSystemStateInterface()72     public SystemStateInterface getSystemStateInterface() { return mSystemStateInterface; }
getTimeInterface()73     public TimeInterface getTimeInterface() { return mTimeInterface; }
getWakeLockInterface()74     public WakeLockInterface getWakeLockInterface() { return mWakeLockInterface; }
75 
76     @Override
sendBroadcastAsUser(Intent intent, UserHandle user)77     public void sendBroadcastAsUser(Intent intent, UserHandle user) {
78         mActivityManagerInterface.sendBroadcastAsUser(intent, user);
79     }
80 
81     @Override
getSystemCarDir()82     public File getSystemCarDir() {
83         return mIOInterface.getSystemCarDir();
84     }
85 
86     @Override
releaseAllWakeLocks(int displayId)87     public void releaseAllWakeLocks(int displayId) {
88         mWakeLockInterface.releaseAllWakeLocks(displayId);
89     }
90 
91     @Override
switchToPartialWakeLock(int displayId)92     public void switchToPartialWakeLock(int displayId) {
93         mWakeLockInterface.switchToPartialWakeLock(displayId);
94     }
95 
96     @Override
switchToFullWakeLock(int displayId)97     public void switchToFullWakeLock(int displayId) {
98         mWakeLockInterface.switchToFullWakeLock(displayId);
99     }
100 
101     @Override
getUptime()102     public long getUptime() {
103         return mTimeInterface.getUptime();
104     }
105 
106     @Override
getUptime(boolean includeDeepSleepTime)107     public long getUptime(boolean includeDeepSleepTime) {
108         return mTimeInterface.getUptime(includeDeepSleepTime);
109     }
110 
111     @Override
scheduleAction(Runnable r, long delayMs)112     public void scheduleAction(Runnable r, long delayMs) {
113         mTimeInterface.scheduleAction(r, delayMs);
114     }
115 
116     /**
117      * @deprecated see {@link ProcessInfo}
118      */
119     @Deprecated
120     @Override
getRunningProcesses()121     public List<ProcessInfo> getRunningProcesses() {
122         return mSystemStateInterface.getRunningProcesses();
123     }
124 
125     @Override
cancelAllActions()126     public void cancelAllActions() {
127         mTimeInterface.cancelAllActions();
128     }
129 
130     @Override
setDisplayBrightness(int brightness)131     public void setDisplayBrightness(int brightness) {
132         mDisplayInterface.setDisplayBrightness(brightness);
133     }
134 
135     @Override
setDisplayBrightness(int displayId, int brightness)136     public void setDisplayBrightness(int displayId, int brightness) {
137         mDisplayInterface.setDisplayBrightness(displayId, brightness);
138     }
139 
140     @Override
setDisplayState(int displayId, boolean on)141     public void setDisplayState(int displayId, boolean on) {
142         mDisplayInterface.setDisplayState(displayId, on);
143     }
144 
145     @Override
setAllDisplayState(boolean on)146     public void setAllDisplayState(boolean on) {
147         mDisplayInterface.setAllDisplayState(on);
148     }
149 
150     @Override
init(CarPowerManagementService carPowerManagementService, CarUserService carUserService)151     public void init(CarPowerManagementService carPowerManagementService,
152             CarUserService carUserService) {
153         mDisplayInterface.init(carPowerManagementService, carUserService);
154     }
155 
156     @Override
startDisplayStateMonitoring()157     public void startDisplayStateMonitoring() {
158         mDisplayInterface.startDisplayStateMonitoring();
159     }
160 
161     @Override
stopDisplayStateMonitoring()162     public void stopDisplayStateMonitoring() {
163         mDisplayInterface.stopDisplayStateMonitoring();
164     }
165 
166     @Override
isAnyDisplayEnabled()167     public boolean isAnyDisplayEnabled() {
168         return mDisplayInterface.isAnyDisplayEnabled();
169     }
170 
171     @Override
isDisplayEnabled(int displayId)172     public boolean isDisplayEnabled(int displayId) {
173         return mDisplayInterface.isDisplayEnabled(displayId);
174     }
175 
176     @Override
getFlashWearInformationProviders( String lifetimePath, String eolPath)177     public WearInformationProvider[] getFlashWearInformationProviders(
178             String lifetimePath, String eolPath) {
179         return mStorageMonitoringInterface.getFlashWearInformationProviders(
180                 lifetimePath, eolPath);
181     }
182 
183     @Override
getUidIoStatsProvider()184     public UidIoStatsProvider getUidIoStatsProvider() {
185         return mStorageMonitoringInterface.getUidIoStatsProvider();
186     }
187 
188     @Override
getLifetimeWriteInfoProvider()189     public LifetimeWriteInfoProvider getLifetimeWriteInfoProvider() {
190         return mStorageMonitoringInterface.getLifetimeWriteInfoProvider();
191     }
192 
193     @Override
shutdown()194     public void shutdown() {
195         mSystemStateInterface.shutdown();
196     }
197 
198     @Override
enterDeepSleep()199     public boolean enterDeepSleep() {
200         return mSystemStateInterface.enterDeepSleep();
201     }
202 
203     @Override
enterHibernation()204     public boolean enterHibernation() {
205         return mSystemStateInterface.enterHibernation();
206     }
207 
208     @Override
scheduleActionForBootCompleted(Runnable action, Duration delay)209     public void scheduleActionForBootCompleted(Runnable action, Duration delay) {
210         mSystemStateInterface.scheduleActionForBootCompleted(action, delay);
211     }
212 
213     @Override
scheduleActionForBootCompleted(Runnable action, Duration delay, Duration delayRange)214     public void scheduleActionForBootCompleted(Runnable action, Duration delay,
215             Duration delayRange) {
216         mSystemStateInterface.scheduleActionForBootCompleted(action, delay, delayRange);
217     }
218 
219     @Override
isWakeupCausedByTimer()220     public boolean isWakeupCausedByTimer() {
221         return mSystemStateInterface.isWakeupCausedByTimer();
222     }
223 
224     @Override
isSystemSupportingDeepSleep()225     public boolean isSystemSupportingDeepSleep() {
226         return mSystemStateInterface.isSystemSupportingDeepSleep();
227     }
228 
229     @Override
isSystemSupportingHibernation()230     public boolean isSystemSupportingHibernation() {
231         return mSystemStateInterface.isSystemSupportingHibernation();
232     }
233 
234     @Override
refreshDisplayBrightness()235     public void refreshDisplayBrightness() {
236         mDisplayInterface.refreshDisplayBrightness();
237     }
238 
239     @Override
refreshDisplayBrightness(int displayId)240     public void refreshDisplayBrightness(int displayId) {
241         mDisplayInterface.refreshDisplayBrightness(displayId);
242     }
243 
244     public final static class Builder {
245         private ActivityManagerInterface mActivityManagerInterface;
246         private DisplayInterface mDisplayInterface;
247         private IOInterface mIOInterface;
248         private StorageMonitoringInterface mStorageMonitoringInterface;
249         private SystemStateInterface mSystemStateInterface;
250         private TimeInterface mTimeInterface;
251         private WakeLockInterface mWakeLockInterface;
252 
Builder()253         private Builder() {}
254 
newSystemInterface()255         public static Builder newSystemInterface() {
256             return new Builder();
257         }
258 
defaultSystemInterface(Context context)259         public static Builder defaultSystemInterface(Context context) {
260             Objects.requireNonNull(context);
261             Builder builder = newSystemInterface();
262             builder.withActivityManagerInterface(new ActivityManagerInterface.DefaultImpl(context));
263             builder.withWakeLockInterface(new WakeLockInterface.DefaultImpl(context));
264             builder.withDisplayInterface(new DisplayInterface.DefaultImpl(context,
265                     builder.mWakeLockInterface));
266             builder.withIOInterface(new IOInterface.DefaultImpl());
267             builder.withStorageMonitoringInterface(new StorageMonitoringInterface.DefaultImpl());
268             builder.withSystemStateInterface(new SystemStateInterface.DefaultImpl(context));
269             return builder.withTimeInterface(new TimeInterface.DefaultImpl());
270         }
271 
fromBuilder(Builder otherBuilder)272         public static Builder fromBuilder(Builder otherBuilder) {
273             return newSystemInterface()
274                     .withActivityManagerInterface(otherBuilder.mActivityManagerInterface)
275                     .withDisplayInterface(otherBuilder.mDisplayInterface)
276                     .withIOInterface(otherBuilder.mIOInterface)
277                     .withStorageMonitoringInterface(otherBuilder.mStorageMonitoringInterface)
278                     .withSystemStateInterface(otherBuilder.mSystemStateInterface)
279                     .withTimeInterface(otherBuilder.mTimeInterface)
280                     .withWakeLockInterface(otherBuilder.mWakeLockInterface);
281         }
282 
withActivityManagerInterface(ActivityManagerInterface activityManagerInterface)283         public Builder withActivityManagerInterface(ActivityManagerInterface
284                 activityManagerInterface) {
285             mActivityManagerInterface = activityManagerInterface;
286             return this;
287         }
288 
withDisplayInterface(DisplayInterface displayInterface)289         public Builder withDisplayInterface(DisplayInterface displayInterface) {
290             mDisplayInterface = displayInterface;
291             return this;
292         }
293 
withIOInterface(IOInterface ioInterface)294         public Builder withIOInterface(IOInterface ioInterface) {
295             mIOInterface = ioInterface;
296             return this;
297         }
298 
withStorageMonitoringInterface(StorageMonitoringInterface storageMonitoringInterface)299         public Builder withStorageMonitoringInterface(StorageMonitoringInterface
300                 storageMonitoringInterface) {
301             mStorageMonitoringInterface = storageMonitoringInterface;
302             return this;
303         }
304 
withSystemStateInterface(SystemStateInterface systemStateInterface)305         public Builder withSystemStateInterface(SystemStateInterface systemStateInterface) {
306             mSystemStateInterface = systemStateInterface;
307             return this;
308         }
309 
withTimeInterface(TimeInterface timeInterface)310         public Builder withTimeInterface(TimeInterface timeInterface) {
311             mTimeInterface = timeInterface;
312             return this;
313         }
314 
withWakeLockInterface(WakeLockInterface wakeLockInterface)315         public Builder withWakeLockInterface(WakeLockInterface wakeLockInterface) {
316             mWakeLockInterface = wakeLockInterface;
317             return this;
318         }
319 
build()320         public SystemInterface build() {
321             return new SystemInterface(Objects.requireNonNull(mActivityManagerInterface),
322                     Objects.requireNonNull(mDisplayInterface),
323                     Objects.requireNonNull(mIOInterface),
324                     Objects.requireNonNull(mStorageMonitoringInterface),
325                     Objects.requireNonNull(mSystemStateInterface),
326                     Objects.requireNonNull(mTimeInterface),
327                     Objects.requireNonNull(mWakeLockInterface));
328         }
329     }
330 }
331