DevicePolicyManagerServiceTestable.java revision b88f42b6b1414ae8107be13c0d2f3957549ecaaf
1/* 2 * Copyright (C) 2015 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 */ 16package com.android.server.devicepolicy; 17 18import android.app.IActivityManager; 19import android.app.NotificationManager; 20import android.app.backup.IBackupManager; 21import android.content.pm.IPackageManager; 22import android.content.pm.PackageManagerInternal; 23import android.database.ContentObserver; 24import android.media.IAudioService; 25import android.net.IIpConnectivityMetrics; 26import android.net.Uri; 27import android.os.Looper; 28import android.os.PowerManagerInternal; 29import android.os.UserHandle; 30import android.os.UserManager; 31import android.os.UserManagerInternal; 32import android.telephony.TelephonyManager; 33import android.util.ArrayMap; 34import android.util.Pair; 35import android.view.IWindowManager; 36 37import com.android.internal.widget.LockPatternUtils; 38 39import java.io.File; 40import java.io.IOException; 41import java.util.Map; 42 43/** 44 * Overrides {@link #DevicePolicyManagerService} for dependency injection. 45 */ 46public class DevicePolicyManagerServiceTestable extends DevicePolicyManagerService { 47 /** 48 * Overrides {@link #Owners} for dependency injection. 49 */ 50 public static class OwnersTestable extends Owners { 51 public static final String LEGACY_FILE = "legacy.xml"; 52 public static final String DEVICE_OWNER_FILE = "device_owner2.xml"; 53 public static final String PROFILE_OWNER_FILE_BASE = "profile_owner.xml"; 54 55 private final File mLegacyFile; 56 private final File mDeviceOwnerFile; 57 private final File mProfileOwnerBase; 58 59 public OwnersTestable(DpmMockContext context) { 60 super(context.userManager, context.userManagerInternal, context.packageManagerInternal); 61 mLegacyFile = new File(context.dataDir, LEGACY_FILE); 62 mDeviceOwnerFile = new File(context.dataDir, DEVICE_OWNER_FILE); 63 mProfileOwnerBase = new File(context.dataDir, PROFILE_OWNER_FILE_BASE); 64 } 65 66 @Override 67 File getLegacyConfigFileWithTestOverride() { 68 return mLegacyFile; 69 } 70 71 @Override 72 File getDeviceOwnerFileWithTestOverride() { 73 return mDeviceOwnerFile; 74 } 75 76 @Override 77 File getProfileOwnerFileWithTestOverride(int userId) { 78 return new File(mDeviceOwnerFile.getAbsoluteFile() + "-" + userId); 79 } 80 } 81 82 public final DpmMockContext context; 83 private final MockInjector mMockInjector; 84 85 public DevicePolicyManagerServiceTestable(DpmMockContext context, File dataDir) { 86 this(new MockInjector(context, dataDir)); 87 } 88 89 private DevicePolicyManagerServiceTestable(MockInjector injector) { 90 super(injector); 91 mMockInjector = injector; 92 this.context = injector.context; 93 } 94 95 96 public void notifyChangeToContentObserver(Uri uri, int userHandle) { 97 ContentObserver co = mMockInjector.mContentObservers 98 .get(new Pair<Uri, Integer>(uri, userHandle)); 99 if (co != null) { 100 co.onChange(false, uri, userHandle); // notify synchronously 101 } 102 103 // Notify USER_ALL observer too. 104 co = mMockInjector.mContentObservers 105 .get(new Pair<Uri, Integer>(uri, UserHandle.USER_ALL)); 106 if (co != null) { 107 co.onChange(false, uri, userHandle); // notify synchronously 108 } 109 } 110 111 112 private static class MockInjector extends Injector { 113 114 public final DpmMockContext context; 115 116 public final File dataDir; 117 118 // Key is a pair of uri and userId 119 private final Map<Pair<Uri, Integer>, ContentObserver> mContentObservers = new ArrayMap<>(); 120 121 private MockInjector(DpmMockContext context, File dataDir) { 122 super(context); 123 this.context = context; 124 this.dataDir = dataDir; 125 } 126 127 @Override 128 Owners newOwners() { 129 return new OwnersTestable(context); 130 } 131 132 @Override 133 UserManager getUserManager() { 134 return context.userManager; 135 } 136 137 @Override 138 UserManagerInternal getUserManagerInternal() { 139 return context.userManagerInternal; 140 } 141 142 @Override 143 PackageManagerInternal getPackageManagerInternal() { 144 return context.packageManagerInternal; 145 } 146 147 @Override 148 PowerManagerInternal getPowerManagerInternal() { 149 return context.powerManagerInternal; 150 } 151 152 @Override 153 NotificationManager getNotificationManager() { 154 return context.notificationManager; 155 } 156 157 @Override 158 IIpConnectivityMetrics getIIpConnectivityMetrics() { 159 return context.iipConnectivityMetrics; 160 } 161 162 @Override 163 IWindowManager getIWindowManager() { 164 return context.iwindowManager; 165 } 166 167 @Override 168 IActivityManager getIActivityManager() { 169 return context.iactivityManager; 170 } 171 172 @Override 173 IPackageManager getIPackageManager() { 174 return context.ipackageManager; 175 } 176 177 @Override 178 IBackupManager getIBackupManager() { 179 return context.ibackupManager; 180 } 181 182 @Override 183 IAudioService getIAudioService() { 184 return context.iaudioService; 185 } 186 187 @Override 188 Looper getMyLooper() { 189 return Looper.getMainLooper(); 190 } 191 192 @Override 193 LockPatternUtils newLockPatternUtils() { 194 return context.lockPatternUtils; 195 } 196 197 @Override 198 boolean storageManagerIsFileBasedEncryptionEnabled() { 199 return context.storageManager.isFileBasedEncryptionEnabled(); 200 } 201 202 @Override 203 boolean storageManagerIsNonDefaultBlockEncrypted() { 204 return context.storageManager.isNonDefaultBlockEncrypted(); 205 } 206 207 @Override 208 boolean storageManagerIsEncrypted() { 209 return context.storageManager.isEncrypted(); 210 } 211 212 @Override 213 boolean storageManagerIsEncryptable() { 214 return context.storageManager.isEncryptable(); 215 } 216 217 @Override 218 String getDevicePolicyFilePathForSystemUser() { 219 return context.systemUserDataDir.getAbsolutePath() + "/"; 220 } 221 222 @Override 223 long binderClearCallingIdentity() { 224 return context.binder.clearCallingIdentity(); 225 } 226 227 @Override 228 void binderRestoreCallingIdentity(long token) { 229 context.binder.restoreCallingIdentity(token); 230 } 231 232 @Override 233 int binderGetCallingUid() { 234 return context.binder.getCallingUid(); 235 } 236 237 @Override 238 int binderGetCallingPid() { 239 return context.binder.getCallingPid(); 240 } 241 242 @Override 243 UserHandle binderGetCallingUserHandle() { 244 return context.binder.getCallingUserHandle(); 245 } 246 247 @Override 248 boolean binderIsCallingUidMyUid() { 249 return context.binder.isCallerUidMyUid(); 250 } 251 252 @Override 253 File environmentGetUserSystemDirectory(int userId) { 254 return context.environment.getUserSystemDirectory(userId); 255 } 256 257 @Override 258 void powerManagerGoToSleep(long time, int reason, int flags) { 259 context.powerManager.goToSleep(time, reason, flags); 260 } 261 262 @Override 263 void powerManagerReboot(String reason) { 264 context.powerManager.reboot(reason); 265 } 266 267 @Override 268 void recoverySystemRebootWipeUserData(boolean shutdown, String reason, boolean force) 269 throws IOException { 270 context.recoverySystem.rebootWipeUserData(shutdown, reason, force); 271 } 272 273 @Override 274 boolean systemPropertiesGetBoolean(String key, boolean def) { 275 return context.systemProperties.getBoolean(key, def); 276 } 277 278 @Override 279 long systemPropertiesGetLong(String key, long def) { 280 return context.systemProperties.getLong(key, def); 281 } 282 283 @Override 284 String systemPropertiesGet(String key, String def) { 285 return context.systemProperties.get(key, def); 286 } 287 288 @Override 289 String systemPropertiesGet(String key) { 290 return context.systemProperties.get(key); 291 } 292 293 @Override 294 void systemPropertiesSet(String key, String value) { 295 context.systemProperties.set(key, value); 296 } 297 298 @Override 299 boolean userManagerIsSplitSystemUser() { 300 return context.userManagerForMock.isSplitSystemUser(); 301 } 302 303 @Override 304 void registerContentObserver(Uri uri, boolean notifyForDescendents, 305 ContentObserver observer, int userHandle) { 306 mContentObservers.put(new Pair<Uri, Integer>(uri, userHandle), observer); 307 } 308 309 @Override 310 int settingsSecureGetIntForUser(String name, int def, int userHandle) { 311 return context.settings.settingsSecureGetIntForUser(name, def, userHandle); 312 } 313 314 @Override 315 void settingsSecurePutIntForUser(String name, int value, int userHandle) { 316 context.settings.settingsSecurePutIntForUser(name, value, userHandle); 317 } 318 319 @Override 320 void settingsSecurePutStringForUser(String name, String value, int userHandle) { 321 context.settings.settingsSecurePutStringForUser(name, value, userHandle); 322 } 323 324 @Override 325 void settingsGlobalPutStringForUser(String name, String value, int userHandle) { 326 context.settings.settingsGlobalPutStringForUser(name, value, userHandle); 327 } 328 329 @Override 330 void settingsSecurePutInt(String name, int value) { 331 context.settings.settingsSecurePutInt(name, value); 332 } 333 334 @Override 335 void settingsGlobalPutInt(String name, int value) { 336 context.settings.settingsGlobalPutInt(name, value); 337 } 338 339 @Override 340 void settingsSecurePutString(String name, String value) { 341 context.settings.settingsSecurePutString(name, value); 342 } 343 344 @Override 345 void settingsGlobalPutString(String name, String value) { 346 context.settings.settingsGlobalPutString(name, value); 347 } 348 349 @Override 350 int settingsGlobalGetInt(String name, int def) { 351 return context.settings.settingsGlobalGetInt(name, def); 352 } 353 354 @Override 355 void securityLogSetLoggingEnabledProperty(boolean enabled) { 356 context.settings.securityLogSetLoggingEnabledProperty(enabled); 357 } 358 359 @Override 360 boolean securityLogGetLoggingEnabledProperty() { 361 return context.settings.securityLogGetLoggingEnabledProperty(); 362 } 363 364 @Override 365 boolean securityLogIsLoggingEnabled() { 366 return context.settings.securityLogIsLoggingEnabled(); 367 } 368 369 @Override 370 TelephonyManager getTelephonyManager() { 371 return context.telephonyManager; 372 } 373 374 @Override 375 boolean isBuildDebuggable() { 376 return context.buildMock.isDebuggable; 377 } 378 } 379} 380