• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 
17 package com.android.settings.testutils.shadow;
18 
19 import android.Manifest;
20 import android.content.Context;
21 import android.content.pm.CrossProfileApps;
22 import android.content.pm.PackageManager;
23 import android.content.pm.PackageInfo;
24 
25 import androidx.annotation.NonNull;
26 
27 import org.robolectric.annotation.Implementation;
28 import org.robolectric.annotation.Implements;
29 
30 import java.util.Arrays;
31 import java.util.HashSet;
32 import java.util.Set;
33 
34 @Implements(CrossProfileApps.class)
35 public class ShadowCrossProfileApps extends org.robolectric.shadows.ShadowCrossProfileApps {
36     private static final Set<String> configurableInteractAcrossProfilePackages = new HashSet<>();
37 
addCrossProfilePackage(String packageName)38     public void addCrossProfilePackage(String packageName) {
39         configurableInteractAcrossProfilePackages.add(packageName);
40     }
41 
42     @Implementation
canConfigureInteractAcrossProfiles(@onNull String packageName)43     protected boolean canConfigureInteractAcrossProfiles(@NonNull String packageName) {
44         return configurableInteractAcrossProfilePackages.contains(packageName);
45     }
46 
47     @Implementation
canUserAttemptToConfigureInteractAcrossProfiles(@onNull String packageName)48     protected boolean canUserAttemptToConfigureInteractAcrossProfiles(@NonNull String packageName) {
49         PackageInfo packageInfo;
50         try {
51             packageInfo = getContext().getPackageManager().getPackageInfo(
52                 packageName,
53                 /* flags= */ 0);
54         } catch (PackageManager.NameNotFoundException e) {
55             return false;
56         }
57         if (packageInfo == null || packageInfo.requestedPermissions == null) {
58             return false;
59         }
60         return Arrays.asList(packageInfo.requestedPermissions).contains(
61                 Manifest.permission.INTERACT_ACROSS_PROFILES);
62     }
63 }
64