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