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()87 public void releaseAllWakeLocks() { 88 mWakeLockInterface.releaseAllWakeLocks(); 89 } 90 91 @Override switchToPartialWakeLock()92 public void switchToPartialWakeLock() { 93 mWakeLockInterface.switchToPartialWakeLock(); 94 } 95 96 @Override switchToFullWakeLock()97 public void switchToFullWakeLock() { 98 mWakeLockInterface.switchToFullWakeLock(); 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 setDisplayState(boolean on)136 public void setDisplayState(boolean on) { 137 mDisplayInterface.setDisplayState(on); 138 } 139 140 @Override init(CarPowerManagementService carPowerManagementService, CarUserService carUserService)141 public void init(CarPowerManagementService carPowerManagementService, 142 CarUserService carUserService) { 143 mDisplayInterface.init(carPowerManagementService, carUserService); 144 } 145 146 @Override startDisplayStateMonitoring()147 public void startDisplayStateMonitoring() { 148 mDisplayInterface.startDisplayStateMonitoring(); 149 } 150 151 @Override stopDisplayStateMonitoring()152 public void stopDisplayStateMonitoring() { 153 mDisplayInterface.stopDisplayStateMonitoring(); 154 } 155 156 @Override isDisplayEnabled()157 public boolean isDisplayEnabled() { 158 return mDisplayInterface.isDisplayEnabled(); 159 } 160 161 @Override getFlashWearInformationProviders( String lifetimePath, String eolPath)162 public WearInformationProvider[] getFlashWearInformationProviders( 163 String lifetimePath, String eolPath) { 164 return mStorageMonitoringInterface.getFlashWearInformationProviders( 165 lifetimePath, eolPath); 166 } 167 168 @Override getUidIoStatsProvider()169 public UidIoStatsProvider getUidIoStatsProvider() { 170 return mStorageMonitoringInterface.getUidIoStatsProvider(); 171 } 172 173 @Override getLifetimeWriteInfoProvider()174 public LifetimeWriteInfoProvider getLifetimeWriteInfoProvider() { 175 return mStorageMonitoringInterface.getLifetimeWriteInfoProvider(); 176 } 177 178 @Override shutdown()179 public void shutdown() { 180 mSystemStateInterface.shutdown(); 181 } 182 183 @Override enterDeepSleep()184 public boolean enterDeepSleep() { 185 return mSystemStateInterface.enterDeepSleep(); 186 } 187 188 @Override enterHibernation()189 public boolean enterHibernation() { 190 return mSystemStateInterface.enterHibernation(); 191 } 192 193 @Override scheduleActionForBootCompleted(Runnable action, Duration delay)194 public void scheduleActionForBootCompleted(Runnable action, Duration delay) { 195 mSystemStateInterface.scheduleActionForBootCompleted(action, delay); 196 } 197 198 @Override isWakeupCausedByTimer()199 public boolean isWakeupCausedByTimer() { 200 return mSystemStateInterface.isWakeupCausedByTimer(); 201 } 202 203 @Override isSystemSupportingDeepSleep()204 public boolean isSystemSupportingDeepSleep() { 205 return mSystemStateInterface.isSystemSupportingDeepSleep(); 206 } 207 208 @Override isSystemSupportingHibernation()209 public boolean isSystemSupportingHibernation() { 210 return mSystemStateInterface.isSystemSupportingHibernation(); 211 } 212 213 @Override refreshDisplayBrightness()214 public void refreshDisplayBrightness() { 215 mDisplayInterface.refreshDisplayBrightness(); 216 } 217 218 public final static class Builder { 219 private ActivityManagerInterface mActivityManagerInterface; 220 private DisplayInterface mDisplayInterface; 221 private IOInterface mIOInterface; 222 private StorageMonitoringInterface mStorageMonitoringInterface; 223 private SystemStateInterface mSystemStateInterface; 224 private TimeInterface mTimeInterface; 225 private WakeLockInterface mWakeLockInterface; 226 Builder()227 private Builder() {} 228 newSystemInterface()229 public static Builder newSystemInterface() { 230 return new Builder(); 231 } 232 defaultSystemInterface(Context context)233 public static Builder defaultSystemInterface(Context context) { 234 Objects.requireNonNull(context); 235 Builder builder = newSystemInterface(); 236 builder.withActivityManagerInterface(new ActivityManagerInterface.DefaultImpl(context)); 237 builder.withWakeLockInterface(new WakeLockInterface.DefaultImpl(context)); 238 builder.withDisplayInterface(new DisplayInterface.DefaultImpl(context, 239 builder.mWakeLockInterface)); 240 builder.withIOInterface(new IOInterface.DefaultImpl(context)); 241 builder.withStorageMonitoringInterface(new StorageMonitoringInterface.DefaultImpl()); 242 builder.withSystemStateInterface(new SystemStateInterface.DefaultImpl(context)); 243 return builder.withTimeInterface(new TimeInterface.DefaultImpl()); 244 } 245 fromBuilder(Builder otherBuilder)246 public static Builder fromBuilder(Builder otherBuilder) { 247 return newSystemInterface() 248 .withActivityManagerInterface(otherBuilder.mActivityManagerInterface) 249 .withDisplayInterface(otherBuilder.mDisplayInterface) 250 .withIOInterface(otherBuilder.mIOInterface) 251 .withStorageMonitoringInterface(otherBuilder.mStorageMonitoringInterface) 252 .withSystemStateInterface(otherBuilder.mSystemStateInterface) 253 .withTimeInterface(otherBuilder.mTimeInterface) 254 .withWakeLockInterface(otherBuilder.mWakeLockInterface); 255 } 256 withActivityManagerInterface(ActivityManagerInterface activityManagerInterface)257 public Builder withActivityManagerInterface(ActivityManagerInterface 258 activityManagerInterface) { 259 mActivityManagerInterface = activityManagerInterface; 260 return this; 261 } 262 withDisplayInterface(DisplayInterface displayInterface)263 public Builder withDisplayInterface(DisplayInterface displayInterface) { 264 mDisplayInterface = displayInterface; 265 return this; 266 } 267 withIOInterface(IOInterface ioInterface)268 public Builder withIOInterface(IOInterface ioInterface) { 269 mIOInterface = ioInterface; 270 return this; 271 } 272 withStorageMonitoringInterface(StorageMonitoringInterface storageMonitoringInterface)273 public Builder withStorageMonitoringInterface(StorageMonitoringInterface 274 storageMonitoringInterface) { 275 mStorageMonitoringInterface = storageMonitoringInterface; 276 return this; 277 } 278 withSystemStateInterface(SystemStateInterface systemStateInterface)279 public Builder withSystemStateInterface(SystemStateInterface systemStateInterface) { 280 mSystemStateInterface = systemStateInterface; 281 return this; 282 } 283 withTimeInterface(TimeInterface timeInterface)284 public Builder withTimeInterface(TimeInterface timeInterface) { 285 mTimeInterface = timeInterface; 286 return this; 287 } 288 withWakeLockInterface(WakeLockInterface wakeLockInterface)289 public Builder withWakeLockInterface(WakeLockInterface wakeLockInterface) { 290 mWakeLockInterface = wakeLockInterface; 291 return this; 292 } 293 build()294 public SystemInterface build() { 295 return new SystemInterface(Objects.requireNonNull(mActivityManagerInterface), 296 Objects.requireNonNull(mDisplayInterface), 297 Objects.requireNonNull(mIOInterface), 298 Objects.requireNonNull(mStorageMonitoringInterface), 299 Objects.requireNonNull(mSystemStateInterface), 300 Objects.requireNonNull(mTimeInterface), 301 Objects.requireNonNull(mWakeLockInterface)); 302 } 303 } 304 } 305