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 isWakeupCausedByTimer()214 public boolean isWakeupCausedByTimer() { 215 return mSystemStateInterface.isWakeupCausedByTimer(); 216 } 217 218 @Override isSystemSupportingDeepSleep()219 public boolean isSystemSupportingDeepSleep() { 220 return mSystemStateInterface.isSystemSupportingDeepSleep(); 221 } 222 223 @Override isSystemSupportingHibernation()224 public boolean isSystemSupportingHibernation() { 225 return mSystemStateInterface.isSystemSupportingHibernation(); 226 } 227 228 @Override refreshDisplayBrightness()229 public void refreshDisplayBrightness() { 230 mDisplayInterface.refreshDisplayBrightness(); 231 } 232 233 @Override refreshDisplayBrightness(int displayId)234 public void refreshDisplayBrightness(int displayId) { 235 mDisplayInterface.refreshDisplayBrightness(displayId); 236 } 237 238 public final static class Builder { 239 private ActivityManagerInterface mActivityManagerInterface; 240 private DisplayInterface mDisplayInterface; 241 private IOInterface mIOInterface; 242 private StorageMonitoringInterface mStorageMonitoringInterface; 243 private SystemStateInterface mSystemStateInterface; 244 private TimeInterface mTimeInterface; 245 private WakeLockInterface mWakeLockInterface; 246 Builder()247 private Builder() {} 248 newSystemInterface()249 public static Builder newSystemInterface() { 250 return new Builder(); 251 } 252 defaultSystemInterface(Context context)253 public static Builder defaultSystemInterface(Context context) { 254 Objects.requireNonNull(context); 255 Builder builder = newSystemInterface(); 256 builder.withActivityManagerInterface(new ActivityManagerInterface.DefaultImpl(context)); 257 builder.withWakeLockInterface(new WakeLockInterface.DefaultImpl(context)); 258 builder.withDisplayInterface(new DisplayInterface.DefaultImpl(context, 259 builder.mWakeLockInterface)); 260 builder.withIOInterface(new IOInterface.DefaultImpl()); 261 builder.withStorageMonitoringInterface(new StorageMonitoringInterface.DefaultImpl()); 262 builder.withSystemStateInterface(new SystemStateInterface.DefaultImpl(context)); 263 return builder.withTimeInterface(new TimeInterface.DefaultImpl()); 264 } 265 fromBuilder(Builder otherBuilder)266 public static Builder fromBuilder(Builder otherBuilder) { 267 return newSystemInterface() 268 .withActivityManagerInterface(otherBuilder.mActivityManagerInterface) 269 .withDisplayInterface(otherBuilder.mDisplayInterface) 270 .withIOInterface(otherBuilder.mIOInterface) 271 .withStorageMonitoringInterface(otherBuilder.mStorageMonitoringInterface) 272 .withSystemStateInterface(otherBuilder.mSystemStateInterface) 273 .withTimeInterface(otherBuilder.mTimeInterface) 274 .withWakeLockInterface(otherBuilder.mWakeLockInterface); 275 } 276 withActivityManagerInterface(ActivityManagerInterface activityManagerInterface)277 public Builder withActivityManagerInterface(ActivityManagerInterface 278 activityManagerInterface) { 279 mActivityManagerInterface = activityManagerInterface; 280 return this; 281 } 282 withDisplayInterface(DisplayInterface displayInterface)283 public Builder withDisplayInterface(DisplayInterface displayInterface) { 284 mDisplayInterface = displayInterface; 285 return this; 286 } 287 withIOInterface(IOInterface ioInterface)288 public Builder withIOInterface(IOInterface ioInterface) { 289 mIOInterface = ioInterface; 290 return this; 291 } 292 withStorageMonitoringInterface(StorageMonitoringInterface storageMonitoringInterface)293 public Builder withStorageMonitoringInterface(StorageMonitoringInterface 294 storageMonitoringInterface) { 295 mStorageMonitoringInterface = storageMonitoringInterface; 296 return this; 297 } 298 withSystemStateInterface(SystemStateInterface systemStateInterface)299 public Builder withSystemStateInterface(SystemStateInterface systemStateInterface) { 300 mSystemStateInterface = systemStateInterface; 301 return this; 302 } 303 withTimeInterface(TimeInterface timeInterface)304 public Builder withTimeInterface(TimeInterface timeInterface) { 305 mTimeInterface = timeInterface; 306 return this; 307 } 308 withWakeLockInterface(WakeLockInterface wakeLockInterface)309 public Builder withWakeLockInterface(WakeLockInterface wakeLockInterface) { 310 mWakeLockInterface = wakeLockInterface; 311 return this; 312 } 313 build()314 public SystemInterface build() { 315 return new SystemInterface(Objects.requireNonNull(mActivityManagerInterface), 316 Objects.requireNonNull(mDisplayInterface), 317 Objects.requireNonNull(mIOInterface), 318 Objects.requireNonNull(mStorageMonitoringInterface), 319 Objects.requireNonNull(mSystemStateInterface), 320 Objects.requireNonNull(mTimeInterface), 321 Objects.requireNonNull(mWakeLockInterface)); 322 } 323 } 324 } 325