1 /* 2 * Copyright (C) 2020 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.managedprovisioning.ota; 17 18 import static android.os.Build.VERSION_CODES.Q; 19 import static android.os.Build.VERSION_CODES.R; 20 21 import android.annotation.Nullable; 22 import android.annotation.SuppressLint; 23 import android.app.ApplicationPackageManager; 24 import android.app.KeyguardManager; 25 import android.app.admin.DeviceAdminReceiver; 26 import android.app.admin.DevicePolicyManager; 27 import android.app.admin.IDevicePolicyManager; 28 import android.content.ComponentName; 29 import android.content.Context; 30 import android.content.IntentFilter; 31 import android.content.pm.PackageInfo; 32 import android.content.pm.PackageManager; 33 import android.content.pm.PackageManager.NameNotFoundException; 34 import android.os.Build; 35 import android.os.Build.VERSION_CODES; 36 import android.os.Bundle; 37 import android.os.Handler; 38 import android.os.Process; 39 import android.text.TextUtils; 40 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.Collection; 44 import java.util.Collections; 45 import java.util.HashMap; 46 import java.util.HashSet; 47 import java.util.List; 48 import java.util.Map; 49 import java.util.Set; 50 import org.robolectric.RuntimeEnvironment; 51 import org.robolectric.annotation.Implementation; 52 import org.robolectric.annotation.Implements; 53 import org.robolectric.annotation.RealObject; 54 import org.robolectric.shadow.api.Shadow; 55 import org.robolectric.shadows.ShadowDevicePolicyManager; 56 57 58 @Implements(DevicePolicyManager.class) 59 @SuppressLint("NewApi") 60 public class ExtendsShadowDevicePolicyManager extends ShadowDevicePolicyManager { 61 62 private Set<String> defaultCrossProfilePackages = new HashSet<>(); 63 private Set<String> crossProfileCalendarPackages = Collections.emptySet(); 64 enforceProfileOwner(ComponentName admin)65 private void enforceProfileOwner(ComponentName admin) { 66 if (!admin.equals(getProfileOwner())) { 67 throw new SecurityException("[" + admin + "] is not a profile owner"); 68 } 69 } 70 71 // BEGIN-INTERNAL 72 @Implementation(minSdk = Q) getCrossProfileCalendarPackages()73 protected Set<String> getCrossProfileCalendarPackages() { 74 return crossProfileCalendarPackages; 75 } 76 77 @Implementation(minSdk = Q) setCrossProfileCalendarPackages(ComponentName admin, Set<String> packageNames)78 public void setCrossProfileCalendarPackages(ComponentName admin, Set<String> packageNames) { 79 enforceProfileOwner(admin); 80 crossProfileCalendarPackages = packageNames; 81 } 82 83 @Implementation(minSdk = R) getDefaultCrossProfilePackages()84 protected Set<String> getDefaultCrossProfilePackages() { 85 return new HashSet<>(defaultCrossProfilePackages); 86 } 87 addDefaultCrossProfilePackage(String packageName)88 public void addDefaultCrossProfilePackage(String packageName) { 89 defaultCrossProfilePackages.add(packageName); 90 } 91 setDefaultCrossProfilePackages(Set<String> defaultCrossProfilePackages)92 public void setDefaultCrossProfilePackages(Set<String> defaultCrossProfilePackages) { 93 this.defaultCrossProfilePackages = new HashSet<>(defaultCrossProfilePackages); 94 } 95 96 } 97