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