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.internal.ICarServiceHelper;
24 import com.android.car.power.CarPowerManagementService;
25 import com.android.car.procfsinspector.ProcessInfo;
26 import com.android.car.storagemonitoring.LifetimeWriteInfoProvider;
27 import com.android.car.storagemonitoring.UidIoStatsProvider;
28 import com.android.car.storagemonitoring.WearInformationProvider;
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
isDisplayEnabled()150     public boolean isDisplayEnabled() {
151         return mDisplayInterface.isDisplayEnabled();
152     }
153 
154     @Override
getFlashWearInformationProviders()155     public WearInformationProvider[] getFlashWearInformationProviders() {
156         return mStorageMonitoringInterface.getFlashWearInformationProviders();
157     }
158 
159     @Override
getUidIoStatsProvider()160     public UidIoStatsProvider getUidIoStatsProvider() {
161         return mStorageMonitoringInterface.getUidIoStatsProvider();
162     }
163 
164     @Override
getLifetimeWriteInfoProvider()165     public LifetimeWriteInfoProvider getLifetimeWriteInfoProvider() {
166         return mStorageMonitoringInterface.getLifetimeWriteInfoProvider();
167     }
168 
169     @Override
shutdown()170     public void shutdown() {
171         mSystemStateInterface.shutdown();
172     }
173 
174     @Override
enterDeepSleep()175     public boolean enterDeepSleep() {
176         return mSystemStateInterface.enterDeepSleep();
177     }
178 
179     @Override
scheduleActionForBootCompleted(Runnable action, Duration delay)180     public void scheduleActionForBootCompleted(Runnable action, Duration delay) {
181         mSystemStateInterface.scheduleActionForBootCompleted(action, delay);
182     }
183 
184     @Override
isWakeupCausedByTimer()185     public boolean isWakeupCausedByTimer() {
186         return mSystemStateInterface.isWakeupCausedByTimer();
187     }
188 
189     @Override
isSystemSupportingDeepSleep()190     public boolean isSystemSupportingDeepSleep() {
191         return mSystemStateInterface.isSystemSupportingDeepSleep();
192     }
193 
194     @Override
refreshDisplayBrightness()195     public void refreshDisplayBrightness() {
196         mDisplayInterface.refreshDisplayBrightness();
197     }
198 
199     public final static class Builder {
200         private ActivityManagerInterface mActivityManagerInterface;
201         private DisplayInterface mDisplayInterface;
202         private IOInterface mIOInterface;
203         private StorageMonitoringInterface mStorageMonitoringInterface;
204         private SystemStateInterface mSystemStateInterface;
205         private TimeInterface mTimeInterface;
206         private WakeLockInterface mWakeLockInterface;
207 
Builder()208         private Builder() {}
209 
newSystemInterface()210         public static Builder newSystemInterface() {
211             return new Builder();
212         }
213 
defaultSystemInterface(Context context)214         public static Builder defaultSystemInterface(Context context) {
215             Objects.requireNonNull(context);
216             Builder builder = newSystemInterface();
217             builder.withActivityManagerInterface(new ActivityManagerInterface.DefaultImpl(context));
218             builder.withWakeLockInterface(new WakeLockInterface.DefaultImpl(context));
219             builder.withDisplayInterface(new DisplayInterface.DefaultImpl(context,
220                     builder.mWakeLockInterface));
221             builder.withIOInterface(new IOInterface.DefaultImpl(context));
222             builder.withStorageMonitoringInterface(new StorageMonitoringInterface.DefaultImpl());
223             builder.withSystemStateInterface(new SystemStateInterface.DefaultImpl(context));
224             return builder.withTimeInterface(new TimeInterface.DefaultImpl());
225         }
226 
fromBuilder(Builder otherBuilder)227         public static Builder fromBuilder(Builder otherBuilder) {
228             return newSystemInterface()
229                     .withActivityManagerInterface(otherBuilder.mActivityManagerInterface)
230                     .withDisplayInterface(otherBuilder.mDisplayInterface)
231                     .withIOInterface(otherBuilder.mIOInterface)
232                     .withStorageMonitoringInterface(otherBuilder.mStorageMonitoringInterface)
233                     .withSystemStateInterface(otherBuilder.mSystemStateInterface)
234                     .withTimeInterface(otherBuilder.mTimeInterface)
235                     .withWakeLockInterface(otherBuilder.mWakeLockInterface);
236         }
237 
withActivityManagerInterface(ActivityManagerInterface activityManagerInterface)238         public Builder withActivityManagerInterface(ActivityManagerInterface
239                 activityManagerInterface) {
240             mActivityManagerInterface = activityManagerInterface;
241             return this;
242         }
243 
withDisplayInterface(DisplayInterface displayInterface)244         public Builder withDisplayInterface(DisplayInterface displayInterface) {
245             mDisplayInterface = displayInterface;
246             return this;
247         }
248 
withIOInterface(IOInterface ioInterface)249         public Builder withIOInterface(IOInterface ioInterface) {
250             mIOInterface = ioInterface;
251             return this;
252         }
253 
withStorageMonitoringInterface(StorageMonitoringInterface storageMonitoringInterface)254         public Builder withStorageMonitoringInterface(StorageMonitoringInterface
255                 storageMonitoringInterface) {
256             mStorageMonitoringInterface = storageMonitoringInterface;
257             return this;
258         }
259 
withSystemStateInterface(SystemStateInterface systemStateInterface)260         public Builder withSystemStateInterface(SystemStateInterface systemStateInterface) {
261             mSystemStateInterface = systemStateInterface;
262             return this;
263         }
264 
withTimeInterface(TimeInterface timeInterface)265         public Builder withTimeInterface(TimeInterface timeInterface) {
266             mTimeInterface = timeInterface;
267             return this;
268         }
269 
withWakeLockInterface(WakeLockInterface wakeLockInterface)270         public Builder withWakeLockInterface(WakeLockInterface wakeLockInterface) {
271             mWakeLockInterface = wakeLockInterface;
272             return this;
273         }
274 
build()275         public SystemInterface build() {
276             return new SystemInterface(Objects.requireNonNull(mActivityManagerInterface),
277                 Objects.requireNonNull(mDisplayInterface),
278                 Objects.requireNonNull(mIOInterface),
279                 Objects.requireNonNull(mStorageMonitoringInterface),
280                 Objects.requireNonNull(mSystemStateInterface),
281                 Objects.requireNonNull(mTimeInterface),
282                 Objects.requireNonNull(mWakeLockInterface));
283         }
284     }
285 }
286