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