Home | History | Annotate | Download | only in devicepolicy
      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  */
     16 package com.android.server.devicepolicy;
     17 
     18 import android.app.IActivityManager;
     19 import android.app.NotificationManager;
     20 import android.app.PendingIntent;
     21 import android.app.backup.IBackupManager;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.pm.IPackageManager;
     25 import android.content.pm.PackageManagerInternal;
     26 import android.database.ContentObserver;
     27 import android.media.IAudioService;
     28 import android.net.IIpConnectivityMetrics;
     29 import android.net.Uri;
     30 import android.os.Bundle;
     31 import android.os.Looper;
     32 import android.os.PowerManagerInternal;
     33 import android.os.UserHandle;
     34 import android.os.UserManager;
     35 import android.os.UserManagerInternal;
     36 import android.security.KeyChain;
     37 import android.telephony.TelephonyManager;
     38 import android.util.ArrayMap;
     39 import android.util.Pair;
     40 import android.view.IWindowManager;
     41 
     42 import com.android.internal.widget.LockPatternUtils;
     43 
     44 import java.io.File;
     45 import java.io.IOException;
     46 import java.util.Map;
     47 
     48 /**
     49  * Overrides {@link #DevicePolicyManagerService} for dependency injection.
     50  */
     51 public class DevicePolicyManagerServiceTestable extends DevicePolicyManagerService {
     52     /**
     53      * Overrides {@link #Owners} for dependency injection.
     54      */
     55     public static class OwnersTestable extends Owners {
     56         public static final String LEGACY_FILE = "legacy.xml";
     57         public static final String DEVICE_OWNER_FILE = "device_owner2.xml";
     58         public static final String PROFILE_OWNER_FILE = "profile_owner.xml";
     59 
     60         private final File mLegacyFile;
     61         private final File mDeviceOwnerFile;
     62         private final File mUsersDataDir;
     63 
     64         public OwnersTestable(DpmMockContext context) {
     65             super(context.userManager, context.userManagerInternal, context.packageManagerInternal);
     66             mLegacyFile = new File(context.dataDir, LEGACY_FILE);
     67             mDeviceOwnerFile = new File(context.dataDir, DEVICE_OWNER_FILE);
     68             mUsersDataDir = new File(context.dataDir, "users");
     69         }
     70 
     71         @Override
     72         File getLegacyConfigFileWithTestOverride() {
     73             return mLegacyFile;
     74         }
     75 
     76         @Override
     77         File getDeviceOwnerFileWithTestOverride() {
     78             return mDeviceOwnerFile;
     79         }
     80 
     81         @Override
     82         File getProfileOwnerFileWithTestOverride(int userId) {
     83             final File userDir = new File(mUsersDataDir, String.valueOf(userId));
     84             return new File(userDir, PROFILE_OWNER_FILE);
     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         String settingsGlobalGetString(String name) {
    373             return context.settings.settingsGlobalGetString(name);
    374         }
    375 
    376         @Override
    377         void securityLogSetLoggingEnabledProperty(boolean enabled) {
    378             context.settings.securityLogSetLoggingEnabledProperty(enabled);
    379         }
    380 
    381         @Override
    382         boolean securityLogGetLoggingEnabledProperty() {
    383             return context.settings.securityLogGetLoggingEnabledProperty();
    384         }
    385 
    386         @Override
    387         boolean securityLogIsLoggingEnabled() {
    388             return context.settings.securityLogIsLoggingEnabled();
    389         }
    390 
    391         @Override
    392         TelephonyManager getTelephonyManager() {
    393             return context.telephonyManager;
    394         }
    395 
    396         @Override
    397         boolean isBuildDebuggable() {
    398             return context.buildMock.isDebuggable;
    399         }
    400 
    401         @Override
    402         KeyChain.KeyChainConnection keyChainBindAsUser(UserHandle user) {
    403             return context.keyChainConnection;
    404         }
    405     }
    406 }
    407