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