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